rebal.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: jaco
  5. * Date: 28/04/16
  6. * Time: 12.34
  7. */
  8. $doc = <<<DOC
  9. Rebal.
  10. Usage:
  11. rebal.php [<input> <output>]
  12. Options:
  13. -h --help Show this screen.
  14. --version Show version.
  15. DOC;
  16. require('src/docopt.php');
  17. require 'File/MARC.php';
  18. $args = Docopt::handle($doc, array('version'=>'RebAl-CLI 0.1'));
  19. //foreach ($args as $k=>$v)
  20. // print $k.': '.$v.PHP_EOL;
  21. $in = $args['<input>'];
  22. $out = $args['<output>'];
  23. $count = 0;
  24. // Retrieve a set of MARC records from a file
  25. $entries = new File_MARC($in);
  26. // Prepare to write the new MARC file
  27. $marc21_file = fopen($out, "wb");
  28. // Iterate through the retrieved records
  29. while ($record = $entries->next()) {
  30. // Get the UNIMARC author field and iterate through subfields
  31. // normalising the output if the Name is found without comma
  32. $lead_authors = $record->getFields('700');
  33. $sec_authors = $record->getFields('702');
  34. foreach ($lead_authors as $auth) {
  35. $a = $auth->getSubfield('a');
  36. $b = $auth->getSubfield('b');
  37. if ($b && strpos($b, ',')) {
  38. //print $b;
  39. //print "\n";
  40. $count++;
  41. } else if ($b) {
  42. $bdata = $b->getData();
  43. $b->setData(', ' . $bdata);
  44. //print $b;
  45. //print "\n";
  46. }
  47. if ($a) {
  48. $adata= $a->getData();
  49. $a->setData(ucwords(strtolower($adata)));
  50. };
  51. }
  52. foreach ($sec_authors as $auth) {
  53. $a = $auth->getSubfield('a');
  54. $b = $auth->getSubfield('b');
  55. if ($b && strpos($b, ',')) {
  56. //print $b;
  57. //print "\n";
  58. $count++;
  59. } else if ($b) {
  60. $bdata = $b->getData();
  61. $b->setData(', ' . $bdata);
  62. //print $b;
  63. //print "\n";
  64. }
  65. if ($a) {
  66. $adata= $a->getData();
  67. $a->setData(ucwords(strtolower($adata)));
  68. };
  69. }
  70. fwrite($marc21_file, $record->toRaw());
  71. //print "\n";
  72. }
  73. print $count;
  74. fclose($marc21_file);
  75. ?>