Created
January 6, 2026 07:17
-
-
Save dmj/86deae0c16b77d0675d30ee993711b65 to your computer and use it in GitHub Desktop.
VuFind OpenURL Serializer
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
| <?php | |
| declare(strict_types=1); | |
| namespace SUBHH\VuFind\KatalogPlus; | |
| use VuFind\RecordDriver; | |
| final class OpenUrlSerializer | |
| { | |
| /** @return array<int, string[]> */ | |
| public function createDescriptiveMetadata(RecordDriver\SolrDefault $record): array | |
| { | |
| if (array_intersect($record->getFormats(), ['Journal', 'eJournal'])) { | |
| $values = $this->createDescriptiveJournalMetadata($record); | |
| } elseif (array_intersect($record->getFormats(), ['Article', 'electronic Article'])) { | |
| $values = $this->createDescriptiveArticleMetadata($record); | |
| } else { | |
| $values = $this->createDescriptiveBookMetadata($record); | |
| } | |
| return $this->normalize($values); | |
| } | |
| /** @return string[] */ | |
| public function serialize(RecordDriver\SolrDefault $record): array | |
| { | |
| $values = array(); | |
| $values[] = 'ctx_ver=Z39.88-2004'; | |
| foreach ($this->createDescriptiveMetadata($record) as $pair) { | |
| $values[] = sprintf('%s=%s', $pair[0], rawurlencode($pair[1])); | |
| } | |
| return $values; | |
| } | |
| /** @return array<int, string[]> */ | |
| public function createDescriptiveSharedMetadata(RecordDriver\DefaultRecord $record): array | |
| { | |
| $values = array(); | |
| if ($authors = $record->getPrimaryAuthors()) { | |
| foreach ($authors as $author) { | |
| $values[] = ['rft.au', $author]; | |
| } | |
| } | |
| if ($aucorp = $record->getCorporateAuthors()) { | |
| $values[] = ['rft.aucorp', $aucorp]; | |
| } | |
| if ($dates = $record->getPublicationDates()) { | |
| foreach ($dates as $date) { | |
| if (preg_match('@^[0-9]{4}@u', $date)) { | |
| $values[] = ['rft.date', substr($date, 0, 4)]; | |
| break; | |
| } | |
| } | |
| } | |
| if ($spage = $record->getContainerStartPage()) { | |
| $values[] = ['rft.spage', $spage]; | |
| } | |
| if ($epage = $record->getContainerEndPage()) { | |
| $values[] = ['rft.epage', $epage]; | |
| } | |
| if ($spage && $epage) { | |
| $values[] = ['rft.pages', $spage . '-' . $epage]; | |
| } | |
| if ($issn = $record->getCleanISSN()) { | |
| $values[] = ['rft.issn', $issn]; | |
| } | |
| if ($isbn = $record->getCleanISBN()) { | |
| $values[] = ['rft.isbn', $isbn]; | |
| } | |
| return $values; | |
| } | |
| /** @return array<int, string[]> */ | |
| public function createDescriptiveJournalMetadata(RecordDriver\DefaultRecord $record): array | |
| { | |
| $values = $this->createDescriptiveSharedMetadata($record); | |
| $values[] = ['rft_val_fmt', 'info:ofi/fmt:kev:mtx:journal']; | |
| $values[] = ['rft.genre', 'journal']; | |
| if ($title = $record->getTitle()) { | |
| $values[] = ['rft.jtitle', $title]; | |
| } | |
| return $values; | |
| } | |
| /** @return array<int, string[]> */ | |
| public function createDescriptiveArticleMetadata(RecordDriver\DefaultRecord $record): array | |
| { | |
| $values = $this->createDescriptiveSharedMetadata($record); | |
| $values[] = ['rft_val_fmt', 'info:ofi/fmt:kev:mtx:journal']; | |
| $values[] = ['rft.genre', 'article']; | |
| if ($title = $record->getContainerTitle()) { | |
| $values[] = ['rft.jtitle', $title]; | |
| } | |
| if ($title = $record->getTitle()) { | |
| $values[] = ['rft.atitle', $title]; | |
| } | |
| if ($volume = $record->getContainerVolume()) { | |
| $values[] = ['rft.volume', $volume]; | |
| } | |
| if ($issue = $record->getContainerIssue()) { | |
| $values[] = ['rft.issue', $issue]; | |
| } | |
| return $values; | |
| } | |
| /** @return array<int, string[]> */ | |
| public function createDescriptiveBookMetadata(RecordDriver\DefaultRecord $record): array | |
| { | |
| $values = $this->createDescriptiveSharedMetadata($record); | |
| $values[] = ['rft_val_fmt', 'info:ofi/fmt:kev:mtx:book']; | |
| $values[] = ['rft.genre', 'book']; | |
| if ($title = $record->getTitle()) { | |
| $values[] = ['rft.btitle', $title]; | |
| } | |
| if ($place = $record->getRawData()['publishPlace'] ?? null) { | |
| $values[] = ['rft.place', $place]; | |
| } | |
| if ($publishers = $record->getPublishers()) { | |
| $values[] = ['rft.pub', implode(', ', $publishers)]; | |
| } | |
| if ($edition = $record->getEdition()) { | |
| $values[] = ['rft.edition', $edition]; | |
| } | |
| if ($series = $record->getSeries()) { | |
| $values[] = ['rft.series', reset($series)]; | |
| } | |
| return $values; | |
| } | |
| /** | |
| * @param mixed[] $values | |
| * @return mixed[] | |
| */ | |
| private function normalize(array $values): array | |
| { | |
| $normalizedValues = array(); | |
| foreach ($values as $pair) { | |
| if (!$pair[1]) { | |
| continue; | |
| } | |
| if (is_array($pair[1])) { | |
| foreach ($pair[1] as $value) { | |
| if ($value) { | |
| $pair[1] = $value; | |
| break; | |
| } | |
| } | |
| } | |
| $normalizedValues[] = $pair; | |
| } | |
| return $normalizedValues; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment