Created
February 10, 2015 09:46
-
-
Save dmj/6c6a84213d859921effc to your computer and use it in GitHub Desktop.
Fetch records via z3950
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
#!/usr/bin/env php | |
<?php | |
function terminate ($message, $code = 1) { | |
fwrite(STDERR, $message); | |
fwrite(STDERR, "\nProgramm abgebrochen\n"); | |
exit($code); | |
} | |
if (extension_loaded('yaz') !== true) { | |
terminate("Die erforderliche PHP-Extension 'yaz' ist nicht verfügbar"); | |
} | |
$options = array( | |
'format' => 'picamarc', | |
'outfile' => 'php://stdout', | |
'z3950' => null, | |
'query' => null | |
); | |
$cmdline = array_slice($argv, 1); | |
while (!empty($cmdline)) { | |
$current = array_shift($cmdline); | |
switch ($current) { | |
case '--format': | |
case '-f': | |
$options['format'] = array_shift($cmdline); | |
break; | |
case '--outfile': | |
case '-o': | |
$options['outfile'] = array_shift($cmdline); | |
break; | |
default: | |
$options['z3950'] = $current; | |
$options['query'] = implode(' ', $cmdline); | |
break(2); | |
} | |
} | |
extract($options); | |
$handle = yaz_connect($z3950, array('persistent' => false)); | |
yaz_wait(); | |
if (!is_resource($handle)) { | |
terminate("Die Verbindung zur Z3950-Schnittstelle '{$z3950}' konnte nicht hergestellt werden"); | |
} | |
yaz_syntax($handle, $format); | |
yaz_search($handle, 'rpn', $query); | |
yaz_wait(); | |
if (yaz_errno($handle) != 0) { | |
terminate( | |
sprintf( | |
"Fehler beim Ausführen der Suchanfrage '%s' (Fehlercode %d, %s)", | |
$query, yaz_errno($handle), yaz_error($handle) | |
) | |
); | |
} | |
$hits = yaz_hits($handle); | |
echo "Schnittstelle: {$z3950}\n"; | |
echo "Suchanfrage: {$query}\n"; | |
echo "Treffer: {$hits}\n"; | |
echo "Format: {$format}\n"; | |
echo "Ausgabedatei: {$outfile}\n"; | |
$out = fopen($outfile, 'wb'); | |
for ($i = 1; $i <= $hits; $i++) { | |
$record = yaz_record($handle, $i, 'raw'); | |
if (empty($record)) { | |
terminate( | |
sprintf( | |
"Kein Datensatz an Position %d von %d", | |
$i, $hits | |
) | |
); | |
} | |
fwrite($out, $record); | |
fwrite($out, "\n"); | |
} | |
fclose($out); | |
// Local-Variables: // | |
// mode: php // | |
// End: // |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment