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>
26 <?php wp_import_upload_form( add_query_arg('step', 1) ); ?>
27 <form method="post" action="<?php echo add_query_arg('step', 1); ?>" class="import-upload-form">
28 <?php wp_nonce_field('import-upload'); ?>
30 <input type="hidden" name="upload_type" value="ftp" />
31 <?php _e('Or use <code>mt-export.txt</code> in your <code>/wp-content/</code> directory'); ?></p>
33 <input type="submit" value="<?php echo attribute_escape(__('Import mt-export.txt »')); ?>" />
36 <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>
42 function users_form($n) {
43 global $wpdb, $testing;
44 $users = $wpdb->get_results("SELECT * FROM $wpdb->users ORDER BY ID");
45 ?><select name="userselect[<?php echo $n; ?>]">
46 <option value="#NONE#"><?php _e('- Select -') ?></option>
50 foreach ($users as $user) {
51 echo '<option value="'.$user->user_login.'">'.$user->user_login.'</option>';
60 //function to check the authorname and do the mapping
61 function checkauthor($author) {
63 //mtnames is an array with the names in the mt import file
65 if (!(in_array($author, $this->mtnames))) { //a new mt author name is found
67 $this->mtnames[$this->j] = $author; //add that new mt author name to an array
68 $user_id = username_exists($this->newauthornames[$this->j]); //check if the new author name defined by the user is a pre-existing wp user
69 if (!$user_id) { //banging my head against the desk now.
70 if ($newauthornames[$this->j] == 'left_blank') { //check if the user does not want to change the authorname
71 $user_id = wp_create_user($author, $pass);
72 $this->newauthornames[$this->j] = $author; //now we have a name, in the place of left_blank.
74 $user_id = wp_create_user($this->newauthornames[$this->j], $pass);
77 return $user_id; // return pre-existing wp username if it exists
80 $key = array_search($author, $this->mtnames); //find the array key for $author in the $mtnames array
81 $user_id = username_exists($this->newauthornames[$key]); //use that key to get the value of the author's name from $newauthornames
87 function get_mt_authors() {
91 $handle = fopen($this->file, 'r');
92 if ( $handle == null )
96 while ( $line = fgets($handle) ) {
99 if ( 'COMMENT:' == $line )
101 else if ( '-----' == $line )
104 if ( $in_comment || 0 !== strpos($line,"AUTHOR:") )
107 $temp[] = trim( substr($line, strlen("AUTHOR:")) );
110 //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.
111 $authors[0] = array_shift($temp);
112 $y = count($temp) + 1;
113 for ($x = 1; $x < $y; $x ++) {
114 $next = array_shift($temp);
115 if (!(in_array($next, $authors)))
116 array_push($authors, "$next");
124 function get_authors_from_post() {
125 $formnames = array ();
126 $selectnames = array ();
128 foreach ($_POST['user'] as $key => $line) {
129 $newname = trim(stripslashes($line));
131 $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.
132 array_push($formnames, "$newname");
133 } // $formnames is the array with the form entered names
135 foreach ($_POST['userselect'] as $user => $key) {
136 $selected = trim(stripslashes($key));
137 array_push($selectnames, "$selected");
140 $count = count($formnames);
141 for ($i = 0; $i < $count; $i ++) {
142 if ($selectnames[$i] != '#NONE#') { //if no name was selected from the select menu, use the name entered in the form
143 array_push($this->newauthornames, "$selectnames[$i]");
145 array_push($this->newauthornames, "$formnames[$i]");
150 function mt_authors_form() {
153 <h2><?php _e('Assign Authors'); ?></h2>
154 <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>
155 <p><?php _e('Below, you can see the names of the authors of the MovableType posts in <i>italics</i>. 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>
156 <p><?php _e('If a new user is created by WordPress, the password will be set, by default, to "changeme". Quite suggestive, eh? ;)'); ?></p>
160 $authors = $this->get_mt_authors();
161 echo '<ol id="authors">';
162 echo '<form action="?import=mt&step=2&id=' . $this->id . '" method="post">';
163 wp_nonce_field('import-mt');
165 foreach ($authors as $author) {
167 echo '<li>'.__('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 />');
168 $this->users_form($j);
172 echo '<input type="submit" value="'.__('Submit').'">'.'<br />';
178 function select_authors() {
179 if ( $_POST['upload_type'] === 'ftp' ) {
180 $file['file'] = ABSPATH . 'wp-content/mt-export.txt';
181 if ( !file_exists($file['file']) )
182 $file['error'] = __('<code>mt-export.txt</code> does not exist');
184 $file = wp_import_handle_upload();
186 if ( isset($file['error']) ) {
188 echo '<p>'.__('Sorry, there has been an error').'.</p>';
189 echo '<p><strong>' . $file['error'] . '</strong></p>';
193 $this->file = $file['file'];
194 $this->id = (int) $file['id'];
196 $this->mt_authors_form();
199 function save_post(&$post, &$comments, &$pings) {
202 $post = get_object_vars($post);
203 $post = add_magic_quotes($post);
204 $post = (object) $post;
206 if ( $post_id = post_exists($post->post_title, '', $post->post_date) ) {
208 printf(__('Post <i>%s</i> already exists.'), stripslashes($post->post_title));
211 printf(__('Importing post <i>%s</i>...'), stripslashes($post->post_title));
213 if ( '' != trim( $post->extended ) )
214 $post->post_content .= "\n<!--more-->\n$post->extended";
216 $post->post_author = $this->checkauthor($post->post_author); //just so that if a post already exists, new users are not created by checkauthor
217 $post_id = wp_insert_post($post);
218 if ( is_wp_error( $post_id ) )
222 if ( 0 != count($post->categories) ) {
223 wp_create_categories($post->categories, $post_id);
228 foreach ( $comments as $comment ) {
229 $comment = get_object_vars($comment);
230 $comment = add_magic_quotes($comment);
232 if ( !comment_exists($comment['comment_author'], $comment['comment_date'])) {
233 $comment['comment_post_ID'] = $post_id;
234 $comment = wp_filter_comment($comment);
235 wp_insert_comment($comment);
241 printf(' '.__('(%s comments)'), $num_comments);
244 foreach ( $pings as $ping ) {
245 $ping = get_object_vars($ping);
246 $ping = add_magic_quotes($ping);
248 if ( !comment_exists($ping['comment_author'], $ping['comment_date'])) {
249 $ping['comment_content'] = "<strong>{$ping['title']}</strong>\n\n{$ping['comment_content']}";
250 $ping['comment_post_ID'] = $post_id;
251 $ping = wp_filter_comment($ping);
252 wp_insert_comment($ping);
258 printf(' '.__('(%s pings)'), $num_pings);
261 //ob_flush();flush();
264 function process_posts() {
267 $handle = fopen($this->file, 'r');
268 if ( $handle == null )
272 $post = new StdClass();
273 $comment = new StdClass();
275 $ping = new StdClass();
278 echo "<div class='wrap'><ol>";
280 while ( $line = fgets($handle) ) {
283 if ( '-----' == $line ) {
284 // Finishing a multi-line field
285 if ( 'comment' == $context ) {
286 $comments[] = $comment;
287 $comment = new StdClass();
288 } else if ( 'ping' == $context ) {
290 $ping = new StdClass();
293 } else if ( '--------' == $line ) {
296 $result = $this->save_post($post, $comments, $pings);
297 if ( is_wp_error( $result ) )
299 $post = new StdClass;
300 $comment = new StdClass();
301 $ping = new StdClass();
304 } else if ( 'BODY:' == $line ) {
306 } else if ( 'EXTENDED BODY:' == $line ) {
307 $context = 'extended';
308 } else if ( 'EXCERPT:' == $line ) {
309 $context = 'excerpt';
310 } else if ( 'KEYWORDS:' == $line ) {
311 $context = 'keywords';
312 } else if ( 'COMMENT:' == $line ) {
313 $context = 'comment';
314 } else if ( 'PING:' == $line ) {
316 } else if ( 0 === strpos($line, "AUTHOR:") ) {
317 $author = trim( substr($line, strlen("AUTHOR:")) );
318 if ( '' == $context )
319 $post->post_author = $author;
320 else if ( 'comment' == $context )
321 $comment->comment_author = $author;
322 } else if ( 0 === strpos($line, "TITLE:") ) {
323 $title = trim( substr($line, strlen("TITLE:")) );
324 if ( '' == $context )
325 $post->post_title = $title;
326 else if ( 'ping' == $context )
327 $ping->title = $title;
328 } else if ( 0 === strpos($line, "STATUS:") ) {
329 $status = trim( substr($line, strlen("STATUS:")) );
330 if ( empty($status) )
332 $post->post_status = $status;
333 } else if ( 0 === strpos($line, "ALLOW COMMENTS:") ) {
334 $allow = trim( substr($line, strlen("ALLOW COMMENTS:")) );
336 $post->comment_status = 'open';
338 $post->comment_status = 'closed';
339 } else if ( 0 === strpos($line, "ALLOW PINGS:") ) {
340 $allow = trim( substr($line, strlen("ALLOW PINGS:")) );
342 $post->ping_status = 'open';
344 $post->ping_status = 'closed';
345 } else if ( 0 === strpos($line, "CATEGORY:") ) {
346 $category = trim( substr($line, strlen("CATEGORY:")) );
347 if ( '' != $category )
348 $post->categories[] = $category;
349 } else if ( 0 === strpos($line, "PRIMARY CATEGORY:") ) {
350 $category = trim( substr($line, strlen("PRIMARY CATEGORY:")) );
351 if ( '' != $category )
352 $post->categories[] = $category;
353 } else if ( 0 === strpos($line, "DATE:") ) {
354 $date = trim( substr($line, strlen("DATE:")) );
355 $date = strtotime($date);
356 $date = date('Y-m-d H:i:s', $date);
357 $date_gmt = get_gmt_from_date($date);
358 if ( '' == $context ) {
359 $post->post_modified = $date;
360 $post->post_modified_gmt = $date_gmt;
361 $post->post_date = $date;
362 $post->post_date_gmt = $date_gmt;
363 } else if ( 'comment' == $context ) {
364 $comment->comment_date = $date;
365 } else if ( 'ping' == $context ) {
366 $ping->comment_date = $date;
368 } else if ( 0 === strpos($line, "EMAIL:") ) {
369 $email = trim( substr($line, strlen("EMAIL:")) );
370 if ( 'comment' == $context )
371 $comment->comment_author_email = $email;
373 $ping->comment_author_email = '';
374 } else if ( 0 === strpos($line, "IP:") ) {
375 $ip = trim( substr($line, strlen("IP:")) );
376 if ( 'comment' == $context )
377 $comment->comment_author_IP = $ip;
379 $ping->comment_author_IP = $ip;
380 } else if ( 0 === strpos($line, "URL:") ) {
381 $url = trim( substr($line, strlen("URL:")) );
382 if ( 'comment' == $context )
383 $comment->comment_author_url = $url;
385 $ping->comment_author_url = $url;
386 } else if ( 0 === strpos($line, "BLOG NAME:") ) {
387 $blog = trim( substr($line, strlen("BLOG NAME:")) );
388 $ping->comment_author = $blog;
390 // Processing multi-line field, check context.
393 if ( 'body' == $context ) {
394 $post->post_content .= $line;
395 } else if ( 'extended' == $context ) {
396 $post->extended .= $line;
397 } else if ( 'excerpt' == $context ) {
398 $post->post_excerpt .= $line;
399 } else if ( 'comment' == $context ) {
400 $comment->comment_content .= $line;
401 } else if ( 'ping' == $context ) {
402 $ping->comment_content .= $line;
409 wp_import_cleanup($this->id);
410 do_action('import_done', 'mt');
412 echo '<h3>'.sprintf(__('All done. <a href="%s">Have fun!</a>'), get_option('home')).'</h3></div>';
416 $this->id = (int) $_GET['id'];
417 if ( $this->id == 0 )
418 $this->file = ABSPATH . 'wp-content/mt-export.txt';
420 $this->file = get_attached_file($this->id);
421 $this->get_authors_from_post();
422 $result = $this->process_posts();
423 if ( is_wp_error( $result ) )
427 function dispatch() {
428 if (empty ($_GET['step']))
431 $step = (int) $_GET['step'];
438 check_admin_referer('import-upload');
439 $this->select_authors();
442 check_admin_referer('import-mt');
443 $result = $this->import();
444 if ( is_wp_error( $result ) )
445 echo $result->get_error_message();
450 function MT_Import() {
455 $mt_import = new MT_Import();
457 register_importer('mt', __('Movable Type and TypePad'), __('Import posts and comments from a Movable Type or Typepad blog'), array ($mt_import, 'dispatch'));