8 var $mtnames = array ();
9 var $newauthornames = array ();
13 echo '<div class="wrap">';
14 echo '<h2>'.__('Import Movable Type').'</h2>';
24 <p><?php _e('Howdy! We’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—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>
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>
39 foreach ($users as $user) {
40 echo '<option value="'.$user->user_login.'">'.$user->user_login.'</option>';
49 //function to check the authorname and do the mapping
50 function checkauthor($author) {
52 //mtnames is an array with the names in the mt import file
54 if (!(in_array($author, $this->mtnames))) { //a new mt author name is found
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.
63 $user_id = wp_create_user($this->newauthornames[$this->j], $pass);
66 return $user_id; // return pre-existing wp username if it exists
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
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);
85 function get_mt_authors() {
88 foreach ($this->posts as $post) {
89 if ('' != trim($post)) {
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
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");
109 function get_authors_from_post() {
110 $formnames = array ();
111 $selectnames = array ();
113 foreach ($_POST['user'] as $key => $line) {
114 $newname = trim(stripslashes($line));
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
120 foreach ($_POST['userselect'] as $user => $key) {
121 $selected = trim(stripslashes($key));
122 array_push($selectnames, "$selected");
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]");
130 array_push($this->newauthornames, "$formnames[$i]");
135 function mt_authors_form() {
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>
143 $authors = $this->get_mt_authors();
144 echo '<ol id="authors">';
145 echo '<form action="?import=mt&step=2&id=' . $this->id . '" method="post">';
147 foreach ($authors as $author) {
149 echo '<li><i>'.$author.'</i><br />'.'<input type="text" value="'.$author.'" name="'.'user[]'.'" maxlength="30">';
150 $this->users_form($j);
154 echo '<input type="submit" value="Submit">'.'<br/>';
161 function select_authors() {
162 $file = wp_import_handle_upload();
163 if ( isset($file['error']) ) {
167 $this->file = $file['file'];
168 $this->id = $file['id'];
170 $this->get_entries();
171 $this->mt_authors_form();
174 function process_posts() {
178 foreach ($this->posts as $post) {
179 if ('' != trim($post)) {
181 unset ($post_categories);
183 // Take the pings out first
184 preg_match("|(-----\n\nPING:.*)|s", $post, $pings);
185 $post = preg_replace("|(-----\n\nPING:.*)|s", '', $post);
187 // Then take the comments out
188 preg_match("|(-----\nCOMMENT:.*)|s", $post, $comments);
189 $post = preg_replace("|(-----\nCOMMENT:.*)|s", '', $post);
191 // We ignore the keywords
192 $post = preg_replace("|(-----\nKEYWORDS:.*)|s", '', $post);
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);
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]);
203 $extended = "\n<!--more-->\n$extended";
204 $post = preg_replace("|(-----\nEXTENDED BODY:.*)|s", '', $post);
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);
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
223 $post_author = $value;
226 $post_title = $wpdb->escape($value);
229 // "publish" and "draft" enumeration items match up; no change required
230 $post_status = $value;
231 if (empty ($post_status))
232 $post_status = 'publish';
234 case 'ALLOW COMMENTS' :
235 $post_allow_comments = $value;
236 if ($post_allow_comments == 1) {
237 $comment_status = 'open';
239 $comment_status = 'closed';
242 case 'CONVERT BREAKS' :
243 $post_convert_breaks = $value;
246 $ping_status = trim($meta[2][0]);
247 if ($ping_status == 1) {
248 $ping_status = 'open';
250 $ping_status = 'closed';
253 case 'PRIMARY CATEGORY' :
254 if (! empty ($value) )
255 $post_categories[] = $wpdb->escape($value);
258 if (! empty ($value) )
259 $post_categories[] = $wpdb->escape($value);
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;
269 // echo "\n$key: $value";
274 // Let's check to see if it's in already
275 if ($post_id = post_exists($post_title, '', $post_date)) {
277 printf(__('Post <i>%s</i> already exists.'), stripslashes($post_title));
280 printf(__('Importing post <i>%s</i>...'), stripslashes($post_title));
282 $post_author = $this->checkauthor($post_author); //just so that if a post already exists, new users are not created by checkauthor
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);
287 if (0 != count($post_categories)) {
288 wp_create_categories($post_categories, $post_id);
292 $comment_post_ID = $post_id;
293 $comment_approved = 1;
296 $comments = explode("-----\nCOMMENT:", $comments[0]);
298 foreach ($comments as $comment) {
299 if ('' != trim($comment)) {
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);
308 preg_match("|IP:(.*)|", $comment, $comment_author_IP);
309 $comment_author_IP = trim($comment_author_IP[1]);
310 $comment = preg_replace('|(\n?IP:.*)|', '', $comment);
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);
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);
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);
333 printf(__('(%s comments)'), $num_comments);
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]);
340 foreach ($pings as $ping) {
341 if ('' != trim($ping)) {
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);
347 preg_match("|IP:(.*)|", $ping, $comment_author_IP);
348 $comment_author_IP = trim($comment_author_IP[1]);
349 $ping = preg_replace('|(\n?IP:.*)|', '', $ping);
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);
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);
360 preg_match("|TITLE:(.*)|", $ping, $ping_title);
361 $ping_title = $wpdb->escape(trim($ping_title[1]));
362 $ping = preg_replace('|(\n?TITLE:.*)|', '', $ping);
364 $comment_content = $wpdb->escape(trim($ping));
365 $comment_content = str_replace('-----', '', $comment_content);
367 $comment_content = "<strong>$ping_title</strong>\n\n$comment_content";
369 $comment_type = 'trackback';
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);
381 printf(__('(%s pings)'), $num_pings);
390 wp_import_cleanup($this->id);
392 echo '<h3>'.sprintf(__('All done. <a href="%s">Have fun!</a>'), get_option('home')).'</h3>';
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();
403 function dispatch() {
404 if (empty ($_GET['step']))
407 $step = (int) $_GET['step'];
414 $this->select_authors();
422 function MT_Import() {
427 $mt_import = new MT_Import();
429 register_importer('mt', 'Movable Type', __('Import posts and comments from your Movable Type blog'), array ($mt_import, 'dispatch'));