<?php
/*
forum_post_tableid
pre_forum_post
*/

if($start <= 1 && $tableid == 0) {
	$posttables = $comma = '';
	
	$query_1 = $xc->db['source']->query("SELECT id FROM {$source_tablepre}tablelist");
	while ($list = $xc->db['source']->fetch_assoc($query_1)){
		$tb['id'] = $list['id'];
		$posttables .= $comma.'posts'.$tb['id'];
		$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];

/*
print_r($tablearray);echo "<br>";
echo "tableid=".$tableid."<br>";
echo "posttable=".$posttable."<br>";
file_put_contents("yh0620.txt", "posttable=".$posttable."\r\n", FILE_APPEND);
*/

if($start <= 1 && $tableid == 0) {
	truncatetable('forum_post_tableid');
	truncatetable('forum_post');
	//validid('pid', $tablearray[$tableid]);
}
$maxpid = $xc->db['source']->result($xc->db['source']->query("SELECT MAX(pid) as maxpid FROM {$source_tablepre}$posttable "), 0, "MAXPID");
if($start <= $maxpid ) {
	$converted = 1;
}

$query = $xc->db['source']->query("SELECT * FROM {$source_tablepre}$posttable WHERE pid BETWEEN $start AND $end") or die("检索数据表 '$posttable' 错误！请将表 '$posttable' 字段类型为 nText 的改为 Text 类型。");

while ($p = $xc->db['source']->fetch_assoc($query)) {
	$p = daddslashes(array_change_key_case($p));

	$pid		= $p['pid'];
	$fid		= $p['fid'];
	$tid		= $p['tid'];
	$first		= $p['layer'] == 0 ? 1 : 0;
	$author		= $p['poster'];
	$authorid	= $p['posterid'];
	$subject	= $p['title'];
	$dateline	= timetounix($p['postdatetime']);
	$message	= convertbbcode($p['message']);
	
	$useip		= $p['ip'];
	$invisible	= $p['invisible'];
	$anonymous	= 0;
	$usesig		= $p['usesig'];
	$htmlon		= $p['htmlon'];
	$bbcodeoff	= $p['bbcodeoff'];
	$smileyoff	= $p['smileyoff'];
	$parseurloff= $p['parseurloff'];
	$attachment = str_replace('\\\\', '/', $p['attachment']);
	$rate		= $p['rate'];
	$ratetimes	= $p['ratetimes'];
	$status		= 0;
	$tags		=	'';
	$comment	=	0;
	$replycredit =	0;
	$position	=	$xc->db['discuz']->result($xc->db['discuz']->query("SELECT maxposition FROM {$discuz_tablepre}forum_thread WHERE tid='{$p[tid]}'"));
	$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='$p[pid]'");

		$fields2 = 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", $fields2);

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

		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 ");
	}
	$converted = 1;
	$totalrows ++;
	if($multitables){
		$url ='index.php?mod=dealdata&step='.$step.'&start='.($p['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='.($p['pid']+1).'&stay='.$stay.'&totalrows='.$totalrows.'&convertedrows='.$convertedrows.'&sqlfilenum='.$sqlfilenum;
	}
	file_put_contents(XCONVERT_ROOT.'./cache/lock/progress_'.$step.".txt", $url);
}

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

?>