Migrar un blog desde B2evolution a WordPress

@cmdearcos
Grupo TIECs

 

Los que me seguís habitualmente habréis observado que ha cambiado el aspecto del blog. Pero este cambio no solo ha sido un cambio de aspecto (de plantilla) sino que ha sido un cambio mucho más profundo. He migrado el blog de b2evolution a wordpress.

Muchas son las razones para realizar este cambio, como la cantidad de plugins y plantillas existentes para wordpress. Pero la razón principal es que en las últimas semanas estoy trabajando con el mejor equipo de CMS que existe en España.

Para poder migrar de un sistema a otro lo más importante es la migración de la base de datos donde se guardan los post. Para ello he utilizado, después de adaptarlo, el script migrateb2towp creado por Justin Mazzi y modificado posteriormente por TuMahler y Alejandro Espinosa.

Lo primero que he hecho es hacer un backup de todo el site. Después he borrado (renombrado en una primera fase y una comprobado que todo estaba bien eliminando) los direcctorios y ficheros de b2evolution que estaban en el raiz del site excepto el directorio “media” que es donde estaban las fotografías (y ficheros) que utilizaba en mis post.

Posteriormente he instalado el WordPress en mi site (www.cmdearcos.es) con una nueva base de datos y he modificado el script anterior para que pase los datos de la antigua base de datos a la nueva. Para que funciones correctamente el script debes estar en el mismo directorio donde se encuentra el fichero wp-config.php.

Una vez ejecutado el script ya se puede entrar en wordpress para configurar el estilo del blog.

Aquí os dejo el script. Recordad que debéis introducir el nombre de vuestra base de datos de b2evolution y la password para que se pueda hacer la migración,

  1. <?php
  2.  
  3. //
  4. //  migrateb2towp.php
  5. //  b2evolution -> wordpress migrator
  6. //
  7. //  Created by Justin Mazzi (http://r00tshell.com)
  8. //  This script was originally found on (http://www.nocblog.com/software/2006/01/23/migrator-b2evolution-wordpress/)
  9. //  Modified by TuMahler (http://www.tumahler.com) 4/3/2006
  10. //  Modified by Alejandro Espinosa 03/01/2014
  11. //  Modified by Carlos Martín 20/07/2014
  12. //
  13. //  4/3/2006 (TuMahler):
  14. //  Updated to allow migration from B2evolution version 0.9.0.12 to Wordpress 2.02
  15. //  Also added transfer of comments and fixed a bug in the creation of posts in multiple categories
  16. //
  17. //  03/01/2014 (Alejandro Espinosa):
  18. //  Updated to work with new db structures of b2evo (version 4.1.2-stable) and WP (version 3.8, db_version 26691)
  19. //  Also added slugify function (not shure if necesary).
  20. //  Also added advise to install redirection plugin in WP in order to take care of old links
  21. //  in Google and to preserve 'google reputation' of yor posts.
  22. //  Also added advise to install WP in a subdirectory and directions on how to make it visible
  23. //  in your site root.
  24. //
  25. //  *** LIMITATIONS ***
  26. //
  27. //  This script migrates your b2evo forum to WP assuming you are in a 'one user' scenario.
  28. //  So, all posts will be imported in WP belonging to the admin user.
  29. //
  30. //  *** WARNING ***
  31. //
  32. //  This script will wipe out your wordpress posts, comments, terms, taxonomies
  33. //  and 'Term Relationships' before importing your b2evolution blog. Only
  34. //  leaves 'Uncategorized' term and related taxonomy and 'Term Relationship'.
  35. //
  36. //  *** WARNING ***
  37. //
  38. //  1) Install a fresh WP in a new directory in yor public_html. Maybe yow want WordPress
  39. //     to power your site's root (e.g. http://example.com) but you don't want all of the
  40. //     WordPress files cluttering up their root directory. WordPress allows you to install
  41. //     it into a subdirectory, but have your blog exist in the site root. Here you can
  42. //     find how to do it (after migration):
  43. //     http://codex.wordpress.org/Giving_WordPress_Its_Own_Directory#Using_a_pre-existing_subdirectory_install
  44. //  2) Copy the following tables from b2evo db to WP db (using phpMySql):
  45. //     evo_categories, evo_items__item, evo_comments
  46. //  3) Copy this script to WP installation directory, and run it accesing it's URL.
  47. //     Read carefully the output searching for errors.
  48. //  4) Access yor WP and check over your posts, comments, users, categories etc.
  49. //  5) Install 'redirection' plugin in WP in order to take care of old links, redirecting
  50. //     them to the new ones. It is easy to do with only one 'regular espression'.
  51. //     In this way, you will conserve the 'google reputation' of your posts.
  52. //     Frecuently visit the plugin statistics to discover pages people try to access but they
  53. //     can't (404 errors), and do the convenient redirections.
  54. //  6) Enjoy your new wordpress blog
  55. //
  56. //
  57. //
  58. //
  59.  
  60. function slugify($text)
  61. {
  62.   // replace non letter or digits by -
  63.   $text = preg_replace('~[^\\pL\d]+~u', '-', $text);
  64.   // trim
  65.   $text = trim($text, '-');
  66.   // transliterate
  67.   $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
  68.   // lowercase
  69.   $text = strtolower($text);
  70.   // remove unwanted characters
  71.   $text = preg_replace('~[^-\w]+~', '', $text);
  72.   if (empty($text))  { return 'n-a'; }
  73.   return $text;
  74. }
  75.  
  76. require('wp-config.php');
  77.  
  78. $sufijowp = "wp_";
  79.  
  80.  
  81. global $wpdb;
  82.  
  83. // Use credentials defined as constants in wp-config.php:
  84. $conexionwp=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD,TRUE);
  85. $conexionevo=mysql_connect(DB_HOST,'mc_demouser','demopass',TRUE);
  86.  
  87. mysql_select_db(DB_NAME,$conexionwp);
  88. mysql_select_db('mc_b2evolution',$conexionevo);
  89.  
  90.  
  91. print "<!DOCTYPE html>\n<html>\n<head>\n<title>Migrar b2evo a WordPress</title>\n";
  92. print "<meta charset=\"UTF-8\">\n</head>\n<body>\n<h1>Migrar b2evo a WordPress</h1>\n<pre>\n";
  93.  
  94. print "Borrar Comentarios...<br/>";
  95. mysql_query("DELETE FROM ".$sufijowp."comments", $conexionwp);
  96. mysql_query("ALTER TABLE ".$sufijowp."comments AUTO_INCREMENT=1", $conexionwp);
  97.  
  98. print "Borrar Posts...<br/>";
  99. mysql_query("DELETE FROM ".$sufijowp."posts", $conexionwp);
  100. mysql_query("ALTER TABLE ".$sufijowp."posts AUTO_INCREMENT=1", $conexionwp);
  101.  
  102. print "Borrar Terms...<br/>";
  103. mysql_query("DELETE FROM ".$sufijowp."terms WHERE term_id>1", $conexionwp);
  104. mysql_query("ALTER TABLE ".$sufijowp."terms AUTO_INCREMENT=2", $conexionwp);
  105.  
  106. print "Borrar Taxonomias...<br/>";
  107. mysql_query("DELETE FROM ".$sufijowp."term_taxonomy WHERE term_taxonomy_id>1", $conexionwp);
  108. mysql_query("ALTER TABLE ".$sufijowp."term_taxonomy AUTO_INCREMENT=2", $conexionwp);
  109.  
  110. print "Borrar Relaciones...<br/>";
  111. mysql_query("DELETE FROM ".$sufijowp."term_relationships WHERE object_id>1", $conexionwp);
  112. mysql_query("ALTER TABLE ".$sufijowp."term_relationships AUTO_INCREMENT=2", $conexionwp);
  113.  
  114.  
  115. print "Procesando Categorias...<br/><br/>";
  116. // post_category is no longer used in WP. Categories are now just a type
  117. // of term, and the wpterm_relationships table holds the links between
  118. // the wpposts table and the wpterm_taxonomy table (which defines terms
  119. // as part of a taxonomy.. 'category' in our case).
  120.  
  121.  
  122. $query = mysql_query("SELECT cat_ID, cat_name, cat_parent_ID as category_parent FROM evo_categories", $conexionevo);
  123. if(mysql_error()) {
  124.     print mysql_error() . "<br/>";
  125. }
  126.  
  127.  
  128. while($i = mysql_fetch_array($query)) {
  129.     print "  Importing: $i[cat_name]...\n";
  130.     $nicename = slugify(sanitize_title($i['cat_name']));
  131.     $catename = $wpdb->escape(wp_specialchars($i['cat_name']));
  132.     $query2 = "INSERT INTO ".$sufijowp."terms (term_id, name, slug) VALUES ('$i[cat_ID]', '$catename', '$nicename')";
  133.     mysql_query($query2,$conexionwp);
  134.     if(mysql_error()) {
  135.         print mysql_error() . "\n";
  136.     }
  137.     mysql_query("INSERT INTO ".$sufijowp."term_taxonomy (term_id, taxonomy) VALUES ($i[cat_ID], 'category')", $conexionwp);
  138.     if(mysql_error()) {
  139.         print mysql_error() . "\n";
  140.     }
  141. }
  142.  
  143. print "<br/><br/>";
  144.  
  145.  
  146. print "Procesando Posts...<br/><br/>";
  147.  
  148. $query = mysql_query("SELECT post_ID, post_datecreated, post_content, post_title, post_urltitle,  post_main_cat_ID, post_title as post_name, post_datemodified as post_modified_gmt, post_datemodified as post_modified FROM evo_items__item ORDER BY post_ID ASC", $conexionevo);
  149. if(mysql_error()) {
  150.     print mysql_error() . "<br/>";
  151. }
  152. while($i = mysql_fetch_array($query)) {
  153.     print "POST $i[post_ID]: \nWritting post category $i[post_main_cat_ID]...\n";
  154.     // wpterm_relationships table holds the links between the wpposts table and the
  155.     // wpterm_taxonomy table (which defines terms as part of a taxonomy, 'category' in this case).
  156.     mysql_query("INSERT INTO ".$sufijowp."term_relationships (object_id, term_taxonomy_id) VALUES ($i[post_ID], $i[post_main_cat_ID])", $conexionwp);
  157.     if(mysql_error()) {
  158.         print mysql_error() . "<br/>";
  159.     }
  160.  
  161.     $queryComment = mysql_query("SELECT count(*) as comment_count FROM `evo_comments` WHERE comment_post_ID=$i[post_ID]", $conexionevo);
  162.     $j = mysql_fetch_array($queryComment);
  163.  
  164.     foreach($i as $key=>$val) {
  165.         if ($key == "post_content")
  166.         {
  167.             $i[$key] = mysql_escape_string($val);
  168.         }
  169.         else
  170.         {
  171.             $i[$key] = mysql_escape_string($val);
  172.         }
  173.     }
  174.  
  175.     $post_content = $i[post_content];
  176.  
  177.     $post_content = $wpdb->escape($post_content);
  178.     $post_title = $wpdb->escape($i[post_title]);
  179.     $post_name = sanitize_title($i[post_urltitle]);
  180.  
  181.     print "Post_title: $i[post_title]\n";
  182.     print "Name: $i[post_name] | urltitle: $i[post_urltitle]\n";
  183.     print "Post_content:\n$i[post_content]\n\n";
  184.     $i[post_urltitle] = str_replace("aamp", "and", $i[post_urltitle]);
  185.     if(empty($i[post_name])) $i[post_name] = $i[post_urltitle];
  186.  
  187.     $query2  = "INSERT INTO ".$sufijowp."posts (ID, post_author, post_date, post_date_gmt, post_content, post_title, post_status, post_name, ";
  188.     $query2 .= "post_modified_gmt, post_modified, comment_count) VALUES ('$i[post_ID]', '1', '$i[post_datecreated]', '$i[post_datecreated]', '$i[post_content]', '$i[post_name]', ";
  189.     $query2 .= "'publish', '$i[post_urltitle]', '$i[post_modified]', '$i[post_modified]', '$j[comment_count]')";
  190.  
  191.     print "Writting post...<br/>";
  192.     mysql_query($query2, $conexionwp);
  193.     if(mysql_error()) {
  194.         print mysql_error() . "<br/>";
  195.     }
  196.     print "<br/><br/><br/>";
  197.  
  198.     $j++; // Why?
  199. }
  200. print "<br/>";
  201.  
  202.  
  203. print "Procesando Comentarios...<br/>";
  204. $query = mysql_query("SELECT comment_post_ID, comment_author, comment_author_email, comment_author_url, comment_author_IP, comment_date, comment_content, comment_karma FROM evo_comments", $conexionevo);
  205. if(mysql_error()) {
  206.     print mysql_error() . "<br/>";
  207. }
  208.  
  209. while($i = mysql_fetch_array($query)) {
  210.     foreach($i as $key=>$val) {
  211.         $i[$key] = mysql_escape_string($val);
  212.     }
  213.     //$i[comment_content] = mb_convert_encoding($i[comment_content],"UTF-8");
  214.     $query2 = "INSERT INTO ".$sufijowp."comments (comment_post_ID, comment_author, comment_author_email, comment_author_url, comment_author_IP, comment_date, comment_content, comment_karma) VALUES ('$i[comment_post_ID]', '$i[comment_author]', '$i[comment_author_email]', '$i[comment_author_url]', '$i[comment_author_IP]', '$i[comment_date]', '$i[comment_content]', '$i[comment_karma]')";
  215.     mysql_query($query2, $conexionwp);
  216.     if(mysql_error()) {
  217.         print mysql_error() . "<br/>";
  218.     }
  219. }
  220.  
  221. print "<br/>";
  222.  
  223. print "Processing Category Counts...<br/>";
  224.  
  225. $query = mysql_query("SELECT * FROM ".$sufijowp."terms", $conexionwp);
  226. if(mysql_error()) {
  227.     print mysql_error() . "<br/>";
  228. }
  229. while($i = mysql_fetch_array($query)) {
  230.     $query2 = mysql_query("SELECT count(*) FROM ".$sufijowp."term_relationships WHERE term_taxonomy_id = $i[term_id]", $conexionwp);
  231.     if(mysql_error()) {
  232.         print mysql_error() . "<br/>";
  233.     }
  234.     $result = mysql_fetch_array($query2);
  235.     print "$i[cat_name]: $result[0]...<br/>";
  236.     mysql_query("UPDATE ".$sufijowp."term_taxonomy set count = '$result[0]' WHERE term_id = '$i[term_id]'", $conexionwp);
  237.     if(mysql_error()) {
  238.         print mysql_error() . "<br/>";
  239.     }
  240.  
  241. }
  242. print "</pre>\n</body>\n</html>";
  243. ?>

 

 

@cmdearcos
Grupo TIECs

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *