? taxonomy_xml.info ? taxonomy_xml.patch2 ? taxonomy_xml_drupal5.patch Index: taxonomy_xml.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/taxonomy_xml/taxonomy_xml.module,v retrieving revision 1.3 diff -u -p -r1.3 taxonomy_xml.module --- taxonomy_xml.module 5 May 2006 20:13:44 -0000 1.3 +++ taxonomy_xml.module 26 Jan 2007 11:30:07 -0000 @@ -25,19 +25,14 @@ */ function taxonomy_xml_help($section) { switch ($section) { - case 'admin/modules#description': - return t('Makes it possible to import and export taxonomy terms via XML.'); - case 'admin/taxonomy/import': - return t("You can upload a vocabulary and/or taxonomy terms from a properly-formatted XML document. If you want to add the terms to an existing vocabulary, ". - "use the \"add to vocabulary\" selector below. If you select \"determined by source file,\" the add to vocabulary will be specified by the XML document itself. ". - "(To avoid duplications, already-existing terms will not be added.)"); - case 'admin/taxonomy/export': - return t("XML documents for each vocabulary and its terms in this website's %taxonomies can be downloaded from the list below.", - array('%taxonomies' => l(t("taxonomies"), "admin/help/taxonomy"))); + case 'admin/content/taxonomy/import': + return t('You can upload a vocabulary and/or taxonomy terms from a properly-formatted XML document. If you want to add the terms to an existing vocabulary, use the Target vocabulary selector below. If you select Determined by source file, the target vocabulary will be specified by the XML document itself. To avoid duplications, already-existing terms will not be added.'); + case 'admin/content/taxonomy/export': + return t("XML documents for each vocabulary and its terms in this website's !taxonomies can be downloaded from the list below.", + array('!taxonomies' => l(t("taxonomies"), "admin/help/taxonomy"))); case 'admin/help#taxonomy_xml': - return t("This module makes it possible to import and export vocabularies and taxonomy terms via XML (requires taxonomy.module). ". - "Once installed and enabled, it module provides a list of downloadable XML documents for each vocabulary at %downloads. To import a vocabulary, use %upload.", - array('%downloads' => l(t("taxonomy XML"), "admin/taxonomy/export"), '%upload' => l("administer » categories » import", "admin/taxonomy/import", array(), NULL, NULL, FALSE, TRUE))); + return t("This module makes it possible to import and export vocabularies and taxonomy terms via XML (requires taxonomy.module). Once installed and enabled, it module provides a !downloads for each vocabulary. To import a vocabulary from a XML file, use !upload.", + array('!downloads' => l(t('downloadable XML document'), "admin/content/taxonomy/export"), '!upload' => l("Administer » Content management » Categories » Import", "admin/content/taxonomy/import", array(), NULL, NULL, FALSE, TRUE))); } } @@ -47,24 +42,24 @@ function taxonomy_xml_help($section) { * @note See hook_menu for a description of parameters and return values. */ function taxonomy_xml_menu($may_cache) { - if(!module_exist('taxonomy')){ + if (!module_exists('taxonomy')){ return; } $items = array(); if ($may_cache) { - $items[] = array('path' => 'admin/taxonomy/export', - 'title' => t('export'), + $items[] = array('path' => 'admin/content/taxonomy/export', + 'title' => t('Export'), 'access' => user_access('administer taxonomy'), 'callback' => 'taxonomy_xml_export', 'type' => MENU_LOCAL_TASK); - $items[] = array('path' => 'admin/taxonomy/import', - 'title' => t('import'), + $items[] = array('path' => 'admin/content/taxonomy/import', + 'title' => t('Import'), 'access' => user_access('administer taxonomy'), 'callback' => 'taxonomy_xml_import', 'type' => MENU_LOCAL_TASK); } $items[] = array('path' => 'taxonomy_xml', - 'title' => t('taxonomy XML'), + 'title' => t('Taxonomy XML'), 'callback' => 'taxonomy_xml_file', 'access' => true, 'type' => MENU_CALLBACK); @@ -93,7 +88,8 @@ function taxonomy_xml_file() { $file = taxonomy_xml_create($vid); if (!empty($HTTP_USER_AGENT) && (strpos($HTTP_USER_AGENT, 'MSIE 5.5') || strpos($HTTP_USER_AGENT, 'Opera'))) { header('Content-Type: application/dummy'); - } else { + } + else { header('Content-Type: application/octet-stream'); } if (headers_sent()) { @@ -104,69 +100,51 @@ function taxonomy_xml_file() { echo $file; } - /** - * taxonomy_xml_import + * Menu callback for the import page. */ function taxonomy_xml_import() { - - $op = $_POST["op"]; - $edit = $_POST["edit"]; - - if (empty($op)) { - $op = arg(1); - } - switch ($op) { - case 'load': - $output = taxonomy_xml_import_load($edit); - case 'import': - default: - $output .= taxonomy_xml_import_form($edit); - } - return $output; + return drupal_get_form('taxonomy_xml_import_form'); } /** - * taxonomy_xml_import_load + * Imports the actual XML. */ -function taxonomy_xml_import_load($edit) { - if ($file = file_check_upload(xml)) { +function taxonomy_xml_import_form_validate($form_id, $form_values) { + if ($file = file_check_upload('xml')) { $fd = fopen($file->filepath, "rb"); if (!$fd) { - $output = '
' . t('Vocabulary import failed: file %filename cannot be read.', array('%filename' => "$file->filename")) . "
\n"; - } else { + form_set_error('xml', t('Vocabulary import failed: file %filename cannot be read.', array('%filename' => $file->filename))); + } + else { $info = fstat($fd); $len = $info["size"]; $text = fread($fd, $len); fclose($fd); - $output = '' . t('Loaded file %filename.', array('%filename' => "$file->filename")) . "
\n"; - - $output .= taxonomy_xml_parse($text, $edit['vid'], $edit['term']); + drupal_set_message(t('Loaded file %filename.', array('%filename' => $file->filename))); + taxonomy_xml_parse($text, $form_values['vid'], $form_values['term']); } - } else { - $output = '' . t('Vocabulary import failed: file was not uploaded.') . "
\n"; } - return '' . t('Failed to parse file: %error at line %line.', array('%error' => xml_error_string(xml_get_error_code($xml_parser)), '%line' => xml_get_current_line_number($xml_parser))) . "
\n"; + drupal_set_message(t('Failed to parse file: %error at line %line.', array('%error' => xml_error_string(xml_get_error_code($xml_parser)), '%line' => xml_get_current_line_number($xml_parser))), 'error'); } xml_parser_free($xml_parser); @@ -322,66 +295,77 @@ function taxonomy_xml_parse(&$data, $vid // If an existing vocabulary has been chosen or has the same name as the vocabulary being added, // terms should be added to the existing vocabulary. Otherwise a new vocabulary should be created. - if($vid == 0){ + if ($vid == 0) { $name = $vocabulary['name']; $vid = db_result(db_query("SELECT vid FROM {vocabulary} WHERE LOWER('%s') LIKE LOWER(name)", trim($name))); - if($vid){ + if ($vid) { $vocabulary = (array) (module_invoke('taxonomy', 'get_vocabulary', $vid)); - }else{ + } + else { unset($vocabulary['vid']); $vocabulary['nodes'] = explode(',', $vocabulary['nodes']); taxonomy_save_vocabulary($vocabulary); } - }else{ + } + else { $vocabulary = (array) (module_invoke('taxonomy', 'get_vocabulary', $vid)); } - + // Get the maximum depth of terms foreach ($terms as $term) { - $term['vid'] = $vocabulary['vid']; - $term['old_tid'] = $term['tid']; - unset($term['tid']); - if (is_array($term['parent'])) { + $term_depth[] = $term['depth']; + } + // Import terms in order of depth + for ($i=0;$i<=max($term_depth);$i++) { + foreach ($terms as $term) { + if ($term['depth'] != $i) { + continue; + } + $term['vid'] = $vocabulary['vid']; + $term['old_tid'] = $term['tid']; + unset($term['tid']); + if (is_array($term['parent'])) { foreach ($term['parent'] as $key => $value) { if ($value) { $term['parent'][$key] = $new_tid[$value]; } } } - // If the term doesn't already exist in this vocabulary, add it. - $term_exists = false; - $existing_terms = module_invoke('taxonomy', 'get_term_by_name', $term['name']); - - if(count($existing_terms > 0)){ - foreach ($existing_terms as $existing_term) { - if ($existing_term->vid == $term['vid']) { - $term_exists = true; - //not quite sure what this statement does: - $new_tid[$term['old_tid']] = $existing_term->tid; - $skipped_terms[$existing_term->name] = 1; + // If the term doesn't already exist in this vocabulary, add it. + $term_exists = false; + $existing_terms = module_invoke('taxonomy', 'get_term_by_name', $term['name']); + + if (count($existing_terms > 0)) { + foreach ($existing_terms as $existing_term) { + if ($existing_term->vid == $term['vid']) { + $term_exists = true; + // Map the term tid from the imported XML file to the tid in term_data database table + $new_tid[$term['old_tid']] = $existing_term->tid; + $skipped_terms[$existing_term->name] = 1; + } } - } - - } - if (!$term_exists){ + } + if (!$term_exists){ taxonomy_save_term($term); + // Map the term tid from the imported XML file to the tid in term_data database table $new_tid[$term['old_tid']] = $term['tid']; $new_terms[] = $term['name']; - } + } + } } - $output .= '' . t('Vocabulary') . ' "' . $vocabulary['name'] . '": '; + $output .= t('Vocabulary %name: ', array('%name' => $vocabulary['name'])); if ($new_terms) { - $output .= t('added term(s)') . ' ' . implode(', ', $new_terms) . '. '; - } else { - $output .= t('no terms added. '); + $output .= t('Added term(s) %terms', array('%terms' => implode(', ', $new_terms))); + } + else { + $output .= t('No terms added.'); } if ($skipped_terms) { - $output .= t('Ignored duplicate term(s)') . ' ' . implode(', ', array_keys($skipped_terms)) . '.'; + $output .= t('Ignored duplicate term(s) %terms', array('%terms' => implode(', ', array_keys($skipped_terms)))); } - $output .= "
\n"; - return '