-
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
Draft
EreMaijala
wants to merge
7
commits into
vufind-org:dev
Choose a base branch
from
EreMaijala:dev-qdc
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
847dcd7
Add initial support for qualified Dublin Core records.
EreMaijala 350d8d9
Tweaks,
EreMaijala d007612
Move locale methods to a trait.
EreMaijala 1c0454f
Merge remote-tracking branch 'origin/dev' into dev-qdc
EreMaijala 9f86355
Switch to LocaleSettingsAwareInterface/Trait.
EreMaijala 99022a3
Tweak locale matching.
EreMaijala 76dad0e
Fix code style.
EreMaijala File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
97 changes: 97 additions & 0 deletions
97
module/VuFind/src/VuFind/RecordDriver/Feature/LocaleSupportTrait.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { | ||||
| 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); | ||||
|
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. We don't need to calculate this twice.
Suggested change
|
||||
| foreach ($localeResults as $resultLocale => $results) { | ||||
| [$resultLanguage] = explode('-', $resultLocale); | ||||
| if ($resultLanguage === $language) { | ||||
| return $results; | ||||
| } | ||||
| } | ||||
|
|
||||
| return null; | ||||
| } | ||||
| } | ||||
97 changes: 97 additions & 0 deletions
97
module/VuFind/src/VuFind/RecordDriver/Feature/XmlTrait.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.