DatasetUpdate.php
Go to the documentation of this file.
00001 <?php 00002 00005 00026 class DatasetUpdate extends WebService 00027 { 00029 private $db; 00030 00032 private $conneg; 00033 00035 private $dtdURL; 00036 00038 private $requester_ip = ""; 00039 00041 private $datasetUri = ""; 00042 00044 private $datasetTitle = ""; 00045 00047 private $description = ""; 00048 00050 private $contributors = ""; 00051 00053 private $modified = ""; 00054 00056 public static $supportedSerializations = 00057 array ("application/json", "application/rdf+xml", "application/rdf+n3", "application/*", "text/xml", "text/*", 00058 "*/*"); 00059 00061 private $errorMessenger = 00062 '{ 00063 "ws": "/ws/dataset/update/", 00064 "_200": { 00065 "id": "WS-DATASET-UPDATE-200", 00066 "level": "Warning", 00067 "name": "No unique identifier specified for this dataset", 00068 "description": "No URI defined for this new dataset" 00069 }, 00070 "_201": { 00071 "id": "WS-DATASET-UPDATE-201", 00072 "level": "Fatal", 00073 "name": "Can\'t check if the dataset is existing", 00074 "description": "An error occured when we tried to check if the dataset was existing" 00075 }, 00076 "_202": { 00077 "id": "WS-DATASET-UPDATE-202", 00078 "level": "Warning", 00079 "name": "This dataset doesn\'t exist in this WSF", 00080 "description": "The target dataset is not existing in the web service framework" 00081 }, 00082 "_300": { 00083 "id": "WS-DATASET-UPDATE-300", 00084 "level": "Fatal", 00085 "name": "Can\'t update the title of the dataset in the triple store", 00086 "description": "An error occured when we tried to update the title of the dataset in the triple store" 00087 }, 00088 "_301": { 00089 "id": "WS-DATASET-UPDATE-301", 00090 "level": "Fatal", 00091 "name": "Can\'t update the description of the dataset in the triple store", 00092 "description": "An error occured when we tried to update the description of the dataset in the triple store" 00093 }, 00094 "_302": { 00095 "id": "WS-DATASET-UPDATE-302", 00096 "level": "Fatal", 00097 "name": "Can\'t update the last modification date of the dataset in the triple store", 00098 "description": "An error occured when we tried to update the last modification date of the dataset in the triple store" 00099 }, 00100 "_303": { 00101 "id": "WS-DATASET-UPDATE-303", 00102 "level": "Fatal", 00103 "name": "Can\'t update the contributors of the dataset in the triple store", 00104 "description": "An error occured when we tried to update the contributors of the dataset in the triple store" 00105 } 00106 }'; 00107 00108 00128 function __construct($uri, $title, $description, $contributors, $modified, $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->datasetTitle = $title; 00136 $this->description = $description; 00137 $this->contributors = $contributors; 00138 $this->modified = $modified; 00139 $this->requester_ip = $requester_ip; 00140 00141 if($registered_ip == "") 00142 { 00143 $this->registered_ip = $requester_ip; 00144 } 00145 else 00146 { 00147 $this->registered_ip = $registered_ip; 00148 } 00149 00150 if(strtolower(substr($this->registered_ip, 0, 4)) == "self") 00151 { 00152 $pos = strpos($this->registered_ip, "::"); 00153 00154 if($pos !== FALSE) 00155 { 00156 $account = substr($this->registered_ip, $pos + 2, strlen($this->registered_ip) - ($pos + 2)); 00157 00158 $this->registered_ip = $requester_ip . "::" . $account; 00159 } 00160 else 00161 { 00162 $this->registered_ip = $requester_ip; 00163 } 00164 } 00165 00166 $this->uri = $this->wsf_base_url . "/wsf/ws/dataset/update/"; 00167 $this->title = "Dataset Update Web Service"; 00168 $this->crud_usage = new CrudUsage(FALSE, FALSE, TRUE, FALSE); 00169 $this->endpoint = $this->wsf_base_url . "/ws/dataset/update/"; 00170 00171 $this->dtdURL = "dataset/datasetUpdate.dtd"; 00172 00173 $this->errorMessenger = json_decode($this->errorMessenger); 00174 } 00175 00176 function __destruct() 00177 { 00178 parent::__destruct(); 00179 00180 if(isset($this->db)) 00181 { 00182 @$this->db->close(); 00183 } 00184 } 00185 00207 protected function validateQuery() 00208 { 00209 // Check if the requester has access to the main "http://.../wsf/datasets/" graph. 00210 $ws_av = new AuthValidator($this->requester_ip, $this->wsf_graph . "datasets/", $this->uri); 00211 00212 $ws_av->pipeline_conneg($this->conneg->getAccept(), $this->conneg->getAcceptCharset(), 00213 $this->conneg->getAcceptEncoding(), $this->conneg->getAcceptLanguage()); 00214 00215 $ws_av->process(); 00216 00217 if($ws_av->pipeline_getResponseHeaderStatus() != 200) 00218 { 00219 // If he doesn't, then check if he has access to the dataset itself 00220 $ws_av2 = new AuthValidator($this->requester_ip, $this->datasetUri, $this->uri); 00221 00222 $ws_av2->pipeline_conneg($this->conneg->getAccept(), $this->conneg->getAcceptCharset(), 00223 $this->conneg->getAcceptEncoding(), $this->conneg->getAcceptLanguage()); 00224 00225 $ws_av2->process(); 00226 00227 if($ws_av2->pipeline_getResponseHeaderStatus() != 200) 00228 { 00229 $this->conneg->setStatus($ws_av2->pipeline_getResponseHeaderStatus()); 00230 $this->conneg->setStatusMsg($ws_av2->pipeline_getResponseHeaderStatusMsg()); 00231 $this->conneg->setStatusMsgExt($ws_av2->pipeline_getResponseHeaderStatusMsgExt()); 00232 $this->conneg->setError($ws_av2->pipeline_getError()->id, $ws_av2->pipeline_getError()->webservice, 00233 $ws_av2->pipeline_getError()->name, $ws_av2->pipeline_getError()->description, 00234 $ws_av2->pipeline_getError()->debugInfo, $ws_av2->pipeline_getError()->level); 00235 00236 return; 00237 } 00238 } 00239 00240 // If the system send a query on the behalf of another user, we validate that other user as well 00241 if($this->registered_ip != $this->requester_ip) 00242 { 00243 // Check if the requester has access to the main "http://.../wsf/datasets/" graph. 00244 $ws_av = new AuthValidator($this->registered_ip, $this->wsf_graph . "datasets/", $this->uri); 00245 00246 $ws_av->pipeline_conneg($this->conneg->getAccept(), $this->conneg->getAcceptCharset(), 00247 $this->conneg->getAcceptEncoding(), $this->conneg->getAcceptLanguage()); 00248 00249 $ws_av->process(); 00250 00251 if($ws_av->pipeline_getResponseHeaderStatus() != 200) 00252 { 00253 // If he doesn't, then check if he has access to the dataset itself 00254 $ws_av2 = new AuthValidator($this->registered_ip, $this->datasetUri, $this->uri); 00255 00256 $ws_av2->pipeline_conneg($this->conneg->getAccept(), $this->conneg->getAcceptCharset(), 00257 $this->conneg->getAcceptEncoding(), $this->conneg->getAcceptLanguage()); 00258 00259 $ws_av2->process(); 00260 00261 if($ws_av2->pipeline_getResponseHeaderStatus() != 200) 00262 { 00263 $this->conneg->setStatus($ws_av2->pipeline_getResponseHeaderStatus()); 00264 $this->conneg->setStatusMsg($ws_av2->pipeline_getResponseHeaderStatusMsg()); 00265 $this->conneg->setStatusMsgExt($ws_av2->pipeline_getResponseHeaderStatusMsgExt()); 00266 $this->conneg->setError($ws_av2->pipeline_getError()->id, $ws_av2->pipeline_getError()->webservice, 00267 $ws_av2->pipeline_getError()->name, $ws_av2->pipeline_getError()->description, 00268 $ws_av2->pipeline_getError()->debugInfo, $ws_av2->pipeline_getError()->level); 00269 00270 return; 00271 } 00272 } 00273 } 00274 } 00275 00286 public function pipeline_getError() { return ($this->conneg->error); } 00287 00288 00299 public function pipeline_getResultset() { return ""; } 00300 00313 public function injectDoctype($xmlDoc) { return ""; } 00314 00333 public function ws_conneg($accept, $accept_charset, $accept_encoding, $accept_language) 00334 { 00335 $this->conneg = 00336 new Conneg($accept, $accept_charset, $accept_encoding, $accept_language, DatasetUpdate::$supportedSerializations); 00337 00338 // Validate query 00339 $this->validateQuery(); 00340 00341 // If the query is still valid 00342 if($this->conneg->getStatus() == 200) 00343 { 00344 // Check for errors 00345 if($this->datasetUri == "") 00346 { 00347 $this->conneg->setStatus(400); 00348 $this->conneg->setStatusMsg("Bad Request"); 00349 $this->conneg->setStatusMsgExt($this->errorMessenger->_200->name); 00350 $this->conneg->setError($this->errorMessenger->_200->id, $this->errorMessenger->ws, 00351 $this->errorMessenger->_200->name, $this->errorMessenger->_200->description, "", 00352 $this->errorMessenger->_200->level); 00353 00354 return; 00355 } 00356 00357 // Check if the dataset is existing 00358 $query .= " select ?dataset 00359 from <" . $this->wsf_graph . "datasets/> 00360 where 00361 { 00362 <$this->datasetUri> a ?dataset . 00363 }"; 00364 00365 $resultset = @$this->db->query($this->db->build_sparql_query(str_replace(array ("\n", "\r", "\t"), " ", $query), 00366 array( "dataset" ), FALSE)); 00367 00368 if(odbc_error()) 00369 { 00370 $this->conneg->setStatus(500); 00371 $this->conneg->setStatusMsg("Internal Error"); 00372 $this->conneg->setStatusMsgExt($this->errorMessenger->_201->name); 00373 $this->conneg->setError($this->errorMessenger->_201->id, $this->errorMessenger->ws, 00374 $this->errorMessenger->_201->name, $this->errorMessenger->_201->description, odbc_errormsg(), 00375 $this->errorMessenger->_201->level); 00376 00377 return; 00378 } 00379 elseif(odbc_fetch_row($resultset) === FALSE) 00380 { 00381 $this->conneg->setStatus(400); 00382 $this->conneg->setStatusMsg("Bad Request"); 00383 $this->conneg->setStatusMsgExt($this->errorMessenger->_202->name); 00384 $this->conneg->setError($this->errorMessenger->_202->id, $this->errorMessenger->ws, 00385 $this->errorMessenger->_202->name, $this->errorMessenger->_202->description, "", 00386 $this->errorMessenger->_202->level); 00387 00388 unset($resultset); 00389 } 00390 00391 unset($resultset); 00392 } 00393 } 00394 00413 public function pipeline_conneg($accept, $accept_charset, $accept_encoding, $accept_language) 00414 { $this->ws_conneg($accept, $accept_charset, $accept_encoding, $accept_language); } 00415 00426 public function pipeline_getResponseHeaderStatus() { return $this->conneg->getStatus(); } 00427 00438 public function pipeline_getResponseHeaderStatusMsg() { return $this->conneg->getStatusMsg(); } 00439 00452 public function pipeline_getResponseHeaderStatusMsgExt() { return $this->conneg->getStatusMsgExt(); } 00453 00464 public function pipeline_serialize() { return ""; } 00465 00474 public function pipeline_serialize_reification() { return ""; } 00475 00486 public function ws_serialize() { return ""; } 00487 00500 public function ws_respond($content) 00501 { 00502 // First send the header of the request 00503 $this->conneg->respond(); 00504 00505 // second, send the content of the request 00506 00507 // Make sure there is no error. 00508 if($this->conneg->getStatus() == 200) 00509 { 00510 echo $content; 00511 } 00512 00513 $this->__destruct(); 00514 } 00515 00516 00525 public function process() 00526 { 00527 // Make sure there was no conneg error prior to this process call 00528 if($this->conneg->getStatus() == 200) 00529 { 00530 /* 00531 $query = "modify <".$this->wsf_graph."datasets/> 00532 delete 00533 { 00534 ".($this->datasetTitle != "" ? "<$this->datasetUri> <http://purl.org/dc/terms/title> ?datasetTitle ." : "")." 00535 ".($this->description != "" ? "<$this->datasetUri> <http://purl.org/dc/terms/description> ?description ." : "")." 00536 ".($this->modified != "" ? "<$this->datasetUri> <http://purl.org/dc/terms/modified> ?modified ." : "")." 00537 ".(count($this->contributors) > 0 && isset($contributor[0]) ? "<$this->datasetUri> <http://purl.org/dc/terms/contributor> ?contributors ." : "")." 00538 } 00539 insert 00540 { 00541 ".($this->datasetTitle != "" ? "<$this->datasetUri> <http://purl.org/dc/terms/title> \"\"\"$this->datasetTitle\"\"\" ." : "")." 00542 ".($this->description != "" ? "<$this->datasetUri> <http://purl.org/dc/terms/description> \"\"\"$this->description\"\"\" ." : "")." 00543 ".($this->modified != "" ? "<$this->datasetUri> <http://purl.org/dc/terms/modified> \"\"\"$this->modified\"\"\" ." : "").""; 00544 00545 foreach($this->contributors as $contributor) 00546 { 00547 $query .= ($this->contributor != "" ? "<$this->datasetUri> <http://purl.org/dc/terms/contributor> <$contributor> ." : ""); 00548 } 00549 00550 $query .= "} 00551 where 00552 { 00553 graph <".$this->wsf_graph."datasets/> 00554 { 00555 <$this->datasetUri> a <http://rdfs.org/ns/void#Dataset> . 00556 ".($this->datasetTitle != "" ? "<$this->datasetUri> <http://purl.org/dc/terms/title> ?datasetTitle ." : "")." 00557 ".($this->description != "" ? "<$this->datasetUri> <http://purl.org/dc/terms/description> ?description ." : "")." 00558 ".($this->modified != "" ? "<$this->datasetUri> <http://purl.org/dc/terms/modified> ?modified ." : "")." 00559 ".(count($this->contributors) > 0 ? "<$this->datasetUri> <http://purl.org/dc/terms/contributor> ?contributors ." : "")." 00560 } 00561 }"; 00562 */ 00563 00564 // Note: here we can't create a single SPARUL query to update everything because if one of the clause is not existing in the "delete" pattern, 00565 // then nothing will be updated. Also, the problem come from the fact that "OPTIONAL" clauses only happen at the level of the "where" clause 00566 // and can't be used in the "delete" clause. 00567 00568 // Updating the title if it exists in the description 00569 if($this->datasetTitle != "") 00570 { 00571 00572 $query = "delete from <" . $this->wsf_graph . "datasets/> 00573 { 00574 <$this->datasetUri> <http://purl.org/dc/terms/title> ?datasetTitle . 00575 } 00576 where 00577 { 00578 graph <" . $this->wsf_graph . "datasets/> 00579 { 00580 <$this->datasetUri> a <http://rdfs.org/ns/void#Dataset> . 00581 <$this->datasetUri> <http://purl.org/dc/terms/title> ?datasetTitle . 00582 } 00583 } 00584 " . ($this->datasetTitle != "-delete-" ? " 00585 insert into <" . $this->wsf_graph . "datasets/> 00586 { 00587 <$this->datasetUri> <http://purl.org/dc/terms/title> \"\"\"" . str_replace("'", "\'", $this->datasetTitle) . "\"\"\" . 00588 }" : ""); 00589 } 00590 00591 @$this->db->query($this->db->build_sparql_query(str_replace(array ("\n", "\r", "\t"), " ", $query), array(), 00592 FALSE)); 00593 00594 if(odbc_error()) 00595 { 00596 $this->conneg->setStatus(500); 00597 $this->conneg->setStatusMsg("Internal Error"); 00598 $this->conneg->setStatusMsgExt($this->errorMessenger->_300->name); 00599 $this->conneg->setError($this->errorMessenger->_300->id, $this->errorMessenger->ws, 00600 $this->errorMessenger->_300->name, $this->errorMessenger->_300->description, odbc_errormsg(), 00601 $this->errorMessenger->_300->level); 00602 00603 return; 00604 } 00605 00606 // Updating the description if it exists in the description 00607 if($this->description != "") 00608 { 00609 $query = "delete from <" . $this->wsf_graph . "datasets/> 00610 { 00611 <$this->datasetUri> <http://purl.org/dc/terms/description> ?description . 00612 } 00613 where 00614 { 00615 graph <" . $this->wsf_graph . "datasets/> 00616 { 00617 <$this->datasetUri> a <http://rdfs.org/ns/void#Dataset> . 00618 <$this->datasetUri> <http://purl.org/dc/terms/description> ?description . 00619 } 00620 } 00621 " . ($this->description != "-delete-" ? " 00622 insert into <" . $this->wsf_graph . "datasets/> 00623 { 00624 <$this->datasetUri> <http://purl.org/dc/terms/description> \"\"\"" . str_replace("'", "\'", $this->description) . "\"\"\" . 00625 }" : ""); 00626 } 00627 00628 @$this->db->query($this->db->build_sparql_query(str_replace(array ("\n", "\r", "\t"), " ", $query), array(), 00629 FALSE)); 00630 00631 if(odbc_error()) 00632 { 00633 $this->conneg->setStatus(500); 00634 $this->conneg->setStatusMsg("Internal Error"); 00635 $this->conneg->setStatusMsgExt($this->errorMessenger->_301->name); 00636 $this->conneg->setError($this->errorMessenger->_301->id, $this->errorMessenger->ws, 00637 $this->errorMessenger->_301->name, $this->errorMessenger->_301->description, odbc_errormsg(), 00638 $this->errorMessenger->_301->level); 00639 00640 return; 00641 } 00642 00643 // Updating the modification date if it exists in the description 00644 if($this->modified != "") 00645 { 00646 $query = "delete from <" . $this->wsf_graph . "datasets/> 00647 { 00648 <$this->datasetUri> <http://purl.org/dc/terms/modified> ?modified . 00649 } 00650 where 00651 { 00652 graph <" . $this->wsf_graph . "datasets/> 00653 { 00654 <$this->datasetUri> a <http://rdfs.org/ns/void#Dataset> . 00655 <$this->datasetUri> <http://purl.org/dc/terms/modified> ?modified . 00656 } 00657 } 00658 " . ($this->modified != "-delete-" ? " 00659 insert into <" . $this->wsf_graph . "datasets/> 00660 { 00661 <$this->datasetUri> <http://purl.org/dc/terms/modified> \"\"\"" . str_replace("'", "\'", $this->modified) . "\"\"\" . 00662 }" : ""); 00663 } 00664 00665 @$this->db->query($this->db->build_sparql_query(str_replace(array ("\n", "\r", "\t"), " ", $query), array(), 00666 FALSE)); 00667 00668 if(odbc_error()) 00669 { 00670 $this->conneg->setStatus(500); 00671 $this->conneg->setStatusMsg("Internal Error"); 00672 $this->conneg->setStatusMsgExt($this->errorMessenger->_302->name); 00673 $this->conneg->setError($this->errorMessenger->_302->id, $this->errorMessenger->ws, 00674 $this->errorMessenger->_302->name, $this->errorMessenger->_302->description, odbc_errormsg(), 00675 $this->errorMessenger->_302->level); 00676 00677 return; 00678 } 00679 00680 // Updating the contributors list if it exists in the description 00681 if($this->contributors != "") 00682 { 00683 $query = "delete from <" . $this->wsf_graph . "datasets/> 00684 { 00685 <$this->datasetUri> <http://purl.org/dc/terms/contributor> ?contributor . 00686 } 00687 where 00688 { 00689 graph <" 00690 . $this->wsf_graph 00691 . "datasets/> 00692 { 00693 <$this->datasetUri> a <http://rdfs.org/ns/void#Dataset> . 00694 <$this->datasetUri> <http://purl.org/dc/terms/contributor> ?contributor . 00695 } 00696 }"; 00697 00698 if($this->contributors != "-delete-") 00699 { 00700 $cons = array(); 00701 00702 if(strpos($this->contributors, ";") !== FALSE) 00703 { 00704 $cons = explode(";", $this->contributors); 00705 } 00706 00707 $query .= "insert into <" . $this->wsf_graph . "datasets/> 00708 {"; 00709 00710 foreach($cons as $contributor) 00711 { 00712 $query .= "<$this->datasetUri> <http://purl.org/dc/terms/contributor> <$contributor> ."; 00713 } 00714 00715 if(count($cons) == 0) 00716 { 00717 $query .= "<$this->datasetUri> <http://purl.org/dc/terms/contributor> <$this->contributors> ."; 00718 } 00719 $query .= "}"; 00720 } 00721 } 00722 00723 @$this->db->query($this->db->build_sparql_query(str_replace(array ("\n", "\r", "\t"), " ", $query), array(), 00724 FALSE)); 00725 00726 if(odbc_error()) 00727 { 00728 $this->conneg->setStatus(500); 00729 $this->conneg->setStatusMsg("Internal Error"); 00730 $this->conneg->setStatusMsgExt($this->errorMessenger->_303->name); 00731 $this->conneg->setError($this->errorMessenger->_303->id, $this->errorMessenger->ws, 00732 $this->errorMessenger->_303->name, $this->errorMessenger->_303->description, odbc_errormsg(), 00733 $this->errorMessenger->_303->level); 00734 00735 return; 00736 } 00737 } 00738 } 00739 } 00740 00742 00743 ?>
