Index: taxonomy_xml/taxonomy_xml.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/taxonomy_xml/taxonomy_xml.module,v
retrieving revision 1.7
diff -u -u -p -r1.7 taxonomy_xml.module
--- taxonomy_xml/taxonomy_xml.module	3 Jul 2007 06:59:32 -0000	1.7
+++ taxonomy_xml/taxonomy_xml.module	15 Dec 2007 14:14:19 -0000
@@ -9,7 +9,7 @@
     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
@@ -27,15 +27,15 @@ function taxonomy_xml_help($section) {
       return t('Makes it possible to import and export taxonomy terms via XML.');
     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 add to vocabulary will be specified by the XML document itself. ".
-	  "(To avoid duplications, already-existing terms will not be added.)");
+    "use the \"Target 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/content/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")));
+        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)));
+        array('!downloads' => l(t("taxonomy XML"), "admin/taxonomy/export"), '!upload' => l("administer &raquo; categories &raquo; import", "admin/taxonomy/import", array(), NULL, NULL, FALSE, TRUE)));
   }
 }
 
@@ -81,13 +81,13 @@ function taxonomy_xml_export() {
   $output = '';
   $vocabularies = module_invoke('taxonomy', 'get_vocabularies');
   
-  if(empty($vocabularies)){
+  if (empty($vocabularies)) {
     $output .= t('There are no vocabularies present');
   }
-  else{
+  else {
     $output .= "\n<ul>";
     foreach ($vocabularies as $vocabulary) {
-      $output .= "\n\t<li>". l($vocabulary->name, "taxonomy_xml/$vocabulary->vid") . '</li>';
+      $output .= "\n\t<li>". l($vocabulary->name, "taxonomy_xml/$vocabulary->vid") .'</li>';
     }
     $output .= "\n</ul>";
   }
@@ -102,7 +102,7 @@ function taxonomy_xml_file() {
   
   //retriving Vocabulary name
   $vocabulary = (array) (module_invoke('taxonomy', 'get_vocabulary', $vid));
-  $vname = strtolower(str_replace(' ','',trim($vocabulary['name'])));
+  $vname = strtolower(str_replace(' ', '_', trim($vocabulary['name'])));
   unset($vocabulary);
   
   $file = taxonomy_xml_create($vid);
@@ -116,7 +116,7 @@ function taxonomy_xml_file() {
   if (headers_sent()) {
     echo 'Some data has already been output to browser, can\'t send file';
   }
-  header('Content-Length: ' . strlen($file));
+  header('Content-Length: '. strlen($file));
   header("Content-Disposition: attachment; filename=taxonomy_$vname.xml");
   echo $file;
 }
@@ -156,7 +156,7 @@ function taxonomy_xml_import_form_valida
  * Builds the import form.
  */
 function taxonomy_xml_import_form() {
-  $vocs[0] = t('<Determined by source file>');
+  $vocs[0] = t('[Determined by source file]');
   foreach (module_invoke('taxonomy', 'get_vocabularies') as $vid => $voc) {
     $vocs[$vid] = $voc->name;
   }
@@ -212,18 +212,18 @@ function taxonomy_xml_import_form() {
  *   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";
+  $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>\n";
     foreach ($vocabulary as $key => $value) {
       if (is_array($value)) {
-        $output .= "<$key>" . check_plain(implode(',', $value)) . "</$key>";
+        $output .= "<$key>". check_plain(implode(',', $value)) ."</$key>";
       }
       else {
-        $output .= "<$key>" . check_plain($value) . "</$key>";
+        $output .= "<$key>". check_plain($value) ."</$key>";
       }
     }
     foreach ($tree as $term) {
@@ -233,11 +233,11 @@ function taxonomy_xml_create($vid, $pare
       foreach ($term as $key => $value) {
         if ($key == 'parents') {
           foreach ($value as $parent) {
-            $output .= "<parent>" . check_plain($parent) . "</parent>";
+            $output .= "<parent>". check_plain($parent) ."</parent>";
           }
         }
         else {
-          $output .= "<$key>" . check_plain($value) . "</$key>";
+          $output .= "<$key>". check_plain($value) ."</$key>";
         }
       }
       if ($synonyms) {
@@ -256,29 +256,29 @@ function taxonomy_xml_create($vid, $pare
  * Call-back function used by the XML parser.
  */
 function taxonomy_xml_element_start($parser, $name, $attributes) {
-  global $term, $element, $tag;
+  global $_tx_term, $_tx_element, $_tx_tag;
 
   switch ($name) {
     case 'vocabulary':
-      $element = $name;
+      $_tx_element = $name;
       break;
     case 'term':
-      $element = $name;
-      $term += 1;
+      $_tx_element = $name;
+      $_tx_term += 1;
   }
-  $tag = $name;
+  $_tx_tag = $name;
 }
 
 /**
  * Call-back function used by the XML parser.
  */
 function taxonomy_xml_element_end($parser, $name) {
-  global $element;
+  global $_tx_element;
 
   switch ($name) {
     case 'vocabulary':
     case 'term':
-      $element = '';
+      $_tx_element = '';
   }
 }
 
@@ -286,31 +286,41 @@ function taxonomy_xml_element_end($parse
  * Call-back function used by the XML parser.
  */
 function taxonomy_xml_element_data($parser, $data) {
-  global $vocabulary, $element, $terms, $term, $image, $tag;
+  global $_tx_vocabulary, $_tx_element, $_tx_terms, $_tx_term, $_tx_tag;
 
-  switch ($element) {
+  switch ($_tx_element) {
     case 'term':
-      if ($tag == 'parent') {
+      if ($_tx_tag == 'parent') {
         if (trim($data)) {
-          $terms[$term][$tag][] = $data;
+          $_tx_terms[$_tx_term][$_tx_tag][] = $data;
         }
       }
       else {
-        $terms[$term][$tag] .= trim($data);
+        $_tx_terms[$_tx_term][$_tx_tag] .= trim($data);
       }
       break;
     default:
-      $vocabulary[$tag] .= trim($data);
+      $_tx_vocabulary[$_tx_tag] .= trim($data);
   }
 }
 
+/**
+ * Initiate the parser on the custom XML schema.
+ * 
+ * This uses the XML callback parser with tag callbacks.
+ * 
+ * @see taxonomy_xml_element_start
+ * @see taxonomy_xml_element_end
+ * @see taxonomy_xml_element_data
+ *
+ */
 function taxonomy_xml_parse(&$data, $vid = 0, $parent_tid = NULL, $duplicate = 0) {
-  global $terms, $vocabulary;
+  global $_tx_terms, $_tx_vocabulary;
 
   // Unset the global variables before we use them:
   unset($GLOBALS['element'], $GLOBALS['term'], $GLOBALS['tag']);
-  $terms = array();
-  $vocabulary = array();
+  $_tx_terms = array();
+  $_tx_vocabulary = array();
   $new_terms = array();
 
   // parse the data:
@@ -330,39 +340,40 @@ function taxonomy_xml_parse(&$data, $vid
   // terms should be added to the existing vocabulary. Otherwise a new vocabulary should be created.
   
   if ($vid == 0) {
-    $name = $vocabulary['name'];
+    $name = $_tx_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));
+      $_tx_vocabulary = (array) (module_invoke('taxonomy', 'get_vocabulary', $vid));
     }
     else {
-      unset($vocabulary['vid']);
+      unset($_tx_vocabulary['vid']);
       
       // The node type array for a vocab has the type name as the key and the value set to 1
       // so we have to slightly modify the array
-      $node_types = explode(',', $vocabulary['nodes']);
-      $vocabulary['nodes'] = array();
+      $node_types = explode(',', $_tx_vocabulary['nodes']);
+      $_tx_vocabulary['nodes'] = array();
       foreach ($node_types as $type) {
         $vocabulary['nodes'][$type] = 1;
       }
-      taxonomy_save_vocabulary($vocabulary);
+      taxonomy_save_vocabulary($_tx_vocabulary);
     }
   }
   else {
-    $vocabulary = (array) (module_invoke('taxonomy', 'get_vocabulary', $vid));
+    $_tx_vocabulary = (array) (module_invoke('taxonomy', 'get_vocabulary', $vid));
   }
   
   // Get the maximum depth of terms
-  foreach ($terms as $term) {
+  foreach ($_tx_terms as $term) {
     $term_depth[] = $term['depth'];
   }
   // Import terms in order of depth
+  $new_tid = array();
   for ($i=0;$i<=max($term_depth);$i++) {
-    foreach ($terms as $term) {
+    foreach ($_tx_terms as $term) {
       if ($term['depth'] != $i) {
         continue;
       }
-      $term['vid'] = $vocabulary['vid'];
+      $term['vid'] = $_tx_vocabulary['vid'];
       $term['old_tid'] = $term['tid'];
       unset($term['tid']);
       if (is_array($term['parent'])) {
@@ -372,22 +383,22 @@ function taxonomy_xml_parse(&$data, $vid
           }
         }
       }
-  	  $term_exists = false;
-  	  if ($duplicate == 0) {
-  	    $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;
-  	        }
-  	      }
-  	    }
-  	  }
+      $term_exists = false;
+      if ($duplicate == 0) {
+        $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 the term doesn't already exist in this vocabulary, add it.
-      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'];
@@ -408,4 +419,3 @@ function taxonomy_xml_parse(&$data, $vid
     drupal_set_message($output);
   }
 }
-?>
\ No newline at end of file
