-
Notifications
You must be signed in to change notification settings - Fork 393
Add qualified Dublin Core record driver. #5053
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
base: dev
Are you sure you want to change the base?
Changes from 3 commits
847dcd7
350d8d9
d007612
1c0454f
9f86355
99022a3
76dad0e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) { | ||
| 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; | ||
| } | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my earlier comment, I was suggesting including this logic as part of the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be done, If I now understood properly. :) |
||
| // 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; | ||
| } | ||
| } | ||
| 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 | ||
| */ | ||
|
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'); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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.