<?php
/*
pre_forum_thread
*/
if($start <= 1) {
	truncatetable('forum_thread');
	validid('topicid', 'topic');
}
$query = $xc->db['source']->query("SELECT max(TopicID) FROM {$source_tablepre}Topic");
$maxid = $xc->db['source']->fetch_array($query);
$query = $xc->db['source']->query("SELECT * FROM {$source_tablepre}Topic WHERE TopicID BETWEEN $start AND $end") or dexit("检索数据表 '{$source_tablepre}Topic' 错误！<br>请将主题表 '{$source_tablepre}Topic' 字段类型为 nText 的改为 Text 类型。", 'mssql');

while($dvt = mssql_fetch_assoc($query)) {
	$dvt = array_change_key_case(daddslashes($dvt));

	$lp		=	explode('$', $dvt['lastpost']);

	//主题主表
	//pre_forum_thread
	$tid			=	$dvt['topicid'];
	$fid			=	$dvt['boardid'];
	$posttableid	=	0;
	$typeid			=	$dvt['mode'];
	$sortid			=	'';
	$readperm		=	0;
	$price			=	0;
	$author			=	cutstr(htmlspecialchars(trim($dvt['postusername'])), 15);
	$authorid		=	$dvt['postuserid'];
	
	$subject		=	cutstr(htmlspecialchars(trim(@strip_tags($dvt['title']))), 78);
	$subject		=	$subject ? $subject : 'no subject';
	
	$dateline		=	timetounix($dvt['dateandtime']);
	$dateline		=	$dateline > $timestamp ? $timestamp : $dateline;

	$lastpost		=	timetounix($dvt['lastposttime']);
	$lastpost		=	$lastpost > $timestamp ? $timestamp : $lastpost;

	$lastposter		=	cutstr(htmlspecialchars(trim($lp[0])), 15);
	$views			=	$dvt['hits'];
	$replies		=	$dvt['child'];
	$displayorder	=	$dvt['istop'];
	$highlight		=	0;
	$digest			=	$dvt['isbest'];
	$rate			=	0;
	$special 		=	$dvt['pollid'] > 0 ? 1 : 0;
	$attachment		=	0;
	$moderated		=	0;
	$closed			=	0;
	$stickreply		=	0;
	$recommends		=	0;
	$recommend_add	=	0;
	$recommend_sub	=	0;
	$heats			=	0;
	$status			=	0;
	$isgroup		=	0;

	if($special == 1) {
	} elseif($special == 2) {
	} elseif($special == 3) {
		$query_3 = $xc->db['source']->query("SELECT cbtype FROM {$source_tablepre}reward WHERE tid='$tid'");
		if($cbtypeprice = $xc->db['source']->result($query_3, 0)) {
			$price = $cbtypeprice;
		} else {
			$price = 0;
		}
	} elseif($special == 4) {
	} elseif($special == 5) {
	}

	$field1	= array('tid','fid','posttableid','typeid','sortid','readperm','price','author','authorid','subject','dateline','lastpost','lastposter','views','replies','displayorder','highlight','digest','rate','special','attachment','moderated','closed','stickreply','recommends','recommend_add','recommend_sub','heats','status','isgroup');
	$query1	=	getinsertsql("{$discuz_tablepre}forum_thread", $field1);

	if($xc->db['discuz']->query($query1)){
		if($dvt['issmstopic']) {
			$xc->db['discuz']->query("UPDATE {$discuz_tablepre}forum_thread SET special=2 WHERE tid='$tid'");
		}
		$convertedrows ++;
	} else {
		reportlog("无法转换主题 tid = $tid subject = '$subject'，SQL 语句如下 ：<br><textarea rows=\"3\" style=\"width: 100%;\">".$query1."</textarea>");
	}

	$converted = 1;
	$totalrows ++;
	$url = 'index.php?mod=dealdata&step='.$step.'&start='.($dvt['TopicID']+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;
}
?>