[ Index ] |
PHP Cross Reference of Textpattern 4.0.8 |
[Summary view] [Print] [Text view]
1 <?php 2 3 function doImportWP($b2dblogin, $b2db, $b2dbpass, $b2dbhost, $wpdbprefix, $insert_into_section, $insert_with_status, $default_comment_invite) 4 { 5 global $txpcfg; 6 7 $b2link = mysql_connect($b2dbhost, $b2dblogin, $b2dbpass, true); 8 9 if (!$b2link) 10 { 11 return 'WordPress database values don’t work. Go back, replace them and try again.'; 12 } 13 14 mysql_select_db($b2db, $b2link); 15 16 17 // Keep some response on some part 18 $results = array(); 19 $errors = array(); 20 21 $results[] = hed('Connected to WordPress database. Importing Data…', 1); 22 23 24 /* 25 export users 26 */ 27 28 $users = array(); 29 30 31 $user_query = mysql_query(" 32 select 33 ID as user_id, 34 user_login as name, 35 user_email as email, 36 display_name as RealName 37 from ".$wpdbprefix."users 38 ", $b2link) or $errors[] = mysql_error(); 39 40 while ($user = mysql_fetch_array($user_query)) 41 { 42 $user_privs_query = mysql_query(" 43 select 44 meta_value 45 from ".$wpdbprefix."usermeta 46 where user_id = ".$user['user_id']." and meta_key = 'capabilities' 47 ", $b2link) or $errors[] = mysql_error(); 48 49 $privs = unserialize(mysql_result($user_privs_query, 0)); 50 51 foreach ($privs as $key => $val) 52 { 53 // convert the built-in WordPress roles 54 // to their Txp equivalent 55 switch ($key) 56 { 57 // publisher 58 case 'administrator': 59 $user['privs'] = 1; 60 break; 61 62 // managing editor 63 case 'editor': 64 $user['privs'] = 2; 65 break; 66 67 // staff writer 68 case 'author': 69 $user['privs'] = 4; 70 break; 71 72 // freelancer 73 case 'contributor': 74 $user['privs'] = 5; 75 break; 76 77 // none 78 case 'subscriber': 79 default: 80 $user['privs'] = 0; 81 break; 82 } 83 } 84 85 $users[] = $user; 86 } 87 88 89 /* 90 export article and link categories 91 */ 92 93 $categories = array(); 94 95 $category_query = mysql_query(" 96 select 97 t.slug as name, 98 t.name as title, 99 tt.taxonomy as type, 100 tt.parent as parent 101 from ".$wpdbprefix."terms as t inner join ".$wpdbprefix."term_taxonomy as tt 102 on(t.term_id = tt.term_id) 103 order by field(tt.taxonomy, 'category','post_tag','link_category'), tt.parent asc, t.name asc 104 ", $b2link) or $errors[] = mysql_error(); 105 106 while ($category = mysql_fetch_array($category_query)) 107 { 108 if ($category['parent'] != 0) 109 { 110 $category_parent_query = mysql_query(" 111 select 112 slug as name 113 from ".$wpdbprefix."terms 114 where term_id = '".doSlash($category['parent'])."' 115 ", $b2link) or $errors[] = mysql_error(); 116 117 while ($parent = mysql_fetch_array($category_parent_query)) 118 { 119 $category['parent'] = $parent['name']; 120 } 121 } 122 123 else 124 { 125 $category['parent'] = 'root'; 126 } 127 128 switch ($category['type']) 129 { 130 case 'post_tag': 131 case 'category': 132 $category['type'] = 'article'; 133 break; 134 135 case 'link_category': 136 $category['type'] = 'link'; 137 break; 138 } 139 140 $categories[] = $category; 141 } 142 143 144 /* 145 export articles 146 */ 147 148 $article_query = mysql_query(" 149 select 150 p.ID as ID, 151 p.post_status as Status, 152 p.post_date as Posted, 153 p.post_modified as LastMod, 154 p.post_title as Title, 155 p.post_content as Body, 156 p.comment_status as Annotate, 157 p.comment_count as comments_count, 158 p.post_name as url_title, 159 u.user_login as AuthorID 160 from ".$wpdbprefix."posts as p left join ".$wpdbprefix."users as u 161 on u.ID = p.post_author 162 order by p.ID asc 163 ", $b2link) or $errors[] = mysql_error(); 164 165 while ($article = mysql_fetch_array($article_query)) 166 { 167 // convert WP article status to Txp equivalent 168 switch ($article['Status']) 169 { 170 case 'draft': 171 $article['Status'] = 1; 172 break; 173 174 // hidden 175 case 'private': 176 $article['Status'] = 2; 177 break; 178 179 case 'pending': 180 $article['Status'] = 3; 181 break; 182 183 // live 184 case 'publish': 185 $article['Status'] = 4; 186 break; 187 188 default: 189 $article['Status'] = $insert_with_status; 190 break; 191 } 192 193 // convert WP comment status to Txp equivalent 194 switch ($article['Annotate']) 195 { 196 // on 197 case 'open': 198 $article['Annotate'] = 1; 199 break; 200 201 // off 202 case 'closed': 203 case 'registered_only': 204 $article['Annotate'] = 0; 205 break; 206 } 207 208 // article commments 209 $comments = array(); 210 211 $comment_query = mysql_query(" 212 select 213 comment_author_IP as ip, 214 comment_author as name, 215 comment_author_email as email, 216 comment_author_url as web, 217 comment_content as message, 218 comment_date as posted 219 from ".$wpdbprefix."comments 220 where comment_post_ID = '".$article['ID']."' 221 order by comment_ID asc 222 ", $b2link) or $errors[]= mysql_error(); 223 224 while ($comment = mysql_fetch_assoc($comment_query)) 225 { 226 $comments[] = $comment; 227 } 228 229 $article['comments'] = $comments; 230 231 232 // article categories 233 $article_categories = array(); 234 235 $article_category_query = mysql_query(" 236 select 237 t.name as title, 238 t.slug as name 239 from ".$wpdbprefix."terms as t inner join ".$wpdbprefix."term_taxonomy as tt 240 on(t.term_id = tt.term_id) 241 inner join ".$wpdbprefix."term_relationships as tr 242 on(tt.term_taxonomy_id = tr.term_taxonomy_id) 243 where tr.object_id = '".$article['ID']."' and tt.taxonomy in('post_tag', 'category') 244 order by tr.object_id asc, t.name asc 245 limit 2; 246 ", $b2link) or $errors[] = mysql_error(); 247 248 while ($category = mysql_fetch_array($article_category_query)) 249 { 250 $article_categories[] = $category; 251 } 252 253 $article['Category1'] = !empty($article_categories[0]) ? $article_categories[0]['name'] : ''; 254 $article['Category2'] = !empty($article_categories[1]) ? $article_categories[1]['name'] : ''; 255 256 257 $articles[] = $article; 258 } 259 260 261 /* 262 export links 263 */ 264 265 $links = array(); 266 267 $link_query = mysql_query(" 268 select 269 link_id as id, 270 link_name as linkname, 271 link_description as description, 272 link_updated as date, 273 link_url as url 274 from ".$wpdbprefix."links 275 order by link_id asc 276 ", $b2link) or $errors[] = mysql_error(); 277 278 while ($link = mysql_fetch_array($link_query)) 279 { 280 // link categories 281 $link_categories = array(); 282 283 $link_category_query = mysql_query(" 284 select 285 t.name as title, 286 t.slug as name 287 from ".$wpdbprefix."terms as t inner join ".$wpdbprefix."term_taxonomy as tt 288 on(t.term_id = tt.term_id) 289 inner join ".$wpdbprefix."term_relationships as tr 290 on(tt.term_taxonomy_id = tr.term_taxonomy_id) 291 where tr.object_id = '".$link['id']."' and tt.taxonomy = 'link_category' 292 order by tr.object_id asc, t.name asc 293 ", $b2link) or $errors[] = mysql_error(); 294 295 while ($category = mysql_fetch_array($link_category_query)) 296 { 297 $link['category'] = $category['name']; 298 } 299 300 301 $links[] = $link; 302 } 303 304 305 306 mysql_close($b2link); 307 308 309 /* 310 begin import 311 */ 312 313 314 // keep a handy copy of txpdb values, and do not alter Dean code 315 // for now! ;-) 316 317 $txpdb = $txpcfg['db']; 318 $txpdblogin = $txpcfg['user']; 319 $txpdbpass = $txpcfg['pass']; 320 $txpdbhost = $txpcfg['host']; 321 322 // Yes, we have to make a new connection 323 // otherwise doArray complains 324 $DB = new DB; 325 $txplink = &$DB->link; 326 327 mysql_select_db($txpdb, $txplink); 328 329 330 /* 331 import users 332 */ 333 334 if ($users) 335 { 336 include_once txpath.'/lib/txplib_admin.php'; 337 338 $results[] = hed('Imported Users:', 2). 339 n.graf('Because WordPress uses a different password mechanism than Textpattern, you will need to reset each user’s password from <a href="index.php?event=admin">the Users tab</a>.'). 340 n.'<ul>'; 341 342 foreach ($users as $user) 343 { 344 extract($user); 345 346 if (!safe_row('user_id', 'txp_users', "name = '".doSlash($name)."'")) 347 { 348 $pass = doSlash(generate_password(6)); 349 $nonce = doSlash(md5(uniqid(mt_rand(), TRUE))); 350 351 $rs = mysql_query(" 352 insert into ".safe_pfx('txp_users')." set 353 name = '".doSlash($name)."', 354 pass = '".doSlash($pass)."', 355 email = '".doSlash($email)."', 356 RealName = '".doSlash($RealName)."', 357 privs = ".$privs.", 358 nonce = '".doSlash($nonce)."' 359 ", $txplink) or $errors[] = mysql_error(); 360 361 if (mysql_insert_id()) 362 { 363 $results[] = '<li>'.$name.' ('.$RealName.')</li>'; 364 } 365 } 366 } 367 368 $results[] = '</ul>'; 369 } 370 371 372 373 /* 374 import categories 375 */ 376 377 if ($categories) 378 { 379 $results[] = hed('Imported Categories:', 2).n.'<ul>'; 380 381 foreach ($categories as $category) 382 { 383 extract($category); 384 385 if (!safe_row('id', 'txp_category', "name = '".doSlash($name)."' and type = '".doSlash($type)."' and parent = '".doSlash($parent)."'")) 386 { 387 $rs = mysql_query(" 388 insert into ".safe_pfx('txp_category')." set 389 name = '".doSlash($name)."', 390 title = '".doSlash($title)."', 391 type = '".doSlash($type)."', 392 parent = '".doSlash($parent)."' 393 ", $txplink) or $errors[] = mysql_error(); 394 395 if (mysql_insert_id()) 396 { 397 $results[] = '<li>'.$title.' ('.$type.')</li>'; 398 } 399 } 400 } 401 402 rebuild_tree_full('article'); 403 rebuild_tree_full('link'); 404 405 $results[] = '</ul>'; 406 } 407 408 409 /* 410 import articles 411 */ 412 413 if ($articles) 414 { 415 $results[] = hed('Imported Articles and Comments:', 2).n.'<ul>'; 416 417 include txpath.'/lib/classTextile.php'; 418 419 $textile = new Textile; 420 421 foreach ($articles as $article) 422 { 423 extract($article); 424 425 // Ugly, really ugly way to workaround the slashes WP gotcha 426 $Body = str_replace('<!--more-->', '', $Body); 427 $Body_html = $textile->textileThis($Body); 428 429 // can not use array slash due to way on which comments are selected 430 $rs = mysql_query(" 431 insert into ".safe_pfx('textpattern')." set 432 Posted = '".doSlash($Posted)."', 433 LastMod = '".doSlash($LastMod)."', 434 Title = '".doSlash($textile->TextileThis($Title, 1))."', 435 url_title = '".doSlash($url_title)."', 436 Body = '".doSlash($Body)."', 437 Body_html = '".doSlash($Body_html)."', 438 AuthorID = '".doSlash($AuthorID)."', 439 Category1 = '".doSlash($Category1)."', 440 Category2 = '".doSlash($Category2)."', 441 Section = '$insert_into_section', 442 uid = '".md5(uniqid(rand(), true))."', 443 feed_time = '".substr($Posted, 0, 10)."', 444 Annotate = '".doSlash($Annotate)."', 445 AnnotateInvite = '$default_comment_invite', 446 Status = '".doSlash($Status)."' 447 ", $txplink) or $errors[] = mysql_error(); 448 449 if ((int)$insert_id = mysql_insert_id()) 450 { 451 $results[] = '<li>'.$Title.'</li>'; 452 453 if (!empty($comments)) 454 { 455 $inserted_comments = 0; 456 457 foreach ($comments as $comment) 458 { 459 extract(array_slash($comment)); 460 461 // The ugly workaroud again 462 $message = nl2br($message); 463 464 $rs = mysql_query(" 465 insert into ".safe_pfx('txp_discuss')." set 466 parentid = '$insert_id', 467 name = '".doSlash($name)."', 468 email = '".doSlash($email)."', 469 web = '".doSlash($web)."', 470 ip = '".doSlash($ip)."', 471 posted = '".doSlash($posted)."', 472 message = '".doSlash($message)."', 473 visible = 1 474 ", $txplink) or $results[] = mysql_error(); 475 476 if (mysql_insert_id()) 477 { 478 $inserted_comments++; 479 } 480 } 481 482 $results[] = '<li>- '.$inserted_comments.' of '.$comments_count.' comment(s)</li>'; 483 } 484 } 485 } 486 487 $results[] = '</ul>'; 488 } 489 490 491 /* 492 import links 493 */ 494 495 if ($links) 496 { 497 $results[] = hed('Imported Links:', 2).n.'<ul>'; 498 499 foreach ($links as $link) 500 { 501 extract($link); 502 503 $rs = mysql_query(" 504 insert into ".safe_pfx('txp_link')." set 505 linkname = '".doSlash($linkname)."', 506 linksort = '".doSlash($linkname)."', 507 description = '".doSlash($description)."', 508 category = '".doSlash($category)."', 509 date = '".doSlash($date)."', 510 url = '".doSlash($url)."' 511 ", $txplink) or $errors[] = mysql_error(); 512 513 if (mysql_insert_id()) 514 { 515 $results[] = '<li>'.$linkname.'</li>'; 516 } 517 } 518 519 $results[] = '</ul>'; 520 } 521 522 523 /* 524 show any errors we encountered 525 */ 526 527 if ($errors) 528 { 529 $results[] = hed('Errors Encountered:', 2).n.'<ul>'; 530 531 foreach ($errors as $error) 532 { 533 $results[] = '<li>'.$error.'</li>'; 534 } 535 536 $results[] = '</ul>'; 537 } 538 539 540 return join(n, $results); 541 } 542 543 function undoSlash($in) 544 { 545 return doArray($in, 'stripslashes'); 546 } 547 548 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu May 21 23:03:01 2009 | Cross-referenced by PHPXref 0.7 |