'. t('Use this form to import taxonomy terms into a vocabulary from a CSV file.'). '

'. t('Warning: If you want to update an existing vocabulary, make sure you have a backup before you proceed so you can roll back, if necessary.'). theme('more_help_link', url('admin/help/taxonomy_csv')); case 'admin/help#taxonomy_csv': $output = '

'. t('This module allows you to import taxonomy terms into a vocabulary from a CSV file.

The term name will be imported from the first column. You can specify how additional columns should be imported:', array('!import-url' => url('admin/content/taxonomy/csv'))) .'

'; $output .= '
'. t('Ignore'). '
'. t('This has the same effect has having a single column.'). '
'; $output .= '
'. t('Term description, term synonyms'). '
'. t('The second column will be imported as the term description and any additional columns will be imported as term synonyms. Either or both may be empty.'). '
'; $output .= '
'. t('Child term names'). '
'. t('The second column will be imported as the name of a child term of the term defined by the first column. The third column will be imported as a child of the second column, and so on. For example, you might have a line Animal,Mammal,Dog.'). '
'; $output .= '
'. t('Related terms'). '
'. t('The second and next columns will be imported as related terms of the first column term. For example, a line may be: Thesaurus,Taxonomy,Ontology.'). '
'; $output .= '

'. t('If you want to import child term names as well as descriptions and synonyms for each term, you will need to do this in two steps. First, upload a file containing the parent and child term names with the Child term names option. Second, upload a file containing descriptions and synonyms for each term with the Term description, term synonyms option, while making sure to enable the Update if name matches option.'). '

'; $output .= '

'. t('If you want to import child term names as well as related terms, first import child term names as above and second upload a file containing related terms with Related terms and Update if name matches options.'). '

'; $output .= '

'. t('Be careful with the Update option: import of synonyms and related terms to a term currently implies lost of existing synonyms and related terms of this term. It\'s not a problem when the vocabulary is a new one.'). '

'; $output .= '

'. t('It is recommended to protect terms with quotation marks ("), specialy if they contain non-ASCII letters: "term 1","term 2","term 3".'). '

'; $output .= '

'. t('If you are unsure how to create a CSV file, you might want to use OpenOffice Calc or another spreadsheet application to export your data into a CSV file.'). '

'; return $output; } } /** * Implementation of hook_menu(). */ function taxonomy_csv_menu() { $items = array(); $items['admin/content/taxonomy/csv'] = array( 'title' => t('CSV import'), 'page callback' => 'drupal_get_form', 'page arguments' => array('taxonomy_csv_import'), 'access arguments' => array(t('administer taxonomy')), 'weight' => 12, 'type' => MENU_LOCAL_TASK, ); return $items; } /** * Generates the CSV import form. */ function taxonomy_csv_import() { $form = array('#attributes' => array('enctype' => 'multipart/form-data')); $form['source'] = array('#type' => 'fieldset', '#title' => t('Source')); $form['source']['upload'] = array( '#type' => 'file', '#title' => t('CSV file'), ); if ($max_size = _taxonomy_csv_parse_size(ini_get('upload_max_filesize'))) { $form['source']['upload']['#description'] = t('Due to server restrictions, the maximum upload file size is !size. Files that exceed this size will be disregarded without notice.', array('!size' => format_size($max_size))); } $form['source']['delimiter'] = array( '#type' => 'select', '#title' => t('CSV file delimiter'), '#options' => array( TAXONOMY_CSV_COMMA => t('Comma – default'), TAXONOMY_CSV_SEMICOLON => t('Semicolon – Microsoft Excel'), ), '#description' => t('Choose the delimiter used in the CSV file you want to import.'), ); $form['source']['columns'] = array( '#type' => 'radios', '#title' => t('Additional columns'), '#options' => array( TAXONOMY_CSV_IGNORE => t('Ignore'), TAXONOMY_CSV_FIELDS => t('Term description, term synonyms (may be empty)'), TAXONOMY_CSV_CHILDREN => t('Child term names'), TAXONOMY_CSV_RELATED => t('Related terms'), ), '#default_value' => TAXONOMY_CSV_IGNORE, '#description' => t('The first column is always imported as the term name. This option determines how additional columns will be imported. If your CSV file only contains one column, this option will be ignored.'), ); $form['dest'] = array('#type' => 'fieldset', '#title' => t('Destination')); $form['dest']['vid'] = array( '#type' => 'select', '#title' => t('Vocabulary'), '#options' => array(), '#required' => TRUE, '#description' => t('The vocabulary you want to import the file into. You might want to !add-new-vocab.', array('!add-new-vocab' => l(t('add a new vocabulary'), 'admin/content/taxonomy/add/vocabulary', array('query' => drupal_get_destination())))), ); $vocabularies = taxonomy_get_vocabularies(); foreach ($vocabularies as $vid => $vocabulary) { $form['dest']['vid']['#options'][$vid] = $vocabulary->name; } $form['dest']['update'] = array( '#type' => 'radios', '#title' => t('Existing terms'), '#options' => array( FALSE => t('Ignore'), TRUE => t('Update if name matches'), ), '#default_value' => TRUE, '#description' => t('Whether existing terms with the same name should be re–created or updated.'), ); $form['submit'] = array('#type' => 'submit', '#value' => t('Import')); return $form; } /** * Parses PHP configuration size values into bytes. * * Edited from an example at http://php.net/manual/en/function.ini-get.php */ function _taxonomy_csv_parse_size($value) { $value = trim($value); $number = (int)substr($value, 0, -1); $suffix = strtoupper(substr($value, -1)); switch ($suffix) { case 'G': $number *= 1024; case 'M': $number *= 1024; case 'K': $number *= 1024; } return $number; } /** * Handles CSV import form validation. */ function taxonomy_csv_import_validate($form, &$form_state) { $form_state['upload_file'] = file_save_upload('upload'); if (!$form_state['upload_file']) { form_set_error('upload', t('Please upload a file.')); } } /** * Handles CSV import form submission. */ function taxonomy_csv_import_submit($form, &$form_state) { $options = $form_state['values']; $file = $form_state['upload_file']; if ($options['delimiter'] == TAXONOMY_CSV_SEMICOLON) { $delimiter = ';'; } else { $delimiter = ','; } $options['vocabulary'] = taxonomy_vocabulary_load($options['vid']); // Automatically detect line endings. ini_set('auto_detect_line_endings', '1'); $handle = fopen($file->filepath, 'r'); $batch = array( 'operations' => array(), 'finished' => 'taxonomy_csv_import_finished', 'title' => t('Importing terms from CSV file'), 'init_message' => t('Starting import...'), 'progress_message' => t('Imported @current out of @total lines.'), 'error_message' => t('An error occurred during the import.'), ); $first = TRUE; while ($line = fgetcsv($handle, 4096, $delimiter)) { if (empty($line) || (count($line) == 1 && $line[0] == NULL)) continue; // Skip UTF-8 byte order mark. if ($first) { if (strncmp($line[0], "\xEF\xBB\xBF", 3) === 0) $line[0] = substr($line[0], 3); $first = FALSE; } // Encode the line in base64 to prevent batch errors for weird encodings. $batch['operations'][] = array('taxonomy_csv_import_line', array(array_map('base64_encode', $line), $options)); } fclose($handle); batch_set($batch); } /** * Callback for finished batch import. */ function taxonomy_csv_import_finished($success, $results, $operations) { drupal_set_message(t('The CSV file has been imported.')); } function taxonomy_csv_import_line($line, $options) { // Decode the line. $line = array_map('base64_decode', $line); // Convert line to UTF-8. $line = array_map('_taxonomy_csv_import_line_to_utf8', $line); switch ($options['columns']) { case TAXONOMY_CSV_CHILDREN : $parent = 0; for ($c = 0; $c < count($line); $c++) { if (empty($line[$c])) break; $term = array( 'name' => $line[$c], 'vid' => $options['vid'], 'parent' => $parent, ); // Parent terms (so all terms but the last on this line) are always updated. $term = taxonomy_csv_import_term($term, $options['update'] || $c < count($line) - 1); $parent = $term['tid']; } break; case TAXONOMY_CSV_FIELDS : if (!empty($line[0])) { $term = array( 'name' => $line[0], 'vid' => $options['vid'], ); if (count($line) > 1 && !empty($line[1])) { $term['description'] = $line[1]; } if (count($line) > 2) { $term['synonyms'] = implode("\n", array_filter(array_slice($line, 2))); } taxonomy_csv_import_term($term, $options['update']); } break; case TAXONOMY_CSV_RELATED : if (!empty($line[0])) { $term = array( 'name' => $line[0], 'vid' => $options['vid'], ); // each related term must exist before it can be related (need of its tid) for ($c = 1; $c < count($line); $c++) { if (empty($line[$c])) break; $related_term = array( 'name' => $line[$c], 'vid' => $options['vid'], ); $related_term = taxonomy_csv_import_term($related_term, $options['update']); $term['relations'][$c - 1] = $related_term['tid']; } taxonomy_csv_import_term($term, $options['update']); } break; default : if (!empty($line[0])) { $term = array( 'name' => $line[0], 'vid' => $options['vid'], ); taxonomy_csv_import_term($term, $options['update']); } } } /** * Helper function to convert each line item to UTF-8. */ function _taxonomy_csv_import_line_to_utf8($value) { $enc = mb_detect_encoding($value, "UTF-8, ISO-8859-1, ISO-8859-15", TRUE); if ($enc != "UTF-8") { $value = drupal_convert_to_utf8($value, $enc); } return $value; } /** * Get or create a term with the given name in the given vocabulary and given parent. */ function taxonomy_csv_import_term($term, $update = TRUE) { if ($update && ($existing_term = taxonomy_csv_find_term($term['name'], $term['vid'], isset($term['parent']) ? $term['parent'] : NULL))) { foreach (array('description', 'synonyms', 'relations') as $key) { // Existing synonyms and relations are lost (see taxonomy_save_term too)! if (array_key_exists($key, $term)) $existing_term[$key] = $term[$key]; } $term = $existing_term; } taxonomy_save_term($term); return $term; } /** * Find, by its name, the first existing term in a given vocabulary and a given parent. */ function taxonomy_csv_find_term($name, $vid, $parent = NULL) { static $cache = array(); $name = drupal_strtolower(trim($name)); $key = $name .'_'. $vid; if (!is_null($parent)) { $key .= '_'. $parent; } if (isset($cache[$key])) { return $cache[$key]; } else { $sql = "SELECT t.tid, t.*, h.parent FROM {term_data} t INNER JOIN {term_hierarchy} h ON t.tid = h.tid WHERE '%s' LIKE LOWER(t.name) AND t.vid = %d "; $args = array($name, $vid); if (!is_null($parent)) { $sql .= "AND h.parent = %d "; $args[] = $parent; } $sql .= "LIMIT 1 "; $result = db_query($sql, $args); $term = db_fetch_array($result); if ($term) { $cache[$key] = $term; if (is_null($parent)) { $cache[$key .'_'. $term['parent']] = $term; } } } return $term; } ?>