PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/bin/blog/b2-to-bxb-import.php

https://github.com/chregu/fluxcms
PHP | 191 lines | 150 code | 35 blank | 6 comment | 14 complexity | 355bf56ff5b8120f09f07fc99c11c1fd MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, Apache-2.0, LGPL-2.1
  1. <?php
  2. include_once("../inc/bx/init.php");
  3. bx_init::start('conf/config.xml',"../");
  4. $b2['dbName'] = 'b2';
  5. $b2['dbPrefix'] = 'b2';
  6. $b2['dbHost'] = '';
  7. $b2['dbUser'] = 'root';
  8. $b2['dbPass'] = '';
  9. $b2['dbType'] = 'mysql';
  10. $serendipity['dbPrefix'] = "";
  11. $b2db = MDB2::Connect($b2['dbType']."://".$b2['dbUser'].":".$b2['dbPass']."@".$b2['dbHost']."/".$b2['dbName']);
  12. $bxbdb = $GLOBALS['POOL']->db;
  13. $bxbdb->loadModule('extended');
  14. $tidyOptions = array(
  15. "output-xhtml" => true,
  16. "show-body-only" => true,
  17. "clean" => true,
  18. "wrap" => "350",
  19. "indent" => true,
  20. "indent-spaces" => 1,
  21. "ascii-chars" => false,
  22. "wrap-attributes" => false,
  23. "alt-text" => "",
  24. "doctype" => "loose",
  25. "numeric-entities" => true,
  26. "drop-proprietary-attributes" => true
  27. );
  28. $tidy = new tidy();
  29. //import authors
  30. $res = $b2db->query("select * from ". $b2['dbPrefix'] ."users");
  31. $useridmap = array();
  32. while ($row = $res->fetchRow(MDB2_FETCHMODE_ASSOC)) {
  33. $newid = $bxbdb->extended->getOne( "select id from users where user_login = ".$bxbdb->quote($row['user_login']));
  34. if ($newid) {
  35. $useridmap[$row['id']] = $newid;
  36. print $row['user_login'] . "already in DB\n";
  37. }
  38. else {
  39. $newid = $bxbdb->extended->getOne("select max(id) from users") + 1;
  40. $query = "insert into users (user_login, id, user_pass, user_email) VALUES
  41. (".$bxbdb->quote($row['user_login']).",".($newid).",".$bxbdb->quote(md5($row['user_pass'])).",".$bxbdb->quote($row['user_email']).")";
  42. $bxbdb->query($query);
  43. $useridmap[$row['id']] = $newid;
  44. }
  45. $usernamemap[$row['id']] = $row['user_login'];
  46. }
  47. //import categories
  48. $bxbdb->query("delete from blogcategories");
  49. // insert root cat
  50. $query = "insert into blogcategories(id, name, uri,parentid) VALUES (1,'All','root',0)";
  51. $bxbdb->query($query);
  52. $res = $b2db->query("select * from ". $b2['dbPrefix'] ."categories");
  53. while ($row = $res->fetchRow(MDB2_FETCHMODE_ASSOC)) {
  54. $query = "insert into blogcategories(id, name, uri,parentid) VALUES (".$bxbdb->quote(($row['cat_id'] + 1)).",".$bxbdb->quote($row['cat_name']).",".$bxbdb->quote(makeUri($row['cat_name'])).",1)";
  55. $bxbdb->query($query);
  56. print $query . "\n";
  57. }
  58. //nested set fun
  59. include("../forms/blogcategories/updatetree.php");
  60. //import posts
  61. $bxbdb->query("delete from blogposts");
  62. $bxbdb->query("delete from blogposts2categories");
  63. $res = $b2db->query("select * from ". $b2['dbPrefix'] ."posts ");
  64. while ($row = $res->fetchRow(MDB2_FETCHMODE_ASSOC)) {
  65. $query = "insert into blogposts
  66. (id, post_title, post_date, post_content, post_uri, post_author, changed) VALUES
  67. (".
  68. $row['id'].",".
  69. $bxbdb->quote(tidyUp(stripslashes($row['post_title']))).",".
  70. $bxbdb->quote($row['post_date']).",".
  71. $bxbdb->quote(tidyUp(stripslashes($row['post_content']))).",".
  72. $bxbdb->quote(makeUri(stripslashes($row['id'].'-'.$row['post_title']))).",".
  73. $bxbdb->quote( $usernamemap[$row['post_author']]). ",".
  74. $bxbdb->quote($row['post_date'])."
  75. )";
  76. $res2 = $bxbdb->query($query);
  77. if (MDB2::isError($res2)) {
  78. print $res2->getMessage();
  79. print $res2->getUserinfo();
  80. die();
  81. }
  82. $query = "insert into blogposts2categories( id, blogposts_id,blogcategories_id) VALUES(".
  83. $row['id'].",".
  84. $row['id'].",".
  85. ($row['post_category'] + 1) .")";
  86. $res2 = $bxbdb->query($query);
  87. if (MDB2::isError($res2)) {
  88. print $res2->getMessage();
  89. print $res2->getUserinfo();
  90. die();
  91. }
  92. print "import post ".$row['id']."\n";
  93. }
  94. //import comments
  95. $bxbdb->query("delete from blogcomments");
  96. $res = $b2db->query("select * from ". $b2['dbPrefix'] ."comments ");
  97. while ($row = $res->fetchRow(MDB2_FETCHMODE_ASSOC)) {
  98. if (strpos($row['comment_content'],"<trackback") !== false ) {
  99. $type = "TRACKBACK";
  100. } else if (strpos($row['comment_content'],"<pingback") !== false) {
  101. $type = "PINGBACK";
  102. } else {
  103. $type = "NORMAL";
  104. }
  105. $query = "insert into blogcomments
  106. (id, comment_posts_id, comment_date, comment_author, comment_author_email, comment_author_url,
  107. comment_author_ip, comment_content, comment_type)
  108. VALUES (".
  109. $bxbdb->quote(($row['comment_id'])).",".
  110. $bxbdb->quote(($row['comment_post_id'])).",".
  111. $bxbdb->quote(($row['comment_date'])).",".
  112. $bxbdb->quote(tidyUp(stripslashes($row['comment_author']))).",".
  113. $bxbdb->quote(stripslashes($row['comment_author_email'])).",".
  114. $bxbdb->quote(tidyUp(stripslashes($row['comment_author_url']))).",".
  115. $bxbdb->quote(stripslashes($row['comment_author_ip'])).",".
  116. $bxbdb->quote(tidyUp(stripslashes($row['comment_content']))).",".
  117. $bxbdb->quote($type)."".
  118. ")";
  119. $res2 = $bxbdb->query($query);
  120. if (MDB2::isError($res2)) {
  121. print $res2->getMessage();
  122. print $res2->getUserinfo();
  123. die();
  124. }
  125. print "import comment ".$row['comment_id']." on post " . $row['comment_post_id'] ." with type $type\n";
  126. }
  127. function makeUri($title,$id = 0) {
  128. if (!$title) {
  129. $title = "notitle".$id;
  130. }
  131. $newValue= strtolower(utf8_encode(preg_replace("/[-;: \.!,?+'$£\"*ç%&\/\(\)=]/","-",$title)));
  132. $newValue= preg_replace("/-{2,}/","-",$newValue);
  133. $newValue= preg_replace("/[öÖ]/u","oe",$newValue);
  134. $newValue= preg_replace("/[üÜ]/u","ue",$newValue);
  135. $newValue= preg_replace("/[äÄ]/u","ae",$newValue);
  136. $newValue= preg_replace("/[éè]/u","e",$newValue);
  137. $newValue= preg_replace("/[ï]/u","i",$newValue);
  138. $newValue= preg_replace("/[ñ]/u","n",$newValue);
  139. $newValue= preg_replace("/[à]/u","a",$newValue);
  140. //numbers at the end are nasty for bxcms
  141. $newValue= preg_replace("/—+$/","",$newValue);
  142. $newValue= preg_replace("/^-/","",$newValue);
  143. return $newValue;
  144. }
  145. function tidyUp ($string) {
  146. global $tidy, $tidyOptions;
  147. $tidy->parseString($string,$tidyOptions,"latin1");
  148. $tidy->cleanRepair();
  149. return bx_helpers_string::utf2entities(utf8_encode((string) $tidy));
  150. }
  151. ?>