PHP Cross Reference of WordPress Subversion HEAD

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]

title

Body

[close]

/wp-admin/import/ -> textpattern.php (source)

   1  <?php
   2  /**
   3      Add These Functions to make our lives easier
   4  **/
   5  
   6  if(!function_exists('get_comment_count'))
   7  {
   8  	function get_comment_count($post_ID)
   9      {
  10          global $wpdb;
  11          return $wpdb->get_var('SELECT count(*) FROM '.$wpdb->comments.' WHERE comment_post_ID = '.$post_ID);
  12      }
  13  }
  14  
  15  if(!function_exists('link_exists'))
  16  {
  17  	function link_exists($linkname)
  18      {
  19          global $wpdb;
  20          return $wpdb->get_var('SELECT link_id FROM '.$wpdb->links.' WHERE link_name = "'.$wpdb->escape($linkname).'"');
  21      }
  22  }
  23  
  24  /**
  25      The Main Importer Class
  26  **/
  27  class Textpattern_Import {
  28  
  29  	function header()
  30      {
  31          echo '<div class="wrap">';
  32          echo '<h2>'.__('Import Textpattern').'</h2>';
  33          echo '<p>'.__('Steps may take a few minutes depending on the size of your database. Please be patient.').'</p>';
  34      }
  35  
  36  	function footer()
  37      {
  38          echo '</div>';
  39      }
  40  
  41  	function greet() {
  42          echo '<div class="narrow">';
  43          echo '<p>'.__('Howdy! This imports categories, users, posts, comments, and links from any Textpattern 4.0.2+ into this blog.').'</p>';
  44          echo '<p>'.__('This has not been tested on previous versions of Textpattern.  Mileage may vary.').'</p>';
  45          echo '<p>'.__('Your Textpattern Configuration settings are as follows:').'</p>';
  46          echo '<form action="admin.php?import=textpattern&amp;step=1" method="post">';
  47          wp_nonce_field('import-textpattern');
  48          $this->db_form();
  49          echo '<p class="submit"><input type="submit" name="submit" value="'.attribute_escape(__('Import Categories &raquo;')).'" /></p>';
  50          echo '</form>';
  51          echo '</div>';
  52      }
  53  
  54  	function get_txp_cats()
  55      {
  56          global $wpdb;
  57          // General Housekeeping
  58          $txpdb = new wpdb(get_option('txpuser'), get_option('txppass'), get_option('txpname'), get_option('txphost'));
  59          set_magic_quotes_runtime(0);
  60          $prefix = get_option('tpre');
  61  
  62          // Get Categories
  63          return $txpdb->get_results('SELECT
  64              id,
  65              name,
  66              title
  67              FROM '.$prefix.'txp_category
  68              WHERE type = "article"',
  69              ARRAY_A);
  70      }
  71  
  72  	function get_txp_users()
  73      {
  74          global $wpdb;
  75          // General Housekeeping
  76          $txpdb = new wpdb(get_option('txpuser'), get_option('txppass'), get_option('txpname'), get_option('txphost'));
  77          set_magic_quotes_runtime(0);
  78          $prefix = get_option('tpre');
  79  
  80          // Get Users
  81  
  82          return $txpdb->get_results('SELECT
  83              user_id,
  84              name,
  85              RealName,
  86              email,
  87              privs
  88              FROM '.$prefix.'txp_users', ARRAY_A);
  89      }
  90  
  91  	function get_txp_posts()
  92      {
  93          // General Housekeeping
  94          $txpdb = new wpdb(get_option('txpuser'), get_option('txppass'), get_option('txpname'), get_option('txphost'));
  95          set_magic_quotes_runtime(0);
  96          $prefix = get_option('tpre');
  97  
  98          // Get Posts
  99          return $txpdb->get_results('SELECT
 100              ID,
 101              Posted,
 102              AuthorID,
 103              LastMod,
 104              Title,
 105              Body,
 106              Excerpt,
 107              Category1,
 108              Category2,
 109              Status,
 110              Keywords,
 111              url_title,
 112              comments_count
 113              FROM '.$prefix.'textpattern
 114              ', ARRAY_A);
 115      }
 116  
 117  	function get_txp_comments()
 118      {
 119          global $wpdb;
 120          // General Housekeeping
 121          $txpdb = new wpdb(get_option('txpuser'), get_option('txppass'), get_option('txpname'), get_option('txphost'));
 122          set_magic_quotes_runtime(0);
 123          $prefix = get_option('tpre');
 124  
 125          // Get Comments
 126          return $txpdb->get_results('SELECT * FROM '.$prefix.'txp_discuss', ARRAY_A);
 127      }
 128  
 129  		function get_txp_links()
 130      {
 131          //General Housekeeping
 132          $txpdb = new wpdb(get_option('txpuser'), get_option('txppass'), get_option('txpname'), get_option('txphost'));
 133          set_magic_quotes_runtime(0);
 134          $prefix = get_option('tpre');
 135  
 136          return $txpdb->get_results('SELECT
 137              id,
 138              date,
 139              category,
 140              url,
 141              linkname,
 142              description
 143              FROM '.$prefix.'txp_link',
 144              ARRAY_A);
 145      }
 146  
 147  	function cat2wp($categories='')
 148      {
 149          // General Housekeeping
 150          global $wpdb;
 151          $count = 0;
 152          $txpcat2wpcat = array();
 153          // Do the Magic
 154          if(is_array($categories))
 155          {
 156              echo '<p>'.__('Importing Categories...').'<br /><br /></p>';
 157              foreach ($categories as $category)
 158              {
 159                  $count++;
 160                  extract($category);
 161  
 162  
 163                  // Make Nice Variables
 164                  $name = $wpdb->escape($name);
 165                  $title = $wpdb->escape($title);
 166  
 167                  if($cinfo = category_exists($name))
 168                  {
 169                      $ret_id = wp_insert_category(array('cat_ID' => $cinfo, 'category_nicename' => $name, 'cat_name' => $title));
 170                  }
 171                  else
 172                  {
 173                      $ret_id = wp_insert_category(array('category_nicename' => $name, 'cat_name' => $title));
 174                  }
 175                  $txpcat2wpcat[$id] = $ret_id;
 176              }
 177  
 178              // Store category translation for future use
 179              add_option('txpcat2wpcat',$txpcat2wpcat);
 180              echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> categories imported.'), $count).'<br /><br /></p>';
 181              return true;
 182          }
 183          echo __('No Categories to Import!');
 184          return false;
 185      }
 186  
 187  	function users2wp($users='')
 188      {
 189          // General Housekeeping
 190          global $wpdb;
 191          $count = 0;
 192          $txpid2wpid = array();
 193  
 194          // Midnight Mojo
 195          if(is_array($users))
 196          {
 197              echo '<p>'.__('Importing Users...').'<br /><br /></p>';
 198              foreach($users as $user)
 199              {
 200                  $count++;
 201                  extract($user);
 202  
 203                  // Make Nice Variables
 204                  $name = $wpdb->escape($name);
 205                  $RealName = $wpdb->escape($RealName);
 206  
 207                  if($uinfo = get_userdatabylogin($name))
 208                  {
 209  
 210                      $ret_id = wp_insert_user(array(
 211                                  'ID'            => $uinfo->ID,
 212                                  'user_login'    => $name,
 213                                  'user_nicename'    => $RealName,
 214                                  'user_email'    => $email,
 215                                  'user_url'        => 'http://',
 216                                  'display_name'    => $name)
 217                                  );
 218                  }
 219                  else
 220                  {
 221                      $ret_id = wp_insert_user(array(
 222                                  'user_login'    => $name,
 223                                  'user_nicename'    => $RealName,
 224                                  'user_email'    => $email,
 225                                  'user_url'        => 'http://',
 226                                  'display_name'    => $name)
 227                                  );
 228                  }
 229                  $txpid2wpid[$user_id] = $ret_id;
 230  
 231                  // Set Textpattern-to-WordPress permissions translation
 232                  $transperms = array(1 => '10', 2 => '9', 3 => '5', 4 => '4', 5 => '3', 6 => '2', 7 => '0');
 233  
 234                  // Update Usermeta Data
 235                  $user = new WP_User($ret_id);
 236                  if('10' == $transperms[$privs]) { $user->set_role('administrator'); }
 237                  if('9'  == $transperms[$privs]) { $user->set_role('editor'); }
 238                  if('5'  == $transperms[$privs]) { $user->set_role('editor'); }
 239                  if('4'  == $transperms[$privs]) { $user->set_role('author'); }
 240                  if('3'  == $transperms[$privs]) { $user->set_role('contributor'); }
 241                  if('2'  == $transperms[$privs]) { $user->set_role('contributor'); }
 242                  if('0'  == $transperms[$privs]) { $user->set_role('subscriber'); }
 243  
 244                  update_usermeta( $ret_id, 'wp_user_level', $transperms[$privs] );
 245                  update_usermeta( $ret_id, 'rich_editing', 'false');
 246              }// End foreach($users as $user)
 247  
 248              // Store id translation array for future use
 249              add_option('txpid2wpid',$txpid2wpid);
 250  
 251  
 252              echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> users imported.'), $count).'<br /><br /></p>';
 253              return true;
 254          }// End if(is_array($users)
 255  
 256          echo __('No Users to Import!');
 257          return false;
 258  
 259      }// End function user2wp()
 260  
 261  	function posts2wp($posts='')
 262      {
 263          // General Housekeeping
 264          global $wpdb;
 265          $count = 0;
 266          $txpposts2wpposts = array();
 267          $cats = array();
 268  
 269          // Do the Magic
 270          if(is_array($posts))
 271          {
 272              echo '<p>'.__('Importing Posts...').'<br /><br /></p>';
 273              foreach($posts as $post)
 274              {
 275                  $count++;
 276                  extract($post);
 277  
 278                  // Set Textpattern-to-WordPress status translation
 279                  $stattrans = array(1 => 'draft', 2 => 'private', 3 => 'draft', 4 => 'publish', 5 => 'publish');
 280  
 281                  //Can we do this more efficiently?
 282                  $uinfo = ( get_userdatabylogin( $AuthorID ) ) ? get_userdatabylogin( $AuthorID ) : 1;
 283                  $authorid = ( is_object( $uinfo ) ) ? $uinfo->ID : $uinfo ;
 284  
 285                  $Title = $wpdb->escape($Title);
 286                  $Body = $wpdb->escape($Body);
 287                  $Excerpt = $wpdb->escape($Excerpt);
 288                  $post_status = $stattrans[$Status];
 289  
 290                  // Import Post data into WordPress
 291  
 292                  if($pinfo = post_exists($Title,$Body))
 293                  {
 294                      $ret_id = wp_insert_post(array(
 295                          'ID'                => $pinfo,
 296                          'post_date'            => $Posted,
 297                          'post_date_gmt'        => $post_date_gmt,
 298                          'post_author'        => $authorid,
 299                          'post_modified'        => $LastMod,
 300                          'post_modified_gmt' => $post_modified_gmt,
 301                          'post_title'        => $Title,
 302                          'post_content'        => $Body,
 303                          'post_excerpt'        => $Excerpt,
 304                          'post_status'        => $post_status,
 305                          'post_name'            => $url_title,
 306                          'comment_count'        => $comments_count)
 307                          );
 308                      if ( is_wp_error( $ret_id ) )
 309                          return $ret_id;
 310                  }
 311                  else
 312                  {
 313                      $ret_id = wp_insert_post(array(
 314                          'post_date'            => $Posted,
 315                          'post_date_gmt'        => $post_date_gmt,
 316                          'post_author'        => $authorid,
 317                          'post_modified'        => $LastMod,
 318                          'post_modified_gmt' => $post_modified_gmt,
 319                          'post_title'        => $Title,
 320                          'post_content'        => $Body,
 321                          'post_excerpt'        => $Excerpt,
 322                          'post_status'        => $post_status,
 323                          'post_name'            => $url_title,
 324                          'comment_count'        => $comments_count)
 325                          );
 326                      if ( is_wp_error( $ret_id ) )
 327                          return $ret_id;
 328                  }
 329                  $txpposts2wpposts[$ID] = $ret_id;
 330  
 331                  // Make Post-to-Category associations
 332                  $cats = array();
 333                  $category1 = get_category_by_slug($Category1);
 334                  $category1 = $category1->term_id;
 335                  $category2 = get_category_by_slug($Category2);
 336                  $category2 = $category1->term_id;
 337                  if($cat1 = $category1) { $cats[1] = $cat1; }
 338                  if($cat2 = $category2) { $cats[2] = $cat2; }
 339  
 340                  if(!empty($cats)) { wp_set_post_categories($ret_id, $cats); }
 341              }
 342          }
 343          // Store ID translation for later use
 344          add_option('txpposts2wpposts',$txpposts2wpposts);
 345  
 346          echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> posts imported.'), $count).'<br /><br /></p>';
 347          return true;
 348      }
 349  
 350  	function comments2wp($comments='')
 351      {
 352          // General Housekeeping
 353          global $wpdb;
 354          $count = 0;
 355          $txpcm2wpcm = array();
 356          $postarr = get_option('txpposts2wpposts');
 357  
 358          // Magic Mojo
 359          if(is_array($comments))
 360          {
 361              echo '<p>'.__('Importing Comments...').'<br /><br /></p>';
 362              foreach($comments as $comment)
 363              {
 364                  $count++;
 365                  extract($comment);
 366  
 367                  // WordPressify Data
 368                  $comment_ID = ltrim($discussid, '0');
 369                  $comment_post_ID = $postarr[$parentid];
 370                  $comment_approved = (1 == $visible) ? 1 : 0;
 371                  $name = $wpdb->escape($name);
 372                  $email = $wpdb->escape($email);
 373                  $web = $wpdb->escape($web);
 374                  $message = $wpdb->escape($message);
 375  
 376                  if($cinfo = comment_exists($name, $posted))
 377                  {
 378                      // Update comments
 379                      $ret_id = wp_update_comment(array(
 380                          'comment_ID'            => $cinfo,
 381                          'comment_post_ID'        => $comment_post_ID,
 382                          'comment_author'        => $name,
 383                          'comment_author_email'    => $email,
 384                          'comment_author_url'    => $web,
 385                          'comment_date'            => $posted,
 386                          'comment_content'        => $message,
 387                          'comment_approved'        => $comment_approved)
 388                          );
 389                  }
 390                  else
 391                  {
 392                      // Insert comments
 393                      $ret_id = wp_insert_comment(array(
 394                          'comment_post_ID'        => $comment_post_ID,
 395                          'comment_author'        => $name,
 396                          'comment_author_email'    => $email,
 397                          'comment_author_url'    => $web,
 398                          'comment_author_IP'        => $ip,
 399                          'comment_date'            => $posted,
 400                          'comment_content'        => $message,
 401                          'comment_approved'        => $comment_approved)
 402                          );
 403                  }
 404                  $txpcm2wpcm[$comment_ID] = $ret_id;
 405              }
 406              // Store Comment ID translation for future use
 407              add_option('txpcm2wpcm', $txpcm2wpcm);
 408  
 409              // Associate newly formed categories with posts
 410              get_comment_count($ret_id);
 411  
 412  
 413              echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> comments imported.'), $count).'<br /><br /></p>';
 414              return true;
 415          }
 416          echo __('No Comments to Import!');
 417          return false;
 418      }
 419  
 420  	function links2wp($links='')
 421      {
 422          // General Housekeeping
 423          global $wpdb;
 424          $count = 0;
 425  
 426          // Deal with the links
 427          if(is_array($links))
 428          {
 429              echo '<p>'.__('Importing Links...').'<br /><br /></p>';
 430              foreach($links as $link)
 431              {
 432                  $count++;
 433                  extract($link);
 434  
 435                  // Make nice vars
 436                  $category = $wpdb->escape($category);
 437                  $linkname = $wpdb->escape($linkname);
 438                  $description = $wpdb->escape($description);
 439  
 440                  if($linfo = link_exists($linkname))
 441                  {
 442                      $ret_id = wp_insert_link(array(
 443                                  'link_id'            => $linfo,
 444                                  'link_url'            => $url,
 445                                  'link_name'            => $linkname,
 446                                  'link_category'        => $category,
 447                                  'link_description'    => $description,
 448                                  'link_updated'        => $date)
 449                                  );
 450                  }
 451                  else
 452                  {
 453                      $ret_id = wp_insert_link(array(
 454                                  'link_url'            => $url,
 455                                  'link_name'            => $linkname,
 456                                  'link_category'        => $category,
 457                                  'link_description'    => $description,
 458                                  'link_updated'        => $date)
 459                                  );
 460                  }
 461                  $txplinks2wplinks[$link_id] = $ret_id;
 462              }
 463              add_option('txplinks2wplinks',$txplinks2wplinks);
 464              echo '<p>';
 465              printf(__('Done! <strong>%s</strong> Links imported'), $count);
 466              echo '<br /><br /></p>';
 467              return true;
 468          }
 469          echo __('No Links to Import!');
 470          return false;
 471      }
 472  
 473  	function import_categories()
 474      {
 475          // Category Import
 476          $cats = $this->get_txp_cats();
 477          $this->cat2wp($cats);
 478          add_option('txp_cats', $cats);
 479  
 480  
 481  
 482          echo '<form action="admin.php?import=textpattern&amp;step=2" method="post">';
 483          wp_nonce_field('import-textpattern');
 484          printf('<input type="submit" name="submit" value="%s" />', attribute_escape(__('Import Users')));
 485          echo '</form>';
 486  
 487      }
 488  
 489  	function import_users()
 490      {
 491          // User Import
 492          $users = $this->get_txp_users();
 493          $this->users2wp($users);
 494  
 495          echo '<form action="admin.php?import=textpattern&amp;step=3" method="post">';
 496          wp_nonce_field('import-textpattern');
 497          printf('<input type="submit" name="submit" value="%s" />', attribute_escape(__('Import Posts')));
 498          echo '</form>';
 499      }
 500  
 501  	function import_posts()
 502      {
 503          // Post Import
 504          $posts = $this->get_txp_posts();
 505          $result = $this->posts2wp($posts);
 506          if ( is_wp_error( $result ) )
 507              return $result;
 508  
 509          echo '<form action="admin.php?import=textpattern&amp;step=4" method="post">';
 510          wp_nonce_field('import-textpattern');
 511          printf('<input type="submit" name="submit" value="%s" />', attribute_escape(__('Import Comments')));
 512          echo '</form>';
 513      }
 514  
 515  	function import_comments()
 516      {
 517          // Comment Import
 518          $comments = $this->get_txp_comments();
 519          $this->comments2wp($comments);
 520  
 521          echo '<form action="admin.php?import=textpattern&amp;step=5" method="post">';
 522          wp_nonce_field('import-textpattern');
 523          printf('<input type="submit" name="submit" value="%s" />', attribute_escape(__('Import Links')));
 524          echo '</form>';
 525      }
 526  
 527  	function import_links()
 528      {
 529          //Link Import
 530          $links = $this->get_txp_links();
 531          $this->links2wp($links);
 532          add_option('txp_links', $links);
 533  
 534          echo '<form action="admin.php?import=textpattern&amp;step=6" method="post">';
 535          wp_nonce_field('import-textpattern');
 536          printf('<input type="submit" name="submit" value="%s" />', attribute_escape(__('Finish')));
 537          echo '</form>';
 538      }
 539  
 540  	function