PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/textpattern/include/import/import_b2.php

https://bitbucket.org/Manfre/xpattern
PHP | 159 lines | 123 code | 25 blank | 11 comment | 8 complexity | fd1dbd20d27d1144e1716367689366c9 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. //Absolutely untested. Any volunteer with a b2 db dump to collaborate?
  3. function doImportB2($b2dblogin, $b2db, $b2dbpass, $b2dbhost, $insert_into_section, $insert_with_status, $default_comment_invite)
  4. {
  5. global $txpcfg;
  6. //Keep some response on some part
  7. $results = array();
  8. // let's go - Dean says ;-).
  9. $b2link = mysql_connect($b2dbhost,$b2dblogin,$b2dbpass,true);
  10. if(!$b2link){
  11. return 'b2 database values don&#8217;t work. Go back, replace them and try again';
  12. }
  13. mysql_select_db($b2db,$b2link);
  14. $results[]='connected to b2 database. Importing Data';
  15. // Copy & Paste your table-definitions from b2config.php
  16. $tableposts = 'b2posts';
  17. $tableusers = 'b2users';
  18. $tablecategories = 'b2categories';
  19. $tablecomments = 'b2comments';
  20. $a = mysql_query("
  21. select
  22. ".$tableposts.".ID as ID,
  23. ".$tableposts.".post_date as Posted,
  24. ".$tableposts.".post_title as Title,
  25. ".$tableposts.".post_content as Body,
  26. ".$tablecategories.".cat_name as Category1,
  27. ".$tableusers.".user_login as AuthorID
  28. from ".$tableposts."
  29. left join ".$tablecategories." on
  30. ".$tablecategories.".cat_ID = ".$tableposts.".post_category
  31. left join ".$tableusers." on
  32. ".$tableusers.".ID = ".$tableposts.".post_author
  33. ORDER BY post_date DESC
  34. ",$b2link) or $results[]= mysql_error();
  35. while($b=mysql_fetch_array($a)) {
  36. $articles[] = $b;
  37. }
  38. $a = mysql_query("
  39. select
  40. ".$tablecomments.".comment_ID as discussid,
  41. ".$tablecomments.".comment_post_ID as parentid,
  42. ".$tablecomments.".comment_author_IP as ip,
  43. ".$tablecomments.".comment_author as name,
  44. ".$tablecomments.".comment_author_email as email,
  45. ".$tablecomments.".comment_author_url as web,
  46. ".$tablecomments.".comment_content as message,
  47. ".$tablecomments.".comment_date as posted
  48. from ".$tablecomments."
  49. ",$b2link) or $results[]= mysql_error();
  50. while($b=mysql_fetch_assoc($a)){
  51. $comments[] = $b;
  52. }
  53. mysql_close($b2link);
  54. //keep a handy copy of txpdb values, and do not alter Dean code
  55. // for now! ;-)
  56. $txpdb = $txpcfg['db'];
  57. $txpdblogin = $txpcfg['user'];
  58. $txpdbpass = $txpcfg['pass'];
  59. $txpdbhost = $txpcfg['host'];
  60. //Yes, we have to make a new connection
  61. //otherwise doArray complains
  62. $DB = new DB;
  63. $txplink = &$DB->link;
  64. mysql_select_db($txpdb,$txplink);
  65. include txpath.'/lib/classTextile.php';
  66. $textile = new Textile;
  67. if (!empty($articles)) {
  68. foreach($articles as $a){
  69. if (is_callable('utf8_encode'))
  70. {
  71. // Also fixing break-tags for users with b2s Auto-BR
  72. $a['Body'] = utf8_encode(str_replace("<br />\n","\n",stripslashes($a['Body'])));
  73. $a['Title'] = utf8_encode(stripslashes($a['Title']));
  74. $a['Title'] = $textile->TextileThis($a['Title'],'',1);
  75. }
  76. // b2 uses the magic word "<!--more-->" to generate excerpts
  77. if (strpos($a['Body'],'<!--more-->'))
  78. {
  79. //Everything that is before "more" can be treated as the excerpt.
  80. $pos = strpos($a['Body'],'<!--more-->');
  81. $a['Excerpt'] = substr($a['Body'],0,$pos);
  82. $a['Excerpt_html'] = $textile->textileThis($a['Excerpt']);
  83. $a['Body'] = str_replace('<!--more-->','',$a['Body']);
  84. }
  85. else
  86. {
  87. $a['Excerpt'] = '';
  88. $a['Excerpt_html'] = '';
  89. }
  90. $a['url_title'] = stripSpace($a['Title'],1);
  91. $a['Body_html'] = $textile->textileThis($a['Body']);
  92. extract(array_slash($a));
  93. $q = mysql_query("
  94. insert into `".PFX."textpattern` set
  95. ID = '$ID',
  96. Posted = '$Posted',
  97. Title = '$Title',
  98. url_title = '$url_title',
  99. Body = '$Body',
  100. Body_html = '$Body_html',
  101. Excerpt = '$Excerpt',
  102. Excerpt_html = '$Excerpt_html',
  103. Category1 = '$Category1',
  104. AuthorID = '$AuthorID',
  105. Section = '$insert_into_section',
  106. AnnotateInvite = '$default_comment_invite',
  107. uid='".md5(uniqid(rand(),true))."',
  108. feed_time='".substr($Posted,0,10)."',
  109. Status = '$insert_with_status'
  110. ",$txplink) or $results[]= mysql_error();
  111. if (mysql_insert_id() ) {
  112. $results[]= 'inserted b2 entry '.$Title.
  113. ' into Textpattern as article '.$ID.'';
  114. }
  115. }
  116. }
  117. if (!empty($comments)) {
  118. foreach ($comments as $comment) {
  119. extract(array_slash($comment));
  120. if (is_callable('utf8_encode'))
  121. $message = utf8_encode($message);
  122. $message = nl2br($message);
  123. $q = mysql_query("insert into `".PFX."txp_discuss` values
  124. ($discussid,$parentid,'$name','$email','$web','$ip','$posted','$message',1)",
  125. $txplink) or $results[]= mysql_error($q);
  126. if(mysql_insert_id()) {
  127. $results[]= 'inserted b2 comment <strong>'.$parentid
  128. .'</strong> into txp_discuss';
  129. }
  130. }
  131. }
  132. return join('<br />', $results);
  133. }
  134. ?>