/** * Run service. * * @param array $arguments Command line arguments. * * @return boolean success */ public function run($arguments) { if (!isset($arguments[0]) || $arguments[0] != 'Y') { echo "Usage:\n php index.php util update_search_hashes Y\n\n" . " Update hashes of saved searches.\n"; return false; } $searchWhere = ['checksum' => null, 'saved' => 1]; $searchRows = $this->table->select($searchWhere); if (count($searchRows) > 0) { $count = 0; $orFilterCount = 0; foreach ($searchRows as $searchRow) { try { $minified = $searchRow->getSearchObject(); if (!empty($minified->o)) { // Fix orFilters while at it if (!isset($minified->f)) { $minified->f = []; } foreach ($minified->o as $field => $orFilters) { foreach ($orFilters as $orFilter) { $minified->f["~{$field}"][] = $orFilter; } } unset($minified->o); ++$orFilterCount; $searchRow->search_object = serialize($minified); } $searchObj = $minified->deminify($this->manager); $url = $searchObj->getUrlQuery()->getParams(); $checksum = crc32($url) & 0xfffffff; $searchRow->checksum = $checksum; $searchRow->save(); } catch (\Exception $e) { echo "Failed to process search {$searchRow->id}: " . $e->getMessage() . "\n"; print_r($searchRow->getSearchObject()); continue; } ++$count; } echo "Added checksum to {$count} rows and converted orFilters in" . " {$orFilterCount} rows in search table\n"; } else { echo "No saved rows without hash found\n"; } return true; }
/** * Constructor */ public function __construct() { parent::__construct(); $this->rowClass = 'Finna\\Db\\Row\\Search'; }
/** * Add a search into the search table (history) * * @param \VuFind\Search\Results\PluginManager $manager Search manager * @param \VuFind\Search\Base\Results $newSearch Search to save * @param string $sessionId Current session ID * @param array $searchHistory Existing saved * searches (for deduplication purposes) * * @return void */ public function saveSearch(\VuFind\Search\Results\PluginManager $manager, $newSearch, $sessionId, $searchHistory = []) { // Resolve search hash of current search $hash = null; // Duplicate elimination foreach ($searchHistory as $oldSearch) { // Deminify the old search (note that if we have a resource, we need // to grab the contents -- this is necessary for PostgreSQL compatibility // although MySQL returns a plain string). $minSO = $oldSearch->getSearchObject(); $dupSearch = $minSO->deminify($manager); // See if the classes and urls match $oldUrl = $dupSearch->getUrlQuery()->getParams(); $newUrl = $newSearch->getUrlQuery()->getParams(); if (get_class($dupSearch) == get_class($newSearch) && $oldUrl == $newUrl) { $hash = $oldSearch->finna_search_id; break; } } parent::saveSearch($manager, $newSearch, $sessionId, $searchHistory); // Augment row updated by parent with search hash $row = $this->select(['id' => $newSearch->getSearchId()])->current(); if (empty($row)) { return false; } $row['finna_search_object'] = serialize(new fminSO($newSearch)); if (!$hash) { $hash = md5(md5($row->search_object) . md5($row->finna_search_object)); } $row->finna_search_id = $hash; $row->save(); $newSearch->setSearchHash($row->finna_search_id); }