DatasetRead.php
Go to the documentation of this file.
00001 <?php 00002 00005 00026 class DatasetRead extends WebService 00027 { 00029 private $db; 00030 00032 private $conneg; 00033 00035 private $dtdURL; 00036 00038 private $requester_ip = ""; 00039 00041 private $registered_ip = ""; 00042 00044 private $datasetUri = ""; 00045 00047 private $datasetsDescription = array(); 00048 00050 private $addMeta = "false"; 00051 00053 private $namespaces = 00054 array ("http://www.w3.org/2002/07/owl#" => "owl", "http://www.w3.org/1999/02/22-rdf-syntax-ns#" => "rdf", 00055 "http://www.w3.org/2000/01/rdf-schema#" => "rdfs", "http://purl.org/ontology/wsf#" => "wsf"); 00056 00057 00059 public static $supportedSerializations = 00060 array ("application/json", "application/rdf+xml", "application/rdf+n3", "application/*", "text/xml", "text/*", 00061 "*/*"); 00062 00064 private $errorMessenger = 00065 '{ 00066 "ws": "/ws/dataset/read/", 00067 "_200": { 00068 "id": "WS-DATASET-READ-200", 00069 "level": "Warning", 00070 "name": "No unique identifier specified for this dataset", 00071 "description": "No URI defined for this new dataset" 00072 }, 00073 "_300": { 00074 "id": "WS-DATASET-READ-300", 00075 "level": "Fatal", 00076 "name": "Can\'t get the description of any dataset", 00077 "description": "An error occured when we tried to get information about all datasets" 00078 }, 00079 "_301": { 00080 "id": "WS-DATASET-READ-301", 00081 "level": "Fatal", 00082 "name": "Can\'t get the description of the target dataset", 00083 "description": "An error occured when we tried to get information about the target dataset" 00084 }, 00085 "_302": { 00086 "id": "WS-DATASET-READ-302", 00087 "level": "Fatal", 00088 "name": "Can\'t get meta-information about the dataset(s)", 00089 "description": "An error occured when we tried to get meta-information about the dataset(s)" 00090 }, 00091 "_303": { 00092 "id": "WS-DATASET-READ-303", 00093 "level": "Fatal", 00094 "name": "Can\'t get information about the contributors", 00095 "description": "An error occured when we tried to get information about the contributors of this dataset" 00096 }, 00097 "_304": { 00098 "id": "WS-DATASET-READ-304", 00099 "level": "Warning", 00100 "name": "This dataset doesn\'t exist in this WSF", 00101 "description": "The target dataset doesn\'t exist in this web service framework" 00102 }, 00103 "_305": { 00104 "id": "WS-DATASET-READ-305", 00105 "level": "Fatal", 00106 "name": "Can\'t get meta-information about the dataset(s)", 00107 "description": "An error occured when we tried to get meta-information about the dataset(s)" 00108 } 00109 }'; 00110 00111 00128 function __construct($uri, $meta, $registered_ip, $requester_ip) 00129 { 00130 parent::__construct(); 00131 00132 $this->db = new DB_Virtuoso($this->db_username, $this->db_password, $this->db_dsn, $this->db_host); 00133 00134 $this->datasetUri = $uri; 00135 $this->requester_ip = $requester_ip; 00136 $this->addMeta = strtolower($meta); 00137 00138 if($registered_ip == "") 00139 { 00140 $this->registered_ip = $requester_ip; 00141 } 00142 else 00143 { 00144 $this->registered_ip = $registered_ip; 00145 } 00146 00147 if(strtolower(substr($this->registered_ip, 0, 4)) == "self") 00148 { 00149 $pos = strpos($this->registered_ip, "::"); 00150 00151 if($pos !== FALSE) 00152 { 00153 $account = substr($this->registered_ip, $pos + 2, strlen($this->registered_ip) - ($pos + 2)); 00154 00155 $this->registered_ip = $requester_ip . "::" . $account; 00156 } 00157 else 00158 { 00159 $this->registered_ip = $requester_ip; 00160 } 00161 } 00162 00163 $this->uri = $this->wsf_base_url . "/wsf/ws/dataset/read/"; 00164 $this->title = "Dataset Read Web Service"; 00165 $this->crud_usage = new CrudUsage(FALSE, TRUE, FALSE, FALSE); 00166 $this->endpoint = $this->wsf_base_url . "/ws/dataset/read/"; 00167 00168 $this->dtdURL = "dataset/datasetRead.dtd"; 00169 00170 $this->errorMessenger = json_decode($this->errorMessenger); 00171 } 00172 00173 function __destruct() 00174 { 00175 parent::__destruct(); 00176 00177 if(isset($this->db)) 00178 { 00179 @$this->db->close(); 00180 } 00181 } 00182 00204 protected function validateQuery() 00205 { 00206 // Check if the requester has access to the main "http://.../wsf/datasets/" graph. 00207 $ws_av = new AuthValidator($this->requester_ip, $this->wsf_graph . "datasets/", $this->uri); 00208 00209 $ws_av->pipeline_conneg($this->conneg->getAccept(), $this->conneg->getAcceptCharset(), 00210 $this->conneg->getAcceptEncoding(), $this->conneg->getAcceptLanguage()); 00211 00212 $ws_av->process(); 00213 00214 if($ws_av->pipeline_getResponseHeaderStatus() != 200) 00215 { 00216 // If he doesn't, then check if he has access to the dataset itself 00217 $ws_av2 = new AuthValidator($this->requester_ip, $this->datasetUri, $this->uri); 00218 00219 $ws_av2->pipeline_conneg($this->conneg->getAccept(), $this->conneg->getAcceptCharset(), 00220 $this->conneg->getAcceptEncoding(), $this->conneg->getAcceptLanguage()); 00221 00222 $ws_av2->process(); 00223 00224 if($ws_av2->pipeline_getResponseHeaderStatus() != 200) 00225 { 00226 $this->conneg->setStatus($ws_av2->pipeline_getResponseHeaderStatus()); 00227 $this->conneg->setStatusMsg($ws_av2->pipeline_getResponseHeaderStatusMsg()); 00228 $this->conneg->setStatusMsgExt($ws_av2->pipeline_getResponseHeaderStatusMsgExt()); 00229 $this->conneg->setError($ws_av2->pipeline_getError()->id, $ws_av2->pipeline_getError()->webservice, 00230 $ws_av2->pipeline_getError()->name, $ws_av2->pipeline_getError()->description, 00231 $ws_av2->pipeline_getError()->debugInfo, $ws_av2->pipeline_getError()->level); 00232 00233 return; 00234 } 00235 } 00236 00237 // If the system send a query on the behalf of another user, we validate that other user as well 00238 if($this->registered_ip != $this->requester_ip) 00239 { 00240 // Check if the requester has access to the main "http://.../wsf/datasets/" graph. 00241 $ws_av = new AuthValidator($this->registered_ip, $this->wsf_graph . "datasets/", $this->uri); 00242 00243 $ws_av->pipeline_conneg($this->conneg->getAccept(), $this->conneg->getAcceptCharset(), 00244 $this->conneg->getAcceptEncoding(), $this->conneg->getAcceptLanguage()); 00245 00246 $ws_av->process(); 00247 00248 if($ws_av->pipeline_getResponseHeaderStatus() != 200) 00249 { 00250 // If he doesn't, then check if he has access to the dataset itself 00251 $ws_av2 = new AuthValidator($this->registered_ip, $this->datasetUri, $this->uri); 00252 00253 $ws_av2->pipeline_conneg($this->conneg->getAccept(), $this->conneg->getAcceptCharset(), 00254 $this->conneg->getAcceptEncoding(), $this->conneg->getAcceptLanguage()); 00255 00256 $ws_av2->process(); 00257 00258 if($ws_av2->pipeline_getResponseHeaderStatus() != 200) 00259 { 00260 $this->conneg->setStatus($ws_av2->pipeline_getResponseHeaderStatus()); 00261 $this->conneg->setStatusMsg($ws_av2->pipeline_getResponseHeaderStatusMsg()); 00262 $this->conneg->setStatusMsgExt($ws_av2->pipeline_getResponseHeaderStatusMsgExt()); 00263 $this->conneg->setError($ws_av2->pipeline_getError()->id, $ws_av2->pipeline_getError()->webservice, 00264 $ws_av2->pipeline_getError()->name, $ws_av2->pipeline_getError()->description, 00265 $ws_av2->pipeline_getError()->debugInfo, $ws_av2->pipeline_getError()->level); 00266 00267 return; 00268 } 00269 } 00270 } 00271 } 00272 00285 private function uriEncode($uri) 00286 { 00287 $uri = preg_replace("|[^a-zA-z0-9]|", " ", $uri); 00288 $uri = preg_replace("/\s+/", " ", $uri); 00289 $uri = str_replace(" ", "_", $uri); 00290 00291 return ($uri); 00292 } 00293 00304 public function pipeline_getError() { return ($this->conneg->error); } 00305 00306 00317 public function pipeline_getResultset() 00318 { 00319 $xml = new ProcessorXML(); 00320 00321 // Creation of the RESULTSET 00322 $resultset = $xml->createResultset(); 00323 00324 // Creation of the prefixes elements. 00325 $wsf = $xml->createPrefix("wsf", "http://purl.org/ontology/wsf#"); 00326 $resultset->appendChild($wsf); 00327 $void = $xml->createPrefix("void", "http://rdfs.org/ns/void#"); 00328 $resultset->appendChild($void); 00329 $rdf = $xml->createPrefix("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); 00330 $resultset->appendChild($rdf); 00331 $sioc = $xml->createPrefix("sioc", "http://rdfs.org/sioc/ns#"); 00332 $resultset->appendChild($sioc); 00333 $dcterms = $xml->createPrefix("dcterms", "http://purl.org/dc/terms/"); 00334 $resultset->appendChild($dcterms); 00335 $dcterms = $xml->createPrefix("rdfs", "http://www.w3.org/2000/01/rdf-schema#"); 00336 $resultset->appendChild($dcterms); 00337 00338 // Creation of the SUBJECT of the RESULTSET 00339 foreach($this->datasetsDescription as $dd) 00340 { 00341 if($dd->uri != "") 00342 { 00343 $subject = $xml->createSubject("void:Dataset", $dd->uri); 00344 00345 if($dd->title != "") 00346 { 00347 $pred = $xml->createPredicate("dcterms:title"); 00348 $object = $xml->createObjectContent($dd->title); 00349 $pred->appendChild($object); 00350 $subject->appendChild($pred); 00351 } 00352 00353 if($dd->description != "") 00354 { 00355 $pred = $xml->createPredicate("dcterms:description"); 00356 $object = $xml->createObjectContent($dd->description); 00357 $pred->appendChild($object); 00358 $subject->appendChild($pred); 00359 } 00360 00361 $pred = $xml->createPredicate("dcterms:created"); 00362 $object = $xml->createObjectContent($dd->created); 00363 $pred->appendChild($object); 00364 $subject->appendChild($pred); 00365 00366 if($dd->modified != "") 00367 { 00368 $pred = $xml->createPredicate("dcterms:modified"); 00369 $object = $xml->createObjectContent($dd->modified); 00370 $pred->appendChild($object); 00371 $subject->appendChild($pred); 00372 } 00373 00374 if($dd->creator != "") 00375 { 00376 $pred = $xml->createPredicate("dcterms:creator"); 00377 $object = $xml->createObject("sioc:User", $dd->creator); 00378 $pred->appendChild($object); 00379 $subject->appendChild($pred); 00380 } 00381 00382 if($dd->meta != "" && $this->addMeta == "true") 00383 { 00384 $pred = $xml->createPredicate("wsf:meta"); 00385 $object = $xml->createObject("", $dd->meta); 00386 $pred->appendChild($object); 00387 $subject->appendChild($pred); 00388 } 00389 00390 if(count($dd->metaDescription) > 0 && $this->addMeta == "true") 00391 { 00392 $subjectMeta = $xml->createSubject("void:Dataset", $dd->meta); 00393 00394 foreach($dd->metaDescription as $predicate => $values) 00395 { 00396 foreach($values as $key => $value) 00397 { 00398 if(gettype($key) == "integer" && $value != "") 00399 { 00400 if($dd->metaDescription[$predicate]["type"] != NULL) 00401 { 00402 /* 00403 @TODO The internal XML structure of structWSF should be enhanced with datatypes such as xsd:double, int, 00404 literal, etc. 00405 */ 00406 $pred = $xml->createPredicate($predicate); 00407 $object = $xml->createObjectContent($value); 00408 $pred->appendChild($object); 00409 $subjectMeta->appendChild($pred); 00410 } 00411 else 00412 { 00413 $pred = $xml->createPredicate($predicate); 00414 $object = $xml->createObject("", $value); 00415 00416 // Check if we have to reify statements to this object 00417 $query = " select ?s ?p ?o 00418 from <" . $this->datasetUri . "reification/> 00419 where 00420 { 00421 ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#subject> <" . $dd->meta . ">. 00422 ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate> <" . $predicate . ">. 00423 ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#object> <" . $value . ">. 00424 ?s ?p ?o. 00425 }"; 00426 00427 $rset = 00428 @$this->db->query($this->db->build_sparql_query(str_replace(array ("\n", "\r", "\t"), " ", $query), 00429 array ("s", "p", "o"), FALSE)); 00430 00431 if(!odbc_error()) 00432 { 00433 while(odbc_fetch_row($rset)) 00434 { 00435 $s = odbc_result($rset, 1); 00436 $p = odbc_result($rset, 2); 00437 $o = odbc_result($rset, 3); 00438 00439 if($p == "http://purl.org/ontology/bibo/uri" 00440 || $p == "http://www.w3.org/2000/01/rdf-schema#label") 00441 { 00442 $reify = $xml->createReificationStatement($p, $o); 00443 $object->appendChild($reify); 00444 } 00445 } 00446 } 00447 00448 $pred->appendChild($object); 00449 $subjectMeta->appendChild($pred); 00450 } 00451 } 00452 } 00453 } 00454 00455 $resultset->appendChild($subjectMeta); 00456 } 00457 00458 foreach($dd->contributors as $contributor) 00459 { 00460 if($contributor != "") 00461 { 00462 $pred = $xml->createPredicate("dcterms:contributor"); 00463 $object = $xml->createObject("sioc:User", $contributor); 00464 $pred->appendChild($object); 00465 $subject->appendChild($pred); 00466 } 00467 } 00468 00469 $resultset->appendChild($subject); 00470 } 00471 } 00472 00473 return ($this->injectDoctype($xml->saveXML($resultset))); 00474 } 00475 00488 public function injectDoctype($xmlDoc) 00489 { 00490 $posHeader = strpos($xmlDoc, '"?>') + 3; 00491 $xmlDoc = substr($xmlDoc, 0, $posHeader) 00492 . "\n<!DOCTYPE resultset PUBLIC \"-//Structured Dynamics LLC//Dataset Read DTD 0.1//EN\" \"" . $this->dtdBaseURL 00493 . $this->dtdURL . "\">" . substr($xmlDoc, $posHeader, strlen($xmlDoc) - $posHeader); 00494 00495 return ($xmlDoc); 00496 } 00497 00516 public function ws_conneg($accept, $accept_charset, $accept_encoding, $accept_language) 00517 { 00518 $this->conneg = 00519 new Conneg($accept, $accept_charset, $accept_encoding, $accept_language, DatasetRead::$supportedSerializations); 00520 00521 // Validate query 00522 $this->validateQuery(); 00523 00524 // If the query is still valid 00525 if($this->conneg->getStatus() == 200) 00526 { 00527 // Check for errors 00528 if($this->uri == "") 00529 { 00530 $this->conneg->setStatus(400); 00531 $this->conneg->setStatusMsg("Bad Request"); 00532 $this->conneg->setStatusMsgExt("No URI specified for any dataset"); 00533 $this->conneg->setStatusMsgExt($this->errorMessenger->_200->name); 00534 $this->conneg->setError($this->errorMessenger->_200->id, $this->errorMessenger->ws, 00535 $this->errorMessenger->_200->name, $this->errorMessenger->_200->description, "", 00536 $this->errorMessenger->_200->level); 00537 00538 return; 00539 } 00540 } 00541 } 00542 00561 public function pipeline_conneg($accept, $accept_charset, $accept_encoding, $accept_language) 00562 { $this->ws_conneg($accept, $accept_charset, $accept_encoding, $accept_language); } 00563 00574 public function pipeline_getResponseHeaderStatus() { return $this->conneg->getStatus(); } 00575 00586 public function pipeline_getResponseHeaderStatusMsg() { return $this->conneg->getStatusMsg(); } 00587 00600 public function pipeline_getResponseHeaderStatusMsgExt() { return $this->conneg->getStatusMsgExt(); } 00601 00614 private function getNamespace($uri) 00615 { 00616 $pos = strrpos($uri, "#"); 00617 00618 if($pos !== FALSE) 00619 { 00620 return array (substr($uri, 0, $pos) . "#", substr($uri, $pos + 1, strlen($uri) - ($pos + 1))); 00621 } 00622 else 00623 { 00624 $pos = strrpos($uri, "/"); 00625 00626 if($pos !== FALSE) 00627 { 00628 return array (substr($uri, 0, $pos) . "/", substr($uri, $pos + 1, strlen($uri) - ($pos + 1))); 00629 } 00630 else 00631 { 00632 $pos = strpos($uri, ":"); 00633 00634 if($pos !== FALSE) 00635 { 00636 $nsUri = explode(":", $uri, 2); 00637 00638 foreach($this->namespaces as $uri2 => $prefix2) 00639 { 00640 $uri2 = urldecode($uri2); 00641 00642 if($prefix2 == $nsUri[0]) 00643 { 00644 return (array ($uri2, $nsUri[1])); 00645 } 00646 } 00647 00648 return explode(":", $uri, 2); 00649 } 00650 } 00651 } 00652 00653 return (FALSE); 00654 } 00665 public function pipeline_serialize() 00666 { 00667 $rdf_part = ""; 00668 00669 switch($this->conneg->getMime()) 00670 { 00671 case "application/json": 00672 $json_part = ""; 00673 $xml = new ProcessorXML(); 00674 $xml->loadXML($this->pipeline_getResultset()); 00675 00676 $subjects = $xml->getSubjects(); 00677 00678 foreach($subjects as $subject) 00679 { 00680 $subjectURI = $xml->getURI($subject); 00681 $subjectType = $xml->getType($subject); 00682 00683 $json_part .= " { \n"; 00684 $json_part .= " \"uri\": \"" . parent::jsonEncode($subjectURI) . "\", \n"; 00685 $json_part .= " \"type\": \"" . parent::jsonEncode($subjectType) . "\", \n"; 00686 00687 $predicates = $xml->getPredicates($subject); 00688 00689 $nbPredicates = 0; 00690 00691 foreach($predicates as $predicate) 00692 { 00693 $objects = $xml->getObjects($predicate); 00694 00695 foreach($objects as $object) 00696 { 00697 $nbPredicates++; 00698 00699 if($nbPredicates == 1) 00700 { 00701 $json_part .= " \"predicate\": [ \n"; 00702 } 00703 00704 $objectType = $xml->getType($object); 00705 $predicateType = $xml->getType($predicate); 00706 00707 if($objectType == "rdfs:Literal") 00708 { 00709 $objectValue = $xml->getContent($object); 00710 00711 $json_part .= " { \n"; 00712 $json_part .= " \"" . parent::jsonEncode($predicateType) . "\": \"" 00713 . parent::jsonEncode($objectValue) . "\" \n"; 00714 $json_part .= " },\n"; 00715 } 00716 else 00717 { 00718 $objectURI = $xml->getURI($object); 00719 $rdf_part .= " <$predicateType> <$objectURI> ;\n"; 00720 00721 $json_part .= " { \n"; 00722 $json_part .= " \"" . parent::jsonEncode($predicateType) . "\": { \n"; 00723 $json_part .= " \"uri\": \"" . parent::jsonEncode($objectURI) . "\" \n"; 00724 $json_part .= " } \n"; 00725 $json_part .= " },\n"; 00726 } 00727 } 00728 } 00729 00730 if(strlen($json_part) > 0) 00731 { 00732 $json_part = substr($json_part, 0, strlen($json_part) - 2) . "\n"; 00733 } 00734 00735 if($nbPredicates > 0) 00736 { 00737 $json_part .= " ]\n"; 00738 } 00739 00740 $json_part .= " },\n"; 00741 } 00742 00743 if(strlen($json_part) > 0) 00744 { 00745 $json_part = substr($json_part, 0, strlen($json_part) - 2) . "\n"; 00746 } 00747 00748 return ($json_part); 00749 break; 00750 00751 case "application/rdf+n3": 00752 $xml = new ProcessorXML(); 00753 $xml->loadXML($this->pipeline_getResultset()); 00754 00755 $dataset = $xml->getSubjectsByType("void:Dataset"); 00756 00757 $dataset_uri = $xml->getURI($dataset->item(0)); 00758 00759 $rdf_part .= "<$dataset_uri> a void:Dataset ;\n"; 00760 00761 // Get title 00762 $predicates = $xml->getPredicatesByType($dataset->item(0), "dcterms:title"); 00763 00764 if($predicates->item(0)) 00765 { 00766 $objects = $xml->getObjectsByType($predicates->item(0), "rdfs:Literal"); 00767 $rdf_part .= "dcterms:title \"\"\"" . $xml->getContent($objects->item(0)) . "\"\"\" ;\n"; 00768 } 00769 00770 // Get description 00771 $predicates = $xml->getPredicatesByType($dataset->item(0), "dcterms:description"); 00772 00773 if($predicates->item(0)) 00774 { 00775 $objects = $xml->getObjectsByType($predicates->item(0), "rdfs:Literal"); 00776 $rdf_part .= "dcterms:description \"\"\"" . $xml->getContent($objects->item(0)) . "\"\"\" ;\n"; 00777 } 00778 00779 // Get created 00780 $predicates = $xml->getPredicatesByType($dataset->item(0), "dcterms:created"); 00781 00782 if($predicates->item(0)) 00783 { 00784 $objects = $xml->getObjectsByType($predicates->item(0), "rdfs:Literal"); 00785 $rdf_part .= "dcterms:created \"\"\"" . $xml->getContent($objects->item(0)) . "\"\"\" ;\n"; 00786 } 00787 00788 // Get modified 00789 $predicates = $xml->getPredicatesByType($dataset->item(0), "dcterms:modified"); 00790 00791 if($predicates->item(0)) 00792 { 00793 $objects = $xml->getObjectsByType($predicates->item(0), "rdfs:Literal"); 00794 $rdf_part .= "dcterms:modified \"\"\"" . $xml->getContent($objects->item(0)) . "\"\"\" ;\n"; 00795 } 00796 00797 // Get creator 00798 $predicates = $xml->getPredicatesByType($dataset->item(0), "dcterms:creator"); 00799 00800 foreach($predicates as $predicate) 00801 { 00802 $objects = $xml->getObjectsByType($predicate, "sioc:User"); 00803 $rdf_part .= "dcterms:creator <" . $xml->getURI($objects->item(0)) . "> ;\n"; 00804 } 00805 00806 // Get contributors 00807 $predicates = $xml->getPredicatesByType($dataset->item(0), "dcterms:contributor"); 00808 00809 foreach($predicates as $predicate) 00810 { 00811 $objects = $xml->getObjectsByType($predicate, "sioc:User"); 00812 $rdf_part .= "dcterms:contributor <" . $xml->getURI($objects->item(0)) . "> ;\n"; 00813 } 00814 00815 return ($rdf_part); 00816 00817 break; 00818 00819 case "application/rdf+xml": 00820 00821 $xml = new ProcessorXML(); 00822 $xml->loadXML($this->pipeline_getResultset()); 00823 00824 00828 /* 00829 $nsId = 0; 00830 */ 00831 00832 $dataset = $xml->getSubjectsByType("void:Dataset"); 00833 00834 $dataset_uri = $xml->getURI($dataset->item(0)); 00835 00836 $rdf_part .= "<void:Dataset rdf:about=\"".$this->xmlEncode($dataset_uri)."\">\n"; 00837 00838 // Get title 00839 $predicates = $xml->getPredicatesByType($dataset->item(0), "dcterms:title"); 00840 00841 if($predicates->item(0)) 00842 { 00843 $objects = $xml->getObjectsByType($predicates->item(0), "rdfs:Literal"); 00844 $rdf_part .= "<dcterms:title>" . $this->xmlEncode($xml->getContent($objects->item(0))) . "</dcterms:title>\n"; 00845 } 00846 00847 // Get description 00848 $predicates = $xml->getPredicatesByType($dataset->item(0), "dcterms:description"); 00849 00850 if($predicates->item(0)) 00851 { 00852 $objects = $xml->getObjectsByType($predicates->item(0), "rdfs:Literal"); 00853 $rdf_part .= "<dcterms:description>" . $this->xmlEncode($xml->getContent($objects->item(0))) . 00854 "</dcterms:description>\n"; 00855 } 00856 00857 // Get created 00858 $predicates = $xml->getPredicatesByType($dataset->item(0), "dcterms:created"); 00859 00860 if($predicates->item(0)) 00861 { 00862 $objects = $xml->getObjectsByType($predicates->item(0), "rdfs:Literal"); 00863 $rdf_part .= "<dcterms:created>" . $this->xmlEncode($xml->getContent($objects->item(0))) . 00864 "</dcterms:created>\n"; 00865 } 00866 00867 // Get modified 00868 $predicates = $xml->getPredicatesByType($dataset->item(0), "dcterms:modified"); 00869 00870 if($predicates->item(0)) 00871 { 00872 $objects = $xml->getObjectsByType($predicates->item(0), "rdfs:Literal"); 00873 $rdf_part .= "<dcterms:modified>" . $this->xmlEncode($xml->getContent($objects->item(0))) . 00874 "</dcterms:modified>\n"; 00875 } 00876 00877 // Get creator 00878 $predicates = $xml->getPredicatesByType($dataset->item(0), "dcterms:creator"); 00879 00880 foreach($predicates as $predicate) 00881 { 00882 $objects = $xml->getObjectsByType($predicate, "sioc:User"); 00883 $rdf_part .= "<dcterms:creator rdf:resource=\"" . $this->xmlEncode($xml->getURI($objects->item(0))) . 00884 "\" />\n"; 00885 } 00886 00887 // Get contributors 00888 $predicates = $xml->getPredicatesByType($dataset->item(0), "dcterms:contributor"); 00889 00890 foreach($predicates as $predicate) 00891 { 00892 $objects = $xml->getObjectsByType($predicate, "sioc:User"); 00893 $rdf_part .= "<dcterms:contributor rdf:resource=\"" . $this->xmlEncode($xml->getURI($objects->item(0))) . 00894 "\" />\n"; 00895 } 00896 00897 $rdf_part .= "</void:Dataset>\n"; 00898 00899 return ($rdf_part); 00900 break; 00901 } 00902 } 00903 00912 public function pipeline_serialize_reification() { return ""; } 00913 00924 public function ws_serialize() 00925 { 00926 switch($this->conneg->getMime()) 00927 { 00928 case "application/rdf+n3": 00929 $rdf_document = ""; 00930 $rdf_document .= "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n"; 00931 $rdf_document .= "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n"; 00932 $rdf_document .= "@prefix void: <http://rdfs.org/ns/void#> .\n"; 00933 $rdf_document .= "@prefix dcterms: <http://purl.org/dc/terms/> .\n"; 00934 $rdf_document .= "@prefix wsf: <http://purl.org/ontology/wsf#> .\n"; 00935 00936 $rdf_document .= $this->pipeline_serialize(); 00937 00938 $rdf_document = substr($rdf_document, 0, strlen($rdf_document) - 2) . ".\n"; 00939 00940 return $rdf_document; 00941 break; 00942 00943 case "application/rdf+xml": 00944 $rdf_document = ""; 00945 $rdf_document .= "<?xml version=\"1.0\"?>\n"; 00946 $rdf_document 00947 .= "<rdf:RDF xmlns:wsf=\"http://purl.org/ontology/wsf#\" xmlns:void=\"http://rdfs.org/ns/void#\" xmlns:dcterms=\"http://purl.org/dc/terms/\" xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n\n"; 00948 00949 $rdf_document .= $this->pipeline_serialize(); 00950 00951 $rdf_document .= "</rdf:RDF>"; 00952 00953 return $rdf_document; 00954 break; 00955 00956 case "text/xml": 00957 return $this->pipeline_getResultset(); 00958 break; 00959 00960 case "application/json": 00961 $json_document = ""; 00962 $json_document .= "{\n"; 00963 $json_document .= " \"prefixes\": [ \n"; 00964 $json_document .= " {\n"; 00965 $json_document .= " \"rdf\": \"http:\/\/www.w3.org\/1999\/02\/22-rdf-syntax-ns#\",\n"; 00966 $json_document .= " \"rdfs\": \"http://www.w3.org/2000/01/rdf-schema#\",\n"; 00967 $json_document .= " \"void\": \"http://rdfs.org/ns/void#\",\n"; 00968 $json_document .= " \"dcterms\": \"http://purl.org/dc/terms/\"\n"; 00969 $json_document .= " } \n"; 00970 $json_document .= " ],\n"; 00971 $json_document .= " \"resultset\": {\n"; 00972 $json_document .= " \"subject\": [\n"; 00973 $json_document .= $this->pipeline_serialize(); 00974 $json_document .= " ]\n"; 00975 $json_document .= " }\n"; 00976 $json_document .= "}"; 00977 00978 return ($json_document); 00979 break; 00980 } 00981 } 00982 00995 public function ws_respond($content) 00996 { 00997 // First send the header of the request 00998 $this->conneg->respond(); 00999 01000 // second, send the content of the request 01001 01002 // Make sure there is no error. 01003 if($this->conneg->getStatus() == 200) 01004 { 01005 echo $content; 01006 } 01007 01008 $this->__destruct(); 01009 } 01010 01011 01020 public function process() 01021 { 01022 // Make sure there was no conneg error prior to this process call 01023 if($this->conneg->getStatus() == 200) 01024 { 01025 /* 01026 In the future, this single query should be used for that ALL purpose. 01027 There is currently a bug in virtuoso that doesnt return anything in the resultset (virtuoso v5.0.10) 01028 if one of the OPTIONAL pattern is not existing in the triple store (so, OPTIONAL doesn't work) 01029 01030 sparql 01031 select * 01032 from named </wsf/datasets/> 01033 from named </wsf/> 01034 where 01035 { 01036 graph </wsf/> 01037 { 01038 ?access <http://purl.org/ontology/wsf#registeredIP> "174.129.251.47::1" ; 01039 <http://purl.org/ontology/wsf#read> "True" ; 01040 <http://purl.org/ontology/wsf#datasetAccess> ?dataset . 01041 } 01042 01043 graph </wsf/datasets/> 01044 { 01045 ?dataset a <http://rdfs.org/ns/void#Dataset> ; 01046 <http://purl.org/dc/terms/created> ?created. 01047 01048 OPTIONAL{?dataset <http://purl.org/dc/terms/title> ?title.} 01049 OPTIONAL{?dataset <http://purl.org/dc/terms/description> ?description.} 01050 OPTIONAL{?dataset <http://purl.org/dc/terms/creator> ?creator.} 01051 OPTIONAL{?dataset <http://purl.org/dc/terms/modified> ?modified.} 01052 } 01053 }; 01054 */ 01055 01056 $query = ""; 01057 $datasets = array(); 01058 01059 if($this->datasetUri == "all") 01060 { 01061 $query = " select distinct ?dataset ?title ?description ?creator ?created ?modified ?contributor ?meta 01062 from named <" . $this->wsf_graph . "> 01063 from named <" . $this->wsf_graph . "datasets/> 01064 where 01065 { 01066 graph <" . $this->wsf_graph . "> 01067 { 01068 ?access <http://purl.org/ontology/wsf#registeredIP> ?ip ; 01069 <http://purl.org/ontology/wsf#read> \"True\" ; 01070 <http://purl.org/ontology/wsf#datasetAccess> ?dataset . 01071 filter( str(?ip) = \"$this->registered_ip\" or str(?ip) = \"0.0.0.0\") . 01072 } 01073 01074 graph <" 01075 . $this->wsf_graph 01076 . "datasets/> 01077 { 01078 ?dataset a <http://rdfs.org/ns/void#Dataset> ; 01079 <http://purl.org/dc/terms/created> ?created. 01080 01081 OPTIONAL{?dataset <http://purl.org/ontology/wsf#meta> ?meta.} 01082 OPTIONAL{?dataset <http://purl.org/dc/terms/title> ?title.} 01083 OPTIONAL{?dataset <http://purl.org/dc/terms/description> ?description.} 01084 OPTIONAL{?dataset <http://purl.org/dc/terms/modified> ?modified.} 01085 OPTIONAL{?dataset <http://purl.org/dc/terms/contributor> ?contributor.} 01086 OPTIONAL{?dataset <http://purl.org/dc/terms/creator> ?creator.} 01087 } 01088 }"; 01089 01090 $resultset = @$this->db->query($this->db->build_sparql_query(str_replace(array ("\n", "\r", "\t"), " ", $query), 01091 array ("dataset", "title", "description", "creator", "created", "modified", "contributor", "meta"), FALSE)); 01092 01093 if(odbc_error()) 01094 { 01095 $this->conneg->setStatus(500); 01096 $this->conneg->setStatusMsg("Internal Error"); 01097 $this->conneg->setStatusMsgExt($this->errorMessenger->_300->name); 01098 $this->conneg->setError($this->errorMessenger->_300->id, $this->errorMessenger->ws, 01099 $this->errorMessenger->_300->name, $this->errorMessenger->_300->description, odbc_errormsg(), 01100 $this->errorMessenger->_300->level); 01101 01102 return; 01103 } 01104 else 01105 { 01106 $dataset = ""; 01107 $title = ""; 01108 $description = ""; 01109 $creator = ""; 01110 $created = ""; 01111 $modified = ""; 01112 $contributors = array(); 01113 $meta = ""; 01114 01115 while(odbc_fetch_row($resultset)) 01116 { 01117 $dataset2 = odbc_result($resultset, 1); 01118 01119 if($dataset2 != $dataset && $dataset != "") 01120 { 01121 array_push($this->datasetsDescription, 01122 new DatasetDescription($dataset, $title, $description, $creator, $created, $modified, $contributors)); 01123 $contributors = array(); 01124 } 01125 01126 $dataset = $dataset2; 01127 01128 $title = odbc_result($resultset, 2); 01129 $description = odbc_result($resultset, 3); 01130 $creator = odbc_result($resultset, 4); 01131 $created = odbc_result($resultset, 5); 01132 $modified = odbc_result($resultset, 6); 01133 array_push($contributors, odbc_result($resultset, 7)); 01134 $meta = odbc_result($resultset, 8); 01135 } 01136 01137 $metaDescription = array(); 01138 01139 // We have to add the meta information if available 01140 if($meta != "" && $this->addMeta == "true") 01141 { 01142 $query = "select ?p ?o (str(DATATYPE(?o))) as ?otype (LANG(?o)) as ?olang 01143 from <" . $this->wsf_graph . "datasets/> 01144 where 01145 { 01146 <$meta> ?p ?o. 01147 }"; 01148 01149 $resultset = 01150 @$this->db->query($this->db->build_sparql_query(str_replace(array ("\n", "\r", "\t"), " ", $query), 01151 array ('p', 'o', 'otype', 'olang'), FALSE)); 01152 01153 $contributors = array(); 01154 01155 if(odbc_error()) 01156 { 01157 $this->conneg->setStatus(500); 01158 $this->conneg->setStatusMsg("Internal Error"); 01159 $this->conneg->setStatusMsgExt($this->errorMessenger->_305->name); 01160 $this->conneg->setError($this->errorMessenger->_305->id, $this->errorMessenger->ws, 01161 $this->errorMessenger->_305->name, $this->errorMessenger->_305->description, odbc_errormsg(), 01162 $this->errorMessenger->_305->level); 01163 01164 return; 01165 } 01166 else 01167 { 01168 while(odbc_fetch_row($resultset)) 01169 { 01170 $predicate = odbc_result($resultset, 1); 01171 $object = odbc_result($resultset, 2); 01172 $otype = odbc_result($resultset, 3); 01173 $olang = odbc_result($resultset, 4); 01174 01175 if(isset($metaDescription[$predicate])) 01176 { 01177 array_push($metaDescription[$predicate], $object); 01178 } 01179 else 01180 { 01181 $metaDescription[$predicate] = array( $object ); 01182 01183 if($olang && $olang != "") 01184 { 01185 /* If a language is defined for an object, we force its type to be xsd:string */ 01186 $metaDescription[$predicate]["type"] = "http://www.w3.org/2001/XMLSchema#string"; 01187 } 01188 else 01189 { 01190 $metaDescription[$predicate]["type"] = $otype; 01191 } 01192 } 01193 } 01194 } 01195 01196 unset($resultset); 01197 } 01198 01199 if($dataset != "") 01200 { 01201 array_push($this->datasetsDescription, 01202 new DatasetDescription($dataset, $title, $description, $creator, $created, $modified, $contributors, 01203 $meta, $metaDescription)); 01204 } 01205 01206 unset($resultset); 01207 } 01208 01209 /* 01210 // Get all datasets this user has access to 01211 $query = "select ?dataset 01212 from named <".$this->wsf_graph."datasets/> 01213 from named <".$this->wsf_graph."> 01214 where 01215 { 01216 graph <".$this->wsf_graph."datasets/> 01217 { 01218 ?dataset a <http://rdfs.org/ns/void#Dataset> . 01219 } 01220 01221 graph <".$this->wsf_graph."> 01222 { 01223 ?access <http://purl.org/ontology/wsf#registeredIP> \"$this->registered_ip\" ; 01224 <http://purl.org/ontology/wsf#read> \"True\" ; 01225 <http://purl.org/ontology/wsf#datasetAccess> ?dataset . 01226 } 01227 }"; 01228 01229 $resultset = @$this->db->query($this->db->build_sparql_query(str_replace(array("\n", "\r", "\t"), " ", $query), array("dataset"), FALSE)); 01230 01231 if (odbc_error()) 01232 { 01233 $this->conneg->setStatus(500); 01234 $this->conneg->setStatusMsg("Internal Error"); 01235 $this->conneg->setStatusMsgExt("Error #dataset-read-100"); 01236 return; 01237 } 01238 else 01239 { 01240 while(odbc_fetch_row($resultset)) 01241 { 01242 array_push($datasets, odbc_result($resultset, 1)); 01243 } 01244 01245 unset($resultset); 01246 } 01247 */ 01248 01249 } 01250 else 01251 { 01252 $dataset = $this->datasetUri; 01253 01254 $query = 01255 "select ?title ?description ?creator ?created ?modified ?meta 01256 from named <" . $this->wsf_graph . "datasets/> 01257 where 01258 { 01259 graph <" . $this->wsf_graph 01260 . "datasets/> 01261 { 01262 <$dataset> a <http://rdfs.org/ns/void#Dataset> ; 01263 <http://purl.org/dc/terms/created> ?created. 01264 01265 OPTIONAL{<$dataset> <http://purl.org/dc/terms/title> ?title.} . 01266 OPTIONAL{<$dataset> <http://purl.org/dc/terms/description> ?description.} . 01267 OPTIONAL{<$dataset> <http://purl.org/dc/terms/creator> ?creator.} . 01268 OPTIONAL{<$dataset> <http://purl.org/dc/terms/modified> ?modified.} . 01269 OPTIONAL{<$dataset> <http://purl.org/ontology/wsf#meta> ?meta.} . 01270 } 01271 }"; 01272 01273 $resultset = @$this->db->query($this->db->build_sparql_query(str_replace(array ("\n", "\r", "\t"), " ", $query), 01274 array ('title', 'description', 'creator', 'created', 'modified', 'meta'), FALSE)); 01275 01276 if(odbc_error()) 01277 { 01278 $this->conneg->setStatus(500); 01279 $this->conneg->setStatusMsg("Internal Error"); 01280 $this->conneg->setStatusMsgExt($this->errorMessenger->_301->name); 01281 $this->conneg->setError($this->errorMessenger->_301->id, $this->errorMessenger->ws, 01282 $this->errorMessenger->_301->name, $this->errorMessenger->_301->description, odbc_errormsg(), 01283 $this->errorMessenger->_301->level); 01284 01285 return; 01286 } 01287 else 01288 { 01289 if(odbc_fetch_row($resultset)) 01290 { 01291 $title = odbc_result($resultset, 1); 01292 $description = odbc_result($resultset, 2); 01293 $creator = odbc_result($resultset, 3); 01294 $created = odbc_result($resultset, 4); 01295 $modified = odbc_result($resultset, 5); 01296 $meta = odbc_result($resultset, 6); 01297 01298 unset($resultset); 01299 01300 $metaDescription = array(); 01301 01302 // We have to add the meta information if available 01303 if($meta != "" && $this->addMeta == "true") 01304 { 01305 $query = "select ?p ?o (str(DATATYPE(?o))) as ?otype (LANG(?o)) as ?olang 01306 from <" . $this->wsf_graph . "datasets/> 01307 where 01308 { 01309 <$meta> ?p ?o. 01310 }"; 01311 01312 $resultset = 01313 @$this->db->query($this->db->build_sparql_query(str_replace(array ("\n", "\r", "\t"), " ", $query), 01314 array ('p', 'o', 'otype', 'olang'), FALSE)); 01315 01316 $contributors = array(); 01317 01318 if(odbc_error()) 01319 { 01320 $this->conneg->setStatus(500); 01321 $this->conneg->setStatusMsg("Internal Error"); 01322 $this->conneg->setStatusMsgExt($this->errorMessenger->_302->name); 01323 $this->conneg->setError($this->errorMessenger->_302->id, $this->errorMessenger->ws, 01324 $this->errorMessenger->_302->name, $this->errorMessenger->_302->description, odbc_errormsg(), 01325 $this->errorMessenger->_302->level); 01326 01327 return; 01328 } 01329 else 01330 { 01331 while(odbc_fetch_row($resultset)) 01332 { 01333 $predicate = odbc_result($resultset, 1); 01334 $object = odbc_result($resultset, 2); 01335 $otype = odbc_result($resultset, 3); 01336 $olang = odbc_result($resultset, 4); 01337 01338 if(isset($metaDescription[$predicate])) 01339 { 01340 array_push($metaDescription[$predicate], $object); 01341 } 01342 else 01343 { 01344 $metaDescription[$predicate] = array( $object ); 01345 01346 if($olang && $olang != "") 01347 { 01348 /* If a language is defined for an object, we force its type to be xsd:string */ 01349 $metaDescription[$predicate]["type"] = "http://www.w3.org/2001/XMLSchema#string"; 01350 } 01351 else 01352 { 01353 $metaDescription[$predicate]["type"] = $otype; 01354 } 01355 } 01356 } 01357 } 01358 01359 unset($resultset); 01360 } 01361 01362 01363 // Get all contributors (users that have CUD perissions over the dataset) 01364 $query = 01365 "select ?contributor 01366 from <" . $this->wsf_graph 01367 . "datasets/> 01368 where 01369 { 01370 <$dataset> a <http://rdfs.org/ns/void#Dataset> ; 01371 <http://purl.org/dc/terms/contributor> ?contributor. 01372 }"; 01373 01374 $resultset = 01375 @$this->db->query($this->db->build_sparql_query(str_replace(array ("\n", "\r", "\t"), " ", $query), 01376 array( 'contributor' ), FALSE)); 01377 01378 $contributors = array(); 01379 01380 if(odbc_error()) 01381 { 01382 $this->conneg->setStatus(500); 01383 $this->conneg->setStatusMsg("Internal Error"); 01384 $this->conneg->setStatusMsgExt($this->errorMessenger->_303->name); 01385 $this->conneg->setError($this->errorMessenger->_303->id, $this->errorMessenger->ws, 01386 $this->errorMessenger->_303->name, $this->errorMessenger->_303->description, odbc_errormsg(), 01387 $this->errorMessenger->_303->level); 01388 01389 return; 01390 } 01391 elseif(odbc_fetch_row($resultset)) 01392 { 01393 array_push($contributors, odbc_result($resultset, 1)); 01394 } 01395 01396 array_push($this->datasetsDescription, 01397 new DatasetDescription($dataset, $title, $description, $creator, $created, $modified, $contributors, 01398 $meta, $metaDescription)); 01399 } 01400 } 01401 } 01402 01403 if(count($this->datasetsDescription) == 0 && $this->datasetUri != "all") 01404 { 01405 $this->conneg->setStatus(400); 01406 $this->conneg->setStatusMsg("Bad Request"); 01407 $this->conneg->setStatusMsgExt("This dataset doesn't exist in this WSF"); 01408 $this->conneg->setStatusMsgExt($this->errorMessenger->_304->name); 01409 $this->conneg->setError($this->errorMessenger->_304->id, $this->errorMessenger->ws, 01410 $this->errorMessenger->_304->name, $this->errorMessenger->_304->description, "", 01411 $this->errorMessenger->_304->level); 01412 } 01413 } 01414 } 01415 } 01416 01426 class DatasetDescription 01427 { 01429 public $uri = ""; 01430 01432 public $title = ""; 01433 01435 public $description = ""; 01436 01438 public $creator = ""; 01439 01441 public $created = ""; 01442 01444 public $modified = ""; 01445 01447 public $contributors = array(); 01448 01450 public $meta = ""; 01451 01453 public $metaDescription = ""; 01454 01455 function __construct($uri, $title, $description, $creator, $created, $modified, $contributors, $meta = "", 01456 $metaDescription = array()) 01457 { 01458 $this->uri = $uri; 01459 $this->title = $title; 01460 $this->description = $description; 01461 $this->creator = $creator; 01462 $this->created = $created; 01463 $this->modified = $modified; 01464 $this->contributors = $contributors; 01465 $this->meta = $meta; 01466 $this->metaDescription = $metaDescription; 01467 } 01468 } 01469 01471 01472 ?>
