[ Index ]
 

Code source de b2evolution 2.1.0-beta

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/blogs/inc/tools/ -> wpimport.ctrl.php (source)

   1  <?php
   2  /* WordPress 2.3 to b2evolution 2.0 alpha converter

   3     Copyright (C) 2007 V.Harishankar.

   4  

   5     Please use this with care and at your own discretion. This script will try and import the following from

   6     WP to b2evolution:

   7      1. posts

   8      2. comments

   9      3. categories

  10      4. users

  11      

  12     This is alpha software and subject to change.

  13  */
  14  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  15  
  16      // set error reporting to full in order to get useful info when something fails

  17      $prevlevel = error_reporting (E_ALL);
  18  
  19      ?>
  20  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  21  <html>
  22  
  23  <head>
  24    <title>WP to b2evolution Converter</title>
  25    <meta name="GENERATOR" content="Quanta Plus">
  26    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  27  </head>
  28  <body bgcolor="#EEEEEE" text="#000000" link="#0000FF" alink="#FF0000" vlink="#7E0089">
  29      <h1>WordPress 2.3 to b2evolution importer</h1>
  30      [<a href="admin.php?ctrl=tools">Back to b2evolution</a>]
  31      <p><FONT SIZE="-2">Copyright &copy; 2007 <a href="http://hari.literaryforums.org/2007/10/04/wordpress-to-b2evolution-import-script">V.Harishankar</a>.  Released under the GNU GPL.</FONT></p>
  32      <?php
  33  
  34      // Check if user is logged in and is in group #1 (admins)

  35      if( !is_logged_in() || $current_User->Group->ID != 1 )
  36      {    // login failed
  37          debug_die( 'You must login with an administrator (group #1) account.' );
  38      }
  39  
  40      // The form has not yet been posted

  41      if ( ! isset ( $_POST['wp_db'] ) ) { ?>
  42          <P>Before running this importer, you must ensure that a proper <font color="#00CC00"><strong><em>NEW, EMPTY</em></strong></font> installation of b2evolution 2 exists! <strong><font color="#FF0000">IMPORTANT</font></strong>: This works <strong>only</strong> with WordPress 2.3 and above.</P>
  43          
  44          <p><strong><font color="#FF0000">Warning!!</strong> Your existing b2evolution posts, categories, comments and users (except admin) will be removed if you run this script. Make sure you have a backup before you proceed.</font></p>
  45          
  46          <FORM action="admin.php?ctrl=wpimport" enctype="multipart/form-data" method="POST" >
  47          <h2>DB Settings</h2>
  48          <table>
  49              <tbody>
  50              <tr>
  51              <td>WordPress database name</td>
  52              <td><INPUT type="text" name="wp_db"><br></td>
  53              </tr>
  54  
  55              <tr>
  56              <td>WordPress table prefix</td>
  57              <td><INPUT type="text" name="wp_prefix" value="wp_"><br></td>
  58              </tr>
  59              
  60              <tr>
  61              <td>b2evolution database</td>
  62              <td><INPUT type="text" name="b2evo_db"></td>
  63              </tr>
  64  
  65              <td>b2evolution table prefix</td>
  66              <td><INPUT type="text" name="b2evo_prefix" value="evo_"></td>
  67              </tr>
  68              <tr>
  69  
  70              <tr>
  71              <td>Database host</td>
  72              <td><INPUT type="text" name="db_host" value="localhost"></td>
  73              </tr>
  74  
  75              <tr>
  76              <td>Username</td>
  77              <td><INPUT type="text" name="db_user"></td>
  78              </tr>
  79  
  80              <tr>
  81              <td>Password</td>
  82              <td><INPUT type="password" name="db_pass"></td>
  83              </tr>
  84                      
  85              <tr>
  86              <td>Default locale for imported posts</td>
  87              <td><INPUT type="text" name="locale" value="en-US"></td>
  88              </td>
  89  
  90              <tr>
  91              <td></td>
  92              <td><INPUT type="submit" value="import"></td>
  93              </tr>
  94              </tbody>
  95              </table>
  96          
  97          </FORM>
  98      <?php // The form has been posted; do the conversion
  99          }
 100          else
 101          {
 102              // Try to obtain some serious time to do some serious processing (15 minutes)

 103              @set_time_limit( 900 );
 104  
 105              // required fields initialization

 106              $wp_db = $_POST['wp_db'];
 107              $evo_db = $_POST['b2evo_db'];
 108              $host = $_POST['db_host'];
 109              $user = $_POST['db_user'];
 110              $password = $_POST['db_pass'];
 111              $wp = $_POST['wp_prefix'];
 112              $b2 = $_POST['b2evo_prefix'];
 113              $locale = $_POST['locale'];
 114  
 115              // establish database connection

 116              $con = mysql_connect ($host, $user, $password);
 117              if (! $con ) 
 118                  die ( 'Error connecting to MySQL. Please check whether the server is running and the host, username and password fields are correct!' );
 119                  
 120              // First remove existing database items in categories, users, postcats, items__item, comments, blogusers

 121              $db = mysql_select_db ($evo_db, $con);
 122              if (! $db)
 123                  die ('b2evolution database name is incorrect. Please check your b2evolution installation.');
 124                  
 125              $query = 'DELETE FROM ' . $b2 . 'categories;';
 126              $flag = mysql_query ($query);
 127              if (! $flag )
 128                  die ('Existing categories deleting failed. Cannot proceed.');
 129              
 130              $query = 'DELETE FROM ' . $b2 . 'items__item;';
 131              $flag = mysql_query ($query);
 132              if (! $flag )
 133                  die ('Existing posts deletion failed. Cannot proceed.');
 134              
 135              $query = 'DELETE FROM ' . $b2 . 'postcats;';
 136              $flag = mysql_query ($query);
 137              if (! $flag )
 138                  die ('Existing post categories deletion failed. Cannot proceed.');
 139              
 140              $query = 'DELETE FROM ' . $b2 . 'comments;';
 141              $flag = mysql_query ($query);
 142              if (! $flag )
 143                  die ('Existing comments deletion failed. Cannot proceed.');
 144              
 145              $query = 'DELETE FROM ' . $b2 . 'users WHERE user_ID <> 1;';
 146              $flag = mysql_query ($query);
 147              if (! $flag )
 148                  die ('Existing users deletion failed. Cannot proceed.');
 149              
 150              $query = 'DELETE FROM ' . $b2 . 'blogusers WHERE bloguser_user_ID <> 1;';
 151              $flag = mysql_query ($query);
 152              if (! $flag )
 153                  die ('Existing user permissions deletion failed. Cannot proceed.');
 154              
 155              // Get the categories

 156              echo '<h2>Trying to import categories:</h2>';
 157              $cats = array ();
 158              
 159              // select the wordpress database

 160              $db = mysql_select_db ($wp_db, $con);
 161              if (! $db)
 162                  die ('WordPress database name is incorrect. Please check the name and try again.');
 163              
 164              // get the list of taxonomy terms. includes categories, link cats and tags as well

 165              $query = 'SELECT * FROM ' . $wp . 'terms;' ;
 166              $res = mysql_query ($query);
 167              if (! $res )
 168                  die ('Query failed. Please check your WordPress installation.');
 169              
 170              $i = 0;
 171              while ( $row = mysql_fetch_array ($res, MYSQL_ASSOC) )
 172              {
 173                  // in order to establish whether a term is a category or not

 174                  $query2 = 'SELECT * FROM ' . $wp . 'term_taxonomy WHERE term_id=' . $row['term_id'] . ';';
 175                  $res2 = mysql_query ($query2);
 176                  if (! $res2)
 177                      die ('Query 2 failed. Please check your WordPress installation.');
 178                  $row2 = mysql_fetch_array ($res2, MYSQL_ASSOC);
 179                  
 180                  // if it is a category only then import. ignore tags and link categories

 181                  if (strcmp ($row2['taxonomy'], 'category') == 0)
 182                  {
 183                      $cats[$i]['name'] = $row['name'];
 184                      $cats[$i]['slug'] = $row['slug'];
 185                      $cats[$i]['description'] = $row2['description'];
 186                      $cats[$i]['cat_id'] = $row2['term_taxonomy_id'];
 187                      echo 'Reading: ' . $cats[$i]['name'] . '<br>';
 188                      $i ++;
 189                  }
 190                  mysql_free_result ($res2);
 191              }
 192              mysql_free_result ($res);
 193              
 194              // select the evolution database

 195              $db = mysql_select_db ($evo_db, $con);
 196              if (! $db)
 197                  die ('b2evolution database name is incorrect. Please check the name and try again.');
 198              foreach ($cats as $category)
 199              {
 200                  // insert each category into the evolution database

 201                  $query = 'INSERT INTO ' . $b2 . 'categories (cat_ID, cat_name, cat_urlname, cat_blog_ID, cat_description) VALUES ("' . $category['cat_id'] . '", "' . $category['name'] . '", "' . $category['slug'] . '", "1", "' . $category['description'] . '");';
 202                  
 203                  $flag = mysql_query ($query);
 204                  
 205                  if (! $flag )
 206                      die ('Category importing failed. Please check your b2evolution installation.');
 207              }
 208              echo '<font color="#00CC00">Categories inserted successfully!</font><br>';
 209              
 210              // Now import the posts into b2evolution

 211              echo '<h2>Trying to import posts</h2>';
 212              
 213              $posts = array ();
 214              $db = mysql_select_db ($wp_db, $con);
 215              if (! $db)
 216                  die ('WordPress database name is incorrect. Please check the name and try again.');
 217              
 218              $query = 'SELECT * FROM ' . $wp . 'posts WHERE post_type="post" OR post_type="page";' ;
 219              $res = mysql_query ($query);
 220              if (! $res )
 221                  die ('Query failed. Please check your WordPress installation.');
 222                  
 223              $i = 0;
 224              while ( $row = mysql_fetch_array ($res, MYSQL_ASSOC) )
 225              {
 226                  $posts[$i]['post_id'] = $row['ID'];
 227                  $posts[$i]['slug'] = $row['post_name'];
 228                  $posts[$i]['title'] = $row['post_title'];
 229                  $posts[$i]['status'] = $row['post_status'];
 230                  $posts[$i]['create_date'] = $row['post_date'];
 231                  $posts[$i]['modified_date'] = $row['post_modified'];
 232                  $posts[$i]['excerpt'] = $row['post_excerpt'];
 233                  $posts[$i]['comment_status'] = $row['comment_status'];
 234                  $posts[$i]['content'] = $row['post_content'];
 235                  $posts[$i]['author'] = $row['post_author'];
 236                  $posts[$i]['type'] = 1;
 237  
 238                  if (strcmp ($row['post_type'], 'page') == 0)
 239                      $posts[$i]['type'] = 1000;
 240  
 241                  echo 'Reading: ' . $posts[$i]['title'] . '<br>';
 242                  
 243                  // Now to get the cats for each post. This is a bit tricky since we have to avoid 'tags' in the result

 244                  $j = 0;
 245                  $posts[$i]['cats'] = array ();
 246                  
 247                  $query2 = 'SELECT * FROM ' . $wp . 'term_relationships WHERE object_id=' . $row['ID'] . '; ';
 248                  $res2 = mysql_query ($query2);
 249                  if (! $res2)
 250                      die ('Query 2 failed. Please check your WordPress installation.');
 251                  
 252                  while ($row2 = mysql_fetch_array ($res2, MYSQL_ASSOC) )
 253                  {
 254                      $query3 = 'SELECT * FROM ' . $wp . 'term_taxonomy WHERE term_taxonomy_id=' . $row2['term_taxonomy_id'] . ';';
 255                      $res3 = mysql_query ($query3);
 256                      if (! $res3 )
 257                          die ('Query 3 failed. Please check your WordPress installation.');
 258                      $row3 = mysql_fetch_array ($res3, MYSQL_ASSOC);
 259                      if (strcmp ($row3['taxonomy'], 'category') == 0)
 260                      {
 261                          $posts[$i]['cats'][$j] = $row2['term_taxonomy_id'];
 262                          $j ++;
 263                      }
 264                      mysql_free_result ($res3);
 265                  }
 266                  mysql_free_result ($res2);
 267                  $i ++;
 268              }
 269              mysql_free_result ($res);
 270              
 271              // select the evolution database

 272              $db = mysql_select_db ($evo_db, $con);
 273              if (! $db)
 274                  die ('b2evolution database name is incorrect. Please check the name and try again.');
 275              
 276              foreach ($posts as $post)
 277              {
 278                  // insert the post categories first into the postcats table

 279                  foreach ($post['cats'] as $cat)
 280                  {
 281                      // query to insert each category for the particular post

 282                      $query = 'INSERT INTO ' . $b2 . 'postcats (postcat_post_ID, postcat_cat_ID) VALUES ("' . $post['post_id']. '", "' . $cat . '");';
 283                      $flag = mysql_query ($query);
 284                      if (! $flag )
 285                          die ('Post categories insertion failed. Please check your b2evolution installation.');
 286                  
 287                  }
 288                  
 289                  // set the post rendering options. TODO: this could probably be an option for the user before importing

 290                  $postrenderers = 'b2evSmil.b2evALnk.b2WPAutP';    
 291                  
 292                  // query to insert the posts into the b2evolution table

 293                  $query = 'INSERT INTO ' . $b2 . 'items__item (post_ptyp_ID, post_ID, post_main_cat_ID, post_creator_user_ID, post_lastedit_user_ID, post_datestart, post_datecreated, post_datemodified, post_status, post_locale, post_content, post_excerpt, post_title, post_urltitle, post_comment_status, post_renderers) VALUES ("'. $post['type'] . '", "' . $post['post_id'] . '", "' . $post['cats'][0] . '", "' .  $post['author'] . '", "' . $post['author'] . '", "' . $post['create_date'] . '", "' . $post['create_date'] . '", "' . $post['modified_date'] . '", "' . 'published' . '", "' . $locale . '", "' . mysql_real_escape_string($post['content']) . '", "' . mysql_real_escape_string($post['excerpt']) . '", "' . mysql_real_escape_string($post['title']) . '", "' . $post['slug'] . '", "' . $post['comment_status'] . '", "' . $postrenderers . '");';
 294                  
 295                  $flag = mysql_query ($query);
 296                  if (! $flag )
 297                      die ('Post importing failed. Please check your b2evolution installation.');
 298                  
 299  
 300              }
 301              echo '<font color="#00CC00">Posts and post categories imported successfully!</font>';
 302  
 303              // Now import the comments

 304              echo '<h2>Trying to import comments</h2>';
 305              $comments = array ();
 306  
 307              // select the wordpress database

 308              $db = mysql_select_db ($wp_db);
 309              if (! $db)
 310                  die ('WordPress database name is incorrect. Please check the name and try again.');
 311  
 312              // discard the spam comments. select only comments where status is either 'in moderation' or 'approved'

 313              $query = 'SELECT * FROM ' . $wp . 'comments WHERE comment_approved="0" OR comment_approved="1" ORDER BY comment_date ASC;';
 314              
 315              $res = mysql_query ($query);
 316              if (! $res )
 317                  die ('Query failed. Please check your WordPress installation.');
 318              
 319              $i = 0;
 320              while ($row = mysql_fetch_array ($res, MYSQL_ASSOC) )
 321              {
 322                  // set the values from comments table

 323                  $comments[$i]['comment_id'] = $row['comment_ID'];
 324                  $comments[$i]['post_id'] = $row['comment_post_ID'];
 325                  $comments[$i]['author'] = $row['comment_author'];
 326                  $comments[$i]['email'] = $row['comment_author_email'];
 327                  $comments[$i]['url'] = $row['comment_author_url'];
 328                  $comments[$i]['ip'] = $row['comment_author_IP'];
 329                  $comments[$i]['date'] = $row['comment_date'];
 330                  $comments[$i]['content'] = $row['comment_content'];
 331                  $comments[$i]['author_id'] = $row['user_id'];
 332                  
 333                  // set default comment status to published

 334                  $comments[$i]['status'] = 'published';
 335                  // if the comment isn't approved set it to draft

 336                  if ($row['comment_approved'] == 0)
 337                      $comments[$i]['status'] = 'draft';
 338                      
 339                  // default comment type is 'comment

 340                  $comments[$i]['type'] = 'comment';
 341                  // if it is a pingback or trackback change the type accordingly

 342                  if ($row['comment_type'] == 'pingback' || $row['comment_type'] == 'trackback')
 343                      $comments[$i]['type'] = 'pingback';
 344  
 345                  $i ++;
 346              }
 347              // free the query result set

 348              mysql_free_result ($res);
 349              
 350              // select the evolution db

 351              $db = mysql_select_db ($evo_db, $con);
 352              if (! $db)
 353                  die ('b2evolution database name is incorrect. Please check the name and try again.');
 354                  
 355              foreach ($comments as $comment)
 356              {
 357                  // escape the string and replace UNIX newlines to line breaks in order to 

 358                  // render properly in b2evolution

 359                  $ccontent = mysql_real_escape_string ($comment['content']);
 360                  $ccontent = str_replace ('\r\n', '<br />', $ccontent);
 361                  
 362                  // query to insert the comments into the b2evolution table

 363                  $query = 'INSERT INTO ' . $b2 . 'comments (comment_ID, comment_post_ID, comment_type, comment_status, comment_author_ID, comment_author, comment_author_email, comment_author_url, comment_author_IP, comment_date, comment_content, comment_allow_msgform) VALUES ("'. $comment['comment_id'] . '", "' . $comment['post_id'] . '", "' . $comment['type'] . '", "' . $comment['status'] . '", "' . $comment['author_id'] . '", "' . $comment['author'] . '", "' . $comment['email'] . '", "' . $comment['url'] . '", "' . $comment['ip'] . '", "' . $comment['date'] . '", "' . $ccontent . '", "1");';
 364                  
 365                  $flag = mysql_query ($query);
 366                  if (! $flag)
 367                      die ('Comment importing failed. Please check your b2evolution installation.');
 368              
 369              }
 370              echo '<font color="#00CC00">Comments imported successfully!</font>';
 371              
 372              // Now to import users. Note: all users other than admin will be imported and then they will be set to the default level

 373              // of 0

 374              echo '<h2>Trying to import all users (except admin)</h2>';
 375              $users = array ();
 376              
 377              // select the wordpress database

 378              $db = mysql_select_db ($wp_db, $con);
 379              if (! $db) 
 380                  die ('WordPress database name is incorrect. Please check the name and try again.');
 381              
 382              // select all users except the admin user

 383              $query = 'SELECT * FROM '. $wp . 'users WHERE user_login <> "admin";';
 384              $res = mysql_query ($query);
 385              if (! $res )
 386                  die ('Query failed. Please check your WordPress installation.');
 387              
 388              $i = 0;
 389              while ( $row = mysql_fetch_array ($res, MYSQL_ASSOC) )
 390              {
 391                  // set all the values from the user table

 392                  $users[$i]['id'] = $row['ID'];
 393                  $users[$i]['login'] = $row['user_login'];
 394                  $users[$i]['password'] = $row['user_pass'];
 395                  $users[$i]['nickname'] = $row['user_nicename'];
 396                  $users[$i]['email'] = $row['user_email'];
 397                  $users[$i]['url'] = $row['user_url'];
 398                  $users[$i]['date'] = $row['user_registered'];
 399                  $users[$i]['firstname'] = $row['display_name'];
 400                  echo 'Reading: ' . $users[$i]['login'] . '<br>';
 401                  $i ++;
 402              }
 403              mysql_free_result ($res);
 404              
 405              // select the evolution db

 406              $db = mysql_select_db ($evo_db, $con);
 407              if (! $db )
 408                  die ('b2evolution database name is incorrect. Please check the name and try again.');
 409              
 410              foreach ($users as $a_user)
 411              {
 412                  // Import the user

 413                  $query = 'INSERT INTO ' . $b2 . 'users (user_ID, user_login, user_pass, user_firstname, user_nickname, user_email, user_url, dateYMDhour, user_validated, user_grp_ID) VALUES ("' . $a_user['id'] . '", "' . $a_user['login'] . '", "' . $a_user['password'] . '", "' . $a_user['firstname'] . '", "' . $a_user['nickname'] . '", "' . $a_user['email'] . '", "' . $a_user['url'] . '", "' .$a_user['date'] . '", "1", "4");';
 414                  
 415                  $flag = mysql_query ($query);
 416                  if (! $flag)
 417                      die ('User importing failed. Please check your b2evolution installation.');
 418                      
 419                  // Import the permissions for blog for the user

 420                  $query = 'INSERT INTO ' . $b2 . 'blogusers (bloguser_blog_ID, bloguser_user_ID, bloguser_ismember) VALUES ("1", "' . $a_user['id'] . '", "1");';
 421                  
 422                  $flag = mysql_query ($query);
 423                  if (! $flag)
 424                      die ('User (permissions) importing failed. Please check you b2evolution installation.');
 425              }
 426              echo '<font color="#00CC00">Users imported successfully! <strong>NOTE:</strong> all users are set to basic users level by default. You should probably reconfigure user permissions in the admin control panel if you want to give them higher privileges.</font>';
 427              
 428              // All done

 429              echo '<br><br>';
 430              echo '<strong><font color="#00CC00">Everything imported correctly. Try out your new b2evolution blog!</font></strong>';
 431              
 432              // close the connection to the MySQL server

 433              mysql_close ($con);
 434       } ?>
 435       <?php    // reset the PHP error reporting to the previous level
 436            error_reporting ($prevlevel); ?>
 437  </body>
 438  </html>
 439  


Généré le : Thu Nov 29 23:58:50 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics