uid] = $user->name ." ($user->mail)"; } asort($users); $select = array ( '#type' => 'select', '#title' => t("Set user ID of entries in this file to"), '#options' => $users, '#default_value' => $my_uid, '#disabled' => (user_access('administer biblio')) ? FALSE : TRUE ); return $select; } /** * Return a form used to import files into biblio. * * @return * An array which will be used by the form builder to build the import form */ function biblio_import_form() { global $user; if (biblio_access('import')) { // && !user_access('administer nodes')) { $form['#attributes']['enctype'] = 'multipart/form-data'; $form['biblio_import_file'] = array ( '#type' => 'file', '#title' => t('Import file'), '#default_value' => '', '#size' => 60 ); $form['filetype'] = array ( '#type' => 'select', '#title' => t('File Type'), '#default_value' => 0, '#options' => array ( 'none' => t('Select type'), 'bib' => t('BibTex'), 'tagged' => t('EndNote Tagged'), 'xml' => t('EndNote 7 XML (and previous versions)'), 'xml8' => t('EndNote 8 XML (and newer versions)'), 'marc' => t('MARC'), 'ris' => t('RIS'), ) ); $form['batch_process'] = array ( '#type' => 'checkbox', '#title' => t('Batch Process'), '#default_value' => 1, '#description' => t('You should use batch processing if your import file contains more than about 20 records, or if you are experiencing script timeouts during import'), ); $form ['userid'] = _biblio_admin_build_user_select($user->uid); // Get the vocabularies attached to the biblio node type ... $vocabularies = module_invoke('taxonomy', 'get_vocabularies', 'biblio'); // ... and print a form to select the terms in each of them $form['import_taxonomy'] = array ( '#type' => 'fieldset', '#collapsible' => TRUE, '#collapsed' => TRUE, '#title' => t('Taxonomy Settings'), '#description' => t('Typically you don\'t have to do anything here, however if you wish, you may select terms to be assigned to imported records. This effectively adds a keyword to all entries being imported.')); if (count($vocabularies)) { if (variable_get('biblio_keyword_freetagging', 0)) { $freetag_vocab = $vocabularies[variable_get('biblio_keyword_vocabulary', 0)]; unset($vocabularies[variable_get('biblio_keyword_vocabulary', 0)]); $msg = t('NOTE: Keyword "free tagging" is turned on, consequently all incomming keywords will be added to the @name vocabulary as specified in the "Keyword" section of the !url page.', array ('@name' => $freetag_vocab->name, '!url' => l(t('admin/settings/biblio'), 'admin/settings/biblio'))); } else { $msg = t('NOTE: Keyword "free tagging" is turned off, consequently keywords will NOT be added to the vocabulary as specified in the Taxonomy section of the !url page.', array ('!url' => l(t('admin/settings/biblio'), 'admin/settings/biblio'))); } $i = 0; foreach ($vocabularies as $vocabulary) { $form['import_taxonomy']['vocabulary'. $i] = module_invoke('taxonomy', 'form', $vocabulary->vid, 0); $form['import_taxonomy']['vocabulary'. $i]['#weight'] = $vocabulary->weight; $form['import_taxonomy']['vocabulary'. $i++]['#description'] = t("Select taxonomy term to be assigned to imported entries"); } $form['import_taxonomy']['copy_to_biblio'] = array( '#type' => 'checkbox', '#title' => t('Copy these terms to the biblio keyword database'), '#return_value' => 1, '#default_value' => variable_get('biblio_copy_taxo_terms_to_keywords', 0), '#description' => t('If this option is selected, the selected taxonomy terms will be copied to the '.variable_get('biblio_base_title', 'Biblio').' keyword database and be displayed as keywords (as well as taxonomy terms) for this entry.') ); } else { if (module_exists('taxonomy')){ $vocab_msg = t('There are currently no vocabularies assigned to the biblio node type, please go the the !url page to fix this', array ('!url' => l(t('admin/content/taxonomy'), 'admin/content/taxonomy'))); }else{ $vocab_msg = '
' . $popup_data . ''; } /** * Import bibtex data. * * @param $data * the contents of a bibtex file passed as one big string * @param $node * an array (populated in biblio_import() ), containing the boiler plate * information common to all nodes * @return * an array of node ids */ function biblio_bibtex_import($file, $terms = array(), $batch = FALSE, $session_id = NULL, $save = TRUE, $string = FALSE) { $nids = array(); module_load_include('php', 'biblio', 'bibtexParse/PARSEENTRIES'); $bibtex = new PARSEENTRIES(); if ($string) { $bibtex->loadBibtexString($file); } else { $bibtex->openBib($file->filepath); } $bibtex->extractEntries(); if ($bibtex->count) { $nids = $bibtex->bib2node($terms, $batch, $session_id, $save); } return $nids; } function biblio_marc_import($file, $terms, $batch, $session_id) { $nids = array(); module_load_include('php', 'biblio', 'marcParse/php-marc'); $marcfile = new File($file->filepath); while ($record = $marcfile->next() ) { $node=array(); foreach($record->fields() as $fields) { foreach ($fields as $field){ switch ($field->tagno) { 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_volumne'] = $field->subfield('v'); break; case '100': case '700': $node['biblio_contributors'][1][] = array( 'name' => $field->subfield('a'), 'auth_type' => 1 ); break; case '110': case '710': $node['biblio_contributors'][5][] = array( 'name' => $field->subfield('a'), 'auth_type' => 5 ); break; } } } if (!empty($node)) { if (!empty($terms)) { if (!isset($node['taxonomy'])) $node['taxonomy'] = array(); $node['taxonomy'] = array_merge($terms,$node['taxonomy']); } $nids[] = biblio_save_node($node, $batch, $session_id); } } return $nids; } /** * Export data in bibtex format. * * @param $result * a database result set pointer * @return * none */ function biblio_bibtex_export($node) { $bibtex = ''; $type = "article"; $journal = $series = $booktitle = $school = $organization = $institution = null; $type = _bibtex_type_map($node->biblio_type); switch ($node->biblio_type) { case 100 : $series = $node->biblio_secondary_title; $organization = $node->biblio_publisher; break; case 101 : case 103 : $booktitle = $node->biblio_secondary_title; $organization = $node->biblio_publisher; $series = $node->biblio_tertiary_title; break; case 108 : $school = $node->biblio_publisher; $node->biblio_publisher = null; if (stripos($node->biblio_type_of_work, 'masters')) { $type = "mastersthesis"; } break; case 109 : $institution = $node->biblio_publisher; $node->biblio_publisher = null; break; case 102 : default: $journal = $node->biblio_secondary_title; break; } $bibtex .= '@'. $type .' {'; $bibtex .= ($node->biblio_citekey) ? $node->biblio_citekey : ""; $bibtex .= _bibtex_format_entry('title', $node->title); $bibtex .= _bibtex_format_entry('journal', $journal); $bibtex .= _bibtex_format_entry('booktitle', $booktitle); $bibtex .= _bibtex_format_entry('series', $series); $bibtex .= _bibtex_format_entry('volume', $node->biblio_volume); $bibtex .= _bibtex_format_entry('number', $node->biblio_number); $bibtex .= _bibtex_format_entry('year', $node->biblio_year); $bibtex .= _bibtex_format_entry('note', $node->biblio_notes); $bibtex .= _bibtex_format_entry('month', $node->biblio_date); $bibtex .= _bibtex_format_entry('pages', $node->biblio_pages); $bibtex .= _bibtex_format_entry('publisher', $node->biblio_publisher); $bibtex .= _bibtex_format_entry('school', $school); $bibtex .= _bibtex_format_entry('organization', $organization); $bibtex .= _bibtex_format_entry('institution', $institution); $bibtex .= _bibtex_format_entry('type', $node->biblio_type_of_work); $bibtex .= _bibtex_format_entry('edition', $node->biblio_edition); $bibtex .= _bibtex_format_entry('chapter', $node->biblio_section); $bibtex .= _bibtex_format_entry('address', $node->biblio_place_published); $bibtex .= _bibtex_format_entry('abstract', $node->biblio_abst_e); $kw_array = array(); if (!empty($node->terms)){ foreach($node->terms as $term){ $kw_array[] = $term->name; } } if (!empty($node->biblio_keywords)) { foreach($node->biblio_keywords as $term){ $kw_array[] = $term; } } if (!empty($kw_array)){ $kw_array = array_unique($kw_array); $bibtex .= _bibtex_format_entry('keywords', implode(', ', $kw_array)); } $bibtex .= _bibtex_format_entry('isbn', $node->biblio_isbn); $bibtex .= _bibtex_format_entry('issn', $node->biblio_issn); $bibtex .= _bibtex_format_entry('doi', $node->biblio_doi); $bibtex .= _bibtex_format_entry('url', $node->biblio_url); if (!empty ($node->files) && count($node->files) && user_access('view uploaded files')) { foreach($node->files as $file) { $attachments[] = file_create_url($file->filepath); } $bibtex .= _bibtex_format_entry('attachments', implode(' , ', $attachments)); } $a = $e = array(); foreach ((array)$node->biblio_contributors[1] as $auth) $a[] = trim($auth['name']); foreach ((array)$node->biblio_contributors[2] as $auth) $e[] = trim($auth['name']); $a = implode(' and ', $a); $e = implode(' and ', $e); if (!empty ($a)) $bibtex .= _bibtex_format_entry('author', $a); if (!empty ($e)) $bibtex .= _bibtex_format_entry('editor', $e); $bibtex .= "\n}\n"; //now convert any special characters to the latex equivelents... module_load_include('php', 'biblio', 'bibtexParse/PARSEENTRIES'); include(drupal_get_path('module', 'biblio') . '/bibtexParse/transtab_unicode_bibtex.inc.php'); $converter = new PARSEENTRIES(); $bibtex = $converter->searchReplaceText($transtab_unicode_bibtex, $bibtex, false); return $bibtex; } function _bibtex_format_entry($key, $value) { return !empty($value) ? ",\n\t$key = {".$value."}" : ''; } function _bibtex_type_map($bibliotype) { static $map = array(); if (empty($map)) { module_load_include('inc', 'biblio', 'biblio.type.mapper'); $map = biblio_get_type_map('bibtex'); } return ($type = array_search($bibliotype, $map)) ? $type : 'article'; } /** * Save node imported from a file. * * @param $node_array * a 2 dimensional array containing all the node information * @return * The node ids of the saved nodes */ function biblio_save_imported_nodes(& $node_array) { $dup_count = 0; if (function_exists('node_save')) { foreach ($node_array as $imp_node) { $node_ids[] = biblio_save_node($imp_node); } } /* if ($dup_count) drupal_set_message(t("Detected @dupcount duplicate node(s) when importing", array ('@dupcount' => $dup_count)), 'error'); drupal_set_message(t("Succesfully imported @count entries.", array ('@count' => count($node_ids))), 'status'); */ return $node_ids; } function biblio_save_node($node, $batch = FALSE, $session_id = NULL, $save_node = TRUE) { $options = variable_get('node_options_biblio', array ('status')); if (module_exists('i18n') && variable_get('i18n_node_biblio', 0) && variable_get('language_content_type_biblio', 0) ){ $node['language'] = module_invoke('i18n', 'default_language'); } $node_template = array ( 'type' => 'biblio', 'comment' => variable_get('comment_biblio', 0), 'promote' => in_array('promote', $options), 'moderate' => in_array('moderate', $options), 'sticky' => in_array('sticky', $options), 'format' => 0, 'status' => in_array('status', $options), ); $node = (object) array_merge($node, $node_template); if(!isset($node->biblio_type)) $node->biblio_type = 129; // default to misc if not set. if ($batch && $session_id){ // we are batch processing some import data $node = serialize($node); db_query("INSERT INTO {biblio_import_cache} (session_id, data) VALUES ('%s', %b)", $session_id, $node); return; } elseif ($save_node) { // $save_node = TRUE, the normal save path node_save($node); return (isset($node->nid)) ? $node->nid : 0; } else { // $save_node = FALSE, primarily used to parse data and return it to the input form return (array)$node; } } function biblio_crossref_xml_import($doi, $terms = array(), $batch = FALSE, $session_id = NULL, $save = FALSE) { global $user, $node, $save_node, $nids; if (!isset($user->biblio_crossref_pid) || empty($user->biblio_crossref_pid) ) return FALSE; $save_node = $save; $nids = array(); $url = 'http://www.crossref.org/openurl/?pid='. $user->biblio_crossref_pid .'&noredirect=true&format=unixref&id=doi%3A'. $doi; if (!($fp = fopen($url, "r"))) { drupal_set_message(t('Could not open crossref.org for XML input'),'error'); return; } $xml = fread($fp, 2048); $xml_parser = drupal_xml_parser_create($xml); // use case-folding so we are sure to find the tag in xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false); xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, true); module_load_include('inc', 'biblio', 'crossref_unixref_parser'); xml_set_element_handler($xml_parser, 'unixref_startElement', 'unixref_endElement'); xml_set_character_data_handler($xml_parser, 'unixref_characterData'); xml_parse($xml_parser, $xml); while ($xml = fread($fp, 2048)){ set_time_limit(30); if(!xml_parse($xml_parser, $xml, feof($fp))){ drupal_set_message(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)),'error'); } } xml_parser_free($xml_parser); fclose($fp); return (!empty($nids)) ? $nids : array(); } /** * Import EndNote XML data. * * @param $data * the contents of an EndNote XML file passed as one big string * @param $node * boiler plate information common to all nodes * @param $version * the EndNote version of the XML file. EndNote uses one format up to version * 7 then change to another format in version 8 and greater. * @return * The node ids of the saved nodes */ function biblio_endnote_XML_import($xml_file, $taxo_terms = array(), $batch_proc = FALSE, $session_id = NULL, $ver = 8) { global $user, $records, $rec_count, $node, $terms, $batch_proc, $nids, $session_id; $terms = $taxo_terms; $nids = array(); if (!($fp = fopen($xml_file->filepath, "r"))) { drupal_set_message("could not open XML input",'error'); return; } $data = fread($fp, 2048); $xml_parser = drupal_xml_parser_create($data); // use case-folding so we are sure to find the tag in xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false); xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, true); module_load_include('inc', 'biblio', 'endnote'.$ver.'_parser'); xml_set_element_handler($xml_parser, 'en'.$ver.'_startElement', 'en'.$ver.'_endElement'); xml_set_character_data_handler($xml_parser, 'en'.$ver.'_characterData'); xml_parse($xml_parser, $data, feof($fp)); while ($data = fread($fp, 2048)){ // $data = fread($fp, 2048); set_time_limit(30); if(!xml_parse($xml_parser, $data, feof($fp))){ drupal_set_message(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)),'error'); } } xml_parser_free($xml_parser); fclose($fp); return (!empty($nids)) ? $nids : array(); } /** * Export data in EndNote XML format. * * @param $result * a database pointer to a result set * @param $version * the EndNote version of the XML file. EndNote uses one format up to version * 7 then change to another format in version 8 and greater. * @return * none */ function biblio_endnote_XML_export($result, $version = 7) { if ($version == 8) { module_load_include('inc', 'biblio', 'endnote8_export'); $xml = _endnote8_XML_export($result); } elseif ($version == 7) { module_load_include('inc', 'biblio', 'endnote7_export'); $xml = _endnote7_XML_export($result); } return $xml; } /** * Export data in EndNote tagged format. * * @param $result * a database pointer to a result set * @return * none */ function biblio_endnote_tagged_export($node) { $tagged = ""; $tagged .= "%0 ". _endnote_tagged_type_map($node->biblio_type) ."\r\n"; switch ($node->biblio_type) { case 100 : case 101 : case 103 : case 104 : case 105 : case 108 : case 119 : if (!empty($node->biblio_secondary_title)) $tagged .= "%B ". trim($node->biblio_secondary_title) ."\r\n"; break; case 102 : if (!empty($node->biblio_secondary_title)) $tagged .= "%J ". trim($node->biblio_secondary_title) ."\r\n"; break; // journal } if (isset($node->biblio_year) && $node->biblio_year < 9998) $tagged .= "%D ". trim($node->biblio_year) ."\r\n"; if (!empty($node->title)) $tagged .= "%T ". trim($node->title) ."\r\n"; foreach ((array)$node->biblio_contributors[1] as $auth) { $tagged .= "%A " . trim($auth['name']) ."\r\n"; } foreach ((array)$node->biblio_contributors[2] as $auth) { $tagged .= "%E " . trim($auth['name']) ."\r\n"; } foreach ((array)$node->biblio_contributors[3] as $auth) { $tagged .= "%Y " . trim($auth['name']) ."\r\n"; } if (!empty($node->biblio_place_published)) $tagged .= "%C ". trim($node->biblio_place_published) ."\r\n"; if (!empty($node->biblio_publisher)) { $tagged .= "%I ". trim($node->biblio_publisher) ."\r\n"; } $kw_array = array(); if (!empty($node->terms)){ foreach($node->terms as $term){ $kw_array[] = $term->name; } } if (!empty($node->biblio_keywords)) { foreach($node->biblio_keywords as $term){ $kw_array[] = $term; } } if (!empty($kw_array)){ $kw_array = array_unique($kw_array); foreach($kw_array as $term){ $tagged .= "%K ". trim($term) ."\r\n"; } } if (!empty($node->biblio_call_number)) $tagged .= "%L ". trim($node->biblio_call_number) ."\r\n"; if (!empty($node->biblio_accession_number)) $tagged .= "%M ". trim($node->biblio_accession_number) ."\r\n"; if (!empty($node->biblio_issue)) $tagged .= "%N ". trim($node->biblio_issue) ."\r\n"; if (!empty($node->biblio_pages)) $tagged .= "%P ". trim($node->biblio_pages) ."\r\n"; if (!empty($node->biblio_doi)) $tagged .= "%R ". trim($node->biblio_doi) ."\r\n"; if (!empty($node->biblio_tertiary_title)) $tagged .= "%S ". trim($node->biblio_tertiary_title) ."\r\n"; if (!empty($node->biblio_url)) $tagged .= "%U ". trim($node->biblio_url) ."\r\n"; if (!empty($node->biblio_volume)) $tagged .= "%V ". trim($node->biblio_volume) ."\r\n"; $abst = ""; if (!empty($node->biblio_abst_e)) $abst .= trim($node->biblio_abst_e); if (!empty($node->biblio_abst_f)) $abst .= trim($node->biblio_abst_f); if ($abst) { $search = array("/\r/", "/\n/"); $replace = " "; $abst = preg_replace($search, $replace, $abst); $tagged .= "%X ". $abst ."\r\n"; } if (!empty($node->biblio_notes)) $tagged .= "%Z ". trim($node->biblio_notes) ."\r\n"; if (!empty($node->biblio_edition)) $tagged .= "%7 ". trim($node->biblio_edition) ."\r\n"; if (!empty($node->biblio_date)) $tagged .= "%8 ". trim($node->biblio_date) ."\r\n"; if (!empty($node->biblio_type_of_work)) $tagged .= "%9 ". trim($node->biblio_type_of_work) ."\r\n"; if (!empty($node->biblio_isbn)) $tagged .= "%@ ". trim($node->biblio_isbn) ."\r\n"; if (!empty ($node->files) && count($node->files) && user_access('view uploaded files')) { foreach($node->files as $file) { $tagged .= "%> ". file_create_url($file->filepath) . "\r\n"; // insert file here. } } $tagged .= "\r\n"; return $tagged; } function _endnote_tagged_type_map($bibliotype) { static $map = array(); if (empty($map)) { module_load_include('inc', 'biblio', 'biblio.type.mapper'); $map = biblio_get_type_map('tagged'); } return ($type = array_search($bibliotype, $map)) ? $type : 'Generic'; //return the biblio type or 129 (Misc) if type not found } function biblio_csv_export_2($result, $bfields) { // $query_biblio_fields = 'SELECT name, title FROM {biblio_fields}'; // $res_biblio_fields = db_query($query_biblio_fields); // while ($rec = db_fetch_object($res_biblio_fields)){ // $bfields[$rec->name] = $rec->title; // } $bfields = biblio_get_db_fields('all'); $query_biblio_types = 'SELECT tid, name FROM {biblio_types}'; $res_biblio_types = db_query($query_biblio_types); while ($rec = db_fetch_object($res_biblio_types)) { $btypes[$rec->tid] = $rec->name; } switch (variable_get('biblio_csv_field_sep', 'tab')) { case 'tab' : $filedsep = "\t"; break; case 'comma' : $filedsep = ','; break; } switch (variable_get('biblio_csv_text_sep', 'dquote')) { case 'dquote' : $textsep = '"'; break; case 'quote' : $textsep = '\''; break; } $label = (variable_get('biblio_csv_col_head', 'label') == 'label' ? 1 : 0); // or 'col_name' $linebreak = variable_get('biblio_linebreak_exp', 1); while ($rec = db_fetch_object($result)) { $node_id = $rec->nid; $node_array[$node_id]['type'] = $btypes[$rec->biblio_type]; // there is no "label" for "type" $col_array['type'] = 'Type'; foreach (array_keys($bfields) as $fieldname) { if (!empty ($rec-> $fieldname) && !in_array($fieldname, array ( 'biblio_citekey', 'biblio_coins' ))) { $col_array[$fieldname] = $bfields[$fieldname]; // mark field as in use $text = strtr($rec-> $fieldname, $textsep, "$textsep$textsep"); if ($linebreak) { $text = strtr($text, ';', "\n"); } $node_array[$node_id][$fieldname] = trim($text); } } } //end while if ($label) { // head line containing column names $csv = $textsep . join("$textsep$filedsep$textsep", array_values($col_array)) ."$textsep\n"; } else { // original DB field names $csv = $textsep . join("$textsep$filedsep$textsep", array_keys($col_array)) ."$textsep\n"; } // Enclosing text in "