00001 <?php
00002
00005
00026 class CrudRead extends WebService
00027 {
00029 private $db;
00030
00032 private $conneg;
00033
00035 private $dtdURL;
00036
00038 private $include_linksback = "";
00039
00041 private $include_reification = "";
00042
00044 private $resourceUri = "";
00045
00047 private $requester_ip = "";
00048
00050 private $registered_ip = "";
00051
00053 private $dataset = "";
00054
00056 private $datasetsDescription = array();
00057
00059 private $globalDataset = FALSE;
00060
00062 private $namespaces =
00063 array ("http://www.w3.org/2002/07/owl#" => "owl", "http://www.w3.org/1999/02/22-rdf-syntax-ns#" => "rdf",
00064 "http://www.w3.org/2000/01/rdf-schema#" => "rdfs", "http://purl.org/ontology/wsf#" => "wsf");
00065
00067 public $subjectTriples = array();
00068
00070 public $objectTriples = array();
00071
00073 public $reificationTriples = array();
00074
00076 public static $supportedSerializations =
00077 array ("application/bib+json", "application/iron+json", "application/json", "application/rdf+xml",
00078 "application/rdf+n3", "application/*", "text/xml", "text/*", "*/*");
00079
00081 private $errorMessenger =
00082 '{
00083 "ws": "/ws/crud/read/",
00084 "_200": {
00085 "id": "WS-CRUD-READ-200",
00086 "level": "Warning",
00087 "name": "No URI specified for any resource",
00088 "description": "No record URI defined for this query"
00089 },
00090 "_201": {
00091 "id": "WS-CRUD-READ-201",
00092 "level": "Warning",
00093 "name": "Missing Dataset URIs",
00094 "description": "Not all dataset URIs have been defined for each requested record URI. Remember that each URI of the list of URIs have to have a matching dataset URI in the datasets list."
00095 },
00096 "_202": {
00097 "id": "WS-CRUD-READ-202",
00098 "level": "Warning",
00099 "name": "Record URI(s) not existing or not accessible",
00100 "description": "The requested record URI(s) are not existing in this structWSF instance, or are not accessible to the requester. This error is only sent when no data URI are defined."
00101 },
00102 "_300": {
00103 "id": "WS-CRUD-READ-300",
00104 "level": "Warning",
00105 "name": "This resource is not existing",
00106 "description": "The target resource to be read is not existing in the system"
00107 },
00108 "_301": {
00109 "id": "WS-CRUD-READ-301",
00110 "level": "Warning",
00111 "name": "You can\'t read more than 64 resources at once",
00112 "description": "You are limited to read maximum 64 resources for each query to the CrudRead web service endpoint"
00113 },
00114 "_302": {
00115 "id": "WS-CRUD-READ-302",
00116 "level": "Fatal",
00117 "name": "Can\'t get the description of the resource(s)",
00118 "description": "An error occured when we tried to get the description of the resource(s)"
00119 },
00120 "_303": {
00121 "id": "WS-CRUD-READ-303",
00122 "level": "Fatal",
00123 "name": "Can\'t get the links-to the resource(s)",
00124 "description": "An error occured when we tried to get the links-to the resource(s)"
00125 },
00126 "_304": {
00127 "id": "WS-CRUD-READ-304",
00128 "level": "Fatal",
00129 "name": "Can\'t get the reification statements for that resource(s)",
00130 "description": "An error occured when we tried to get the reification statements of the resource(s)"
00131 }
00132
00133 }';
00134
00135
00158 function __construct($uri, $dataset, $include_linksback, $include_reification, $registered_ip, $requester_ip)
00159 {
00160 parent::__construct();
00161
00162 $this->db = new DB_Virtuoso($this->db_username, $this->db_password, $this->db_dsn, $this->db_host);
00163
00164 $this->dataset = $dataset;
00165
00166
00167 if($this->dataset == "")
00168 {
00169 $this->globalDataset = TRUE;
00170 }
00171
00172 $this->resourceUri = $uri;
00173
00174 $this->include_linksback = $include_linksback;
00175 $this->include_reification = $include_reification;
00176 $this->requester_ip = $requester_ip;
00177
00178 if($registered_ip == "")
00179 {
00180 $this->registered_ip = $requester_ip;
00181 }
00182 else
00183 {
00184 $this->registered_ip = $registered_ip;
00185 }
00186
00187 if(strtolower(substr($this->registered_ip, 0, 4)) == "self")
00188 {
00189 $pos = strpos($this->registered_ip, "::");
00190
00191 if($pos !== FALSE)
00192 {
00193 $account = substr($this->registered_ip, $pos + 2, strlen($this->registered_ip) - ($pos + 2));
00194
00195 $this->registered_ip = $requester_ip . "::" . $account;
00196 }
00197 else
00198 {
00199 $this->registered_ip = $requester_ip;
00200 }
00201 }
00202
00203 $this->uri = $this->wsf_base_url . "/wsf/ws/crud/read/";
00204 $this->title = "Crud Read Web Service";
00205 $this->crud_usage = new CrudUsage(FALSE, TRUE, FALSE, FALSE);
00206 $this->endpoint = $this->wsf_base_url . "/ws/crud/read/";
00207
00208 $this->dtdURL = "crud/crudRead.dtd";
00209
00210 $this->errorMessenger = json_decode($this->errorMessenger);
00211 }
00212
00213 function __destruct()
00214 {
00215 parent::__destruct();
00216
00217 if(isset($this->db))
00218 {
00219 @$this->db->close();
00220 }
00221 }
00222
00235 protected function validateQuery()
00236 {
00237
00238
00239
00240
00241
00242 if($this->globalDataset === TRUE)
00243 {
00244 include_once("../../auth/lister/AuthLister.php");
00245
00246 $ws_al = new AuthLister("access_user", "", $this->registered_ip, $this->wsf_local_ip);
00247
00248 $ws_al->pipeline_conneg($this->conneg->getAccept(), $this->conneg->getAcceptCharset(),
00249 $this->conneg->getAcceptEncoding(), $this->conneg->getAcceptLanguage());
00250
00251 $ws_al->process();
00252
00253 $xml = new ProcessorXML();
00254 $xml->loadXML($ws_al->pipeline_getResultset());
00255
00256 $accesses = $xml->getSubjectsByType("wsf:Access");
00257
00258 $accessibleDatasets = array();
00259
00260 foreach($accesses as $access)
00261 {
00262 $predicates = $xml->getPredicatesByType($access, "wsf:datasetAccess");
00263 $objects = $xml->getObjects($predicates->item(0));
00264 $datasetUri = $xml->getURI($objects->item(0));
00265
00266 $predicates = $xml->getPredicatesByType($access, "wsf:read");
00267 $objects = $xml->getObjects($predicates->item(0));
00268 $read = $xml->getContent($objects->item(0));
00269
00270 if(strtolower($read) == "true")
00271 {
00272 $this->dataset .= "$datasetUri;";
00273 array_push($accessibleDatasets, $datasetUri);
00274 }
00275 }
00276
00277 if(count($accessibleDatasets) <= 0)
00278 {
00279 $this->conneg->setStatus(400);
00280 $this->conneg->setStatusMsg("Bad Request");
00281 $this->conneg->setStatusMsgExt($this->errorMessenger->_202->name);
00282 $this->conneg->setError($this->errorMessenger->_202->id, $this->errorMessenger->ws,
00283 $this->errorMessenger->_202->name, $this->errorMessenger->_202->description, "",
00284 $this->errorMessenger->_202->level);
00285
00286 return;
00287 }
00288
00289 unset($ws_al);
00290
00291 $this->dataset = rtrim($this->dataset, ";");
00292 }
00293 else
00294 {
00295 $datasets = explode(";", $this->dataset);
00296
00297 $datasets = array_unique($datasets);
00298
00299
00300 foreach($datasets as $dataset)
00301 {
00302
00303 $ws_av = new AuthValidator($this->requester_ip, $dataset, $this->uri);
00304
00305 $ws_av->pipeline_conneg("text/xml", $this->conneg->getAcceptCharset(), $this->conneg->getAcceptEncoding(),
00306 $this->conneg->getAcceptLanguage());
00307
00308 $ws_av->process();
00309
00310 if($ws_av->pipeline_getResponseHeaderStatus() != 200)
00311 {
00312 $this->conneg->setStatus($ws_av->pipeline_getResponseHeaderStatus());
00313 $this->conneg->setStatusMsg($ws_av->pipeline_getResponseHeaderStatusMsg());
00314 $this->conneg->setStatusMsgExt($ws_av->pipeline_getResponseHeaderStatusMsgExt());
00315 $this->conneg->setError($ws_av->pipeline_getError()->id, $ws_av->pipeline_getError()->webservice,
00316 $ws_av->pipeline_getError()->name, $ws_av->pipeline_getError()->description,
00317 $ws_av->pipeline_getError()->debugInfo, $ws_av->pipeline_getError()->level);
00318
00319 return;
00320 }
00321
00322 unset($ws_av);
00323
00324
00325 $ws_av = new AuthValidator($this->registered_ip, $dataset, $this->uri);
00326
00327 $ws_av->pipeline_conneg("text/xml", $this->conneg->getAcceptCharset(), $this->conneg->getAcceptEncoding(),
00328 $this->conneg->getAcceptLanguage());
00329
00330 $ws_av->process();
00331
00332 if($ws_av->pipeline_getResponseHeaderStatus() != 200)
00333 {
00334 $this->conneg->setStatus($ws_av->pipeline_getResponseHeaderStatus());
00335 $this->conneg->setStatusMsg($ws_av->pipeline_getResponseHeaderStatusMsg());
00336 $this->conneg->setStatusMsgExt($ws_av->pipeline_getResponseHeaderStatusMsgExt());
00337 $this->conneg->setError($ws_av->pipeline_getError()->id, $ws_av->pipeline_getError()->webservice,
00338 $ws_av->pipeline_getError()->name, $ws_av->pipeline_getError()->description,
00339 $ws_av->pipeline_getError()->debugInfo, $ws_av->pipeline_getError()->level);
00340 return;
00341 }
00342 }
00343 }
00344 }
00345
00356 public function pipeline_getError() { return ($this->conneg->error); }
00357
00358
00369 public function pipeline_getResultset()
00370 {
00371 $xml = new ProcessorXML();
00372
00373
00374 $resultset = $xml->createResultset();
00375
00376
00377 $void = $xml->createPrefix("owl", "http://www.w3.org/2002/07/owl#");
00378 $resultset->appendChild($void);
00379 $rdf = $xml->createPrefix("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
00380 $resultset->appendChild($rdf);
00381 $dcterms = $xml->createPrefix("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
00382 $resultset->appendChild($dcterms);
00383 $dcterms = $xml->createPrefix("wsf", "http://purl.org/ontology/wsf#");
00384 $resultset->appendChild($dcterms);
00385
00386 $subject;
00387
00388 foreach($this->subjectTriples as $u => $sts)
00389 {
00390 if(isset($sts["http://www.w3.org/1999/02/22-rdf-syntax-ns#type"]))
00391 {
00392 foreach($sts["http://www.w3.org/1999/02/22-rdf-syntax-ns#type"] as $key => $type)
00393 {
00394 if($key > 0)
00395 {
00396 $pred = $xml->createPredicate("http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
00397 $object = $xml->createObject("", $type[0]);
00398 $pred->appendChild($object);
00399 $subject->appendChild($pred);
00400 }
00401 else
00402 {
00403 $subject = $xml->createSubject($type[0], $u);
00404 }
00405 }
00406 }
00407 else
00408 {
00409 $subject = $xml->createSubject("http://www.w3.org/2002/07/owl#Thing", $u);
00410 }
00411
00412 foreach($sts as $property => $values)
00413 {
00414 if($property != "http://www.w3.org/1999/02/22-rdf-syntax-ns#type")
00415 {
00416 foreach($values as $value)
00417 {
00418 if($value[1] != NULL)
00419 {
00420
00421
00422
00423
00424 $pred = $xml->createPredicate($property);
00425 $object = $xml->createObjectContent($this->xmlEncode($value[0]));
00426 $pred->appendChild($object);
00427
00428 if(isset($this->reificationTriples[$u][$property][$value[0]]))
00429 {
00430 foreach($this->reificationTriples[$u][$property][$value[0]] as $rStatement => $rValue)
00431 {
00432 foreach($rValue as $rv)
00433 {
00434 $reify = $xml->createReificationStatement($rStatement, $rv);
00435 $object->appendChild($reify);
00436 }
00437 }
00438 }
00439
00440 $subject->appendChild($pred);
00441 }
00442 else
00443 {
00444 $pred = $xml->createPredicate($property);
00445 $object = $xml->createObject("", $value[0]);
00446 $pred->appendChild($object);
00447
00448 if(isset($this->reificationTriples[$u][$property][$value[0]]))
00449 {
00450 foreach($this->reificationTriples[$u][$property][$value[0]] as $rStatement => $rValue)
00451 {
00452 foreach($rValue as $rv)
00453 {
00454 $reify = $xml->createReificationStatement($rStatement, $rv);
00455 $object->appendChild($reify);
00456 }
00457 }
00458 }
00459
00460 $subject->appendChild($pred);
00461 }
00462 }
00463 }
00464 }
00465
00466 $resultset->appendChild($subject);
00467
00468
00469 if(count($this->objectTriples[$u]) > 0)
00470 {
00471 foreach($this->objectTriples[$u] as $property => $propertyValue)
00472 {
00473 foreach($propertyValue as $resource)
00474 {
00475 $subject = $xml->createSubject("http://www.w3.org/2002/07/owl#Thing", $resource);
00476
00477 $pred = $xml->createPredicate($property);
00478 $object = $xml->createObject("", $u);
00479 $pred->appendChild($object);
00480 $subject->appendChild($pred);
00481
00482 $resultset->appendChild($subject);
00483 }
00484 }
00485 }
00486 }
00487
00488 return ($this->injectDoctype($xml->saveXML($resultset)));
00489 }
00490
00503 public function injectDoctype($xmlDoc)
00504 {
00505 $posHeader = strpos($xmlDoc, '"?>') + 3;
00506 $xmlDoc = substr($xmlDoc, 0, $posHeader)
00507 . "\n<!DOCTYPE resultset PUBLIC \"-//Structured Dynamics LLC//Crud Read DTD 0.1//EN\" \"" . $this->dtdBaseURL
00508 . $this->dtdURL . "\">" . substr($xmlDoc, $posHeader, strlen($xmlDoc) - $posHeader);
00509
00510 return ($xmlDoc);
00511 }
00512
00531 public function ws_conneg($accept, $accept_charset, $accept_encoding, $accept_language)
00532 {
00533 $this->conneg =
00534 new Conneg($accept, $accept_charset, $accept_encoding, $accept_language, CrudRead::$supportedSerializations);
00535
00536
00537 $this->validateQuery();
00538
00539
00540 if($this->conneg->getStatus() == 200)
00541 {
00542
00543 if($this->resourceUri == "")
00544 {
00545 $this->conneg->setStatus(400);
00546 $this->conneg->setStatusMsg("Bad Request");
00547 $this->conneg->setStatusMsgExt($this->errorMessenger->_200->name);
00548 $this->conneg->setError($this->errorMessenger->_200->id, $this->errorMessenger->ws,
00549 $this->errorMessenger->_200->name, $this->errorMessenger->_200->description, "",
00550 $this->errorMessenger->_200->level);
00551
00552 return;
00553 }
00554 }
00555
00556
00557 if($this->globalDataset === FALSE)
00558 {
00559 $uris = explode(";", $this->resourceUri);
00560 $datasets = explode(";", $this->dataset);
00561
00562 if(count($uris) != count($datasets))
00563 {
00564 $this->conneg->setStatus(400);
00565 $this->conneg->setStatusMsg("Bad Request");
00566 $this->conneg->setStatusMsgExt($this->errorMessenger->_201->name);
00567 $this->conneg->setError($this->errorMessenger->_201->id, $this->errorMessenger->ws,
00568 $this->errorMessenger->_201->name, $this->errorMessenger->_201->description, "",
00569 $this->errorMessenger->_201->level);
00570
00571 return;
00572 }
00573 }
00574 }
00575
00594 public function pipeline_conneg($accept, $accept_charset, $accept_encoding, $accept_language)
00595 { $this->ws_conneg($accept, $accept_charset, $accept_encoding, $accept_language); }
00596
00607 public function pipeline_getResponseHeaderStatus() { return $this->conneg->getStatus(); }
00608
00619 public function pipeline_getResponseHeaderStatusMsg() { return $this->conneg->getStatusMsg(); }
00620
00633 public function pipeline_getResponseHeaderStatusMsgExt() { return $this->conneg->getStatusMsgExt(); }
00634
00645 public function pipeline_serialize()
00646 {
00647 $rdf_part = "";
00648
00649 switch($this->conneg->getMime())
00650 {
00651 case "application/bib+json":
00652 case "application/iron+json":
00653 include_once("../../converter/irjson/ConverterIrJSON.php");
00654 include_once("../../converter/irjson/Dataset.php");
00655 include_once("../../converter/irjson/InstanceRecord.php");
00656 include_once("../../converter/irjson/LinkageSchema.php");
00657 include_once("../../converter/irjson/StructureSchema.php");
00658 include_once("../../converter/irjson/irJSONParser.php");
00659
00660
00661 $documentToConvert = $this->pipeline_getResultset();
00662
00663 $datasets = explode(";", $this->dataset);
00664
00665 $datasets = array_unique($datasets);
00666
00667
00669 $d = $datasets[0];
00670
00671 $d = str_replace("/wsf/datasets/", "/conStruct/datasets/", $d) . "resource/";
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682 $ws_irv =
00683 new ConverterIrJSON($documentToConvert, "text/xml", "true", $this->registered_ip, $this->requester_ip);
00684
00685 $ws_irv->pipeline_conneg("application/iron+json", $this->conneg->getAcceptCharset(),
00686 $this->conneg->getAcceptEncoding(), $this->conneg->getAcceptLanguage());
00687
00688 $ws_irv->process();
00689
00690 if($ws_irv->pipeline_getResponseHeaderStatus() != 200)
00691 {
00692 $this->conneg->setStatus($ws_irv->pipeline_getResponseHeaderStatus());
00693 $this->conneg->setStatusMsg($ws_irv->pipeline_getResponseHeaderStatusMsg());
00694 $this->conneg->setStatusMsgExt($ws_irv->pipeline_getResponseHeaderStatusMsgExt());
00695 $this->conneg->setError($ws_irv->pipeline_getError()->id, $ws_irv->pipeline_getError()->webservice,
00696 $ws_irv->pipeline_getError()->name, $ws_irv->pipeline_getError()->description,
00697 $ws_irv->pipeline_getError()->debugInfo, $ws_irv->pipeline_getError()->level);
00698 return;
00699 }
00700
00701 return ($ws_irv->pipeline_serialize());
00702
00703 break;
00704
00705 case "application/json":
00706 $json_part = "";
00707 $xml = new ProcessorXML();
00708 $xml->loadXML($this->pipeline_getResultset());
00709
00710 $subjects = $xml->getSubjects();
00711
00712 $nsId = 0;
00713
00714 foreach($subjects as $subject)
00715 {
00716 $subjectURI = $xml->getURI($subject);
00717 $subjectType = $xml->getType($subject);
00718
00719 $ns = $this->getNamespace($subjectType);
00720
00721 if(!isset($this->namespaces[$ns[0]]))
00722 {
00723 $this->namespaces[$ns[0]] = "ns" . $nsId;
00724 $nsId++;
00725 }
00726
00727 $json_part .= " { \n";
00728 $json_part .= " \"uri\": \"" . parent::jsonEncode($subjectURI) . "\", \n";
00729 $json_part .= " \"type\": \"" . parent::jsonEncode($this->namespaces[$ns[0]] . ":" . $ns[1])
00730 . "\", \n";
00731
00732 $predicates = $xml->getPredicates($subject);
00733
00734 $nbPredicates = 0;
00735
00736 foreach($predicates as $predicate)
00737 {
00738 $objects = $xml->getObjects($predicate);
00739
00740 foreach($objects as $object)
00741 {
00742 $nbPredicates++;
00743
00744 if($nbPredicates == 1)
00745 {
00746 $json_part .= " \"predicate\": [ \n";
00747 }
00748
00749 $objectType = $xml->getType($object);
00750 $predicateType = $xml->getType($predicate);
00751
00752 if($objectType == "rdfs:Literal")
00753 {
00754 $objectValue = $xml->getContent($object);
00755
00756 $ns = $this->getNamespace($predicateType);
00757
00758 if(!isset($this->namespaces[$ns[0]]))
00759 {
00760 $this->namespaces[$ns[0]] = "ns" . $nsId;
00761 $nsId++;
00762 }
00763
00764 $json_part .= " { \n";
00765 $json_part .= " \"" . parent::jsonEncode($this->namespaces[$ns[0]] . ":" . $ns[1]) . "\": \""
00766 . parent::jsonEncode($objectValue) . "\" \n";
00767 $json_part .= " },\n";
00768 }
00769 else
00770 {
00771 $objectURI = $xml->getURI($object);
00772
00773 $ns = $this->getNamespace($predicateType);
00774
00775 if(!isset($this->namespaces[$ns[0]]))
00776 {
00777 $this->namespaces[$ns[0]] = "ns" . $nsId;
00778 $nsId++;
00779 }
00780
00781 $json_part .= " { \n";
00782 $json_part .= " \"" . parent::jsonEncode($this->namespaces[$ns[0]] . ":" . $ns[1])
00783 . "\": { \n";
00784 $json_part .= " \"uri\": \"" . parent::jsonEncode($objectURI) . "\",\n";
00785
00786
00787 $reifies = $xml->getReificationStatements($object, "wsf:objectLabel");
00788
00789 $nbReification = 0;
00790
00791 foreach($reifies as $reify)
00792 {
00793 $nbReification++;
00794
00795 if($nbReification > 0)
00796 {
00797 $json_part .= " \"reify\": [\n";
00798 }
00799
00800 $json_part .= " { \n";
00801 $json_part .= " \"type\": \"wsf:objectLabel\", \n";
00802 $json_part .= " \"value\": \"" . parent::jsonEncode($xml->getValue($reify))
00803 . "\" \n";
00804 $json_part .= " },\n";
00805 }
00806
00807 if($nbReification > 0)
00808 {
00809 $json_part = substr($json_part, 0, strlen($json_part) - 2) . "\n";
00810
00811 $json_part .= " ]\n";
00812 }
00813 else
00814 {
00815 $json_part = substr($json_part, 0, strlen($json_part) - 2) . "\n";
00816 }
00817
00818 $json_part .= " } \n";
00819 $json_part .= " },\n";
00820 }
00821 }
00822 }
00823
00824 if(strlen($json_part) > 0)
00825 {
00826 $json_part = substr($json_part, 0, strlen($json_part) - 2) . "\n";
00827 }
00828
00829 if($nbPredicates > 0)
00830 {
00831 $json_part .= " ]\n";
00832 }
00833
00834 $json_part .= " },\n";
00835 }
00836
00837 if(strlen($json_part) > 0)
00838 {
00839 $json_part = substr($json_part, 0, strlen($json_part) - 2) . "\n";
00840 }
00841
00842 $json_header .= " \"prefixes\": [ \n";
00843 $json_header .= " {\n";
00844 $json_header .= " \"rdf\": \"http://www.w3.org/1999/02/22-rdf-syntax-ns#\",\n";
00845 $json_header .= " \"wsf\": \"http://purl.org/ontology/wsf#\",\n";
00846
00847 foreach($this->namespaces as $ns => $prefix)
00848 {
00849 $json_header .= " \"$prefix\": \"$ns\",\n";
00850 }
00851
00852 if(strlen($json_header) > 0)
00853 {
00854 $json_header = substr($json_header, 0, strlen($json_header) - 2) . "\n";
00855 }
00856
00857 $json_header .= " } \n";
00858 $json_header .= " ],\n";
00859 $json_header .= " \"resultset\": {\n";
00860 $json_header .= " \"subject\": [\n";
00861 $json_header .= $json_part;
00862 $json_header .= " ]\n";
00863 $json_header .= " }\n";
00864
00865 return ($json_header);
00866
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926
00927
00928
00929
00930
00931
00932
00933
00934
00935
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945
00946
00947
00948
00949
00950
00951
00952
00953
00954
00955
00956
00957
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972
00973
00974
00975
00976
00977
00978 break;
00979
00980 case "application/rdf+n3":
00981
00982 $xml = new ProcessorXML();
00983 $xml->loadXML($this->pipeline_getResultset());
00984
00985 $subjects = $xml->getSubjects();
00986
00987 foreach($subjects as $subject)
00988 {
00989 $subjectURI = $xml->getURI($subject);
00990 $subjectType = $xml->getType($subject, FALSE);
00991
00992 $rdf_part .= "\n <$subjectURI> a <$subjectType> ;\n";
00993
00994 $predicates = $xml->getPredicates($subject);
00995
00996 foreach($predicates as $predicate)
00997 {
00998 $objects = $xml->getObjects($predicate);
00999
01000 foreach($objects as $object)
01001 {
01002 $objectType = $xml->getType($object);
01003 $predicateType = $xml->getType($predicate, FALSE);
01004
01005 if($objectType == "rdfs:Literal")
01006 {
01007 $objectValue = $xml->getContent($object);
01008 $rdf_part .= " <$predicateType> \"\"\"" . str_replace(array( "\\" ), "\\\\", $objectValue)
01009 . "\"\"\" ;\n";
01010 }
01011 else
01012 {
01013 $objectURI = $xml->getURI($object);
01014 $rdf_part .= " <$predicateType> <$objectURI> ;\n";
01015 }
01016 }
01017 }
01018
01019 if(strlen($rdf_part) > 0)
01020 {
01021 $rdf_part = substr($rdf_part, 0, strlen($rdf_part) - 2) . ". \n";
01022 }
01023 }
01024
01025 return ($rdf_part);
01026 break;
01027
01028 case "application/rdf+xml":
01029 $xml = new ProcessorXML();
01030 $xml->loadXML($this->pipeline_getResultset());
01031
01032 $subjects = $xml->getSubjects();
01033
01034 $nsId = 0;
01035
01036 foreach($subjects as $subject)
01037 {
01038 $subjectURI = $xml->getURI($subject);
01039 $subjectType = $xml->getType($subject);
01040
01041 $ns1 = $this->getNamespace($subjectType);
01042
01043 if(!isset($this->namespaces[$ns1[0]]))
01044 {
01045 $this->namespaces[$ns1[0]] = "ns" . $nsId;
01046 $nsId++;
01047 }
01048
01049 $rdf_part .= "\n <" . $this->namespaces[$ns1[0]] . ":" . $ns1[1] . " rdf:about=\"$subjectURI\">\n";
01050
01051 $predicates = $xml->getPredicates($subject);
01052
01053 foreach($predicates as $predicate)
01054 {
01055 $objects = $xml->getObjects($predicate);
01056
01057 foreach($objects as $object)
01058 {
01059 $objectType = $xml->getType($object);
01060 $predicateType = $xml->getType($predicate);
01061
01062 if($objectType == "rdfs:Literal")
01063 {
01064 $objectValue = $xml->getContent($object);
01065
01066 $ns = $this->getNamespace($predicateType);
01067
01068 if(!isset($this->namespaces[$ns[0]]))
01069 {
01070 $this->namespaces[$ns[0]] = "ns" . $nsId;
01071 $nsId++;
01072 }
01073
01074 $rdf_part .= " <" . $this->namespaces[$ns[0]] . ":" . $ns[1] . ">"
01075 . $this->xmlEncode($objectValue) . "</" . $this->namespaces[$ns[0]] . ":" . $ns[1] . ">\n";
01076 }
01077 else
01078 {
01079 $objectURI = $xml->getURI($object);
01080
01081 $ns = $this->getNamespace($predicateType);
01082
01083 if(!isset($this->namespaces[$ns[0]]))
01084 {
01085 $this->namespaces[$ns[0]] = "ns" . $nsId;
01086 $nsId++;
01087 }
01088
01089 $rdf_part .= " <" . $this->namespaces[$ns[0]] . ":" . $ns[1]
01090 . " rdf:resource=\"$objectURI\" />\n";
01091 }
01092 }
01093 }
01094
01095 $rdf_part .= " </" . $this->namespaces[$ns1[0]] . ":" . $ns1[1] . ">\n";
01096 }
01097
01098 $rdf_header = "<rdf:RDF ";
01099
01100 foreach($this->namespaces as $ns => $prefix)
01101 {
01102 $rdf_header .= " xmlns:$prefix=\"$ns\"";
01103 }
01104
01105 $rdf_header .= ">\n\n";
01106
01107 $rdf_part = $rdf_header . $rdf_part;
01108
01109 return ($rdf_part);
01110 break;
01111 }
01112 }
01113
01126 private function getNamespace($uri)
01127 {
01128 $pos = strrpos($uri, "#");
01129
01130 if($pos !== FALSE)
01131 {
01132 return array (substr($uri, 0, $pos) . "#", substr($uri, $pos + 1, strlen($uri) - ($pos + 1)));
01133 }
01134 else
01135 {
01136 $pos = strrpos($uri, "/");
01137
01138 if($pos !== FALSE)
01139 {
01140 return array (substr($uri, 0, $pos) . "/", substr($uri, $pos + 1, strlen($uri) - ($pos + 1)));
01141 }
01142 else
01143 {
01144 $pos = strpos($uri, ":");
01145
01146 if($pos !== FALSE)
01147 {
01148 $nsUri = explode(":", $uri, 2);
01149
01150 foreach($this->namespaces as $uri2 => $prefix2)
01151 {
01152 $uri2 = urldecode($uri2);
01153
01154 if($prefix2 == $nsUri[0])
01155 {
01156 return (array ($uri2, $nsUri[1]));
01157 }
01158 }
01159
01160 return explode(":", $uri, 2);
01161 }
01162 }
01163 }
01164
01165 return (FALSE);
01166 }
01167
01176 public function pipeline_serialize_reification()
01177 {
01178 $rdf_reification = "";
01179
01180 switch($this->conneg->getMime())
01181 {
01182 case "application/rdf+n3":
01183 $xml = new ProcessorXML();
01184 $xml->loadXML($this->pipeline_getResultset());
01185
01186 $subjects = $xml->getSubjects();
01187
01188 $bnodeCounter = 0;
01189
01190 foreach($subjects as $subject)
01191 {
01192 $predicates = $xml->getPredicates($subject);
01193
01194 foreach($predicates as $predicate)
01195 {
01196 $predicateType = $xml->getType($predicate, FALSE);
01197
01198 $objects = $xml->getObjects($predicate);
01199
01200 foreach($objects as $object)
01201 {
01202 $reifies = $xml->getReificationStatements($object);
01203
01204 foreach($reifies as $reify)
01205 {
01206 $reifyPredicate = $xml->getType($reify, FALSE);
01207
01208 $ns = $this->getNamespace($reifyPredicate);
01209
01210 if(!isset($this->namespaces[$ns[0]]))
01211 {
01212 $this->namespaces[$ns[0]] = "ns" . $nsId;
01213 $nsId++;
01214 }
01215
01216 $rdf_reification .= "_:" . md5($xml->getURI($subject) . $predicateType . $xml->getURI($object))
01217 . " a rdf:Statement ;\n";
01218 $bnodeCounter++;
01219 $rdf_reification .= " rdf:subject <" . $xml->getURI($subject) . "> ;\n";
01220 $rdf_reification .= " rdf:predicate <" . $predicateType . "> ;\n";
01221 $rdf_reification .= " rdf:object <" . $xml->getURI($object) . "> ;\n";
01222 $rdf_reification .= " " . $this->namespaces[$ns[0]] . ":" . $ns[1] . " \"" . $xml->getValue($reify)
01223 . "\" .\n\n";
01224 $bnodeCounter++;
01225 }
01226 }
01227 }
01228 }
01229
01230 return ($rdf_reification);
01231
01232 break;
01233
01234 case "application/rdf+xml":
01235 $xml = new ProcessorXML();
01236 $xml->loadXML($this->pipeline_getResultset());
01237
01238 $subjects = $xml->getSubjects();
01239
01240 foreach($subjects as $subject)
01241 {
01242 $predicates = $xml->getPredicates($subject);
01243
01244 foreach($predicates as $predicate)
01245 {
01246 $predicateType = $xml->getType($predicate, FALSE);
01247
01248 $objects = $xml->getObjects($predicate);
01249
01250 foreach($objects as $object)
01251 {
01252 $reifies = $xml->getReificationStatements($object);
01253
01254 foreach($reifies as $reify)
01255 {
01256 $reifyPredicate = $xml->getType($reify, FALSE);
01257
01258 $ns = $this->getNamespace($reifyPredicate);
01259
01260 if(!isset($this->namespaces[$ns[0]]))
01261 {
01262 $this->namespaces[$ns[0]] = "ns" . $nsId;
01263 $nsId++;
01264 }
01265
01266 $rdf_reification .= "<rdf:Statement rdf:about=\""
01267 . md5($xml->getURI($subject) . $predicateType . $xml->getURI($object)) . "\">\n";
01268 $rdf_reification .= " <rdf:subject rdf:resource=\"" . $xml->getURI($subject) . "\" />\n";
01269 $rdf_reification .= " <rdf:predicate rdf:resource=\"" . $predicateType . "\" />\n";
01270 $rdf_reification .= " <rdf:object rdf:resource=\"" . $xml->getURI($object) . "\" />\n";
01271 $rdf_reification .= " <" . $this->namespaces[$ns[0]] . ":" . $ns[1] . ">"
01272 . $this->xmlEncode($xml->getValue($reify)) . "</$reifyPredicate>\n";
01273 $rdf_reification .= "</rdf:Statement> \n\n";
01274 }
01275 }
01276 }
01277 }
01278
01279 return ($rdf_reification);
01280
01281 break;
01282 }
01283 }
01284
01295 public function ws_serialize()
01296 {
01297 switch($this->conneg->getMime())
01298 {
01299 case "application/rdf+n3":
01300 $rdf_document = "";
01301 $rdf_document .= "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n";
01302
01303 $rdf_document .= $this->pipeline_serialize();
01304
01305 $rdf_document .= $this->pipeline_serialize_reification();
01306
01307 return $rdf_document;
01308 break;
01309
01310 case "application/rdf+xml":
01311 $rdf_document = "";
01312 $rdf_document .= "<?xml version=\"1.0\"?>\n";
01313
01314 $rdf_document .= $this->pipeline_serialize();
01315
01316 $rdf_document .= $this->pipeline_serialize_reification();
01317
01318 $rdf_document .= "</rdf:RDF>";
01319
01320 return $rdf_document;
01321 break;
01322
01323 case "text/xml":
01324 return $this->pipeline_getResultset();
01325 break;
01326
01327 case "application/json":
01328
01329
01330
01331
01332
01333
01334
01335
01336
01337
01338
01339
01340 $json_document = "";
01341 $json_document .= "{\n";
01342 $json_document .= $this->pipeline_serialize();
01343 $json_document .= "}";
01344
01345 return ($json_document);
01346 break;
01347
01348 case "application/bib+json":
01349 case "application/iron+json":
01350 return ($this->pipeline_serialize());
01351 break;
01352 }
01353 }
01354
01367 public function ws_respond($content)
01368 {
01369
01370 $this->conneg->respond();
01371
01372
01373
01374
01375 if($this->conneg->getStatus() == 200)
01376 {
01377 echo $content;
01378 }
01379
01380 $this->__destruct();
01381 }
01382
01383
01392 public function process()
01393 {
01394
01395 if($this->conneg->getStatus() == 200)
01396 {
01397 $uris = explode(";", $this->resourceUri);
01398 $datasets = explode(";", $this->dataset);
01399
01400 if(count($uris) > 64)
01401 {
01402 $this->conneg->setStatus(400);
01403 $this->conneg->setStatusMsg("Bad Request");
01404 $this->conneg->setStatusMsgExt($this->errorMessenger->_301->name);
01405 $this->conneg->setError($this->errorMessenger->_301->id, $this->errorMessenger->ws,
01406 $this->errorMessenger->_301->name, $this->errorMessenger->_301->description, "",
01407 $this->errorMessenger->_301->level);
01408
01409 return;
01410 }
01411
01412 foreach($uris as $key => $u)
01413 {
01414
01415
01416 $u = str_ireplace("%3B", ";", $u);
01417 $d = str_ireplace("%3B", ";", $datasets[$key]);
01418
01419 $query = "";
01420
01421 if($this->globalDataset === FALSE)
01422 {
01423 $d = str_ireplace("%3B", ";", $datasets[$key]);
01424
01425
01426 $query = $this->db->build_sparql_query("select ?p ?o (DATATYPE(?o)) as ?otype (LANG(?o)) as ?olang "
01427 . "from <" . $d . "> where {<" . $u
01428 . "> ?p ?o.}", array ('p', 'o', 'otype', 'olang'), FALSE);
01429 }
01430 else
01431 {
01432 $d = "";
01433
01434 foreach($datasets as $dataset)
01435 {
01436 if($dataset != "")
01437 {
01438 $d .= " from named <$dataset> ";
01439 }
01440 }
01441
01442
01443 $query = $this->db->build_sparql_query("select ?p ?o (DATATYPE(?o)) as ?otype (LANG(?o)) as ?olang "
01444 . " $d where {graph ?g{<" . $u
01445 . "> ?p ?o.}}", array ('p', 'o', 'otype', 'olang'), FALSE);
01446 }
01447
01448 $resultset = $this->db->query($query);
01449
01450 if(odbc_error())
01451 {
01452 $this->conneg->setStatus(500);
01453 $this->conneg->setStatusMsg("Internal Error");
01454 $this->conneg->setStatusMsgExt($this->errorMessenger->_302->name);
01455 $this->conneg->setError($this->errorMessenger->_302->id, $this->errorMessenger->ws,
01456 $this->errorMessenger->_302->name, $this->errorMessenger->_302->description, odbc_errormsg(),
01457 $this->errorMessenger->_302->level);
01458 }
01459
01460 while(odbc_fetch_row($resultset))
01461 {
01462 $p = odbc_result($resultset, 1);
01463 $o = odbc_result($resultset, 2);
01464
01465 $otype = odbc_result($resultset, 3);
01466 $olang = odbc_result($resultset, 4);
01467
01468 if(!isset($this->subjectTriples[$u][$p]))
01469 {
01470 $this->subjectTriples[$u][$p] = array();
01471 }
01472
01473 if($olang && $olang != "")
01474 {
01475
01476 array_push($this->subjectTriples[$u][$p], array ($o, "http://www.w3.org/2001/XMLSchema#string"));
01477 }
01478 else
01479 {
01480 array_push($this->subjectTriples[$u][$p], array ($o, $otype));
01481 }
01482 }
01483
01484 if(count($this->subjectTriples) <= 0)
01485 {
01486 $this->conneg->setStatus(400);
01487 $this->conneg->setStatusMsg("Bad Request");
01488 $this->conneg->setStatusMsgExt($this->errorMessenger->_300->name);
01489 $this->conneg->setError($this->errorMessenger->_300->id, $this->errorMessenger->ws,
01490 $this->errorMessenger->_300->name, $this->errorMessenger->_300->description, "",
01491 $this->errorMessenger->_300->level);
01492
01493 return;
01494 }
01495
01496
01497 if(strtolower($this->include_linksback) == "true")
01498 {
01499 $query = "";
01500
01501 if($this->globalDataset === FALSE)
01502 {
01503 $query = $this->db->build_sparql_query("select ?s ?p from <" . $d . "> where {?s ?p <" . $u . ">.}",
01504 array ('s', 'p'), FALSE);
01505 }
01506 else
01507 {
01508 $d = "";
01509
01510 foreach($datasets as $dataset)
01511 {
01512 if($dataset != "")
01513 {
01514 $d .= " from named <$dataset> ";
01515 }
01516 }
01517
01518 $query =
01519 $this->db->build_sparql_query("select ?s ?p $d where {graph ?g{?s ?p <" . $u . ">.}}", array ('s', 'p'),
01520 FALSE);
01521 }
01522
01523 $resultset = $this->db->query($query);
01524
01525 if(odbc_error())
01526 {
01527 $this->conneg->setStatus(500);
01528 $this->conneg->setStatusMsg("Internal Error");
01529 $this->conneg->setStatusMsgExt($this->errorMessenger->_303 > name);
01530 $this->conneg->setError($this->errorMessenger->_303->id, $this->errorMessenger->ws,
01531 $this->errorMessenger->_303->name, $this->errorMessenger->_303->description, odbc_errormsg(),
01532 $this->errorMessenger->_303->level);
01533 }
01534
01535 while(odbc_fetch_row($resultset))
01536 {
01537 $s = odbc_result($resultset, 1);
01538 $p = odbc_result($resultset, 2);
01539
01540 if(!isset($this->objectTriples[$u][$p]))
01541 {
01542 $this->objectTriples[$u][$p] = array();
01543 }
01544
01545 array_push($this->objectTriples[$u][$p], $s);
01546 }
01547
01548 unset($resultset);
01549 }
01550
01551
01552 if(strtolower($this->include_reification) == "true")
01553 {
01554
01555 $query = "";
01556
01557 if($this->globalDataset === FALSE)
01558 {
01559 $query = " select ?rei_p ?rei_o ?p ?o from <" . $d . "reification/>
01560 where
01561 {
01562 ?statement <http://www.w3.org/1999/02/22-rdf-syntax-ns#subject> <"
01563 . $u
01564 . ">.
01565 ?statement <http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate> ?rei_p.
01566 ?statement <http://www.w3.org/1999/02/22-rdf-syntax-ns#object> ?rei_o.
01567 ?statement ?p ?o.
01568 }";
01569 }
01570 else
01571 {
01572 $d = "";
01573
01574 foreach($datasets as $dataset)
01575 {
01576 if($dataset != "")
01577 {
01578 $d .= " from named <" . $dataset . "reification/> ";
01579 }
01580 }
01581
01582 $query = " select ?rei_p ?rei_o ?p ?o $d
01583 where
01584 {
01585 graph ?g
01586 {
01587 ?statement <http://www.w3.org/1999/02/22-rdf-syntax-ns#subject> <"
01588 . $u
01589 . ">.
01590 ?statement <http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate> ?rei_p.
01591 ?statement <http://www.w3.org/1999/02/22-rdf-syntax-ns#object> ?rei_o.
01592 ?statement ?p ?o.
01593 }
01594 }";
01595 }
01596
01597 $query = $this->db->build_sparql_query(str_replace(array ("\n", "\r", "\t"), " ", $query),
01598 array ('rei_p', 'rei_o', 'p', 'o'), FALSE);
01599
01600 $resultset = $this->db->query($query);
01601
01602 if(odbc_error())
01603 {
01604 $this->conneg->setStatus(500);
01605 $this->conneg->setStatusMsg("Internal Error");
01606 $this->conneg->setStatusMsgExt($this->errorMessenger->_304->name);
01607 $this->conneg->setError($this->errorMessenger->_304->id, $this->errorMessenger->ws,
01608 $this->errorMessenger->_304->name, $this->errorMessenger->_304->description, odbc_errormsg(),
01609 $this->errorMessenger->_304->level);
01610 }
01611
01612 while(odbc_fetch_row($resultset))
01613 {
01614 $rei_p = odbc_result($resultset, 1);
01615 $rei_o = odbc_result($resultset, 2);
01616 $p = odbc_result($resultset, 3);
01617 $o = odbc_result($resultset, 4);
01618
01619 if($p != "http://www.w3.org/1999/02/22-rdf-syntax-ns#subject"
01620 && $p != "http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate"
01621 && $p != "http://www.w3.org/1999/02/22-rdf-syntax-ns#object"
01622 && $p != "http://www.w3.org/1999/02/22-rdf-syntax-ns#type")
01623 {
01624 if(!isset($this->reificationTriples[$u][$rei_p][$rei_o][$p]))
01625 {
01626 $this->reificationTriples[$u][$rei_p][$rei_o][$p] = array();
01627 }
01628
01629 array_push($this->reificationTriples[$u][$rei_p][$rei_o][$p], $o);
01630 }
01631 }
01632
01633 unset($resultset);
01634 }
01635 }
01636 }
01637 }
01638 }
01639
01640
01642
01643 ?>