root/tags/1.3/wp-admin/import/mt.php

Revision 1139, 12.7 kB (checked in by donncha, 1 year ago)

Merge with WordPress? 2.3.1

Line 
1 <?php
2
3 class MT_Import {
4
5     var $posts = array ();
6     var $file;
7     var $id;
8     var $mtnames = array ();
9     var $newauthornames = array ();
10     var $j = -1;
11
12     function header() {
13         echo '<div class="wrap">';
14         echo '<h2>'.__('Import Movable Type or TypePad').'</h2>';
15     }
16
17     function footer() {
18         echo '</div>';
19     }
20
21     function greet() {
22         $this->header();
23 ?>
24 <div class="narrow">
25 <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>
26 <?php wp_import_upload_form( add_query_arg('step', 1) ); ?>
27     <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>
28 </div>
29 <?php
30         $this->footer();
31     }
32
33     function users_form($n) {
34         global $wpdb, $testing;
35         $users = get_users_of_blog($wpdb->blogid);
36 ?><select name="userselect[<?php echo $n; ?>]">
37     <option value="#NONE#"><?php _e('- Select -') ?></option>
38     <?php
39
40
41         foreach ($users as $user) {
42             echo '<option value="'.$user->user_login.'">'.$user->user_login.'</option>';
43         }
44 ?>
45     </select>
46     <?php
47
48
49     }
50
51     //function to check the authorname and do the mapping
52     function checkauthor($author) {
53         global $wpdb;
54         //mtnames is an array with the names in the mt import file
55         $key = array_search($author, $this->mtnames); //find the array key for $author in the $mtnames array
56         $user_id = username_exists($this->newauthornames[$key]); //use that key to get the value of the author's name from $newauthornames
57
58         return $user_id;
59     }
60
61     function get_mt_authors() {
62         $temp = array();
63         $authors = array();
64
65         $handle = fopen($this->file, 'r');
66         if ( $handle == null )
67             return false;
68
69         $in_comment = false;
70         while ( $line = fgets($handle) ) {
71             $line = trim($line);
72
73             if ( 'COMMENT:' == $line )
74                 $in_comment = true;
75             else if ( '-----' == $line )
76                 $in_comment = false;
77
78             if ( $in_comment || 0 !== strpos($line,"AUTHOR:") )
79                 continue;
80
81             $temp[] = trim( substr($line, strlen("AUTHOR:")) );
82         }
83
84         //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.
85         $authors[0] = array_shift($temp);
86         $y = count($temp) + 1;
87         for ($x = 1; $x < $y; $x ++) {
88             $next = array_shift($temp);
89             if (!(in_array($next, $authors)))
90                 array_push($authors, "$next");
91         }
92
93         fclose($handle);
94
95         return $authors;
96     }
97
98     function get_authors_from_post() {
99         $formnames = array ();
100         $selectnames = array ();
101
102         foreach ($_POST['user'] as $key => $line) {
103             $newname = trim(stripslashes($line));
104             if ($newname == '')
105                 $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.
106             array_push($formnames, "$newname");
107         } // $formnames is the array with the form entered names
108
109         foreach ($_POST['userselect'] as $user => $key) {
110             $selected = trim(stripslashes($key));
111             array_push($selectnames, "$selected");
112         }
113
114         $count = count($formnames);
115         for ($i = 0; $i < $count; $i ++) {
116             if ($selectnames[$i] != '#NONE#') { //if no name was selected from the select menu, use the name entered in the form
117                 array_push($this->newauthornames, "$selectnames[$i]");
118             } else {
119                 array_push($this->newauthornames, "$formnames[$i]");
120             }
121         }
122     }
123
124     function mt_authors_form() {
125 ?>
126 <div class="wrap">
127 <h2><?php _e('Assign Authors'); ?></h2>
128 <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>
129 <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>
130 <p><?php _e('If a new user is created by WordPress, the password will be set, by default, to "changeme". Quite suggestive, eh? ;)'); ?></p>
131     <?php
132
133
134         $authors = $this->get_mt_authors();
135         echo '<ol id="authors">';
136         echo '<form action="?import=mt&amp;step=2&amp;id=' . $this->id . '" method="post">';
137         wp_nonce_field('import-mt');
138         $j = -1;
139         foreach ($authors as $author) {
140             ++ $j;
141             echo '<li>'.__('Current author:').' <strong>'.$author.'</strong><br />'.'<input type="text" value="'.$author.'" name="'.'user[]'.'" maxlength="30">';
142             $this->users_form($j);
143             echo '</li>';
144         }
145
146         echo '<input type="submit" value="'.__('Submit').'">'.'<br />';
147         echo '</form>';
148         echo '</ol></div>';
149
150     }
151
152     function select_authors() {
153         $file = wp_import_handle_upload();
154         if ( isset($file['error']) ) {
155             $this->header();
156             echo '<p>'.__('Sorry, there has been an error').'.</p>';
157             echo '<p><strong>' . $file['error'] . '</strong></p>';
158             $this->footer();
159             return;
160         }
161         $this->file = $file['file'];
162         $this->id = (int) $file['id'];
163
164         $this->mt_authors_form();
165     }
166
167     function save_post(&$post, &$comments, &$pings) {
168         // Reset the counter
169         set_time_limit(30);
170         $post = get_object_vars($post);
171         $post = add_magic_quotes($post);
172         $post = (object) $post;
173
174         if ( $post_id = post_exists($post->post_title, '', $post->post_date) ) {
175             echo '<li>';
176             printf(__('Post <i>%s</i> already exists.'), stripslashes($post->post_title));
177         } else {
178             echo '<li>';
179             printf(__('Importing post <i>%s</i>...'), stripslashes($post->post_title));
180
181             if ( '' != trim( $post->extended ) )
182                     $post->post_content .= "\n<!--more-->\n$post->extended";
183
184             $post->post_author = $this->checkauthor($post->post_author); //just so that if a post already exists, new users are not created by checkauthor
185             $post_id = wp_insert_post($post);
186             if ( is_wp_error( $post_id ) )
187                 return $post_id;
188
189             // Add categories.
190             if ( 0 != count($post->categories) ) {
191                 wp_create_categories($post->categories, $post_id);
192             }
193         }
194
195         $num_comments = 0;
196         foreach ( $comments as $comment ) {
197             $comment = get_object_vars($comment);
198             $comment = add_magic_quotes($comment);
199
200             if ( !comment_exists($comment['comment_author'], $comment['comment_date'])) {
201                 $comment['comment_post_ID'] = $post_id;
202                 $comment = wp_filter_comment($comment);
203                 wp_insert_comment($comment);
204                 $num_comments++;
205             }
206         }
207
208         if ( $num_comments )
209             printf(' '.__('(%s comments)'), $num_comments);
210
211         $num_pings = 0;
212         foreach ( $pings as $ping ) {
213             $ping = get_object_vars($ping);
214             $ping = add_magic_quotes($ping);
215
216             if ( !comment_exists($ping['comment_author'], $ping['comment_date'])) {
217                 $ping['comment_content'] = "<strong>{$ping['title']}</strong>\n\n{$ping['comment_content']}";
218                 $ping['comment_post_ID'] = $post_id;
219                 $ping = wp_filter_comment($ping);
220                 wp_insert_comment($ping);
221                 $num_pings++;
222             }
223         }
224
225         if ( $num_pings )
226             printf(' '.__('(%s pings)'), $num_pings);
227
228         echo "</li>";
229         //ob_flush();flush();
230     }
231
232     function process_posts() {
233         global $wpdb;
234
235         $handle = fopen($this->file, 'r');
236         if ( $handle == null )
237             return false;
238
239         $context = '';
240         $post = new StdClass();
241         $comment = new StdClass();
242         $comments = array();
243         $ping = new StdClass();
244         $pings = array();
245
246         echo "<div class='wrap'><ol>";
247
248         while ( $line = fgets($handle) ) {
249             $line = trim($line);
250
251             if ( '-----' == $line ) {
252                 // Finishing a multi-line field
253                 if ( 'comment' == $context ) {
254                     $comments[] = $comment;
255                     $comment = new StdClass();
256                 } else if ( 'ping' == $context ) {
257                     $pings[] = $ping;
258                     $ping = new StdClass();
259                 }
260                 $context = '';
261             } else if ( '--------' == $line ) {
262                 // Finishing a post.
263                 $context = '';
264                 $result = $this->save_post($post, $comments, $pings);
265                 if ( is_wp_error( $result ) )
266                     return $result;
267                 $post = new StdClass;
268                 $comment = new StdClass();
269                 $ping = new StdClass();
270                 $comments = array();
271                 $pings = array();
272             } else if ( 'BODY:' == $line ) {
273                 $context = 'body';
274             } else if ( 'EXTENDED BODY:' == $line ) {
275                 $context = 'extended';
276             } else if ( 'EXCERPT:' == $line ) {
277                 $context = 'excerpt';
278             } else if ( 'KEYWORDS:' == $line ) {
279                 $context = 'keywords';
280             } else if ( 'COMMENT:' == $line ) {
281                 $context = 'comment';
282             } else if ( 'PING:' == $line ) {
283                 $context = 'ping';
284             } else if ( 0 === strpos($line, "AUTHOR:") ) {
285                 $author = trim( substr($line, strlen("AUTHOR:")) );
286                 if ( '' == $context )
287                     $post->post_author = $author;
288                 else if ( 'comment' == $context )
289                      $comment->comment_author = $author;
290             } else if ( 0 === strpos($line, "TITLE:") ) {
291                 $title = trim( substr($line, strlen("TITLE:")) );
292                 if ( '' == $context )
293                     $post->post_title = $title;
294                 else if ( 'ping' == $context )
295                     $ping->title = $title;
296             } else if ( 0 === strpos($line, "STATUS:") ) {
297                 $status = trim( substr($line, strlen("STATUS:")) );
298                 if ( empty($status) )
299                     $status = 'publish';
300                 $post->post_status = $status;
301             } else if ( 0 === strpos($line, "ALLOW COMMENTS:") ) {
302                 $allow = trim( substr($line, strlen("ALLOW COMMENTS:")) );
303                 if ( $allow == 1 )
304                     $post->comment_status = 'open';
305                 else
306                     $post->comment_status = 'closed';
307             } else if ( 0 === strpos($line, "ALLOW PINGS:") ) {
308                 $allow = trim( substr($line, strlen("ALLOW PINGS:")) );
309                 if ( $allow == 1 )
310                     $post->ping_status = 'open';
311                 else
312                     $post->ping_status = 'closed';
313             } else if ( 0 === strpos($line, "CATEGORY:") ) {
314                 $category = trim( substr($line, strlen("CATEGORY:")) );
315                 if ( '' != $category )
316                     $post->categories[] = $category;
317             } else if ( 0 === strpos($line, "PRIMARY CATEGORY:") ) {
318                 $category = trim( substr($line, strlen("PRIMARY CATEGORY:")) );
319                 if ( '' != $category )
320                     $post->categories[] = $category;
321             } else if ( 0 === strpos($line, "DATE:") ) {
322                 $date = trim( substr($line, strlen("DATE:")) );
323                 $date = strtotime($date);
324                 $date = date('Y-m-d H:i:s', $date);
325                 $date_gmt = get_gmt_from_date($date);
326                 if ( '' == $context ) {
327                     $post->post_modified = $date;
328                     $post->post_modified_gmt = $date_gmt;
329                     $post->post_date = $date;
330                     $post->post_date_gmt = $date_gmt;
331                 } else if ( 'comment' == $context ) {
332                     $comment->comment_date = $date;
333                 } else if ( 'ping' == $context ) {
334                     $ping->comment_date = $date;
335                 }
336             } else if ( 0 === strpos($line, "EMAIL:") ) {
337                 $email = trim( substr($line, strlen("EMAIL:")) );
338                 if ( 'comment' == $context )
339                     $comment->comment_author_email = $email;
340                 else
341                     $ping->comment_author_email = '';
342             } else if ( 0 === strpos($line, "IP:") ) {
343                 $ip = trim( substr($line, strlen("IP:")) );
344                 if ( 'comment' == $context )
345                     $comment->comment_author_IP = $ip;
346                 else
347                     $ping->comment_author_IP = $ip;
348             } else if ( 0 === strpos($line, "URL:") ) {
349                 $url = trim( substr($line, strlen("URL:")) );
350                 if ( 'comment' == $context )
351                     $comment->comment_author_url = $url;
352                 else
353                     $ping->comment_author_url = $url;
354             } else if ( 0 === strpos($line, "BLOG NAME:") ) {
355                 $blog = trim( substr($line, strlen("BLOG NAME:")) );
356                 $ping->comment_author = $blog;
357             } else {
358                 // Processing multi-line field, check context.
359
360                 $line .= "\n";
361                 if ( 'body' == $context ) {
362                     $post->post_content .= $line;
363                 } else if ( 'extended' ==  $context ) {
364                     $post->extended .= $line;
365                 } else if ( 'excerpt' == $context ) {
366                     $post->post_excerpt .= $line;
367                 } else if ( 'comment' == $context ) {
368                     $comment->comment_content .= $line;
369                 } else if ( 'ping' == $context ) {
370                     $ping->comment_content .= $line;
371                 }
372             }
373         }
374
375         echo '</ol>';
376
377         wp_import_cleanup($this->id);
378         do_action('import_done', 'mt');
379
380         echo '<h3>'.sprintf(__('All done. <a href="%s">Have fun!</a>'), get_option('home')).'</h3></div>';
381     }
382
383     function import() {
384         $this->id = (int) $_GET['id'];
385
386         $this->file = get_attached_file($this->id);
387         $this->get_authors_from_post();
388         $result = $this->process_posts();
389         if ( is_wp_error( $result ) )
390             return $result;
391     }
392
393     function dispatch() {
394         if (empty ($_GET['step']))
395             $step = 0;
396         else
397             $step = (int) $_GET['step'];
398
399         switch ($step) {
400             case 0 :
401                 $this->greet();
402                 break;
403             case 1 :
404                 check_admin_referer('import-upload');
405                 $this->select_authors();
406                 break;
407             case 2:
408                 check_admin_referer('import-mt');
409                 $result = $this->import();
410                 if ( is_wp_error( $result ) )
411                     echo $result->get_error_message();
412                 break;
413         }
414     }
415
416     function MT_Import() {
417         // Nothing.
418     }
419 }
420
421 $mt_import = new MT_Import();
422
423 register_importer('mt', __('Movable Type and TypePad'), __('Import posts and comments from a Movable Type or Typepad blog'), array ($mt_import, 'dispatch'));
424 ?>
425
Note: See TracBrowser for help on using the browser.