Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move record version key handling to the new Versions helper. #3033

Merged
merged 3 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions module/VuFind/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@
'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 @@ -48,30 +48,26 @@ trait RecordVersionsSearchTrait
*/
public function versionsAction()
{
$id = $this->params()->fromQuery('id');
$keys = $this->params()->fromQuery('keys');
$record = null;
if ($id) {
$loader = $this->serviceLocator->get(\VuFind\Record\Loader::class);
$record = $loader->load($id, $this->searchClassId, true);
if ($record instanceof \VuFind\RecordDriver\Missing) {
$record = null;
} else {
$keys = $record->tryMethod('getWorkKeys');
}
$versionsHelper
= $this->serviceLocator->get(\VuFind\Record\VersionsHelper::class);
$driverAndKeys = $versionsHelper->getDriverAndWorkKeysFromParams(
$this->params()->fromQuery(),
$this->searchClassId
);
$record = $driverAndKeys['driver'];
if ($record instanceof \VuFind\RecordDriver\Missing) {
$record = null;
}

if (empty($keys)) {
if (empty($driverAndKeys['keys'])) {
return $this->forwardTo('Search', 'Home');
}

$mapFunc = function ($val) {
return '"' . addcslashes($val, '"') . '"';
};

$query = $this->getRequest()->getQuery();
$query->lookfor = implode(' OR ', array_map($mapFunc, (array)$keys));
$query->type = 'WorkKeys';
$query->lookfor = $versionsHelper->getSearchStringFromWorkKeys(
(array)$driverAndKeys['keys']
);
$query->type = $versionsHelper->getWorkKeysSearchType();

// Don't save to history -- history page doesn't handle correctly:
$this->saveToHistory = false;
Expand All @@ -94,8 +90,8 @@ public function versionsAction()
// won't in RSS mode):
if (isset($view->results)) {
$view->results->getUrlQuery()
->setDefaultParameter('id', $id)
->setDefaultParameter('keys', $keys)
->setDefaultParameter('id', $this->params()->fromQuery('id'))
->setDefaultParameter('keys', $this->params()->fromQuery('keys'))
->setSuppressQuery(true);
$view->driver = $record;
}
Expand Down
110 changes: 110 additions & 0 deletions module/VuFind/src/VuFind/Record/VersionsHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

/**
* Helper that provides support methods for record versions search
*
* PHP version 7
*
* Copyright (C) The National Library of Finland 2020-2023.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category VuFind
* @package Record
* @author Ere Maijala <ere.maijala@helsinki.fi>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org Main Page
*/

namespace VuFind\Record;

/**
* Helper that provides support methods for record versions search
*
* @category VuFind
* @package Record
* @author Ere Maijala <ere.maijala@helsinki.fi>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org Main Page
*/
class VersionsHelper
{
/**
* Record loader
*
* @var Loader
*/
protected $recordLoader;

/**
* Constructor
*
* @param Loader $recordLoader Record loader
*/
public function __construct(
Loader $recordLoader
) {
$this->recordLoader = $recordLoader;
}

/**
* Get record driver and work keys from query params
*
* @param array $params Query params containing id and/or keys
* @param string $backend Search backend ID
*
* @return array with driver and keys
*/
public function getDriverAndWorkKeysFromParams(
array $params,
string $backend
): array {
$id = $params['id'] ?? null;
$keys = $params['keys'] ?? null;
$driver = null;
if ($id) {
$driver = $this->recordLoader->load($id, $backend, true);
if (!($driver instanceof \VuFind\RecordDriver\Missing)) {
$keys = $driver->tryMethod('getWorkKeys') ?? $keys;
}
}
return compact('driver', 'keys');
}

/**
* Convert work keys to a search string
*
* @param array $keys Work keys
*
* @return string
*/
public function getSearchStringFromWorkKeys(array $keys): string
{
$mapFunc = function ($val) {
return '"' . addcslashes($val, '"') . '"';
};

return implode(' OR ', array_map($mapFunc, (array)$keys));
}

/**
* Get search type for work keys search
*
* @return string
*/
public function getWorkKeysSearchType(): string
{
return 'WorkKeys';
}
}
75 changes: 75 additions & 0 deletions module/VuFind/src/VuFind/Record/VersionsHelperFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/**
* Versions helper factory.
*
* PHP version 7
*
* Copyright (C) The National Library of Finland 2023.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category VuFind
* @package Record
* @author Ere Maijala <ere.maijala@helsinki.fi>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development Wiki
*/

namespace VuFind\Record;

use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
use Laminas\ServiceManager\Factory\FactoryInterface;
use Psr\Container\ContainerExceptionInterface as ContainerException;
use Psr\Container\ContainerInterface;

/**
* Versions helper factory.
*
* @category VuFind
* @package Record
* @author Ere Maijala <ere.maijala@helsinki.fi>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development Wiki
*/
class VersionsHelperFactory implements FactoryInterface
{
/**
* Create an object
*
* @param ContainerInterface $container Service manager
* @param string $requestedName Service being created
* @param null|array $options Extra options (optional)
*
* @return object
*
* @throws ServiceNotFoundException if unable to resolve the service.
* @throws ServiceNotCreatedException if an exception is raised when
* creating a service.
* @throws ContainerException&\Throwable if any other error occurs
*/
public function __invoke(
ContainerInterface $container,
$requestedName,
array $options = null
) {
if (!empty($options)) {
throw new \Exception('Unexpected options passed to factory.');
}
return new $requestedName(
$container->get(\VuFind\Record\Loader::class)
);
}
}