From 0f64939def162303c9a597568be60d9fd7dc725c Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Fri, 1 Sep 2023 12:30:47 +0200 Subject: [PATCH] Properly validate PIM IDs Close #20 --- src/enums/MDNodaRepository.php | 34 +++++++++++++++++++++++++++++++++- tests/MDNodaRepositoryTest.php | 8 ++++++-- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/enums/MDNodaRepository.php b/src/enums/MDNodaRepository.php index ab5220a..493570b 100644 --- a/src/enums/MDNodaRepository.php +++ b/src/enums/MDNodaRepository.php @@ -399,6 +399,38 @@ enum MDNodaRepository implements MDValueEnumInterface, JsonSerializable { } + /** + * Validates a PIM ID, returning a string or false. + * + * @param string $id ID to validate. + * + * @return string|false + */ + private static function validatePimId(string $id):string|false { + + if (filter_var($id, FILTER_VALIDATE_URL) !== false) { + $toRemove = []; + foreach ([ + 'https://opac-nevter.pim.hu/en/record/-/record/', + 'https://resolver.pim.hu/auth/', + ] as $prefix) { + $toRemove[$prefix] = ""; + } + $id = strtr($id, $toRemove); + } + + // There is an issue with this regex + if (preg_match("/^[0-9-PIM]*$/", $id) === false) { + return false; + } + if (is_numeric(substr($id, 3)) === false) { + return false; + } + + return $id; + + } + /** * Validates a wikidata ID, returning a string or false. * @@ -484,7 +516,7 @@ enum MDNodaRepository implements MDValueEnumInterface, JsonSerializable { self::oberbegriffsdatei => self::validateNumericId($id, ['https://term.museum-digital.de/oberbegriffsdatei/tag/']), self::orcid => preg_match('/^[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4}$/', $id) ? $id : false, self::osm => self::validateNumericId($id, ['https://www.openstreetmap.org/relation/']), - self::pim => self::validateNumericId($id, ['https://opac-nevter.pim.hu/en/record/-/record/']), + self::pim => self::validatePimId($id), self::pleiades => self::validateNumericId($id, ['https://pleiades.stoa.org/places/']), self::rkd => self::validateNumericId($id, ['http://rkd.nl/explore/artists/', 'https://rkd.nl/explore/artists/']), self::ulan => self::validateNumericId($id, ['http://vocab.getty.edu/ulan/', 'http://vocab.getty.edu/page/ulan/', 'https://vocab.getty.edu/page/ulan/']), diff --git a/tests/MDNodaRepositoryTest.php b/tests/MDNodaRepositoryTest.php index 0ae4c5c..8d3a3f9 100644 --- a/tests/MDNodaRepositoryTest.php +++ b/tests/MDNodaRepositoryTest.php @@ -21,6 +21,9 @@ final class MDNodaRepositoryTest extends TestCase { */ public function testValidIdsValidate():void { + self::assertEquals("XX5034943", MDNodaRepository::bne->validateId("http://datos.bne.es/persona/XX5034943")); + self::assertEquals("XX5034943", MDNodaRepository::bne->validateId("XX5034943")); + // GND (Germany) self::assertEquals("102423008", MDNodaRepository::gnd->validateId("https://d-nb.info/gnd/102423008")); self::assertEquals("102423008", MDNodaRepository::gnd->validateId("http://d-nb.info/gnd/102423008")); @@ -43,8 +46,9 @@ final class MDNodaRepositoryTest extends TestCase { self::assertEquals("sh2022014604", MDNodaRepository::lcsh->validateId("http://id.loc.gov/authorities/names/sh2022014604")); self::assertEquals("sh2022014604", MDNodaRepository::lcsh->validateId("sh2022014604")); - self::assertEquals("XX5034943", MDNodaRepository::bne->validateId("http://datos.bne.es/persona/XX5034943")); - self::assertEquals("XX5034943", MDNodaRepository::bne->validateId("XX5034943")); + self::assertEquals("PIM71684", MDNodaRepository::pim->validateId("PIM71684")); + self::assertEquals("PIM71684", MDNodaRepository::pim->validateId("https://opac-nevter.pim.hu/en/record/-/record/PIM71684")); + self::assertEquals("PIM71684", MDNodaRepository::pim->validateId("https://resolver.pim.hu/auth/PIM71684")); self::assertEquals("86145857811423020454", MDNodaRepository::viaf->validateId("86145857811423020454")); self::assertEquals("2869150688328112660005", MDNodaRepository::viaf->validateId("2869150688328112660005"));