<?php
/*
转换回复及附件
pre_forum_post
pre_forum_postcomment
pre_forum_ratelog
*/

if($start <= 1 && $tableid == 0) {
	truncatetable('forum_post_tableid');
	truncatetable('forum_post');
	truncatetable('forum_ratelog');
	
	$ptblquery = $xc->db['source']->query("SELECT db_value FROM {$source_tablepre}config WHERE db_name = 'db_plist'");
	$t_result = $xc->db['source']->result($ptblquery);
	$t_list = unserialize($t_result);
	if(empty($t_list)){
		$tablelist = ','.$t_result;
		$maxtableid = substr($tablelist, (strrpos($tablelist, ',') + 1));
	} else {
		$tablelist = $t_list;
		$maxtableid = is_array($tablelist) ? (count($tablelist)-1) : 0;
	}

	$sqlfilenum = 0;
	$extrastr = '';
}

$sqlfilenum ++;
$extrastr = '&sqlfilenum='.$sqlfilenum;

$tableid = isset($tableid) ? $tableid : 0;
$maxtableid = isset($maxtableid) ? $maxtableid : 0;
$multitables = $maxtableid > 0 ? 1 : 0;
$tnum = $tableid == 0 ? '' : $tableid;
$maxpid = $xc->db['source']->result($xc->db['source']->query("SELECT max(pid) FROM {$source_tablepre}posts{$tnum}"), 0);
if($start < $maxpid){
	$converted = 1;
}

$query = $xc->db['source']->query("SELECT * FROM {$source_tablepre}posts{$tnum} WHERE pid >= $start AND pid < $start + $rpp") or dexit();
while ($pwpost = $xc->db['source']->fetch_array($query)) {
	$pwpost			=	daddslashes($pwpost);
	//pre_forum_post
	$pid			=	$pwpost['pid'];
	$fid			=	$pwpost['fid'];
	$tid			=	$pwpost['tid'];
	$first			=	0;
	$author			=	cutstr(htmlspecialchars(trim($pwpost['author'])), 15);
	$authorid		=	$pwpost['authorid'];
	$subject		=	daddslashes(cutstr(htmlspecialchars(trim(@strip_tags($pwpost['subject']))), 78));//formatstr去除字符串结尾的'\'
	$dateline		=	$pwpost['postdate'];
	$message		=	daddslashes(convertbbcode($pwpost['content'] ? $pwpost['content'] : htmlspecialchars(trim($pwpost['subject']))));//formatstr去除字符串结尾的'\'
	$useip			=	$pwpost['userip'];
	$invisible		=	0;
	$anonymous		=	$pwpost['anonymous'];
	$usesig			=	$pwpost['ifsign']%2 == 0 ? 0 : 1;
	$htmlon			=	$pwpost['ifsign'] < 2 ? 0 : 1;
	$bbcodeoff		=	0;
	$smileyoff		=	0;
	$parseurloff	=	0;
	$attachment		=	$pwpost['aid'] ? 1 : 0;
	$rate			=	0;
	$ratetimes		=	0;
	$status			=	$pwpost['ifshield'];
	$tags			=	'';
	$comment		=	0;
	$position		=	$xc->db['discuz']->result_first("SELECT maxposition FROM {$discuz_tablepre}forum_thread WHERE tid='{$pwpost[tid]}'");
	$position		=	$position ? $position + 1 : 2;
	$rushthread		=	$xc->db['source']->result_first("SELECT tpcstatus FROM {$source_tablepre}threads WHERE tid='$tid'");
	$rushthread		=	($rusthread & pow(2, 7)) ? 1 : 0;
	if(empty($tid) && empty($fid)) {
		$tid		= $xc->db['source']->result_first("SELECT tid FROM {$source_tablepre}postsfloor WHERE pid='$pid'");
		$fid		= $xc->db['source']->result_first("SELECT fid FROM {$source_tablepre}threads WHERE tid='$tid'");
		$invisible	= 0;
		$status		= 1;
	}
	
    
    /* modified yanghuan */
    $replyrewardrecord = $xc->db['source']->result($xc->db['source']->query("SELECT creditnum FROM {$source_tablepre}replyrewardrecord WHERE pid='$pid'"), 0);
    $replycredit = $replyrewardrecord > 0 ? intval($replyrewardrecord) : 0;
    /* modified yanghuan */

	$field1 = array('pid');
	$query1 = getinsertsql("{$discuz_tablepre}forum_post_tableid", $field1);

	if($rushthread) {
		$position = $xc->db['source']->result_first("SELECT floor FROM {$source_tablepre}robbuildfloor WHERE tid='$tid' AND pid='$pid'");
	}
	$field2 = array('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');
	$query2 = getinsertsql("{$discuz_tablepre}forum_post", $field2);

	$query4 = $xc->db['discuz']->query("UPDATE {$discuz_tablepre}forum_thread SET maxposition='{$position}' WHERE tid='{$pwpost[tid]}'");

	if($xc->config['info']['convert_type'] == 'file') {
		if($xc->db['discuz']->query($query1)) {
			writesql('forum_post', $sqlfilenum, $query2);
			$convertedrows ++;
		} else {
			reportlog("无法转换帖子 pid=$pid");
		}
	} else {
		if($xc->db['discuz']->query($query1)) {
			if ($xc->db['discuz']->query($query2)) {
				$convertedrows++;
			} else {
				$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}forum_post_tableid WHERE pid='$pid'");
				reportlog("无法转换帖子 pid = $pid subject = '$subject'");
			}
		} else {
			reportlog("无法转换帖子 pid=$pid");
		}
	}
	$converted = 1;
	$totalrows ++;
	if($multitables){
		$url ='index.php?mod=dealdata&step='.$step.'&start='.($pwpost['pid']+1).'&stay='.$stay.'&totalrows='.$totalrows.'&convertedrows='.$convertedrows.'&tableid='.$tableid.'&maxtableid='.$maxtableid.'&sqlfilenum='.$sqlfilenum.'&posttables='.$posttables;
	} else {
		$url = 'index.php?mod=dealdata&step='.$step.'&start='.($pwpost['pid']+1).'&stay='.$stay.'&totalrows='.$totalrows.'&convertedrows='.$convertedrows.'&sqlfilenum='.$sqlfilenum;
	}
	file_put_contents(XCONVERT_ROOT.'./cache/lock/progress_'.$step.".txt", $url);
}

if($converted || $end < $maxid) {
	$nowtableid = $tableid + 1;

} elseif($tableid < $maxtableid) {
	$converted = 1;
	$nextid = $tableid + 1;
	$nexttableid = $tableid + 2;
	unset($start);
	unset($end);
}
?>
