Index: taxonomy_xml.module =================================================================== RCS file: /cvs/drupal/contributions/modules/taxonomy_xml/taxonomy_xml.module,v retrieving revision 1.3 diff -u -F^function -r1.3 taxonomy_xml.module --- taxonomy_xml.module 5 May 2006 20:13:44 -0000 1.3 +++ taxonomy_xml.module 20 Dec 2006 09:04:30 -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 '
' . $output . "
\n"; + else { + form_set_error('xml', t('Vocabulary import failed: file was not uploaded.')); + } } /** - * taxonomy_xml_import_form + * Builds the import form. */ -function taxonomy_xml_import_form($edit) { - if (!$edit['vid']) { - $edit['vid'] = 0; - } - $vocs[0] = t(""); +function taxonomy_xml_import_form() { + $vocs[0] = t(''); foreach (module_invoke('taxonomy', 'get_vocabularies') as $vid => $voc) { $vocs[$vid] = $voc->name; } - $form["vid"] = array( + $form['vid'] = array( '#type' => 'select', - '#title' => t("Add to vocabulary"), - '#default_value' => $edit['vid'], + '#title' => t('Target vocabulary'), + '#default_value' => 0, '#options' => $vocs, - '#description' => t("The vocabulary into which terms should be loaded."), + '#description' => t('The vocabulary into which terms should be loaded.'), ); /* $form['term'] = array( @@ -174,26 +152,18 @@ function taxonomy_xml_import_form($edit) '#title' => ('Select a term from the vocabulary.'), '#description' => t("Allows a selection of a term in the vocabulary to be the parent of the imported taxonomy."), );*/ - - - $form["xml"] = array( + $form['xml'] = array( '#type' => 'file', - '#title' => t('Attach new file'), - '#size' => 50, - '#description' => t("Click \"Browse...\" to select an XML document to upload."), + '#title' => t('File to import'), + '#description' => t('Click "Browse..." to select an XML document to upload.'), ); - - $form[] = array( + $form['submit'] = array( '#type' => 'submit', - '#value' => t("load"), + '#value' => t('Import'), ); - $form['#method'] = 'POST'; - $form['#action'] = url('admin/taxonomy/import'); $form['#attributes'] = array('enctype' => 'multipart/form-data'); - - $output = drupal_get_form('taxonomy_import', $form); - - return $output; + + return $form; } /** @@ -222,20 +192,22 @@ function taxonomy_xml_create($vid, $pare $vocabulary = module_invoke('taxonomy', 'get_vocabulary', $vid); $output .= "\n"; foreach ($vocabulary as $key => $value) { - if (is_array($value)) { + if (is_array($value)) { $output .= "<$key>" . check_plain(implode(',', $value)) . ""; - } else { + } + else { $output .= "<$key>" . check_plain($value) . ""; } } foreach ($tree as $term) { - $output .= ""; + $output .= ""; foreach ($term as $key => $value) { - if ($key == 'parents') { + if ($key == 'parents') { foreach ($value as $parent) { - $output .= "" . check_plain($parent) . ""; - } - } else { + $output .= "" . check_plain($parent) . ""; + } + } + else { $output .= "<$key>" . check_plain($value) . ""; } } @@ -288,7 +260,8 @@ function taxonomy_xml_element_data($pars if (trim($data)) { $terms[$term][$tag][] = $data; } - } else { + } + else { $terms[$term][$tag] .= $data; } break; @@ -297,7 +270,7 @@ function taxonomy_xml_element_data($pars } } -function taxonomy_xml_parse(&$data, $vid, $parent_tid = NULL) { +function taxonomy_xml_parse(&$data, $vid = 0, $parent_tid = NULL) { global $terms, $vocabulary; // Unset the global variables before we use them: @@ -314,7 +287,7 @@ function taxonomy_xml_parse(&$data, $vid if (!xml_parse($xml_parser, $data, 1)) { watchdog('error', t('Taxonomy_xml: 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)))); - 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,17 +295,19 @@ 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)); } @@ -340,48 +315,46 @@ function taxonomy_xml_parse(&$data, $vid foreach ($terms as $term) { $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]; - } + 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; + $term_exists = false; $existing_terms = module_invoke('taxonomy', 'get_term_by_name', $term['name']); - if(count($existing_terms > 0)){ + if (count($existing_terms > 0)) { foreach ($existing_terms as $existing_term) { - if ($existing_term->vid == $term['vid']) { + 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 (!$term_exists){ - taxonomy_save_term($term); - $new_tid[$term['old_tid']] = $term['tid']; - $new_terms[] = $term['name']; - } + taxonomy_save_term($term); + $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 '
' . $output . "
\n"; + drupal_set_message($output); }