PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/textpattern/include/import/import_mtdb.php

https://bitbucket.org/Manfre/xpattern
PHP | 220 lines | 168 code | 41 blank | 11 comment | 19 complexity | 62c2b40b643db835953731681a26ce35 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. // ----------------------------------------------------------------
  3. function doImportMTDB($mt_dblogin, $mt_db, $mt_dbpass, $mt_dbhost, $blog_id, $insert_into_section, $insert_with_status, $default_comment_invite)
  4. {
  5. global $txpcfg;
  6. //Keep some response on some part
  7. $results = array();
  8. //Avoid left joins
  9. $authors_map = array();
  10. $categories_map = array();
  11. // let's go - Dean says ;-).
  12. $mtlink = mysql_connect($mt_dbhost,$mt_dblogin,$mt_dbpass,true);
  13. if(!$mtlink){
  14. return 'mt database values don&#8217;t work. Please replace them and try again';
  15. }
  16. mysql_select_db($mt_db,$mtlink);
  17. $results[]= 'connected to mt database. Importing Data';
  18. sleep(2);
  19. $a = mysql_query("
  20. select
  21. author_id as user_id,
  22. author_nickname as name,
  23. author_name as RealName,
  24. author_email as email,
  25. author_password as pass
  26. from mt_author
  27. ",$mtlink);
  28. while($b = mysql_fetch_assoc($a)){
  29. $authors[] = $b;
  30. }
  31. $a = mysql_query("
  32. select
  33. mt_entry.entry_id as ID,
  34. mt_entry.entry_text as Body,
  35. mt_entry.entry_text_more as Body2,
  36. mt_entry.entry_title as Title,
  37. mt_entry.entry_excerpt as Excerpt,
  38. mt_entry.entry_keywords as Keywords,
  39. mt_entry.entry_created_on as Posted,
  40. mt_entry.entry_modified_on as LastMod,
  41. mt_entry.entry_author_id as AuthorID
  42. from mt_entry
  43. where entry_blog_id = '$blog_id'
  44. ",$mtlink);
  45. $results[]= mysql_error();
  46. while($b = mysql_fetch_assoc($a)){
  47. $cat = mysql_query("select placement_category_id as category_id from mt_placement where placement_entry_id='{$b['ID']}'");
  48. while ($cat_id = mysql_fetch_row($cat)){
  49. $categories[] = $cat_id[0];
  50. }
  51. if (!empty($categories[0])) $b['Category1'] = $categories[0];
  52. if (!empty($categories[1])) $b['Category2'] = $categories[1];
  53. unset($categories);
  54. //Trap comments for each article
  55. $comments = array();
  56. $q = "
  57. select
  58. mt_comment.comment_id as discussid,
  59. mt_comment.comment_ip as ip,
  60. mt_comment.comment_author as name,
  61. mt_comment.comment_email as email,
  62. mt_comment.comment_url as web,
  63. mt_comment.comment_text as message,
  64. mt_comment.comment_created_on as posted
  65. from mt_comment where comment_blog_id = '$blog_id' AND comment_entry_id='{$b['ID']}'
  66. ";
  67. $c = mysql_query($q, $mtlink);
  68. while($d=mysql_fetch_assoc($c)){
  69. $comments[] = $d;
  70. }
  71. //Attach comments to article
  72. $b['comments'] = $comments;
  73. unset($comments);
  74. //Article finished
  75. $articles[] = $b;
  76. }
  77. $a = mysql_query("
  78. select category_id,category_label from mt_category where category_blog_id='{$blog_id}'
  79. ",$mtlink);
  80. while($b=mysql_fetch_assoc($a)){
  81. $categories_map[$b['category_id']] = $b['category_label'];
  82. }
  83. mysql_close($mtlink);
  84. //Yes, we have to make a new connection
  85. //otherwise doArray complains
  86. $DB = new DB;
  87. include txpath.'/lib/classTextile.php';
  88. $textile = new Textile;
  89. if (!empty($authors)) {
  90. foreach($authors as $author) {
  91. extract($author);
  92. $name = (empty($name)) ? $RealName : $name;
  93. $authors_map[$user_id] = $name;
  94. $authorid = safe_field('user_id', 'txp_users', "name = '".doSlash($name)."'");
  95. if (!$authorid){
  96. //Add new authors
  97. $q = safe_insert("txp_users","
  98. name = '".doSlash($RealName)."',
  99. email = '".doSlash($email)."',
  100. pass = '".doSlash($pass)."',
  101. RealName = '".doSlash($RealName)."',
  102. privs='1'"
  103. );
  104. if($q) {
  105. $results[]= 'inserted '.$RealName.' into txp_users';
  106. } else $results[]=mysql_error();
  107. }
  108. }
  109. }
  110. if (!empty($categories_map)) {
  111. foreach ($categories_map as $category) {
  112. $category = doSlash($category);
  113. $rs = safe_row('id', 'txp_category', "name='$category' and type='article'");
  114. if (!$rs){
  115. $q = safe_insert("txp_category","name='$category',type='article',parent='root'");
  116. if($q) {
  117. $results[]= 'inserted '.stripslashes($category).' into txp_category';
  118. } else $results[]=mysql_error();
  119. }
  120. }
  121. }
  122. if (!empty($articles)) {
  123. foreach ($articles as $article) {
  124. extract($article);
  125. $Body .= (trim($Body2)) ? "\n\n".$Body2 : '';
  126. $Body_html = $textile->textileThis($Body);
  127. $Excerpt_html = $textile->textileThis($Excerpt);
  128. $Title = $textile->textileThis($Title,1);
  129. $Category1 = (!empty($Category1)) ? doSlash($Category1) : '';
  130. $AuthorID = (!empty($authors_map[$AuthorID])) ? doSlash($authors_map[$AuthorID]) : '';
  131. $insertID = safe_insert("textpattern","
  132. ID = '$ID',
  133. Posted = '$Posted',
  134. LastMod = '$LastMod',
  135. Title = '".doSlash($Title)."',
  136. Body = '".doSlash($Body)."',
  137. Excerpt = '".doSlash($Excerpt)."',
  138. Excerpt_html = '".doSlash($Excerpt_html)."',
  139. Keywords = '".doSlash($Keywords)."',
  140. Body_html = '".doSlash($Body_html)."',
  141. AuthorID = '$AuthorID',
  142. Category1 = '$Category1',
  143. AnnotateInvite = '".doSlash($default_comment_invite)."',
  144. Section = '".doSlash($insert_into_section)."',
  145. uid = '".md5(uniqid(rand(),true))."',
  146. feed_time = '".substr($Posted,0,10)."',
  147. Status = '$insert_with_status'
  148. ");
  149. if($insertID) {
  150. $results[] = 'inserted MT entry '.strong($Title).
  151. ' into Textpattern as article '.strong($insertID).'';
  152. //Do coment for article
  153. if (!empty($comments) && is_array($comments)) {
  154. foreach ($comments as $comment) {
  155. extract($comment);
  156. $message = nl2br($message);
  157. $commentID = safe_insert("txp_discuss","
  158. discussid = $discussid,
  159. parentid = $insertID,
  160. name = '".doSlash($name)."',
  161. email = '".doSlash($email)."',
  162. web = '".doSlash($web)."',
  163. message = '".doSlash($message)."',
  164. ip = '$ip',
  165. posted = '$posted',
  166. visible = 1"
  167. );
  168. if($commentID) {
  169. $results[] = 'inserted MT comment '.$commentID.
  170. ' for article '.$insertID.' into txp_discuss';
  171. } else $results[]=mysql_error();
  172. }
  173. }
  174. } else $results[] = mysql_error();
  175. }
  176. }
  177. return join('<br />', $results);
  178. }
  179. ?>