You are here

function _biblio_ris_import in Bibliography Module 6.2

Same name and namespace in other branches
  1. 7 modules/RIS/biblio_ris.module \_biblio_ris_import()
  2. 7.2 modules/RIS/biblio_ris.module \_biblio_ris_import()
1 call to _biblio_ris_import()
biblio_ris_biblio_import in modules/RIS/biblio_ris.module

File

modules/RIS/biblio_ris.module, line 239

Code

function _biblio_ris_import($file, $terms = array(), $batch = FALSE, $session_id = NULL) {
  ini_set('auto_detect_line_endings', true);
  if (!($fp = fopen($file->filepath, "r"))) {
    drupal_set_message(t("Could not open RIS input file for reading."), 'error');
    return;
  }
  $tag = '';
  $nids = array();
  $dups = array();
  $unmapped = array();
  $node = new stdClass();
  while (!feof($fp)) {
    $line = fgets($fp);

    // Remove any character other than: carriage return, line feed, tab, or ANSI/ASCII character codes 32-255
    $line = preg_replace('/[^\\r\\n\\t\\x20-\\xFF]/', '', $line);
    $line_len = strlen($line);
    if ($line_len > 3) {
      $start = strpos($line, '  -');

      // There could be some unprintables at the beginning of the line so fine the location of the %
      if ($start !== FALSE) {
        $tag = drupal_substr($line, $start - 2, 2);
        $data = trim(drupal_substr($line, $start + 3));
      }
      else {
        $data = $line;
      }
    }
    if ($line_len > 3 && !empty($tag)) {

      // if this is not a blank line
      if ($tag == 'ER') {
        if (!empty($node)) {
          $node->biblio_ris_md5 = md5(serialize($node));
          if (empty($node->title)) {
            $node->title = t("Untitled");
          }
          if (!empty($terms)) {
            if (!isset($node->taxonomy)) {
              $node->taxonomy = array();
            }
            $node->taxonomy = array_merge($terms, $node->taxonomy);
          }
          if (!($dup = biblio_ris_check_md5($node->biblio_ris_md5))) {
            biblio_save_node($node, $batch, $session_id);
            if (!empty($node->nid)) {
              $nids[] = $node->nid;
            }
          }
          else {
            $dups[] = $dup;
          }
        }
        $node = new stdClass();
        $node->biblio_contributors = array();
      }
      else {
        _biblio_ris_parse_line($tag, $data, $node, $unmapped);
      }
    }
  }

  // end while
  fclose($fp);
  if (!empty($unmapped)) {
    $ignored_tags = array_unique($unmapped);
    $message = t("The following elements were ignored because they do not map to any biblio fields: ");
    $message .= implode(', ', $ignored_tags);
    if (user_access('administer biblio')) {
      $message .= '. ' . t('Click !url if you wish to check the field mapping', array(
        '!url' => l(t('here'), 'admin/settings/biblio/iomap/edit/ris'),
      ));
    }
    drupal_set_message($message, 'warning');
  }
  return array(
    $nids,
    $dups,
  );
}