Skip to content

Commit

Permalink
Support filters and custom heading on recommendation modules (vufind-…
Browse files Browse the repository at this point in the history
  • Loading branch information
maccabeelevine authored Nov 14, 2023
1 parent c8a3c83 commit ec7a698
Show file tree
Hide file tree
Showing 21 changed files with 282 additions and 40 deletions.
54 changes: 37 additions & 17 deletions config/vufind/searches.ini
Original file line number Diff line number Diff line change
Expand Up @@ -258,28 +258,33 @@ WorkKeys = year
;
; Available modules recommended for use in the side area:
;
; CatalogResults:[GET parameter]:[result limit]
; CatalogResults:[GET parameter]:[result limit]:[custom heading]:[filter ini section]
; Display catalog search results matching the terms found in the specified
; GET parameter (default = "lookfor"), limited to a specified number of
; matches (default = 5). This is designed for use with non-catalog modules.
; matches (default = 5). This is designed for use with non-catalog modules.
; The optional custom heading overrides the default for this recommendation.
; Filters may be defined in the specified section of the searches.ini
; file; see [CatalogResultsVideoFilter] below for an example of this.
; Databases:[result limit]:[ini name]
; Displays a list of the databases referenced in EDS or similar facets, each
; linking to its individual website. The .ini file must contain a [Databases]
; section with the relevant configuration; see example in EDS.ini.
; DPLATerms:[collapsed]
; Display results from the DPLA catalog. Provide a boolean to have the sidebar
; collapsed or open on page load.
; EDSResults:[GET parameter]:[result limit]
; EDSResults:[GET parameter]:[result limit]:[custom heading]
; Display EDS search results matching the terms found in the specified
; GET parameter (default = "lookfor"), limited to a specified number of
; matches (default = 5).
; EDSResultsDeferred:[GET parameter]:[result limit]
; matches (default = 5). The optional custom heading overrides the default
; for this recommendation.
; EDSResultsDeferred:[GET parameter]:[result limit]:[custom heading]
; Same as EDSResults, but loaded via AJAX.
; EPFResults:[GET parameter]:[result limit]
; EPFResults:[GET parameter]:[result limit]:[custom heading]
; Display EPF (EBSCO Publication Finder) search results matching the terms
; found in the specified GET parameter (default = "lookfor"), limited to a
; specified number of matches (default = 5).
; EPFResultsDeferred:[GET parameter]:[result limit]
; specified number of matches (default = 5). The optional custom heading
; overrides the default for this recommendation.
; EPFResultsDeferred:[GET parameter]:[result limit]:[custom heading]
; Same as EPFResults, but loaded via AJAX.
; EuropeanaResults:[url]:[requestParam]:[limit]:[unwanted data providers]
; Display search results from Europeana.eu API.
Expand Down Expand Up @@ -308,17 +313,19 @@ WorkKeys = year
; who owns a subject guide whose title most closely matches the search terms.
; Uses configuration in LibGuidesAPI.ini and requires configuration in
; contentsecuritypolicy.ini.
; LibGuidesResults:[GET parameter]:[result limit]
; LibGuidesResults:[GET parameter]:[result limit]:[custom heading]
; Display LibGuides research guides search results matching the terms found in
; the specified GET parameter (default = "lookfor"), limited to a specified
; number of matches (default = 5).
; LibGuidesResultsDeferred:[GET parameter]:[result limit]
; number of matches (default = 5). The optional custom heading overrides the
; default for this recommendation.
; LibGuidesResultsDeferred:[GET parameter]:[result limit]:[custom heading]
; Same as LibGuidesResults, but loaded via AJAX.
; LibGuidesAZResults:[GET parameter]:[result limit]
; LibGuidesAZResults:[GET parameter]:[result limit]:[custom heading]
; Display LibGuides A-Z Databases search results matching the terms found in
; the specified GET parameter (default = "lookfor"), limited to a specified
; number of matches (default = 5).
; LibGuidesAZResultsDeferred:[GET parameter]:[result limit]
; number of matches (default = 5). The optional custom heading overrides the
; default for this recommendation.
; LibGuidesAZResultsDeferred:[GET parameter]:[result limit]:[custom heading]
; Same as LibGuidesAZResults, but loaded via AJAX.
; OpenLibrarySubjects:[GET parameter]:[limit]:[date filter]:[Subject types]
; Display full-text, public scans from the Open Library (OL) Subjects API.
Expand Down Expand Up @@ -390,22 +397,28 @@ WorkKeys = year
; specified when combining this module with a non-Summon-based search module.
; SummonDatabasesDeferred:[GET parameter]
; Same as SummonDatabases, but loaded via AJAX.
; SummonResults:[GET parameter]:[result limit]
; SummonResults:[GET parameter]:[result limit]:[custom heading]:[filter ini section]
; Display Summon search results matching the terms found in the specified
; GET parameter (default = "lookfor"), limited to a specified number of
; matches (default = 5).
; SummonResultsDeferred:[GET parameter]:[result limit]
; Filters may be defined in the specified section of the Summon.ini
; file; see [CatalogResultsVideoFilter] below for an example of this. The
; optional custom heading overrides the default for this recommendation module.
; SummonResultsDeferred:[GET parameter]:[result limit]:[custom heading]:[ini section]
; Same as SummonResults, but loaded via AJAX.
; SummonTopics:[GET parameter]
; Display Summon-generated topic recommendations matching the terms found
; in the specified GET parameter. NOTE: If you are using this module with a
; Summon search, the [GET parameter] setting will be ignored and the actual
; current Summon search will be used instead. The parameter only needs to be
; specified when combining this module with a non-Summon-based search module.
; WebResults:[GET parameter]:[result limit]
; WebResults:[GET parameter]:[result limit]:[custom heading]:[filter ini section]
; Display website search results matching the terms found in the specified
; GET parameter (default = "lookfor"), limited to a specified number of
; matches (default = 5). This requires the website Solr core to be active.
; Filters may be defined in the specified section of the website.ini
; file; see [CatalogResultsVideoFilter] below for an example of this. The
; optional custom heading overrides the default for this recommendation module.
;
; Available modules recommended for use in the top area:
;
Expand Down Expand Up @@ -869,3 +882,10 @@ maxLimit = 100
; ttl, Time to Live, i.e. cache entry life time in seconds. 300 seconds by default.
;options[ttl] = 300

; This example section provides filters for the CatalogResults recommendation
; module. The section name is specified in the recommendation module config.
;[CatalogResultsVideoFilter]

; Filters are defined as in [RawHiddenFilters] in order to handle more complex
; filter combinations.
;0 = "format:Video"
63 changes: 61 additions & 2 deletions module/VuFind/src/VuFind/Recommend/AbstractSearchObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ abstract class AbstractSearchObject implements RecommendInterface
*/
protected $limit;

/**
* Heading for this recommendation module
*
* @var string
*/
protected $heading;

/**
* Config section with filters for this search
*
* @var string
*/
protected $filterIniSection;

/**
* Name of request parameter to use for search query
*
Expand All @@ -75,14 +89,23 @@ abstract class AbstractSearchObject implements RecommendInterface
*/
protected $runner;

/**
* Config PluginManager
*
* @var \VuFind\Config\PluginManager
*/
protected $configManager;

/**
* Constructor
*
* @param SearchRunner $runner Search runner
* @param SearchRunner $runner Search runner
* @param \VuFind\Config\PluginManager $configManager Config manager
*/
public function __construct(SearchRunner $runner)
public function __construct(SearchRunner $runner, \VuFind\Config\PluginManager $configManager)
{
$this->runner = $runner;
$this->configManager = $configManager;
}

/**
Expand All @@ -99,6 +122,9 @@ public function setConfig($settings)
$this->limit
= (isset($settings[1]) && is_numeric($settings[1]) && $settings[1] > 0)
? intval($settings[1]) : 5;

$this->heading = $settings[2] ?? $this->getDefaultHeading();
$this->filterIniSection = $settings[3] ?? false;
}

/**
Expand Down Expand Up @@ -139,6 +165,22 @@ public function init($params, $request)
$lookfor,
$params->getOptions()->getHandlerForLabel($typeLabel)
);

// Set any filters configured for this search
if (!empty($this->filterIniSection)) {
$ini = $params->getOptions()->getSearchIni();
$config = $this->configManager->get($ini);
try {
$filters = $config->{$this->filterIniSection}->toArray() ?? [];
} catch (\Error $e) {
throw new \Exception(
"No section found matching '$this->filterIniSection' in $ini.ini."
);
}
foreach ($filters as $filter) {
$params->addFilter($filter);
}
}
};

// Perform the search:
Expand Down Expand Up @@ -170,10 +212,27 @@ public function getResults()
return $this->results;
}

/**
* Get the heading.
*
* @return string
*/
public function getHeading()
{
return $this->heading;
}

/**
* Get the search class ID to use for building search objects.
*
* @return string
*/
abstract protected function getSearchClassId();

/**
* Get the default heading for this recommendation module.
*
* @return string
*/
abstract protected function getDefaultHeading();
}
81 changes: 81 additions & 0 deletions module/VuFind/src/VuFind/Recommend/AbstractSearchObjectFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

/**
* Factory for instantiating recommendation modules.
*
* PHP version 8
*
* Copyright (C) Villanova University 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 Recommendations
* @author Demian Katz <[email protected]>
* @author Maccabee Levine <[email protected]>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development Wiki
*/

namespace VuFind\Recommend;

use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
use Psr\Container\ContainerExceptionInterface as ContainerException;
use Psr\Container\ContainerInterface;
use VuFind\Search\SearchRunner;

/**
* Factory for instantiating recommendation modules.
*
* @category VuFind
* @package Recommendations
* @author Demian Katz <[email protected]>
* @author Maccabee Levine <[email protected]>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development Wiki
*/
class AbstractSearchObjectFactory implements \Laminas\ServiceManager\Factory\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
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function __invoke(
ContainerInterface $container,
$requestedName,
array $options = null
) {
if (!empty($options)) {
throw new \Exception('Unexpected options passed to factory.');
}
$module = new $requestedName(
$container->get(SearchRunner::class),
$container->get(\VuFind\Config\PluginManager::class)
);
return $module;
}
}
10 changes: 10 additions & 0 deletions module/VuFind/src/VuFind/Recommend/CatalogResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,14 @@ protected function getSearchClassId()
{
return 'Solr';
}

/**
* Get the default heading for this recommendation module.
*
* @return string
*/
protected function getDefaultHeading()
{
return 'Catalog Results';
}
}
10 changes: 10 additions & 0 deletions module/VuFind/src/VuFind/Recommend/EDSResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,14 @@ protected function getSearchClassId()
{
return 'EDS';
}

/**
* Get the default heading for this recommendation module.
*
* @return string
*/
protected function getDefaultHeading()
{
return 'EDS Results';
}
}
10 changes: 10 additions & 0 deletions module/VuFind/src/VuFind/Recommend/EPFResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,14 @@ protected function getSearchClassId()
{
return 'EPF';
}

/**
* Get the default heading for this recommendation module.
*
* @return string
*/
protected function getDefaultHeading()
{
return 'epf_recommendations';
}
}
11 changes: 6 additions & 5 deletions module/VuFind/src/VuFind/Recommend/InjectSearchRunnerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@
/**
* Factory for instantiating recommendation modules with search runner.
*
* @category VuFind
* @package Recommendations
* @author Demian Katz <[email protected]>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development Wiki
* @category VuFind
* @package Recommendations
* @author Demian Katz <[email protected]>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development Wiki
* @deprecated Use AbstractSearchObjectFactory
*/
class InjectSearchRunnerFactory implements \Laminas\ServiceManager\Factory\FactoryInterface
{
Expand Down
10 changes: 10 additions & 0 deletions module/VuFind/src/VuFind/Recommend/LibGuidesAZResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,14 @@ protected function getSearchClassId()
{
return 'LibGuidesAZ';
}

/**
* Get the default heading for this recommendation module.
*
* @return string
*/
protected function getDefaultHeading()
{
return 'libguidesaz_recommendations';
}
}
10 changes: 10 additions & 0 deletions module/VuFind/src/VuFind/Recommend/LibGuidesResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,14 @@ protected function getSearchClassId()
{
return 'LibGuides';
}

/**
* Get the default heading for this recommendation module.
*
* @return string
*/
protected function getDefaultHeading()
{
return 'libguides_recommendations';
}
}
Loading

0 comments on commit ec7a698

Please sign in to comment.