Skip to content

Commit

Permalink
Refactor versions search to fix deferred facets and simplify code. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
EreMaijala authored Jan 5, 2024
1 parent ee1a7c5 commit 7fab326
Show file tree
Hide file tree
Showing 20 changed files with 150 additions and 364 deletions.
1 change: 0 additions & 1 deletion module/VuFind/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,6 @@
'VuFind\Record\FallbackLoader\PluginManager' => 'VuFind\ServiceManager\AbstractPluginManagerFactory',
'VuFind\Record\Loader' => 'VuFind\Record\LoaderFactory',
'VuFind\Record\Router' => 'VuFind\Service\ServiceWithConfigIniFactory',
'VuFind\Record\VersionsHelper' => 'VuFind\Record\VersionsHelperFactory',
'VuFind\RecordDriver\PluginManager' => 'VuFind\ServiceManager\AbstractPluginManagerFactory',
'VuFind\RecordTab\PluginManager' => 'VuFind\ServiceManager\AbstractPluginManagerFactory',
'VuFind\RecordTab\TabManager' => 'VuFind\RecordTab\TabManagerFactory',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/**
* VuFind Action Feature Trait - Record Versions Search
* Depends on method getSearchResultsView and record driver's method getWorkKeys.
* Depends on method getSearchResultsView.
*
* PHP version 8
*
Expand Down Expand Up @@ -52,23 +52,15 @@ trait RecordVersionsSearchTrait
*/
public function versionsAction()
{
$versionsHelper
= $this->serviceLocator->get(\VuFind\Record\VersionsHelper::class);
$keyData = $versionsHelper->getIdDriverAndWorkKeysFromParams(
$this->params()->fromQuery(),
$this->searchClassId
);
if (empty($keyData['keys'])) {
return $this->forwardTo('Search', 'Home');
}

$query = new WorkKeysQuery(null, $keyData['keys']);

// Don't save to history -- history page doesn't handle correctly:
$this->saveToHistory = false;

$callback = function ($runner, $params, $searchId) use ($query) {
$params->setQuery($query);
$id = null;
$callback = function ($runner, $params, $searchId) use (&$id) {
$query = $params->getQuery();
if ($query instanceof WorkKeysQuery) {
$id = $query->getId();
}
$defaultCallback = is_callable([$this, 'getSearchSetupCallback'])
? $this->getSearchSetupCallback() : null;
if (is_callable($defaultCallback)) {
Expand All @@ -80,19 +72,10 @@ public function versionsAction()
};

$view = $this->getSearchResultsView($callback);

if (isset($view->results)) {
// Customize the URL helper to make sure it builds proper versions URLs
// (but only do this if we have access to a results object, which we
// won't in RSS mode):
$view->results->getUrlQuery()
->setDefaultParameter('id', $keyData['id'])
// original keys from the query, if it had any:
->setDefaultParameter('keys', $this->params()->fromQuery('keys'))
->setSuppressQuery(true);
$view->driver = $keyData['driver'];
if (null !== $id) {
$loader = $this->serviceLocator->get(\VuFind\Record\Loader::class);
$view->driver = $loader->load($id, $this->searchClassId);
}

return $view;
}
}
112 changes: 0 additions & 112 deletions module/VuFind/src/VuFind/Record/VersionsHelper.php

This file was deleted.

75 changes: 0 additions & 75 deletions module/VuFind/src/VuFind/Record/VersionsHelperFactory.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function getOtherVersionCount()
}

if (!isset($this->otherVersionsCount)) {
if (!($workKeys = $this->tryMethod('getWorkKeys'))) {
if (!$this->tryMethod('getWorkKeys')) {
if (!($this instanceof VersionAwareInterface)) {
throw new \Exception(
'VersionAwareTrait requires VersionAwareInterface'
Expand All @@ -84,7 +84,7 @@ public function getOtherVersionCount()
$command = new WorkExpressionsCommand(
$this->getSourceIdentifier(),
$this->getUniqueID(),
$workKeys,
false,
$params
);
$results = $this->searchService->invoke($command)->getResult();
Expand All @@ -104,11 +104,7 @@ public function getOtherVersionCount()
*/
public function getVersions($includeSelf = false, $count = 20, $offset = 0)
{
if (null === $this->searchService) {
return false;
}

if (!($workKeys = $this->getWorkKeys())) {
if (null === $this->searchService || !$this->getWorkKeys()) {
return false;
}

Expand All @@ -118,8 +114,8 @@ public function getVersions($includeSelf = false, $count = 20, $offset = 0)
$params->add('start', $offset);
$command = new WorkExpressionsCommand(
$this->getSourceIdentifier(),
$includeSelf ? '' : $this->getUniqueID(),
$workKeys,
$this->getUniqueID(),
$includeSelf,
$params
);
$this->otherVersions = $this->searchService->invoke(
Expand Down
37 changes: 32 additions & 5 deletions module/VuFind/src/VuFind/Search/QueryAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,18 @@ abstract class QueryAdapter
*
* @param array $search Minified search arguments
*
* @return Query|QueryGroup
* @return Query|QueryGroup|WorkKeysQuery
*/
public static function deminify(array $search)
{
$type = $search['s'] ?? null;
if ('w' === $type) {
// WorkKeysQuery
return new WorkKeysQuery($search['l'], $search['i']);
}
// Use array_key_exists since null is also valid
if (array_key_exists('l', $search)) {
if ('b' === $type || array_key_exists('l', $search)) {
// Basic search
$handler = $search['i'] ?? $search['f'];
return new Query(
$search['l'],
Expand Down Expand Up @@ -109,7 +115,7 @@ public static function display(AbstractQuery $query, $translate, $showName)

// Work keys query:
if ($query instanceof WorkKeysQuery) {
return $query->getId() ?? '';
return $translate('Versions') . ' - ' . ($query->getId() ?? '');
}

// Complex case -- advanced query:
Expand Down Expand Up @@ -179,15 +185,25 @@ protected static function displayAdvanced(

/**
* Convert user request parameters into a query (currently for advanced searches
* only).
* and work keys searches only).
*
* @param Parameters $request User-submitted parameters
* @param string $defaultHandler Default search handler
*
* @return Query|QueryGroup
* @return Query|QueryGroup|WorkKeysQuery
*/
public static function fromRequest(Parameters $request, $defaultHandler)
{
// Check for a work keys query first (id and keys included for back-compatibility):
if (
$request->get('search') === 'versions'
|| ($request->offsetExists('id') && $request->offsetExists('keys'))
) {
if (null !== ($id = $request->offsetGet('id'))) {
return new WorkKeysQuery($id, true);
}
}

$groups = [];
// Loop through all parameters and look for 'lookforX'
foreach ($request as $key => $value) {
Expand Down Expand Up @@ -245,10 +261,20 @@ public static function minify(AbstractQuery $query, $topLevel = true)
[
'l' => $query->getString(),
'i' => $query->getHandler(),
's' => 'b',
],
];
}

// WorkKeys query:
if ($query instanceof WorkKeysQuery) {
return [
'l' => $query->getId(),
'i' => $query->getIncludeSelf(),
's' => 'w',
];
}

// Advanced query:
$retVal = [];
$operator = $query->isNegated() ? 'NOT' : $query->getOperator();
Expand All @@ -257,6 +283,7 @@ public static function minify(AbstractQuery $query, $topLevel = true)
$retVal[] = [
'g' => self::minify($current, false),
'j' => $operator,
's' => 'a',
];
} elseif ($current instanceof QueryGroup) {
throw new \Exception('Not sure how to minify this query!');
Expand Down
Loading

0 comments on commit 7fab326

Please sign in to comment.