CrudDelete.php
Go to the documentation of this file.
00001 <?php 00002 00005 00027 class CrudDelete extends WebService 00028 { 00030 private $db; 00031 00033 private $conneg; 00034 00036 private $dtdURL; 00037 00039 public static $supportedSerializations = 00040 array ("application/json", "application/rdf+xml", "application/rdf+n3", "application/*", "text/xml", "text/*", 00041 "*/*"); 00042 00044 private $registered_ip = ""; 00045 00047 private $dataset; 00048 00050 private $resourceUri; 00051 00053 private $requester_ip = ""; 00054 00056 private $errorMessenger = 00057 '{ 00058 "ws": "/ws/crud/delete/", 00059 "_200": { 00060 "id": "WS-CRUD-DELETE-200", 00061 "level": "Warning", 00062 "name": "No resource URI to delete specified", 00063 "description": "No resource URI has been defined for this query" 00064 }, 00065 "_201": { 00066 "id": "WS-CRUD-DELETE-201", 00067 "level": "Warning", 00068 "name": "No dataset specified", 00069 "description": "No dataset URI defined for this query" 00070 }, 00071 "_300": { 00072 "id": "WS-CRUD-DELETE-300", 00073 "level": "Fatal", 00074 "name": "Can\'t delete the record in the triple store", 00075 "description": "An error occured when we tried to delete that record in the triple store" 00076 }, 00077 "_301": { 00078 "id": "WS-CRUD-DELETE-301", 00079 "level": "Fatal", 00080 "name": "Can\'t delete the record in Solr", 00081 "description": "An error occured when we tried to delete that record in Solr" 00082 }, 00083 "_302": { 00084 "id": "WS-CRUD-DELETE-302", 00085 "level": "Fatal", 00086 "name": "Can\'t commit changes to the Solr index", 00087 "description": "An error occured when we tried to commit changes to the Solr index" 00088 }, 00089 "_303": { 00090 "id": "WS-CRUD-CREATE-303", 00091 "level": "Fatal", 00092 "name": "Can\'t create a tracking record for one of the input records", 00093 "description": "We can\'t create the records because we can\'t ensure that we have a track of their changes." 00094 } 00095 }'; 00096 00097 00114 function __construct($uri, $dataset, $registered_ip, $requester_ip) 00115 { 00116 parent::__construct(); 00117 00118 $this->db = new DB_Virtuoso($this->db_username, $this->db_password, $this->db_dsn, $this->db_host); 00119 00120 $this->requester_ip = $requester_ip; 00121 $this->dataset = $dataset; 00122 $this->resourceUri = $uri; 00123 00124 if($registered_ip == "") 00125 { 00126 $this->registered_ip = $requester_ip; 00127 } 00128 else 00129 { 00130 $this->registered_ip = $registered_ip; 00131 } 00132 00133 if(strtolower(substr($this->registered_ip, 0, 4)) == "self") 00134 { 00135 $pos = strpos($this->registered_ip, "::"); 00136 00137 if($pos !== FALSE) 00138 { 00139 $account = substr($this->registered_ip, $pos + 2, strlen($this->registered_ip) - ($pos + 2)); 00140 00141 $this->registered_ip = $requester_ip . "::" . $account; 00142 } 00143 else 00144 { 00145 $this->registered_ip = $requester_ip; 00146 } 00147 } 00148 00149 $this->uri = $this->wsf_base_url . "/wsf/ws/crud/delete/"; 00150 $this->title = "Crud Delete Web Service"; 00151 $this->crud_usage = new CrudUsage(FALSE, FALSE, FALSE, TRUE); 00152 $this->endpoint = $this->wsf_base_url . "/ws/crud/delete/"; 00153 00154 $this->dtdURL = "auth/CrudDelete.dtd"; 00155 00156 $this->errorMessenger = json_decode($this->errorMessenger); 00157 } 00158 00159 function __destruct() 00160 { 00161 parent::__destruct(); 00162 00163 if(isset($this->db)) 00164 { 00165 @$this->db->close(); 00166 } 00167 } 00168 00179 protected function validateQuery() 00180 { 00181 // Validation of the "requester_ip" to make sure the system that is sending the query as the rights. 00182 $ws_av = new AuthValidator($this->requester_ip, $this->dataset, $this->uri); 00183 00184 $ws_av->pipeline_conneg($this->conneg->getAccept(), $this->conneg->getAcceptCharset(), 00185 $this->conneg->getAcceptEncoding(), $this->conneg->getAcceptLanguage()); 00186 00187 $ws_av->process(); 00188 00189 if($ws_av->pipeline_getResponseHeaderStatus() != 200) 00190 { 00191 $this->conneg->setStatus($ws_av->pipeline_getResponseHeaderStatus()); 00192 $this->conneg->setStatusMsg($ws_av->pipeline_getResponseHeaderStatusMsg()); 00193 $this->conneg->setStatusMsgExt($ws_av->pipeline_getResponseHeaderStatusMsgExt()); 00194 $this->conneg->setError($ws_av->pipeline_getError()->id, $ws_av->pipeline_getError()->webservice, 00195 $ws_av->pipeline_getError()->name, $ws_av->pipeline_getError()->description, 00196 $ws_av->pipeline_getError()->debugInfo, $ws_av->pipeline_getError()->level); 00197 00198 return; 00199 } 00200 00201 unset($ws_av); 00202 00203 // If the system send a query on the behalf of another user, we validate that other user as well 00204 if($this->registered_ip != $this->requester_ip) 00205 { 00206 // Validation of the "registered_ip" to make sure the user of this system has the rights 00207 $ws_av = new AuthValidator($this->registered_ip, $this->dataset, $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 $this->conneg->setStatus($ws_av->pipeline_getResponseHeaderStatus()); 00217 $this->conneg->setStatusMsg($ws_av->pipeline_getResponseHeaderStatusMsg()); 00218 $this->conneg->setStatusMsgExt($ws_av->pipeline_getResponseHeaderStatusMsgExt()); 00219 $this->conneg->setError($ws_av->pipeline_getError()->id, $ws_av->pipeline_getError()->webservice, 00220 $ws_av->pipeline_getError()->name, $ws_av->pipeline_getError()->description, 00221 $ws_av->pipeline_getError()->debugInfo, $ws_av->pipeline_getError()->level); 00222 return; 00223 } 00224 } 00225 } 00226 00237 public function pipeline_getError() { return ($this->conneg->error); } 00238 00239 00250 public function pipeline_getResultset() { return ""; } 00251 00264 public function injectDoctype($xmlDoc) 00265 { 00266 $posHeader = strpos($xmlDoc, '"?>') + 3; 00267 $xmlDoc = substr($xmlDoc, 0, $posHeader) 00268 . "\n<!DOCTYPE resultset PUBLIC \"-//Structured Dynamics LLC//Crud Delete DTD 0.1//EN\" \"" . $this->dtdBaseURL 00269 . $this->dtdURL . "\">" . substr($xmlDoc, $posHeader, strlen($xmlDoc) - $posHeader); 00270 00271 return ($xmlDoc); 00272 } 00273 00292 public function ws_conneg($accept, $accept_charset, $accept_encoding, $accept_language) 00293 { 00294 $this->conneg = 00295 new Conneg($accept, $accept_charset, $accept_encoding, $accept_language, CrudDelete::$supportedSerializations); 00296 00297 // Check for errors 00298 00299 if($this->uri == "") 00300 { 00301 $this->conneg->setStatus(400); 00302 $this->conneg->setStatusMsg("Bad Request"); 00303 $this->conneg->setStatusMsgExt($this->errorMessenger->_200->name); 00304 $this->conneg->setError($this->errorMessenger->_200->id, $this->errorMessenger->ws, 00305 $this->errorMessenger->_200->name, $this->errorMessenger->_200->description, "", 00306 $this->errorMessenger->_200->level); 00307 00308 return; 00309 } 00310 00311 if($this->dataset == "") 00312 { 00313 $this->conneg->setStatus(400); 00314 $this->conneg->setStatusMsg("Bad Request"); 00315 $this->conneg->setStatusMsgExt($this->errorMessenger->_201->name); 00316 $this->conneg->setError($this->errorMessenger->_201->id, $this->errorMessenger->ws, 00317 $this->errorMessenger->_201->name, $this->errorMessenger->_201->description, "", 00318 $this->errorMessenger->_201->level); 00319 00320 return; 00321 } 00322 00323 // Check if the dataset is created 00324 00325 $ws_dr = new DatasetRead($this->dataset, "false", "self", 00326 $this->wsf_local_ip); // Here the one that makes the request is the WSF (internal request). 00327 00328 $ws_dr->pipeline_conneg($this->conneg->getAccept(), $this->conneg->getAcceptCharset(), 00329 $this->conneg->getAcceptEncoding(), $this->conneg->getAcceptLanguage()); 00330 00331 $ws_dr->process(); 00332 00333 if($ws_dr->pipeline_getResponseHeaderStatus() != 200) 00334 { 00335 $this->conneg->setStatus($ws_dr->pipeline_getResponseHeaderStatus()); 00336 $this->conneg->setStatusMsg($ws_dr->pipeline_getResponseHeaderStatusMsg()); 00337 $this->conneg->setStatusMsgExt($ws_dr->pipeline_getResponseHeaderStatusMsgExt()); 00338 $this->conneg->setError($ws_av->pipeline_getError()->id, $ws_av->pipeline_getError()->webservice, 00339 $ws_av->pipeline_getError()->name, $ws_av->pipeline_getError()->description, 00340 $ws_av->pipeline_getError()->debugInfo, $ws_av->pipeline_getError()->level); 00341 00342 return; 00343 } 00344 } 00345 00364 public function pipeline_conneg($accept, $accept_charset, $accept_encoding, $accept_language) 00365 { $this->ws_conneg($accept, $accept_charset, $accept_encoding, $accept_language); } 00366 00377 public function pipeline_getResponseHeaderStatus() { return $this->conneg->getStatus(); } 00378 00389 public function pipeline_getResponseHeaderStatusMsg() { return $this->conneg->getStatusMsg(); } 00390 00403 public function pipeline_getResponseHeaderStatusMsgExt() { return $this->conneg->getStatusMsgExt(); } 00404 00415 public function pipeline_serialize() { return ""; } 00416 00425 public function pipeline_serialize_reification() { return ""; } 00426 00437 public function ws_serialize() { return ""; } 00438 00451 public function ws_respond($content) 00452 { 00453 // First send the header of the request 00454 $this->conneg->respond(); 00455 00456 // second, send the content of the request 00457 00458 // Make sure there is no error. 00459 if($this->conneg->getStatus() == 200) 00460 { 00461 echo $content; 00462 } 00463 00464 $this->__destruct(); 00465 } 00466 00467 00476 public function process() 00477 { 00478 // Make sure there was no conneg error prior to this process call 00479 if($this->conneg->getStatus() == 200) 00480 { 00481 $this->validateQuery(); 00482 00483 // If the query is still valid 00484 if($this->conneg->getStatus() == 200) 00485 { 00486 00487 // Track the record description changes 00488 if($this->track_delete === TRUE) 00489 { 00490 // First check if the record is already existing for this record, within this dataset. 00491 include_once("../read/CrudRead.php"); 00492 00493 $ws_cr = new CrudRead($this->resourceUri, $this->dataset, FALSE, TRUE, $this->registered_ip, $this->requester_ip); 00494 00495 $ws_cr->ws_conneg("application/rdf+xml", "utf-8", "identity", "en"); 00496 00497 $ws_cr->process(); 00498 00499 $oldRecordDescription = $ws_cr->ws_serialize(); 00500 00501 $ws_cr_error = $ws_cr->pipeline_getError(); 00502 00503 if($ws_cr->pipeline_getResponseHeaderStatus() != 200) 00504 { 00505 // An error occured. Since we can't get the past state of a record, we have to send an error 00506 // for the CrudUpdate call since we can't create a tracking record for this record. 00507 $this->conneg->setStatus(400); 00508 $this->conneg->setStatusMsg("Bad Request"); 00509 $this->conneg->setError($this->errorMessenger->_303->id, $this->errorMessenger->ws, 00510 $this->errorMessenger->_303->name, $this->errorMessenger->_303->description, 00511 "We can't create a track record for the following record: ".$this->resourceUri, 00512 $this->errorMessenger->_303->level); 00513 00514 break; 00515 } 00516 00517 $endpoint = ""; 00518 if($this->tracking_endpoint != "") 00519 { 00520 // We send the query to a remove tracking endpoint 00521 $endpoint = $this->tracking_endpoint."create/"; 00522 } 00523 else 00524 { 00525 // We send the query to a local tracking endpoint 00526 $endpoint = $this->wsf_base_url."/ws/tracker/create/"; 00527 } 00528 00529 include_once("../../framework/WebServiceQuerier.php"); 00530 00531 $wsq = new WebServiceQuerier($endpoint, "post", 00532 "text/xml", "from_dataset=" . urlencode($this->dataset) . 00533 "&record=" . urlencode($this->resourceUri) . 00534 "&action=delete" . 00535 "&previous_state=" . urlencode($oldRecordDescription) . 00536 "&previous_state_mime=" . urlencode("application/rdf+xml") . 00537 "&performer=" . urlencode($this->registered_ip) . 00538 "®istered_ip=self"); 00539 00540 if($wsq->getStatus() != 200) 00541 { 00542 $this->conneg->setStatus($wsq->getStatus()); 00543 $this->conneg->setStatusMsg($wsq->getStatusMessage()); 00544 /* 00545 $this->conneg->setError($this->errorMessenger->_302->id, $this->errorMessenger->ws, 00546 $this->errorMessenger->_302->name, $this->errorMessenger->_302->description, odbc_errormsg(), 00547 $this->errorMessenger->_302->level); 00548 */ 00549 } 00550 00551 unset($wsq); 00552 } 00553 00554 // Delete all triples for this URI in that dataset 00555 $query = "delete from <" . $this->dataset . "> 00556 { 00557 <" . $this->resourceUri . "> ?p ?o. 00558 } 00559 where 00560 { 00561 <" . $this->resourceUri . "> ?p ?o. 00562 }"; 00563 00564 @$this->db->query($this->db->build_sparql_query(str_replace(array ("\n", "\r", "\t"), " ", $query), array(), 00565 FALSE)); 00566 00567 if(odbc_error()) 00568 { 00569 $this->conneg->setStatus(500); 00570 $this->conneg->setStatusMsg("Internal Error"); 00571 $this->conneg->setStatusMsgExt($this->errorMessenger->_300->name); 00572 $this->conneg->setError($this->errorMessenger->_300->id, $this->errorMessenger->ws, 00573 $this->errorMessenger->_300->name, $this->errorMessenger->_300->description, odbc_errormsg(), 00574 $this->errorMessenger->_300->level); 00575 00576 return; 00577 } 00578 00579 // Delete the Solr document in the Solr index 00580 $solr = new Solr($this->wsf_solr_core, $this->solr_host, $this->solr_port); 00581 00582 if(!$solr->deleteInstanceRecord($this->resourceUri, $this->dataset)) 00583 { 00584 $this->conneg->setStatus(500); 00585 $this->conneg->setStatusMsg("Internal Error"); 00586 $this->conneg->setStatusMsgExt($this->errorMessenger->_301->name); 00587 $this->conneg->setError($this->errorMessenger->_301->id, $this->errorMessenger->ws, 00588 $this->errorMessenger->_301->name, $this->errorMessenger->_301->description, odbc_errormsg(), 00589 $this->errorMessenger->_301->level); 00590 00591 return; 00592 } 00593 00594 if($this->solr_auto_commit === FALSE) 00595 { 00596 if(!$solr->commit()) 00597 { 00598 $this->conneg->setStatus(500); 00599 $this->conneg->setStatusMsg("Internal Error"); 00600 $this->conneg->setStatusMsgExt($this->errorMessenger->_302->name); 00601 $this->conneg->setError($this->errorMessenger->_302->id, $this->errorMessenger->ws, 00602 $this->errorMessenger->_302->name, $this->errorMessenger->_302->description, odbc_errormsg(), 00603 $this->errorMessenger->_302->level); 00604 00605 return; 00606 } 00607 } 00608 } 00609 } 00610 } 00611 } 00612 00614 00615 ?>
