Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
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 composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
"matthiasmullie/minify": "1.3.75",
"monolog/monolog": "^3.9",
"mpdf/mpdf": "v8.2.6",
"natlibfi/finna-xml": "1.6.0",
"paytrail/paytrail-php-sdk": "2.7.5",
"pear/archive_tar": "^1.4",
"phing/phing": "3.1.0",
Expand Down
56 changes: 55 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

/**
* Functions for locale-specific processing in record drivers.
*
* PHP version 8
*
* Copyright (C) The National Library of Finland 2026.
*
* 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, see
* <https://www.gnu.org/licenses/>.
*
* @category VuFind
* @package RecordDrivers
* @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:plugins:record_drivers Wiki
*/

namespace VuFind\RecordDriver\Feature;

/**
* Functions for locale-specific processing in record drivers.
*
* @category VuFind
* @package RecordDrivers
* @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:plugins:record_drivers Wiki
*/
trait LocaleSupportTrait
{
/**
* Pick correct results from locale-specific results with fallback to all results.
*
* @param array $localeResults Result(s) keyed by locale
* @param array|string $allResults All results
*
* @return array|string
*/
protected function getLocaleSpecificResults(array $localeResults, array|string $allResults): array|string
{
if (null === $this->localeSettings) {
Comment thread
demiankatz marked this conversation as resolved.
Outdated
return $allResults;
}
$userLocale = $this->localeSettings->getUserLocale();
if (null !== ($results = $this->getBestLocaleMatch($userLocale, $localeResults))) {
return $results;
}
// Check for matching language in locale-specific results:
[$userLanguage] = explode('-', $userLocale);
foreach ($localeResults as $locale => $results) {
[$lang] = explode('-', $locale);
if ($lang === $userLanguage) {
return $results;
}
}
Comment thread
demiankatz marked this conversation as resolved.
Outdated
// Check for match in default and fallback locales:
$locales = [$this->localeSettings->getDefaultLocale(), ...$this->localeSettings->getFallbackLocales()];
foreach ($locales as $locale) {
if (null !== ($results = $this->getBestLocaleMatch($locale, $localeResults))) {
return $results;
}
}
// Could not find anything else, so return all:
return $allResults;
}

/**
* Pick best match for a locale from the results.
*
* @param string $locale Locale
* @param array $localeResults Result(s) keyed by locale
*
* @return mixed
*/
protected function getBestLocaleMatch(string $locale, array $localeResults): mixed
{
[$language] = explode('-', $locale);
return $localeResults[$locale] ?? $localeResults[$language] ?? null;
}
}
97 changes: 97 additions & 0 deletions module/VuFind/src/VuFind/RecordDriver/Feature/XmlTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

/**
* Functions for reading XML records.
*
* PHP version 8
*
* Copyright (C) The National Library of Finland 2026.
*
* 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, see
* <https://www.gnu.org/licenses/>.
*
* @category VuFind
* @package RecordDrivers
* @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:plugins:record_drivers Wiki
*/

namespace VuFind\RecordDriver\Feature;

use FinnaXml\XmlDoc;

/**
* Functions for reading XML records.
*
* Assumption: raw XML data can be found in $this->fields['fullrecord'].
*
* @category VuFind
* @package RecordDrivers
* @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:plugins:record_drivers Wiki
*/
trait XmlTrait
{
/**
* The XML namespace.
*
* Note: this is a property instead of a constant to make use of it in strings cleaner.
*
* @var string
*/
protected string $xmlNs = 'http://www.w3.org/2000/xmlns/';

/**
* XML class to use.
*
* @var string
*/
protected string $xmlClass = \FinnaXml\XmlDoc::class;

/**
* XML instance. Access only via getXmlReader() as this is initialized lazily.
*
* @var XmlDoc
*/
Comment thread
demiankatz marked this conversation as resolved.
protected ?XmlDoc $lazyXmlReader = null;

/**
* Get access to the XML object.
*
* @return XmlDoc
*/
public function getXmlReader(): XmlDoc
{
if (null === $this->lazyXmlReader) {
$this->lazyXmlReader = new $this->xmlClass();
$this->lazyXmlReader->parse($this->fields['fullrecord']);
}

return $this->lazyXmlReader;
}

/**
* Get lang attribute from xml namespace with fallback to default namespace.
*
* @param array $node XmlDoc node
*
* @return ?string
*/
protected function getLangAttr(array $node): ?string
{
$xml = $this->getXmlReader();
return $xml->attr($node, '{{$this->xmlNs}}lang') ?? $xml->attr($node, 'lang');
}
}
2 changes: 2 additions & 0 deletions module/VuFind/src/VuFind/RecordDriver/PluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class PluginManager extends \VuFind\ServiceManager\AbstractPluginManager
'solrmarc' => SolrMarc::class,
'solrmarcremote' => SolrMarcRemote::class,
'solroverdrive' => SolrOverdrive::class,
'solrqdc' => SolrQdc::class,
'solrreserves' => SolrReserves::class,
'solrweb' => SolrWeb::class,
'summon' => Summon::class,
Expand Down Expand Up @@ -109,6 +110,7 @@ class PluginManager extends \VuFind\ServiceManager\AbstractPluginManager
SolrMarc::class => SolrDefaultFactory::class,
SolrMarcRemote::class => SolrDefaultFactory::class,
SolrOverdrive::class => SolrOverdriveFactory::class,
SolrQdc::class => SolrDefaultFactory::class,
SolrReserves::class => SolrDefaultWithoutSearchServiceFactory::class,
SolrWeb::class => SolrWebFactory::class,
Summon::class => SummonFactory::class,
Expand Down
20 changes: 20 additions & 0 deletions module/VuFind/src/VuFind/RecordDriver/SolrDefault.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

namespace VuFind\RecordDriver;

use VuFind\I18n\Locale\LocaleSettings;
use VuFindSearch\Command\SearchCommand;

use function count;
Expand Down Expand Up @@ -129,6 +130,13 @@ class SolrDefault extends DefaultRecord implements
*/
protected $explainEnabled = false;

/**
* Locale settings, if available
*
* @var ?LocaleSettings
*/
protected ?LocaleSettings $localeSettings = null;

/**
* Constructor
*
Expand Down Expand Up @@ -295,6 +303,18 @@ public function attachSearchService(\VuFindSearch\Service $service)
$this->searchService = $service;
}

/**
* Attach locale settings.
*
* @param LocaleSettings $localeSettings Locale settings
*
* @return void
*/
public function attachLocaleSettings(LocaleSettings $localeSettings): void
{
$this->localeSettings = $localeSettings;
}

/**
* Get the number of child records belonging to this record
*
Expand Down
2 changes: 2 additions & 0 deletions module/VuFind/src/VuFind/RecordDriver/SolrDefaultFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
use Psr\Container\ContainerExceptionInterface as ContainerException;
use Psr\Container\ContainerInterface;
use VuFind\I18n\Locale\LocaleSettings;

/**
* Factory for SolrDefault record drivers.
Expand Down Expand Up @@ -66,6 +67,7 @@ public function __invoke(
) {
$driver = parent::__invoke($container, $requestedName, $options);
$driver->attachSearchService($container->get(\VuFindSearch\Service::class));
$driver->attachLocaleSettings($container->get(LocaleSettings::class));
return $driver;
}
}
Loading