/[drupal]/contributions/modules/import_typepad/import_typepad.module
ViewVC logotype

Diff of /contributions/modules/import_typepad/import_typepad.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.7, Sun Jan 7 04:28:02 2007 UTC revision 1.8, Wed Sep 24 05:42:25 2008 UTC
# Line 12  define('_WORKING_FILE', 'import_typepad_ Line 12  define('_WORKING_FILE', 'import_typepad_
12  define('_IMPORT_TYPEPAD', 'import_typepad');  define('_IMPORT_TYPEPAD', 'import_typepad');
13  define('_ACTION_DELETE_FILE', 'Delete file from server');  define('_ACTION_DELETE_FILE', 'Delete file from server');
14    
15  function import_typepad_help($section) {  function import_typepad_menu() {
16    switch ($section) {    $items = array();
17      case 'admin/modules#description':    $items['admin/content/import_typepad'] = array(
18        return t('Import nodes from a typepad file.');      'title' => 'Import Typepad Content',
19      case 'admin/help#import_typepad':      'page callback' => 'import_typepad_page',
20        return t(' ', array('%admin-node-import_typepad' => url('admin/node/import_typepad'), '%admin-access-permission' => url('admin/access/permission')));      'weight' => 5,
21    }      'access arguments' => array('import nodes'),
22  }    );
23      $items['admin/content/import_typepad/preview'] = array(
24  function import_typepad_menu($may_cache) {      'title' => 'type pad',
25    $links = array();      'page callback' => 'import_typepad_preview',
26    if ($may_cache) {      'weight' => 5,
27      $links[] = array('path' => 'admin/node/import_typepad', 'title' => t('type pad'), 'callback' => 'import_typepad_page', 'weight' => 5, 'access' => user_access('import nodes'));      'access arguments' => array('import nodes'),
28      $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);      'type' => MENU_CALLBACK,
29    }    );
30    return $links;    return $items;
31  }  }
32    
33  function import_typepad_perm() {  function import_typepad_perm() {
# Line 37  function import_typepad_perm() { Line 37  function import_typepad_perm() {
37  /**  /**
38   * Show the front page, with either the current working file, or a file selection form   * Show the front page, with either the current working file, or a file selection form
39   */   */
40  function import_typepad_page($form_values=NULL) {  function import_typepad_page() {
41    
42    if ($edit[_WORKING_FILE]) {    if ($edit[_WORKING_FILE]) {
43      $output .= form_item(t('File'), $edit[_WORKING_FILE] .' ('. format_size( filesize($edit[_WORKING_FILE]) ) .')<br />'. form_submit(t('Use a different file')));      $output .= form_item(t('File'), $edit[_WORKING_FILE] .' ('. format_size( filesize($edit[_WORKING_FILE]) ) .')<br />'. form_submit(t('Use a different file')));
44    }    }
   else {  
     $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(  
       '#type' => 'textfield',  
       '#title' => t('Server side file'),  
       '#description' => t('The file located on the server you wish to import'));  
   }  
45    
46    // todo add an insert/update option    // todo add an insert/update option
47      $output = drupal_get_form('import_typepad_form_select');
48    
49      return $output;
50    }
51    
52    function import_typepad_form_select(){
53      $form['upload_file'] = array(
54        '#type' => 'file',
55        '#title' => t('Upload Type Pad/Movable Type file'),
56        '#description' => t('The export file you wish to import'));
57      $form['serverside_file'] = array(
58        '#type' => 'textfield',
59        '#title' => t('Server side file'),
60        '#description' => t('The file located on the server you wish to import'));
61    $form['submit'] = array(    $form['submit'] = array(
62      '#type' => 'submit',      '#type' => 'submit',
63      '#value' => t('Step 2'));      '#value' => t('Step 2'));
64    
65    $form['#attributes'] = array('enctype' => 'multipart/form-data');    $form['#attributes'] = array('enctype' => 'multipart/form-data');
   
   $output = drupal_get_form('typepad_step1', $form);  
66    
67    return $output;    return $form;
68  }  }
69    
70  /**  /**
71   * Discover which file we are using, and move it into the appropriate position   * Discover which file we are using, and move it into the appropriate position
72   */   */
73  function typepad_step1_validate($form_id, $form_values){  function import_typepad_form_select_validate($form, &$form_state){
74    global $user;    global $user;
75    global $base_url;    global $base_url;
76    
# Line 80  function typepad_step1_validate($form_id Line 82  function typepad_step1_validate($form_id
82      //$_SESSION[_IMPORT_TYPEPAD][_WORKING_FILE] = $file->filepath;      //$_SESSION[_IMPORT_TYPEPAD][_WORKING_FILE] = $file->filepath;
83      variable_set(_WORKING_FILE, $file->filepath);      variable_set(_WORKING_FILE, $file->filepath);
84      variable_set(_FILE_TYPE, 'UPLOAD');      variable_set(_FILE_TYPE, 'UPLOAD');
85    } 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'])){
86      drupal_set_message("Using server file ".$form_values['serverside_file']);      drupal_set_message("Using server file ".$form_state['values']['serverside_file']);
87      // See if there was a server file      // See if there was a server file
88      variable_set(_WORKING_FILE, $form_values['serverside_file']);      variable_set(_WORKING_FILE, $form_state['values']['serverside_file']);
89      variable_set(_FILE_TYPE, 'SERVER');      variable_set(_FILE_TYPE, 'SERVER');
90      //$_SESSION[_IMPORT_TYPEPAD][_WORKING_FILE] = $edit['serverfile'];      //$_SESSION[_IMPORT_TYPEPAD][_WORKING_FILE] = $edit['serverfile'];
91    }    }
# Line 96  function typepad_step1_validate($form_id Line 98  function typepad_step1_validate($form_id
98  /**  /**
99   * If the validation passed then simple redirect to the preview   * If the validation passed then simple redirect to the preview
100   */   */
101  function typepad_step1_submit($form_id, $form_values){  function import_typepad_form_select_submit($form, &$form_state){
102    return 'admin/node/import_typepad/preview';    $form_state['redirect'] = 'admin/content/import_typepad/preview';
103  }  }
104    
105  /**  /**
# Line 106  function typepad_step1_submit($form_id, Line 108  function typepad_step1_submit($form_id,
108   * In due course we'll pass the preview count via the parameters   * In due course we'll pass the preview count via the parameters
109   */   */
110  function import_typepad_preview($previewCount = 10) {  function import_typepad_preview($previewCount = 10) {
   $authors = array();  
   $cats = array();  
111    $output = '';    $output = '';
112      $authors = array();
113    
114    $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'], $previewCount, $authors, $cats, array());
115    
116      // Previews
117      $output .= "<h3>".t("Preview Items To Import")."</h3>".$items;
118      $output .= "<h3>".t("Select Import Options")."</h3>".
119                        drupal_get_form('import_typepad_form_process',$authors,$cats);
120    
   // Refresh preview w/ entry count  
121    
122    // Previews    return $output;
123    $output .= $items;  }
124    
125    function import_typepad_form_process($form_state,$authors,$cats){
126    // Mappings and import    // Mappings and import
127    // Get user list    // Get user list
128    $users = array();    $users = array();
# Line 127  function import_typepad_preview($preview Line 132  function import_typepad_preview($preview
132      $users[$user->uid] = $user->name;      $users[$user->uid] = $user->name;
133    }    }
134    
135    $import_form["author_title"] = array(    $import_form["author_mappings"] = array(
136      "#type" => "markup",      "#type" => "fieldset",
137      "#value" => t("<span>Author mappings</span>"));      "#title" => t("Author mappings"));
138    
139    foreach ($authors as $author=>$uid){    foreach ($authors as $author=>$uid){
140      $import_form[$author] = array(      $import_form["author_mappings"][$author] = array(
141        "#type" => 'select',        "#type" => 'select',
142        "#title"=> $author,        "#title"=> $author,
143        '#options' => $users);        '#options' => $users);
144    }    }
145    
146    $import_form["taxonomy_title"] = array(    $import_form["taxonomy_mappings"] = array(
147      "#type" => "markup",      "#type" => "fieldset",
148      "#value" => t("<span>Taxonomy mappings</span>"));      "#title" => t("Taxonomy mappings"));
149    
150    foreach ($cats as $cat=>$count){    foreach ($cats as $cat=>$count){
151      $item = _import_typepad_taxonomy_list($cat);      $item = _import_typepad_taxonomy_list($cat);
152      $item['#title'] = "$cat ($count)";      $item['#title'] = "$cat ($count)";
153      $import_form['taxonomy_'.str_replace(' ','_',$cat)] = $item;      $import_form["taxonomy_mappings"]['taxonomy_'.str_replace(' ','_',$cat)] = $item;
154    }    }
155    
156    $import_form["import_warning"] = array (    $import_form["import_warning"] = array (
157      "#type" => "markup",      "#type" => "markup",
158      "#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>')  );      "#value" => t('Importing may take a while, do not click \'Import\' more than once. To see progress, look at the <a href="?=admin/content/node">administer content</a> page in a new window.<br/><br/>'));
159    
160    $import_form['submit'] = array('#type' => 'submit', '#value' => t('Import'));    $import_form['submit'] = array('#type' => 'submit', '#value' => t('Import'));
161    
162    $output .= drupal_get_form('import_typepad_process', $import_form);    return $import_form;
   
   return $output;  
163  }  }
164    
165  /**  /**
# Line 165  function import_typepad_preview($preview Line 169  function import_typepad_preview($preview
169  function _import_typepad_taxonomy_list($name){  function _import_typepad_taxonomy_list($name){
170    if (user_access('access content')) {    if (user_access('access content')) {
171      $default = '';      $default = '';
172      $vocabs = taxonomy_get_vocabularies('blog');      $vocabs = taxonomy_get_vocabularies();
173      $items = array();      $items = array(0=>t("(none)"));
174      foreach ($vocabs as $vocab) {      foreach ($vocabs as $vocab) {
175        $tree = taxonomy_get_tree($vocab->vid);        $tree = taxonomy_get_tree($vocab->vid);
176        foreach ($tree as $term) {        foreach ($tree as $term) {
177          $items[$term->tid] = _taxonomy_depth($term->depth) .' '.$term->name;          $items[$term->tid] = $vocab->name." | ".$term->name;
178          if ($name == $term->name){          if ($name == $term->name){
179            $default = $term->tid;                  $default = $term->tid;
180          }          }
181        }        }
182      }      }
183      return array(      return array(
# Line 186  function _import_typepad_taxonomy_list($ Line 190  function _import_typepad_taxonomy_list($
190  /**  /**
191   * Simply run the import, passing the form in for the taxonomy and author maps   * Simply run the import, passing the form in for the taxonomy and author maps
192   */   */
193  function import_typepad_process_submit($formid, $form_elements){  function import_typepad_form_process_submit($form, &$form_state){
194    $cats = array(); // unused    $cats = array(); // unused
195    $items = _import_typepad_get_nodes( variable_get(_WORKING_FILE, null), $edit['type'], 0, $form_elements, $cats, $form_elements);    $items = _import_typepad_get_nodes( variable_get(_WORKING_FILE, null), $edit['type'], 0, $form_state['values'], $cats, $form_state['values']);
196    if ( variable_get(_FILE_TYPE, null) == 'UPLOAD'){    if ( variable_get(_FILE_TYPE, null) == 'UPLOAD'){
197      unlink( variable_get(_WORKING_FILE, null) );      unlink( variable_get(_WORKING_FILE, null) );
198    }    }
199    variable_del(_WORKING_FILE);    variable_del(_WORKING_FILE);
200    variable_del(_FILE_TYPE);    variable_del(_FILE_TYPE);
201    return 'admin/node/import_typepad';    $form_state['redirect'] = 'admin/content/import_typepad';
202  }  }
203    
204  /**  /**
# Line 266  function _import_typepad_errors_validate Line 270  function _import_typepad_errors_validate
270   *   *
271   * @return preview   * @return preview
272   */   */
273  function _import_typepad_get_nodes($path, $type, $preview, &$authors, &$cats, $taxonomy) {  function _import_typepad_get_nodes($path, $type, $preview, &$authors, &$cats, $taxonomy, $type="blog") {
274    $handle = fopen($path, 'r');    $handle = fopen($path, 'r');
275    if ($handle == null)    if ($handle == null)
276      return false;      return false;
# Line 345  function _import_typepad_get_nodes($path Line 349  function _import_typepad_get_nodes($path
349            // Start new blog            // Start new blog
350            $blogCats = array();            $blogCats = array();
351            $currentBlog = new StdClass();            $currentBlog = new StdClass();
352            $currentBlog->type = 'blog';            $currentBlog->type = $type;
353            $currentBlog->author = trim( substr($line, strpos($line,"AUTHOR:") + strlen("AUTHOR:")) );            $currentBlog->author = trim( substr($line, strpos($line,"AUTHOR:") + strlen("AUTHOR:")) );
354            // We generate a hash for the author name which can be used in HTML forms            // We generate a hash for the author name which can be used in HTML forms
355            $authorHash = 'author_map_'.str_replace(" ","_",$currentBlog->author);            $authorHash = 'author_map_'.str_replace(" ","_",$currentBlog->author);
# Line 381  function _import_typepad_get_nodes($path Line 385  function _import_typepad_get_nodes($path
385          } else if ($state == 2){          } else if ($state == 2){
386            //echo "<!-- $name $val $sep -->\n";            //echo "<!-- $name $val $sep -->\n";
387            if (strlen($name) > 0 && array_key_exists($name, $mapping)){            if (strlen($name) > 0 && array_key_exists($name, $mapping)){
388              $currentBlog->comments[count($currentBlog->comments)-1]->$mapping[$name] = $value;              $currentBlog->comments[count($currentBlog->comments)-1]->$mapping[$name] = trim($value);
389            } else {            } else {
390              $currentBlog->comments[count($currentBlog->comments)-1]->comment .= $rawLine;              $currentBlog->comments[count($currentBlog->comments)-1]->comment .= trim($rawLine);
391            }            }
392          } else if ($state == 3){          } else if ($state == 3){
393                  // Do nothing (Skip)                  // Do nothing (Skip)
# Line 425  function _import_typepad_get_nodes($path Line 429  function _import_typepad_get_nodes($path
429    }    }
430    */    */
431    if ($success) {    if ($success) {
432      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' => $success . format_plural($success, ' node', ' nodes'))));
433    }    }
434    
435    return $output;    return $output;
# Line 451  function _import_typepad_save($currentBl Line 455  function _import_typepad_save($currentBl
455    
456    // Save taxonomy items    // Save taxonomy items
457    $terms = array_unique($terms);    $terms = array_unique($terms);
458    taxonomy_node_save($currentBlog->nid, $terms);    taxonomy_node_save($currentBlog, $terms);
459    
460    //echo "<!-- Number of comments = ".count($currentBlog->comments)." -->\n";    //echo "<!-- Number of comments = ".count($currentBlog->comments)." -->\n";
461    foreach ($currentBlog->comments as $comment){    foreach ($currentBlog->comments as $comment){
462      // Most of the following code is ripped straight from comment.module      // Most of the following code is ripped straight from comment.module
463      $cid = db_next_id('{comments}_cid');  
   
464      // This is a comment with no parent comment (depth 0): we start      // This is a comment with no parent comment (depth 0): we start
465      // by retrieving the maximum thread level.      // by retrieving the maximum thread level.
466      $max = db_result(db_query('SELECT MAX(thread) FROM {comments} WHERE nid = %d', $edit['nid']));      $max = db_result(db_query('SELECT MAX(thread) FROM {comments} WHERE nid = %d', $edit['nid']));
# Line 486  function _import_typepad_save($currentBl Line 489  function _import_typepad_save($currentBl
489      $thread = $decimals . $units .'/';      $thread = $decimals . $units .'/';
490    
491      // Type pad doesn't have threaded comments, or subject lines, so we have to improvise them      // Type pad doesn't have threaded comments, or subject lines, so we have to improvise them
492          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')",          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);
493            $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);  
494          _comment_update_node_statistics($nid);          db_last_insert_id('comments', 'cid');
495    //      _comment_update_node_statistics($nid);
496    }    }
497  }  }
498  }  }

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.8

  ViewVC Help
Powered by ViewVC 1.1.2