class admsswPlugin extends Plugin {
+ public static $PAGING_LIMIT = 10;
+
//var $trovecat_id_index; // cat_id to TroveCat instances
var $trovecat_id_to_shortname; // cat_id to shortname
var $trovecat_id_to_path; // cat_id to path
'adms' => 'http://www.w3.org/ns/adms#',
'foaf' => 'http://xmlns.com/foaf/0.1/',
'schema' => 'http://schema.org/',
- 'rad' => 'http://www.w3.org/ns/rad#'
+ 'rad' => 'http://www.w3.org/ns/rad#',
+ 'ldp' => 'http://www.w3.org/ns/ldp#'
);
//$this->trovecat_id_index = array();
*
* @return array the namespaces associative array
*/
- private function admsswNameSpaces() {
+ public function admsswNameSpaces() {
return $this->ns;
}
// Project list / SoftwareRepository related methods
//
+ /**
+ * Returns the number of projects in a project index
+ */
+ public function getProjectListSize() {
+ // same as for trove's full list
+ $projects = get_public_active_projects_asc();
+ return count($projects);
+ }
+
/**
* Provides a Graphite graph for resource(s) representing the ADMS.SW SoftwareRepository
*
* @param string URI of the document to use
* @param bool are projects to be fully described or just a URI of their resource
+ * @param $chunk number of the chunk to be returned in case of paging
+ * @param $chunksize size of chunks in case of paging
*/
- public function getProjectListResourcesGraph($documenturi, $detailed=false) {
+ public function getProjectListResourcesGraph($documenturi, $detailed=false, $chunk=null, $chunksize=null) {
// Construct an ARC2_Resource containing the project's RDF (DOAP) description
$ns = $this->admsswNameSpaces();
// same as for trove's full list
$projects = get_public_active_projects_asc();
+
+
+ if ( isset($chunk) && isset($chunksize) ) {
+ // TODO : do some checks on $chunk $chunksize values
+ // if ( ($chunk < 1) && ($chunksize >= 1) ) {
+ // // error
+ // }
+ $projects_chunks = array_chunk($projects, $chunksize);
+ $projects = $projects_chunks[$chunk-1];
+ }
+
$proj_uris = array();
foreach ($projects as $row_grp) {
$proj_uri = util_make_url_g(strtolower($row_grp['unix_group_name']),$row_grp['group_id']).'#project';
return $graph;
}
+ /*
+ * Returns the size of the pages for paged documents (project indexes or full projects dump)
+ */
+ public function getPagingLimit() {
+ return self::$PAGING_LIMIT;
+ }
+
+ /*
+ * Process the paging parameters and eventually redirect, ala LDP
+ *
+ * When there are too many projects to be displayed, it will redirect to the first page : ?page=1
+ * This can be overriden with the ?allatonce parameter
+ */
+ public function process_paging_params_or_redirect($projectsnum, $pl) {
+
+ $p = getIntFromRequest('page', 0);
+
+ if ( null !== getStringFromRequest('allatonce', null)) {
+ $pl = $projectsnum + 1;
+ $p = 0;
+ }
+
+ // force paging if too many projects
+ if ( ($projectsnum > $pl) && ! ($p > 0) ) {
+ header("Location: ?page=1");
+ header($_SERVER["SERVER_PROTOCOL"]." 303 See Other",true,303);
+ exit;
+ }
+
+ // if paging is requested
+ if ($p > 0) {
+ $maxpage = (int) ($projectsnum / $pl);
+ if ($p > $maxpage) {
+ header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found",true,404);
+ printf("Page %d requested is beyond the maximum %d !", $p, $maxpage);
+ exit;
+ }
+ }
+
+ return $p;
+ }
+
+
/**
- * Provides an HTML preview of the ADMS.SW SoftwareRepository meta-data looking like turtle
+ * Provides either an HTML preview looking like turtle, or plain RDF of the ADMS.SW SoftwareRepository meta-data
*
- * @param int $group_id
+ * @param string URL of the RDF document
+ * @param string expected content type for the document
+ * @param int page number. If null, means no paging but full document
+ * @param int page length : how many projects per page
+ * @param bool if has to provide full details about projects
+ * @param string URL of the HTML script if different than the RDF document
*/
- public function htmlPreviewProjectsAsTurtle() {
- $graph = $this->getProjectListResourcesGraph(util_make_url ("/projects"));
+ public function getProjectsListDisplay($documenturi, $content_type, $p, $pl, $detailed=false, $scripturl=false) {
- return $graph->dump();
+ $doc = '';
+
+ if(! $scripturl) {
+ $scripturl = $documenturi;
+ }
+
+ $pageuri = '';
+ $chunksize = null;
+ $chunk = null;
+ // if paging is requested
+ if ($p > 0) {
+ $chunksize = $pl;
+ $chunk = $p;
+ $pageuri = $documenturi . '?page='. (string)$p;
+ }
+
+ $projectsnum = $this->getProjectListSize();
+
+ // process as in content_negociated_projects_list but with full details
+ $graph = $this->getProjectListResourcesGraph($documenturi, $detailed, $chunk, $chunksize);
+
+ // if not HTML
+ if($content_type != 'text/html') {
+
+ if ($p > 0) {
+ $ns = $this->admsswNameSpaces();
+ $conf = array(
+ 'ns' => $ns
+ );
+
+ $res = ARC2::getResource($conf);
+ $res->setURI( $pageuri );
+ rdfutils_setPropToUri($res, 'rdf:type', 'ldp:Page');
+
+ if($p < ( (int) ($projectsnum / $pl) ) ) {
+ $nextpageuri = $documenturi . '?page=' . (string) ($p + 1);
+ rdfutils_setPropToUri($res, 'ldp:nextPage', $nextpageuri);
+ }
+ else {
+ rdfutils_setPropToUri($res, 'ldp:nextPage', 'rdf:nil');
+ }
+ rdfutils_setPropToUri($res, 'ldp:pageOf', $documenturi);
+
+ $count = $graph->addTriples( ARC2::getTriplesFromIndex($res->index) );
+ }
+
+ // We can support only RDF as RDF+XML or Turtle
+ if ($content_type == 'text/turtle' || $content_type == 'application/rdf+xml') {
+ if ($content_type == 'text/turtle') {
+ $doc = $graph->serialize($serializer="Turtle")."\n";
+ }
+ if ($content_type == 'application/rdf+xml') {
+ $doc = $graph->serialize()."\n";
+ }
+ }
+ else {
+ header('HTTP/1.1 406 Not Acceptable',true,406);
+ print $graph->dumpText();
+ exit(0);
+ }
+ } else {
+ // HTML
+
+ $doc = '<p>'. _('The following is a preview of (machine-readable) RDF meta-data, in Turtle format (see at the bottom for more details)') .'<br />';
+
+ $html_limit = '<span style="text-align:center;font-size:smaller">';
+ $html_limit .= sprintf(_('<strong>%1$s</strong> projects in result set.'), $projectsnum);
+ // only display pages stuff if there is more to display
+ if ($projectsnum > $pl) {
+ $html_limit .= trove_html_limit_navigation_box($scripturl, $projectsnum, $pl, $p);
+ }
+ $html_limit .= '</span>';
+
+ $doc .= $html_limit;
+
+ $doc .= $graph->dump();
+
+ $doc .= _('To access this RDF document, you may use, for instance :<br />');
+ $doc .= '<tt>$ curl -L -H "Accept: text/turtle" '. $documenturi .'</tt><br />';
+
+ $doc .= _('This may redirect to several pages documents in case of too big number of results (observing the LDP paging specifications).<br /><br />');
+
+ $doc .= _('Alternatively, if you are sure you want the full dump in one single document, use :<br />');
+ $doc .= '<tt>$ curl -H "Accept: text/turtle" "'. $documenturi .'?allatonce"</tt>';
+
+ }
+ return $doc;
}
/**
* Outputs the public projects list as ADMS.SW for /projects
*
* @param unknown_type $params
+ *
+ * This has a counterpart in /plugins/admssw/projectsturtle.php which previews the Turtle as HTML
*/
public function content_negociated_projects_list (&$params) {
+
$accept = $params['accept'];
// we are asked for RDF either as RDF+XML or Turtle
if($accept == 'application/rdf+xml' || $accept == 'text/turtle') {
-
-
- // We will return RDF
- $params['content_type'] = $accept;
-
- $graph = $this->getProjectListResourcesGraph(util_make_url ("/projects"));
-
- if ($accept == 'text/turtle') {
- $doc = $graph->serialize($serializer="Turtle");
- }
- if ($accept == 'application/rdf+xml') {
- $doc = $graph->serialize();
- }
-
- $params['content'] = $doc . "\n";
-
- }
+
+ // We will return RDF
+ $params['content_type'] = $accept;
+
+ $documenturi = util_make_url ("/projects");
+
+ // page length
+ $pl = admsswPlugin::$PAGING_LIMIT;
+
+ $projectsnum = $this->getProjectListSize();
+
+ $p = $this->process_paging_params_or_redirect($projectsnum, $pl);
+
+ $doc = $this->getProjectsListDisplay($documenturi, $accept, $p, $pl);
+
+ $params['content'] = $doc . "\n";
+
+ }
}
}
}
+
// Local Variables:
// mode: php
// c-file-style: "bsd"