Index: import_typepad.info
===================================================================
RCS file: /cvs/drupal/contributions/modules/import_typepad/import_typepad.info,v
retrieving revision 1.4
diff -u -r1.4 import_typepad.info
--- import_typepad.info	3 Oct 2008 00:47:47 -0000	1.4
+++ import_typepad.info	7 Jan 2010 15:06:37 -0000
@@ -1,17 +1,4 @@
+; $Id$
 name = "Import Typepad"
-description = "Imports Typepad/MoveableType backup files to Content Types"
-version = "5.x-dev"
-project = "import_typepad"
-
-; Information added by drupal.org packaging script on 2008-09-24
-version = "5.x-1.0"
-project = "import_typepad"
-datestamp = "1222234217"
-
-
-; Information added by drupal.org packaging script on 2008-09-25
-version = "6.x-1.0"
-core = "6.x"
-project = "import_typepad"
-datestamp = "1222316414"
-
+description = "Imports content from Typepad export."
+core = 6.x
Index: import_typepad.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/import_typepad/import_typepad.module,v
retrieving revision 1.9
diff -u -r1.9 import_typepad.module
--- import_typepad.module	3 Oct 2008 00:47:47 -0000	1.9
+++ import_typepad.module	8 Jan 2010 19:32:11 -0000
@@ -12,61 +12,100 @@
 define('_IMPORT_TYPEPAD', 'import_typepad');
 define('_ACTION_DELETE_FILE', 'Delete file from server');
 
-function import_typepad_help($section) {
-  switch ($section) {
-    case 'admin/modules#description':
-      return t('Import nodes from a typepad file.');
-    case 'admin/help#import_typepad': 
-      return t(' ', array('%admin-node-import_typepad' => url('admin/node/import_typepad'), '%admin-access-permission' => url('admin/access/permission')));
-  }
+function import_typepad_perm() {
+  return array('import nodes');  
 }
 
-function import_typepad_menu($may_cache) {
-  $links = array();
-  $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);
-  return $links;
-}
+function import_typepad_menu() {  
+  $items['admin/content/import_typepad'] = array(
+    'title' => t('Import Typepad'), 
+    'description' => t('Description goes here.'),
+    'page callback' => 'import_typepad_page', 
+    'page arguments' => array($form_values),
+    'access arguments' => array('import nodes'),
+  );
+  
+  $items['admin/content/import_typepad/preview'] = array(
+    'title' => t('Import Typepad Preview'), 
+    'page callback' => 'import_typepad_preview',
+    'page arguments' => array($previewCount),
+    'description' => t('Description goes here.'), 
+    'access arguments' => array('import nodes'), 
+    'type' => MENU_CALLBACK
+  );
 
-function import_typepad_perm() {
-  return array('import nodes');  
+  return $items;
 }
 
+
+
 /**
  * Show the front page, with either the current working file, or a file selection form
  */
-function import_typepad_page($form_values=NULL) {
+function import_typepad_page() {
+  
+  $output = drupal_get_form('import_typepad_step1_form');
+  return $output;
+}
 
-  if ($edit[_WORKING_FILE]) {
-    $output .= form_item(t('File'), $edit[_WORKING_FILE] .' ('. format_size( filesize($edit[_WORKING_FILE]) ) .')<br />'. form_submit(t('Use a different file')));
+function import_typepad_step1_form($form_state) {
+  // get all of the content types for this Drupal installation 
+  $types = db_query('SELECT DISTINCT type, name FROM {node_type} ORDER BY name ASC');
+  while ($row = db_fetch_object($types))
+  {
+    $opts[$row->type] = $row->name;
+  }
+  
+  // get all of the vocabs for this Drupal installation 
+  $vocabs = taxonomy_get_vocabularies();
+  foreach($vocabs as $v)
+  {
+    $vocab[$v->name] = $v->name;
   }
-  else {
-    $form['upload_file'] = array(
+  
+  $form['#attributes'] = array('enctype' => 'multipart/form-data');
+  
+  $form['upload_file'] = array(
       '#type' => 'file',
       '#title' => t('Upload Type Pad/Movable Type file'),
-      '#description' => t('The export file you wish to import'));
-    $form['serverside_file'] = array(
+      '#description' => t('The export file you wish to import')
+  );
+  
+  $form['serverside_file'] = array(
       '#type' => 'textfield',
       '#title' => t('Server side file'),
-      '#description' => t('The file located on the server you wish to import'));
-  }
+      '#description' => t('The file located on the server you wish to import')
+  );
+  
+  $form['content_type'] = array(
+      '#type' => 'select',
+      '#title' => t('Content type'),
+      '#default_value' => variable_get('import_typepad_content_type', null),
+      '#options' => $opts,
+      '#description' => t('The content type the new nodes will have.'),
+      '#required' => true,
+  );
+  
+  $form['vocab'] = array(
+      '#type' => 'select',
+      '#title' => t('Vocabulary'),
+      '#default_value' => variable_get('import_typepad_vocab', null),
+      '#options' => $vocab,
+      '#description' => t('The vocabulary with which you want to map taxonomy terms.'),
+      '#required' => true,
+  );
   
-  // todo add an insert/update option
   $form['submit'] = array(
     '#type' => 'submit',
-    '#value' => t('Step 2'));
-
-  $form['#attributes'] = array('enctype' => 'multipart/form-data');
-  
-  $output = drupal_get_form('typepad_step1', $form);
-
-  return $output;
+    '#value' => t('Step 2')
+  );
+  return $form;
 }
 
 /**
  * Discover which file we are using, and move it into the appropriate position
  */
-function typepad_step1_validate($form_id, $form_values){
+ function import_typepad_step1_form_validate($form, &$form_state){
   global $user;
   global $base_url;
 
@@ -78,24 +117,28 @@
     //$_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'])){
+  } else if (strlen($form_state['values']['serverside_file'])>0 && file_exists($form_state['values']['serverside_file'])){
     drupal_set_message("Using server file ".$form_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 (!($file-->filename)) {
+    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 import_typepad_step1_form_submit($form, &$form_state){
+  
+  dsm($form_state);
+  variable_set('import_typepad_content_type', $form_state['values']['content_type']);
+  variable_set('import_typepad_vocab', $form_state['values']['vocab']);
+  $form_state['redirect'] = 'admin/content/import_typepad/preview';
 }
 
 /**
@@ -106,16 +149,9 @@
 function import_typepad_preview($previewCount = 10) {
   $authors = array();
   $cats = array();
-  $output = '';
-
-  $items = _import_typepad_get_nodes( variable_get(_WORKING_FILE, null), $edit['type'], $previewCount, $authors, $cats, array());
-
-
-  // Refresh preview w/ entry count
-
   // Previews
-  $output .= $items;
-
+  $output .= _import_typepad_get_nodes(variable_get(_WORKING_FILE, null), 'import_typepad', $previewCount = 10, $authors, $cats, array());
+  
   // Mappings and import
   // Get user list
   $users = array();
@@ -124,36 +160,46 @@
   while ($user = db_fetch_object($result)) {
     $users[$user->uid] = $user->name;
   }
+  return $output.drupal_get_form('import_typepad_process_form', $users, $authors, $cats);
+}
 
-  $import_form["author_title"] = array(
-    "#type" => "markup",
-    "#value" => t("<span>Author mappings</span>"));
-  foreach ($authors as $author=>$uid){
+function import_typepad_process_form($form_state, $users, $authors, $cats) {
+  
+  $import_form['author_title'] = array(
+    '#type' => 'markup',
+    '#value' => t('<span>Author mappings</span>')
+  );
+  
+  foreach ($authors as $author => $uid){
     $import_form[$author] = array(
-      "#type" => 'select',
-      "#title"=> $author,
-      '#options' => $users);
+      '#type' => 'select',
+      '#title' => $author,
+      '#options' => $users
+    );
   }
+  
+  $import_form['taxonomy_title'] = array(
+    '#type' => 'markup',
+    '#value' => t('<span>Taxonomy mappings</span>')
+  );
 
-  $import_form["taxonomy_title"] = array(
-    "#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["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>')  );
-
-  $import_form['submit'] = array('#type' => 'submit', '#value' => t('Import'));
-
-  $output .= drupal_get_form('import_typepad_process', $import_form);
-
-  return $output;
+  $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>')  
+  );
+
+  $import_form['submit'] = array(
+    '#type' => 'submit', 
+    '#value' => t('Import')
+  );
+  
+  return $import_form;
 }
 
 /**
@@ -163,36 +209,40 @@
 function _import_typepad_taxonomy_list($name){
   if (user_access('access content')) {
     $default = '';
-    $vocabs = taxonomy_get_vocabularies('blog');
+    $v = variable_get('import_typepad_vocab', false);
+    $vocabs = taxonomy_get_vocabularies($v);
     $items = array();
     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){
+        if ($name == $term->name){
           $default = $term->tid;
-	}
+        }
       }
     }
     return array(
-      "#type" => "select",
-      "#default_value" => $default,
-      "#options" => $items );
+      '#type' => 'select',
+      '#default_value' => $default,
+      '#options' => $items );
   }
 }
 
 /**
  * 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_form_submit($form, &$form_state){
+  dsm($form);
   $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), 'import_typepad', 0, $form_state['values'], $cats, $form_state['values']);
+  if ( variable_get(_FILE_TYPE, false) == 'UPLOAD'){
+    unlink( variable_get(_WORKING_FILE, false) );
   }
   variable_del(_WORKING_FILE);
   variable_del(_FILE_TYPE);
-  return 'admin/node/import_typepad';
+  variable_del('import_typepad_content_type');
+  variable_del('import_typepad_content_type');
+  $form_state['redirect'] = 'admin/content/import_typepad';
 }
 
 /**
@@ -206,16 +256,16 @@
   $authorMap = array();
   $cats = array();
   foreach ($_POST['edit'] as $id=>$val){
-  	if (!(strpos($id,"author_map_") === FALSE)){
-  	  $authorMap[substr($id, strlen("author_map_"))] = $val;
-  	}
+    if (!(strpos($id,"author_map_") === FALSE)){
+      $authorMap[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(variable_get(_WORKING_FILE, null), 'import_typepad', 'import_typepad', $edit['global'], $edit['errors'], $authorMap, $cats, $edit);
   unset($edit['match']);
 
-  return form($output);
+  return $output;
 }
 
 /**
@@ -265,6 +315,7 @@
  * @return preview
  */
 function _import_typepad_get_nodes($path, $type, $preview, &$authors, &$cats, $taxonomy) {
+
   $handle = fopen($path, 'r');
   if ($handle == null)
     return false;
@@ -291,111 +342,111 @@
         $state = 0;
         // We currently want to ignore these sections
       } else if (in_array($line, array("EXCERPT:","KEYWORDS:"))) {
-      	$state = 3;
+        $state = 3;
       } else if ($line == "EXTENDED BODY:"){
-      	$state = 4;
+        $state = 4;
       } else if ($line == "BODY:"){
-      	$state = 5;
+        $state = 5;
       } else if ( !(strpos($line,"COMMENT:") === FALSE) ){
-      	// We are working on a comment
-	//echo "<!-- Starting new comment -->\n";
-      	$state = 2;
+        // 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";
-	}
+        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){
+        $value = trim(substr($line,strlen("STATUS:")));
+        if (strcmp($value, "Publish") == 0){
           $currentBlog->status = 1;
-      	} else {
+        } 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
+  $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){
+        // 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){
+            if (!$preview){
               $c = array();
               foreach ($blogCats as $id=>$val){
                 $c[] = $taxonomy['taxonomy_'.str_replace(' ','_',$val)];
               }
               _import_typepad_save($currentBlog, $c);
               $success++;
-      	    } else if ($previewCount < $preview){
-    	      $output .= node_view($currentBlog);
-    	      $previewCount++;
+            } else if ($previewCount < $preview){
+            $output .= node_view($currentBlog);
+            $previewCount++;
             }
-      	  }
-      		
-      	  // Start new blog
+          }
+          
+          // 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>";
-      	}
+          $currentBlog = new StdClass();
+          $currentBlog->type = variable_get('import_typepad_content_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";
         
-  	$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 )
+        $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>";
+        }
       }
     }
     $run = false;
@@ -414,16 +465,8 @@
   }
 
   fclose($handle);
-  /*
-  if ($errors) {
-    // todo use drupal_set_message?
-    drupal_set_message(t('%errors. Click \'Download rows with errors\' for a CSV file of the failed rows.', array('%errors' => format_plural(count($errors), 'There was 1 error', 'There were %count errors'))));
-    $output = form_submit(t('Download rows with errors')) . $output;
-    $errors;
-  }
-  */
   if ($success) {
-    drupal_set_message(t('Successfully imported %count.', array('%count' => format_plural($success, '1 node', '%count nodes'))));
+    drupal_set_message(t('Successfully imported %count.', array('%count' => format_plural($success, '1 node', $success.' nodes'))));
   }
   
   return $output;
@@ -454,8 +497,8 @@
   //echo "<!-- Number of comments = ".count($currentBlog->comments)." -->\n";
   foreach ($currentBlog->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', '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']));
@@ -484,10 +527,9 @@
     $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} (nid, pid, uid, subject, comment, format, hostname, timestamp, status, thread, name, mail, homepage) VALUES (%d, %d, %d, '%s', '%s', %d, '%s', %d, %d, '%s', '%s', '%s', '%s')", 
+      $currentBlog->nid, 0/*pid*/, 0, "re: ".$currentBlog->title, $comment->comment, 1/*format*/, $comment->hostname, $comment->timestamp, 0/*status*/, $thread, $comment->name, $comment->mail, $comment->homepage);
+    _comment_update_node_statistics($nid);
   }
 }
-}
-?>
+}
\ No newline at end of file

