3 * Movable Type and TypePad Importer
10 * Moveable Type and TypePad Importer class
12 * Upload your exported Movable Type or TypePad entries into WordPress.
18 var $posts = array ();
21 var $mtnames = array ();
22 var $newauthornames = array ();
26 echo '<div class="wrap">';
28 echo '<h2>'.__('Import Movable Type or TypePad').'</h2>';
39 <p><?php _e('Howdy! We’re about to begin importing all of your Movable Type or TypePad entries into WordPress. To begin, either choose a file to upload and click “Upload file and import”, or use FTP to upload your MT export file as <code>mt-export.txt</code> in your <code>/wp-content/</code> directory and then click "Import mt-export.txt"'); ?></p>
41 <?php wp_import_upload_form( add_query_arg('step', 1) ); ?>
42 <form method="post" action="<?php echo esc_attr(add_query_arg('step', 1)); ?>" class="import-upload-form">
44 <?php wp_nonce_field('import-upload'); ?>
46 <input type="hidden" name="upload_type" value="ftp" />
47 <?php _e('Or use <code>mt-export.txt</code> in your <code>/wp-content/</code> directory'); ?></p>
49 <input type="submit" class="button" value="<?php esc_attr_e('Import mt-export.txt'); ?>" />
52 <p><?php _e('The importer is smart enough not to import duplicates, so you can run this multiple times without worry if—for whatever reason—it doesn’t finish. If you get an <strong>out of memory</strong> error try splitting up the import file into pieces.'); ?> </p>
58 function users_form($n) {
60 $users = $wpdb->get_results("SELECT * FROM $wpdb->users ORDER BY ID");
61 ?><select name="userselect[<?php echo $n; ?>]">
62 <option value="#NONE#"><?php _e('- Select -') ?></option>
66 foreach ($users as $user) {
67 echo '<option value="'.$user->user_login.'">'.$user->user_login.'</option>';
76 return is_callable('gzopen');
79 function fopen($filename, $mode='r') {
80 if ( $this->has_gzip() )
81 return gzopen($filename, $mode);
82 return fopen($filename, $mode);
86 if ( $this->has_gzip() )
91 function fgets($fp, $len=8192) {
92 if ( $this->has_gzip() )
93 return gzgets($fp, $len);
94 return fgets($fp, $len);
97 function fclose($fp) {
98 if ( $this->has_gzip() )
103 //function to check the authorname and do the mapping
104 function checkauthor($author) {
105 //mtnames is an array with the names in the mt import file
106 $pass = wp_generate_password();
107 if (!(in_array($author, $this->mtnames))) { //a new mt author name is found
109 $this->mtnames[$this->j] = $author; //add that new mt author name to an array
110 $user_id = username_exists($this->newauthornames[$this->j]); //check if the new author name defined by the user is a pre-existing wp user
111 if (!$user_id) { //banging my head against the desk now.
112 if ($this->newauthornames[$this->j] == 'left_blank') { //check if the user does not want to change the authorname
113 $user_id = wp_create_user($author, $pass);
114 $this->newauthornames[$this->j] = $author; //now we have a name, in the place of left_blank.
116 $user_id = wp_create_user($this->newauthornames[$this->j], $pass);
119 return $user_id; // return pre-existing wp username if it exists
122 $key = array_search($author, $this->mtnames); //find the array key for $author in the $mtnames array
123 $user_id = username_exists($this->newauthornames[$key]); //use that key to get the value of the author's name from $newauthornames
129 function get_mt_authors() {
133 $handle = $this->fopen($this->file, 'r');
134 if ( $handle == null )
138 while ( $line = $this->fgets($handle) ) {
141 if ( 'COMMENT:' == $line )
143 else if ( '-----' == $line )
146 if ( $in_comment || 0 !== strpos($line,"AUTHOR:") )
149 $temp[] = trim( substr($line, strlen("AUTHOR:")) );
152 //we need to find unique values of author names, while preserving the order, so this function emulates the unique_value(); php function, without the sorting.
153 $authors[0] = array_shift($temp);
154 $y = count($temp) + 1;
155 for ($x = 1; $x < $y; $x ++) {
156 $next = array_shift($temp);
157 if (!(in_array($next, $authors)))
158 array_push($authors, "$next");
161 $this->fclose($handle);
166 function get_authors_from_post() {
167 $formnames = array ();
168 $selectnames = array ();
170 foreach ($_POST['user'] as $key => $line) {
171 $newname = trim(stripslashes($line));
173 $newname = 'left_blank'; //passing author names from step 1 to step 2 is accomplished by using POST. left_blank denotes an empty entry in the form.
174 array_push($formnames, "$newname");
175 } // $formnames is the array with the form entered names
177 foreach ($_POST['userselect'] as $user => $key) {
178 $selected = trim(stripslashes($key));
179 array_push($selectnames, "$selected");
182 $count = count($formnames);
183 for ($i = 0; $i < $count; $i ++) {
184 if ($selectnames[$i] != '#NONE#') { //if no name was selected from the select menu, use the name entered in the form
185 array_push($this->newauthornames, "$selectnames[$i]");
187 array_push($this->newauthornames, "$formnames[$i]");
192 function mt_authors_form() {
195 <?php screen_icon(); ?>
196 <h2><?php _e('Assign Authors'); ?></h2>
197 <p><?php _e('To make it easier for you to edit and save the imported posts and drafts, you may want to change the name of the author of the posts. For example, you may want to import all the entries as admin’s entries.'); ?></p>
198 <p><?php _e('Below, you can see the names of the authors of the MovableType posts in <em>italics</em>. For each of these names, you can either pick an author in your WordPress installation from the menu, or enter a name for the author in the textbox.'); ?></p>
199 <p><?php _e('If a new user is created by WordPress, a password will be randomly generated. Manually change the user’s details if necessary.'); ?></p>
203 $authors = $this->get_mt_authors();
204 echo '<ol id="authors">';
205 echo '<form action="?import=mt&step=2&id=' . $this->id . '" method="post">';
206 wp_nonce_field('import-mt');
208 foreach ($authors as $author) {
210 echo '<li><label>'.__('Current author:').' <strong>'.$author.'</strong><br />'.sprintf(__('Create user %1$s or map to existing'), ' <input type="text" value="'. esc_attr($author) .'" name="'.'user[]'.'" maxlength="30"> <br />');
211 $this->users_form($j);
212 echo '</label></li>';
215 echo '<p class="submit"><input type="submit" class="button" value="'.esc_attr__('Submit').'"></p>'.'<br />';
221 function select_authors() {
222 if ( $_POST['upload_type'] === 'ftp' ) {
223 $file['file'] = WP_CONTENT_DIR . '/mt-export.txt';
224 if ( !file_exists($file['file']) )
225 $file['error'] = __('<code>mt-export.txt</code> does not exist');
227 $file = wp_import_handle_upload();
229 if ( isset($file['error']) ) {
231 echo '<p>'.__('Sorry, there has been an error').'.</p>';
232 echo '<p><strong>' . $file['error'] . '</strong></p>';
236 $this->file = $file['file'];
237 $this->id = (int) $file['id'];
239 $this->mt_authors_form();
242 function save_post(&$post, &$comments, &$pings) {
245 $post = get_object_vars($post);
246 $post = add_magic_quotes($post);
247 $post = (object) $post;
249 if ( $post_id = post_exists($post->post_title, '', $post->post_date) ) {
251 printf(__('Post <em>%s</em> already exists.'), stripslashes($post->post_title));
254 printf(__('Importing post <em>%s</em>...'), stripslashes($post->post_title));
256 if ( '' != trim( $post->extended ) )
257 $post->post_content .= "\n<!--more-->\n$post->extended";
259 $post->post_author = $this->checkauthor($post->post_author); //just so that if a post already exists, new users are not created by checkauthor
260 $post_id = wp_insert_post($post);
261 if ( is_wp_error( $post_id ) )
265 if ( 0 != count($post->categories) ) {
266 wp_create_categories($post->categories, $post_id);
269 // Add tags or keywords
270 if ( 1 < strlen($post->post_keywords) ) {
272 printf(__('<br />Adding tags <i>%s</i>...'), stripslashes($post->post_keywords));
273 wp_add_post_tags($post_id, $post->post_keywords);
278 foreach ( $comments as $comment ) {
279 $comment = get_object_vars($comment);
280 $comment = add_magic_quotes($comment);
282 if ( !comment_exists($comment['comment_author'], $comment['comment_date'])) {
283 $comment['comment_post_ID'] = $post_id;
284 $comment = wp_filter_comment($comment);
285 wp_insert_comment($comment);
291 printf(' '._n('(%s comment)', '(%s comments)', $num_comments), $num_comments);
294 foreach ( $pings as $ping ) {
295 $ping = get_object_vars($ping);
296 $ping = add_magic_quotes($ping);
298 if ( !comment_exists($ping['comment_author'], $ping['comment_date'])) {
299 $ping['comment_content'] = "<strong>{$ping['title']}</strong>\n\n{$ping['comment_content']}";
300 $ping['comment_post_ID'] = $post_id;
301 $ping = wp_filter_comment($ping);
302 wp_insert_comment($ping);
308 printf(' '._n('(%s ping)', '(%s pings)', $num_pings), $num_pings);
311 //ob_flush();flush();
314 function process_posts() {
317 $handle = $this->fopen($this->file, 'r');
318 if ( $handle == null )
322 $post = new StdClass();
323 $comment = new StdClass();
325 $ping = new StdClass();
328 echo "<div class='wrap'><ol>";
330 while ( $line = $this->fgets($handle) ) {
333 if ( '-----' == $line ) {
334 // Finishing a multi-line field
335 if ( 'comment' == $context ) {
336 $comments[] = $comment;
337 $comment = new StdClass();
338 } else if ( 'ping' == $context ) {
340 $ping = new StdClass();
343 } else if ( '--------' == $line ) {
346 $result = $this->save_post($post, $comments, $pings);
347 if ( is_wp_error( $result ) )
349 $post = new StdClass;
350 $comment = new StdClass();
351 $ping = new StdClass();
354 } else if ( 'BODY:' == $line ) {
356 } else if ( 'EXTENDED BODY:' == $line ) {
357 $context = 'extended';
358 } else if ( 'EXCERPT:' == $line ) {
359 $context = 'excerpt';
360 } else if ( 'KEYWORDS:' == $line ) {
361 $context = 'keywords';
362 } else if ( 'COMMENT:' == $line ) {
363 $context = 'comment';
364 } else if ( 'PING:' == $line ) {
366 } else if ( 0 === strpos($line, "AUTHOR:") ) {
367 $author = trim( substr($line, strlen("AUTHOR:")) );
368 if ( '' == $context )
369 $post->post_author = $author;
370 else if ( 'comment' == $context )
371 $comment->comment_author = $author;
372 } else if ( 0 === strpos($line, "TITLE:") ) {
373 $title = trim( substr($line, strlen("TITLE:")) );
374 if ( '' == $context )
375 $post->post_title = $title;
376 else if ( 'ping' == $context )
377 $ping->title = $title;
378 } else if ( 0 === strpos($line, "STATUS:") ) {
379 $status = trim( strtolower( substr($line, strlen("STATUS:")) ) );
380 if ( empty($status) )
382 $post->post_status = $status;
383 } else if ( 0 === strpos($line, "ALLOW COMMENTS:") ) {
384 $allow = trim( substr($line, strlen("ALLOW COMMENTS:")) );
386 $post->comment_status = 'open';
388 $post->comment_status = 'closed';
389 } else if ( 0 === strpos($line, "ALLOW PINGS:") ) {
390 $allow = trim( substr($line, strlen("ALLOW PINGS:")) );
392 $post->ping_status = 'open';
394 $post->ping_status = 'closed';
395 } else if ( 0 === strpos($line, "CATEGORY:") ) {
396 $category = trim( substr($line, strlen("CATEGORY:")) );
397 if ( '' != $category )
398 $post->categories[] = $category;
399 } else if ( 0 === strpos($line, "PRIMARY CATEGORY:") ) {
400 $category = trim( substr($line, strlen("PRIMARY CATEGORY:")) );
401 if ( '' != $category )
402 $post->categories[] = $category;
403 } else if ( 0 === strpos($line, "DATE:") ) {
404 $date = trim( substr($line, strlen("DATE:")) );
405 $date = strtotime($date);
406 $date = date('Y-m-d H:i:s', $date);
407 $date_gmt = get_gmt_from_date($date);
408 if ( '' == $context ) {
409 $post->post_modified = $date;
410 $post->post_modified_gmt = $date_gmt;
411 $post->post_date = $date;
412 $post->post_date_gmt = $date_gmt;
413 } else if ( 'comment' == $context ) {
414 $comment->comment_date = $date;
415 } else if ( 'ping' == $context ) {
416 $ping->comment_date = $date;
418 } else if ( 0 === strpos($line, "EMAIL:") ) {
419 $email = trim( substr($line, strlen("EMAIL:")) );
420 if ( 'comment' == $context )
421 $comment->comment_author_email = $email;
423 $ping->comment_author_email = '';
424 } else if ( 0 === strpos($line, "IP:") ) {
425 $ip = trim( substr($line, strlen("IP:")) );
426 if ( 'comment' == $context )
427 $comment->comment_author_IP = $ip;
429 $ping->comment_author_IP = $ip;
430 } else if ( 0 === strpos($line, "URL:") ) {
431 $url = trim( substr($line, strlen("URL:")) );
432 if ( 'comment' == $context )
433 $comment->comment_author_url = $url;
435 $ping->comment_author_url = $url;
436 } else if ( 0 === strpos($line, "BLOG NAME:") ) {
437 $blog = trim( substr($line, strlen("BLOG NAME:")) );
438 $ping->comment_author = $blog;
440 // Processing multi-line field, check context.
445 if ( 'body' == $context ) {
446 $post->post_content .= $line;
447 } else if ( 'extended' == $context ) {
448 $post->extended .= $line;
449 } else if ( 'excerpt' == $context ) {
450 $post->post_excerpt .= $line;
451 } else if ( 'keywords' == $context ) {
452 $post->post_keywords .= $line;
453 } else if ( 'comment' == $context ) {
454 $comment->comment_content .= $line;
455 } else if ( 'ping' == $context ) {
456 $ping->comment_content .= $line;
461 $this->fclose($handle);
465 wp_import_cleanup($this->id);
466 do_action('import_done', 'mt');
468 echo '<h3>'.sprintf(__('All done. <a href="%s">Have fun!</a>'), get_option('home')).'</h3></div>';
472 $this->id = (int) $_GET['id'];
473 if ( $this->id == 0 )
474 $this->file = WP_CONTENT_DIR . '/mt-export.txt';
476 $this->file = get_attached_file($this->id);
477 $this->get_authors_from_post();
478 $result = $this->process_posts();
479 if ( is_wp_error( $result ) )
483 function dispatch() {
484 if (empty ($_GET['step']))
487 $step = (int) $_GET['step'];
494 check_admin_referer('import-upload');
495 $this->select_authors();
498 check_admin_referer('import-mt');
499 $result = $this->import();
500 if ( is_wp_error( $result ) )
501 echo $result->get_error_message();
506 function MT_Import() {
511 $mt_import = new MT_Import();
513 register_importer('mt', __('Movable Type and TypePad'), __('Import posts and comments from a Movable Type or TypePad blog.'), array ($mt_import, 'dispatch'));