Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Import the forum bbpress into the new forum
- *
- *
- */
- function umc_wp_forum_import() {
- // get all the current posts
- $DB = 'XXX';
- $table = 'YYY_forum_posts';
- $forum_prefix = 'YYY_forum_forums';
- $import_category = 137;
- // forums
- $sql_forum = "SELECT post_title, post_author, ID, post_date, post_content, post_name FROM `$DB`.`$table` "
- . "WHERE post_type='forum' AND post_status='publish';";
- $F = umc_mysql_fetch_all($sql_forum);
- foreach ($F as $f) {
- $check_duplicates = "SELECT * FROM `{$forum_prefix}_forums` WHERE parent_id=$import_category AND slug='{$f['post_name']}'";
- $C = umc_mysql_fetch_all($check_duplicates);
- if (count($C) > 0) {
- continue;
- }
- echo $f['post_title'] . "<br>";
- // insert new forum
- $ins_forum = "INSERT INTO `{$forum_prefix}_forums`(`name`, `parent_id`, `parent_forum`, `description`, `sort`, `closed`, `slug`) "
- . "VALUES ('{$f['post_title']}', $import_category, 0, '{$f['post_content']}', 99 , 1 ,'{$f['post_name']}')";
- umc_mysql_execute_query($ins_forum);
- $ins_forum_id = umc_mysql_insert_id();
- umc_wp_forum_import_topics($f['ID'], $ins_forum_id);
- break;
- }
- echo "done!";
- }
- function umc_wp_forum_import_topics($source_id, $target_id) {
- $DB = 'XXX';
- $table = 'YYY_forum_posts';
- $forum_prefix = 'YYY_forum_forums';
- $sql_topics = "SELECT post_title, post_author, ID, post_date, post_content, post_name FROM `$DB`.`$table` "
- . "WHERE post_type='topic' AND post_status='publish' AND post_parent=$source_id;";
- $T = umc_mysql_fetch_all($sql_topics);
- foreach ($T as $t) {
- // insert topic
- $title = umc_mysql_real_escape_string($t['post_title']);
- $ins_topic = "INSERT INTO `{$forum_prefix}_topics`(`parent_id`, `views`, `name`, `status`) VALUES "
- . "($target_id, 0,$title,'normal_open')";
- umc_mysql_execute_query($ins_topic);
- $ins_topic_id = umc_mysql_insert_id();
- // insert the text of the main post
- $content = umc_mysql_real_escape_string($t['post_content']);
- $ins_topic_text = "INSERT INTO `{$forum_prefix}_posts`(`text`, `parent_id`, `date`, `author_id`, `uploads`) VALUES "
- . "($content, $ins_topic_id,'{$t['post_date']}',{$t['post_author']}, 'a:0:{}')";
- umc_mysql_execute_query($ins_topic_text);
- // insert the replies
- umc_wp_forum_import_replies($t['ID'], $ins_topic_id);
- }
- }
- function umc_wp_forum_import_replies($source_id, $target_id) {
- $DB = 'XXX';
- $table = 'YYY_forum_posts';
- $forum_prefix = 'YYY_forum_forums';
- $sql_replies = "SELECT post_title, post_author, ID, post_date, post_content, post_name FROM `$DB`.`$table` "
- . "WHERE post_type='reply' AND post_status='publish' AND post_parent=$source_id;";
- $R = umc_mysql_fetch_all($sql_replies);
- foreach ($R as $r) {
- // insert the text of the main post
- $content = umc_mysql_real_escape_string($r['post_content']);
- $ins_reply = "INSERT INTO `{$forum_prefix}_posts`(`text`, `parent_id`, `date`, `author_id`, `uploads`) VALUES "
- . "($content, $target_id,'{$r['post_date']}',{$r['post_author']}, 'a:0:{}')";
- umc_mysql_execute_query($ins_reply);
- }
- }
- /**
- * PDO abstraction
- *
- */
- global $UNC_DB;
- // configure your database here
- $default_config=array(
- 'database' => 'XXX',
- 'username' => 'ZZZ',
- 'server' => 'mysql.WWW.fr',
- 'password' => 'AZERTY',
- );
- // let's set the values in the global variable
- foreach ($default_config as $type => $value) {
- if (!isset($UNC_DB[$type])) {
- $UNC_DB[$type]=$value;
- }
- }
- // this is custom error reporting
- // see https://github.com/uncovery/xmpp_error for more info
- // if not available, will be ignored
- $UNC_DB['XMPP_ERROR'] = true;
- if (!function_exists('XMPP_ERROR_trace')) {
- $UNC_DB['XMPP_ERROR'] = false;
- }
- // make a connection to the database.
- $UNC_DB['link']=new PDO("mysql:host={$UNC_DB['server']};dbname={$UNC_DB['database']}", $UNC_DB['username'],$UNC_DB['password']);
- var_dump($UNC_DB['link']);
- $UNC_DB['link']->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
- var_dump($UNC_DB['link']);
- /**
- * Replacement for mysql_query
- * returns result. includes error notification
- *
- * can close the query if needed, otherwise returns result
- *
- * @global PDO $UMC_DB
- * @param string $sql
- * @param boolean $close
- * @return type
- */
- function umc_mysql_query($sql, $close=false) {
- global $UNC_DB;
- if ($UNC_DB['XMPP_ERROR']) {XMPP_ERROR_trace(__FUNCTION__, func_get_args());}
- $rst=$UNC_DB['link']->query($sql);
- $error=$UNC_DB['link']->errorInfo();
- if (!is_null($error[2]) && $UNC_DB['XMPP_ERROR']) {
- XMPP_ERROR_trigger("MySQL Query Error: '$sql' : " . $error[2]);
- return false;
- } if (!is_null($error[2])) {
- error_log("MySQL Query Error: '$sql' : " . $error[2]);
- return false;
- } else if ($close) {
- $rst->closeCursor();
- return true;
- } else {
- return $rst;
- }
- }
- /**
- * Replacement for mysql_query
- * specific for DELETE, UPDATE and INSERT queries. returns affected rows
- *
- * can close the query if needed, otherwise returns result
- *
- * @global PDO $UMC_DB
- * @param string $sql
- * @param boolean $close
- * @return type
- */
- function umc_mysql_execute_query($sql) {
- global $UNC_DB;
- if ($UNC_DB['XMPP_ERROR']) {XMPP_ERROR_trace(__FUNCTION__, func_get_args());}
- $obj=$UNC_DB['link']->prepare($sql);
- $obj->execute();
- $error=$obj->errorInfo();
- if (!is_null($error[2]) && $UNC_DB['XMPP_ERROR']) {
- XMPP_ERROR_trigger("MySQL Query Error: '$sql' : " . $error[2]);
- return false;
- } else if (!is_null($error[2]) ) {
- error_log("MySQL Query Error: '$sql' : " . $error[2]);
- return false;
- } else {
- $count=$obj->rowCount();
- return $count;
- }
- }
- /**
- * Replacement for mysql_affected_rows
- * Returns count. Optionally closes the recordset
- *
- * @param recordset $rst
- * @param boolean $close
- * @return int
- */
- function umc_mysql_affected_rows($rst, $close=false) {
- $rowCount=$rst->fetchColumn();
- if ($close) {
- $rst->closeCursor();
- }
- return $rowCount;
- }
- /**
- * Replacement for mysql_insert_id
- *
- * @global PDO $UMC_DB
- * @return type integer
- */
- function umc_mysql_insert_id() {
- global $UNC_DB;
- return $UNC_DB['link']->lastInsertId();
- }
- /**
- * Replacement for mysql_fetch_array (MYSQL_ASSOC)
- * Returns one line of associative arrays
- *
- * @param type $rst
- * @return type
- */
- function umc_mysql_fetch_array($rst) {
- global $UNC_DB;
- if ($UNC_DB['XMPP_ERROR']) {XMPP_ERROR_trace(__FUNCTION__, func_get_args());}
- if (!$rst && $UNC_DB['XMPP_ERROR']) {
- XMPP_ERROR_trigger("tried fetch_array on erroneous recordset");
- return false;
- } else if (!$rst) {
- error_log("tried fetch_array on erroneous recordset");
- return false;
- }
- $row=$rst->fetch(PDO::FETCH_ASSOC);
- return $row;
- }
- /**
- * Replacement of mysql_free_result
- *
- * @param type $rst
- */
- function umc_mysql_free_result($rst) {
- $rst->closeCursor();
- }
- /**
- * Replacement of mysql_real_escape_string
- * ATTENTION: This also puts quotes around the value
- *
- * @global PDO $UMC_DB
- * @param type $value
- * @return type
- */
- function umc_mysql_real_escape_string($value) {
- global $UNC_DB;
- return $UNC_DB['link']->quote($value);
- }
- function umc_mysql_fetch_all($sql) {
- global $UNC_DB;
- if ($UNC_DB['XMPP_ERROR']) {XMPP_ERROR_trace(__FUNCTION__, func_get_args());}
- $stmt=$UNC_DB['link']->prepare($sql);
- if (!$stmt && $UNC_DB['XMPP_ERROR']) {
- $error=$UNC_DB['link']->errorInfo();
- XMPP_ERROR_trigger($error);
- return false;
- } else if (!$stmt) {
- error_log($UNC_DB['link']->errorInfo());
- return false;
- } else {
- $stmt->execute();
- }
- $result=$stmt->fetchAll(PDO::FETCH_ASSOC);
- return $result;
- }
- umc_wp_forum_import()
- ?>
Advertisement
Add Comment
Please, Sign In to add comment