00001 <?php
00002
00005
00027 class Browse extends WebService
00028 {
00030 private $conneg;
00031
00033 private $dtdURL;
00034
00036 private $attributes = "";
00037
00039 private $types = "";
00040
00042 private $datasets = "";
00043
00045 private $items = "";
00046
00048 private $page = "";
00049
00051 private $inference = "";
00052
00054 private $requester_ip = "";
00055
00057 private $registered_ip = "";
00058
00060 private $namespaces =
00061 array ("http://www.w3.org/2002/07/owl#" => "owl", "http://www.w3.org/1999/02/22-rdf-syntax-ns#" => "rdf",
00062 "http://www.w3.org/2000/01/rdf-schema#" => "rdfs", "http://purl.org/ontology/wsf#" => "wsf");
00063
00064
00066 public $subjectTriples = array();
00067
00069 public $objectTriples = array();
00070
00072 public $resultset = array();
00073
00075 public $resultsetObjectProperties = array();
00076
00078 public $resultsetObjectPropertiesUris = array();
00079
00081 public $aggregates = array();
00082
00084 public $include_aggregates = array();
00085
00087 public static $supportedSerializations =
00088 array ("application/json", "application/rdf+xml", "application/rdf+n3", "application/*", "text/xml", "text/*",
00089 "*/*");
00090
00092 private $errorMessenger =
00093 '{
00094 "ws": "/ws/browse/",
00095 "_200": {
00096 "id": "WS-BROWSE-200",
00097 "level": "Warning",
00098 "name": "Invalid number of items requested",
00099 "description": "The number of items returned per request has to be greater than 0 and lesser than 128"
00100 },
00101 "_300": {
00102 "id": "WS-BROWSE-300",
00103 "level": "Warning",
00104 "name": "No datasets accessible by that user",
00105 "description": "No datasets are accessible to that user"
00106 }
00107 }';
00108
00109
00131 function __construct($attributes, $types, $datasets, $items, $page, $inference, $include_aggregates, $registered_ip,
00132 $requester_ip)
00133 {
00134 parent::__construct();
00135
00136 $this->attributes = $attributes;
00137 $this->items = $items;
00138 $this->page = $page;
00139 $this->inference = $inference;
00140 $this->includeAggregates = $include_aggregates;
00141
00142 $this->types = $types;
00143 $this->datasets = $datasets;
00144
00145 $this->requester_ip = $requester_ip;
00146
00147 if($registered_ip == "")
00148 {
00149 $this->registered_ip = $requester_ip;
00150 }
00151 else
00152 {
00153 $this->registered_ip = $registered_ip;
00154 }
00155
00156 if(strtolower(substr($this->registered_ip, 0, 4)) == "self")
00157 {
00158 $pos = strpos($this->registered_ip, "::");
00159
00160 if($pos !== FALSE)
00161 {
00162 $account = substr($this->registered_ip, $pos + 2, strlen($this->registered_ip) - ($pos + 2));
00163
00164 $this->registered_ip = $requester_ip . "::" . $account;
00165 }
00166 else
00167 {
00168 $this->registered_ip = $requester_ip;
00169 }
00170 }
00171
00172 $this->uri = $this->wsf_base_url . "/wsf/ws/browse/";
00173 $this->title = "Browse Web Service";
00174 $this->crud_usage = new CrudUsage(FALSE, TRUE, FALSE, FALSE);
00175 $this->endpoint = $this->wsf_base_url . "/ws/browse/";
00176
00177 $this->dtdURL = "browse/browse.dtd";
00178
00179 $this->errorMessenger = json_decode($this->errorMessenger);
00180 }
00181
00182 function __destruct() { parent::__destruct(); }
00183
00196 protected function validateQuery() {
00197
00198
00199
00200
00201
00202 }
00203
00214 public function pipeline_getError() { return ($this->conneg->error); }
00215
00216
00227 public function pipeline_getResultset()
00228 {
00229 $xml = new ProcessorXML();
00230
00231
00232 $resultset = $xml->createResultset();
00233
00234
00235 $void = $xml->createPrefix("owl", "http://www.w3.org/2002/07/owl#");
00236 $resultset->appendChild($void);
00237 $rdf = $xml->createPrefix("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
00238 $resultset->appendChild($rdf);
00239 $dcterms = $xml->createPrefix("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
00240 $resultset->appendChild($dcterms);
00241 $dcterms = $xml->createPrefix("wsf", "http://purl.org/ontology/wsf#");
00242 $resultset->appendChild($dcterms);
00243
00244 $subject;
00245
00246 foreach($this->resultset as $uri => $result)
00247 {
00248
00249 if(isset($result["type"]))
00250 {
00251 foreach($result["type"] as $key => $type)
00252 {
00253 if($key > 0)
00254 {
00255 $pred = $xml->createPredicate("http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
00256 $object = $xml->createObject("", $type);
00257 $pred->appendChild($object);
00258 $subject->appendChild($pred);
00259 }
00260 else
00261 {
00262 $subject = $xml->createSubject($type, $uri);
00263 }
00264 }
00265 }
00266 else
00267 {
00268 $subject = $xml->createSubject("http://www.w3.org/2002/07/owl#Thing", $this->resourceUri);
00269 }
00270
00271
00272 if(isset($result["dataset"]))
00273 {
00274 $pred = $xml->createPredicate("http://purl.org/dc/terms/isPartOf");
00275 $object = $xml->createObject("http://rdfs.org/ns/void#Dataset", $result["dataset"]);
00276 $pred->appendChild($object);
00277 $subject->appendChild($pred);
00278 }
00279
00280
00281 if(isset($result["prefLabel"]))
00282 {
00283 $pred = $xml->createPredicate(Namespaces::$iron . "prefLabel");
00284 $object = $xml->createObjectContent($this->xmlEncode($result["prefLabel"]));
00285 $pred->appendChild($object);
00286 $subject->appendChild($pred);
00287 }
00288
00289
00290 if(isset($result["altLabel"]))
00291 {
00292 foreach($result["altLabel"] as $altLabel)
00293 {
00294 $pred = $xml->createPredicate(Namespaces::$iron . "altLabel");
00295 $object = $xml->createObjectContent($this->xmlEncode($altLabel));
00296 $pred->appendChild($object);
00297 $subject->appendChild($pred);
00298 }
00299 }
00300
00301
00302 if(isset($result["description"]))
00303 {
00304 $pred = $xml->createPredicate(Namespaces::$iron . "description");
00305 $object = $xml->createObjectContent($this->xmlEncode($result["description"]));
00306 $pred->appendChild($object);
00307 $subject->appendChild($pred);
00308 }
00309
00310
00311
00312 foreach($result as $property => $values)
00313 {
00314 if($property != "type" && $property != "dataset")
00315 {
00316 foreach($values as $value)
00317 {
00318 $pred = $xml->createPredicate($property);
00319 $object = $xml->createObjectContent($this->xmlEncode($value));
00320 $pred->appendChild($object);
00321 $subject->appendChild($pred);
00322 }
00323 }
00324 }
00325
00326
00327 if(isset($this->resultsetObjectProperties[$uri]))
00328 {
00329 foreach($this->resultsetObjectProperties[$uri] as $property => $values)
00330 {
00331 if($propeerty != "type" && $property != "dataset")
00332 {
00333 foreach($values as $key => $value)
00334 {
00335 $pred = $xml->createPredicate($property);
00336
00337 $object = $xml->createObject("", $this->resultsetObjectPropertiesUris[$uri][$property][$key], "");
00338 $pred->appendChild($object);
00339
00340 $reify = $xml->createReificationStatement("wsf:objectLabel", $value);
00341 $object->appendChild($reify);
00342
00343 $subject->appendChild($pred);
00344 }
00345 }
00346 }
00347 }
00348
00349 $resultset->appendChild($subject);
00350 }
00351
00352
00353
00354
00355
00356
00357 if(strtolower($this->includeAggregates) == "true")
00358 {
00359 $aggregatesUri = $this->uri . "aggregate/" . md5(microtime());
00360
00361 $typeLabelsCounts = array();
00362
00363 foreach($this->aggregates["type"] as $ftype => $fcount)
00364 {
00365
00366 if(isset($this->aggregates["inferred_type"][$ftype]))
00367 {
00368 $fcount = $this->aggregates["inferred_type"][$ftype];
00369 }
00370
00371 $subject =
00372 $xml->createSubject("http://purl.org/ontology/aggregate#Aggregate", $aggregatesUri . "/" . md5($ftype) . "/");
00373
00374 $pred = $xml->createPredicate("http://purl.org/ontology/aggregate#property");
00375 $object = $xml->createObject("", "http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
00376 $pred->appendChild($object);
00377 $subject->appendChild($pred);
00378
00379 $pred = $xml->createPredicate("http://purl.org/ontology/aggregate#object");
00380 $object = $xml->createObject("", $ftype);
00381 $pred->appendChild($object);
00382 $subject->appendChild($pred);
00383
00384 $pred = $xml->createPredicate("http://purl.org/ontology/aggregate#count");
00385 $object = $xml->createObjectContent($this->xmlEncode($fcount));
00386 $pred->appendChild($object);
00387 $subject->appendChild($pred);
00388
00389 $resultset->appendChild($subject);
00390
00391 $typeLabelsCounts = array();
00392 }
00393
00394
00395 foreach($this->aggregates["inferred_type"] as $ftype => $fcount)
00396 {
00397
00398 if(!isset($this->aggregates["type"][$ftype]))
00399 {
00400 $subject = $xml->createSubject("http://purl.org/ontology/aggregate#Aggregate",
00401 $aggregatesUri . "/" . md5($ftype) . "/");
00402
00403 $pred = $xml->createPredicate("http://purl.org/ontology/aggregate#property");
00404 $object = $xml->createObject("", "http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
00405 $pred->appendChild($object);
00406 $subject->appendChild($pred);
00407
00408 $pred = $xml->createPredicate("http://purl.org/ontology/aggregate#object");
00409 $object = $xml->createObject("", $ftype);
00410 $pred->appendChild($object);
00411 $subject->appendChild($pred);
00412
00413 $pred = $xml->createPredicate("http://purl.org/ontology/aggregate#count");
00414 $object = $xml->createObjectContent($this->xmlEncode($fcount));
00415 $pred->appendChild($object);
00416 $subject->appendChild($pred);
00417
00418 $resultset->appendChild($subject);
00419 }
00420 }
00421
00422
00423
00424 $aggregatesUri = $this->uri . "aggregate/" . md5(microtime());
00425
00426 foreach($this->aggregates["dataset"] as $ftype => $fcount)
00427 {
00428 $subject =
00429 $xml->createSubject("http://purl.org/ontology/aggregate#Aggregate", $aggregatesUri . "/" . md5($ftype) . "/");
00430
00431 $pred = $xml->createPredicate("http://purl.org/ontology/aggregate#property");
00432 $object = $xml->createObject("", "http://rdfs.org/ns/void#Dataset");
00433 $pred->appendChild($object);
00434 $subject->appendChild($pred);
00435
00436 $pred = $xml->createPredicate("http://purl.org/ontology/aggregate#object");
00437 $object = $xml->createObject("", $ftype);
00438 $pred->appendChild($object);
00439 $subject->appendChild($pred);
00440
00441 $pred = $xml->createPredicate("http://purl.org/ontology/aggregate#count");
00442 $object = $xml->createObjectContent($this->xmlEncode($fcount));
00443 $pred->appendChild($object);
00444 $subject->appendChild($pred);
00445
00446 $resultset->appendChild($subject);
00447 }
00448
00449
00450
00451
00452 $aggregatesUri = $this->uri . "aggregate/" . md5(microtime());
00453
00454 foreach($this->aggregates["attributes"] as $ftype => $fcount)
00455 {
00456 $subject =
00457 $xml->createSubject("http://purl.org/ontology/aggregate#Aggregate", $aggregatesUri . "/" . md5($ftype) . "/");
00458
00459 $pred = $xml->createPredicate("http://purl.org/ontology/aggregate#property");
00460 $object = $xml->createObject("", "http://www.w3.org/1999/02/22-rdf-syntax-ns#Property");
00461 $pred->appendChild($object);
00462 $subject->appendChild($pred);
00463
00464 $pred = $xml->createPredicate("http://purl.org/ontology/aggregate#object");
00465 $object = $xml->createObject("", $ftype);
00466 $pred->appendChild($object);
00467 $subject->appendChild($pred);
00468
00469 $pred = $xml->createPredicate("http://purl.org/ontology/aggregate#count");
00470 $object = $xml->createObjectContent($this->xmlEncode($fcount));
00471 $pred->appendChild($object);
00472 $subject->appendChild($pred);
00473
00474 $resultset->appendChild($subject);
00475 }
00476 }
00477
00478 return ($this->injectDoctype($xml->saveXML($resultset)));
00479 }
00480
00493 public function injectDoctype($xmlDoc)
00494 {
00495 $posHeader = strpos($xmlDoc, '"?>') + 3;
00496 $xmlDoc = substr($xmlDoc, 0, $posHeader)
00497 . "\n<!DOCTYPE resultset PUBLIC \"-//Structured Dynamics LLC//Browse DTD 0.1//EN\" \"" . $this->dtdBaseURL
00498 . $this->dtdURL . "\">" . substr($xmlDoc, $posHeader, strlen($xmlDoc) - $posHeader);
00499
00500 return ($xmlDoc);
00501 }
00502
00521 public function ws_conneg($accept, $accept_charset, $accept_encoding, $accept_language)
00522 {
00523 $this->conneg =
00524 new Conneg($accept, $accept_charset, $accept_encoding, $accept_language, Browse::$supportedSerializations);
00525
00526
00527 $this->validateQuery();
00528
00529
00530 if($this->conneg->getStatus() == 200)
00531 {
00532 if($this->items < 0 || $this->items > 128)
00533 {
00534 $this->conneg->setStatus(400);
00535 $this->conneg->setStatusMsg("Bad Request");
00536 $this->conneg->setStatusMsgExt($this->errorMessenger->_200->name);
00537 $this->conneg->setError($this->errorMessenger->_200->id, $this->errorMessenger->ws,
00538 $this->errorMessenger->_200->name, $this->errorMessenger->_200->description, "",
00539 $this->errorMessenger->_200->level);
00540 return;
00541 }
00542 }
00543 }
00544
00563 public function pipeline_conneg($accept, $accept_charset, $accept_encoding, $accept_language)
00564 { $this->ws_conneg($accept, $accept_charset, $accept_encoding, $accept_language); }
00565
00576 public function pipeline_getResponseHeaderStatus() { return $this->conneg->getStatus(); }
00577
00588 public function pipeline_getResponseHeaderStatusMsg() { return $this->conneg->getStatusMsg(); }
00589
00602 public function pipeline_getResponseHeaderStatusMsgExt() { return $this->conneg->getStatusMsgExt(); }
00603
00614 public function pipeline_serialize()
00615 {
00616 $rdf_part = "";
00617
00618 switch($this->conneg->getMime())
00619 {
00620 case "application/json":
00621 $json_part = "";
00622 $xml = new ProcessorXML();
00623 $xml->loadXML($this->pipeline_getResultset());
00624
00625 $subjects = $xml->getSubjects();
00626
00627 $nsId = 0;
00628
00629 foreach($subjects as $subject)
00630 {
00631 $subjectURI = $xml->getURI($subject);
00632 $subjectType = $xml->getType($subject);
00633
00634 $ns = $this->getNamespace($subjectType);
00635
00636 if(!isset($this->namespaces[$ns[0]]))
00637 {
00638 $this->namespaces[$ns[0]] = "ns" . $nsId;
00639 $nsId++;
00640 }
00641
00642 $json_part .= " { \n";
00643 $json_part .= " \"uri\": \"" . parent::jsonEncode($subjectURI) . "\", \n";
00644 $json_part .= " \"type\": \"" . parent::jsonEncode($this->namespaces[$ns[0]] . ":" . $ns[1])
00645 . "\", \n";
00646
00647 $predicates = $xml->getPredicates($subject);
00648
00649 $nbPredicates = 0;
00650
00651 foreach($predicates as $predicate)
00652 {
00653 $objects = $xml->getObjects($predicate);
00654
00655 foreach($objects as $object)
00656 {
00657 $nbPredicates++;
00658
00659 if($nbPredicates == 1)
00660 {
00661 $json_part .= " \"predicate\": [ \n";
00662 }
00663
00664 $objectType = $xml->getType($object);
00665 $predicateType = $xml->getType($predicate);
00666
00667 if($objectType == "rdfs:Literal")
00668 {
00669 $objectValue = $xml->getContent($object);
00670
00671 $ns = $this->getNamespace($predicateType);
00672
00673 if(!isset($this->namespaces[$ns[0]]))
00674 {
00675 $this->namespaces[$ns[0]] = "ns" . $nsId;
00676 $nsId++;
00677 }
00678
00679 $json_part .= " { \n";
00680 $json_part .= " \"" . parent::jsonEncode($this->namespaces[$ns[0]] . ":" . $ns[1]) . "\": \""
00681 . parent::jsonEncode($objectValue) . "\" \n";
00682 $json_part .= " },\n";
00683 }
00684 else
00685 {
00686 $objectURI = $xml->getURI($object);
00687
00688 $ns = $this->getNamespace($predicateType);
00689
00690 if(!isset($this->namespaces[$ns[0]]))
00691 {
00692 $this->namespaces[$ns[0]] = "ns" . $nsId;
00693 $nsId++;
00694 }
00695
00696 $json_part .= " { \n";
00697 $json_part .= " \"" . parent::jsonEncode($this->namespaces[$ns[0]] . ":" . $ns[1])
00698 . "\": { \n";
00699 $json_part .= " \"uri\": \"" . parent::jsonEncode($objectURI) . "\",\n";
00700
00701
00702 $reifies = $xml->getReificationStatements($object, "wsf:objectLabel");
00703
00704 $nbReification = 0;
00705
00706 foreach($reifies as $reify)
00707 {
00708 $nbReification++;
00709
00710 if($nbReification > 0)
00711 {
00712 $json_part .= " \"reify\": [\n";
00713 }
00714
00715 $json_part .= " { \n";
00716 $json_part .= " \"type\": \"wsf:objectLabel\", \n";
00717 $json_part .= " \"value\": \"" . parent::jsonEncode($xml->getValue($reify))
00718 . "\" \n";
00719 $json_part .= " },\n";
00720 }
00721
00722 if($nbReification > 0)
00723 {
00724 $json_part = substr($json_part, 0, strlen($json_part) - 2) . "\n";
00725
00726 $json_part .= " ]\n";
00727 }
00728 else
00729 {
00730 $json_part = substr($json_part, 0, strlen($json_part) - 2) . "\n";
00731 }
00732
00733 $json_part .= " } \n";
00734 $json_part .= " },\n";
00735 }
00736 }
00737 }
00738
00739 if(strlen($json_part) > 0)
00740 {
00741 $json_part = substr($json_part, 0, strlen($json_part) - 2) . "\n";
00742 }
00743
00744 if($nbPredicates > 0)
00745 {
00746 $json_part .= " ]\n";
00747 }
00748
00749 $json_part .= " },\n";
00750 }
00751
00752 if(strlen($json_part) > 0)
00753 {
00754 $json_part = substr($json_part, 0, strlen($json_part) - 2) . "\n";
00755 }
00756
00757 $json_header .= " \"prefixes\": [ \n";
00758 $json_header .= " {\n";
00759 $json_header .= " \"rdf\": \"http://www.w3.org/1999/02/22-rdf-syntax-ns#\",\n";
00760 $json_header .= " \"wsf\": \"http://purl.org/ontology/wsf#\",\n";
00761
00762 foreach($this->namespaces as $ns => $prefix)
00763 {
00764 $json_header .= " \"$prefix\": \"$ns\",\n";
00765 }
00766
00767 if(strlen($json_header) > 0)
00768 {
00769 $json_header = substr($json_header, 0, strlen($json_header) - 2) . "\n";
00770 }
00771
00772 $json_header .= " } \n";
00773 $json_header .= " ],\n";
00774 $json_header .= " \"resultset\": {\n";
00775 $json_header .= " \"subject\": [\n";
00776 $json_header .= $json_part;
00777 $json_header .= " ]\n";
00778 $json_header .= " }\n";
00779
00780 return ($json_header);
00781 break;
00782
00783 case "application/rdf+n3":
00784
00785 $xml = new ProcessorXML();
00786 $xml->loadXML($this->pipeline_getResultset());
00787
00788 $subjects = $xml->getSubjects();
00789
00790 foreach($subjects as $subject)
00791 {
00792 $subjectURI = $xml->getURI($subject);
00793 $subjectType = $xml->getType($subject, FALSE);
00794
00795 $rdf_part .= "\n <$subjectURI> a <$subjectType> ;\n";
00796
00797 $predicates = $xml->getPredicates($subject);
00798
00799 foreach($predicates as $predicate)
00800 {
00801 $objects = $xml->getObjects($predicate);
00802
00803 foreach($objects as $object)
00804 {
00805 $objectType = $xml->getType($object);
00806 $predicateType = $xml->getType($predicate, FALSE);
00807 $objectContent = $xml->getContent($object);
00808
00809 if($objectType == "rdfs:Literal")
00810 {
00811 $objectValue = $xml->getContent($object);
00812 $rdf_part .= " <$predicateType> \"\"\"" . str_replace(array( "\\" ), "\\\\", $objectValue)
00813 . "\"\"\" ;\n";
00814 }
00815 else
00816 {
00817 $objectURI = $xml->getURI($object);
00818 $rdf_part .= " <$predicateType> <$objectURI> ;\n";
00819 }
00820 }
00821 }
00822
00823 if(strlen($rdf_part) > 0)
00824 {
00825 $rdf_part = substr($rdf_part, 0, strlen($rdf_part) - 2) . ".\n";
00826 }
00827 }
00828
00829 return ($rdf_part);
00830 break;
00831
00832 case "application/rdf+xml":
00833 $xml = new ProcessorXML();
00834 $xml->loadXML($this->pipeline_getResultset());
00835
00836 $subjects = $xml->getSubjects();
00837
00838 $nsId = 0;
00839
00840 foreach($subjects as $subject)
00841 {
00842 $subjectURI = $xml->getURI($subject);
00843 $subjectType = $xml->getType($subject);
00844
00845 $ns1 = $this->getNamespace($subjectType);
00846
00847 if(!isset($this->namespaces[$ns1[0]]))
00848 {
00849 $this->namespaces[$ns1[0]] = "ns" . $nsId;
00850 $nsId++;
00851 }
00852
00853 $rdf_part .= "\n <" . $this->namespaces[$ns1[0]] . ":" . $ns1[1] . " rdf:about=\"$subjectURI\">\n";
00854
00855 $predicates = $xml->getPredicates($subject);
00856
00857 foreach($predicates as $predicate)
00858 {
00859 $objects = $xml->getObjects($predicate);
00860
00861 foreach($objects as $object)
00862 {
00863 $objectType = $xml->getType($object);
00864 $predicateType = $xml->getType($predicate);
00865
00866 if($objectType == "rdfs:Literal")
00867 {
00868 $objectValue = $xml->getContent($object);
00869
00870 $ns = $this->getNamespace($predicateType);
00871
00872 if(!isset($this->namespaces[$ns[0]]))
00873 {
00874 $this->namespaces[$ns[0]] = "ns" . $nsId;
00875 $nsId++;
00876 }
00877
00878 $rdf_part .= " <" . $this->namespaces[$ns[0]] . ":" . $ns[1] . ">"
00879 . $this->xmlEncode($objectValue) . "</" . $this->namespaces[$ns[0]] . ":" . $ns[1] . ">\n";
00880 }
00881 else
00882 {
00883 $objectURI = $xml->getURI($object);
00884
00885 $ns = $this->getNamespace($predicateType);
00886
00887 if(!isset($this->namespaces[$ns[0]]))
00888 {
00889 $this->namespaces[$ns[0]] = "ns" . $nsId;
00890 $nsId++;
00891 }
00892
00893 $rdf_part .= " <" . $this->namespaces[$ns[0]] . ":" . $ns[1]
00894 . " rdf:resource=\"$objectURI\" />\n";
00895 }
00896 }
00897 }
00898
00899 $rdf_part .= " </" . $this->namespaces[$ns1[0]] . ":" . $ns1[1] . ">\n";
00900 }
00901
00902 $rdf_header = "<rdf:RDF ";
00903
00904 foreach($this->namespaces as $ns => $prefix)
00905 {
00906 $rdf_header .= " xmlns:$prefix=\"$ns\"";
00907 }
00908
00909 $rdf_header .= ">\n\n";
00910
00911 $rdf_part = $rdf_header . $rdf_part;
00912
00913 return ($rdf_part);
00914 break;
00915 }
00916 }
00917
00930 private function getNamespace($uri)
00931 {
00932 $pos = strrpos($uri, "#");
00933
00934 if($pos !== FALSE)
00935 {
00936 return array (substr($uri, 0, $pos) . "#", substr($uri, $pos + 1, strlen($uri) - ($pos + 1)));
00937 }
00938 else
00939 {
00940 $pos = strrpos($uri, "/");
00941
00942 if($pos !== FALSE)
00943 {
00944 return array (substr($uri, 0, $pos) . "/", substr($uri, $pos + 1, strlen($uri) - ($pos + 1)));
00945 }
00946 else
00947 {
00948 $pos = strpos($uri, ":");
00949
00950 if($pos !== FALSE)
00951 {
00952 $nsUri = explode(":", $uri, 2);
00953
00954 foreach($this->namespaces as $uri2 => $prefix2)
00955 {
00956 $uri2 = urldecode($uri2);
00957
00958 if($prefix2 == $nsUri[0])
00959 {
00960 return (array ($uri2, $nsUri[1]));
00961 }
00962 }
00963
00964 return explode(":", $uri, 2);
00965 }
00966 }
00967 }
00968
00969 return (FALSE);
00970 }
00971
00980 public function pipeline_serialize_reification()
00981 {
00982 $rdf_reification = "";
00983
00984 switch($this->conneg->getMime())
00985 {
00986 case "application/rdf+n3":
00987 $xml = new ProcessorXML();
00988 $xml->loadXML($this->pipeline_getResultset());
00989
00990 $subjects = $xml->getSubjects();
00991
00992 $bnodeCounter = 0;
00993
00994 foreach($subjects as $subject)
00995 {
00996 $predicates = $xml->getPredicates($subject);
00997
00998 foreach($predicates as $predicate)
00999 {
01000 $predicateType = $xml->getType($predicate, FALSE);
01001
01002 $objects = $xml->getObjects($predicate);
01003
01004 foreach($objects as $object)
01005 {
01006 $reifies = $xml->getReificationStatementsByType($object, "wsf:objectLabel");
01007
01008 foreach($reifies as $reify)
01009 {
01010 $rdf_reification .= "_:" . md5($xml->getURI($subject) . $predicateType . $xml->getURI($object))
01011 . " a rdf:Statement ;\n";
01012 $bnodeCounter++;
01013 $rdf_reification .= " rdf:subject <" . $xml->getURI($subject) . "> ;\n";
01014 $rdf_reification .= " rdf:predicate <" . $predicateType . "> ;\n";
01015 $rdf_reification .= " rdf:object <" . $xml->getURI($object) . "> ;\n";
01016 $rdf_reification .= " wsf:objectLabel \"" . $xml->getValue($reify) . "\" .\n\n";
01017 $bnodeCounter++;
01018 }
01019 }
01020 }
01021 }
01022
01023 return ($rdf_reification);
01024
01025 break;
01026
01027 case "application/rdf+xml":
01028
01029 $xml = new ProcessorXML();
01030 $xml->loadXML($this->pipeline_getResultset());
01031
01032 $subjects = $xml->getSubjects();
01033
01034 foreach($subjects as $subject)
01035 {
01036 $predicates = $xml->getPredicates($subject);
01037
01038 foreach($predicates as $predicate)
01039 {
01040 $predicateType = $xml->getType($predicate, FALSE);
01041
01042 $objects = $xml->getObjects($predicate);
01043
01044 foreach($objects as $object)
01045 {
01046 $reifies = $xml->getReificationStatementsByType($object, "wsf:objectLabel");
01047
01048 foreach($reifies as $reify)
01049 {
01050 $rdf_reification .= "<rdf:Statement rdf:about=\""
01051 . md5($xml->getURI($subject) . $predicateType . $xml->getURI($object)) . "\">\n";
01052 $rdf_reification .= " <rdf:subject rdf:resource=\"" . $xml->getURI($subject) . "\" />\n";
01053 $rdf_reification .= " <rdf:predicate rdf:resource=\"" . $predicateType . "\" />\n";
01054 $rdf_reification .= " <rdf:object rdf:resource=\"" . $xml->getURI($object) . "\" />\n";
01055 $rdf_reification .= " <wsf:objectLabel>" . $this->xmlEncode($xml->getValue($reify))
01056 . "</wsf:objectLabel>\n";
01057 $rdf_reification .= "</rdf:Statement> \n\n";
01058 }
01059 }
01060 }
01061 }
01062
01063 return ($rdf_reification);
01064
01065 break;
01066 }
01067 }
01068
01079 public function ws_serialize()
01080 {
01081 switch($this->conneg->getMime())
01082 {
01083 case "application/rdf+n3":
01084 $rdf_document = "";
01085 $rdf_document .= "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n";
01086 $rdf_document .= "@prefix wsf: <http://purl.org/ontology/wsf#> .\n";
01087
01088 $rdf_document .= $this->pipeline_serialize();
01089
01090 $rdf_document .= $this->pipeline_serialize_reification();
01091
01092 return $rdf_document;
01093 break;
01094
01095 case "application/rdf+xml":
01096 $rdf_document = "";
01097 $rdf_document .= "<?xml version=\"1.0\"?>\n";
01098
01099 $rdf_document .= $this->pipeline_serialize();
01100
01101 $rdf_document .= $this->pipeline_serialize_reification();
01102
01103 $rdf_document .= "</rdf:RDF>";
01104
01105 return $rdf_document;
01106 break;
01107
01108 case "application/json":
01109 $json_document = "";
01110 $json_document .= "{\n";
01111 $json_document .= $this->pipeline_serialize();
01112 $json_document .= "}";
01113
01114 return ($json_document);
01115 break;
01116
01117 case "text/xml":
01118 return $this->pipeline_getResultset();
01119 break;
01120 }
01121 }
01122
01135 public function ws_respond($content)
01136 {
01137
01138 $this->conneg->respond();
01139
01140
01141
01142
01143 if($this->conneg->getStatus() == 200)
01144 {
01145 echo $content;
01146 }
01147
01148 $this->__destruct();
01149 }
01150
01151
01161 public function process()
01162 {
01163
01164 if($this->conneg->getStatus() == 200)
01165 {
01166 $solr = new Solr($this->wsf_solr_core, $this->solr_host);
01167
01168 $solrQuery = "";
01169
01170
01171
01172 $accessibleDatasets = array();
01173
01174 $ws_al = new AuthLister("access_user", "", $this->registered_ip, $this->wsf_local_ip);
01175
01176 $ws_al->pipeline_conneg($this->conneg->getAccept(), $this->conneg->getAcceptCharset(),
01177 $this->conneg->getAcceptEncoding(), $this->conneg->getAcceptLanguage());
01178
01179 $ws_al->process();
01180
01181 $xml = new ProcessorXML();
01182 $xml->loadXML($ws_al->pipeline_getResultset());
01183
01184 $accesses = $xml->getSubjectsByType("wsf:Access");
01185
01186 foreach($accesses as $access)
01187 {
01188 $predicates = $xml->getPredicatesByType($access, "wsf:datasetAccess");
01189 $objects = $xml->getObjects($predicates->item(0));
01190 $datasetUri = $xml->getURI($objects->item(0));
01191
01192 $predicates = $xml->getPredicatesByType($access, "wsf:read");
01193 $objects = $xml->getObjects($predicates->item(0));
01194 $read = $xml->getContent($objects->item(0));
01195
01196 if(strtolower($read) == "true")
01197 {
01198 array_push($accessibleDatasets, $datasetUri);
01199 }
01200 }
01201
01202 if(count($accessibleDatasets) <= 0)
01203 {
01204 $this->conneg->setStatus(400);
01205 $this->conneg->setStatusMsg("Bad Request");
01206 $this->conneg->setStatusMsgExt($this->errorMessenger->_300->name);
01207 $this->conneg->setError($this->errorMessenger->_300->id, $this->errorMessenger->ws,
01208 $this->errorMessenger->_300->name, $this->errorMessenger->_300->description, "",
01209 $this->errorMessenger->_300->level);
01210 return;
01211 }
01212
01213 unset($ws_al);
01214
01215 if(strtolower($this->datasets) == "all")
01216 {
01217 $datasetList = "";
01218
01219 $solrQuery = "q=*:*&start=" . $this->page . "&rows=" . $this->items
01220 . (strtolower($this->includeAggregates) == "true" ? "&facet=true&facet.limit=-1&facet.field=type" .
01221 "&facet.field=attribute" . (strtolower($this->inference) == "on" ? "&facet.field=inferred_type" : "") . "&" .
01222 "facet.field=dataset&facet.mincount=1" : "");
01223
01224 foreach($accessibleDatasets as $key => $dataset)
01225 {
01226 if($key == 0)
01227 {
01228 $solrQuery .= "&fq=dataset:%22" . urlencode($dataset) . "%22";
01229 }
01230 else
01231 {
01232 $solrQuery .= " OR dataset:%22" . urlencode($dataset) . "%22";
01233 }
01234 }
01235 }
01236 else
01237 {
01238 $datasets = explode(";", $this->datasets);
01239
01240 $solrQuery = "q=*:*&start=" . $this->page . "&rows=" . $this->items
01241 . (strtolower($this->includeAggregates) == "true" ? "&facet=true&facet.limit=-1&facet.field=type" .
01242 "&facet.field=attribute" . (strtolower($this->inference) == "on" ? "&facet.field=inferred_type" : "") . "&" .
01243 "facet.field=dataset&facet.mincount=1" : "");
01244
01245 $solrQuery .= "&fq=dataset:%22%22";
01246
01247 foreach($datasets as $dataset)
01248 {
01249
01250 if(array_search($dataset, $accessibleDatasets) !== FALSE)
01251 {
01252
01253 $dataset = str_replace(array ("%3B", "%3b"), ";", $dataset);
01254
01255 $solrQuery .= " OR dataset:%22" . urlencode($dataset) . "%22";
01256 }
01257 }
01258 }
01259
01260 if($this->types != "all")
01261 {
01262
01263
01264 $types = explode(";", $this->types);
01265
01266 $nbProcessed = 0;
01267
01268 foreach($types as $type)
01269 {
01270
01271 $type = str_replace(array ("%3B", "%3b"), ";", $type);
01272
01273 if($nbProcessed == 0)
01274 {
01275 $solrQuery .= "&fq=type:%22" . urlencode($type) . "%22";
01276 }
01277 else
01278 {
01279 $solrQuery .= " OR type:%22" . urlencode($type) . "%22";
01280 }
01281
01282 $nbProcessed++;
01283
01284 if(strtolower($this->inference) == "on")
01285 {
01286 $solrQuery .= " OR inferred_type:%22" . urlencode($type) . "%22";
01287 }
01288 }
01289 }
01290
01291 if($this->attributes != "all")
01292 {
01293
01294
01295 $attributes = explode(";", $this->attributes);
01296
01297 $nbProcessed = 0;
01298
01299 foreach($attributes as $attribute)
01300 {
01301
01302 $type = str_replace(array ("%3B", "%3b"), ";", $type);
01303
01304 if($nbProcessed == 0)
01305 {
01306 $solrQuery .= "&fq=(attribute:%22" . urlencode($attribute) . "%22)";
01307 }
01308 else
01309 {
01310 $solrQuery .= " AND (attribute:%22" . urlencode($attribute) . "%22)";
01311 }
01312
01313 $nbProcessed++;
01314 }
01315 }
01316
01317 $resultset = $solr->select($solrQuery);
01318
01319 $domResultset = new DomDocument("1.0", "utf-8");
01320 $domResultset->loadXML($resultset);
01321
01322 $xpath = new DOMXPath($domResultset);
01323
01324
01325 $founds = $xpath->query("*[@numFound]");
01326
01327 foreach($founds as $found)
01328 {
01329 $nbResources = $found->attributes->getNamedItem("numFound")->nodeValue;
01330 break;
01331 }
01332
01333
01334 $founds = $xpath->query("//*/lst[@name='facet_fields']//lst[@name='type']/int");
01335
01336
01337
01338 $this->aggregates["type"] = array();
01339
01340 foreach($founds as $found)
01341 {
01342 $this->aggregates["type"][$found->attributes->getNamedItem("name")->nodeValue] = $found->nodeValue;
01343 }
01344
01345
01346
01347 if(strtolower($this->inference) == "on")
01348 {
01349
01350 $founds = $xpath->query("//*/lst[@name='facet_fields']//lst[@name='inferred_type']/int");
01351
01352
01353 $this->aggregates["inferred_type"] = array();
01354
01355 foreach($founds as $found)
01356 {
01357 $this->aggregates["inferred_type"][$found->attributes->getNamedItem("name")->nodeValue] = $found->nodeValue;
01358 }
01359 }
01360
01361
01362 $founds = $xpath->query("//*/lst[@name='facet_fields']//lst[@name='dataset']/int");
01363
01364 $this->aggregates["dataset"] = array();
01365
01366 foreach($founds as $found)
01367 {
01368 $this->aggregates["dataset"][$found->attributes->getNamedItem("name")->nodeValue] = $found->nodeValue;
01369 }
01370
01371
01372
01373 $founds = $xpath->query("//*/lst[@name='facet_fields']//lst[@name='attribute']/int");
01374
01375 $this->aggregates["attributes"] = array();
01376
01377 foreach($founds as $found)
01378 {
01379 $this->aggregates["attributes"][$found->attributes->getNamedItem("name")->nodeValue] = $found->nodeValue;
01380 }
01381
01382
01383
01384
01385 $resultsDom = $xpath->query("//doc");
01386
01387 foreach($resultsDom as $result)
01388 {
01389
01390 $resultURI = $xpath->query("arr[@name='uri']/str", $result);
01391
01392
01393 $uri = "";
01394
01395 foreach($resultURI as $u)
01396 {
01397 $uri = $u->nodeValue;
01398 $this->resultset[$uri] = array();
01399 break;
01400 }
01401
01402
01403 $resultDatasetURI = $xpath->query("arr[@name='dataset']", $result);
01404
01405
01406 $datasetUri = "";
01407
01408 foreach($resultDatasetURI as $u)
01409 {
01410 $this->resultset[$uri]["dataset"] = $u->nodeValue;
01411 break;
01412 }
01413
01414
01415
01416 $resultPrefLabelURI = $xpath->query("arr[@name='prefLabel']", $result);
01417
01418 foreach($resultPrefLabelURI as $u)
01419 {
01420 $this->resultset[$uri]["prefLabel"] = $u->nodeValue;
01421 break;
01422 }
01423
01424
01425 $resultAltLabelURI = $xpath->query("arr[@name='altLabel']", $result);
01426
01427 foreach($resultAltLabelURI as $u)
01428 {
01429 if(!isset($this->resultset[$uri]["altLabel"]))
01430 {
01431 $this->resultset[$uri]["altLabel"] = array( $u->nodeValue );
01432 }
01433 else
01434 {
01435 array_push($this->resultset[$uri]["altLabel"], $u->nodeValue);
01436 }
01437 }
01438
01439
01440 $resultDescriptionURI = $xpath->query("arr[@name='description']", $result);
01441
01442 foreach($resultDescriptionURI as $u)
01443 {
01444 $this->resultset[$uri]["description"] = $u->nodeValue;
01445 }
01446
01447
01448
01449 $resultProperties = $xpath->query("arr", $result);
01450
01451 $tempProperties = array();
01452
01453 foreach($resultProperties as $property)
01454 {
01455 $attribute = $property->getAttribute("name");
01456
01457
01458 $attributeType = "";
01459
01460 if(($pos = stripos($attribute, "_reify")) !== FALSE)
01461 {
01462 $attributeType = substr($attribute, $pos, strlen($attribute) - $pos);
01463 }
01464 elseif(($pos = stripos($attribute, "_attr")) !== FALSE)
01465 {
01466 $attributeType = substr($attribute, $pos, strlen($attribute) - $pos);
01467 }
01468
01469
01470 $attributeURI = urldecode(str_replace($attributeType, "", $attribute));
01471
01472 switch($attributeType)
01473 {
01474 case "_attr":
01475 $values = $property->getElementsByTagName("str");
01476
01477 foreach($values as $value)
01478 {
01479 if(!isset($this->resultset[$uri][$attributeURI]))
01480 {
01481 $this->resultset[$uri][$attributeURI] = array( $value->nodeValue );
01482 }
01483 else
01484 {
01485 array_push($this->resultset[$uri][$attributeURI], $value->nodeValue);
01486 }
01487 }
01488 break;
01489
01490 case "_attr_obj":
01491 $values = $property->getElementsByTagName("str");
01492
01493 foreach($values as $value)
01494 {
01495 if(!isset($this->resultsetObjectProperties[$uri][$attributeURI]))
01496 {
01497 $this->resultsetObjectProperties[$uri][$attributeURI] = array( $value->nodeValue );
01498 }
01499 else
01500 {
01501 array_push($this->resultsetObjectProperties[$uri][$attributeURI], $value->nodeValue);
01502 }
01503 }
01504 break;
01505
01506 case "_attr_obj_uri":
01507 $values = $property->getElementsByTagName("str");
01508
01509 foreach($values as $value)
01510 {
01511 if(!isset($this->resultsetObjectPropertiesUris[$uri][$attributeURI]))
01512 {
01513 $this->resultsetObjectPropertiesUris[$uri][$attributeURI] = array( $value->nodeValue );
01514 }
01515 else
01516 {
01517 array_push($this->resultsetObjectPropertiesUris[$uri][$attributeURI], $value->nodeValue);
01518 }
01519 }
01520 break;
01521
01522 case "_reify_attr":
01523 case "_reify_attr_obj":
01524 case "_reify_obj":
01525 case "_reify_value": break;
01526 }
01527 }
01528
01529
01530 $resultTypes = $xpath->query("arr[@name='type']/str", $result);
01531
01532 foreach($resultTypes as $t)
01533 {
01534 if($t->nodeValue != "-")
01535 {
01536 if(!isset($this->resultset[$uri]["type"]))
01537 {
01538 $this->resultset[$uri]["type"] = array( $t->nodeValue );
01539 }
01540 else
01541 {
01542 array_push($this->resultset[$uri]["type"], $t->nodeValue);
01543 }
01544 }
01545 }
01546 }
01547 }
01548 }
01549 }
01550
01551
01553
01554 ?>