public function selectAction()
 {
     $engine = new Engine();
     $query = new Query();
     $query->addTerm('username', null, 'query', null, $this->request->getSessionData('username'));
     $results = $engine->searchRetrieve($query, 1, 500);
 }
Example #2
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 #3
0
 /** 
  * Extract query, limit and target params from the URL 
  * @return array 
  */
 public function getAllSearchParams()
 {
     $lq = parent::getAllSearchParams();
     $lq['target'] = $this->targetnames;
     //Debug::Dump($lq);
     return $lq;
 }
Example #4
0
 protected function doSearch(Search\Query $search, $start = 1, $max = 10, $sort = "")
 {
     $username = $search->getQueryTerm(0)->phrase;
     $label = $search->getLimit("facet.label");
     $format = $search->getLimit("facet.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 = "Label";
         // @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 #5
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 #6
0
 /**
  * Create a Saved Record Query
  * 
  * @param Request $request
  * @param Config $config
  */
 public function __construct(Request $request = null, Config $config = null)
 {
     $final = parent::__construct($request, $config);
     // add the username as the first query term
     $term = new Search\QueryTerm();
     $term->phrase = $this->request->getSessionData('username');
     array_unshift($this->terms, $term);
 }
Example #7
0
 public function __construct(Request $request = null, Config $config = null)
 {
     parent::__construct($request, $config);
     $uo = new UserOptions($request);
     $tids = $uo->getSessionData('targets');
     $type = $uo->getSessionData('source_type');
     $this->targets = new Targets($type, $tids);
 }
Example #8
0
 /**
  * Search and return results
  * Called from SearchController::resultsAction
  * Required by abstract parent
  *
  * @param Query $search         search object
  * @param int $start            [optional] starting record number
  * @param int $max              [optional] max records
  * @param string $sort          [optional] sort order
  *
  * @return Results
  */
 public function searchRetrieve(Search\Query $search, $start = 0, $max = 100, $sort = "")
 {
     $start = $start - 1;
     // allow for pz2 starting from 0
     $max = $max - 1;
     $results = $this->client->pz2_show($start, $max, $sort);
     $pzt = new Targets(null, array_keys($search->getTargetLocations()));
     $result_set = new MergedResultSet($results, $pzt);
     // fetch facets
     // only if facets should be shown and there are more than facet_min results
     if ($this->conf()->getConfig('FACET_FIELDS', false) == true && $result_set->total > $this->conf()->getConfig('FACET_MIN', false)) {
         $terms = array_keys($this->conf()->getFacets());
         $xml = $this->client->pz2_termlist($terms, $this->conf()->getConfig('FACET_MAX', false));
         $facets = $this->extractFacets($xml);
         $result_set->setFacets($facets);
     }
     return $result_set;
 }
Example #9
0
 /**
  * Create an Ebsco Query
  *
  * @param Request $request
  * @param Config $config
  */
 public function __construct(Request $request = null, Config $config = null)
 {
     parent::__construct($request, $config);
     if ($this->config != null) {
         // server address
         $this->username = $this->config->getConfig("EBSCO_USERNAME");
         $this->password = $this->config->getConfig("EBSCO_PASSWORD");
     }
 }
Example #10
0
 public function getUrl(Query $query)
 {
     $term = $query->getQueryTerm(0);
     $phrase = $term->phrase;
     switch ($term->field) {
         case "title":
             $phrase = "t:({$phrase})";
             break;
         case "subject":
             $phrase = "s:({$phrase})";
             break;
         case "author":
             $phrase = "a:({$phrase})";
             break;
     }
     $url = $this->server . "search/?searchtype=X&searcharg=" . urlencode($phrase);
     return $url;
 }
Example #11
0
 /**
  * Create a Solr Query
  *
  * @param Request $request
  * @param Config $config
  */
 public function __construct(Request $request = null, Config $config = null)
 {
     parent::__construct($request, $config);
     if ($this->config != null) {
         // server address
         $this->server = $this->config->getConfig('SOLR', true);
         $this->server = rtrim($this->server, '/');
         $this->server .= "/select/?version=2.2";
         // limits set in config
         $auto_limit = $this->config->getConfig("LIMIT", false);
         if ($auto_limit != null) {
             $this->server .= "&fq=" . urlencode($auto_limit);
         }
     }
 }
Example #12
0
 /**
  * Create a Primo Query
  *
  * @param Request $request
  * @param Config $config
  */
 public function __construct(Request $request = null, Config $config = null)
 {
     parent::__construct($request, $config);
     // server info
     $this->server = $this->config->getConfig('PRIMO_ADDRESS', true);
     $this->server = rtrim($this->server, '/');
     // institutional id's
     $this->institution = $this->config->getConfig('INSTITUTION', true);
     $this->vid = $this->config->getConfig('VID', false);
     // scope
     $loc = $this->config->getConfig('LOC', false, $this->request->getParam('scope'));
     if ($loc != "") {
         $this->loc = explode(";", $loc);
     }
 }
Example #13
0
 /**
  * Create an EDS Query
  *
  * @param Request $request
  * @param Config $config
  */
 public function __construct(Request $request = null, Config $config = null)
 {
     parent::__construct($request, $config);
     // server address
     $this->server = 'http://eds-api.ebscohost.com/edsapi/rest/';
     $profile = 'edsapi';
     if ($request != null) {
         // set session id
         $this->session_id = $request->getSessionData('ebsco_session');
         if ($this->session_id == "") {
             $this->session_id = $this->createSession($profile);
         }
         $this->request->setSessionData('ebsco_session', $this->session_id);
     }
     $this->headers = array('Accept' => 'application/json', 'x-sessionToken' => $this->session_id);
 }
 /**
  * Check for mispelled terms
  * 
  * @param Query $query
  * @return Suggestion
  */
 protected function checkSpelling()
 {
     // advanced search?  no thanks!
     if ($this->request->getParam('advanced') != null) {
         return new Suggestion();
     }
     $id = $this->query->getHash();
     // have we checked it already?
     $suggestion = $this->request->getSessionData("spelling_{$id}");
     if ($suggestion == null) {
         $suggestion = $this->query->checkSpelling();
         // check it
         $this->request->setSessionData("spelling_{$id}", serialize($suggestion));
         // save for later
     } else {
         $suggestion = unserialize($suggestion);
         // resurrect it, like shane
     }
     return $suggestion;
 }
Example #15
0
 /**
  * Add links to the query object limits
  * 
  * @param Query $query
  */
 public function addQueryLinks(Query $query)
 {
     // we have to pass in the query object here rather than take
     // the property above because adding the links doesn't seem
     // to reflect back in the main object, even though they should
     // be references, maybe because limit objects are in an array?
     // search option links
     $search = $this->registry->getConfig('search');
     if ($search instanceof \SimpleXMLElement) {
         $controller_map = $this->request->getControllerMap();
         foreach ($search->xpath("//option") as $option) {
             // format the number
             // is this the current tab?
             if ($controller_map->getControllerName() == (string) $option["id"] && ($this->request->getParam('source') == (string) $option["source"] || (string) $option["source"] == '')) {
                 $option->addAttribute('current', "1");
             }
             // url
             $params = $query->extractSearchParams();
             $params['controller'] = $controller_map->getUrlAlias((string) $option["id"]);
             $params['action'] = "results";
             $params['source'] = (string) $option["source"];
             $url = $this->request->url_for($params);
             $option->addAttribute('url', $url);
             // cached search hit count?
             foreach ($this->request->getAllSessionData() as $session_id => $session_value) {
                 // does this value in the cache have the save id as our tab?
                 $id = str_replace("_" . $query->getHash(), "", $session_id);
                 if ($id == (string) $option["id"]) {
                     // yup, so add it
                     $option->addAttribute('hits', Parser::number_format($session_value));
                 }
             }
         }
         $this->registry->setConfig('search', $search);
     }
     // links to remove facets
     foreach ($query->getLimits() as $limit) {
         $params = $this->currentParams();
         $params = Parser::removeFromArray($params, $limit->field, $limit->value);
         $limit->remove_url = $this->request->url_for($params);
     }
 }
Example #16
0
 /**
  * Do the actual search
  * 
  * @param Query $search		search object
  * @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\Query $search, $start = 1, $max = 10, $sort = "")
 {
     // limit to local users?
     if ($search->getUser()->isAuthorized()) {
         $this->summon_client->setToAuthenticated();
     }
     // prepare the query
     $query = $search->toQuery();
     // facets to include in the response
     foreach ($this->config->getFacets() as $facet_config) {
         if ($facet_config['type'] == 'date') {
             $this->summon_client->setDateRangesToInclude((string) $facet_config["ranges"]);
         } else {
             $this->summon_client->includeFacet((string) $facet_config["internal"] . ",or,1," . (string) $facet_config["max"]);
         }
     }
     // limit to local holdings unless told otherwise
     if ($this->config->getConfig('LIMIT_TO_HOLDINGS', false)) {
         $this->summon_client->limitToHoldings();
     }
     // limits
     foreach ($search->getLimits(true) as $limit) {
         if ($limit->field == 'newspapers') {
             continue;
             // we'll handle you later
         }
         // holdings only
         if ($limit->field == 'holdings') {
             if ($limit->value == 'false') {
                 // this is actually an expander to search everything
                 $this->summon_client->limitToHoldings(false);
             } else {
                 $this->summon_client->limitToHoldings();
             }
         } elseif ($this->config->getFacetType($limit->field) == 'date') {
             // @todo: make this not 'display'
             if ($limit->value == 'start' && $limit->display != '') {
                 $this->summon_client->setStartDate($limit->display);
             } elseif ($limit->value == 'end' && $limit->display != '') {
                 $this->summon_client->setEndDate($limit->display);
             }
         } else {
             $value = '';
             $boolean = 'false';
             if ($limit->boolean == "NOT") {
                 $boolean = 'true';
             }
             // multi-select filter
             if (is_array($limit->value)) {
                 // exclude
                 if ($boolean == 'true') {
                     foreach ($limit->value as $limited) {
                         $value = str_replace(',', '\\,', $limited);
                         $this->summon_client->addFilter($limit->field . ",{$value},{$boolean}");
                     }
                 } else {
                     foreach ($limit->value as $limited) {
                         $value .= ',' . str_replace(',', '\\,', $limited);
                     }
                     $this->summon_client->addComplexFilter($limit->field . ',' . $boolean . $value);
                 }
             } else {
                 $value = str_replace(',', '\\,', $limit->value);
                 $this->summon_client->addFilter($limit->field . ",{$value},{$boolean}");
             }
         }
     }
     // format filters
     // newspapers are a special case, i.e., they can be optional
     if ($this->config->getConfig('NEWSPAPERS_OPTIONAL', false)) {
         $news_limit = $search->getLimit('facet.newspapers');
         if ($news_limit->value != 'true') {
             $this->formats_exclude[] = 'Newspaper Article';
         }
     }
     // always exclude these
     foreach ($this->formats_exclude as $format) {
         $this->summon_client->addFilter("ContentType,{$format},true");
     }
     // summon deals in pages, not start record number
     if ($max > 0) {
         $page = ceil($start / $max);
     } else {
         $page = 1;
     }
     // get the results
     $summon_results = $this->summon_client->query($query, $page, $max, $sort);
     return $this->parseResponse($summon_results);
 }
Example #17
0
 /**
  * Return identifier for this query = search ID + query hash
  * 
  * @return string
  */
 public function getQueryID()
 {
     return $this->id . "_" . $this->query->getHash();
 }
Example #18
0
 /**
  * Do the actual search
  * 
  * @param Query $search		search object
  * @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\Query $search, $start = 1, $max = 10, $sort = "")
 {
     // prepare the query
     $query = $search->toQuery();
     // facets to include in the response
     $facets_to_include = array();
     foreach ($this->config->getFacets() as $facet_config) {
         $facets_to_include[(string) $facet_config["internal"]] = (string) $facet_config["internal"] . ",or,1," . (string) $facet_config["max"];
     }
     // limits
     $facets = array();
     foreach ($search->getLimits(true) as $limit) {
         // remove chosen facet from response
         // @todo: make multi-value facets selectable
         $facets_to_include = Parser::removeFromArray($facets_to_include, $limit->field);
         array_push($facets, $limit->field . "," . str_replace(',', '\\,', $limit->value) . ",false");
     }
     // set actual response facets
     $this->summon_client->setFacetsToInclude($facets_to_include);
     // filter out formats
     foreach ($this->formats_exclude as $format) {
         array_push($facets, "ContentType,{$format},true");
     }
     // holdings only
     if ($search->isHoldingsOnly()) {
         $this->summon_client->limitToHoldings();
     }
     // summon deals in pages, not start record number
     if ($max > 0) {
         $page = ceil($start / $max);
     } else {
         $page = 1;
     }
     // get the results
     $summon_results = $this->summon_client->query($query, $facets, $page, $max, $sort);
     return $this->parseResponse($summon_results);
 }
Example #19
0
 /**
  * calculate query identifier
  * 
  * @param string|Query $query
  * @return string
  */
 protected function getResultsID($query)
 {
     if ($query == '') {
         throw new \DomainException("Query ID cannot be empty");
     }
     $id = 'results';
     if ($query instanceof Query) {
         $id .= $query->getUrlHash();
     } else {
         $id .= $query;
     }
     return $id;
 }
 /**
  * Extract the search params as a url
  * @param Query $query
  */
 protected function getSearchQuery(Query $query)
 {
     $url = '';
     $params = $query->getAllSearchParams();
     foreach ($params as $key => $value) {
         $url .= ';' . $key . '=' . urlencode($value);
     }
     return $url;
 }
Example #21
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)
 {
     // can't search on this field, so return 0
     if ($this->query->hasUnsupportedField()) {
         $results = new Search\ResultSet($this->getConfig());
         $results->total = 0;
         return $results;
     }
     // convert query
     $query = $search->toQuery();
     // get results from Worldcat
     $xml = $this->worldcat_client->searchRetrieve($query, $start, $max, $sort);
     return $this->parseResponse($xml);
 }
Example #22
0
 /**
  * Do the actual search
  * 
  * @param Query $search		search object
  * @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\Query $search, $start = 1, $max = 10, $sort = "")
 {
     // convert query
     $query = $search->toQuery();
     // get results from Worldcat
     $xml = $this->worldcat_client->searchRetrieve($query, $start, $max, $sort);
     return $this->parseResponse($xml);
 }
Example #23
0
 /**
  * Add links to the query object limits
  * 
  * @param Query $query
  */
 public function addQueryLinks(Query $query)
 {
     // we have to pass in the query object here rather than take
     // the property above because adding the links doesn't seem
     // to reflect back in the main object, even though they should
     // be references, maybe because limit objects are in an array?
     // search option links
     $search = $this->registry->getConfig('search');
     if ($search instanceof \SimpleXMLElement) {
         $controller_map = $this->request->getControllerMap();
         foreach ($search->xpath("//option") as $option) {
             $id = (string) $option["id"];
             if ((string) $option["source"] != '') {
                 $id .= '_' . (string) $option["source"];
             }
             $id .= '_' . $this->query->getHash();
             // is this the current tab?
             if ($controller_map->getControllerName() == (string) $option["id"] && ($this->request->getParam('source') == (string) $option["source"] || (string) $option["source"] == '')) {
                 // mark as current
                 $option->addAttribute('current', "1");
                 // keep the current url, too, minus the start #
                 $params = $this->request->getParams();
                 $params['start'] = null;
                 $this->request->setSessionData("url-{$id}", $this->request->url_for($params));
             }
             // url
             $url = $this->request->getSessionData("url-{$id}");
             // already cached, so use it
             if ($url == null) {
                 $params = $query->extractSearchParams();
                 $params['controller'] = $controller_map->getUrlAlias((string) $option["id"]);
                 $params['action'] = "results";
                 $params['source'] = (string) $option["source"];
                 $params['sort'] = $this->request->getParam('sort');
                 $url = $this->request->url_for($params);
             }
             $option->addAttribute('url', $url);
             // cached search hit count?
             $session_hits = $this->request->getSessionData($id);
             if ($session_hits != null) {
                 $option->addAttribute('hits', Parser::number_format($session_hits));
             }
         }
         $this->registry->setConfig('search', $search);
     }
     // links to remove facets
     foreach ($query->getLimits() as $limit) {
         $params = $this->currentParams();
         // urlencode here necessary to support the urlencode above on 'key' urls
         $params = Parser::removeFromArray($params, urlencode($limit->field), $limit->value);
         $limit->remove_url = $this->request->url_for($params);
     }
 }