<?php
/*
家园评论：pre_home_comment
*/
if($start <= 1) {
	truncatetable('home_comment');
}
$maxcomid = $xc->db['source']->result($xc->db['source']->query("SELECT max(id) FROM {$source_tablepre}comment"), 0);
if($start < $maxcomid) {
	$converted = 1;
}
$forxgrouptid = $xc->db['discuz']->result($xc->db['discuz']->query("SELECT svalue FROM {$discuz_tablepre}common_setting WHERE skey = 'forxgrouptid'"), 0);
$query = $xc->db['source']->query("SELECT * FROM {$source_tablepre}comment WHERE type != 'write' AND id >= $start AND id < $start + $rpp") or dexit();
while ($pwcomment = $xc->db['source']->fetch_array($query)) {
	$pwcomment		=	daddslashes($pwcomment);

	$cid			=	$pwcomment['id'];
	$uid			=	$pwcomment['uid'];
	$id				=	$pwcomment['typeid'];
	switch($pwcomment['type']) {
		case 'diary':
			$idtype = 'blogid';
			break;
		case 'photo':
			$idtype = 'picid';
			break;
		case 'share':
			$idtype = 'sid';
	}
	$authorid		=	$pwcomment['uid'];
	$author			=	$pwcomment['username'];
	$ip				=	'';
	$dateline		=	$pwcomment['postdate'];
	$message		=	$pwcomment['title'];
	$magicflicker	=	0;

	$field1 = array('cid','uid','id','idtype','authorid','author','ip','dateline','message','magicflicker');
	$query1 = getinsertsql("{$discuz_tablepre}home_comment", $field1);

	if($pwcomment['type'] == 'groupwrite') {
		$tid			=	$forxgrouptid + $pwcomment['typeid'];
		$fid			=	$xc->db['discuz']->result($xc->db['discuz']->query("SELECT fid FROM {$discuz_tablepre}forum_thread WHERE tid = '{$tid}'"), 0);
		$first			=	0;
		$author			=	$pwcomment['username'];
		$authorid		=	$pwcomment['uid'];
		$subject		=	daddslashes(cutstr(htmlspecialchars(trim(@strip_tags($pwcomment['title']))), 78));
		$dateline		=	$pwcomment['postdate'];
		$message		=	convertbbcode($pwcomment['title']);
		$useip			=	'';
		$invisible		=	0;
		$anonymous		=	0;
		$usesig			=	1;
		$htmlon			=	0;
		$bbcodeoff		=	'-1';
		$smileyoff		=	'-1';
		$parseurloff	=	0;
		$attachment		=	0;
		$rate			=	0;
		$ratetimes		=	0;
		$status			=	0;
		$tags			=	0;
		$comment		=	0;

		$field2 = array('pid');
		$query2 = getinsertsql("{$discuz_tablepre}forum_post_tableid", $field2);

		if($xc->db['discuz']->query($query2)) {
			$pid = $xc->db['discuz']->insert_id();
			$field1 = array('pid','fid','tid','first','author','authorid','subject','dateline','message','useip','invisible','anonymous','usesig','htmlon','bbcodeoff','smileyoff','parseurloff','attachment','rate','ratetimes','status','tags','comment');
			$query1 = getinsertsql("{$discuz_tablepre}forum_post", $field1);
		}
		$xc->db['discuz']->query("UPDATE {$discuz_tablepre}forum_thread SET replies=replies+'1' WHERE tid='{$tid}' LIMIT 1");
		$pid = 0;
	}
	if ($xc->db['discuz']->query($query1)) {
		$convertedrows ++;
	} else {
		reportlog("无法转换评论 id = $cid ，SQL 语句如下 ：<br><textarea rows=\"3\" style=\"width: 100%;\">".$query1."</textarea>");
	}
	$converted = 1;
	$totalrows ++;
	$url = 'index.php?mod=dealdata&step='.$step.'&start='.($pwcomment['id']+1).'&stay='.$stay.'&totalrows='.$totalrows.'&convertedrows='.$convertedrows.'&sqlfilenum='.$sqlfilenum;
	file_put_contents(XCONVERT_ROOT.'./cache/lock/progress_'.$step.".txt", $url);
}
?>