--- import_typepad.module.orig	2007-01-06 23:28:02.000000000 -0500
+++ import_typepad.module	2009-06-19 16:54:24.000000000 -0400
@@ -1,5 +1,9 @@
 <?php 
-/**
+// $Id$
+
+/*
+ * @file
+ * Drupal Module: import_typepad
  * Module to import Type Pad exports into drupal
  * Based on node_import (the cvs content importer)
  * 
@@ -12,8 +16,8 @@ define('_WORKING_FILE', 'import_typepad_
 define('_IMPORT_TYPEPAD', 'import_typepad');
 define('_ACTION_DELETE_FILE', 'Delete file from server');
 
-function import_typepad_help($section) {
-  switch ($section) {
+function import_typepad_help($path, $arg) {
+  switch ($path) {
     case 'admin/modules#description':
       return t('Import nodes from a typepad file.');
     case 'admin/help#import_typepad': 
@@ -21,12 +25,20 @@ function import_typepad_help($section) {
   }
 }
 
-function import_typepad_menu($may_cache) {
-  $links = array();
-  if ($may_cache) {
-    $links[] = array('path' => 'admin/node/import_typepad', 'title' => t('type pad'), 'callback' => 'import_typepad_page', 'weight' => 5, 'access' => user_access('import nodes'));
-    $links[] = array('path' => 'admin/node/import_typepad/preview', 'title' => t('type pad'), 'callback' => 'import_typepad_preview', 'weight' => 5, 'access' => user_access('import nodes'), 'type' => MENU_CALLBACK);
-  }
+function import_typepad_menu() {
+  $links['admin/content/import_typepad'] = array(
+    'title' => 'type pad',
+    'page_callback' => 'import_typepad_page',
+    'weight' => 5,
+    'access' => user_access('import nodes')
+  );
+  $links['admin/content/import_typepad/preview'] = array(
+    'title' => 'type pad',
+    'page_callback' => 'import_typepad_preview',
+    'weight' => 5,
+    'access' => user_access('import nodes'),
+    'type' => MENU_CALLBACK
+  );
   return $links;
 }
 
@@ -68,7 +80,7 @@ function import_typepad_page($form_value
 /**
  * Discover which file we are using, and move it into the appropriate position
  */
-function typepad_step1_validate($form_id, $form_values){
+function typepad_step1_validate($form, &$form_state) {
   global $user;
   global $base_url;
 
@@ -80,24 +92,25 @@ function typepad_step1_validate($form_id
     //$_SESSION[_IMPORT_TYPEPAD][_WORKING_FILE] = $file->filepath;
     variable_set(_WORKING_FILE, $file->filepath);
     variable_set(_FILE_TYPE, 'UPLOAD');
-  } else if (strlen($form_values['serverside_file'])>0 && file_exists($form_values['serverside_file'])){
-    drupal_set_message("Using server file ".$form_values['serverside_file']);
+  }
+  else if (strlen($form_state['values']['serverside_file'])>0 && file_exists($form_state['values']['serverside_file'])) {
+    drupal_set_message("Using server file ". $form_state['values']['serverside_file']);
     // See if there was a server file
-    variable_set(_WORKING_FILE, $form_values['serverside_file']);
+    variable_set(_WORKING_FILE, $form_state['values']['serverside_file']);
     variable_set(_FILE_TYPE, 'SERVER');
     //$_SESSION[_IMPORT_TYPEPAD][_WORKING_FILE] = $edit['serverfile'];
   }
 
-  if ( variable_get(_WORKING_FILE, null) == null ) {
-    form_set_error('file', t('You must select a file to import.'.variable_get(_FILE_TYPE,'missing')));
+  if ( variable_get(_WORKING_FILE, NULL) == NULL ) {
+    form_set_error('file', t('You must select a file to import.'. variable_get(_FILE_TYPE, 'missing')));
   }
 }
 
 /**
  * If the validation passed then simple redirect to the preview
  */
-function typepad_step1_submit($form_id, $form_values){
-  return 'admin/node/import_typepad/preview';
+function typepad_step1_submit($form, &$form_state) {
+  $form_state['redirect'] = 'admin/node/import_typepad/preview';
 }
 
 /**
@@ -105,12 +118,12 @@ function typepad_step1_submit($form_id, 
  * else display preview form
  * In due course we'll pass the preview count via the parameters
  */
-function import_typepad_preview($previewCount = 10) {
+function import_typepad_preview($preview_count = 10) {
   $authors = array();
   $cats = array();
   $output = '';
 
-  $items = _import_typepad_get_nodes( variable_get(_WORKING_FILE, null), $edit['type'], $previewCount, $authors, $cats, array());
+  $items = _import_typepad_get_nodes( variable_get(_WORKING_FILE, NULL), $edit['type'], $preview_count, $authors, $cats, array());
 
 
   // Refresh preview w/ entry count
@@ -130,10 +143,10 @@ function import_typepad_preview($preview
   $import_form["author_title"] = array(
     "#type" => "markup",
     "#value" => t("<span>Author mappings</span>"));
-  foreach ($authors as $author=>$uid){
+  foreach ($authors as $author => $uid) {
     $import_form[$author] = array(
       "#type" => 'select',
-      "#title"=> $author,
+      "#title" => $author,
       '#options' => $users);
   }
 
@@ -141,13 +154,13 @@ function import_typepad_preview($preview
     "#type" => "markup",
     "#value" => t("<span>Taxonomy mappings</span>"));
 
-  foreach ($cats as $cat=>$count){
+  foreach ($cats as $cat => $count) {
     $item = _import_typepad_taxonomy_list($cat);
     $item['#title'] = "$cat ($count)";
-    $import_form['taxonomy_'.str_replace(' ','_',$cat)] = $item;
+    $import_form['taxonomy_'. str_replace(' ', '_', $cat)] = $item;
   }
 
-  $import_form["import_warning"] = array (
+  $import_form["import_warning"] = array(
     "#type" => "markup",
     "#value" => t('<span>Importing may take awhile, do not click \'Import\' more than once. To see progress, look at the <a href="?q=admin/node" target="_new">Administer -> Content</a> page in a new window.</span>')  );
 
@@ -162,7 +175,7 @@ function import_typepad_preview($preview
  * This function returns a select box of name taxonomy_ESCAPEDNAME
  * with all the blog categories in it.
  */
-function _import_typepad_taxonomy_list($name){
+function _import_typepad_taxonomy_list($name) {
   if (user_access('access content')) {
     $default = '';
     $vocabs = taxonomy_get_vocabularies('blog');
@@ -170,10 +183,10 @@ function _import_typepad_taxonomy_list($
     foreach ($vocabs as $vocab) {
       $tree = taxonomy_get_tree($vocab->vid);
       foreach ($tree as $term) {
-        $items[$term->tid] = _taxonomy_depth($term->depth) .' '.$term->name;
-	if ($name == $term->name){
+        $items[$term->tid] = _taxonomy_depth($term->depth) .' '. $term->name;
+        if ($name == $term->name) {
           $default = $term->tid;
-	}
+        }
       }
     }
     return array(
@@ -186,15 +199,15 @@ function _import_typepad_taxonomy_list($
 /**
  * Simply run the import, passing the form in for the taxonomy and author maps
  */
-function import_typepad_process_submit($formid, $form_elements){
+function import_typepad_process_submit($formid, $form_elements) {
   $cats = array(); // unused
-  $items = _import_typepad_get_nodes( variable_get(_WORKING_FILE, null), $edit['type'], 0, $form_elements, $cats, $form_elements);
-  if ( variable_get(_FILE_TYPE, null) == 'UPLOAD'){
-    unlink( variable_get(_WORKING_FILE, null) );
+  $items = _import_typepad_get_nodes( variable_get(_WORKING_FILE, NULL), $edit['type'], 0, $form_elements, $cats, $form_elements);
+  if ( variable_get(_FILE_TYPE, NULL) == 'UPLOAD') {
+    unlink( variable_get(_WORKING_FILE, NULL) );
   }
   variable_del(_WORKING_FILE);
   variable_del(_FILE_TYPE);
-  return 'admin/node/import_typepad';
+  $form_state['redirect'] = 'admin/node/import_typepad';
 }
 
 /**
@@ -205,16 +218,16 @@ function _import_typepad_import(&$edit) 
   $output .= form_item(t('File'), $edit[_WORKING_FILE] .' ('. format_size( filesize($edit[_WORKING_FILE]) ) .')');
   $output .= form_submit(t(_ACTION_DELETE_FILE)); 
 
-  $authorMap = array();
+  $author_map = array();
   $cats = array();
-  foreach ($_POST['edit'] as $id=>$val){
-  	if (!(strpos($id,"author_map_") === FALSE)){
-  	  $authorMap[substr($id, strlen("author_map_"))] = $val;
-  	}
+  foreach ($_POST['edit'] as $id => $val) {
+    if (!(strpos($id, "author_map_") === FALSE)) {
+      $author_map[substr($id, strlen("author_map_"))] = $val;
+    }
   }
 
   $edit['errors'] = 0;
-  $output .= _import_typepad_get_nodes($edit[_WORKING_FILE], $edit['type'], $edit['type'] == 'import_typepad' ? NULL : $edit['match'], $edit['global'], $edit['errors'], $authorMap, $cats, $edit);
+  $output .= _import_typepad_get_nodes($edit[_WORKING_FILE], $edit['type'], $edit['type'] == 'import_typepad' ? NULL : $edit['match'], $edit['global'], $edit['errors'], $author_map, $cats, $edit);
   unset($edit['match']);
 
   return form($output);
@@ -268,150 +281,175 @@ function _import_typepad_errors_validate
  */
 function _import_typepad_get_nodes($path, $type, $preview, &$authors, &$cats, $taxonomy) {
   $handle = fopen($path, 'r');
-  if ($handle == null)
-    return false;
+  if ($handle == NULL)
+    return FALSE;
 
   $success = 0;
 
-  $run = true;
-  $currentBlog = null;
+  $run = TRUE;
+  $current_blog = NULL;
   $state = 0; // 0-unknown, 1-blog, 2-comment, 3-skip, 4-blog body, 5-blog excerpt
-  $previewCount = 0;
-  $blogCats = array();
+  $preview_count = 0;
+  $blog_cats = array();
   
-  $mapping = array('TITLE:'=>'title',
-    'ALLOW COMMENTS:'=>'comment', 'EMAIL:'=>'mail', 'IP:'=>'hostname', 'URL:'=>'homepage');
+  $mapping = array('TITLE:' => 'title', 'ALLOW COMMENTS:' => 'comment', 'EMAIL:' => 'mail', 'IP:' => 'hostname', 'URL:' => 'homepage');
     
-  while ($run){
-    while ($line = fgets($handle)){
-      $rawLine = $line;
-      //echo "<!-- $rawLine -->\n";
+  while ($run) {
+    while ($line = fgets($handle)) {
+      $raw_line = $line;
+      //echo "<!-- $raw_line -->\n";
       $line = trim($line);
-      if ($line == "-----"){
+      if ($line == "-----") {
         $state = 0;
-      } else if ($line == "--------"){
+      }
+      else if ($line == "--------") {
         $state = 0;
         // We currently want to ignore these sections
-      } else if (in_array($line, array("EXCERPT:","KEYWORDS:"))) {
-      	$state = 3;
-      } else if ($line == "EXTENDED BODY:"){
-      	$state = 4;
-      } else if ($line == "BODY:"){
-      	$state = 5;
-      } else if ( !(strpos($line,"COMMENT:") === FALSE) ){
-      	// We are working on a comment
-	//echo "<!-- Starting new comment -->\n";
-      	$state = 2;
-        $currentBlog->comments[] = new StdClass();
-      } else if ( !(strpos($line, "DATE:") === FALSE )){
-      	if ($state == 1){
-      	  $currentBlog->created = strtotime(substr($line,strlen("DATE:")));
-      	} else if ($state == 2) {
-      	  $currentBlog->comments[count($currentBlog->comments)-1]->timestamp = strtotime(substr($line,strlen("DATE:")));
-      	} else {
-	  //echo "<!-- ERROR -->\n";
-	}
-      } else if ( !(strpos($line, "STATUS:") === FALSE ) ){
-      	$value = trim(substr($line,strlen("STATUS:")));
-      	if (strcmp($value, "Publish") == 0){
-          $currentBlog->status = 1;
-      	} else {
-          $currentBlog->status = 0;
-      	}
-      } else if ( !(strpos($line,"CATEGORY:") === FALSE) ){
-        $category = trim(substr($line, strpos($line,"CATEGORY:") + strlen("CATEGORY:")));
-	$cats[$category]++;
-	$blogCats[] = $category; // Add the category to this entry
-      } else if ( !(strpos($line,"AUTHOR:") === FALSE) ){
-      	// If state is unknown, then the new item is a blog, not a comment
-      	if ($state == 0){
-      	  $state = 1;
-      	  // Save the last blog
-      	  if ($currentBlog != null){
-            $currentBlog->body = strlen(trim($currentBlog->body))==0?$currentBlog->teaser:$currentBlog->body;
-      	    if (!$preview){
+      }
+      else if (in_array($line, array("EXCERPT:", "KEYWORDS:"))) {
+        $state = 3;
+      }
+      else if ($line == "EXTENDED BODY:") {
+        $state = 4;
+      }
+      else if ($line == "BODY:") {
+        $state = 5;
+      }
+      else if ( !(strpos($line, "COMMENT:") === FALSE) ) {
+        // We are working on a comment
+        //echo "<!-- Starting new comment -->\n";
+        $state = 2;
+        $current_blog->comments[] = new stdClass();
+      }
+      else if ( !(strpos($line, "DATE:") === FALSE )) {
+        if ($state == 1) {
+          $current_blog->created = strtotime(substr($line, strlen("DATE:")));
+        }
+        else if ($state == 2) {
+          $current_blog->comments[count($current_blog->comments)-1]->timestamp = strtotime(substr($line, strlen("DATE:")));
+        }
+        else {
+          //echo "<!-- ERROR -->\n";
+        }
+      }
+      else if ( !(strpos($line, "STATUS:") === FALSE ) ) {
+        $value = trim(substr($line, strlen("STATUS:")));
+        if (strcmp($value, "Publish") == 0) {
+          $current_blog->status = 1;
+        }
+        else {
+          $current_blog->status = 0;
+        }
+      }
+      else if ( !(strpos($line, "CATEGORY:") === FALSE) ) {
+        $category = trim(substr($line, strpos($line, "CATEGORY:") + strlen("CATEGORY:")));
+  $cats[$category]++;
+  $blog_cats[] = $category; // Add the category to this entry
+      }
+      else if ( !(strpos($line, "AUTHOR:") === FALSE) ) {
+        // If state is unknown, then the new item is a blog, not a comment
+        if ($state == 0) {
+          $state = 1;
+          // Save the last blog
+          if ($current_blog != NULL) {
+            $current_blog->body = strlen(trim($current_blog->body))==0?$current_blog->teaser:$current_blog->body;
+            if (!$preview) {
               $c = array();
-              foreach ($blogCats as $id=>$val){
-                $c[] = $taxonomy['taxonomy_'.str_replace(' ','_',$val)];
+              foreach ($blog_cats as $id => $val) {
+                $c[] = $taxonomy['taxonomy_'. str_replace(' ', '_', $val)];
               }
-              _import_typepad_save($currentBlog, $c);
+              _import_typepad_save($current_blog, $c);
               $success++;
-      	    } else if ($previewCount < $preview){
-    	      $output .= node_view($currentBlog);
-    	      $previewCount++;
             }
-      	  }
-      		
-      	  // Start new blog
-          $blogCats = array();
-      	  $currentBlog = new StdClass();
-      	  $currentBlog->type = 'blog';
-      	  $currentBlog->author = trim( substr($line, strpos($line,"AUTHOR:") + strlen("AUTHOR:")) );
-      	  // We generate a hash for the author name which can be used in HTML forms
-      	  $authorHash = 'author_map_'.str_replace(" ","_",$currentBlog->author);
-      	  if (!array_key_exists($authorHash, $authors)){
-      	    $authors[$authorHash] = 0;
-      	  } else if (!$preview) {
-      	    $currentBlog->uid = $authors[$authorHash];
-      	  }
-
-      	  $currentBlog->comments = array();
-      		
-      	} else if ($state == 2) {
-      	  $currentBlog->comments[count($currentBlog->comments)-1]->name = substr($line, strlen("AUTHOR:"));
-      	} else {
-      		echo "<h1>ERROR 1 State = $state</h1>";
-      	}
-      } else {
-        //echo "<!-- $state $rawLine -->\n";
+            else if ($preview_count < $preview) {
+            $output .= node_view($current_blog);
+            $preview_count++;
+            }
+          }
+          
+          // Start new blog
+          $blog_cats = array();
+          $current_blog = new stdClass();
+          $current_blog->type = 'blog';
+          $current_blog->author = trim( substr($line, strpos($line, "AUTHOR:") + strlen("AUTHOR:")) );
+          // We generate a hash for the author name which can be used in HTML forms
+          $author_hash = 'author_map_'. str_replace(" ", "_", $current_blog->author);
+          if (!array_key_exists($author_hash, $authors)) {
+            $authors[$author_hash] = 0;
+          }
+          else if (!$preview) {
+            $current_blog->uid = $authors[$author_hash];
+          }
+
+          $current_blog->comments = array();
+          
+        }
+        else if ($state == 2) {
+          $current_blog->comments[count($current_blog->comments)-1]->name = substr($line, strlen("AUTHOR:"));
+        }
+        else {
+          echo "<h1>ERROR 1 State = $state</h1>";
+        }
+      }
+      else {
+        //echo "<!-- $state $raw_line -->\n";
         
-  	$sep = strpos($line, ":");
-	if ($sep === false){ // || !array_key_exists($name, $mapping)){
+        $sep = strpos($line, ":");
+        if ($sep === FALSE) { // || !array_key_exists($name, $mapping)) {
           $name = "";
-	  $value = "";
-	} else {
-  	  $name = substr($line, 0, $sep+1);
-  	  $value = substr($line, $sep+1);
-	}
-
-      	if ($state == 1){
-      	  $prop = $mapping[$name];
-	  if (strlen($prop) > 0 )
-  	    $currentBlog->$prop = $value;
-      	} else if ($state == 2){
-	  //echo "<!-- $name $val $sep -->\n";
-      	  if (strlen($name) > 0 && array_key_exists($name, $mapping)){
-      	    $currentBlog->comments[count($currentBlog->comments)-1]->$mapping[$name] = $value;
-      	  } else {
-      	    $currentBlog->comments[count($currentBlog->comments)-1]->comment .= $rawLine;
-      	  }
-      	} else if ($state == 3){
-      		// Do nothing (Skip)
-      	} else if ($state == 4){
-      		// Blog body
-      		$currentBlog->body .= $rawLine;
-      	} else if ($state == 5){
-      		$currentBlog->teaser .= $rawLine;
-      	} else if ($state == 0){
-      		//Do nothing, to avioid errors over nothing
-      	} else {
-      		echo "<h1>ERROR 2</h1>";
-      		echo "<pre>$state : $line</pre>";
-      	}
+          $value = "";
+        }
+        else {
+          $name = substr($line, 0, $sep+1);
+          $value = substr($line, $sep+1);
+        }
+
+        if ($state == 1) {
+          $prop = $mapping[$name];
+    if (strlen($prop) > 0 )
+        $current_blog->$prop = $value;
+        }
+        else if ($state == 2) {
+    //echo "<!-- $name $val $sep -->\n";
+          if (strlen($name) > 0 && array_key_exists($name, $mapping)) {
+            $current_blog->comments[count($current_blog->comments)-1]->$mapping[$name] = $value;
+          }
+          else {
+            $current_blog->comments[count($current_blog->comments)-1]->comment .= $raw_line;
+          }
+        }
+        else if ($state == 3) {
+          // Do nothing (Skip)
+        }
+        else if ($state == 4) {
+          // Blog body
+          $current_blog->body .= $raw_line;
+        }
+        else if ($state == 5) {
+          $current_blog->teaser .= $raw_line;
+        }
+        else if ($state == 0) {
+          //Do nothing, to avioid errors over nothing
+        }
+        else {
+          echo "<h1>ERROR 2</h1>";
+          echo "<pre>$state : $line</pre>";
+        }
       }
     }
-    $run = false;
-    $currentBlog->body = strlen(trim($currentBlog->body))==0?$currentBlog->teaser:$currentBlog->body;
-    if (!$preview){
+    $run = FALSE;
+    $current_blog->body = strlen(trim($current_blog->body))==0?$current_blog->teaser:$current_blog->body;
+    if (!$preview) {
       $c = array();
-      foreach ($blogCats as $id=>$val){
-        $c[] = $taxonomy['taxonomy_'.str_replace(' ','_',$val)];
+      foreach ($blog_cats as $id => $val) {
+        $c[] = $taxonomy['taxonomy_'. str_replace(' ', '_', $val)];
       }
-      _import_typepad_save($currentBlog, $c);
+      _import_typepad_save($current_blog, $c);
       $success++;
-    } else if ($previewCount < $preview){
-      $output .= node_view($currentBlog);
-      $previewCount++;
+    }
+    else if ($preview_count < $preview) {
+      $output .= node_view($current_blog);
+      $preview_count++;
     }
   }
 
@@ -434,30 +472,30 @@ function _import_typepad_get_nodes($path
 /**
  * Save the content into the database, first the blogs, then the comments
  */
-function _import_typepad_save($currentBlog, $terms){
-  if ($currentBlog != null){
+function _import_typepad_save($current_blog, $terms) {
+  if ($current_blog != NULL) {
   // Apply all the substitutions
-  $subReplace = $_REQUEST['import_typepad_substitution_replace'];
-  $subWith = $_REQUEST['import_typepad_substitution_with'];
+  $sub_replace = $_REQUEST['import_typepad_substitution_replace'];
+  $sub_with = $_REQUEST['import_typepad_substitution_with'];
   
-  if (!is_null($subReplace))
-    foreach ($subReplace as $id=>$val){
-      $currentBlog->teaser = str_replace($val, $subWith[$id], $currentBlog->teaser);
-      $currentBlog->body = str_replace($val, $subWith[$id], $currentBlog->body);
+  if (!is_null($sub_replace))
+    foreach ($sub_replace as $id => $val) {
+      $current_blog->teaser = str_replace($val, $sub_with[$id], $current_blog->teaser);
+      $current_blog->body = str_replace($val, $sub_with[$id], $current_blog->body);
     }
   
   // Save the entry
-  node_save($currentBlog);
+  node_save($current_blog);
 
   // Save taxonomy items
   $terms = array_unique($terms);
-  taxonomy_node_save($currentBlog->nid, $terms);
+  taxonomy_node_save($current_blog->nid, $terms);
   
-  //echo "<!-- Number of comments = ".count($currentBlog->comments)." -->\n";
-  foreach ($currentBlog->comments as $comment){
+  //echo "<!-- Number of comments = ".count($current_blog->comments)." -->\n";
+  foreach ($current_blog->comments as $comment) {
     // Most of the following code is ripped straight from comment.module
-    $cid = db_next_id('{comments}_cid');
-  	
+    $cid = db_last_insert_id('comments', 'cid');
+    
     // This is a comment with no parent comment (depth 0): we start
     // by retrieving the maximum thread level.
     $max = db_result(db_query('SELECT MAX(thread) FROM {comments} WHERE nid = %d', $edit['nid']));
@@ -486,10 +524,9 @@ function _import_typepad_save($currentBl
     $thread = $decimals . $units .'/';
           
     // Type pad doesn't have threaded comments, or subject lines, so we have to improvise them
-  	db_query("INSERT INTO {comments} (cid, nid, pid, uid, subject, comment, format, hostname, timestamp, status, score, users, thread, name, mail, homepage) VALUES (%d, %d, %d, %d, '%s', '%s', %d, '%s', %d, %d, %d, '%s', '%s', '%s', '%s', '%s')", 
-  	  $cid, $currentBlog->nid, 0/*pid*/, 0, "re: ".$currentBlog->title, $comment->comment, 1/*format*/, $comment->hostname, $comment->timestamp, 0/*status*/, 0/*score*/, ''/*$users*/, $thread, $comment->name, $comment->mail, $comment->homepage);
-  	_comment_update_node_statistics($nid);
+    db_query("INSERT INTO {comments} (cid, nid, pid, uid, subject, comment, format, hostname, timestamp, status, score, users, thread, name, mail, homepage) VALUES (%d, %d, %d, %d, '%s', '%s', %d, '%s', %d, %d, %d, '%s', '%s', '%s', '%s', '%s')", 
+      $cid, $current_blog->nid, 0/*pid*/, 0, "re: ". $current_blog->title, $comment->comment, 1/*format*/, $comment->hostname, $comment->timestamp, 0/*status*/, 0/*score*/, ''/*$users*/, $thread, $comment->name, $comment->mail, $comment->homepage);
+    _comment_update_node_statistics($nid);
   }
 }
 }
-?>
