`
kililanxilu
  • 浏览: 24541 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

jforum同步帖子主题至discuzx2.5

阅读更多

本篇讲的是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代码”开启。

分享到:
评论

相关推荐

    JForum论坛数据库表结构.rar

    16、帖子内容表JFORUM_POSTS_TEXT 6 17、消息表JFORUM_PRIVMSGS 6 18、消息内容表JFORUM_PRIVMSGS_TEXT 6 19、配置限定表JFORUM_QUOTA_LIMIT 7 20、会员等级表JFORUM_RANKS 7 21、角色表JFORUM_ROLES 7 22、...

    Jforum数据库

    JForum - Java论坛系统 数据库表结构,如:附件关系表JFORUM_ATTACH,附件详细信息表JFORUM_ATTACH_DESC,用户分组表JFORUM_GROUPS等表结构说明,如帖子表JFORUM_POSTS说明: 名称 字段名 字段类型 字段说明 帖子...

    JForum3 jforum java 开源论坛 论坛

    JForum3 jforum java 开源论坛 论坛

    jforum3.0所需要的jar包

    jforum3.0所需要的jar包

    Jforum二次开发成果

    Jforum二次开发成果 Jforum 二次开发 eclipse 给予JForum 2.1.8开发的一个小论坛。 增加了很多功能。(子论坛、主页、热门帖子、推荐帖子等)

    jforum功能说明

    JForum是著名的开源论坛,支持多达数十种的多国语言,其中包括简体中文。JForum功能强大,界面美观,加上代码结构清晰,而且采用的是BSD授权,不必担心不必要的版权纠纷。可以说JForum是论坛二次开发的绝佳选择。 ...

    Jforum论坛数据库架构

    Jforum论坛数据库架构Jforum论坛数据库架构

    jforum漏洞利用源代码

    jforum漏洞利用源代码

    jforum开源的论坛文档

    jforum开源的论坛文档jforum开源的论坛文档jforum开源的论坛文档

    JForum3 完整项目

    jforum 完整源代码,从svn上 导出 没有任何修改

    JForum数据库ER图

    JForum 开源论坛系统的 数据库ER图,很详细。

    JForum2.6.2.rar

    1、包含jforum2.6.2的war包、源码包 2、war包可直接放在tomcat的webapps目录下 3、2.6.2版本里面自带汉化功能,在http://localhost:8080/jforum/install.jsp安装时,注意选择中文

    JForum 2.1.9 源码包.zip

    JForum 是采用Java开发的功能强大且稳定的论坛系统。它提供了抽象的接口、高效的论坛引擎以及易于使用的管理界面,同时具有完全的权限控制、多语言支持(包括中文)、高性能、可自定义的用户接口、安全、支持多...

    jforum安装部署指南

    jforum安装部署指南:修改对应的数据库文件JFORUM\WEB-INF\config\database下面。例如:mysql.properties修改其中的数据库信息

    jforum3源代码 数据库

    jforum3源代码数据库

    jforum说明文档 源码解析 单点登录 jforum缓存

    jforum的开发文档,以及源码解析 数据库 重要配置文件和包,缓存 权限控制 单点登录

    jforum论坛图片资源

    用于配置中文jforum,里面包含各种语言的图片文件,将其中的zh_CN目录复制到jforum安装目录下的templates\default\images。就可以显示中文图片

    JForum_SSO_-_JForum单点登陆原理与配置

    对于我们已有的WEB应用中的用户,若该用户已经登陆,并通过 联结迁移到JForum页面时,JForum要能够识别该用户已经登陆(不需要二次登陆)才不会让用户感到别扭(对用户来说,就好像使用的是同一个系统似的)。...

    jforum+ckeditor整合案例

    jforum与ckeditor整合,替换自带的编辑器

    jforum 源码

    JForum 开源代码,是很好的freeMaker学习素材。

Global site tag (gtag.js) - Google Analytics