Properly validate PIM IDs

Close #20
This commit is contained in:
Joshua Ramon Enslin 2023-09-01 12:30:47 +02:00
parent f79afa4ed7
commit 0f64939def
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE
2 changed files with 39 additions and 3 deletions

View File

@ -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/']),

View File

@ -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"));