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