<?php
/*
转换主题帖及附件：主题表及帖子表是分开的
pre_forum_post
pre_forum_ratelog
*/

if($start <= 1 && $tableid == 0) {
	$ttblquery = $xc->db['source']->query("SELECT db_value FROM {$source_tablepre}config WHERE db_name = 'db_tlist'");
	$ttablelist = unserialize($xc->db['source']->result($ttblquery));
	if(isset($ttablelist[0])) {
		$maxtableid = is_array($ttablelist) ? count($ttablelist)-1 : 0;
	} else {
		$maxtableid = is_array($ttablelist) ? count($ttablelist) : 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;
$maxtid = $xc->db['source']->result($xc->db['source']->query("SELECT max(tid) FROM {$source_tablepre}tmsgs{$tnum}"), 0);
if($start < $maxtid){
	$converted = 1;
}

$query = $xc->db['source']->query("SELECT * FROM {$source_tablepre}tmsgs{$tnum} m LEFT JOIN {$source_tablepre}threads t ON t.tid = m.tid WHERE m.tid >= $start AND m.tid < $start + $rpp");

while ($pwtmsgs = $xc->db['source']->fetch_array($query)) {
	$pwtmsgs		=	daddslashes($pwtmsgs);

	//pre_forum_post
	//$pid			=	$pwtmsgs['pid'];
	$opid			=	$pwtmsgs['pid'];
	$fid			=	$pwtmsgs['fid'];
	$tid			=	$pwtmsgs['tid'];
	if(empty($tid)) {
		continue;
	}
	$first			=	1;
	$author			=	cutstr(htmlspecialchars(trim($pwtmsgs['author'])), 15);
	$authorid		=	$pwtmsgs['authorid'];
	$subject		=	daddslashes(cutstr(htmlspecialchars(trim(@strip_tags($pwtmsgs['subject']))), 78));//formatstr去除字符串结尾的'\'
	$dateline		=	$pwtmsgs['postdate'];
	if(strpos($pwtmsgs['content'], '[sell=') !== false) {
		if(preg_match('/\[sell=(\d+)(,money)*\]/i', $pwtmsgs['content'], $match)) {
			$xc->db['discuz']->query("UPDATE {$discuz_tablepre}forum_thread SET price='{$match[1]}' WHERE tid='{$tid}' LIMIT 1");
			$pwtmsgs['content'] = '[free]'.$pwtmsgs['content'].'[/free]';
		}
	}
	$message		=	convertbbcode($pwtmsgs['content'] ? $pwtmsgs['content'] : htmlspecialchars(trim($pwtmsgs['subject'])));
	$useip			=	$pwtmsgs['userip'];
	$invisible		=	0;
	$anonymous		=	$pwtmsgs['anonymous'];
	$usesig			=	$pwtmsgs['ifsign']%2 == 0 ? 0 : 1;
	$htmlon			=	$pwtmsgs['ifsign'] < 2 ? 0 : 1;
	$bbcodeoff		=	0;
	$smileyoff		=	0;
	$parseurloff	=	0;
	$attachment		=	$pwtmsgs['aid'] ? 1 : 0;
	$rate			=	0;
	$ratetimes		=	0;
	$status			=	0;
	$tags			=	'';
	$comment		=	0;

	if($pwtmsgs['ifmark'] != ''){
		//pre_forum_ratelog
		//$pid			=	'';
		$uid			=	0;
		$element		=	array();
		$ifmarkarr		=	explode("\t", $pwtmsgs['ifmark']);
		foreach($ifmarkarr as $ifmark){
			preg_match_all("/(.+?):[+-](\d+?)\((.+)\)(.*)/is", $ifmark, $element);
			$rate += $element['2']['0'];
			$username = $element['3']['0'];
			$extcredits = 1;
			$score = $element['2']['0'];
			$reason = $ifmark;
			$uid = getuid($username);
		}
		$ratetimes = $rate;
	}

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

	if($xc->db['discuz']->query($query1)){	//
		$pid = $xc->db['discuz']->insert_id();
		$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');
		$query2	 =	getinsertsql("{$discuz_tablepre}forum_post", $field2);
		$extrado = 0;
		if($xc->config['info']['convert_type'] == 'file') {
			writesql('forum_post', $sqlfilenum, $query2);
			$convertedrows ++;
			$extrado = 1;
		} else {
			if ($xc->db['discuz']->query($query2)) {
				$extrado = 1;
				$convertedrows ++;
			} else {
				$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}forum_post_tableid WHERE pid='$pid'");
				reportlog("转换主题帖出错 tid=$tid pid=$pid ");
			}
		}		
		if ($extrado == 1) {
			if($rate > 0){
				$field3	= array('pid','uid','username','extcredits','dateline','score','reason');
				$query3	= getinsertsql("{$discuz_tablepre}forum_ratelog", $field3);
				$xc->db['discuz']->query($query3);
				$uid = 0;
			}
			if($attachment) {
				$tmptid = (string)$tid;
				$atttableid = $tmptid{strlen($tmptid)-1};
				$query4 = "UPDATE {$discuz_tablepre}forum_attachment SET pid='".$pid."' WHERE tid='".$tid."' AND pid='0'";
				$query5 = "UPDATE {$discuz_tablepre}forum_attachment_{$atttableid} SET pid='".$pid."' WHERE tid='".$tid."' AND pid='0'";

					$xc->db['discuz']->query($query4);
					$xc->db['discuz']->query($query5);
			}
			unset($pid);
		}
	} else {
		reportlog("转换主题帖出错 pid=$pid ");
	}
	$converted = 1;
	$totalrows ++;
}

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