Example #1
0
 public function logSearch($module, Query $query, ResultSet $results)
 {
     $term = $query->getQueryTerm(0);
     $params = array();
     $params[':ip_address'] = $query->getUser()->getIpAddress();
     $params[':module'] = $module;
     $params[':field'] = $term->field;
     $params[':phrase'] = substr($term->phrase, 0, 999);
     $params[':hits'] = $results->getTotal();
     $sql = 'INSERT INTO xerxes_search_stats ' . '( ip_address, stamp, module, field, phrase, hits ) ' . 'VALUES (:ip_address, NOW(), :module, :field, :phrase, :hits)';
     $this->insert($sql, $params);
 }
Example #2
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)
 {
     $records = $this->reading_list->getRecords();
     $results = new Search\ResultSet($this->config);
     $results->total = count($records);
     // convert them into our model
     foreach ($records as $record) {
         $result = $this->createSearchResult($record);
         $results->addResult($result);
     }
     return $results;
 }
Example #3
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;
 }
Example #4
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;
 }
Example #5
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;
 }
Example #6
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)
 {
     $username = $search->getQueryTerm(0)->phrase;
     $label = $search->getLimit("label");
     $format = $search->getLimit("format");
     $results = new Search\ResultSet($this->config);
     $results->total = $this->datamap->totalRecords($username, $label->value, $format->value);
     // just the hit count please
     if ($max == 0) {
         return $results;
     }
     // no we want actual records too
     $records = array();
     if ($label->value != "") {
         $records = $this->datamap->getRecordsByLabel($username, $label->value, $sort, $start, $max);
     } elseif ($format->value != "") {
         $records = $this->datamap->getRecordsByFormat($username, $format->value, $sort, $start, $max);
     } else {
         $records = $this->datamap->getRecords($username, null, $sort, $start, $max);
     }
     // convert them into our model
     foreach ($records as $record) {
         $result = $this->createSearchResult($record);
         $results->addResult($result);
     }
     // facets
     $facets = new Search\Facets();
     // formats
     $formats = $this->datamap->getFormats($username);
     if (count($formats) > 0) {
         $group = new Search\FacetGroup();
         $group->name = "format";
         $group->public = "Formats";
         // @todo: i18n this?
         foreach ($formats as $format) {
             $facet = new Search\Facet();
             $facet->name = $format->format;
             $facet->count = $format->total;
             $group->addFacet($facet);
         }
         $facets->addGroup($group);
     }
     // labels
     $tags = $this->datamap->getTags($username);
     if (count($tags) > 0) {
         $group = new Search\FacetGroup();
         $group->name = "label";
         $group->public = "Labels";
         // @todo: i18n this?
         foreach ($tags as $tag) {
             $facet = new Search\Facet();
             $facet->name = $tag->label;
             $facet->count = $tag->total;
             $group->addFacet($facet);
         }
         $facets->addGroup($group);
     }
     $results->setFacets($facets);
     return $results;
 }
Example #7
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;
 }
Example #8
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;
 }
Example #9
0
 /**
  * Add links to facets
  * 
  * @param ResultSet $results
  */
 public function addFacetLinks(ResultSet &$results)
 {
     // facets
     $facets = $results->getFacets();
     if ($facets != "") {
         foreach ($facets->getGroups() as $group) {
             foreach ($group->getFacets() as $facet) {
                 // existing url
                 $url = $this->facetParams();
                 // now add the new one
                 if ($facet->key != "") {
                     // key defines a way to pass the (internal) value
                     // in the param, while the name is the display value
                     $url["facet." . $group->name . "." . urlencode($facet->key)] = $facet->name;
                 } else {
                     $url["facet." . $group->name] = $facet->name;
                 }
                 $facet->url = $this->request->url_for($url);
             }
         }
     }
 }
Example #10
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;
 }
Example #11
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;
 }
Example #12
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;
 }
Example #13
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;
 }
Example #14
0
 /**
  * Add links to facets
  * 
  * @param ResultSet $results
  */
 public function addFacetLinks(ResultSet &$results)
 {
     // facets
     $facets = $results->getFacets();
     if ($facets != "") {
         $group_id = 0;
         foreach ($facets->getGroups() as $group) {
             // this is used for javascript selecting
             $group_id++;
             $facet_id = 0;
             // group identifiers
             $group->group_id = 'facet-' . $group_id;
             $group->param_name = 'facet.' . $group->name;
             // link to multi-select facet page
             $group_params = $this->query->getAllSearchParams();
             $group_params['controller'] = $this->request->getParam('controller');
             $group_params['action'] = 'facet';
             $group_params['group'] = $group->param_name;
             $group->url = $this->request->url_for($group_params);
             // print_r($group->getFacets());
             foreach ($group->getFacets() as $facet) {
                 $facet_id++;
                 $param_name = Query::getParamFromParts($group->name, urlencode($facet->key), $facet->is_excluded);
                 // link
                 $url = $this->facetParams();
                 if ($facet->is_excluded == true) {
                     // selecting this option removes our exclude param
                     foreach ($url as $key => $value) {
                         if ($key == $param_name) {
                             // if we have multiple values, only remove the matching one
                             if (is_array($value)) {
                                 $new_array = array();
                                 foreach ($value as $entry) {
                                     if ($entry != $facet->name) {
                                         $new_array[] = $entry;
                                     }
                                 }
                                 $url[$key] = $new_array;
                             } else {
                                 $url[$key] = '';
                             }
                         }
                     }
                 } else {
                     // selecting this option adds our param
                     $url[$param_name] = $facet->name;
                 }
                 $facet->url = $this->request->url_for($url);
                 // facet identifiers
                 $facet->input_id = $group->group_id . '-' . $facet_id;
                 // add the name of the param as well
                 $facet->param_name = $param_name;
                 // see if this facet is selected (for multi-select facets)
                 if ($this->request->hasParamValue($param_name, $facet->name)) {
                     $facet->selected = true;
                 }
                 // exclude facet param
                 $facet->param_exclude = str_replace('facet.', 'facet.remove.', $param_name);
             }
         }
     }
 }