You are here

biblio_marc.module in Bibliography Module 6.2

File

modules/marcParse/biblio_marc.module
View source
<?php

/*
 * @file biblio_marc.module
 *
 */

/*
 *   add the marc option to the option list of the biblio_import_form
 *   the key is the module name use by module_invoke to call hook_biblio_import
 *   module_invoke('biblio_marc', 'biblio_import',...)
 */
function biblio_marc_biblio_import_options() {
  return array(
    'biblio_marc' => t('MARC'),
  );
}
function biblio_marc_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  if ($node->type != 'biblio') {
    return;
  }
  $callback = '_biblio_marc_' . str_replace(' ', '_', $op);
  if (function_exists($callback)) {
    return $callback($node, $a3, $a4);
  }
  return;
}
function _biblio_marc_delete($node) {
  db_query('DELETE FROM {biblio_marc} WHERE nid = %d', $node->nid);
}
function _biblio_marc_view($node) {
}
function _biblio_marc_insert($node) {
  if (!isset($node->biblio_marc_md5)) {
    return;
  }
  drupal_write_record('biblio_marc', $node);
}
function biblio_marc_biblio_import($file, $terms = array(), $batch = FALSE, $session_id = NULL) {
  $nids = array();
  $dups = array();
  module_load_include('php', 'biblio_marc', 'php-marc');
  $marcfile = new File($file->filepath);
  while ($record = $marcfile
    ->next()) {
    $node = new stdClass();
    $node->biblio_contributors = array();
    $fields = $record
      ->fields();
    $leader = $record
      ->leader();
    $pubtype = $leader[6];
    $pubtype .= $leader[7];
    $node->biblio_type = _biblio_marc_type_map($pubtype);
    foreach ($record
      ->fields() as $fields) {
      foreach ($fields as $field) {
        $tagnum = $field->tagno;
        switch ($tagnum) {
          case '008':
            $data = $field
              ->data();
            $node->biblio_year = substr($data, 7, 4);
            $node->biblio_lang = substr($data, 35, 3);
            break;
          case '020':
            $node->biblio_isbn = $field
              ->subfield('a');
            break;
          case '022':
            $node->biblio_issn = $field
              ->subfield('a');
            break;
          case '024':
            $node->biblio_other_number = $field
              ->subfield('a');
            break;
          case '050':

          //LIBRARY OF CONGRESS CALL NUMBER
          case '055':

          //CLASSIFICATION NUMBERS ASSIGNED IN CANADA
          case '060':

            //NATIONAL LIBRARY OF MEDICINE CALL NUMBER
            $node->biblio_call_number = $field
              ->subfield('a');
            break;
          case '130':
            $node->title = str_replace(' /', '', $field
              ->subfield('a'));
            break;
          case '210':
            $node->biblio_short_title = str_replace(' /', '', $field
              ->subfield('a'));
            break;
          case '245':
            $node->title = str_replace(' /', '', $field
              ->subfield('a')) . ' ' . $field
              ->subfield('b');
            break;
          case '250':
            $node->biblio_edition = $field
              ->subfield('a');
            break;
          case '260':
            $node->biblio_place_published = str_replace(' :', '', $field
              ->subfield('a'));
            $node->biblio_publisher = $field
              ->subfield('b');
            $node->biblio_date = $field
              ->subfield('c');
            break;
          case '300':
            $node->biblio_pages = $field
              ->subfield('a');
            break;
          case '490':
            $node->biblio_volume = $field
              ->subfield('v');
            break;
          case $tagnum >= 500 && $tagnum <= 599:
            $value = $field
              ->subfield('a');
            if (!empty($value)) {
              $node->biblio_notes .= $value;
            }
            break;
          case '650':
            foreach ($field
              ->subfields() as $subject) {
              $node->biblio_keywords[] = $subject[0];
            }
            break;
          case '100':
          case '700':
            $values = $field
              ->subfield('a', TRUE);
            foreach ($values as $value) {
              $node->biblio_contributors[1][] = array(
                'name' => $value,
                'auth_type' => 1,
              );
            }
            break;
          case '110':
          case '710':
            $node->biblio_contributors[5][] = array(
              'name' => $field
                ->subfield('a'),
              'auth_type' => 5,
            );
            break;
          case '856':
            $value = $field
              ->subfield('u');
            if (!empty($value)) {
              $node->biblio_url = $value;
            }
            break;
        }
      }
    }
    if (!empty($node)) {
      $node->biblio_marc_md5 = md5(serialize($node));
      if (!empty($terms)) {
        if (!isset($node->taxonomy)) {
          $node->taxonomy = array();
        }
        $node->taxonomy = array_merge($terms, $node->taxonomy);
      }
      if (!($dup = biblio_marc_check_md5($node->biblio_marc_md5))) {
        biblio_save_node($node, $batch, $session_id);
        if (!empty($node->nid)) {
          $nids[] = $node->nid;
        }
      }
      else {
        $dups[] = $dup;
      }
    }
  }
  return array(
    $nids,
    $dups,
  );
}
function biblio_marc_check_md5($md5) {
  static $marc_md5s = array();
  if (empty($marc_md5s)) {
    $result = db_query("SELECT * FROM {biblio_marc} ");
    while ($row = db_fetch_object($result)) {
      $marc_md5s[$row->biblio_marc_md5] = $row->nid;
    }
  }
  if (isset($marc_md5s[$md5])) {
    return $marc_md5s[$md5];
  }
  else {
    $marc_md5s[$md5] = TRUE;

    // gaurd against duplicates in the same import
    return;
  }
}
function _biblio_marc_type_map($type, $reverse = FALSE) {
  static $map = array();
  if (empty($map)) {
    $map = unserialize(db_result(db_query("SELECT type_map FROM {biblio_type_maps} WHERE format='marc'")));
  }
  if ($reverse) {
    return ($tag = array_search($type, $map)) ? $tag : 'Generic';

    //return the biblio type or 129 (Misc) if type not found
  }
  return isset($map[$type]) ? $map[$type] : 129;

  //return the biblio type or 129 (Misc) if type not found
}