DatasetCreate.php
Go to the documentation of this file.
00001 <?php 00002 00005 00026 class DatasetCreate 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 $creator = ""; 00051 00053 public static $supportedSerializations = 00054 array ("application/json", "application/rdf+xml", "application/rdf+n3", "application/*", "text/xml", "text/*", 00055 "*/*"); 00056 00058 private $globalPermissions = "False;False;False;False"; 00059 00061 private $webservices = "all"; 00062 00064 private $errorMessenger = 00065 '{ 00066 "ws": "/ws/dataset/create/", 00067 "_200": { 00068 "id": "WS-DATASET-CREATE-200", 00069 "level": "Warning", 00070 "name": "No unique identifier specified for this dataset", 00071 "description": "No URI defined for this new dataset" 00072 }, 00073 "_201": { 00074 "id": "WS-DATASET-CREATE-201", 00075 "level": "Fatal", 00076 "name": "Can\'t check if the dataset is already existing", 00077 "description": "An error occured when we tried to check if the dataset was already existing in the system" 00078 }, 00079 "_202": { 00080 "id": "WS-DATASET-CREATE-202", 00081 "level": "Warning", 00082 "name": "Dataset already existing", 00083 "description": "This dataset is already existing in this web services framework" 00084 }, 00085 "_300": { 00086 "id": "WS-DATASET-CREATE-300", 00087 "level": "Fatal", 00088 "name": "Can\'t create the dataset", 00089 "description": "An error occured when we tried to register the new dataset to the web service framework" 00090 } 00091 }'; 00092 00093 00114 function __construct($uri, $datasetTitle, $description, $creator, $registered_ip, $requester_ip, 00115 $webservices = "all", $globalPermissions = "False;False;False;False") 00116 { 00117 parent::__construct(); 00118 00119 $this->db = new DB_Virtuoso($this->db_username, $this->db_password, $this->db_dsn, $this->db_host); 00120 00121 $this->datasetUri = $uri; 00122 $this->datasetTitle = $datasetTitle; 00123 $this->description = $description; 00124 $this->creator = $creator; 00125 $this->requester_ip = $requester_ip; 00126 $this->globalPermissions = $globalPermissions; 00127 $this->webservices = $webservices; 00128 00129 if($registered_ip == "") 00130 { 00131 $this->registered_ip = $requester_ip; 00132 } 00133 else 00134 { 00135 $this->registered_ip = $registered_ip; 00136 } 00137 00138 if(strtolower(substr($this->registered_ip, 0, 4)) == "self") 00139 { 00140 $pos = strpos($this->registered_ip, "::"); 00141 00142 if($pos !== FALSE) 00143 { 00144 $account = substr($this->registered_ip, $pos + 2, strlen($this->registered_ip) - ($pos + 2)); 00145 00146 $this->registered_ip = $requester_ip . "::" . $account; 00147 } 00148 else 00149 { 00150 $this->registered_ip = $requester_ip; 00151 } 00152 } 00153 00154 $this->uri = $this->wsf_base_url . "/wsf/ws/dataset/create/"; 00155 $this->title = "Dataset Create Web Service"; 00156 $this->crud_usage = new CrudUsage(TRUE, FALSE, FALSE, FALSE); 00157 $this->endpoint = $this->wsf_base_url . "/ws/dataset/create/"; 00158 00159 $this->dtdURL = "dataset/datasetCreate.dtd"; 00160 00161 $this->errorMessenger = json_decode($this->errorMessenger); 00162 } 00163 00164 function __destruct() 00165 { 00166 parent::__destruct(); 00167 00168 if(isset($this->db)) 00169 { 00170 @$this->db->close(); 00171 } 00172 } 00173 00197 protected function validateQuery() 00198 { 00199 // Only users that have "Create" access to the "http://.../wsf/datasets/" graph can create new dataset 00200 // in the structWSF instance. 00201 $ws_av = new AuthValidator($this->requester_ip, $this->wsf_graph . "datasets/", $this->uri); 00202 00203 $ws_av->pipeline_conneg($this->conneg->getAccept(), $this->conneg->getAcceptCharset(), 00204 $this->conneg->getAcceptEncoding(), $this->conneg->getAcceptLanguage()); 00205 00206 $ws_av->process(); 00207 00208 if($ws_av->pipeline_getResponseHeaderStatus() != 200) 00209 { 00210 $this->conneg->setStatus($ws_av->pipeline_getResponseHeaderStatus()); 00211 $this->conneg->setStatusMsg($ws_av->pipeline_getResponseHeaderStatusMsg()); 00212 $this->conneg->setStatusMsgExt($ws_av->pipeline_getResponseHeaderStatusMsgExt()); 00213 $this->conneg->setError($ws_av->pipeline_getError()->id, $ws_av->pipeline_getError()->webservice, 00214 $ws_av->pipeline_getError()->name, $ws_av->pipeline_getError()->description, 00215 $ws_av->pipeline_getError()->debugInfo, $ws_av->pipeline_getError()->level); 00216 00217 return; 00218 } 00219 00220 // If the system send a query on the behalf of another user, we validate that other user as well 00221 if($this->registered_ip != $this->requester_ip) 00222 { 00223 $ws_av = new AuthValidator($this->registered_ip, $this->wsf_graph . "datasets/", $this->uri); 00224 00225 $ws_av->pipeline_conneg($this->conneg->getAccept(), $this->conneg->getAcceptCharset(), 00226 $this->conneg->getAcceptEncoding(), $this->conneg->getAcceptLanguage()); 00227 00228 $ws_av->process(); 00229 00230 if($ws_av->pipeline_getResponseHeaderStatus() != 200) 00231 { 00232 $this->conneg->setStatus($ws_av->pipeline_getResponseHeaderStatus()); 00233 $this->conneg->setStatusMsg($ws_av->pipeline_getResponseHeaderStatusMsg()); 00234 $this->conneg->setStatusMsgExt($ws_av->pipeline_getResponseHeaderStatusMsgExt()); 00235 $this->conneg->setError($ws_av->pipeline_getError()->id, $ws_av->pipeline_getError()->webservice, 00236 $ws_av->pipeline_getError()->name, $ws_av->pipeline_getError()->description, 00237 $ws_av->pipeline_getError()->debugInfo, $ws_av->pipeline_getError()->level); 00238 00239 return; 00240 } 00241 } 00242 } 00243 00254 public function pipeline_getError() { return ($this->conneg->error); } 00255 00256 00267 public function pipeline_getResultset() { return ""; } 00268 00281 public function injectDoctype($xmlDoc) { return ""; } 00282 00301 public function ws_conneg($accept, $accept_charset, $accept_encoding, $accept_language) 00302 { 00303 $this->conneg = 00304 new Conneg($accept, $accept_charset, $accept_encoding, $accept_language, DatasetCreate::$supportedSerializations); 00305 00306 // Validate query 00307 $this->validateQuery(); 00308 00309 // If the query is still valid 00310 if($this->conneg->getStatus() == 200) 00311 { 00312 // Check for errors 00313 if($this->datasetUri == "") 00314 { 00315 $this->conneg->setStatus(400); 00316 $this->conneg->setStatusMsg("Bad Request"); 00317 $this->conneg->setStatusMsgExt($this->errorMessenger->_200->name); 00318 $this->conneg->setError($this->errorMessenger->_200->id, $this->errorMessenger->ws, 00319 $this->errorMessenger->_200->name, $this->errorMessenger->_200->description, "", 00320 $this->errorMessenger->_200->level); 00321 00322 return; 00323 } 00324 00325 // Check if the dataset is already existing 00326 $query .= " select ?dataset 00327 from <" . $this->wsf_graph . "datasets/> 00328 where 00329 { 00330 <" . $this->datasetUri . "> a ?dataset . 00331 }"; 00332 00333 $resultset = @$this->db->query($this->db->build_sparql_query(str_replace(array ("\n", "\r", "\t"), " ", $query), 00334 array( "dataset" ), FALSE)); 00335 00336 if(odbc_error()) 00337 { 00338 $this->conneg->setStatus(500); 00339 $this->conneg->setStatusMsg("Internal Error"); 00340 $this->conneg->setStatusMsgExt($this->errorMessenger->_201->name); 00341 $this->conneg->setError($this->errorMessenger->_201->id, $this->errorMessenger->ws, 00342 $this->errorMessenger->_201->name, $this->errorMessenger->_201->description, odbc_errormsg(), 00343 $this->errorMessenger->_201->level); 00344 00345 return; 00346 } 00347 elseif(odbc_fetch_row($resultset) !== FALSE) 00348 { 00349 $this->conneg->setStatus(400); 00350 $this->conneg->setStatusMsg("Bad Request"); 00351 $this->conneg->setStatusMsgExt($this->errorMessenger->_202->name); 00352 $this->conneg->setError($this->errorMessenger->_202->id, $this->errorMessenger->ws, 00353 $this->errorMessenger->_202->name, $this->errorMessenger->_202->description, "", 00354 $this->errorMessenger->_202->level); 00355 00356 unset($resultset); 00357 return; 00358 } 00359 00360 unset($resultset); 00361 } 00362 } 00363 00382 public function pipeline_conneg($accept, $accept_charset, $accept_encoding, $accept_language) 00383 { $this->ws_conneg($accept, $accept_charset, $accept_encoding, $accept_language); } 00384 00395 public function pipeline_getResponseHeaderStatus() { return $this->conneg->getStatus(); } 00396 00407 public function pipeline_getResponseHeaderStatusMsg() { return $this->conneg->getStatusMsg(); } 00408 00421 public function pipeline_getResponseHeaderStatusMsgExt() { return $this->conneg->getStatusMsgExt(); } 00422 00433 public function pipeline_serialize() { return ""; } 00434 00443 public function pipeline_serialize_reification() { return ""; } 00444 00455 public function ws_serialize() { return ""; } 00456 00469 public function ws_respond($content) 00470 { 00471 // First send the header of the request 00472 $this->conneg->respond(); 00473 00474 // second, send the content of the request 00475 00476 // Make sure there is no error. 00477 if($this->conneg->getStatus() == 200) 00478 { 00479 echo $content; 00480 } 00481 00482 $this->__destruct(); 00483 } 00484 00485 00494 public function process() 00495 { 00496 // Make sure there was no conneg error prior to this process call 00497 if($this->conneg->getStatus() == 200) 00498 { 00499 $query = "insert into <" . $this->wsf_graph . "datasets/> 00500 { 00501 <" . $this->datasetUri . "> a <http://rdfs.org/ns/void#Dataset> ; 00502 " . ($this->datasetTitle != "" ? "<http://purl.org/dc/terms/title> \"\"\"" . str_replace("'", "\'", $this->datasetTitle) . "\"\"\" ; " : "") . " 00503 " . ($this->description != "" ? "<http://purl.org/dc/terms/description> \"\"\"" . str_replace("'", "\'", $this->description) . "\"\"\" ; " : "") . " 00504 " . ($this->creator != "" ? "<http://purl.org/dc/terms/creator> <$this->creator> ; " : "") . " 00505 <http://purl.org/dc/terms/created> \"" . date("Y-m-j") . "\" . 00506 }"; 00507 00508 @$this->db->query($this->db->build_sparql_query(str_replace(array ("\n", "\r", "\t"), " ", $query), array(), 00509 FALSE)); 00510 00511 if(odbc_error()) 00512 { 00513 $this->conneg->setStatus(500); 00514 $this->conneg->setStatusMsg("Internal Error"); 00515 $this->conneg->setStatusMsgExt($this->errorMessenger->_300->name); 00516 $this->conneg->setError($this->errorMessenger->_300->id, $this->errorMessenger->ws, 00517 $this->errorMessenger->_300->name, $this->errorMessenger->_300->description, odbc_errormsg(), 00518 $this->errorMessenger->_300->level); 00519 00520 return; 00521 } 00522 00523 /* 00524 If the dataset has been created, the next step is to create the permissions for this user (full crud) 00525 and for the public one (no crud). 00526 */ 00527 00528 /* Get the list of web services registered to this structWSF instance. */ 00529 if(strtolower($this->webservices) == "all") 00530 { 00531 $ws_al = new AuthLister("ws", $this->datasetUri, $this->requester_ip, $this->wsf_local_ip); 00532 00533 $ws_al->pipeline_conneg($this->conneg->getAccept(), $this->conneg->getAcceptCharset(), 00534 $this->conneg->getAcceptEncoding(), $this->conneg->getAcceptLanguage()); 00535 00536 $ws_al->process(); 00537 00538 if($ws_al->pipeline_getResponseHeaderStatus() != 200) 00539 { 00540 $this->conneg->setStatus($ws_al->pipeline_getResponseHeaderStatus()); 00541 $this->conneg->setStatusMsg($ws_al->pipeline_getResponseHeaderStatusMsg()); 00542 $this->conneg->setStatusMsgExt($ws_al->pipeline_getResponseHeaderStatusMsgExt()); 00543 $this->conneg->setError($ws_al->pipeline_getError()->id, $ws_al->pipeline_getError()->webservice, 00544 $ws_al->pipeline_getError()->name, $ws_al->pipeline_getError()->description, 00545 $ws_al->pipeline_getError()->debugInfo, $ws_al->pipeline_getError()->level); 00546 00547 return; 00548 } 00549 00550 /* Get all web services */ 00551 00552 $webservices = ""; 00553 00554 $xml = new ProcessorXML(); 00555 $xml->loadXML($ws_al->pipeline_getResultset()); 00556 00557 $webServiceElements = $xml->getXPath('//predicate/object[attribute::type="wsf:WebService"]'); 00558 00559 foreach($webServiceElements as $element) 00560 { 00561 $webservices .= $xml->getURI($element) . ";"; 00562 } 00563 00564 $webservices = substr($webservices, 0, strlen($webservices) - 1); 00565 unset($xml); 00566 unset($ws_al); 00567 00568 $this->webservices = $webservices; 00569 } 00570 00571 00572 00573 /* Register full CRUD for the creator of the dataset, for the dataset's ID */ 00574 00575 $ws_ara = new AuthRegistrarAccess("True;True;True;True", $this->webservices, $this->datasetUri, "create", "", 00576 $this->requester_ip, $this->wsf_local_ip); 00577 00578 $ws_ara->pipeline_conneg($this->conneg->getAccept(), $this->conneg->getAcceptCharset(), 00579 $this->conneg->getAcceptEncoding(), $this->conneg->getAcceptLanguage()); 00580 00581 $ws_ara->process(); 00582 00583 if($ws_ara->pipeline_getResponseHeaderStatus() != 200) 00584 { 00585 $this->conneg->setStatus($ws_ara->pipeline_getResponseHeaderStatus()); 00586 $this->conneg->setStatusMsg($ws_ara->pipeline_getResponseHeaderStatusMsg()); 00587 $this->conneg->setStatusMsgExt($ws_ara->pipeline_getResponseHeaderStatusMsgExt()); 00588 $this->conneg->setError($ws_ara->pipeline_getError()->id, $ws_ara->pipeline_getError()->webservice, 00589 $ws_ara->pipeline_getError()->name, $ws_ara->pipeline_getError()->description, 00590 $ws_ara->pipeline_getError()->debugInfo, $ws_ara->pipeline_getError()->level); 00591 00592 return; 00593 } 00594 00595 unset($ws_ara); 00596 00597 /* Register no CRUD for the public user, for the dataset's ID */ 00598 00599 $ws_ara = 00600 new AuthRegistrarAccess($this->globalPermissions, $this->webservices, $this->datasetUri, "create", "", "0.0.0.0", 00601 $this->wsf_local_ip); 00602 00603 $ws_ara->pipeline_conneg($this->conneg->getAccept(), $this->conneg->getAcceptCharset(), 00604 $this->conneg->getAcceptEncoding(), $this->conneg->getAcceptLanguage()); 00605 00606 $ws_ara->process(); 00607 00608 if($ws_ara->pipeline_getResponseHeaderStatus() != 200) 00609 { 00610 $this->conneg->setStatus($ws_ara->pipeline_getResponseHeaderStatus()); 00611 $this->conneg->setStatusMsg($ws_ara->pipeline_getResponseHeaderStatusMsg()); 00612 $this->conneg->setStatusMsgExt($ws_ara->pipeline_getResponseHeaderStatusMsgExt()); 00613 $this->conneg->setError($ws_ara->pipeline_getError()->id, $ws_ara->pipeline_getError()->webservice, 00614 $ws_ara->pipeline_getError()->name, $ws_ara->pipeline_getError()->description, 00615 $ws_ara->pipeline_getError()->debugInfo, $ws_ara->pipeline_getError()->level); 00616 00617 return; 00618 } 00619 00620 unset($ws_ara); 00621 } 00622 } 00623 } 00624 00626 00627 ?>
