<?php

if($start <= 1) {	
	truncatetable('forum_thread');
	truncatetable('forum_threadrush');
	truncatetable('forum_replycredit');
	validid('tid', 'topics');
}

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

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

	$tid			= $t['tid'];
	$fid			= $t['fid'];
	$posttableid		=	0;
	$temptypeid		= $t['typeid'];
	$typename = $xc->db['source']->result($xc->db['source']->query("SELECT name as typename FROM {$source_tablepre}topictypes WHERE typeid = $temptypeid"), 0, "TYPENAME");
	$typeid	= $xc->db['discuz']->result($xc->db['discuz']->query("SELECT typeid as typeid FROM {$discuz_tablepre}forum_threadclass WHERE fid = $fid and name = '$typename'"), 0, "TYPEID");
	$sortid			= 0;
	$readperm		= $t['readperm'];
	$price			= $t['price'];
	$author			= $t['poster'];
	$authorid		= $t['posterid'];
	$subject		= $t['title'];
	$dateline		= timetounix($t['postdatetime']);
	$lastpost		= timetounix($t['lastpost']);
	$lastposter		= $t['lastposter'];
	$views			= $t['views'];
	$replies		= $t['replies'];
	$displayorder	= $t['displayorder'];
	$highlight		= $t['highlight'];
	$digest			= $t['digest'];
	$rate			= $t['rate'];
	switch ($t['special']) {
		case 0:			//普通
			$special = 0;
			break;
		case 1:			//投票
			$special = 1;
			break;
		case 2:			//悬赏
			$special = 3;
			break;
		case 3:			//悬赏
			$special = 3;
			break;
		case 4:			//辩论
			$special = 5;
			break;
	}
	$attachment		= $t['attachment'];
	$moderated		= $t['moderated'];
	$closed			= $t['closed'];
	$recommends	= '';
	$recommend_add	= '';
	$recommend_sub	= '';
	$heats		= 0;
	$status		= 0;

	$fields1 = array('tid', 'fid', 'typeid', 'readperm', 'price', 'author', 'authorid', 'subject', 'dateline', 'lastpost', 'lastposter', 'views', 'replies', 'displayorder', 'highlight', 'digest', 'rate', 'special', 'attachment', 'moderated', 'closed', 'recommends', 'recommend_add', 'recommend_sub', 'heats', 'status');
	$query1 = getinsertsql("{$discuz_tablepre}forum_thread", $fields1);
	if ($xc->db['discuz']->query($query1)) {
		$convertedrows ++;
	} else {
		reportlog("插入 DZ 论坛主题数据出错 tid = $tid subject = $subject");
	}
	$converted = 1;
	$totalrows ++;
	$url = 'index.php?mod=dealdata&step='.$step.'&start='.($t['tid']+1).'&stay='.$stay.'&totalrows='.$totalrows.'&convertedrows='.$convertedrows.'&sqlfilenum='.$sqlfilenum;
	file_put_contents(XCONVERT_ROOT.'./cache/lock/progress_'.$step.".txt", $url);
}

?>
