8 var $mtnames = array ();
9 var $newauthornames = array ();
13 echo '<div class="wrap">';
14 echo '<h2>'.__('Import Movable Type or TypePad').'</h2>';
25 <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>
27 <?php wp_import_upload_form( add_query_arg('step', 1) ); ?>
28 <form method="post" action="<?php echo add_query_arg('step', 1); ?>" class="import-upload-form">
30 <?php wp_nonce_field('import-upload'); ?>
32 <input type="hidden" name="upload_type" value="ftp" />
33 <?php _e('Or use <code>mt-export.txt</code> in your <code>/wp-content/</code> directory'); ?></p>
35 <input type="submit" value="<?php echo attribute_escape(__('Import mt-export.txt')); ?>" />
38 <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>
44 function users_form($n) {
45 global $wpdb, $testing;
46 $users = $wpdb->get_results("SELECT * FROM $wpdb->users ORDER BY ID");
47 ?><select name="userselect[<?php echo $n; ?>]">
48 <option value="#NONE#"><?php _e('- Select -') ?></option>
52 foreach ($users as $user) {
53 echo '<option value="'.$user->user_login.'">'.$user->user_login.'</option>';
62 //function to check the authorname and do the mapping
63 function checkauthor($author) {
65 //mtnames is an array with the names in the mt import file
66 $pass = wp_generate_password();
67 if (!(in_array($author, $this->mtnames))) { //a new mt author name is found
69 $this->mtnames[$this->j] = $author; //add that new mt author name to an array
70 $user_id = username_exists($this->newauthornames[$this->j]); //check if the new author name defined by the user is a pre-existing wp user
71 if (!$user_id) { //banging my head against the desk now.
72 if ($newauthornames[$this->j] == 'left_blank') { //check if the user does not want to change the authorname
73 $user_id = wp_create_user($author, $pass);
74 $this->newauthornames[$this->j] = $author; //now we have a name, in the place of left_blank.
76 $user_id = wp_create_user($this->newauthornames[$this->j], $pass);
79 return $user_id; // return pre-existing wp username if it exists
82 $key = array_search($author, $this->mtnames); //find the array key for $author in the $mtnames array
83 $user_id = username_exists($this->newauthornames[$key]); //use that key to get the value of the author's name from $newauthornames
89 function get_mt_authors() {
93 $handle = fopen($this->file, 'r');
94 if ( $handle == null )
98 while ( $line = fgets($handle) ) {
101 if ( 'COMMENT:' == $line )
103 else if ( '-----' == $line )
106 if ( $in_comment || 0 !== strpos($line,"AUTHOR:") )
109 $temp[] = trim( substr($line, strlen("AUTHOR:")) );
112 //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.
113 $authors[0] = array_shift($temp);
114 $y = count($temp) + 1;
115 for ($x = 1; $x < $y; $x ++) {
116 $next = array_shift($temp);
117 if (!(in_array($next, $authors)))
118 array_push($authors, "$next");
126 function get_authors_from_post() {
127 $formnames = array ();
128 $selectnames = array ();
130 foreach ($_POST['user'] as $key => $line) {
131 $newname = trim(stripslashes($line));
133 $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.
134 array_push($formnames, "$newname");
135 } // $formnames is the array with the form entered names
137 foreach ($_POST['userselect'] as $user => $key) {
138 $selected = trim(stripslashes($key));
139 array_push($selectnames, "$selected");
142 $count = count($formnames);
143 for ($i = 0; $i < $count; $i ++) {
144 if ($selectnames[$i] != '#NONE#') { //if no name was selected from the select menu, use the name entered in the form
145 array_push($this->newauthornames, "$selectnames[$i]");
147 array_push($this->newauthornames, "$formnames[$i]");
152 function mt_authors_form() {
155 <h2><?php _e('Assign Authors'); ?></h2>
156 <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>
157 <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>
158 <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>
162 $authors = $this->get_mt_authors();
163 echo '<ol id="authors">';
164 echo '<form action="?import=mt&step=2&id=' . $this->id . '" method="post">';
165 wp_nonce_field('import-mt');
167 foreach ($authors as $author) {
169 echo '<li><label>'.__('Current author:').' <strong>'.$author.'</strong><br />'.sprintf(__('Create user %1$s or map to existing'), ' <input type="text" value="'.$author.'" name="'.'user[]'.'" maxlength="30"> <br />');
170 $this->users_form($j);
171 echo '</label></li>';
174 echo '<input type="submit" value="'.__('Submit').'">'.'<br />';
180 function select_authors() {
181 if ( $_POST['upload_type'] === 'ftp' ) {
182 $file['file'] = WP_CONTENT_DIR . '/mt-export.txt';
183 if ( !file_exists($file['file']) )
184 $file['error'] = __('<code>mt-export.txt</code> does not exist');
186 $file = wp_import_handle_upload();
188 if ( isset($file['error']) ) {
190 echo '<p>'.__('Sorry, there has been an error').'.</p>';
191 echo '<p><strong>' . $file['error'] . '</strong></p>';
195 $this->file = $file['file'];
196 $this->id = (int) $file['id'];
198 $this->mt_authors_form();
201 function save_post(&$post, &$comments, &$pings) {
204 $post = get_object_vars($post);
205 $post = add_magic_quotes($post);
206 $post = (object) $post;
208 if ( $post_id = post_exists($post->post_title, '', $post->post_date) ) {
210 printf(__('Post <em>%s</em> already exists.'), stripslashes($post->post_title));
213 printf(__('Importing post <em>%s</em>...'), stripslashes($post->post_title));
215 if ( '' != trim( $post->extended ) )
216 $post->post_content .= "\n<!--more-->\n$post->extended";
218 $post->post_author = $this->checkauthor($post->post_author); //just so that if a post already exists, new users are not created by checkauthor
219 $post_id = wp_insert_post($post);
220 if ( is_wp_error( $post_id ) )
224 if ( 0 != count($post->categories) ) {
225 wp_create_categories($post->categories, $post_id);
228 // Add tags or keywords
229 if ( 1 < strlen($post->post_keywords) ) {
231 printf(__('<br />Adding tags <i>%s</i>...'), stripslashes($post->post_keywords));
232 wp_add_post_tags($post_id, $post->post_keywords);
237 foreach ( $comments as $comment ) {
238 $comment = get_object_vars($comment);
239 $comment = add_magic_quotes($comment);
241 if ( !comment_exists($comment['comment_author'], $comment['comment_date'])) {
242 $comment['comment_post_ID'] = $post_id;
243 $comment = wp_filter_comment($comment);
244 wp_insert_comment($comment);
250 printf(' '.__ngettext('(%s comment)', '(%s comments)', $num_comments), $num_comments);
253 foreach ( $pings as $ping ) {
254 $ping = get_object_vars($ping);
255 $ping = add_magic_quotes($ping);
257 if ( !comment_exists($ping['comment_author'], $ping['comment_date'])) {
258 $ping['comment_content'] = "<strong>{$ping['title']}</strong>\n\n{$ping['comment_content']}";
259 $ping['comment_post_ID'] = $post_id;
260 $ping = wp_filter_comment($ping);
261 wp_insert_comment($ping);
267 printf(' '.__ngettext('(%s ping)', '(%s pings)', $num_pings), $num_pings);
270 //ob_flush();flush();
273 function process_posts() {
276 $handle = fopen($this->file, 'r');
277 if ( $handle == null )
281 $post = new StdClass();
282 $comment = new StdClass();
284 $ping = new StdClass();
287 echo "<div class='wrap'><ol>";
289 while ( $line = fgets($handle) ) {
292 if ( '-----' == $line ) {
293 // Finishing a multi-line field
294 if ( 'comment' == $context ) {
295 $comments[] = $comment;
296 $comment = new StdClass();
297 } else if ( 'ping' == $context ) {
299 $ping = new StdClass();
302 } else if ( '--------' == $line ) {
305 $result = $this->save_post($post, $comments, $pings);
306 if ( is_wp_error( $result ) )
308 $post = new StdClass;
309 $comment = new StdClass();
310 $ping = new StdClass();
313 } else if ( 'BODY:' == $line ) {
315 } else if ( 'EXTENDED BODY:' == $line ) {
316 $context = 'extended';
317 } else if ( 'EXCERPT:' == $line ) {
318 $context = 'excerpt';
319 } else if ( 'KEYWORDS:' == $line ) {
320 $context = 'keywords';
321 } else if ( 'COMMENT:' == $line ) {
322 $context = 'comment';
323 } else if ( 'PING:' == $line ) {
325 } else if ( 0 === strpos($line, "AUTHOR:") ) {
326 $author = trim( substr($line, strlen("AUTHOR:")) );
327 if ( '' == $context )
328 $post->post_author = $author;
329 else if ( 'comment' == $context )
330 $comment->comment_author = $author;
331 } else if ( 0 === strpos($line, "TITLE:") ) {
332 $title = trim( substr($line, strlen("TITLE:")) );
333 if ( '' == $context )
334 $post->post_title = $title;
335 else if ( 'ping' == $context )
336 $ping->title = $title;
337 } else if ( 0 === strpos($line, "STATUS:") ) {
338 $status = trim( strtolower( substr($line, strlen("STATUS:")) ) );
339 if ( empty($status) )
341 $post->post_status = $status;
342 } else if ( 0 === strpos($line, "ALLOW COMMENTS:") ) {
343 $allow = trim( substr($line, strlen("ALLOW COMMENTS:")) );
345 $post->comment_status = 'open';
347 $post->comment_status = 'closed';
348 } else if ( 0 === strpos($line, "ALLOW PINGS:") ) {
349 $allow = trim( substr($line, strlen("ALLOW PINGS:")) );
351 $post->ping_status = 'open';
353 $post->ping_status = 'closed';
354 } else if ( 0 === strpos($line, "CATEGORY:") ) {
355 $category = trim( substr($line, strlen("CATEGORY:")) );
356 if ( '' != $category )
357 $post->categories[] = $category;
358 } else if ( 0 === strpos($line, "PRIMARY CATEGORY:") ) {
359 $category = trim( substr($line, strlen("PRIMARY CATEGORY:")) );
360 if ( '' != $category )
361 $post->categories[] = $category;
362 } else if ( 0 === strpos($line, "DATE:") ) {
363 $date = trim( substr($line, strlen("DATE:")) );
364 $date = strtotime($date);
365 $date = date('Y-m-d H:i:s', $date);
366 $date_gmt = get_gmt_from_date($date);
367 if ( '' == $context ) {
368 $post->post_modified = $date;
369 $post->post_modified_gmt = $date_gmt;
370 $post->post_date = $date;
371 $post->post_date_gmt = $date_gmt;
372 } else if ( 'comment' == $context ) {
373 $comment->comment_date = $date;
374 } else if ( 'ping' == $context ) {
375 $ping->comment_date = $date;
377 } else if ( 0 === strpos($line, "EMAIL:") ) {
378 $email = trim( substr($line, strlen("EMAIL:")) );
379 if ( 'comment' == $context )
380 $comment->comment_author_email = $email;
382 $ping->comment_author_email = '';
383 } else if ( 0 === strpos($line, "IP:") ) {
384 $ip = trim( substr($line, strlen("IP:")) );
385 if ( 'comment' == $context )
386 $comment->comment_author_IP = $ip;
388 $ping->comment_author_IP = $ip;
389 } else if ( 0 === strpos($line, "URL:") ) {
390 $url = trim( substr($line, strlen("URL:")) );
391 if ( 'comment' == $context )
392 $comment->comment_author_url = $url;
394 $ping->comment_author_url = $url;
395 } else if ( 0 === strpos($line, "BLOG NAME:") ) {
396 $blog = trim( substr($line, strlen("BLOG NAME:")) );
397 $ping->comment_author = $blog;
399 // Processing multi-line field, check context.
402 if ( 'body' == $context ) {
403 $post->post_content .= $line;
404 } else if ( 'extended' == $context ) {
405 $post->extended .= $line;
406 } else if ( 'excerpt' == $context ) {
407 $post->post_excerpt .= $line;
408 } else if ( 'keywords' == $context ) {
409 $post->post_keywords .= $line;
410 } else if ( 'comment' == $context ) {
411 $comment->comment_content .= $line;
412 } else if ( 'ping' == $context ) {
413 $ping->comment_content .= $line;
420 wp_import_cleanup($this->id);
421 do_action('import_done', 'mt');
423 echo '<h3>'.sprintf(__('All done. <a href="%s">Have fun!</a>'), get_option('home')).'</h3></div>';
427 $this->id = (int) $_GET['id'];
428 if ( $this->id == 0 )
429 $this->file = WP_CONTENT_DIR . '/mt-export.txt';
431 $this->file = get_attached_file($this->id);
432 $this->get_authors_from_post();
433 $result = $this->process_posts();
434 if ( is_wp_error( $result ) )
438 function dispatch() {
439 if (empty ($_GET['step']))
442 $step = (int) $_GET['step'];
449 check_admin_referer('import-upload');
450 $this->select_authors();
453 check_admin_referer('import-mt');
454 $result = $this->import();
455 if ( is_wp_error( $result ) )
456 echo $result->get_error_message();
461 function MT_Import() {
466 $mt_import = new MT_Import();
468 register_importer('mt', __('Movable Type and TypePad'), __('Import posts and comments from a Movable Type or Typepad blog.'), array ($mt_import, 'dispatch'));