<?php
/*
forum_post_tableid
pre_forum_post
*/

if($start <= 1 && $tableid == 0) {
	$posttables = $comma = '';
	$tablequery = $xc->db['source']->query("SELECT TableName FROM {$source_tablepre}TableList");
	while ($tb = $xc->db['source']->fetch_assoc($tablequery)) {
		$tb['TableName'] = substr($tb['TableName'], 0, 3) == 'Dv_' || substr($tb['TableName'], 0, 3) == 'dv_' ? substr($tb['TableName'], 3) : $tb['TableName'];

		$posttables .= $comma.$tb['TableName'];
		$comma = ',';
	}
}

$tablearray = explode(',', $posttables);
if(isset($tablearray[0])) {
	$maxtableid = is_array($tablearray) ? count($tablearray)-1 : 0;
} else {
	$maxtableid = is_array($tablearray) ? count($tablearray) : 0;
}

$tablecount = count($tablearray);
$tableid = isset($tableid) ? $tableid : 0;
$maxtableid = isset($maxtableid) ? $maxtableid : 0;
$multitables = $maxtableid > 0 ? 1 : 0;
$posttable = $tablearray[$tableid];

if($start <= 1 && $tableid == 0) {
	truncatetable('forum_post_tableid');
	truncatetable('forum_post');
	validid('announceid', $tablearray[$tableid]);
}
$query = $xc->db['source']->query("SELECT max(AnnounceID) FROM {$source_tablepre}{$posttable}");
$maxid = $xc->db['source']->fetch_array($query);

$query = $xc->db['source']->query("SELECT b.AnnounceID,b.ParentID,b.BoardID,b.RootID,b.UserName,b.PostUserid,b.Topic, b.isupload,b.DateAndTime,b.ip,b.locktopic,b.signflag,cast(b.Body AS TEXT) AS Body,t.issmstopic FROM {$source_tablepre}{$posttable} b LEFT OUTER JOIN {$source_tablepre}Topic t ON t.TopicID=b.RootID WHERE b.AnnounceID BETWEEN $start AND $end;") or dexit("检索数据表 '$posttable' 错误！<br>请将帖子表 '$posttable' 字段类型为 nText 的改为 Text 类型。", 'mssql');
while ($dvp = $xc->db['source']->fetch_assoc($query)) {
	$dvp = array_change_key_case(daddslashes($dvp));

	//pre_forum_post
	//$pid	=	$dvp['announceid'];
	$fid	=	$dvp['boardid'];
	$tid	=	$dvp['rootid'];

	$atid			=	(string)$tid;
	$tableid		=	$atid{strlen($atid)-1};

	$first	=	$dvp['parentid'] == 0 ? 1 : 0;
	$author	=	$dvp['username'];
	$authorid	=	$dvp['postuserid'];
	$subject	=	cutstr(@strip_tags(trim($dvp['topic'])), 78);
	$dateline	=	timetounix($dvp['dateandtime']);
	$message	=	convertbbcode($dvp['body']);
	$useip		=	$dvp['ip'];
	$invisible	=	0;
	$anonymous	=	0;
	$usesig		=	1;
	$htmlon		=	@strip_tags($message) == $message ? 0 : 1;
	$bbcodeoff	=	0;
	$smileyoff	=	0;
	$parseurloff	=	0;
	$attachment	=	intval($dvp['isupload']);
	$rate		=	0;
	$ratetimes	=	0;
	$status		=	$dvp['locktopic'] == 2 ? 1 : 0;
	$tags		=	'';
	$comment	=	0;
	$replycredit	=	0;
	$position		=	$xc->db['discuz']->result_first("SELECT maxposition FROM {$discuz_tablepre}forum_thread WHERE tid='{$dvp[rootid]}'");
	$position	=	$first ? 1 : ($position ? $position + 1 : 2);

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

	if($xc->db['discuz']->query($query1)) {
		$pid = $xc->db['discuz']->insert_id();

		$xc->db['discuz']->query("UPDATE {$discuz_tablepre}forum_attachment set pid='$pid' WHERE tid='$tid' AND pid='$dvp[announceid]'");
		$xc->db['discuz']->query("UPDATE {$discuz_tablepre}forum_attachment_$tableid set pid='$pid' WHERE tid='$tid' AND pid='$dvp[announceid]'");

		$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='{$dvp[rootid]}'");

		unset($pid);
		if ($xc->db['discuz']->query($query2)) {
			$convertedrows ++;
		} else {
			$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}forum_post_tableid WHERE pid='$pid'");
			reportlog("转换帖子出错 tid=$tid pid=$pid ");
		}
	} else {
		reportlog("转换帖子出错 pid=$pid ");
	}
	$totalrows ++;
	if($multitables){
		$url ='index.php?mod=dealdata&step='.$step.'&start='.($dvp['AnnounceID']+1).'&stay='.$stay.'&totalrows='.$totalrows.'&convertedrows='.$convertedrows.'&tableid='.$tableid.'&maxtableid='.$maxtableid.'&sqlfilenum='.$sqlfilenum.'&posttables='.$posttables;
	} else {
		$url = 'index.php?mod=dealdata&step='.$step.'&start='.($dvp['AnnounceID']+1).'&stay='.$stay.'&totalrows='.$totalrows.'&convertedrows='.$convertedrows.'&sqlfilenum='.$sqlfilenum;
	}
	file_put_contents(XCONVERT_ROOT.'./cache/lock/progress_'.$step.".txt", $url);
}
if( $end < $maxid ) {
	$converted = 1;
}
if($converted) {
	$nowtableid = $tableid + 1;
} elseif($tableid < $maxtableid) {
	$converted = 1;
	$nextid = $tableid + 1;
	$nexttableid = $tableid + 2;
	unset($start);
	unset($end);
}

?>