From b07400bdb7190ec29f4dabb58353a8be284898ee Mon Sep 17 00:00:00 2001 From: Matija Kancijan Date: Thu, 14 Jul 2022 11:01:13 +0200 Subject: [PATCH] feat: add listsets callback method feat: add set arg for listRecords method --- index.php | 65 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 56 insertions(+), 9 deletions(-) diff --git a/index.php b/index.php index ca33fdb..71949cb 100644 --- a/index.php +++ b/index.php @@ -30,10 +30,11 @@ require __DIR__.'/vendor/autoload.php'; require __DIR__.'/Configuration/Main.php'; // Get all available records and their respective status and timestamps -$records = []; -$deleted = []; -$timestamps = []; -$earliest = time(); +$records = []; +$setsrecords = []; +$deleted = []; +$timestamps = []; +$earliest = time(); foreach ($config['metadataPrefix'] as $prefix => $uris) { $files = glob(rtrim($config['dataDirectory'], '/').'/'.$prefix.'/*.xml'); @@ -45,10 +46,36 @@ foreach ($config['metadataPrefix'] as $prefix => $uris) { $earliest = filemtime($file); } } - ksort($records[$prefix]); - reset($records[$prefix]); - ksort($timestamps[$prefix]); - reset($timestamps[$prefix]); + if (isset($records[$prefix])) { + ksort($records[$prefix]); + reset($records[$prefix]); + ksort($timestamps[$prefix]); + reset($timestamps[$prefix]); + } +} + +// get all set records. +foreach ($config['ListSets'] as $set => $setd) { + foreach ($config['metadataPrefix'] as $prefix => $uris) { + $path = rtrim($config['dataDirectory'], '/').'/sets/'.$set.'/'.$prefix.'/*.xml'; + $files = glob(rtrim($config['dataDirectory'], '/').'/sets/'.$set.'/'.$prefix.'/*.xml'); + + foreach ($files as $file) { + $setsrecords[$set]['records'][$prefix][pathinfo($file, PATHINFO_FILENAME)] = $file; + $setsrecords[$set]['deleted'][$prefix][pathinfo($file, PATHINFO_FILENAME)] = !filesize($file); + $setsrecords[$set]['timestamps'][$prefix][filemtime($file)][] = pathinfo($file, PATHINFO_FILENAME); + if (filemtime($file) < $earliest) { + $earliest = filemtime($file); + } + } + + if (isset($setsrecords[$set]['records'][$prefix])) { + ksort($setsrecords[$set]['records'][$prefix]); + reset($setsrecords[$set]['records'][$prefix]); + ksort($setsrecords[$set]['timestamps'][$prefix]); + reset($setsrecords[$set]['timestamps'][$prefix]); + } + } } // Get current base URL @@ -88,7 +115,7 @@ $oai2 = new Server( ]; } }, - 'ListRecords' => function ($metadataPrefix, $from = null, $until = null, $count = false, $deliveredRecords = 0, $maxItems = 100) { + 'ListRecords' => function ($metadataPrefix, $from = null, $until = null, $count = false, $deliveredRecords = 0, $maxItems = 100, $set = null) { global $records, $deleted, $timestamps; $resultSet = []; foreach ($timestamps[$metadataPrefix] as $timestamp => $identifiers) { @@ -126,6 +153,26 @@ $oai2 = new Server( } else { return $config['metadataPrefix']; } + }, + 'ListSets' => function ($identifier = '') { + global $config, $records, $setsrecords; + if (!empty($identifier)) { + $listsets = []; + foreach ($setsrecords as $set => $records) { + foreach ($records as $format => $record) { + if (!empty($record[$identifier])) { + $listsets[$set] = $config['ListSets'][$set]; + } + } + } + if (!empty($listsets)) { + return $listsets; + } else { + throw new Exception('idDoesNotExist'); + } + } else { + return $config['ListSets']; + } } ], $config