Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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,97 @@
<?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) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this property is not defined by this trait, should we at least include a note in the header comment indicating that the trait expects the property (and recommending using it in combination with LocaleSettingsAwareTrait)?

If we're not expecting this scenario to ever happen, might it even be better to throw an exception if the dependency is missing? Otherwise, it may be hard to troubleshoot why things aren't working as expected.

return $allResults;
}
$userLocale = $this->localeSettings->getUserLocale();
if (null !== ($results = $this->getBestLocaleMatch($userLocale, $localeResults))) {
return $results;
}
// 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);
if ($results = $localeResults[$locale] ?? $localeResults[$language] ?? null) {
return $results;
}

// Check for matching language in locale-specific results:
[$language] = explode('-', $locale);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to calculate this twice.

Suggested change
[$language] = explode('-', $locale);

foreach ($localeResults as $resultLocale => $results) {
[$resultLanguage] = explode('-', $resultLocale);
if ($resultLanguage === $language) {
return $results;
}
}

return 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
Loading