]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/import/mt.php
Wordpress 2.7.1
[autoinstalls/wordpress.git] / wp-admin / import / mt.php
1 <?php
2 /**
3  * Movable Type and Typepad Importer
4  *
5  * @package WordPress
6  * @subpackage Importer
7  */
8
9 /**
10  * Moveable Type and Typepad Importer class
11  *
12  * Upload your exported Movable Type or Typepad entries into WordPress.
13  *
14  * @since unknown
15  */
16 class MT_Import {
17
18         var $posts = array ();
19         var $file;
20         var $id;
21         var $mtnames = array ();
22         var $newauthornames = array ();
23         var $j = -1;
24
25         function header() {
26                 echo '<div class="wrap">';
27                 screen_icon();
28                 echo '<h2>'.__('Import Movable Type or TypePad').'</h2>';
29         }
30
31         function footer() {
32                 echo '</div>';
33         }
34
35         function greet() {
36                 $this->header();
37 ?>
38 <div class="narrow">
39 <p><?php _e('Howdy! We&#8217;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>
40
41 <?php wp_import_upload_form( add_query_arg('step', 1) ); ?>
42 <form method="post" action="<?php echo add_query_arg('step', 1); ?>" class="import-upload-form">
43
44 <?php wp_nonce_field('import-upload'); ?>
45 <p>
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>
48 <p class="submit">
49 <input type="submit" class="button" value="<?php echo attribute_escape(__('Import mt-export.txt')); ?>" />
50 </p>
51 </form>
52 <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\'t finish. If you get an <strong>out of memory</strong> error try splitting up the import file into pieces.'); ?> </p>
53 </div>
54 <?php
55                 $this->footer();
56         }
57
58         function users_form($n) {
59                 global $wpdb;
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>
63         <?php
64
65
66                 foreach ($users as $user) {
67                         echo '<option value="'.$user->user_login.'">'.$user->user_login.'</option>';
68                 }
69 ?>
70         </select>
71         <?php
72
73         }
74
75         function has_gzip() {
76                 return is_callable('gzopen');
77         }
78
79         function fopen($filename, $mode='r') {
80                 if ( $this->has_gzip() )
81                         return gzopen($filename, $mode);
82                 return fopen($filename, $mode);
83         }
84
85         function feof($fp) {
86                 if ( $this->has_gzip() )
87                         return gzeof($fp);
88                 return feof($fp);
89         }
90
91         function fgets($fp, $len=8192) {
92                 if ( $this->has_gzip() )
93                         return gzgets($fp, $len);
94                 return fgets($fp, $len);
95         }
96
97         function fclose($fp) {
98                 if ( $this->has_gzip() )
99                         return gzclose($fp);
100                 return fclose($fp);
101         }
102
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
108                         ++ $this->j;
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.
115                                 } else {
116                                         $user_id = wp_create_user($this->newauthornames[$this->j], $pass);
117                                 }
118                         } else {
119                                 return $user_id; // return pre-existing wp username if it exists
120                         }
121                 } else {
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
124                 }
125
126                 return $user_id;
127         }
128
129         function get_mt_authors() {
130                 $temp = array();
131                 $authors = array();
132
133                 $handle = $this->fopen($this->file, 'r');
134                 if ( $handle == null )
135                         return false;
136
137                 $in_comment = false;
138                 while ( $line = $this->fgets($handle) ) {
139                         $line = trim($line);
140
141                         if ( 'COMMENT:' == $line )
142                                 $in_comment = true;
143                         else if ( '-----' == $line )
144                                 $in_comment = false;
145
146                         if ( $in_comment || 0 !== strpos($line,"AUTHOR:") )
147                                 continue;
148
149                         $temp[] = trim( substr($line, strlen("AUTHOR:")) );
150                 }
151
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");
159                 }
160
161                 $this->fclose($handle);
162
163                 return $authors;
164         }
165
166         function get_authors_from_post() {
167                 $formnames = array ();
168                 $selectnames = array ();
169
170                 foreach ($_POST['user'] as $key => $line) {
171                         $newname = trim(stripslashes($line));
172                         if ($newname == '')
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
176
177                 foreach ($_POST['userselect'] as $user => $key) {
178                         $selected = trim(stripslashes($key));
179                         array_push($selectnames, "$selected");
180                 }
181
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]");
186                         } else {
187                                 array_push($this->newauthornames, "$formnames[$i]");
188                         }
189                 }
190         }
191
192         function mt_authors_form() {
193 ?>
194 <div class="wrap">
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>
200         <?php
201
202
203                 $authors = $this->get_mt_authors();
204                 echo '<ol id="authors">';
205                 echo '<form action="?import=mt&amp;step=2&amp;id=' . $this->id . '" method="post">';
206                 wp_nonce_field('import-mt');
207                 $j = -1;
208                 foreach ($authors as $author) {
209                         ++ $j;
210                         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 />');
211                         $this->users_form($j);
212                         echo '</label></li>';
213                 }
214
215                 echo '<p class="submit"><input type="submit" class="button" value="'.__('Submit').'"></p>'.'<br />';
216                 echo '</form>';
217                 echo '</ol></div>';
218
219         }
220
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');
226                 } else {
227                         $file = wp_import_handle_upload();
228                 }
229                 if ( isset($file['error']) ) {
230                         $this->header();
231                         echo '<p>'.__('Sorry, there has been an error').'.</p>';
232                         echo '<p><strong>' . $file['error'] . '</strong></p>';
233                         $this->footer();
234                         return;
235                 }
236                 $this->file = $file['file'];
237                 $this->id = (int) $file['id'];
238
239                 $this->mt_authors_form();
240         }
241
242         function save_post(&$post, &$comments, &$pings) {
243                 // Reset the counter
244                 set_time_limit(30);
245                 $post = get_object_vars($post);
246                 $post = add_magic_quotes($post);
247                 $post = (object) $post;
248
249                 if ( $post_id = post_exists($post->post_title, '', $post->post_date) ) {
250                         echo '<li>';
251                         printf(__('Post <em>%s</em> already exists.'), stripslashes($post->post_title));
252                 } else {
253                         echo '<li>';
254                         printf(__('Importing post <em>%s</em>...'), stripslashes($post->post_title));
255
256                         if ( '' != trim( $post->extended ) )
257                                         $post->post_content .= "\n<!--more-->\n$post->extended";
258
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 ) )
262                                 return $post_id;
263
264                         // Add categories.
265                         if ( 0 != count($post->categories) ) {
266                                 wp_create_categories($post->categories, $post_id);
267                         }
268
269                          // Add tags or keywords
270                         if ( 1 < strlen($post->post_keywords) ) {
271                                 // Keywords exist.
272                                 printf(__('<br />Adding tags <i>%s</i>...'), stripslashes($post->post_keywords));
273                                 wp_add_post_tags($post_id, $post->post_keywords);
274                         }
275                 }
276
277                 $num_comments = 0;
278                 foreach ( $comments as $comment ) {
279                         $comment = get_object_vars($comment);
280                         $comment = add_magic_quotes($comment);
281
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);
286                                 $num_comments++;
287                         }
288                 }
289
290                 if ( $num_comments )
291                         printf(' '.__ngettext('(%s comment)', '(%s comments)', $num_comments), $num_comments);
292
293                 $num_pings = 0;
294                 foreach ( $pings as $ping ) {
295                         $ping = get_object_vars($ping);
296                         $ping = add_magic_quotes($ping);
297
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);
303                                 $num_pings++;
304                         }
305                 }
306
307                 if ( $num_pings )
308                         printf(' '.__ngettext('(%s ping)', '(%s pings)', $num_pings), $num_pings);
309
310                 echo "</li>";
311                 //ob_flush();flush();
312         }
313
314         function process_posts() {
315                 global $wpdb;
316
317                 $handle = $this->fopen($this->file, 'r');
318                 if ( $handle == null )
319                         return false;
320
321                 $context = '';
322                 $post = new StdClass();
323                 $comment = new StdClass();
324                 $comments = array();
325                 $ping = new StdClass();
326                 $pings = array();
327
328                 echo "<div class='wrap'><ol>";
329
330                 while ( $line = $this->fgets($handle) ) {
331                         $line = trim($line);
332
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 ) {
339                                         $pings[] = $ping;
340                                         $ping = new StdClass();
341                                 }
342                                 $context = '';
343                         } else if ( '--------' == $line ) {
344                                 // Finishing a post.
345                                 $context = '';
346                                 $result = $this->save_post($post, $comments, $pings);
347                                 if ( is_wp_error( $result ) )
348                                         return $result;
349                                 $post = new StdClass;
350                                 $comment = new StdClass();
351                                 $ping = new StdClass();
352                                 $comments = array();
353                                 $pings = array();
354                         } else if ( 'BODY:' == $line ) {
355                                 $context = 'body';
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 ) {
365                                 $context = 'ping';
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) )
381                                         $status = 'publish';
382                                 $post->post_status = $status;
383                         } else if ( 0 === strpos($line, "ALLOW COMMENTS:") ) {
384                                 $allow = trim( substr($line, strlen("ALLOW COMMENTS:")) );
385                                 if ( $allow == 1 )
386                                         $post->comment_status = 'open';
387                                 else
388                                         $post->comment_status = 'closed';
389                         } else if ( 0 === strpos($line, "ALLOW PINGS:") ) {
390                                 $allow = trim( substr($line, strlen("ALLOW PINGS:")) );
391                                 if ( $allow == 1 )
392                                         $post->ping_status = 'open';
393                                 else
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;
417                                 }
418                         } else if ( 0 === strpos($line, "EMAIL:") ) {
419                                 $email = trim( substr($line, strlen("EMAIL:")) );
420                                 if ( 'comment' == $context )
421                                         $comment->comment_author_email = $email;
422                                 else
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;
428                                 else
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;
434                                 else
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;
439                         } else {
440                                 // Processing multi-line field, check context.
441                                 
442                                 if( !empty($line) )
443                                         $line .= "\n";
444                                         
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;
457                                 }
458                         }
459                 }
460
461                 $this->fclose($handle);
462
463                 echo '</ol>';
464
465                 wp_import_cleanup($this->id);
466                 do_action('import_done', 'mt');
467
468                 echo '<h3>'.sprintf(__('All done. <a href="%s">Have fun!</a>'), get_option('home')).'</h3></div>';
469         }
470
471         function import() {
472                 $this->id = (int) $_GET['id'];
473                 if ( $this->id == 0 )
474                         $this->file = WP_CONTENT_DIR . '/mt-export.txt';
475                 else
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 ) )
480                         return $result;
481         }
482
483         function dispatch() {
484                 if (empty ($_GET['step']))
485                         $step = 0;
486                 else
487                         $step = (int) $_GET['step'];
488
489                 switch ($step) {
490                         case 0 :
491                                 $this->greet();
492                                 break;
493                         case 1 :
494                                 check_admin_referer('import-upload');
495                                 $this->select_authors();
496                                 break;
497                         case 2:
498                                 check_admin_referer('import-mt');
499                                 $result = $this->import();
500                                 if ( is_wp_error( $result ) )
501                                         echo $result->get_error_message();
502                                 break;
503                 }
504         }
505
506         function MT_Import() {
507                 // Nothing.
508         }
509 }
510
511 $mt_import = new MT_Import();
512
513 register_importer('mt', __('Movable Type and TypePad'), __('Import posts and comments from a Movable Type or Typepad blog.'), array ($mt_import, 'dispatch'));
514 ?>