Scones.php
Go to the documentation of this file.
00001 <?php 00002 00005 00026 class Sparql extends WebService 00027 { 00029 private $conneg; 00030 00032 private $dtdURL; 00033 00035 private $document = ""; 00036 00038 private $docmime = ""; 00039 00042 private $application = ""; 00043 00045 private $requester_ip = ""; 00046 00048 private $registered_ip = ""; 00049 00051 private $config_ini; 00052 00054 private $SconesSession; 00055 00057 private $annotatedDocument = ""; 00058 00060 private $namespaces; 00061 00063 public static $supportedSerializations = array ("text/xml", "text/*", "*/xml", "*/*"); 00064 00066 private $errorMessenger = 00067 '{ 00068 "ws": "/ws/scones/", 00069 "_200": { 00070 "id": "WS-SCONES-200", 00071 "level": "Warning", 00072 "name": "No documents URI specified for this request", 00073 "description": "No documents URI specified for this request" 00074 }, 00075 "_201": { 00076 "id": "WS-SCONES-201", 00077 "level": "Error", 00078 "name": "Scones is not configured.", 00079 "description": "Ask the system administrator to configure Scones" 00080 }, 00081 "_202": { 00082 "id": "WS-SCONES-202", 00083 "level": "Error", 00084 "name": "Scones is not initialized.", 00085 "description": "Ask the system administrator to initialize Scones" 00086 }, 00087 "_203": { 00088 "id": "WS-SCONES-203", 00089 "level": "Warning", 00090 "name": "Scones is being initialized.", 00091 "description": "Wait a minute and send your query again" 00092 }, 00093 "_300": { 00094 "id": "WS-SCONES-300", 00095 "level": "Warning", 00096 "name": "Document MIME type not supported.", 00097 "description": "The MIME type of the document you feeded to Scones is not currently supported" 00098 }, 00099 "_301": { 00100 "id": "WS-SCONES-301", 00101 "level": "Warning", 00102 "name": "Document empty", 00103 "description": "The content of the document you defined is empty" 00104 } 00105 00106 }'; 00107 00108 00127 function __construct($document, $docmime, $application, $registered_ip, $requester_ip) 00128 { 00129 parent::__construct(); 00130 00131 $this->document = $document; 00132 $this->docmime = $docmime; 00133 $this->application = $application; 00134 $this->requester_ip = $requester_ip; 00135 00136 if($registered_ip == "") 00137 { 00138 $this->registered_ip = $requester_ip; 00139 } 00140 else 00141 { 00142 $this->registered_ip = $registered_ip; 00143 } 00144 00145 if(strtolower(substr($this->registered_ip, 0, 4)) == "self") 00146 { 00147 $pos = strpos($this->registered_ip, "::"); 00148 00149 if($pos !== FALSE) 00150 { 00151 $account = substr($this->registered_ip, $pos + 2, strlen($this->registered_ip) - ($pos + 2)); 00152 00153 $this->registered_ip = $requester_ip . "::" . $account; 00154 } 00155 else 00156 { 00157 $this->registered_ip = $requester_ip; 00158 } 00159 } 00160 00161 $this->uri = $this->wsf_base_url . "/wsf/ws/scones/"; 00162 $this->title = "Scones Web Service"; 00163 $this->crud_usage = new CrudUsage(FALSE, TRUE, FALSE, FALSE); 00164 $this->endpoint = $this->wsf_base_url . "/ws/scones/"; 00165 00166 $this->dtdURL = "sparql/sparql.dtd"; 00167 00168 $this->errorMessenger = json_decode($this->errorMessenger); 00169 } 00170 00171 function __destruct() 00172 { 00173 parent::__destruct(); 00174 00175 if(isset($this->db)) 00176 { 00177 @$this->db->close(); 00178 } 00179 } 00180 00193 protected function validateQuery() 00194 { 00195 if($this->docmime != "text/plain") 00196 { 00197 $this->conneg->setStatus(400); 00198 $this->conneg->setStatusMsg("Bad Request"); 00199 $this->conneg->setStatusMsgExt($this->errorMessenger->_300->name); 00200 $this->conneg->setError($this->errorMessenger->_300->id, $this->errorMessenger->ws, 00201 $this->errorMessenger->_300->name, $this->errorMessenger->_300->description, "", 00202 $this->errorMessenger->_300->level); 00203 00204 return; 00205 } 00206 if($this->document == "") 00207 { 00208 $this->conneg->setStatus(400); 00209 $this->conneg->setStatusMsg("Bad Request"); 00210 $this->conneg->setStatusMsgExt($this->errorMessenger->_301->name); 00211 $this->conneg->setError($this->errorMessenger->_301->id, $this->errorMessenger->ws, 00212 $this->errorMessenger->_301->name, $this->errorMessenger->_301->description, "", 00213 $this->errorMessenger->_301->level); 00214 00215 return; 00216 } 00217 } 00218 00229 public function pipeline_getError() { return ($this->conneg->error); } 00230 00231 00242 public function pipeline_getResultset() 00243 { 00244 // Returns the annotated GATE XML document 00245 return($this->annotatedDocument); 00246 } 00247 00260 public function injectDoctype($xmlDoc) 00261 { 00262 $posHeader = strpos($xmlDoc, '"?>') + 3; 00263 $xmlDoc = substr($xmlDoc, 0, $posHeader) 00264 . "\n<!DOCTYPE resultset PUBLIC \"-//Structured Dynamics LLC//SCONES DTD 0.1//EN\" \"" . $this->dtdBaseURL 00265 . $this->dtdURL . "\">" . substr($xmlDoc, $posHeader, strlen($xmlDoc) - $posHeader); 00266 00267 return ($xmlDoc); 00268 } 00269 00288 public function ws_conneg($accept, $accept_charset, $accept_encoding, $accept_language) 00289 { 00290 $this->conneg = 00291 new Conneg($accept, $accept_charset, $accept_encoding, $accept_language, Sparql::$supportedSerializations); 00292 00293 // If the query is still valid 00294 if($this->conneg->getStatus() == 200) 00295 { 00296 // Validate query 00297 $this->validateQuery(); 00298 } 00299 00300 // If the query is still valid 00301 if($this->conneg->getStatus() == 200) 00302 { 00303 /* 00304 Get the pool of stories to process 00305 Can be a URL or a file reference. 00306 */ 00307 $this->config_ini = parse_ini_file("config.ini", TRUE); 00308 00309 /* 00310 Load the namespaces 00311 */ 00312 $this->namespaces = parse_ini_file("namespaces.ini", TRUE); 00313 00314 // Make sure the service if configured 00315 if($this->config_ini === FALSE) 00316 { 00317 $this->conneg->setStatus(400); 00318 $this->conneg->setStatusMsg("Bad Request"); 00319 $this->conneg->setStatusMsgExt($this->errorMessenger->_201->name); 00320 $this->conneg->setError($this->errorMessenger->_201->id, $this->errorMessenger->ws, 00321 $this->errorMessenger->_201->name, $this->errorMessenger->_201->description, "", 00322 $this->errorMessenger->_201->level); 00323 00324 return; 00325 } 00326 00327 // Starts the GATE process/bridge 00328 require_once($this->config_ini["gate"]["gateBridgeURI"]); 00329 00330 // Create a Scones session where we will save the Gate objects (started & loaded Gate application). 00331 // Second param "false" => we re-use the pre-created session without destroying the previous one 00332 // third param "0" => it nevers timeout. 00333 $this->SconesSession = java_session($this->config_ini["gate"]["sessionName"], false, 0); 00334 00335 if(is_null(java_values($this->SconesSession->get("initialized")))) 00336 { 00337 /* 00338 If the "initialized" session variable is null, it means that the Scone threads 00339 are not initialized, and that they is no current in initialization. 00340 */ 00341 00342 $this->conneg->setStatus(400); 00343 $this->conneg->setStatusMsg("Bad Request"); 00344 $this->conneg->setStatusMsgExt($this->errorMessenger->_202->name); 00345 $this->conneg->setError($this->errorMessenger->_202->id, $this->errorMessenger->ws, 00346 $this->errorMessenger->_202->name, $this->errorMessenger->_202->description, "", 00347 $this->errorMessenger->_202->level); 00348 } 00349 00350 if(java_values($this->SconesSession->get("initialized")) === FALSE) 00351 { 00352 /* 00353 If the "initialized" session variable is FALSE, it means that the Scone threads 00354 are being initialized. 00355 */ 00356 00357 $this->conneg->setStatus(400); 00358 $this->conneg->setStatusMsg("Bad Request"); 00359 $this->conneg->setStatusMsgExt($this->errorMessenger->_203->name); 00360 $this->conneg->setError($this->errorMessenger->_203->id, $this->errorMessenger->ws, 00361 $this->errorMessenger->_203->name, $this->errorMessenger->_203->description, "", 00362 $this->errorMessenger->_203->level); 00363 } 00364 } 00365 } 00366 00385 public function pipeline_conneg($accept, $accept_charset, $accept_encoding, $accept_language) 00386 { $this->ws_conneg($accept, $accept_charset, $accept_encoding, $accept_language); } 00387 00398 public function pipeline_getResponseHeaderStatus() { return $this->conneg->getStatus(); } 00399 00410 public function pipeline_getResponseHeaderStatusMsg() { return $this->conneg->getStatusMsg(); } 00411 00424 public function pipeline_getResponseHeaderStatusMsgExt() { return $this->conneg->getStatusMsgExt(); } 00425 00436 public function pipeline_serialize() 00437 { 00438 return $this->pipeline_getResultset(); 00439 } 00440 00449 public function pipeline_serialize_reification() { } 00450 00461 public function ws_serialize() 00462 { 00463 switch($this->conneg->getMime()) 00464 { 00465 case "text/xml": 00466 return $this->pipeline_serialize(); 00467 break; 00468 00469 default: 00470 return $this->pipeline_getResultset(); 00471 break; 00472 } 00473 } 00474 00487 public function ws_respond($content) 00488 { 00489 // First send the header of the request 00490 $this->conneg->respond(); 00491 00492 // second, send the content of the request 00493 00494 // Make sure there is no error. 00495 if($this->conneg->getStatus() == 200) 00496 { 00497 echo $content; 00498 } 00499 00500 $this->__destruct(); 00501 } 00502 00503 00512 public function process() 00513 { 00514 // Make sure there was no conneg error prior to this process call 00515 if($this->conneg->getStatus() == 200) 00516 { 00517 // Check which instance is available right now 00518 $processed = FALSE; 00519 00520 // Get accessible sessions (threads) from the running Scones instance 00521 while($processed === FALSE) // Continue until we get a free running thread 00522 { 00523 for($i = 1; $i <= $this->config_ini["gate"]["nbSessions"]; $i++) 00524 { 00525 // Make sure the issued is not currently used by another user/process 00526 if(java_values($this->SconesSession->get("session".$i."_used")) === FALSE) 00527 { 00528 $this->SconesSession->put("session".$i."_used", TRUE); 00529 00530 // Process the incoming article 00531 $corpus = $this->SconesSession->get("session".$i."_instance")->getCorpus(); 00532 00533 // Create the content of a document 00534 $documentContent = new java("gate.corpora.DocumentContentImpl", $this->document); 00535 00536 // Create the document to process 00537 $document = new java("gate.corpora.DocumentImpl"); 00538 00539 // Add the document content to the document 00540 $document->setContent($documentContent); 00541 00542 // Create the corpus 00543 $corpus = new java("gate.corpora.CorpusImpl"); 00544 00545 // Add the document to the corpus 00546 $corpus->add($document); 00547 00548 // Add the corpus to the corpus controler (the application) 00549 $this->SconesSession->get("session".$i."_instance")->setCorpus($corpus); 00550 00551 // Execute the pipeline 00552 $this->SconesSession->get("session".$i."_instance")->execute(); 00553 00554 // output the XML document 00555 $this->annotatedDocument = $document->toXML(); 00556 00557 // Empty the corpus 00558 $corpus->clear(); 00559 00560 // Stop the thread seeking process 00561 $processed = TRUE; 00562 00563 // Liberate the thread for others to use 00564 $this->SconesSession->put("session".$i."_used", FALSE); 00565 00566 // Fix namespaces of the type of the tagged named entities 00567 $this->fixNamedEntitiesNamespaces(); 00568 00569 break; 00570 } 00571 } 00572 00573 sleep(1); 00574 } 00575 } 00576 } 00577 00586 function fixNamedEntitiesNamespaces() 00587 { 00588 $annotatedNeXML = new SimpleXMLElement($this->annotatedDocument); 00589 00590 foreach($annotatedNeXML->xpath('//AnnotationSet') as $annotationSet) 00591 { 00592 if((string) $annotationSet['Name'] == $this->config_ini["gate"]["neAnnotationSetName"]) 00593 { 00594 foreach($annotationSet->Annotation as $annotation) 00595 { 00596 foreach($annotation->Feature as $feature) 00597 { 00598 if((string) $feature->Name == "majorType") 00599 { 00600 $feature->Value = $this->prefixToUri((string) $feature->Value); 00601 } 00602 } 00603 } 00604 } 00605 } 00606 00607 $this->annotatedDocument = $annotatedNeXML->asXML(); 00608 } 00609 00618 function prefixToUri($uri) 00619 { 00620 $pos = strpos($uri, "_"); 00621 00622 if($pos !== FALSE) 00623 { 00624 $prefix = substr($uri, 0, $pos); 00625 00626 if(isset($this->namespaces[$prefix])) 00627 { 00628 return(str_replace($prefix."_", $this->namespaces[$prefix], $uri)); 00629 } 00630 else 00631 { 00632 return($uri); 00633 } 00634 } 00635 00636 return($uri); 00637 } 00638 } 00639 00640 00642 00643 ?>
