[ Index ]

WordPress Source Cross Reference

title

Body

[close]

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

   1  <?php
   2  /*
   3   * Dotclear import plugin
   4   * by Thomas Quinot - http://thomas.quinot.org/
   5   */
   6  
   7  /**
   8      Add These Functions to make our lives easier
   9  **/
  10  if(!function_exists('get_catbynicename'))
  11  {
  12  	function get_catbynicename($category_nicename) 
  13      {
  14      global $wpdb;
  15  
  16      $cat_id -= 0;     // force numeric
  17      $name = $wpdb->get_var('SELECT cat_ID FROM '.$wpdb->categories.' WHERE category_nicename="'.$category_nicename.'"');
  18  
  19      return $name;
  20      }
  21  }
  22  
  23  if(!function_exists('get_comment_count'))
  24  {
  25  	function get_comment_count($post_ID)
  26      {
  27          global $wpdb;
  28          return $wpdb->get_var('SELECT count(*) FROM '.$wpdb->comments.' WHERE comment_post_ID = '.$post_ID);
  29      }
  30  }
  31  
  32  if(!function_exists('link_cat_exists'))
  33  {
  34  	function link_cat_exists($catname)
  35      {
  36          global $wpdb;
  37          return $wpdb->get_var('SELECT cat_id FROM '.$wpdb->linkcategories.' WHERE cat_name = "'.$wpdb->escape($catname).'"');
  38      }
  39  }
  40  
  41  if(!function_exists('link_exists'))
  42  {
  43  	function link_exists($linkname)
  44      {
  45          global $wpdb;
  46          return $wpdb->get_var('SELECT link_id FROM '.$wpdb->links.' WHERE link_name = "'.$linkname.'"');
  47      }
  48  }
  49  
  50  /*
  51   Identify UTF-8 text
  52   Taken from http://www.php.net/manual/fr/function.mb-detect-encoding.php#50087
  53  */
  54  //
  55  //    utf8 encoding validation developed based on Wikipedia entry at:
  56  //    http://en.wikipedia.org/wiki/UTF-8
  57  //
  58  //    Implemented as a recursive descent parser based on a simple state machine
  59  //    copyright 2005 Maarten Meijer
  60  //
  61  //    This cries out for a C-implementation to be included in PHP core
  62  //
  63     function valid_1byte($char) {
  64         if(!is_int($char)) return false;
  65         return ($char & 0x80) == 0x00;
  66     }
  67    
  68     function valid_2byte($char) {
  69         if(!is_int($char)) return false;
  70         return ($char & 0xE0) == 0xC0;
  71     }
  72  
  73     function valid_3byte($char) {
  74         if(!is_int($char)) return false;
  75         return ($char & 0xF0) == 0xE0;
  76     }
  77  
  78     function valid_4byte($char) {
  79         if(!is_int($char)) return false;
  80         return ($char & 0xF8) == 0xF0;
  81     }
  82    
  83     function valid_nextbyte($char) {
  84         if(!is_int($char)) return false;
  85         return ($char & 0xC0) == 0x80;
  86     }
  87    
  88     function valid_utf8($string) {
  89         $len = strlen($string);
  90         $i = 0;   
  91         while( $i < $len ) {
  92             $char = ord(substr($string, $i++, 1));
  93             if(valid_1byte($char)) {    // continue
  94                 continue;
  95             } else if(valid_2byte($char)) { // check 1 byte
  96                 if(!valid_nextbyte(ord(substr($string, $i++, 1))))
  97                     return false;
  98             } else if(valid_3byte($char)) { // check 2 bytes
  99                 if(!valid_nextbyte(ord(substr($string, $i++, 1))))
 100                     return false;
 101                 if(!valid_nextbyte(ord(substr($string, $i++, 1))))
 102                     return false;
 103             } else if(valid_4byte($char)) { // check 3 bytes
 104                 if(!valid_nextbyte(ord(substr($string, $i++, 1))))
 105                     return false;
 106                 if(!valid_nextbyte(ord(substr($string, $i++, 1))))
 107                     return false;
 108                 if(!valid_nextbyte(ord(substr($string, $i++, 1))))
 109                     return false;
 110             } // goto next char
 111         }
 112         return true; // done
 113     }
 114  
 115  function csc ($s) {
 116      if (valid_utf8 ($s)) {
 117          return $s;
 118      } else {
 119          return iconv(get_option ("dccharset"),"UTF-8",$s);
 120      }
 121  }
 122  
 123  function textconv ($s) {
 124      return csc (preg_replace ('|(?<!<br />)\s*\n|', ' ', $s));
 125  }
 126  
 127  /**
 128      The Main Importer Class
 129  **/
 130  class Dotclear_Import {
 131  
 132  	function header() 
 133      {
 134          echo '<div class="wrap">';
 135          echo '<h2>'.__('Import Dotclear').'</h2>';
 136          echo '<p>'.__('Steps may take a few minutes depending on the size of your database. Please be patient.').'</p>';
 137      }
 138  
 139  	function footer() 
 140      {
 141          echo '</div>';
 142      }
 143  
 144  	function greet() 
 145      {
 146          echo '<p>'.__('Howdy! This importer allows you to extract posts from a Dotclear database into your blog.  Mileage may vary.').'</p>';
 147          echo '<p>'.__('Your Dotclear Configuration settings are as follows:').'</p>';
 148          echo '<form action="admin.php?import=dotclear&amp;step=1" method="post">';
 149          $this->db_form();
 150          echo '<input type="submit" name="submit" value="'.__('Import Categories').'" />';
 151          echo '</form>';
 152      }
 153  
 154  	function get_dc_cats() 
 155      {
 156          global $wpdb;
 157          // General Housekeeping
 158          $dcdb = new wpdb(get_option('dcuser'), get_option('dcpass'), get_option('dcname'), get_option('dchost'));
 159          set_magic_quotes_runtime(0);
 160          $dbprefix = get_option('dcdbprefix');
 161  
 162          // Get Categories
 163          return $dcdb->get_results('SELECT * FROM '.$dbprefix.'categorie', ARRAY_A);
 164      }
 165  
 166  	function get_dc_users()
 167      {
 168          global $wpdb;
 169          // General Housekeeping
 170          $dcdb = new wpdb(get_option('dcuser'), get_option('dcpass'), get_option('dcname'), get_option('dchost'));
 171          set_magic_quotes_runtime(0);
 172          $dbprefix = get_option('dcdbprefix');
 173  
 174          // Get Users
 175  
 176          return $dcdb->get_results('SELECT * FROM '.$dbprefix.'user', ARRAY_A);
 177      }
 178  
 179  	function get_dc_posts()
 180      {
 181          // General Housekeeping
 182          $dcdb = new wpdb(get_option('dcuser'), get_option('dcpass'), get_option('dcname'), get_option('dchost'));
 183          set_magic_quotes_runtime(0);
 184          $dbprefix = get_option('dcdbprefix');
 185  
 186          // Get Posts
 187          return $dcdb->get_results('SELECT '.$dbprefix.'post.*, '.$dbprefix.'categorie.cat_libelle_url AS post_cat_name
 188                          FROM '.$dbprefix.'post INNER JOIN '.$dbprefix.'categorie
 189                            ON '.$dbprefix.'post.cat_id = '.$dbprefix.'categorie.cat_id', ARRAY_A);
 190      }
 191  
 192  	function get_dc_comments()
 193      {
 194          global $wpdb;
 195          // General Housekeeping
 196          $dcdb = new wpdb(get_option('dcuser'), get_option('dcpass'), get_option('dcname'), get_option('dchost'));
 197          set_magic_quotes_runtime(0);
 198          $dbprefix = get_option('dcdbprefix');
 199  
 200          // Get Comments
 201          return $dcdb->get_results('SELECT * FROM '.$dbprefix.'comment', ARRAY_A);
 202      }
 203  
 204  	function get_dc_links()
 205      {
 206          //General Housekeeping
 207          $dcdb = new wpdb(get_option('dcuser'), get_option('dcpass'), get_option('dcname'), get_option('dchost'));
 208          set_magic_quotes_runtime(0);
 209          $dbprefix = get_option('dcdbprefix');
 210  
 211          return $dcdb->get_results('SELECT * FROM '.$dbprefix.'link ORDER BY position', ARRAY_A);
 212      }
 213  
 214  	function cat2wp($categories='') 
 215      {
 216          // General Housekeeping
 217          global $wpdb;
 218          $count = 0;
 219          $dccat2wpcat = array();
 220          // Do the Magic
 221          if(is_array($categories))
 222          {
 223              echo '<p>'.__('Importing Categories...').'<br /><br /></p>';
 224              foreach ($categories as $category) 
 225              {
 226                  $count++;
 227                  extract($category);
 228  
 229                  // Make Nice Variables
 230                  $name = $wpdb->escape($cat_libelle_url);
 231                  $title = $wpdb->escape(csc ($cat_libelle));
 232                  $desc = $wpdb->escape(csc ($cat_desc));
 233  
 234                  if($cinfo = category_exists($name))
 235                  {
 236                      $ret_id = wp_insert_category(array('cat_ID' => $cinfo, 'category_nicename' => $name, 'cat_name' => $title, 'category_description' => $desc));
 237                  }
 238                  else
 239                  {
 240                      $ret_id = wp_insert_category(array('category_nicename' => $name, 'cat_name' => $title, 'category_description' => $desc));
 241                  }
 242                  $dccat2wpcat[$id] = $ret_id;
 243              }
 244  
 245              // Store category translation for future use
 246              add_option('dccat2wpcat',$dccat2wpcat);
 247              echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> categories imported.'), $count).'<br /><br /></p>';
 248              return true;
 249          }
 250          echo __('No Categories to Import!');
 251          return false;
 252      }
 253  
 254  	function users2wp($users='')
 255      {
 256          // General Housekeeping
 257          global $wpdb;
 258          $count = 0;
 259          $dcid2wpid = array();
 260  
 261          // Midnight Mojo
 262          if(is_array($users))
 263          {
 264              echo '<p>'.__('Importing Users...').'<br /><br /></p>';
 265              foreach($users as $user)
 266              {
 267                  $count++;
 268                  extract($user);
 269  
 270                  // Make Nice Variables
 271                  $name = $wpdb->escape(csc ($name));
 272                  $RealName = $wpdb->escape(csc ($user_pseudo));
 273  
 274                  if($uinfo = get_userdatabylogin($name))
 275                  {
 276  
 277                      $ret_id = wp_insert_user(array(
 278                                  'ID'        => $uinfo->ID,
 279                                  'user_login'    => $user_id,
 280                                  'user_nicename'    => $Realname,
 281                                  'user_email'    => $user_email,
 282                                  'user_url'    => 'http://',
 283                                  'display_name'    => $Realname)
 284                                  );
 285                  }
 286                  else 
 287                  {
 288                      $ret_id = wp_insert_user(array(
 289                                  'user_login'    => $user_id,
 290                                  'user_nicename'    => csc ($user_pseudo),
 291                                  'user_email'    => $user_email,
 292                                  'user_url'    => 'http://',
 293                                  'display_name'    => $Realname)
 294                                  );
 295                  }
 296                  $dcid2wpid[$user_id] = $ret_id;
 297  
 298                  // Set Dotclear-to-WordPress permissions translation
 299  
 300                  // Update Usermeta Data
 301                  $user = new WP_User($ret_id);
 302                  $wp_perms = $user_level + 1;
 303                  if(10 == $wp_perms) { $user->set_role('administrator'); }
 304                  else if(9  == $wp_perms) { $user->set_role('editor'); }
 305                  else if(5  <= $wp_perms) { $user->set_role('editor'); }
 306                  else if(4  <= $wp_perms) { $user->set_role('author'); }
 307                  else if(3  <= $wp_perms) { $user->set_role('contributor'); }
 308                  else if(2  <= $wp_perms) { $user->set_role('contributor'); }
 309                  else                     { $user->set_role('subscriber'); }
 310  
 311                  update_usermeta( $ret_id, 'wp_user_level', $wp_perms);
 312                  update_usermeta( $ret_id, 'rich_editing', 'false');
 313                  update_usermeta( $ret_id, 'first_name', csc ($user_prenom));
 314                  update_usermeta( $ret_id, 'last_name', csc ($user_nom));
 315              }// End foreach($users as $user)
 316  
 317              // Store id translation array for future use
 318              add_option('dcid2wpid',$dcid2wpid);
 319  
 320  
 321              echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> users imported.'), $count).'<br /><br /></p>';
 322              return true;
 323          }// End if(is_array($users)
 324  
 325          echo __('No Users to Import!');
 326          return false;
 327  
 328      }// End function user2wp()
 329  
 330  	function posts2wp($posts='')
 331      {
 332          // General Housekeeping
 333          global $wpdb;
 334          $count = 0;
 335          $dcposts2wpposts = array();
 336          $cats = array();
 337  
 338          // Do the Magic
 339          if(is_array($posts))
 340          {
 341              echo '<p>'.__('Importing Posts...').'<br /><br /></p>';
 342              foreach($posts as $post)
 343              {
 344                  $count++;
 345                  extract($post);
 346  
 347                  // Set Dotclear-to-WordPress status translation
 348                  $stattrans = array(0 => 'draft', 1 => 'publish');
 349                  $comment_status_map = array (0 => 'closed', 1 => 'open');
 350  
 351                  //Can we do this more efficiently?
 352                  $uinfo = ( get_userdatabylogin( $user_id ) ) ? get_userdatabylogin( $user_id ) : 1;
 353                  $authorid = ( is_object( $uinfo ) ) ? $uinfo->ID : $uinfo ;
 354  
 355                  $Title = $wpdb->escape(csc ($post_titre));
 356                  $post_content = textconv ($post_content);
 357                  $post_excerpt = "";
 358                  if ($post_chapo != "") {
 359                      $post_excerpt = textconv ($post_chapo);
 360                      $post_content = $post_excerpt ."\n<!--more-->\n".$post_content;
 361                  }
 362                  $post_excerpt = $wpdb->escape ($post_excerpt);
 363                  $post_content = $wpdb->escape ($post_content);
 364                  $post_status = $stattrans[$post_pub];
 365  
 366                  // Import Post data into WordPress
 367  
 368                  if($pinfo = post_exists($Title,$post_content))
 369                  {
 370                      $ret_id = wp_insert_post(array(
 371                              'ID'            => $pinfo,
 372                              'post_author'        => $authorid,
 373                              'post_date'        => $post_dt,
 374                              'post_date_gmt'        => $post_dt,
 375                              'post_modified'        => $post_upddt,
 376                              'post_modified_gmt'    => $post_upddt,
 377                              'post_title'        => $Title,
 378                              'post_content'        => $post_content,
 379                              'post_excerpt'        => $post_excerpt,
 380                              'post_status'        => $post_status,
 381                              'post_name'        => $post_titre_url,
 382                              'comment_status'    => $comment_status_map[$post_open_comment],
 383                              'ping_status'        => $comment_status_map[$post_open_tb],
 384                              'comment_count'        => $post_nb_comment + $post_nb_trackback)
 385                              );
 386                  }
 387                  else 
 388                  {
 389                      $ret_id = wp_insert_post(array(
 390                              'post_author'        => $authorid,
 391                              'post_date'        => $post_dt,
 392                              'post_date_gmt'        => $post_dt,
 393                              'post_modified'        => $post_modified_gmt,
 394                              'post_modified_gmt'    => $post_modified_gmt,
 395                              'post_title'        => $Title,
 396                              'post_content'        => $post_content,
 397                              'post_excerpt'        => $post_excerpt,
 398                              'post_status'        => $post_status,
 399                              'post_name'        => $post_titre_url,
 400                              'comment_status'    => $comment_status_map[$post_open_comment],
 401                              'ping_status'        => $comment_status_map[$post_open_tb],
 402                              'comment_count'        => $post_nb_comment + $post_nb_trackback)
 403                              );
 404                  }
 405                  $dcposts2wpposts[$post_id] = $ret_id;
 406  
 407                  // Make Post-to-Category associations
 408                  $cats = array();
 409                  if($cat1 = get_catbynicename($post_cat_name)) { $cats[1] = $cat1; }
 410  
 411                  if(!empty($cats)) { wp_set_post_cats('', $ret_id, $cats); }
 412              }
 413          }
 414          // Store ID translation for later use
 415          add_option('dcposts2wpposts',$dcposts2wpposts);
 416  
 417          echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> posts imported.'), $count).'<br /><br /></p>';
 418          return true;
 419      }
 420  
 421  	function comments2wp($comments='')
 422      {
 423          // General Housekeeping
 424          global $wpdb;
 425          $count = 0;
 426          $dccm2wpcm = array();
 427          $postarr = get_option('dcposts2wpposts');
 428  
 429          // Magic Mojo
 430          if(is_array($comments))
 431          {
 432              echo '<p>'.__('Importing Comments...').'<br /><br /></p>';
 433              foreach($comments as $comment)
 434              {
 435                  $count++;
 436                  extract($comment);
 437  
 438                  // WordPressify Data
 439                  $comment_ID = ltrim($comment_id, '0');
 440                  $comment_post_ID = $postarr[$post_id];
 441                  $comment_approved = "$comment_pub";
 442                  $name = $wpdb->escape(csc ($comment_auteur));
 443                  $email = $wpdb->escape($comment_email);
 444                  $web = "http://".$wpdb->escape($comment_site);
 445                  $message = $wpdb->escape(textconv ($comment_content));
 446  
 447                  if($cinfo = comment_exists($name, $comment_dt))
 448                  {
 449                      // Update comments
 450                      $ret_id = wp_update_comment(array(
 451                              'comment_ID'        => $cinfo,
 452                              'comment_post_ID'    => $comment_post_ID,
 453                              'comment_author'    => $name,
 454                              'comment_author_email'    => $email,
 455                              'comment_author_url'    => $web,
 456                              'comment_author_IP'    => $comment_ip,
 457                              'comment_date'        => $comment_dt,
 458                              'comment_date_gmt'    => $comment_dt,
 459                              'comment_content'    => $message,
 460                              'comment_approved'    => $comment_approved)
 461                              );
 462                  }
 463                  else 
 464                  {
 465                      // Insert comments
 466                      $ret_id = wp_insert_comment(array(
 467                              'comment_post_ID'    => $comment_post_ID,
 468                              'comment_author'    => $name,
 469                              'comment_author_email'    => $email,
 470                              'comment_author_url'    => $web,
 471                              'comment_author_IP'    => $comment_ip,
 472                              'comment_date'        => $comment_dt,
 473                              'comment_date_gmt'    => $comment_dt,
 474                              'comment_content'    => $message,
 475                              'comment_approved'    => $comment_approved)
 476                              );
 477                  }
 478                  $dccm2wpcm[$comment_ID] = $ret_id;
 479              }
 480              // Store Comment ID translation for future use
 481              add_option('dccm2wpcm', $dccm2wpcm);
 482  
 483              // Associate newly formed categories with posts
 484              get_comment_count($ret_id);
 485  
 486  
 487              echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> comments imported.'), $count).'<br /><br /></p>';
 488              return true;
 489          }
 490          echo __('No Comments to Import!');
 491          return false;
 492      }
 493  
 494  	function links2wp($links='')
 495      {
 496          // General Housekeeping
 497          global $wpdb;
 498          $count = 0;
 499  
 500</