Пример #1
0
 /**
  * Do the actual search and return results
  *
  * @param Query $search  search object
  * @param int $start     [optional] starting record number
  * @param int $max       [optional] max records
  * @param string $sort   [optional] sort order
  * @param bool $facets   [optional] whether to include facets
  *
  * @return Results
  */
 protected function doSearch(Search\Query $search, $start = 1, $max = 10, $sort = "", $facets = true)
 {
     $results = new Search\ResultSet($this->config);
     $query = $search->toQuery();
     $this->url = $this->server . "/search?q=" . urlencode($query);
     if ($this->config->getConfig('client')) {
         $this->url .= '&client=' . urlencode($this->config->getConfig('client'));
     }
     if ($this->config->getConfig('site')) {
         $this->url .= '&site=' . urlencode($this->config->getConfig('site'));
     }
     $this->url .= '&output=xml';
     // google results are 0-based
     if ($start != null) {
         $start = $start - 1;
         $this->url .= '&start=' . $start;
     }
     // echo $this->url; exit;
     $client = Factory::getHttpClient();
     $google_results = $client->getUrl($this->url, 3);
     $xml = simplexml_load_string($google_results);
     // header("Content-type: text/xml"); echo $xml->saveXML(); exit;
     $x = 0;
     // exact matches
     foreach ($xml->GM as $gm) {
         $record = new Record();
         $record->loadXML($gm);
         $results->addRecord($record);
     }
     // regular results
     $results_array = $xml->xpath("//RES");
     if (count($results_array) > 0 && $results_array !== false) {
         $results_xml = $results_array[0];
         $results->total = (int) $results_xml->M;
         foreach ($results_xml->R as $result_xml) {
             if ($x >= $max) {
                 break;
             }
             $record = new Record();
             $record->loadXML($result_xml);
             $results->addRecord($record);
             $x++;
         }
     }
     return $results;
 }
Пример #2
0
 /**
  * Parse the solr response
  *
  * @param string $response
  * @return ResultSet
  */
 public function parseResponse($response)
 {
     // header('Content-type: text/xml'); echo $response; echo '<!--' . $this->url . '-->'; exit;
     $xml = simplexml_load_string($response);
     if ($response == null || $xml === false) {
         throw new \Exception("Could not connect to search engine.");
     }
     // parse the results
     $results = new Search\ResultSet($this->config);
     // extract total
     $results->total = (int) $xml->result["numFound"];
     // extract records
     foreach ($this->extractRecords($xml) as $record) {
         $results->addRecord($record);
     }
     // extract facets
     $facets = $this->extractFacets($xml);
     // associate facets excluded in the query with the
     // multi-select facets returned in the response
     foreach ($facets->getGroups() as $group) {
         foreach ($this->query->getLimits() as $matching_limit) {
             if ($matching_limit->boolean == 'NOT' && $matching_limit->field == $group->name) {
                 $facet_values = $matching_limit->value;
                 if (!is_array($facet_values)) {
                     $facet_values = array($facet_values);
                 }
                 foreach ($group->getFacets() as $facet) {
                     foreach ($facet_values as $facet_value) {
                         if ($facet->name == $facet_value) {
                             $facet->is_excluded = true;
                         }
                     }
                 }
             }
         }
     }
     $results->setFacets($facets);
     return $results;
 }
Пример #3
0
 protected function parseResponse(\DOMDocument $xml)
 {
     // create results
     $results = new Search\ResultSet($this->config);
     // extract total
     $results->total = $this->worldcat_client->getTotal();
     // extract records
     foreach ($this->extractRecords($xml) as $record) {
         $results->addRecord($record);
     }
     return $results;
 }
Пример #4
0
 /**
  * Do the actual search
  * 
  * @param mixed $search							search object or string
  * @param string $database						[optional] database id
  * @param int $start							[optional] starting record number
  * @param int $max								[optional] max records
  * @param string $sort							[optional] sort order
  * 
  * @return Results
  */
 protected function doSearch($search, $database, $start, $max, $sort = "relevance")
 {
     // default for sort
     if ($sort == "") {
         $sort = "relevance";
     }
     // prepare the query
     $query = "";
     if ($search instanceof Search\Query) {
         $query = $search->toQuery();
     } else {
         $query = $search;
     }
     // databases
     $databases = array();
     // we asked for this database specifically
     if ($database != "") {
         $databases = array($database);
     } else {
         // see if any supplied as facet limit
         foreach ($search->getLimits(true) as $limit) {
             array_push($databases, $limit->value);
         }
         // nope
         if (count($databases) == 0) {
             // get 'em from config
             $databases_xml = $this->config->getConfig("EBSCO_DATABASES");
             if ($databases_xml == "") {
                 throw new \Exception("No databases defined");
             }
             foreach ($databases_xml->database as $database) {
                 array_push($databases, (string) $database["id"]);
             }
         }
     }
     // construct url
     $this->url = "http://eit.ebscohost.com/Services/SearchService.asmx/Search?" . "prof=" . $this->username . "&pwd=" . $this->password . "&authType=&ipprof=" . "&query=" . urlencode($query) . "&startrec={$start}&numrec={$max}" . "&sort={$sort}" . "&format=detailed";
     // add in the databases
     foreach ($databases as $database) {
         $this->url .= "&db={$database}";
     }
     // get the xml from ebsco
     $client = Factory::getHttpClient();
     $client->setUri($this->url);
     $response = $client->send()->getBody();
     // testing
     // echo "<pre>$this->url<hr>$response</pre>"; exit;
     if ($response == null) {
         throw new \Exception("Could not connect to Ebsco search server");
     }
     // load it in
     $xml = new \DOMDocument();
     $xml->recover = true;
     $xml->loadXML($response);
     // result set
     $results = new Search\ResultSet($this->config);
     // get total
     $total = 0;
     $hits = $xml->getElementsByTagName("Hits")->item(0);
     if ($hits != null) {
         $total = (int) $hits->nodeValue;
     }
     ### hacks until ebsco gives us proper hit counts, they are almost there
     $check = 0;
     foreach ($xml->getElementsByTagName("rec") as $hits) {
         $check++;
     }
     // no hits, but we're above the first page, so the user has likely
     // skipped here, need to increment down until we find the true ceiling
     if ($check == 0 && $start > $max) {
         // but let's not get crazy here
         if ($this->deincrementing <= 8) {
             $this->deincrementing++;
             $new_start = $start - $max;
             return $this->doSearch($query, $databases, $new_start, $max, $sort);
         }
     }
     // we've reached the end prematurely, so set this to the end
     $check_end = $start + $check;
     if ($check < $max) {
         if ($check_end < $total) {
             $total = $check_end;
         }
     }
     ## end hacks
     // set total
     $results->total = $total;
     // add records
     foreach ($this->extractRecords($xml) as $record) {
         $results->addRecord($record);
     }
     // add clusters
     $facets = $this->extractFacets($xml);
     $results->setFacets($facets);
     return $results;
 }
Пример #5
0
 /**
  * Parse the primo response
  *
  * @param DOMDocument $xml	primo results
  * @return ResultSet
  */
 protected function parseResponse(\DOMDocument $xml)
 {
     // check for errors
     $error = $xml->getElementsByTagName("ERROR")->item(0);
     if ($error != "") {
         throw new \Exception($error->getAttribute("MESSAGE"));
     }
     // set-up the result set
     $result_set = new Search\ResultSet($this->config);
     // total
     $docset = $xml->getElementsByTagName("DOCSET")->item(0);
     if ($docset == null) {
         throw new \Exception("Could not determine total number of records");
     }
     $total = $docset->getAttribute("TOTALHITS");
     $result_set->total = $total;
     // extract records
     foreach ($this->extractRecords($xml) as $xerxes_record) {
         $result_set->addRecord($xerxes_record);
     }
     // facets
     $facets = $this->extractFacets($xml);
     $result_set->setFacets($facets);
     return $result_set;
 }
Пример #6
0
 /**
  * Parse the EDS response
  *
  * @param string $response
  * @return ResultSet
  */
 public function parseResponse($response)
 {
     $json = new Json($response);
     // results
     $result_set = new ResultSet($this->config);
     // total
     $total = $json->extractValue('SearchResult/Statistics/TotalHits');
     $result_set->total = $total;
     // extract records
     foreach ($this->extractRecords($json) as $xerxes_record) {
         $result_set->addRecord($xerxes_record);
     }
     // extract facets
     $facets = $this->extractFacets($json);
     $result_set->setFacets($facets);
     return $result_set;
 }
Пример #7
0
 /**
  * Do the actual search
  * 
  * @param string|Query $search					search object or string
  * @param int $start							[optional] starting record number
  * @param int $max								[optional] max records
  * @param string $sort							[optional] sort order
  * @param bool $include_facets					[optional] whether to include facets or not
  * 
  * @return string
  */
 protected function doSearch($search, $start, $max = 10, $sort = null, $include_facets = true)
 {
     // start
     if ($start > 0) {
         $start--;
         // solr is 0-based
     }
     ### parse the query
     $query = "";
     // passed in a query object, so handle this
     if ($search instanceof Search\Query) {
         $query = $search->toQuery();
     } else {
         $query = "&q=" . urlencode($search);
     }
     ### now the url
     $this->url = $this->server . $query;
     $this->url .= "&start={$start}&rows=" . $max . "&sort=" . urlencode($sort);
     if ($include_facets == true) {
         $this->url .= "&facet=true&facet.mincount=1";
         foreach ($this->config->getFacets() as $facet => $attributes) {
             $sort = (string) $attributes["sort"];
             $max = (string) $attributes["max"];
             $this->url .= "&facet.field={$facet}";
             if ($sort != "") {
                 $this->url .= "&f.{$facet}.facet.sort={$sort}";
             }
             if ($max != "") {
                 $this->url .= "&f.{$facet}.facet.limit={$max}";
             }
         }
     }
     // make sure we get the score
     $this->url .= "&fl=*+score";
     ## get and parse the response
     // get the data
     $client = Factory::getHttpClient();
     $client->setUri($this->url);
     $response = $client->send()->getBody();
     $xml = simplexml_load_string($response);
     if ($response == null || $xml === false) {
         throw new \Exception("Could not connect to search engine.");
     }
     // parse the results
     $results = new Search\ResultSet($this->config);
     // extract total
     $results->total = (int) $xml->result["numFound"];
     // extract records
     foreach ($this->extractRecords($xml) as $record) {
         $results->addRecord($record);
     }
     // extract facets
     $results->setFacets($this->extractFacets($xml));
     return $results;
 }
Пример #8
0
 /**
  * Parse the ebsco response
  *
  * @param string $response
  * @return ResultSet
  */
 public function parseResponse($response)
 {
     // load it in
     $xml = Parser::convertToDOMDocument($response);
     // catch a non-xml response
     if ($xml->documentElement == null) {
         throw new \Exception("Could not connect to Ebsco search server");
     }
     // check for fatal error
     if ($xml->documentElement->nodeName == 'Fault') {
         $message = $xml->getElementsByTagName('Message')->item(0);
         if ($message != null) {
             if ($message->nodeValue == "The following parameter(s) have incorrect values: Field query: Greater than 0") {
                 throw new \Exception('Ebsco search error: your search query cannot be empty');
             }
             // need to get a fresh set of databases
             if (strstr($message->nodeValue, 'User does not have access rights to database')) {
                 throw new DatabaseException($message->nodeValue);
             }
             throw new \Exception('Ebsco server error: ' . $message->nodeValue);
         }
     }
     // result set
     $results = new Search\ResultSet($this->config);
     // get total
     $total = 0;
     $hits = $xml->getElementsByTagName("Hits")->item(0);
     if ($hits != null) {
         $total = (int) $hits->nodeValue;
     }
     ### hacks until ebsco gives us proper hit counts, they are almost there
     $check = 0;
     foreach ($xml->getElementsByTagName("rec") as $hits) {
         $check++;
     }
     // no hits, but we're above the first page, so the user has likely
     // skipped here, need to increment down until we find the true ceiling
     if ($check == 0 && $this->query->start > $this->query->max) {
         // but let's not get crazy here
         if ($this->deincrementing <= 8) {
             $this->deincrementing++;
             $new_start = $this->query->start - $this->query->max;
             $this->query->start = $new_start;
             return $this->doSearch($this->query);
         }
     }
     // we've reached the end prematurely, so set this to the end
     $check_end = $this->query->start + $check;
     if ($check < $this->query->max) {
         if ($check_end < $total) {
             $total = $check_end;
         }
     }
     ## end hacks
     // set total
     $results->total = $total;
     // add records
     foreach ($this->extractRecords($xml) as $record) {
         $results->addRecord($record);
     }
     // add clusters
     $facets = $this->extractFacets($xml);
     $results->setFacets($facets);
     /*
     
     $facets_id = 'facets' . $query->getHash();
     
     // cached clusters
     
     $cached_facets = $this->cache->get($facets_id);
     
     if ( $cached_facets instanceof Facets )
     {
     	$results->setFacets($cached_facets);
     }
     else
     {
     	$facets = $this->extractFacets($xml);
     	$this->cache->set($facets_id, $facets);
     	$results->setFacets($facets);
     }
     */
     return $results;
 }
Пример #9
0
 /**
  * Parse the primo response
  *
  * @param string $response
  * @return ResultSet
  */
 public function parseResponse($response)
 {
     // load it
     $xml = Parser::convertToDOMDocument($response);
     // header("Content-type:text/xml"); echo $xml->saveXML(); exit;
     // check for errors
     $error = $xml->getElementsByTagName("ERROR")->item(0);
     if ($error != "") {
         throw new \Exception($error->getAttribute("MESSAGE"));
     }
     // set-up the result set
     $result_set = new Search\ResultSet($this->config);
     // total
     $docset = $xml->getElementsByTagName("DOCSET")->item(0);
     if ($docset == null) {
         throw new \Exception("Could not determine total number of records");
     }
     $total = $docset->getAttribute("TOTALHITS");
     $result_set->total = $total;
     // extract records
     foreach ($this->extractRecords($xml) as $xerxes_record) {
         $result_set->addRecord($xerxes_record);
     }
     // facets
     $facets = $this->extractFacets($xml);
     $result_set->setFacets($facets);
     return $result_set;
 }