本篇讲的是jforum将帖子和主题同步至discuz中,其他开源论坛也可使用。
<?php // sync_threads(); sync_posts(); // update_forum_id(); // insert_common_stat(); // update_member_count(); // update_post_detail(); // update_thread_detail(); /** * update last post time and author name on every thread */ function update_thread_detail(){ global $_local_conn, $_new_db_name, $_old_db_name; $tidresult = mysql_query("SELECT tid, authorid FROM $_new_db_name.pre_forum_thread", $_local_conn); while ($thread = mysql_fetch_object($tidresult)) { /* * Get recent post from posts under same forum */ $result = mysql_query("SELECT dateline, author FROM $_new_db_name.pre_forum_post WHERE tid = $thread->tid ORDER BY dateline desc LIMIT 1", $_local_conn); $lastpost = mysql_fetch_array($result); $username = getusername($thread->authorid); mysql_query("UPDATE $_new_db_name.pre_forum_thread SET lastpost = $lastpost[dateline], author = '$username', lastposter = '$lastpost[author]' WHERE tid = $thread->tid" , $_local_conn); } } function update_post_detail(){ global $_local_conn, $_new_db_name, $_old_db_name; $authorresult = mysql_query("SELECT pid, authorid FROM $_new_db_name.pre_forum_post", $_local_conn); while($author = mysql_fetch_object($authorresult)){ $username = getusername($author->authorid); mysql_query("UPDATE $_new_db_name.pre_forum_post SET author = '$username' WHERE pid = $author->pid" , $_local_conn); } } function update_member_count(){ global $_local_conn, $_new_db_name, $_old_db_name; $resultofthreadbyuser = mysql_query("SELECT count(1) AS threadsnum, authorid, (SELECT COUNT(1) FROM $_new_db_name.`pre_forum_post` post WHERE post.authorid = thread.authorid) AS postsnum" ." FROM $_new_db_name.`pre_forum_thread` thread GROUP BY thread.authorid", $_local_conn); while($threadbyuser = mysql_fetch_object($resultofthreadbyuser)){ $sql = "UPDATE $_new_db_name.`pre_common_member_count` SET posts = $threadbyuser->postsnum, threads = $threadbyuser->threadsnum WHERE uid = $threadbyuser->authorid"; mysql_query($sql, $_local_conn); } } /** * insert pre_common_stat and update thread num */ function insert_common_stat(){ global $_local_conn, $_new_db_name, $_old_db_name; $from_date = "20120731"; while($from_date < date("Ymd")){ $date = date_create($from_date); date_add($date, date_interval_create_from_date_string("1 days")); $from_date = date_format($date, "Ymd"); // echo strtotime($from_date)."/".(strtotime($from_date)-86400); $resultofthreadsbydate = mysql_query("SELECT COUNT(1) AS threadnum FROM $_new_db_name.pre_forum_thread WHERE dateline > ".strtotime($from_date)." AND dateline < ".(strtotime($from_date)+86400), $_local_conn); $threadbydate = mysql_fetch_array($resultofthreadsbydate); // echo "SELECT COUNT(1) AS threadnum FROM $_new_db_name.pre_forum_thread WHERE dateline > ".strtotime($from_date)." AND dateline < ".(strtotime($from_date)+86400); $sql = "INSERT INTO $_new_db_name.`pre_common_stat` (`daytime`, `login`, `mobilelogin`, `connectlogin`, `register`, `invite`, `appinvite`, `doing`, `blog`, `pic`, `poll`, `activity`, `share`, `thread`, `docomment`, `blogcomment`, `piccomment`, `sharecomment`, `reward`, `debate`, `trade`, `group`, `groupjoin`, `groupthread`, `grouppost`, `post`, `wall`, `poke`, `click`, `sendpm`, `friend`, `addfriend`) " ."VALUES ($from_date, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, $threadbydate[threadnum], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)"; mysql_query($sql, $_local_conn); } } function sync_posts(){ insert_posts(); } function insert_posts(){ global $_local_conn, $_new_db_name, $_old_db_name; $_query_old_posts_sql = "SELECT * FROM $_old_db_name.jforum_posts posts, $_old_db_name.jforum_posts_text text WHERE posts.post_id = text.post_id"; $_old_posts = mysql_query($_query_old_posts_sql, $_local_conn); while(($old_post = mysql_fetch_object($_old_posts))){ $_dateline = strtotime($old_post->post_time); $sql = "INSERT INTO $_new_db_name.`pre_forum_post` (`pid` ,`fid` ,`tid` ,`first` ,`author` ,`authorid` ". ",`subject` ,`dateline` ,`message` ,`useip` ,`invisible` ,`anonymous` ,`usesig` ,`htmlon` ,`bbcodeoff`". " ,`smileyoff` ,`parseurloff` ,`attachment` ,`rate` ,`ratetimes` ,`status` ,`tags` ,`comment` ,`replycredit` ,`position`)" ."VALUES ('$old_post->post_id', '$old_post->forum_id', '$old_post->topic_id', '1', '', '$old_post->user_id', '$old_post->post_subject'," ."$_dateline, '$old_post->post_text', '$old_post->post_ip', '0', '0', '1', '1', '-1', '-1', '0', '0', '0', '0', '0', '', '0', '0', NULL)"; mysql_query($sql, $_local_conn); /* * insert into pre_forum_post_tableid, when insert post */ mysql_query("INSERT INTO $_new_db_name.`pre_forum_post_tableid` VALUES ($old_post->post_id)", $_local_conn); } } function sync_threads(){ insert_threads(); } function update_forum_id(){ global $_local_conn, $_new_db_name, $_old_db_name; /* * update forums */ $forums = getForums(); $jforums = getJforums(); while($forum = mysql_fetch_object($forums)){ while($jforum = mysql_fetch_object($jforums)){ if($forum->name == $jforum->forum_name){ /* * update forum id in table thread */ mysql_query("UPDATE $_new_db_name.pre_forum_thread SET fid = $forum->fid WHERE fid = $jforum->forum_id", $_local_conn); //echo "更新主题表中的板块id信息"; /* * update forum id in table post */ mysql_query("UPDATE $_new_db_name.pre_forum_post SET fid = $forum->fid WHERE fid = $jforum->forum_id", $_local_conn); /* * update pre_forum_forum, forum detail */ $thread_num_result = mysql_query("SELECT count(1) AS thread_num FROM $_new_db_name.pre_forum_thread WHERE fid = $forum->fid", $_local_conn); $thread_num = mysql_fetch_array($thread_num_result); $post_num_result = mysql_query("SELECT count(1) AS post_num FROM $_new_db_name.pre_forum_post WHERE fid = $forum->fid", $_local_conn); $post_num = mysql_fetch_array($post_num_result); $post_message_result = mysql_query("SELECT message FROM $_new_db_name.pre_forum_post WHERE fid = $forum->fid ORDER BY dateline DESC LIMIT 1", $_local_conn); $post_message = mysql_fetch_array($post_message_result); $sub_message = substr($post_message[message], 0, 29); mysql_query("UPDATE $_new_db_name.pre_forum_forum SET threads = $thread_num[thread_num], posts = $post_num[post_num], lastpost = '$sub_message'" ." WHERE fid = $forum->fid" , $_local_conn); } } mysql_data_seek($jforums, 0); //put jforum db result index back to 0 } } /** * insert threads only sql */ function insert_threads(){ global $_local_conn, $_new_db_name, $_old_db_name; //create a query $sql = "SELECT * FROM discuz.pre_common_member limit 0, 10"; $queryJforumTopics = "SELECT * FROM jforum1.jforum_topics "; $result = mysql_query($queryJforumTopics, $_local_conn); ////insert new threads while (($row = mysql_fetch_object($result))){ //print "$row->topic_id<br />"; $_dateline = strtotime($row->topic_time); $sql = "INSERT INTO discuz.pre_forum_thread (`tid` ,`fid` ,`posttableid` ,`typeid` ,`sortid` ,`readperm` ,`price` ,`author` ,`authorid` ,`subject` ,". "`dateline` ,`lastpost` ,`lastposter` ,`views` ,`replies` ,`displayorder` ,`highlight` ,`digest` ,`rate` ,`special` ,`attachment` ,`moderated` ,`closed` ,". "`stickreply` ,`recommends` ,`recommend_add` ,`recommend_sub` ,`heats` ,`status` ,`isgroup` ,`favtimes` ,`sharetimes` ,`stamp` ,`icon` ,`pushedaid` ,`cover` ,". "`replycredit` ,`relatebytag` ,`maxposition`)". " VALUES ('$row->topic_id' , '$row->forum_id', '0', '0', '0', '0', '0', '', '$row->user_id', '$row->topic_title', $_dateline, UNIX_TIMESTAMP(), '', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '32', '0', '0', '0', '-1', '-1', '0', '0', '0', '0', '0')" ; mysql_query($sql, $_local_conn); } } function getusername($userid){ global $_local_conn, $_new_db_name, $_old_db_name; $finduseridsql = "SELECT username FROM $_new_db_name.pre_common_member WHERE uid = $userid"; $userresult = mysql_query($finduseridsql, $_local_conn); $user = mysql_fetch_array($userresult); return $user[username]; } /** * get new forum detail * @return resource */ function getForums(){ global $_local_conn, $_new_db_name, $_old_db_name; $sql = "SELECT * FROM $_new_db_name.pre_forum_forum WHERE type = 'forum' and status = '1'"; $forums = mysql_query($sql, $_local_conn); return $forums; } /** * get old forum detail. <in jforum> * @return resource */ function getJforums(){ global $_local_conn, $_new_db_name, $_old_db_name; $sql = "SELECT * FROM $_old_db_name.jforum_forums "; $jforums = mysql_query($sql, $_local_conn); return $jforums; } ?>
按照上面的function执行顺序执行即可。涉及到需要改动的table有pre_forum_thread,pre_forum_post,pre_forum_forum,pre_common_stat,pre_common_member_count,pre_forum_post_tableid
详见:http://www.gforetell.com/?/question/188#reply1
同步后出现一个问题。在论坛里发主题的时候会报主键重复错误,查看后得知。2.5版本的pre_forum_post里面pid不是自动增长的,在做插入操作的时候会从pre_forum_post_tableid里面取得最大的+1,改了下最大的就解决了。
之后又出现了一个问题。jforum里面的帖子内容是包含html标签的,但是直接同步到discuz后会直接在页面上出现标签。需要在管理端的“论坛”-“板块管理”-“帖子选项”中将相应版块的“允许使用HTML代码”开启。