PluginProbe
Movable Type and TypePad Importer / trunk
Movable Type and TypePad Importer vtrunk
trunk 0.3 0.4 0.6 0.6.1 0.6.2 0.6.3 0.6.4
movabletype-importer / movabletype-importer.php

movabletype-importer.php in Movable Type and TypePad Importer trunk, at movabletype-importer.php

606 lines 19.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Movable Type and TypePad Importer
4 Plugin URI: https://wordpress.org/extend/plugins/movabletype-importer/
5 Description: Import posts and comments from a Movable Type or TypePad blog.
6 Author: wordpressdotorg
7 Author URI: https://wordpress.org/
8 Version: 0.6.4
9 License: GPL version 2 or later - https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10 */
11
12 if ( !defined('WP_LOAD_IMPORTERS') )
13 return;
14
15 // define this to false to disallow duplicated comments. Warning: very slow on large imports
16 if ( !defined('WP_MT_IMPORT_ALLOW_DUPE_COMMENTS') ) {
17 define( 'WP_MT_IMPORT_ALLOW_DUPE_COMMENTS', true );
18 }
19
20 // define this to true to force autocommit during import. Warning: extremely slow on large imports.
21 if ( !defined('WP_MT_IMPORT_FORCE_AUTOCOMMIT') ) {
22 define( 'WP_MT_IMPORT_FORCE_AUTOCOMMIT', false );
23 }
24
25 // Load Importer API
26 require_once ABSPATH . 'wp-admin/includes/import.php';
27
28 if ( !class_exists( 'WP_Importer' ) ) {
29 $class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php';
30 if ( file_exists( $class_wp_importer ) )
31 require_once $class_wp_importer;
32 }
33
34 /**
35 * Movable Type and TypePad Importer
36 *
37 * @package WordPress
38 * @subpackage Importer
39 */
40 if ( class_exists( 'WP_Importer' ) ) {
41 class MT_Import extends WP_Importer {
42
43 var $posts = array ();
44 var $file;
45 var $id;
46 var $mtnames = array ();
47 var $newauthornames = array ();
48 var $j = -1;
49
50 function header() {
51 echo '<div class="wrap">';
52
53 if ( version_compare( get_bloginfo( 'version' ), '3.8.0', '<' ) ) {
54 screen_icon();
55 }
56
57 echo '<h2>'.__('Import Movable Type or TypePad', 'movabletype-importer').'</h2>';
58 }
59
60 function footer() {
61 echo '</div>';
62 }
63
64 function greet() {
65 $this->header();
66 ?>
67 <div class="narrow">
68 <p><?php _e( 'Howdy! We are about to begin importing all of your Movable Type or TypePad entries into WordPress. To begin, either choose a file to upload and click &#8220;Upload file and import&#8221;, 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 &#8220;Import mt-export.txt&#8221;.' , 'movabletype-importer'); ?></p>
69
70 <?php wp_import_upload_form( add_query_arg('step', 1) ); ?>
71 <form method="post" action="<?php echo esc_attr(add_query_arg('step', 1)); ?>" class="import-upload-form">
72
73 <?php wp_nonce_field('import-upload'); ?>
74 <p>
75 <input type="hidden" name="upload_type" value="ftp" />
76 <?php _e('Or use <code>mt-export.txt</code> in your <code>/wp-content/</code> directory', 'movabletype-importer'); ?></p>
77 <p class="submit">
78 <input type="submit" class="button" value="<?php esc_attr_e('Import mt-export.txt', 'movabletype-importer'); ?>" />
79 </p>
80 </form>
81 <p><?php _e('The importer is smart enough not to import duplicates, so you can run this multiple times without worry if&#8212;for whatever reason&#8212;it doesn&#8217;t finish. If you get an <strong>out of memory</strong> error try splitting up the import file into pieces.', 'movabletype-importer'); ?> </p>
82 </div>
83 <?php
84 $this->footer();
85 }
86
87 function users_form($n) {
88 $users = version_compare( get_bloginfo( 'version' ), '3.1.0', '<' ) ? get_users_of_blog() : get_users();
89 ?><select name="userselect[<?php echo $n; ?>]">
90 <option value="#NONE#"><?php _e('&mdash; Select &mdash;', 'movabletype-importer') ?></option>
91 <?php
92 foreach ( $users as $user )
93 echo '<option value="' . $user->user_login . '">' . $user->user_login . '</option>';
94 ?>
95 </select>
96 <?php
97 }
98
99 function has_gzip() {
100 return is_callable('gzopen');
101 }
102
103 function fopen($filename, $mode='r') {
104 if ( $this->has_gzip() )
105 return gzopen($filename, $mode);
106 return fopen($filename, $mode);
107 }
108
109 function feof($fp) {
110 if ( $this->has_gzip() )
111 return gzeof($fp);
112 return feof($fp);
113 }
114
115 function fgets($fp, $len=8192) {
116 if ( $this->has_gzip() )
117 return gzgets($fp, $len);
118 return fgets($fp, $len);
119 }
120
121 function fclose($fp) {
122 if ( $this->has_gzip() )
123 return gzclose($fp);
124 return fclose($fp);
125 }
126
127 //function to check the authorname and do the mapping
128 function checkauthor($author) {
129 //mtnames is an array with the names in the mt import file
130 $pass = wp_generate_password();
131 if (!(in_array($author, $this->mtnames))) { //a new mt author name is found
132 ++ $this->j;
133 $this->mtnames[$this->j] = $author; //add that new mt author name to an array
134 $user_id = username_exists($this->newauthornames[$this->j]); //check if the new author name defined by the user is a pre-existing wp user
135 if (!$user_id) { //banging my head against the desk now.
136 if ($this->newauthornames[$this->j] == 'left_blank') { //check if the user does not want to change the authorname
137 $user_id = wp_create_user($author, $pass);
138 $this->newauthornames[$this->j] = $author; //now we have a name, in the place of left_blank.
139 } else {
140 $user_id = wp_create_user($this->newauthornames[$this->j], $pass);
141 }
142 } else {
143 return $user_id; // return pre-existing wp username if it exists
144 }
145 } else {
146 $key = array_search($author, $this->mtnames); //find the array key for $author in the $mtnames array
147 $user_id = username_exists($this->newauthornames[$key]); //use that key to get the value of the author's name from $newauthornames
148 }
149
150 return $user_id;
151 }
152
153 function get_mt_authors() {
154 $temp = array();
155 $authors = array();
156
157 $handle = $this->fopen($this->file, 'r');
158 if ( $handle == null )
159 return false;
160
161 $in_comment = false;
162 while ( $line = $this->fgets($handle) ) {
163 $line = trim($line);
164
165 if ( 'COMMENT:' == $line )
166 $in_comment = true;
167 else if ( '-----' == $line )
168 $in_comment = false;
169
170 if ( $in_comment || 0 !== strpos($line,"AUTHOR:") )
171 continue;
172
173 $temp[] = trim( substr($line, strlen("AUTHOR:")) );
174 }
175
176 //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.
177 $authors[0] = array_shift($temp);
178 $y = count($temp) + 1;
179 for ($x = 1; $x < $y; $x ++) {
180 $next = array_shift($temp);
181 if (!(in_array($next, $authors)))
182 array_push($authors, $next);
183 }
184
185 $this->fclose($handle);
186
187 return $authors;
188 }
189
190 function get_authors_from_post() {
191 $formnames = array ();
192 $selectnames = array ();
193
194 foreach ($_POST['user'] as $key => $line) {
195 $newname = trim(stripslashes($line));
196 if ($newname == '')
197 $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.
198 array_push($formnames, $newname);
199 } // $formnames is the array with the form entered names
200
201 foreach ($_POST['userselect'] as $user => $key) {
202 $selected = trim(stripslashes($key));
203 array_push($selectnames, $selected);
204 }
205
206 $count = count($formnames);
207 for ($i = 0; $i < $count; $i ++) {
208 if ($selectnames[$i] != '#NONE#') { //if no name was selected from the select menu, use the name entered in the form
209 array_push($this->newauthornames, "$selectnames[$i]");
210 } else {
211 array_push($this->newauthornames, "$formnames[$i]");
212 }
213 }
214 }
215
216 function mt_authors_form() {
217 ?>
218 <div class="wrap">
219 <?php
220 if ( version_compare( get_bloginfo( 'version' ), '3.8.0', '<' ) ) {
221 screen_icon();
222 }
223 ?>
224 <h2><?php _e('Assign Authors', 'movabletype-importer'); ?></h2>
225 <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&#8217;s entries.', 'movabletype-importer'); ?></p>
226 <p><?php _e('Below, you can see the names of the authors of the Movable Type 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.', 'movabletype-importer'); ?></p>
227 <p><?php _e('If a new user is created by WordPress, a password will be randomly generated. Manually change the user&#8217;s details if necessary.', 'movabletype-importer'); ?></p>
228 <?php
229
230
231 $authors = $this->get_mt_authors();
232 echo '<ol id="authors">';
233 echo '<form action="?import=mt&amp;step=2&amp;id=' . $this->id . '" method="post">';
234 wp_nonce_field('import-mt');
235 $j = -1;
236 foreach ($authors as $author) {
237 ++ $j;
238 echo '<li><label>'.__('Current author:', 'movabletype-importer').' <strong>'. esc_html($author) .'</strong><br />'.sprintf(__('Create user %1$s or map to existing', 'movabletype-importer'), ' <input type="text" value="'. esc_attr($author) .'" name="'.'user[]'.'" maxlength="30"> <br />');
239 $this->users_form($j);
240 echo '</label></li>';
241 }
242
243 echo '<p class="submit"><input type="submit" class="button" value="'.esc_attr__('Submit', 'movabletype-importer').'"></p>'.'<br />';
244 echo '</form>';
245 echo '</ol></div>';
246
247 }
248
249 function select_authors() {
250 if ( isset( $_POST['upload_type'] ) && $_POST['upload_type'] === 'ftp' ) {
251 $file['file'] = WP_CONTENT_DIR . '/mt-export.txt';
252 if ( !file_exists($file['file']) )
253 $file['error'] = __('<code>mt-export.txt</code> does not exist', 'movabletype-importer');
254 } else {
255 $file = wp_import_handle_upload();
256 }
257 if ( isset($file['error']) ) {
258 $this->header();
259 echo '<p>'.__('Sorry, there has been an error', 'movabletype-importer').'.</p>';
260 echo '<p><strong>' . $file['error'] . '</strong></p>';
261 $this->footer();
262 return;
263 }
264 $this->file = $file['file'];
265 $this->id = array_key_exists( 'id', $file ) ? (int) $file['id'] : 0;
266
267 $this->mt_authors_form();
268 }
269
270 function save_post(&$post, &$comments, &$pings) {
271 $post = get_object_vars($post);
272 $post = add_magic_quotes($post);
273 $post = (object) $post;
274
275 if ( $post_id = post_exists($post->post_title, '', $post->post_date) ) {
276 echo '<li>';
277 printf(__('Post <em>%s</em> already exists.', 'movabletype-importer'), esc_html( stripslashes($post->post_title) ));
278 } else {
279 echo '<li>';
280 printf(__('Importing post <em>%s</em>...', 'movabletype-importer'), esc_html( stripslashes($post->post_title) ));
281
282 if ( '' != trim( $post->extended ) )
283 $post->post_content .= "\n<!--more-->\n$post->extended";
284
285 $post->post_author = $this->checkauthor($post->post_author); //just so that if a post already exists, new users are not created by checkauthor
286 $post_id = wp_insert_post($post);
287 if ( is_wp_error( $post_id ) )
288 return $post_id;
289
290 // Add categories.
291 if ( 0 != count($post->categories) ) {
292 wp_create_categories($post->categories, $post_id);
293 }
294
295 // Add tags or keywords
296 if ( 1 < strlen($post->post_keywords) ) {
297 // Keywords exist.
298 printf('<br />'.__('Adding tags <em>%s</em>...', 'movabletype-importer'), esc_html( stripslashes($post->post_keywords) ));
299 wp_add_post_tags($post_id, $post->post_keywords);
300 }
301 }
302
303 $num_comments = 0;
304 foreach ( $comments as $comment ) {
305 $comment = get_object_vars($comment);
306 $comment = add_magic_quotes($comment);
307
308 if ( WP_MT_IMPORT_ALLOW_DUPE_COMMENTS || !comment_exists($comment['comment_author'], $comment['comment_date'])) {
309 $comment['comment_post_ID'] = $post_id;
310 $comment = wp_filter_comment($comment);
311 wp_insert_comment($comment);
312 $num_comments++;
313 }
314 }
315
316 if ( $num_comments )
317 printf(' '._n('(%s comment)', '(%s comments)', $num_comments, 'movabletype-importer'), $num_comments);
318
319 $num_pings = 0;
320 foreach ( $pings as $ping ) {
321 $ping = get_object_vars($ping);
322 $ping = add_magic_quotes($ping);
323
324 if ( WP_MT_IMPORT_ALLOW_DUPE_COMMENTS || !comment_exists($ping['comment_author'], $ping['comment_date'])) {
325 $ping['comment_content'] = "<strong>{$ping['title']}</strong>\n\n{$ping['comment_content']}";
326 $ping['comment_post_ID'] = $post_id;
327 $ping = wp_filter_comment($ping);
328 wp_insert_comment($ping);
329 $num_pings++;
330 }
331 }
332
333 if ( $num_pings )
334 printf(' '._n('(%s ping)', '(%s pings)', $num_pings, 'movabletype-importer'), $num_pings);
335
336 echo "</li>";
337 //ob_flush();flush();
338 }
339
340 private function create_post() {
341 $post = new StdClass();
342
343 $post->post_content = '';
344 $post->extended = '';
345 $post->post_excerpt = '';
346 $post->post_keywords = '';
347 $post->categories = array();
348
349 return $post;
350 }
351
352 private function create_comment() {
353 $comment = new StdClass();
354
355 $comment->comment_content = '';
356 $comment->comment_author = '';
357 $comment->comment_author_url = '';
358 $comment->comment_author_email = '';
359 $comment->comment_author_IP = '';
360 $comment->comment_date = null;
361 $comment->comment_post_ID = null;
362
363 return $comment;
364 }
365
366 function process_posts() {
367 global $wpdb;
368
369 $handle = $this->fopen($this->file, 'r');
370 if ( $handle == null )
371 return false;
372
373 $context = '';
374 $post = $this->create_post();
375 $comment = $this->create_comment();
376 $comments = array();
377 $ping = $this->create_comment();
378 $pings = array();
379
380 echo "<div class='wrap'><ol>";
381
382 // disable some slowdown points, turn them back on later
383 wp_suspend_cache_invalidation( true );
384 wp_defer_term_counting( true );
385 wp_defer_comment_counting( true );
386
387 // turn off autocommit, for speed
388 if ( !WP_MT_IMPORT_FORCE_AUTOCOMMIT ) {
389 $wpdb->query('SET autocommit = 0');
390 }
391
392 $count = 0;
393 while ( $line = $this->fgets($handle) ) {
394
395 // commit once every 500 posts
396 $count++;
397 if ( !WP_MT_IMPORT_FORCE_AUTOCOMMIT && $count % 500 === 0 ) {
398 $wpdb->query('COMMIT');
399 }
400
401 $line = trim($line);
402
403 if ( '-----' == $line ) {
404 // Finishing a multi-line field
405 if ( 'comment' == $context ) {
406 $comments[] = $comment;
407 $comment = $this->create_comment();
408 } else if ( 'ping' == $context ) {
409 $pings[] = $ping;
410 $ping = $this->create_comment();
411 }
412 $context = '';
413 } else if ( '--------' == $line ) {
414 // Finishing a post.
415 $context = '';
416 $result = $this->save_post( $post, $comments, $pings );
417 if ( is_wp_error( $result ) ) {
418 return $result;
419 }
420
421 $post = $this->create_post();
422 $comment = $this->create_comment();
423 $comments = array();
424 $ping = $this->create_comment();
425 $pings = array();
426 } else if ( 'BODY:' == $line ) {
427 $context = 'body';
428 } else if ( 'EXTENDED BODY:' == $line ) {
429 $context = 'extended';
430 } else if ( 'EXCERPT:' == $line ) {
431 $context = 'excerpt';
432 } else if ( 'KEYWORDS:' == $line ) {
433 $context = 'keywords';
434 } else if ( 'COMMENT:' == $line ) {
435 $context = 'comment';
436 } else if ( 'PING:' == $line ) {
437 $context = 'ping';
438 } else if ( 0 === strpos($line, 'AUTHOR:') ) {
439 $author = trim( substr($line, strlen('AUTHOR:')) );
440 if ( '' == $context )
441 $post->post_author = $author;
442 else if ( 'comment' == $context )
443 $comment->comment_author = $author;
444 } else if ( 0 === strpos($line, 'TITLE:') ) {
445 $title = trim( substr($line, strlen('TITLE:')) );
446 if ( '' == $context )
447 $post->post_title = $title;
448 else if ( 'ping' == $context )
449 $ping->title = $title;
450 } else if ( 0 === strpos($line, 'BASENAME:') ) {
451 $slug = trim( substr($line, strlen('BASENAME:')) );
452 if ( !empty( $slug ) )
453 $post->post_name = $slug;
454 } else if ( 0 === strpos($line, 'STATUS:') ) {
455 $status = trim( strtolower( substr($line, strlen('STATUS:')) ) );
456 if ( empty($status) )
457 $status = 'publish';
458 $post->post_status = $status;
459 } else if ( 0 === strpos($line, 'ALLOW COMMENTS:') ) {
460 $allow = trim( substr($line, strlen('ALLOW COMMENTS:')) );
461 if ( $allow == 1 )
462 $post->comment_status = 'open';
463 else
464 $post->comment_status = 'closed';
465 } else if ( 0 === strpos($line, 'ALLOW PINGS:') ) {
466 $allow = trim( substr($line, strlen('ALLOW PINGS:')) );
467 if ( $allow == 1 )
468 $post->ping_status = 'open';
469 else
470 $post->ping_status = 'closed';
471 } else if ( 0 === strpos($line, 'CATEGORY:') ) {
472 $category = trim( substr($line, strlen('CATEGORY:')) );
473 if ( '' != $category )
474 $post->categories[] = $category;
475 } else if ( 0 === strpos($line, 'PRIMARY CATEGORY:') ) {
476 $category = trim( substr($line, strlen('PRIMARY CATEGORY:')) );
477 if ( '' != $category )
478 $post->categories[] = $category;
479 } else if ( 0 === strpos($line, 'DATE:') ) {
480 $date = trim( substr($line, strlen('DATE:')) );
481 $date = strtotime($date);
482 $date = date('Y-m-d H:i:s', $date);
483 $date_gmt = get_gmt_from_date($date);
484 if ( '' == $context ) {
485 $post->post_modified = $date;
486 $post->post_modified_gmt = $date_gmt;
487 $post->post_date = $date;
488 $post->post_date_gmt = $date_gmt;
489 } else if ( 'comment' == $context ) {
490 $comment->comment_date = $date;
491 } else if ( 'ping' == $context ) {
492 $ping->comment_date = $date;
493 }
494 } else if ( 0 === strpos($line, 'EMAIL:') ) {
495 $email = trim( substr($line, strlen('EMAIL:')) );
496 if ( 'comment' == $context )
497 $comment->comment_author_email = $email;
498 else
499 $ping->comment_author_email = '';
500 } else if ( 0 === strpos($line, 'IP:') ) {
501 $ip = trim( substr($line, strlen('IP:')) );
502 if ( 'comment' == $context )
503 $comment->comment_author_IP = $ip;
504 else
505 $ping->comment_author_IP = $ip;
506 } else if ( 0 === strpos($line, 'URL:') ) {
507 $url = trim( substr($line, strlen('URL:')) );
508 if ( 'comment' == $context )
509 $comment->comment_author_url = $url;
510 else
511 $ping->comment_author_url = $url;
512 } else if ( 0 === strpos($line, 'BLOG NAME:') ) {
513 $blog = trim( substr($line, strlen('BLOG NAME:')) );
514 $ping->comment_author = $blog;
515 } else {
516 // Processing multi-line field, check context.
517
518 if( !empty($line) )
519 $line .= "\n";
520
521 if ( 'body' == $context ) {
522 $post->post_content .= $line;
523 } else if ( 'extended' == $context ) {
524 $post->extended .= $line;
525 } else if ( 'excerpt' == $context ) {
526 $post->post_excerpt .= $line;
527 } else if ( 'keywords' == $context ) {
528 $post->post_keywords .= $line;
529 } else if ( 'comment' == $context ) {
530 $comment->comment_content .= $line;
531 } else if ( 'ping' == $context ) {
532 $ping->comment_content .= $line;
533 }
534 }
535 }
536
537 $this->fclose($handle);
538
539 echo '</ol>';
540
541 // commit the changes, turn autocommit back on
542 if ( !WP_MT_IMPORT_FORCE_AUTOCOMMIT ) {
543 $wpdb->query('COMMIT');
544 $wpdb->query('SET autocommit = 1');
545 }
546
547 // turn basic caching and counting back on, flush the cache. This will also cause a full count to be performed for terms and comments
548 wp_suspend_cache_invalidation( false );
549 wp_cache_flush();
550 wp_defer_term_counting( false );
551 wp_defer_comment_counting( false );
552
553 wp_import_cleanup($this->id);
554 do_action('import_done', 'mt');
555
556 echo '<h3>'.sprintf(__('All done. <a href="%s">Have fun!</a>', 'movabletype-importer'), get_option('home')).'</h3></div>';
557 }
558
559 function import() {
560 $this->id = (int) $_GET['id'];
561 if ( $this->id == 0 )
562 $this->file = WP_CONTENT_DIR . '/mt-export.txt';
563 else
564 $this->file = get_attached_file($this->id);
565 $this->get_authors_from_post();
566 $result = $this->process_posts();
567 if ( is_wp_error( $result ) )
568 return $result;
569 }
570
571 function dispatch() {
572 if (empty ($_GET['step']))
573 $step = 0;
574 else
575 $step = (int) $_GET['step'];
576
577 switch ($step) {
578 case 0 :
579 $this->greet();
580 break;
581 case 1 :
582 check_admin_referer('import-upload');
583 $this->select_authors();
584 break;
585 case 2:
586 check_admin_referer('import-mt');
587 set_time_limit(0);
588 $result = $this->import();
589 if ( is_wp_error( $result ) )
590 echo $result->get_error_message();
591 break;
592 }
593 }
594 }
595
596 $mt_import = new MT_Import();
597
598 register_importer('mt', __('Movable Type and TypePad', 'movabletype-importer'), __('Import posts and comments from a Movable Type or TypePad blog.', 'movabletype-importer'), array ($mt_import, 'dispatch'));
599
600 } // class_exists( 'WP_Importer' )
601
602 function movabletype_importer_init() {
603 load_plugin_textdomain( 'movabletype-importer', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
604 }
605 add_action( 'init', 'movabletype_importer_init' );
606