Ticket #4421: mt.diff
| File mt.diff, 16.4 kB (added by ryan, 1 year ago) |
|---|
-
wp-admin/import/mt.php
old new 94 94 } 95 95 96 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 } 97 $temp = array(); 98 $authors = array(); 99 100 $handle = fopen($this->file, 'r'); 101 if ( $handle == null ) 102 return false; 103 104 $in_comment = false; 105 while ( $line = fgets($handle) ) { 106 $line = trim($line); 107 108 if ( 'COMMENT:' == $line ) 109 $in_comment = true; 110 else if ( '-----' == $line ) 111 $in_comment = false; 112 113 if ( $in_comment || 0 !== strpos($line,"AUTHOR:") ) 114 continue; 115 116 $temp[] = trim( substr($line, strlen("AUTHOR:")) ); 106 117 } 107 118 108 119 //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. … … 114 125 array_push($authors, "$next"); 115 126 } 116 127 128 fclose($handle); 129 117 130 return $authors; 118 131 } 119 132 … … 173 186 174 187 function select_authors() { 175 188 if ( $_POST['upload_type'] === 'ftp' ) { 176 $file['file'] = @file(ABSPATH . '/wp-content/mt-export.txt');177 if ( ! $file['file'] || !count($file['file']) )189 $file['file'] = ABSPATH . '/wp-content/mt-export.txt'; 190 if ( !file_exists($file['file']) ) 178 191 $file['error'] = __('<code>mt-export.txt</code> does not exist</code>'); 179 else180 $file['file'] = ABSPATH . '/wp-content/mt-export.txt';181 192 } else { 182 193 $file = wp_import_handle_upload(); 183 194 } … … 191 202 $this->file = $file['file']; 192 203 $this->id = (int) $file['id']; 193 204 194 $this->get_entries();195 205 $this->mt_authors_form(); 196 206 } 197 207 198 function process_posts() { 199 global $wpdb; 200 $i = -1; 201 echo "<div class='wrap'><ol>"; 202 foreach ($this->posts as $post) { 203 if ('' != trim($post)) { 204 ++ $i; 205 unset ($post_categories); 208 function save_post(&$post, &$comments, &$pings) { 209 $post = get_object_vars($post); 210 $post = add_magic_quotes($post); 211 $post = (object) $post; 206 212 207 // Take the pings out first 208 preg_match("|(-----\n\nPING:.*)|s", $post, $pings); 209 $post = preg_replace("|(-----\n\nPING:.*)|s", '', $post); 213 if ( $post_id = post_exists($post->post_title, '', $post->post_date) ) { 214 echo '<li>'; 215 printf(__('Post <i>%s</i> already exists.'), stripslashes($post->post_title)); 216 } else { 217 echo '<li>'; 218 printf(__('Importing post <i>%s</i>...'), stripslashes($post->post_title)); 210 219 211 // Then take the comments out 212 preg_match("|(-----\nCOMMENT:.*)|s", $post, $comments); 213 $post = preg_replace("|(-----\nCOMMENT:.*)|s", '', $post); 220 if ( '' != $post->extended ) 221 $post->post_content .= "\n<!--more-->\n$post->extended"; 214 222 215 // We ignore the keywords216 $post = preg_replace("|(-----\nKEYWORDS:.*)|s", '',$post);223 $post->post_author = $this->checkauthor($post->post_author); //just so that if a post already exists, new users are not created by checkauthor 224 $post_id = wp_insert_post($post); 217 225 218 // We want the excerpt 219 preg_match("|-----\nEXCERPT:(.*)|s", $post, $excerpt); 220 $post_excerpt = $wpdb->escape(trim($excerpt[1])); 221 $post = preg_replace("|(-----\nEXCERPT:.*)|s", '', $post); 226 // Add categories. 227 if ( 0 != count($post->categories) ) { 228 wp_create_categories($post->categories, $post_id); 229 } 230 } 222 231 223 // We're going to put extended body into main body with a more tag 224 preg_match("|-----\nEXTENDED BODY:(.*)|s", $post, $extended); 225 $extended = trim($extended[1]); 226 if ('' != $extended) 227 $extended = "\n<!--more-->\n$extended"; 228 $post = preg_replace("|(-----\nEXTENDED BODY:.*)|s", '', $post); 232 $num_comments = 0; 233 foreach ( $comments as $comment ) { 234 $comment = get_object_vars($comment); 235 $comment = add_magic_quotes($comment); 229 236 230 // Now for the main body 231 preg_match("|-----\nBODY:(.*)|s", $post, $body); 232 $body = trim($body[1]); 233 $post_content = $wpdb->escape($body.$extended); 234 $post = preg_replace("|(-----\nBODY:.*)|s", '', $post); 237 if ( !comment_exists($comment['comment_author'], $comment['comment_date'])) { 238 $comment['comment_post_ID'] = $post_id; 239 $comment = wp_filter_comment($comment); 240 wp_insert_comment($comment); 241 $num_comments++; 242 } 243 } 235 244 236 // Grab the metadata from what's left 237 $metadata = explode("\n", $post); 238 foreach ($metadata as $line) { 239 preg_match("/^(.*?):(.*)/", $line, $token); 240 $key = trim($token[1]); 241 $value = trim($token[2]); 242 // Now we decide what it is and what to do with it 243 switch ($key) { 244 case '' : 245 break; 246 case 'AUTHOR' : 247 $post_author = $value; 248 break; 249 case 'TITLE' : 250 $post_title = $wpdb->escape($value); 251 break; 252 case 'STATUS' : 253 // "publish" and "draft" enumeration items match up; no change required 254 $post_status = $value; 255 if (empty ($post_status)) 256 $post_status = 'publish'; 257 break; 258 case 'ALLOW COMMENTS' : 259 $post_allow_comments = $value; 260 if ($post_allow_comments == 1) { 261 $comment_status = 'open'; 262 } else { 263 $comment_status = 'closed'; 264 } 265 break; 266 case 'CONVERT BREAKS' : 267 $post_convert_breaks = $value; 268 break; 269 case 'ALLOW PINGS' : 270 $ping_status = trim($meta[2][0]); 271 if ($ping_status == 1) { 272 $ping_status = 'open'; 273 } else { 274 $ping_status = 'closed'; 275 } 276 break; 277 case 'PRIMARY CATEGORY' : 278 if (! empty ($value) ) 279 $post_categories[] = $wpdb->escape($value); 280 break; 281 case 'CATEGORY' : 282 if (! empty ($value) ) 283 $post_categories[] = $wpdb->escape($value); 284 break; 285 case 'DATE' : 286 $post_modified = strtotime($value); 287 $post_modified = date('Y-m-d H:i:s', $post_modified); 288 $post_modified_gmt = get_gmt_from_date("$post_modified"); 289 $post_date = $post_modified; 290 $post_date_gmt = $post_modified_gmt; 291 break; 292 default : 293 // echo "\n$key: $value"; 294 break; 295 } // end switch 296 } // End foreach 245 if ( $num_comments ) 246 printf(' '.__('(%s comments)'), $num_comments); 297 247 298 // Let's check to see if it's in already 299 if ($post_id = post_exists($post_title, '', $post_date)) { 300 echo '<li>'; 301 printf(__('Post <i>%s</i> already exists.'), stripslashes($post_title)); 302 } else { 303 echo '<li>'; 304 printf(__('Importing post <i>%s</i>...'), stripslashes($post_title)); 248 $num_pings = 0; 249 foreach ( $pings as $ping ) { 250 $ping = get_object_vars($ping); 251 $ping = add_magic_quotes($ping); 305 252 306 $post_author = $this->checkauthor($post_author); //just so that if a post already exists, new users are not created by checkauthor 253 if ( !comment_exists($ping['comment_author'], $ping['comment_date'])) { 254 $ping['comment_content'] = "<strong>{$ping['title']}</strong>\n\n{$ping['comment_content']}"; 255 $ping['comment_post_ID'] = $post_id; 256 $ping = wp_filter_comment($ping); 257 wp_insert_comment($ping); 258 $num_pings++; 259 } 260 } 307 261 308 $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'); 309 $post_id = wp_insert_post($postdata); 310 // Add categories. 311 if (0 != count($post_categories)) { 312 wp_create_categories($post_categories, $post_id); 313 } 314 } 262 if ( $num_pings ) 263 printf(' '.__('(%s pings)'), $num_pings); 315 264 316 $comment_post_ID = (int) $post_id; 317 $comment_approved = 1; 265 echo "</li>"; 266 //ob_flush();flush(); 267 } 318 268 319 // Now for comments 320 $comments = explode("-----\nCOMMENT:", $comments[0]); 321 $num_comments = 0; 322 foreach ($comments as $comment) { 323 if ('' != trim($comment)) { 324 // Author 325 preg_match("|AUTHOR:(.*)|", $comment, $comment_author); 326 $comment_author = $wpdb->escape(trim($comment_author[1])); 327 $comment = preg_replace('|(\n?AUTHOR:.*)|', '', $comment); 328 preg_match("|EMAIL:(.*)|", $comment, $comment_author_email); 329 $comment_author_email = $wpdb->escape(trim($comment_author_email[1])); 330 $comment = preg_replace('|(\n?EMAIL:.*)|', '', $comment); 269 function process_posts() { 270 global $wpdb; 331 271 332 preg_match("|IP:(.*)|", $comment, $comment_author_IP);333 $comment_author_IP = trim($comment_author_IP[1]);334 $comment = preg_replace('|(\n?IP:.*)|', '', $comment);272 $handle = fopen($this->file, 'r'); 273 if ( $handle == null ) 274 return false; 335 275 336 preg_match("|URL:(.*)|", $comment, $comment_author_url); 337 $comment_author_url = $wpdb->escape(trim($comment_author_url[1])); 338 $comment = preg_replace('|(\n?URL:.*)|', '', $comment); 276 $context = ''; 277 $post = new StdClass(); 278 $comment = new StdClass(); 279 $comments = array(); 280 $ping = new StdClass(); 281 $pings = array(); 282 283 echo "<div class='wrap'><ol>"; 339 284 340 preg_match("|DATE:(.*)|", $comment, $comment_date); 341 $comment_date = trim($comment_date[1]); 342 $comment_date = date('Y-m-d H:i:s', strtotime($comment_date)); 343 $comment = preg_replace('|(\n?DATE:.*)|', '', $comment); 285 while ( $line = fgets($handle) ) { 286 $line = trim($line); 344 287 345 $comment_content = $wpdb->escape(trim($comment)); 346 $comment_content = str_replace('-----', '', $comment_content); 347 // Check if it's already there 348 if (!comment_exists($comment_author, $comment_date)) { 349 $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_author_IP', 'comment_date', 'comment_content', 'comment_approved'); 350 $commentdata = wp_filter_comment($commentdata); 351 wp_insert_comment($commentdata); 352 $num_comments++; 353 } 354 } 288 if ( '-----' == $line ) { 289 // Finishing a multi-line field 290 if ( 'comment' == $context ) { 291 $comments[] = $comment; 292 $comment = new StdClass(); 293 } else if ( 'ping' == $context ) { 294 $pings[] = $ping; 295 $ping = new StdClass(); 355 296 } 356 if ( $num_comments ) 357 printf(' '.__('(%s comments)'), $num_comments); 358 359 // Finally the pings 360 // fix the double newline on the first one 361 $pings[0] = str_replace("-----\n\n", "-----\n", $pings[0]); 362 $pings = explode("-----\nPING:", $pings[0]); 363 $num_pings = 0; 364 foreach ($pings as $ping) { 365 if ('' != trim($ping)) { 366 // 'Author' 367 preg_match("|BLOG NAME:(.*)|", $ping, $comment_author); 368 $comment_author = $wpdb->escape(trim($comment_author[1])); 369 $ping = preg_replace('|(\n?BLOG NAME:.*)|', '', $ping); 370 371 preg_match("|IP:(.*)|", $ping, $comment_author_IP); 372 $comment_author_IP = trim($comment_author_IP[1]); 373 $ping = preg_replace('|(\n?IP:.*)|', '', $ping); 374 375 preg_match("|URL:(.*)|", $ping, $comment_author_url); 376 $comment_author_url = $wpdb->escape(trim($comment_author_url[1])); 377 $ping = preg_replace('|(\n?URL:.*)|', '', $ping); 378 379 preg_match("|DATE:(.*)|", $ping, $comment_date); 380 $comment_date = trim($comment_date[1]); 381 $comment_date = date('Y-m-d H:i:s', strtotime($comment_date)); 382 $ping = preg_replace('|(\n?DATE:.*)|', '', $ping); 383 384 preg_match("|TITLE:(.*)|", $ping, $ping_title); 385 $ping_title = $wpdb->escape(trim($ping_title[1])); 386 $ping = preg_replace('|(\n?TITLE:.*)|', '', $ping); 387 388 $comment_content = $wpdb->escape(trim($ping)); 389 $comment_content = str_replace('-----', '', $comment_content); 390 391 $comment_content = "<strong>$ping_title</strong>\n\n$comment_content"; 392 393 $comment_type = 'trackback'; 394 395 // Check if it's already there 396 if (!comment_exists($comment_author, $comment_date)) { 397 $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_author_IP', 'comment_date', 'comment_content', 'comment_type', 'comment_approved'); 398 $commentdata = wp_filter_comment($commentdata); 399 wp_insert_comment($commentdata); 400 $num_pings++; 401 } 402 } 297 $context = ''; 298 } else if ( '--------' == $line ) { 299 // Finishing a post. 300 $context = ''; 301 $this->save_post($post, $comments, $pings); 302 $post = new StdClass; 303 $comment = new StdClass(); 304 $ping = new StdClass(); 305 $comments = array(); 306 $pings = array(); 307 } else if ( 'BODY:' == $line ) { 308 $context = 'body'; 309 } else if ( 'EXTENDED BODY:' == $line ) { 310 $context = 'extended'; 311 } else if ( 'EXCERPT:' == $line ) { 312 $context = 'excerpt'; 313 } else if ( 'KEYWORDS:' == $line ) { 314 $context = 'keywords'; 315 } else if ( 'COMMENT:' == $line ) { 316 $context = 'comment'; 317 } else if ( 'PING:' == $line ) { 318 $context = 'ping'; 319 } else if ( 0 === strpos($line, "AUTHOR:") ) { 320 $author = trim( substr($line, strlen("AUTHOR:")) ); 321 if ( '' == $context ) 322 $post->post_author = $author; 323 else if ( 'comment' == $context ) 324 $comment->comment_author = $author; 325 } else if ( 0 === strpos($line, "TITLE:") ) { 326 $title = trim( substr($line, strlen("TITLE:")) ); 327 if ( '' == $context ) 328 $post->post_title = $title; 329 else if ( 'ping' == $context ) 330 $ping->title = $title; 331 } else if ( 0 === strpos($line, "STATUS:") ) { 332 $status = trim( substr($line, strlen("STATUS:")) ); 333 if ( empty($status) ) 334 $status = 'publish'; 335 $post->post_status = $status; 336 } else if ( 0 === strpos($line, "ALLOW COMMENTS:") ) { 337 $allow = trim( substr($line, strlen("ALLOW COMMENTS:")) ); 338 if ( $allow == 1 ) 339 $post->comment_status = 'open'; 340 else 341 $post->comment_status = 'closed'; 342 } else if ( 0 === strpos($line, "ALLOW PINGS:") ) { 343 $allow = trim( substr($line, strlen("ALLOW PINGS:")) ); 344 if ( $allow == 1 ) 345 $post->ping_status = 'open'; 346 else 347 $post->ping_status = 'closed'; 348 } else if ( 0 === strpos($line, "CATEGORY:") ) { 349 $category = trim( substr($line, strlen("CATEGORY:")) ); 350 $post->categories[] = $category; 351 } else if ( 0 === strpos($line, "PRIMARY CATEGORY:") ) { 352 $category = trim( substr($line, strlen("PRIMARY CATEGORY:")) ); 353 $post->categories[] = $category; 354 } else if ( 0 === strpos($line, "DATE:") ) { 355 $date = trim( substr($line, strlen("DATE:")) ); 356 $date = strtotime($date); 357 $date = date('Y-m-d H:i:s', $date); 358 $date_gmt = get_gmt_from_date($date); 359 if ( '' == $context ) { 360 $post->post_modified = $date; 361 $post->post_modified_gmt = $date_gmt; 362 $post->post_date = $date; 363 $post->post_date_gmt = $date_gmt; 364 } else if ( 'comment' == $context ) { 365 $comment->comment_date = $date; 366 } else if ( 'ping' == $context ) { 367 $ping->comment_date = $date; 403 368 } 404 if ( $num_pings ) 405 printf(' '.__('(%s pings)'), $num_pings); 369 } else if ( 0 === strpos($line, "EMAIL:") ) { 370 $email = trim( substr($line, strlen("EMAIL:")) ); 371 if ( 'comment' == $context ) 372 $comment->comment_author_email = $email; 373 else 374 $ping->comment_author_email = $email; 375 } else if ( 0 === strpos($line, "IP:") ) { 376 $ip = trim( substr($line, strlen("IP:")) ); 377 if ( 'comment' == $context ) 378 $comment->comment_author_IP = $ip; 379 else 380 $ping->comment_author_IP = $ip; 381 } else if ( 0 === strpos($line, "URL:") ) { 382 $url = trim( substr($line, strlen("URL:")) ); 383 if ( 'comment' == $context ) 384 $comment->comment_author_url = $url; 385 else 386 $ping->comment_author_url = $url; 387 } else if ( 0 === strpos($line, "BLOG NAME:") ) { 388 $blog = trim( substr($line, strlen("BLOG NAME:")) ); 389 $ping->comment_author = $blog; 390 } else { 391 // Processing multi-line field, check context. 406 392 407 echo "</li>"; 393 $line .= "\n"; 394 if ( 'body' == $context ) { 395 $post->post_content .= $line; 396 } else if ( 'extended' == $context ) { 397 $post->extended .= $line; 398 } else if ( 'excerpt' == $context ) { 399 $post->post_excerpt .= $line; 400 } else if ( 'comment' == $context ) { 401 $comment->comment_content .= $line; 402 } else if ( 'ping' == $context ) { 403 $ping->comment_content .= $line; 404 } 408 405 } 409 406 } 410 407 … … 422 419 else 423 420 $this->file = get_attached_file($this->id); 424 421 $this->get_authors_from_post(); 425 $this->get_entries();426 422 $this->process_posts(); 427 423 } 428 424
