00001 <?php
00012 class SDRSearchService extends XSI {
00013 private $lastResponse = null;
00014 private $lastRequest = null;
00015
00016 private $defaultOptions = array();
00017
00018
00019 public static $WITH_ANCESTRY = false;
00020
00030 public function setDefault($which, $what) {
00031 $this->defaultOptions[$which] = $what;
00032 }
00033
00042 public function getDefault($which) {
00043 if(isset($this->defaultOptions[$which]))
00044 return $this->defaultOptions[$which];
00045 else
00046 return null;
00047 }
00048
00057 private function mergeDefaults($options) {
00058 return array_merge($this->defaultOptions, $options);
00059 }
00060
00071 private function processStandardOptions($req, $options, $type = SDRContract::REQUEST_TYPE_ADVANCED) {
00072 $req->setMethod(SDRContract::METHOD_FIND);
00073 $req->setType($type);
00074 if(isset($options['projectFilter']))
00075 $req->setProjectFilter($options['projectFilter']);
00076 if(isset($options['startingResult']))
00077 $req->setStartingResult($options['startingResult']);
00078 if(isset($options['numResultsPerPage']))
00079 $req->setNumResultsPerPage($options['numResultsPerPage']);
00080 }
00081
00091 private function processDataOptions($req, $options) {
00092 if(isset($options['extra'])) {
00093 if(in_array('metadata', $options['extra']))
00094 $req->addDataSection(array(SDRContract::DATA_METADATA));
00095 if(in_array('relationships', $options['extra']))
00096 $req->addDataSection(array(SDRContract::DATA_RELATIONSHIPS));
00097 if(in_array('alignments', $options['extra']))
00098 $req->addDataSection(array(SDRContract::DATA_ALIGNMENTS));
00099 }
00100 if(isset($options['modeFilter']))
00101 $req->addConstraints(array('modeFilter' => $options['modeFilter']));
00102 if(isset($options['alignmentMode']))
00103 $req->addConstraints(array('alignmentMode' => $options['alignmentMode']));
00104 if(isset($options['state']))
00105 $req->setState($options['state']);
00106 }
00107
00116 private function loadXML($xml) {
00117 $oldLUIE = libxml_use_internal_errors(true);
00118 $xml = @simplexml_load_string(utf8_encode($xml));
00119 $errors = libxml_get_errors();
00120 libxml_use_internal_errors($oldLUIE);
00121 if(!$xml || count($errors) > 0) {
00122 $html = "<ul>";
00123 foreach($errors as $e) {
00124 $html .= "<li>On line <b>$e->line</b>: $e->message</li>";
00125 }
00126 $html .= "</ul>";
00127 $this->setError($html);
00128 return false;
00129 }
00130 return $xml;
00131 }
00132
00143 public function query($request) {
00144 $url = $request->buildQueryURL();
00145 $XMLData = $request->getXMLData();
00146 $key = $request->getKey();
00147 $result = self::queryURL($url, $XMLData, $key);
00148 return utf8_encode($result);
00149 }
00150
00163 public function getResource($id, $options = array()) {
00164 STimer::start('getResource');
00165 $options = $this->mergeDefaults($options);
00166 $req = new SDRServiceRequest();
00167 $this->processStandardOptions($req, $options);
00168 $this->processDataOptions($req, $options);
00169 $req->addConstraints(array('cserdId' => $id));
00170 $this->lastRequest = $req;
00171
00172 $results = $this->query($req);
00173 if(isset($options['asXML']) && $options['asXML']) {
00174 $xml = $this->loadXML($results);
00175 if(!$xml)
00176 return false;
00177 $this->lastResponse = $xml;
00178 return $this->lastResponse->SearchResults->resources[0];
00179 }
00180
00181 if(isset($options['withAncestry']) && $options['withAncestry'])
00182 self::$WITH_ANCESTRY = true;
00183 else
00184 self::$WITH_ANCESTRY = false;
00185
00186 $response = new SDRResourceResponse($results, $req);
00187
00188 $this->lastResponse = $response;
00189
00190 if(count($response->SearchResults->resources) == 0)
00191 return null;
00192
00193 STimer::end('getResource');
00194 return $response->SearchResults->resources[0];
00195 }
00196
00197
00207 public function getResourceResponse($request) {
00208 return (new SDRResourceResponse(self::query($request), $request));
00209 }
00210
00221 public function getByMetadata($constraints, $options = array()) {
00222 STimer::start('getByMetadata');
00223 $options = $this->mergeDefaults($options);
00224 $req = new SDRServiceRequest();
00225 $this->processStandardOptions($req, $options);
00226 $this->processDataOptions($req, $options);
00227 $req->addConstraints($constraints);
00228 $this->lastRequest = $req;
00229
00230 $results = $this->query($req);
00231 if(isset($options['asXML']) && $options['asXML']) {
00232 $xml = $this->loadXML($results);
00233 if(!$xml)
00234 return false;
00235 $this->lastResponse = $xml;
00236 return $this->lastResponse->SearchResults->SDRResource;
00237 }
00238
00239 if(isset($options['withAncestry']) && $options['withAncestry'])
00240 self::$WITH_ANCESTRY = true;
00241 else
00242 self::$WITH_ANCESTRY = false;
00243
00244 if ($results == false)
00245 return false;
00246 $response = new SDRResourceResponse($results, $req);
00247
00248 $this->lastResponse = $response;
00249
00250 STimer::end('getByMetadata');
00251 return $response->SearchResults->resources;
00252 }
00253
00254
00255
00266 public function getAlignedTo($nodeType, $nodeId, $options = array()) {
00267 STimer::start('getAlignedTo');
00268 $options = $this->mergeDefaults($options);
00269 $req = new SDRServiceRequest();
00270 $this->processStandardOptions($req, $options);
00271 $this->processDataOptions($req, $options);
00272 $req->addConstraints(array('alignedTo' => "$nodeType,$nodeId"));
00273 $this->lastRequest = $req;
00274
00275 $results = $this->query($req);
00276 if(isset($options['asXML']) && $options['asXML']) {
00277 $xml = $this->loadXML($results);
00278 if(!$xml)
00279 return false;
00280 $this->lastResponse = $xml;
00281 return $this->lastResponse->SearchResults->SDRResource;
00282 }
00283
00284 if(isset($options['withAncestry']) && $options['withAncestry'])
00285 self::$WITH_ANCESTRY = true;
00286 else
00287 self::$WITH_ANCESTRY = false;
00288
00289 $response = new SDRResourceResponse($results, $req);
00290
00291 $this->lastResponse = $response;
00292
00293 STimer::end('getAlignedTo');
00294 return $response->SearchResults->resources;
00295 }
00296
00306 public function getRelatedTo($cserdId, $options = array()) {
00307 STimer::start('getRelatedTo');
00308 $options = $this->mergeDefaults($options);
00309 $req = new SDRServiceRequest();
00310 $this->processStandardOptions($req, $options);
00311 $this->processDataOptions($req, $options);
00312 $req->addConstraints(array('relatedTo' => $cserdId));
00313 $this->lastRequest = $req;
00314
00315 $results = $this->query($req);
00316 if(isset($options['asXML']) && $options['asXML']) {
00317 $xml = $this->loadXML($results);
00318 if(!$xml)
00319 return false;
00320 $this->lastResponse = $xml;
00321 return $this->lastResponse->SearchResults->SDRResource;
00322 }
00323
00324 if(isset($options['withAncestry']) && $options['withAncestry'])
00325 self::$WITH_ANCESTRY = true;
00326 else
00327 self::$WITH_ANCESTRY = false;
00328
00329 $response = new SDRResourceResponse($results, $req);
00330
00331 $this->lastResponse = $response;
00332
00333 STimer::end('getRelatedTo');
00334 if($response->SearchResults)
00335 return $response->SearchResults->resources;
00336 else
00337 return false;
00338 }
00339
00340
00341
00351 public function getWithAlignments($types, $options = array()) {
00352 STimer::start('getWithAlignments');
00353 $options = $this->mergeDefaults($options);
00354 $req = new SDRServiceRequest();
00355 $this->processStandardOptions($req, $options);
00356 $this->processDataOptions($req, $options);
00357 $req->addConstraints(array('withAlignments' => implode(',', $types)));
00358 $this->lastRequest = $req;
00359
00360 $results = $this->query($req);
00361 if(isset($options['asXML']) && $options['asXML']) {
00362 $xml = $this->loadXML($results);
00363 if(!$xml)
00364 return false;
00365 $this->lastResponse = $xml;
00366 return $this->lastResponse->SearchResults->SDRResource;
00367 }
00368
00369 if(isset($options['withAncestry']) && $options['withAncestry'])
00370 self::$WITH_ANCESTRY = true;
00371 else
00372 self::$WITH_ANCESTRY = false;
00373
00374 $response = new SDRResourceResponse($results, $req);
00375
00376 $this->lastResponse = $response;
00377
00378 STimer::end('getWithAlignments');
00379 return $response->SearchResults->resources;
00380 }
00381
00382
00383
00392 public function getAlignments($options = array()) {
00393 $this->setError("getAlignments() is not implemented yet; use TSD API instead");
00394 return null;
00395 }
00396
00405 public function getRelationships($options = array()) {
00406 $this->setError("getRelationships() is not implemented yet; use TSD API instead");
00407 return null;
00408 }
00409
00410
00420 public function getVocabulary($field, $options = array()) {
00421 STimer::start('getVocabulary');
00422 $options = $this->mergeDefaults($options);
00423 $req = new SDRServiceRequest();
00424 $req->usePreset(SDRServiceRequest::PRESET_VOCAB, array('field' => $field));
00425 $this->lastRequest = $req;
00426
00427 $results = $this->query($req);
00428 if(isset($options['asXML']) && $options['asXML']) {
00429 $xml = $this->loadXML($results);
00430 if(!$xml)
00431 return false;
00432 $this->lastResponse = $xml;
00433 return $this->lastResponse->SearchResults->values;
00434 }
00435
00436 $response = new SDRVocabResponse($results, $req);
00437
00438 $this->lastRequest = $req;
00439 $this->lastResponse = $response;
00440
00441 STimer::end('getVocabulary');
00442 return $response->SearchResults->values;
00443 }
00444
00452 public function getLastRequest() {
00453 return $this->lastRequest;
00454 }
00455
00463 public function getLastResponse() {
00464 return $this->lastResponse;
00465 }
00466
00477 protected static function queryURL($url, $XMLData = null, $key = null) {
00478 $result = parent::queryURL($url, $XMLData, $key);
00479 $xml = utf8_encode($result);
00480 $xmlobj = @simplexml_load_string($xml);
00481 if (!$xmlobj) {
00482 self::setStaticError('Invalid response from search service URL ' . $url);
00483 return false;
00484 }
00485 if (isset($xmlobj->error)) {
00486 self::setStaticError('Error from server: ' . $xmlobj->error->asXML());
00487 return false;
00488 }
00489 return $result;
00490 }
00491 }
00492 ?>