Skip to content

Commit

Permalink
Item2321: add GET webs.json
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.foswiki.org/trunk/RestPlugin@10157 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
SvenDowideit authored and SvenDowideit committed Dec 2, 2010
1 parent a31dc1e commit b37d1d9
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 16 deletions.
62 changes: 47 additions & 15 deletions lib/Foswiki/UI/Query.pm
Expand Up @@ -197,23 +197,29 @@ sub query {
}

}
elsif (($query eq '') and ($elementAlias eq 'webs')) {
#we're getting all the webs..
$web = '';
$topic = undef;
$baseObjectExists = 1;
}
else {
die 'not implemented';
die 'not implemented ('.$query.')';
}

#need to test if this topic exists, as Meta->new currently returns an obj, even if the web, or the topic don't exist. totally yuck.
#TODO: note that if we're PUT-ing and the item does not exist, we're basically POSTing, but to a static URI, not to a collection.
if ( not $baseObjectExists ) {
$res->header( -type => 'text/html', -status => '404' );
$err =
"ERROR: (401) Invalid query invocation - web or topic do not exist ($web . $topic)";
"ERROR: (404) Invalid query invocation - web or topic do not exist ($web . $topic)";
$res->print($err);
throw Foswiki::EngineException( 404, $err, $res );
}
my $topicObject = Foswiki::Meta->new( $session, $web, $topic );
print STDERR "---- actual Meta ("
print STDERR "---- new($web, ". ( $topic || '>UNDEF<' ) . ") == actual Meta ("
. $topicObject->web . ", "
. ( $topicObject->topic || '' ) . ")\n";
. ( $topicObject->topic || '>UNDEF<' ) . ")\n";

#TODO: this will need ammending when we actually query, as we don't know what topics we're talking about at this point.
my $accessType = 'CHANGE';
Expand Down Expand Up @@ -246,18 +252,32 @@ sub query {
#DOIT
my $result;
try {
my $evalParser = new Foswiki::Query::Parser();
my $querytxt = $query;
$querytxt =~ s/(webs|topic)$/hash/;
print STDERR "~~~~~~~~~~~~~~~~~~~~~~~$querytxt\n";
my $node = $evalParser->parse($querytxt);

#time it.
my $startTime = [Time::HiRes::gettimeofday];
$result = $node->evaluate( tom => $topicObject, data => $topicObject );
if ( $request_method eq 'GET' ) {

#it just gets..
if ( $request_method eq 'GET' ) {
#TODO: the query language currently presumes that the LHS of / isa topic
if ($elementAlias eq 'topic') {
my $evalParser = new Foswiki::Query::Parser();
my $querytxt = $query;
$querytxt =~ s/(topic)$/hash/;
print STDERR "~~~~~~~~~~~~~~~~~~~~~~~topic: use query evaluate $querytxt\n";
my $node = $evalParser->parse($querytxt);

$result = $node->evaluate( tom => $topicObject, data => $topicObject );
} elsif ($elementAlias eq 'webs') {
#TODO: get all subwebs of LHS - so ''/webs == /webs == all webs recursive.
#TODO: consider filter of Func::getListOfWebs( $filter [, $web] )
my $filter = '';
my @webs = Foswiki::Func::getListOfWebs( $filter, $web );
unshift(@webs, $web) if ($web ne '');
my @results = map {
my $m = Foswiki::Meta->load( $Foswiki::Plugins::SESSION, $_ );
print STDERR "::::: load($_) == ".$m->web."\n";
$m
} @webs;
$result = \@results;
}
}
elsif ( $request_method eq 'PUT' ) {
die 'not implemented';
Expand All @@ -273,6 +293,7 @@ sub query {
$topicObject->text( $value->{_text} )
if ( defined( $value->{_text} ) );
$topicObject->save();
$result = $topicObject;
}
elsif ( $request_method eq 'POST' ) {
ASSERT( $requestPayload ne '' ) if DEBUG;
Expand All @@ -295,6 +316,8 @@ sub query {
$topicObject->text( $value->{_text} )
if ( defined( $value->{_text} ) );
$topicObject->save();

$result = $topicObject;

#if we created something and are returning it, and a uri for it, status=201
#need a location header
Expand All @@ -314,8 +337,17 @@ sub query {
#throw something - this should have been noticed before
die 'not implemented';
}
if ( $result->isa('Foswiki::Meta') ) {
$result = Foswiki::Serialise::convertMeta($result);
#might be an array of Meta's
#TODO: should the reply _always_ be an array?
print STDERR "------------------ ref(result): ".ref($result)."\n";
if (ref($result) eq 'ARRAY') {
for (my $i=0; $i<scalar(@$result);$i++) {
$result->[$i] = Foswiki::Serialise::convertMeta($result->[$i]);
}
} else {
if ( $result->isa('Foswiki::Meta') ) {
$result = Foswiki::Serialise::convertMeta($result);
}
}

#TODO: the elementAlias and query != what was requested - it should be what is returned (for eg, POST is the item, not the container
Expand Down
75 changes: 74 additions & 1 deletion test/unit/RestPlugin/RestPluginTests.pm
Expand Up @@ -79,7 +79,7 @@ print STDERR "=-=- the user running the UI: ".$this->{test_user_login}."\n";
return ( $text, $1 );
}

sub testGET {
sub testGET_topic {
my $this = shift;

{
Expand Down Expand Up @@ -108,6 +108,79 @@ sub testGET {
Foswiki::Serialise::convertMeta($meta) );
}
}
sub testGET_webs {
my $this = shift;
##WEB
{
my ( $meta, $text ) =
Foswiki::Func::readTopic( 'System' );

my ( $replytext, $hdr ) = $this->call_UI_query(
'/' . 'System' . '/webs.json',
'GET', {} );
my $fromJSON = JSON::from_json( $replytext, { allow_nonref => 1 } );
$this->assert_deep_equals( $fromJSON,
[Foswiki::Serialise::convertMeta($meta)] );
}
{
my ( $meta, $text ) =
Foswiki::Func::readTopic( $this->{test_web} );

my ( $replytext, $hdr ) = $this->call_UI_query(
'/' . $this->{test_web} . '/webs.json',
'GET', {} );
my $fromJSON = JSON::from_json( $replytext, { allow_nonref => 1 } );
$this->assert_deep_equals( $fromJSON,
[Foswiki::Serialise::convertMeta($meta)] );
}
}
#TODO: catching an exception inside a capture - gotta find out how to doit.
sub TODOtestGET_webs_doesnotexist {
my $this = shift;
#TODO: does not exist
{
my ( $meta, $text ) =
Foswiki::Func::readTopic( 'SystemDoesNotExist' );

try {
my ( $replytext, $hdr ) = $this->call_UI_query(
'/' . 'SystemDoesNotExist' . '/webs.json',
'GET', {} );
my $fromJSON = JSON::from_json( $replytext, { allow_nonref => 1 } );
$this->assert_deep_equals( $fromJSON,
[Foswiki::Serialise::convertMeta($meta)] );
} catch Foswiki::EngineException with {
my $e = shift;
my $result = $e->{-text};
#$res->status( '500 ' . $result );
print STDERR "******************($result)\n";
}
}
}
sub testGET_allwebs {
my $this = shift;
{
#get all webs..
my ( $meta, $text ) =
Foswiki::Func::readTopic( 'SystemDoesNotExist' );

my ( $replytext, $hdr ) = $this->call_UI_query(
'/webs.json',
'GET', {} );
my $fromJSON = JSON::from_json( $replytext, { allow_nonref => 1 } );

my @webs = Foswiki::Func::getListOfWebs( '', '' );
my @results = map {
my ( $meta, $text ) = Foswiki::Func::readTopic( $_ );
print STDERR "::::: load($_) == ".$meta->web."\n";
Foswiki::Serialise::convertMeta($meta)
} @webs;

$this->assert_deep_equals( $fromJSON,
\@results );
}
}


sub LATERtestGET_NoSuchTopic {
my $this = shift;
Expand Down

0 comments on commit b37d1d9

Please sign in to comment.