Hi,
I am using this module with category module.
It looks like everything works fine.
Only the depth of categories/terms and parents situated is not done as in the xml file is stated.
For every new term I have to go into the category and place the parent in category information and in menu.
Can you please help?

greetings,
Martijn

CommentFileSizeAuthor
#6 taxonomy_2.xml_.txt117.53 KBtoologic
#4 taxonomy_xml.module.txt11.7 KBtoologic

Comments

tom friedhof’s picture

I created a patch to address this for Drupal 5 at http://drupal.org/node/104402

toologic’s picture

Depth not worked after patch.

I use revision module 1.3 + taxonomy_xml_drupal5--2.patch

Not stored depth (parent field in thern_hierarchy table sets 0 for all records)

I can`t attach my patched module below for testing. sorry.

summit’s picture

Is your patch ok for 4.7.6 ?
If so please post after you test the patch, thanks in advance,
greetings,
Martijn

toologic’s picture

StatusFileSize
new11.7 KB

Sorry? but not working.

I have a next structure vocabulary in my travel project travelweekly.ru (drupal 4.7.6)
World
-continents
--countries

After export vocabulary "geo" in xml-file i see this structure that stored correctly.
Tag 'parent' for 'australia' has value 4 (as sample)

I have a another project on drupal 5.1 and i want import my vocab in it.
after import - all terms has parent- 0

Patched version attached (please delete .txt extension)

thepanz’s picture

Hi, I'm porting your patches into drupal5 (using HEAD version)...

Can you send me an XML file to recreate your tests for Importing/Exporting?

Bye

-thePanz-

toologic’s picture

StatusFileSize
new117.53 KB

Naturlisch )

Tell me, pls, if your work will be sucess

PS. delete .txt extention

toologic’s picture

Assigned: Unassigned » toologic
Status: Active » Closed (fixed)

Sorry!

Problem is solved.

I make update taxonomy_xml module on my drupal 4.7.6 site on current 4.7.x-1.x-dev version. And make export of my vocabulary again.

New export xml-file not contain LR-CR sequences on the ends tags. This is bad for human readind ;) but better for import.

Next step i import this file in my other Drupal 5.1 site by using combination (your module 4.7.x-1.x-dev + patch ....drupal--2)

All data (voca+ terms and depth, parent parameters loaded correctrly)

Thank for your great works.
And sorry again for my mistakes

summit’s picture

Status: Closed (fixed) » Postponed (maintainer needs more info)

Hi,

Can you please file your 4.7.6. version of Taxonomy_xml with correct working depth feature
It is not working for me. I get the terms, but not on the right depth.

Thanks in advance,
Martijn

summit’s picture

Hi,

Can somebody file a working patch with working Depth for 4.7?
Can you please also file a working example with the right variabels to get the depth working?

Thanks in advance,
Martijn

summit’s picture

Title: Depth not working » Depth still not working with taxonomy xml for 4.7

Hi,

This is my taxonomy_xml file for drupal 4.7.6

<?php
/* $Id: taxonomy_xml.module,v 1.6 2007/01/13 20:58:18 etopian Exp $ */

/**
    Copyright (c) 2006  Sami Khan <sami@etopian.com>
    Copyright (c) 2005  Sheldon Rampton <sheldon@prwatch.org>
                                                                                
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License.
*/

/** 
 * @file taxonomy_xml.module
 * This module makes it possible to import and export taxonomies
 * as XML documents.
 */


/**
 * Implementation of hook_help().
 */
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("You can export XML documents for each vocabulary and its terms in this website's %taxonomies. Choose the vocabulary 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 &raquo; categories &raquo; import", "admin/taxonomy/import", array(), NULL, NULL, FALSE, TRUE)));
  }
}

/** 
 * Implementation of hook_menu: Define menu links.
 * 
 * @note See hook_menu for a description of parameters and return values.
 */
function taxonomy_xml_menu($may_cache) {
  if(!module_exist('taxonomy')){
    return;
  }
  $items = array();
  if ($may_cache) {
    $items[] = array('path' => 'admin/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'),
      'access' => user_access('administer taxonomy'),
      'callback' => 'taxonomy_xml_import',
      'type' => MENU_LOCAL_TASK);
  }
  $items[] = array('path' => 'taxonomy_xml',
    'title' => t('taxonomy XML'),
    'callback' => 'taxonomy_xml_file',
    'access' => true,
    'type' => MENU_CALLBACK);
  return $items;
}

/**
 * taxonomy_xml_export
 */
function taxonomy_xml_export() {
  // return the list of vocabularies
  $output = '';
  $vocabularies = module_invoke('taxonomy', 'get_vocabularies');
  
  if(empty($vocabularies)){
    $output .= t('There are no vocabularies present');
  }
  else{
    $output .= "\n<ul>";
    foreach ($vocabularies as $vocabulary) {
      $output .= "\n\t<li>". l($vocabulary->name, "taxonomy_xml/$vocabulary->vid") . '</li>';
    }
    $output .= "\n</ul>";
  }
  return $output;
}

/**
 * taxonomy_xml_file
 */
function taxonomy_xml_file() {
  $vid = arg(1);
  
  //retriving Vocabulary name
  $vocabulary = (array) (module_invoke('taxonomy', 'get_vocabulary', $vid));
  $vname = str_replace(' ','',trim($vocabulary['name']));
  unset($vocabulary);
  
  $file = taxonomy_xml_create($vid);
  
  if (!empty($_SERVER['HTTP_USER_AGENT']) && (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 5.5') || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera'))) {
    header('Content-Type: application/dummy');
  } else {
    header('Content-Type: application/octet-stream');
  }
  if (headers_sent()) {
    echo 'Some data has already been output to browser, can\'t send file';
  }
  header('Content-Length: ' . strlen($file));
  header("Content-Disposition: attachment; filename=taxonomy_$vname.xml");
  echo $file;
}


/**
 * taxonomy_xml_import
 */
function taxonomy_xml_import() {
  $output = '';
  
  $op = $_POST["op"];
  $edit = $_POST["edit"];
  
  if (empty($op)) {
    $op = arg(1);
  }
  switch ($op) {
    case 'load': taxonomy_xml_import_load($edit);
    case 'import':
	
    default:
      $output .= taxonomy_xml_import_form($edit);
  }
  return $output;
}

/**
 * taxonomy_xml_import_load
 */
function taxonomy_xml_import_load($edit) {
  if ($file = file_check_upload(xml)) {

	if(!is_readable($file->filepath)){
		drupal_set_message(t('Vocabulary import failed: file %filename cannot be read.', array('%filename' => "<em>$file->filename</em>")),
							'error');
    }
	else {
		$text = file_get_contents($file->filepath);
		//drupal_set_message(t('Loaded file %filename.', array('%filename' => "<i>$file->filename</i>")));
		drupal_set_message(taxonomy_xml_parse($text, $edit['vid'], $edit['term']));
    }
  }
  else{
	drupal_set_message(t('Vocabulary import failed: file was not uploaded.'),'error');
  }
 
  return true;
}

/**
 * taxonomy_xml_import_form
 */
function taxonomy_xml_import_form($edit) {
  $output = '';
  if (!$edit['vid']) {
    $edit['vid'] = 0;
  }
  $vocs[0] = t("<Determined by source file>");
  foreach (module_invoke('taxonomy', 'get_vocabularies') as $vid => $voc) {
    $vocs[$vid] = $voc->name;
  }
  $form["vid"] = array(
    '#type' => 'select',
    '#title' => t("Add to vocabulary"),
    '#default_value' => $edit['vid'],
    '#options' => $vocs,
    '#description' => t("The vocabulary into which terms should be loaded."),
  );
/*
  $form['term'] = array(
    '#type' => 'checkbox',
    '#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["preserve_ids"] = array(
    '#type' => 'checkbox',
    '#title' => t("Preserve old term and vocabulary ID's"),
    '#default_value' => true,
    '#description' => t("Allow of inserting vocabularies and terms using old ID's")  
  );
  */

  $form["xml"] = array(
    '#type' => 'file',
    '#title' => t('Attach new file'),
    '#size' => 50,
    '#description' => t("Click \"Browse...\" to select an XML document to upload."),  
  );
 
  $form[] = array(
    '#type' => 'submit',
    '#value' => t("load"),
  );
  $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;
}

/**
 * taxonomy_xml_create
 * @param $vid
 *   Which vocabulary to generate the tree for.
 *
 * @param $parent
 *   The term ID under which to generate the tree. If 0, generate the tree
 *   for the entire vocabulary.
 *
 * @param $depth
 *   Internal use only.
 *
 * @param $max_depth
 *   The number of levels of the tree to return. Leave NULL to return all levels.
 *
 * @return
 *   The text of an XML document.
 */
function taxonomy_xml_create($vid, $parent = 0, $depth = -1, $max_depth = NULL) {
  $output = '<?xml version="1.0" standalone="no"?>' . "\n";
  $output .= '<!DOCTYPE taxonomy SYSTEM "taxonomy.dtd">' . "\n";
  $tree = module_invoke('taxonomy', 'get_tree', $vid, $parent, $depth, $max_depth);
  if ($tree) {
    $vocabulary = module_invoke('taxonomy', 'get_vocabulary', $vid);
    $output .= "<vocabulary>";
    foreach ($vocabulary as $key => $value) {
	  $output .= "\n\t<$key>";
	  if (is_array($value)) {
        $output .= check_plain(implode(',', $value));
      } else {
        $output .= check_plain($value);
      }
	  $output .= "</$key>";
    }
    foreach ($tree as $term) {
	  $output .= "\n\t<term>";
      foreach ($term as $key => $value) {
	    if ($key == 'parents') {
          foreach ($value as $parent) {
		    $output .= "\n\t\t<parent>" . check_plain($parent) . "</parent>";
		  }
		} else {
          $output .= "\n\t\t<$key>" . check_plain($value) . "</$key>";
        }
      }
      $output .= "\n\t</term>";
    }
    $output .= "\n</vocabulary>";
  }
  return $output;
}

/**
 * Call-back function used by the XML parser.
 */
function taxonomy_xml_element_start($parser, $name, $attributes) {
  global $term, $element, $tag;

  switch ($name) {
    case 'vocabulary':
      $element = $name;
      break;
    case 'term':
      $element = $name;
      $term += 1;
  }
  $tag = $name;
}

/**
 * Call-back function used by the XML parser.
 */
function taxonomy_xml_element_end($parser, $name) {
  global $element;

  switch ($name) {
    case 'vocabulary':
    case 'term':
      $element = '';
  }
}

/**
 * Call-back function used by the XML parser.
 */
function taxonomy_xml_element_data($parser, $data) {
  global $vocabulary, $element, $terms, $term, $tag;

  switch ($element) {
    case 'term':
      if ($tag == 'parent') {
        if (trim($data)) {
          $terms[$term][$tag][] = $data;
        }
      } else {
        $terms[$term][$tag] .= rtrim($data);
      }
      break;
    default:
      $vocabulary[$tag] .= rtrim($data);
  }
}

function taxonomy_xml_parse(&$data, $vid = 0, $parent_tid = NULL) {
  global $terms, $vocabulary;
  
  // Unset the global variables before we use them:
  unset($GLOBALS['element'], $GLOBALS['term'], $GLOBALS['tag']);
  $terms = array();
  $vocabulary = array();
  $new_terms = array();

  // parse the data:
  $xml_parser = drupal_xml_parser_create($data);
  xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false); //
  xml_set_element_handler($xml_parser, 'taxonomy_xml_element_start', 'taxonomy_xml_element_end');
  xml_set_character_data_handler($xml_parser, 'taxonomy_xml_element_data');

  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 '<p>' . 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))) . "</p>\n";
  }
  xml_parser_free($xml_parser);

  
  // 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){
    $name = $vocabulary['name'];
    $vid = db_result(db_query("SELECT vid FROM {vocabulary} WHERE LOWER('%s') LIKE LOWER(name)", trim($name)));
    if($vid){
      $vocabulary = (array) (module_invoke('taxonomy', 'get_vocabulary', $vid));
    }else{
      unset($vocabulary['vid']);
      $vocabulary['nodes'] = explode(',', $vocabulary['nodes']);
      taxonomy_save_vocabulary($vocabulary);
    }
  }else{
    $vocabulary = (array) (module_invoke('taxonomy', 'get_vocabulary', $vid));
  }

  
  foreach ($terms as $term) {
    $new_tid = array();
    $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 (!$term_exists){
        taxonomy_save_term($term);
        $new_tid[$term['old_tid']] = $term['tid'];
        $new_terms[] = $term['name'];
     }    
  }
  

  $output .= t('Vocabulary ') .  theme('placeholder', $vocabulary['name']) .': ';
  if ($new_terms) {
    $output .= t('added term(s) ') . theme('placeholder', implode(', ', $new_terms));
  } else {
    $output .= t('no terms added.');
  }
  if ($skipped_terms) {
    $output .= t('Ignored duplicate term(s) ') . theme('placeholder', implode(', ', array_keys($skipped_terms)));
  }
  drupal_set_message($output);
}

/**
 * Sorting XML to FIRST insert root nodes
 */
function _taxonomy_xml_terms_reorder($item1, $item2){
  if (isSet($item1['parent']) && !isSet($item2['parent'])){
    return 1;
  }
  else{
    // TODO: check for order of 2nd level parents...
    return -1;
  }
}


?>

Can someone please file a patch against drupal 4.7

Thanks in advance!

Greetings,
Martijn

summit’s picture

Hi,

I tried patching the 5.0 version which should work.
The code I put in was:

// 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];
}
}
}
/
but without success...
please help..

greetings,
Martijn

nicolash’s picture

Status: Postponed (maintainer needs more info) » Closed (fixed)

Please resume this discussion here if it's regarding D5...I won't resume support for D4.x.