/** * Establishes base settings for making recommendations. * * @param string $settings Settings from config.ini * @param \VuFind\RecordDriver\AbstractBase $driver Record driver object * * @return void */ public function init($settings, $driver) { // If we have query parts, we should try to find related records: $parts = $this->getQueryParts($driver); if (!empty($parts)) { // Limit the number of parts based on the boolean clause limit: $result = $this->resultsManager->get('Solr'); $params = $result->getParams(); $params->getOptions()->spellcheckEnabled(false); $limit = $params->getQueryIDLimit(); if ($this->maxLimit > 0 && $limit > $this->maxLimit) { $limit = $this->maxLimit; } if (count($parts) > $limit) { $parts = array_slice($parts, 0, $limit); } // Assemble the query parts and filter out current record if it comes // from the Solr index.: $query = '(' . implode(' OR ', $parts) . ')'; if ($driver->getResourceSource() == 'VuFind') { $query .= ' NOT id:"' . addcslashes($driver->getUniqueID(), '"') . '"'; } // Perform the search and return either results or an error: $params->setLimit(5); $params->setOverrideQuery($query); $this->results = $result->getResults(); } }
/** * Constructor * * @param ConfigInterface $configuration Configuration settings (optional) */ public function __construct(ConfigInterface $configuration = null) { // These objects are not meant to be shared -- every time we retrieve one, // we are building a brand new object. $this->setShareByDefault(false); parent::__construct($configuration); }
/** * Create information representing an advanced search tab. * * @param string $class Search class ID * @param string $label Display text for tab * * @return array */ protected function createAdvancedTab($class, $label) { // If an advanced search is available, link there; otherwise, just go // to the search home: $options = $this->results->get($class)->getOptions(); $advSearch = $options->getAdvancedSearchAction(); $url = $this->url->__invoke($advSearch ? $advSearch : $options->getSearchHomeAction()); return ['class' => $class, 'label' => $label, 'selected' => false, 'url' => $url]; }
/** * Parse a simple filter array to a keyed array * * @param string $class Search class ID * @param array $filters Filters to parse * * @return array */ protected function parseFilters($class, $filters) { $results = $this->results->get($class); $params = $results->getParams(); $result = []; foreach ($filters as $filter) { list($field, $value) = $params->parseFilter($filter); $result[$field][] = $value; } return $result; }
/** * Called after the Search Results object has performed its main search. This * may be used to extract necessary information from the Search Results object * or to perform completely unrelated processing. * * @param \VuFind\Search\Base\Results $results Search results object * * @return void */ public function process($results) { // If we received a Summon search object, we'll use that. If not, we need // to create a new Summon search object using the specified request // parameter for search terms. if ($results->getParams()->getSearchClassId() != 'Summon') { $results = $this->resultsManager->get('Summon'); $this->configureSummonResults($results); $results->performAndProcessSearch(); } $this->results = $results; }
/** * Process similar authors from an author search * * @return array Facets data arrays */ public function getSimilarAuthors() { // Do not provide recommendations for blank searches: $lookfor = $this->getSearchTerm(); if (empty($lookfor)) { return ['count' => 0, 'list' => []]; } // Start configuring the results object: $results = $this->resultsManager->get('SolrAuthorFacets'); // Set up a special limit for the AuthorFacets search object: $results->getOptions()->setLimitOptions([10]); // Initialize object using parameters from the current Solr search object. $results->getParams()->initFromRequest(new Parameters(['lookfor' => $lookfor])); // Send back the results: return ['count' => false, 'list' => $results->getResults()]; }
/** * Normalize an author name using VIAF. * * @param string $author Author name * * @return string */ protected function normalizeNameWithViaf($author) { // Do authority search: $auth = $this->resultsManager->get('SolrAuth'); $auth->getParams()->setBasicSearch('"' . $author . '"', 'MainHeading'); $results = $auth->getResults(); // Find first useful LCCN: foreach ($results as $current) { $lccn = $current->tryMethod('getRawLCCN'); if (!empty($lccn)) { $name = $this->getWikipediaNameFromViaf($lccn); if (!empty($name)) { return $name; } } } // No LCCN found? Use the default normalization routine: return $this->normalizeName($author); }
/** * Run the search. * * @param array|Parameters $rawRequest Incoming parameters for search * @param string $searchClassId Type of search to perform * @param mixed $setupCallback Optional callback for setting up params * and attaching listeners; if provided, will be passed three parameters: * this object, the search parameters object, and a unique identifier for * the current running search. * @param string $lastView Last valid view parameter loaded * from a previous search (optional; used for view persistence). * * @return \VuFind\Search\Base\Results * * @throws \VuFindSearch\Backend\Exception\BackendException */ public function run($rawRequest, $searchClassId = 'Solr', $setupCallback = null, $lastView = null) { // Increment the ID counter, then save the current value to a variable; // since events within this run could theoretically trigger additional // runs of the SearchRunner, we can't rely on the property value past // this point! $this->searchId++; $runningSearchId = $this->searchId; // Format the request object: $request = $rawRequest instanceof Parameters ? $rawRequest : new Parameters(is_array($rawRequest) ? $rawRequest : []); // Set up the search: $results = $this->resultsManager->get($searchClassId); $params = $results->getParams(); $params->setLastView($lastView); $params->initFromRequest($request); if (is_callable($setupCallback)) { $setupCallback($this, $params, $runningSearchId); } // Trigger the "configuration done" event. $this->getEventManager()->trigger(self::EVENT_CONFIGURED, $this, compact('params', 'request', 'runningSearchId')); // Attempt to perform the search; if there is a problem, inspect any Solr // exceptions to see if we should communicate to the user about them. try { // Explicitly execute search within controller -- this allows us to // catch exceptions more reliably: $results->performAndProcessSearch(); } catch (\VuFindSearch\Backend\Exception\BackendException $e) { if ($e->hasTag('VuFind\\Search\\ParserError')) { // We need to create and process an "empty results" object to // ensure that recommendation modules and templates behave // properly when displaying the error message. $results = $this->resultsManager->get('EmptySet'); $results->setParams($params); $results->performAndProcessSearch(); } else { throw $e; } } // Trigger the "search completed" event. $this->getEventManager()->trigger(self::EVENT_COMPLETE, $this, compact('results', 'runningSearchId')); return $results; }
/** * Called at the end of the Search Params objects' initFromRequest() method. * This method is responsible for setting search parameters needed by the * recommendation module and for reading any existing search parameters that may * be needed. * * @param \VuFind\Search\Base\Params $params Search parameter object * @param \Zend\StdLib\Parameters $request Parameter object representing user * request. * * @return void */ public function init($params, $request) { // See if we can determine the label for the current search type; first // check for an override in the GET parameters, then look at the incoming // params object.... $typeLabel = $request->get('typeLabel'); $type = $request->get('type'); if (empty($typeLabel) && !empty($type)) { $typeLabel = $params->getOptions()->getLabelForBasicHandler($type); } // Extract a search query: $lookfor = $request->get($this->requestParam); if (empty($lookfor) && is_object($params)) { $lookfor = $params->getQuery()->getAllTerms(); } // Set up the parameters: $this->results = $this->resultsManager->get($this->getSearchClassId()); $params = $this->results->getParams(); $params->setLimit($this->limit); $params->setBasicSearch($lookfor, $params->getOptions()->getHandlerForLabel($typeLabel)); // Perform the search: $this->results->performAndProcessSearch(); }
/** * Get an array of information on non-deleted records in the specified range. * * @param int $from Start date. * @param int $until End date. * @param int $offset First record to obtain in full detail. * @param int $limit Max number of full records to return. * @param string $set Set to limit to (empty string for none). * * @return \VuFind\Search\Base\Results Search result object. */ protected function listRecordsGetNonDeleted($from, $until, $offset, $limit, $set = '') { // Set up search parameters: $results = $this->resultsManager->get($this->searchClassId); $params = $results->getParams(); $params->setLimit($limit); $params->getOptions()->disableHighlighting(); $params->getOptions()->spellcheckEnabled(false); $params->setSort('last_indexed asc', true); // Construct a range query based on last indexed time: $params->setOverrideQuery('last_indexed:[' . date($this->iso8601, $from) . ' TO ' . date($this->iso8601, $until) . ']'); // Apply filters as needed. if (!empty($set)) { if (isset($this->setQueries[$set])) { // use hidden filter here to allow for complex queries; // plain old addFilter expects simple field:value queries. $params->getOptions()->addHiddenFilter($this->setQueries[$set]); } else { if (null !== $this->setField) { $params->addFilter($this->setField . ':"' . addcslashes($set, '"') . '"'); } } } // Perform a Solr search: $results->overrideStartRecord($offset + 1); // Return our results: return $results; }
/** * Turn the current object into search results. * * @param \VuFind\Search\Results\PluginManager $manager Search manager * * @return \VuFind\Search\Base\Results */ public function deminify(\VuFind\Search\Results\PluginManager $manager) { // Figure out the parameter and result classes based on the search class ID: $this->populateClassNames(); // Deminify everything: $results = $manager->get($this->cl); $results->getParams()->deminify($this); $results->deminify($this); return $results; }
/** * Initialize the search object used for finding recommendations. * * @return void */ protected function initSearchObject() { // Build a new search object: $this->searchObject = $this->resultsManager->get($this->searchClassId); $this->searchObject->getOptions()->spellcheckEnabled(false); }
/** * Test expected interface. * * @return void * * @expectedException Zend\ServiceManager\Exception\RuntimeException * @expectedExceptionMessage Plugin ArrayObject does not belong to VuFind\Search\Base\Results */ public function testExpectedInterface() { $pm = new PluginManager(null); $pm->validatePlugin(new \ArrayObject()); }
/** * Called after the Search Results object has performed its main search. This * may be used to extract necessary information from the Search Results object * or to perform completely unrelated processing. * * @param \VuFind\Search\Base\Results $results Search results object * * @return void */ public function process($results) { $this->results = $results; // function will return blank on Advanced Search if ($results->getParams()->getSearchType() == 'advanced') { return; } // check result limit before proceeding... if ($this->resultLimit > 0 && $this->resultLimit < $results->getResultTotal()) { return; } // Build an advanced search request that prevents Solr from retrieving // records that would already have been retrieved by a search of the biblio // core, i.e. it only returns results where $lookfor IS found in in the // "Heading" search and IS NOT found in the "MainHeading" search defined // in authsearchspecs.yaml. $request = new Parameters(['join' => 'AND', 'bool0' => ['AND'], 'lookfor0' => [$this->lookfor], 'type0' => ['Heading'], 'bool1' => ['NOT'], 'lookfor1' => [$this->lookfor], 'type1' => ['MainHeading']]); // Initialise and process search (ignore Solr errors -- no reason to fail // just because search syntax is not compatible with Authority core): try { $authResults = $this->resultsManager->get('SolrAuth'); $authParams = $authResults->getParams(); $authParams->initFromRequest($request); foreach ($this->filters as $filter) { $authParams->addHiddenFilter($filter); } $results = $authResults->getResults(); } catch (RequestErrorException $e) { return; } // loop through records and assign id and headings to separate arrays defined // above foreach ($results as $result) { // Extract relevant details: $recordArray = ['id' => $result->getUniqueID(), 'heading' => $result->getBreadcrumb()]; // check for duplicates before adding record to recordSet if (!$this->inArrayR($recordArray['heading'], $this->recommendations)) { array_push($this->recommendations, $recordArray); } else { continue; } } }