Yeye_64

Untitled

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