]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/import/mt.php
Wordpress 2.0.2-scripts
[autoinstalls/wordpress.git] / wp-admin / import / mt.php
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').'</h2>';
15         }
16
17         function footer() {
18                 echo '</div>';
19         }
20
21         function greet() {
22                 $this->header();
23 ?>
24 <p><?php _e('Howdy! We&#8217;re about to begin the process to import all of your Movable Type entries into WordPress. To begin, select a file to upload and click Import.'); ?></p>
25 <?php wp_import_upload_form( add_query_arg('step', 1) ); ?>
26         <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>
27 <?php
28                 $this->footer();
29         }
30
31         function users_form($n) {
32                 global $wpdb, $testing;
33                 $users = $wpdb->get_results("SELECT * FROM $wpdb->users ORDER BY ID");
34 ?><select name="userselect[<?php echo $n; ?>]">
35         <option value="#NONE#">- Select -</option>
36         <?php
37
38
39                 foreach ($users as $user) {
40                         echo '<option value="'.$user->user_login.'">'.$user->user_login.'</option>';
41                 }
42 ?>
43         </select>
44         <?php
45
46
47         }
48
49         //function to check the authorname and do the mapping
50         function checkauthor($author) {
51                 global $wpdb;
52                 //mtnames is an array with the names in the mt import file
53                 $pass = 'changeme';
54                 if (!(in_array($author, $this->mtnames))) { //a new mt author name is found
55                         ++ $this->j;
56                         $this->mtnames[$this->j] = $author; //add that new mt author name to an array 
57                         $user_id = username_exists($this->newauthornames[$this->j]); //check if the new author name defined by the user is a pre-existing wp user
58                         if (!$user_id) { //banging my head against the desk now. 
59                                 if ($newauthornames[$this->j] == 'left_blank') { //check if the user does not want to change the authorname
60                                         $user_id = wp_create_user($author, $pass);
61                                         $this->newauthornames[$this->j] = $author; //now we have a name, in the place of left_blank.
62                                 } else {
63                                         $user_id = wp_create_user($this->newauthornames[$this->j], $pass);
64                                 }
65                         } else {
66                                 return $user_id; // return pre-existing wp username if it exists
67                         }
68                 } else {
69                         $key = array_search($author, $this->mtnames); //find the array key for $author in the $mtnames array
70                         $user_id = username_exists($this->newauthornames[$key]); //use that key to get the value of the author's name from $newauthornames
71                 }
72
73                 return $user_id;
74         }
75
76         function get_entries() {
77                 set_magic_quotes_runtime(0);
78                 $importdata = file($this->file); // Read the file into an array
79                 $importdata = implode('', $importdata); // squish it
80                 $importdata = preg_replace("/(\r\n|\n|\r)/", "\n", $importdata);
81                 $importdata = preg_replace("/\n--------\n/", "--MT-ENTRY--\n", $importdata);
82                 $this->posts = explode("--MT-ENTRY--", $importdata);
83         }
84
85         function get_mt_authors() {
86                 $temp = array ();
87                 $i = -1;
88                 foreach ($this->posts as $post) {
89                         if ('' != trim($post)) {
90                                 ++ $i;
91                                 preg_match("|AUTHOR:(.*)|", $post, $thematch);
92                                 $thematch = trim($thematch[1]);
93                                 array_push($temp, "$thematch"); //store the extracted author names in a temporary array
94                         }
95                 }
96
97                 //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.
98                 $authors[0] = array_shift($temp);
99                 $y = count($temp) + 1;
100                 for ($x = 1; $x < $y; $x ++) {
101                         $next = array_shift($temp);
102                         if (!(in_array($next, $authors)))
103                                 array_push($authors, "$next");
104                 }
105
106                 return $authors;
107         }
108
109         function get_authors_from_post() {
110                 $formnames = array ();
111                 $selectnames = array ();
112
113                 foreach ($_POST['user'] as $key => $line) {
114                         $newname = trim(stripslashes($line));
115                         if ($newname == '')
116                                 $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.
117                         array_push($formnames, "$newname");
118                 } // $formnames is the array with the form entered names
119
120                 foreach ($_POST['userselect'] as $user => $key) {
121                         $selected = trim(stripslashes($key));
122                         array_push($selectnames, "$selected");
123                 }
124
125                 $count = count($formnames);
126                 for ($i = 0; $i < $count; $i ++) {
127                         if ($selectnames[$i] != '#NONE#') { //if no name was selected from the select menu, use the name entered in the form
128                                 array_push($this->newauthornames, "$selectnames[$i]");
129                         } else {
130                                 array_push($this->newauthornames, "$formnames[$i]");
131                         }
132                 }
133         }
134
135         function mt_authors_form() {
136 ?>
137 <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 <code>admin</code>s entries.'); ?></p>
138 <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>
139 <p><?php _e('If a new user is created by WordPress, the password will be set, by default, to "changeme". Quite suggestive, eh? ;)'); ?></p>
140         <?php
141
142
143                 $authors = $this->get_mt_authors();
144                 echo '<ol id="authors">';
145                 echo '<form action="?import=mt&amp;step=2&amp;id=' . $this->id . '" method="post">';
146                 $j = -1;
147                 foreach ($authors as $author) {
148                         ++ $j;
149                         echo '<li><i>'.$author.'</i><br />'.'<input type="text" value="'.$author.'" name="'.'user[]'.'" maxlength="30">';
150                         $this->users_form($j);
151                         echo '</li>';
152                 }
153
154                 echo '<input type="submit" value="Submit">'.'<br/>';
155                 echo '</form>';
156                 echo '</ol>';
157
158                 flush();
159         }
160
161         function select_authors() {
162                 $file = wp_import_handle_upload();
163                 if ( isset($file['error']) ) {
164                         echo $file['error'];
165                         return;
166                 }
167                 $this->file = $file['file'];
168                 $this->id = $file['id'];
169
170                 $this->get_entries();
171                 $this->mt_authors_form();
172         }
173
174         function process_posts() {
175                 global $wpdb;
176                 $i = -1;
177                 echo "<ol>";
178                 foreach ($this->posts as $post) {
179                         if ('' != trim($post)) {
180                                 ++ $i;
181                                 unset ($post_categories);
182
183                                 // Take the pings out first
184                                 preg_match("|(-----\n\nPING:.*)|s", $post, $pings);
185                                 $post = preg_replace("|(-----\n\nPING:.*)|s", '', $post);
186
187                                 // Then take the comments out
188                                 preg_match("|(-----\nCOMMENT:.*)|s", $post, $comments);
189                                 $post = preg_replace("|(-----\nCOMMENT:.*)|s", '', $post);
190
191                                 // We ignore the keywords
192                                 $post = preg_replace("|(-----\nKEYWORDS:.*)|s", '', $post);
193
194                                 // We want the excerpt
195                                 preg_match("|-----\nEXCERPT:(.*)|s", $post, $excerpt);
196                                 $excerpt = $wpdb->escape(trim($excerpt[1]));
197                                 $post = preg_replace("|(-----\nEXCERPT:.*)|s", '', $post);
198
199                                 // We're going to put extended body into main body with a more tag
200                                 preg_match("|-----\nEXTENDED BODY:(.*)|s", $post, $extended);
201                                 $extended = trim($extended[1]);
202                                 if ('' != $extended)
203                                         $extended = "\n<!--more-->\n$extended";
204                                 $post = preg_replace("|(-----\nEXTENDED BODY:.*)|s", '', $post);
205
206                                 // Now for the main body
207                                 preg_match("|-----\nBODY:(.*)|s", $post, $body);
208                                 $body = trim($body[1]);
209                                 $post_content = $wpdb->escape($body.$extended);
210                                 $post = preg_replace("|(-----\nBODY:.*)|s", '', $post);
211
212                                 // Grab the metadata from what's left
213                                 $metadata = explode("\n", $post);
214                                 foreach ($metadata as $line) {
215                                         preg_match("/^(.*?):(.*)/", $line, $token);
216                                         $key = trim($token[1]);
217                                         $value = trim($token[2]);
218                                         // Now we decide what it is and what to do with it
219                                         switch ($key) {
220                                                 case '' :
221                                                         break;
222                                                 case 'AUTHOR' :
223                                                         $post_author = $value;
224                                                         break;
225                                                 case 'TITLE' :
226                                                         $post_title = $wpdb->escape($value);
227                                                         break;
228                                                 case 'STATUS' :
229                                                         // "publish" and "draft" enumeration items match up; no change required
230                                                         $post_status = $value;
231                                                         if (empty ($post_status))
232                                                                 $post_status = 'publish';
233                                                         break;
234                                                 case 'ALLOW COMMENTS' :
235                                                         $post_allow_comments = $value;
236                                                         if ($post_allow_comments == 1) {
237                                                                 $comment_status = 'open';
238                                                         } else {
239                                                                 $comment_status = 'closed';
240                                                         }
241                                                         break;
242                                                 case 'CONVERT BREAKS' :
243                                                         $post_convert_breaks = $value;
244                                                         break;
245                                                 case 'ALLOW PINGS' :
246                                                         $ping_status = trim($meta[2][0]);
247                                                         if ($ping_status == 1) {
248                                                                 $ping_status = 'open';
249                                                         } else {
250                                                                 $ping_status = 'closed';
251                                                         }
252                                                         break;
253                                                 case 'PRIMARY CATEGORY' :
254                                                         if (! empty ($value) )
255                                                                 $post_categories[] = $wpdb->escape($value);
256                                                         break;
257                                                 case 'CATEGORY' :
258                                                         if (! empty ($value) )
259                                                                 $post_categories[] = $wpdb->escape($value);
260                                                         break;
261                                                 case 'DATE' :
262                                                         $post_modified = strtotime($value);
263                                                         $post_modified = date('Y-m-d H:i:s', $post_modified);
264                                                         $post_modified_gmt = get_gmt_from_date("$post_modified");
265                                                         $post_date = $post_modified;
266                                                         $post_date_gmt = $post_modified_gmt;
267                                                         break;
268                                                 default :
269                                                         // echo "\n$key: $value";
270                                                         break;
271                                         } // end switch
272                                 } // End foreach
273
274                                 // Let's check to see if it's in already
275                                 if ($post_id = post_exists($post_title, '', $post_date)) {
276                                         echo '<li>';
277                                         printf(__('Post <i>%s</i> already exists.'), stripslashes($post_title));
278                                 } else {
279                                         echo '<li>';
280                                         printf(__('Importing post <i>%s</i>...'), stripslashes($post_title));
281
282                                         $post_author = $this->checkauthor($post_author); //just so that if a post already exists, new users are not created by checkauthor
283
284                                         $postdata = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_excerpt', 'post_status', 'comment_status', 'ping_status', 'post_modified', 'post_modified_gmt');
285                                         $post_id = wp_insert_post($postdata);
286                                         // Add categories.
287                                         if (0 != count($post_categories)) {
288                                                 wp_create_categories($post_categories, $post_id);
289                                         }
290                                 }
291
292                                 $comment_post_ID = $post_id;
293                                 $comment_approved = 1;
294
295                                 // Now for comments
296                                 $comments = explode("-----\nCOMMENT:", $comments[0]);
297                                 $num_comments = 0;
298                                 foreach ($comments as $comment) {
299                                         if ('' != trim($comment)) {
300                                                 // Author
301                                                 preg_match("|AUTHOR:(.*)|", $comment, $comment_author);
302                                                 $comment_author = $wpdb->escape(trim($comment_author[1]));
303                                                 $comment = preg_replace('|(\n?AUTHOR:.*)|', '', $comment);
304                                                 preg_match("|EMAIL:(.*)|", $comment, $comment_author_email);
305                                                 $comment_author_email = $wpdb->escape(trim($comment_author_email[1]));
306                                                 $comment = preg_replace('|(\n?EMAIL:.*)|', '', $comment);
307
308                                                 preg_match("|IP:(.*)|", $comment, $comment_author_IP);
309                                                 $comment_author_IP = trim($comment_author_IP[1]);
310                                                 $comment = preg_replace('|(\n?IP:.*)|', '', $comment);
311
312                                                 preg_match("|URL:(.*)|", $comment, $comment_author_url);
313                                                 $comment_author_url = $wpdb->escape(trim($comment_author_url[1]));
314                                                 $comment = preg_replace('|(\n?URL:.*)|', '', $comment);
315
316                                                 preg_match("|DATE:(.*)|", $comment, $comment_date);
317                                                 $comment_date = trim($comment_date[1]);
318                                                 $comment_date = date('Y-m-d H:i:s', strtotime($comment_date));
319                                                 $comment = preg_replace('|(\n?DATE:.*)|', '', $comment);
320
321                                                 $comment_content = $wpdb->escape(trim($comment));
322                                                 $comment_content = str_replace('-----', '', $comment_content);
323                                                 // Check if it's already there
324                                                 if (!comment_exists($comment_author, $comment_date)) {
325                                                         $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_author_IP', 'comment_date', 'comment_content', 'comment_approved');
326                                                         $commentdata = wp_filter_comment($commentdata);
327                                                         wp_insert_comment($commentdata);
328                                                         $num_comments++;
329                                                 }
330                                         }
331                                 }
332                                 if ( $num_comments )
333                                         printf(__('(%s comments)'), $num_comments);
334
335                                 // Finally the pings
336                                 // fix the double newline on the first one
337                                 $pings[0] = str_replace("-----\n\n", "-----\n", $pings[0]);
338                                 $pings = explode("-----\nPING:", $pings[0]);
339                                 $num_pings = 0;
340                                 foreach ($pings as $ping) {
341                                         if ('' != trim($ping)) {
342                                                 // 'Author'
343                                                 preg_match("|BLOG NAME:(.*)|", $ping, $comment_author);
344                                                 $comment_author = $wpdb->escape(trim($comment_author[1]));
345                                                 $ping = preg_replace('|(\n?BLOG NAME:.*)|', '', $ping);
346
347                                                 preg_match("|IP:(.*)|", $ping, $comment_author_IP);
348                                                 $comment_author_IP = trim($comment_author_IP[1]);
349                                                 $ping = preg_replace('|(\n?IP:.*)|', '', $ping);
350
351                                                 preg_match("|URL:(.*)|", $ping, $comment_author_url);
352                                                 $comment_author_url = $wpdb->escape(trim($comment_author_url[1]));
353                                                 $ping = preg_replace('|(\n?URL:.*)|', '', $ping);
354
355                                                 preg_match("|DATE:(.*)|", $ping, $comment_date);
356                                                 $comment_date = trim($comment_date[1]);
357                                                 $comment_date = date('Y-m-d H:i:s', strtotime($comment_date));
358                                                 $ping = preg_replace('|(\n?DATE:.*)|', '', $ping);
359
360                                                 preg_match("|TITLE:(.*)|", $ping, $ping_title);
361                                                 $ping_title = $wpdb->escape(trim($ping_title[1]));
362                                                 $ping = preg_replace('|(\n?TITLE:.*)|', '', $ping);
363
364                                                 $comment_content = $wpdb->escape(trim($ping));
365                                                 $comment_content = str_replace('-----', '', $comment_content);
366
367                                                 $comment_content = "<strong>$ping_title</strong>\n\n$comment_content";
368
369                                                 $comment_type = 'trackback';
370
371                                                 // Check if it's already there
372                                                 if (!comment_exists($comment_author, $comment_date)) {
373                                                         $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_author_IP', 'comment_date', 'comment_content', 'comment_type', 'comment_approved');
374                                                         $commentdata = wp_filter_comment($commentdata);
375                                                         wp_insert_comment($commentdata);
376                                                         $num_pings++;
377                                                 }
378                                         }
379                                 }
380                                 if ( $num_pings )
381                                         printf(__('(%s pings)'), $num_pings);
382                                 
383                                 echo "</li>";
384                         }
385                         flush();
386                 }
387
388                 echo '</ol>';
389
390                 wp_import_cleanup($this->id);
391
392                 echo '<h3>'.sprintf(__('All done. <a href="%s">Have fun!</a>'), get_option('home')).'</h3>';
393         }
394
395         function import() {
396                 $this->id = (int) $_GET['id'];
397                 $this->file = get_attached_file($this->id);
398                 $this->get_authors_from_post();
399                 $this->get_entries();
400                 $this->process_posts();
401         }
402
403         function dispatch() {
404                 if (empty ($_GET['step']))
405                         $step = 0;
406                 else
407                         $step = (int) $_GET['step'];
408
409                 switch ($step) {
410                         case 0 :
411                                 $this->greet();
412                                 break;
413                         case 1 :
414                                 $this->select_authors();
415                                 break;
416                         case 2:
417                                 $this->import();
418                                 break;
419                 }
420         }
421
422         function MT_Import() {
423                 // Nothing.     
424         }
425 }
426
427 $mt_import = new MT_Import();
428
429 register_importer('mt', 'Movable Type', __('Import posts and comments from your Movable Type blog'), array ($mt_import, 'dispatch'));
430 ?>