<?php
/*
附件
pre_forum_attachment
pre_forum_attachment_N
*/

if($start <= 1) {
	truncatetable('forum_attachment');
	for($i = 0; $i < 10; $i ++) {
		truncatetable('forum_attachment_'.$i);
	}

}
$maxaid = $xc->db['source']->result($xc->db['source']->query("SELECT MAX(aid) MAXAID FROM {$source_tablepre}attachments"), 0, 'MAXAID');

if($start < $maxaid){
	$converted = 1;
}



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


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

	$aid		= $a['aid'];
	$tid		= $a['tid'];
	$pid		= $a['pid'];
	$uid		= $a['uid'];
	$downloads	= $a['downloads'];
	$atid		= (string)$tid;
	$tableid	= $atid{strlen($atid)-1};

	
	$filetype	= $a['filetype'];
	$dateline	= timetounix($a['postdatetime']);
	$filename	= $a['attachment'];
	$filesize	= $a['filesize'];
	$attachment	= str_replace('\\\\', '/', $a['filename']);
	$remote		= 0;
	$description	= $a['description'];
	$readperm	= $a['readperm'];
	$price		= $a['attachprice'];
	$isimage	= in_array($filetype, array('image/pjpeg', 'image/gif', 'image/bmp', 'image/png')) ? 1 : 0;
	$width		= $a['width'];
	$thumb		= 0;

	//forum_attachment
	$field1		=	array('aid','tid','pid','uid','tableid','downloads');
	$query1		=	getinsertsql("{$discuz_tablepre}forum_attachment", $field1);

	if(!$xc->db['discuz']->query($query1)) {
		reportlog("无法转换附件 $filename ");
		continue;
	}

	//forum_attachment_N
	$field2		=	array(
				'aid','tid','pid','uid','dateline','filename','filesize','attachment',
				'remote','description','readperm','price','isimage','width','thumb');
	$query2		=	getinsertsql("{$discuz_tablepre}forum_attachment_{$tableid}", $field2);

	if(!$xc->db['discuz']->query($query2)) {
		$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}forum_attachment WHERE aid='$aid'");
		reportlog("无法转换附件扩展表 $filename ");
		continue;
	}

	$convertedrows ++;
	$converted = 1;
	$totalrows ++;
	$url = 'index.php?mod=dealdata&step='.$step.'&start='.($a['aid']+1).'&stay='.$stay.'&totalrows='.$totalrows.'&convertedrows='.$convertedrows.'&sqlfilenum='.$sqlfilenum;
	file_put_contents(XCONVERT_ROOT.'./cache/lock/progress_'.$step.".txt", $url);
}
?>