Yeye_64

Untitled

Jan 2nd, 2019
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.06 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Import the forum bbpress into the new forum
  5. *
  6. *
  7. */
  8. function umc_wp_forum_import() {
  9. // get all the current posts
  10. $DB = 'XXXXX';
  11. $table = 'YYY_posts';
  12. $forum_prefix = 'YYY_forum';
  13. $import_category = 129;
  14.  
  15. // forums
  16. $sql_forum = "SELECT post_title, post_author, ID, post_date, post_content, post_name FROM `$DB`.`$table` "
  17. . "WHERE post_type='forum' AND post_status='publish';";
  18.  
  19. $F = umc_mysql_fetch_all($sql_forum);
  20. foreach ($F as $f) {
  21. $check_duplicates = "SELECT * FROM `{$forum_prefix}_forums` WHERE parent_id=$import_category AND slug='{$f['post_name']}'";
  22. $C = umc_mysql_fetch_all($check_duplicates);
  23. if (count($C) > 0) {
  24. continue;
  25. }
  26. echo $f['post_title'] . "<br>";
  27.  
  28. // insert new forum
  29. $ins_forum = "INSERT INTO `{$forum_prefix}_forums`(`name`, `parent_id`, `parent_forum`, `description`, `sort`, `closed`, `slug`) "
  30. . "VALUES ('{$f['post_title']}', $import_category, 0, '{$f['post_content']}', 99 , 1 ,'{$f['post_name']}')";
  31. umc_mysql_execute_query($ins_forum);
  32. $ins_forum_id = umc_mysql_insert_id();
  33. umc_wp_forum_import_topics($f['ID'], $ins_forum_id);
  34. break;
  35. }
  36. echo "done!";
  37. }
  38.  
  39. function umc_wp_forum_import_topics($source_id, $target_id) {
  40. $DB = 'XXXXX';
  41. $table = 'YYY_posts';
  42. $forum_prefix = 'YYY_forum';';
  43.  
  44. $sql_topics = "SELECT post_title, post_author, ID, post_date, post_content, post_name FROM `$DB`.`$table` "
  45. . "WHERE post_type='topic' AND post_status='publish' AND post_parent=$source_id;";
  46. $T = umc_mysql_fetch_all($sql_topics);
  47. foreach ($T as $t) {
  48. // insert topic
  49. $title = umc_mysql_real_escape_string($t['post_title']);
  50. $ins_topic = "INSERT INTO `{$forum_prefix}_topics`(`parent_id`, `views`, `name`, `status`) VALUES "
  51. . "($target_id, 0,$title,'normal_open')";
  52. umc_mysql_execute_query($ins_topic);
  53. $ins_topic_id = umc_mysql_insert_id();
  54.  
  55. // insert the text of the main post
  56. $content = umc_mysql_real_escape_string($t['post_content']);
  57. $ins_topic_text = "INSERT INTO `{$forum_prefix}_posts`(`text`, `parent_id`, `date`, `author_id`, `uploads`) VALUES "
  58. . "($content, $ins_topic_id,'{$t['post_date']}',{$t['post_author']}, 'a:0:{}')";
  59. umc_mysql_execute_query($ins_topic_text);
  60.  
  61. // insert the replies
  62. umc_wp_forum_import_replies($t['ID'], $ins_topic_id);
  63. }
  64. }
  65.  
  66. function umc_wp_forum_import_replies($source_id, $target_id) {
  67. $DB = 'XXXXX';
  68. $table = 'YYY_posts';
  69. $forum_prefix = 'YYY_forum';
  70.  
  71. $sql_replies = "SELECT post_title, post_author, ID, post_date, post_content, post_name FROM `$DB`.`$table` "
  72. . "WHERE post_type='reply' AND post_status='publish' AND post_parent=$source_id;";
  73. $R = umc_mysql_fetch_all($sql_replies);
  74. foreach ($R as $r) {
  75. // insert the text of the main post
  76. $content = umc_mysql_real_escape_string($r['post_content']);
  77. $ins_reply = "INSERT INTO `{$forum_prefix}_posts`(`text`, `parent_id`, `date`, `author_id`, `uploads`) VALUES "
  78. . "($content, $target_id,'{$r['post_date']}',{$r['post_author']}, 'a:0:{}')";
  79. umc_mysql_execute_query($ins_reply);
  80. }
  81. }
  82.  
  83.  
  84. /**
  85. * PDO abstraction
  86. *
  87. */
  88. umc_wp_forum_import();
  89. global $UNC_DB;
  90.  
  91. // configure your database here
  92. $default_config=array(
  93. 'database' => 'XXXXX',
  94. 'username' => 'AZERTY',
  95. 'server' => 'mysql.ZZZZ.fr',
  96. 'password' => 'PASSWORD',
  97. );
  98. // let's set the values in the global variable
  99. foreach ($default_config as $type => $value) {
  100. if (!isset($UNC_DB[$type])) {
  101. $UNC_DB[$type]=$value;
  102. }
  103. }
  104. // this is custom error reporting
  105. // see https://github.com/uncovery/xmpp_error for more info
  106. // if not available, will be ignored
  107. $UNC_DB['XMPP_ERROR'] = true;
  108. if (!function_exists('XMPP_ERROR_trace')) {
  109. $UNC_DB['XMPP_ERROR'] = false;
  110. }
  111. // make a connection to the database.
  112. $UNC_DB['link']=new PDO("mysql:host={$UNC_DB['server']};dbname={$UNC_DB['database']}", $UNC_DB['username'],$UNC_DB['password']);
  113. $UNC_DB['link']->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  114. /**
  115. * Replacement for mysql_query
  116. * returns result. includes error notification
  117. *
  118. * can close the query if needed, otherwise returns result
  119. *
  120. * @global PDO $UMC_DB
  121. * @param string $sql
  122. * @param boolean $close
  123. * @return type
  124. */
  125. function umc_mysql_query($sql, $close=false) {
  126. global $UNC_DB;
  127. if ($UNC_DB['XMPP_ERROR']) {XMPP_ERROR_trace(__FUNCTION__, func_get_args());}
  128. $rst=$UNC_DB['link']->query($sql);
  129. $error=$UNC_DB['link']->errorInfo();
  130. if (!is_null($error[2]) && $UNC_DB['XMPP_ERROR']) {
  131. XMPP_ERROR_trigger("MySQL Query Error: '$sql' : " . $error[2]);
  132. return false;
  133. } if (!is_null($error[2])) {
  134. error_log("MySQL Query Error: '$sql' : " . $error[2]);
  135. return false;
  136. } else if ($close) {
  137. $rst->closeCursor();
  138. return true;
  139. } else {
  140. return $rst;
  141. }
  142. }
  143. /**
  144. * Replacement for mysql_query
  145. * specific for DELETE, UPDATE and INSERT queries. returns affected rows
  146. *
  147. * can close the query if needed, otherwise returns result
  148. *
  149. * @global PDO $UMC_DB
  150. * @param string $sql
  151. * @param boolean $close
  152. * @return type
  153. */
  154. function umc_mysql_execute_query($sql) {
  155. global $UNC_DB;
  156. if ($UNC_DB['XMPP_ERROR']) {XMPP_ERROR_trace(__FUNCTION__, func_get_args());}
  157. $obj=$UNC_DB['link']->prepare($sql);
  158. $obj->execute();
  159. $error=$obj->errorInfo();
  160. if (!is_null($error[2]) && $UNC_DB['XMPP_ERROR']) {
  161. XMPP_ERROR_trigger("MySQL Query Error: '$sql' : " . $error[2]);
  162. return false;
  163. } else if (!is_null($error[2]) ) {
  164. error_log("MySQL Query Error: '$sql' : " . $error[2]);
  165. return false;
  166. } else {
  167. $count=$obj->rowCount();
  168. return $count;
  169. }
  170. }
  171. /**
  172. * Replacement for mysql_affected_rows
  173. * Returns count. Optionally closes the recordset
  174. *
  175. * @param recordset $rst
  176. * @param boolean $close
  177. * @return int
  178. */
  179. function umc_mysql_affected_rows($rst, $close=false) {
  180. $rowCount=$rst->fetchColumn();
  181. if ($close) {
  182. $rst->closeCursor();
  183. }
  184. return $rowCount;
  185. }
  186. /**
  187. * Replacement for mysql_insert_id
  188. *
  189. * @global PDO $UMC_DB
  190. * @return type integer
  191. */
  192. function umc_mysql_insert_id() {
  193. global $UNC_DB;
  194. return $UNC_DB['link']->lastInsertId();
  195. }
  196. /**
  197. * Replacement for mysql_fetch_array (MYSQL_ASSOC)
  198. * Returns one line of associative arrays
  199. *
  200. * @param type $rst
  201. * @return type
  202. */
  203. function umc_mysql_fetch_array($rst) {
  204. global $UNC_DB;
  205. if ($UNC_DB['XMPP_ERROR']) {XMPP_ERROR_trace(__FUNCTION__, func_get_args());}
  206. if (!$rst && $UNC_DB['XMPP_ERROR']) {
  207. XMPP_ERROR_trigger("tried fetch_array on erroneous recordset");
  208. return false;
  209. } else if (!$rst) {
  210. error_log("tried fetch_array on erroneous recordset");
  211. return false;
  212. }
  213. $row=$rst->fetch(PDO::FETCH_ASSOC);
  214. return $row;
  215. }
  216. /**
  217. * Replacement of mysql_free_result
  218. *
  219. * @param type $rst
  220. */
  221. function umc_mysql_free_result($rst) {
  222. $rst->closeCursor();
  223. }
  224. /**
  225. * Replacement of mysql_real_escape_string
  226. * ATTENTION: This also puts quotes around the value
  227. *
  228. * @global PDO $UMC_DB
  229. * @param type $value
  230. * @return type
  231. */
  232. function umc_mysql_real_escape_string($value) {
  233. global $UNC_DB;
  234. return $UNC_DB['link']->quote($value);
  235. }
  236. function umc_mysql_fetch_all($sql) {
  237. global $UNC_DB;
  238. if ($UNC_DB['XMPP_ERROR']) {XMPP_ERROR_trace(__FUNCTION__, func_get_args());}
  239. $stmt=$UNC_DB['link']->prepare($sql);
  240. if (!$stmt && $UNC_DB['XMPP_ERROR']) {
  241. $error=$UNC_DB['link']->errorInfo();
  242. XMPP_ERROR_trigger($error);
  243. return false;
  244. } else if (!$stmt) {
  245. error_log($UNC_DB['link']->errorInfo());
  246. return false;
  247. } else {
  248. $stmt->execute();
  249. }
  250. $result=$stmt->fetchAll(PDO::FETCH_ASSOC);
  251. return $result;
  252. }
Advertisement
Add Comment
Please, Sign In to add comment