From bae51e6ed5c47a3dca2a5fc62a3891c34001a04b Mon Sep 17 00:00:00 2001 From: JT Smith Date: Mon, 13 Oct 2008 16:53:19 +0000 Subject: [PATCH] Added the Asset Discovery web service content handler. --- docs/changelog/7.x.x.txt | 1 + docs/upgrades/upgrade_7.6.0-7.6.1.pl | 15 +++ lib/WebGUI/Content/AssetDiscovery.pm | 148 +++++++++++++++++++++++++++ 3 files changed, 164 insertions(+) create mode 100644 lib/WebGUI/Content/AssetDiscovery.pm diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index 30fbf9f62..a352105ab 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -38,6 +38,7 @@ - fixed #8776: duplicate does not duplicate keywords - rfe: Thingy: export extra rows, meta data (SDH Consulting Group) - rfe: Thingy: Max entries per user (SDH Consulting Group) + - Added the Asset Discovery web service content handler. - fixed: #4174: UserList template show links that should be hidden (Yung Han Khoe) - fixed #8802: isImage not functioning in Folders diff --git a/docs/upgrades/upgrade_7.6.0-7.6.1.pl b/docs/upgrades/upgrade_7.6.0-7.6.1.pl index 4acd1d045..7536d8392 100644 --- a/docs/upgrades/upgrade_7.6.0-7.6.1.pl +++ b/docs/upgrades/upgrade_7.6.0-7.6.1.pl @@ -34,9 +34,24 @@ addDataFormDataIndexes($session); addThingyColumns( $session ); addCommentsAspect( $session ); addCommentsAspectToWiki( $session ); +addAssetDiscoveryService( $session ); finish($session); # this line required +#---------------------------------------------------------------------------- +sub addAssetDiscoveryService { + my $session = shift; + print "\tAdding asset discovery service..." unless $quiet; + my @handlers; + foreach my $handler (@{$session->config->get("contentHandlers")}) { + if ($handler eq "WebGUI::Content::Operation") { + push @handlers, 'WebGUI::Content::AssetDiscovery'; + } + push @handlers, $handler; + } + print "Done.\n" unless $quiet; +} + #---------------------------------------------------------------------------- sub addCommentsAspectToWiki { my $session = shift; diff --git a/lib/WebGUI/Content/AssetDiscovery.pm b/lib/WebGUI/Content/AssetDiscovery.pm new file mode 100644 index 000000000..0b1d606f2 --- /dev/null +++ b/lib/WebGUI/Content/AssetDiscovery.pm @@ -0,0 +1,148 @@ +package WebGUI::Content::AssetDiscovery; + +=head1 LEGAL + + ------------------------------------------------------------------- + WebGUI is Copyright 2001-2008 Plain Black Corporation. + ------------------------------------------------------------------- + Please read the legal notices (docs/legal.txt) and the license + (docs/license.txt) that came with this distribution before using + this software. + ------------------------------------------------------------------- + http://www.plainblack.com info@plainblack.com + ------------------------------------------------------------------- + +=cut + +use strict; +use JSON; +use WebGUI::Asset; +use WebGUI::Utility; +use XML::Simple; + +=head1 NAME + +Package WebGUI::Content::AssetDiscovery + +=head1 DESCRIPTION + +Allows web services to find a list of assets of a given type. + +=head1 SYNOPSIS + + use WebGUI::Content::AssetDiscovery; + my $output = WebGUI::Content::AssetDiscovery::handler($session); + +From the web call any WebGUI URL. The discovery asset will limit your query to the assets below that URL in the asset tree. Here's an example: + + http://admin:123qwe@www.example.com/some-page?op=findAssets;className=WebGUI::Asset::Wobject::Article + +Only the assets that you can view according to your user's privileges will be returned. The following are the parameters you can pass along the URL: + +=head2 op + +Required. Its value must be 'findAssets'. + +=head2 className + +Required. Its value must be a valid WebGUI asset classname. + +=head2 as + +Defaults to 'json'. You may override it by setting its value to 'xml'. This setting determines how the result set will come back. If it is 'json' it will look like: + + { + "assets" : [ + { + "lastUpdated" : "2006-05-14 16:35:15", + "synopsis" : null, + "menuTitle" : "Getting Started (part 2)", + "url" : "http://dev.localhost.localdomain/getting_started/getting-started-part2", + "title" : "Getting Started (part 2)", + "dateCreated" : "2006-05-14 16:35:15" + } + ], + "className" : "WebGUI::Asset::Wobject::Article", + "pageNumber" : 1 + } + +If it is 'xml' it will look like: + + + + 2006-05-14 16:35:15 + 2006-05-14 16:35:15 + Getting Started (part 2) + + Getting Started (part 2) + http://dev.localhost.localdomain/getting_started/getting-started-part2 + + WebGUI::Asset::Wobject::Article + 1 + + +=head2 pn + +Defaults to 1. pn stands for Page Number. The result set from this service returns up to 100 assets at a time. If you need more than that you can set pn to the next page number and so on. B Due to the calculations based upon branch and user privileges this service does not know the maximum number of pages of data there will be. + +=head1 SUBROUTINES + +These subroutines are available from this package: + +=cut + +#------------------------------------------------------------------- + +=head2 handler ( session ) + +The content handler for this package. + +=cut + +sub handler { + my ($session) = @_; + my $form = $session->form; + if ($form->get('op') eq 'findAssets') { + my @assets; + my $as = $form->get('as') || 'json'; + my $pageNumber = $form->get('pn') || 1; + my $class = $form->get('className'); + if ($class ne '') { + my $start = WebGUI::Asset->newByUrl($session); + my $limit = ($pageNumber * 100 - 99).','.($pageNumber * 100); + my $siteUrl = $session->url->getSiteURL; + my $date = $session->datetime; + my $matchingAssets = $session->db->read("select assetId from asset where lineage like ? and className like ? limit ".$limit, [$start->get('lineage').'%', $class.'%']); + while (my ($id) = $matchingAssets->array) { + my $asset = WebGUI::Asset->new($session, $id, $class); + if (defined $asset) { + if ($asset->canView && $asset->get('state') eq 'published' && isIn($asset->get('status'), 'approved', 'archived')) { + push @assets, { + title => $asset->getTitle, + menuTitle => $asset->get('menuTitle'), + synopsis => $asset->get('synopsis'), + url => $siteUrl.$asset->getUrl, + dateCreated => $date->epochToHuman($asset->get('creationDate'), '%y-%m-%d %j:%n:%s'), + lastUpdated => $date->epochToHuman($asset->get('revisionDate'), '%y-%m-%d %j:%n:%s'), + }; + } + } + } + } + my $document = { + pageNumber => $pageNumber, + className => $class, + assets => \@assets + }; + if ($as eq "xml") { + $session->http->setMimeType('text/xml'); + return XML::Simple::XMLout($document, NoAttr => 1); + } + $session->http->setMimeType('text/json'); + return JSON->new->encode($document); + } + return undef; +} + +1; +#vim:ft=perl