<?php
/*
pre_forum_attachment
pre_forum_attachmentfield
*/

if($start <= 1) {
	truncatetable('forum_attachment');
	truncatetable('forum_attachmentfield');
	validid('f_id', 'upfile');
}
$query = $xc->db['source']->query("SELECT max(F_ID) FROM {$source_tablepre}Upfile");
$maxid = $xc->db['source']->fetch_array($query);

$query = $xc->db['source']->query("SELECT * FROM {$source_tablepre}Upfile WHERE (F_ID BETWEEN $start AND $end)");
while ($dvatt =  $xc->db['source']->fetch_assoc($query)) {
	$dvatt = array_change_key_case(daddslashes($dvatt));

	if($dvatt['f_announceid']) {
		//pre_forum_attachment
		$temp	=	explode('|', $dvatt['f_announceid']);
		
		$aid	=	$dvatt['f_id'];
		$tid	=	intval($temp[0]);
		$pid	=	intval($temp[1]);

		if($tid && $pid) {
			//pre_forum_attachment
			$atid			=	(string)$tid;
			$tableid		=	$atid{strlen($atid)-1};
			$downloads		=	$dvatt['f_downnum'];

			//pre_forum_attachment_X
			$width			=	0;
			$dateline		=	timetounix($dvatt['f_addtime']);
			$readperm		=	0;
			$price			=	0;
			$filename		=	random(8).".".$dvatt['f_filetype'];
			$filetype		=	getfiletype($filename);
			$filesize		=	$dvatt['f_filesize'];
			$attachment		=	'dvbbs/'.$dvatt['f_filename'];
			$isimage		=	in_array($filetype, array('image/pjpeg', 'image/gif', 'image/bmp', 'image/png')) ? 1 : 0;
			$uid			=	$dvatt['f_userid'];
			$thumb			=	0;
			$remote			=	0;
			$description	=	daddslashes(cutstr(htmlspecialchars(stripslashes($dvatt['f_readme'])), 90));

			//$field1			=	array('aid','tid','pid','width','dateline','readperm','price','filename','filetype','filesize','attachment','downloads','isimage','uid','thumb','remote');
			$field1			=	array('aid','tid','pid','uid','tableid','downloads');
			$query1			=	getinsertsql("{$discuz_tablepre}forum_attachment", $field1);

			$query2			=	"UPDATE {$discuz_tablepre}forum_thread SET attachment=1 WHERE tid='$tid'";

			//$field3			=	array('aid','tid','pid','uid','description');
			$field3			=	array('aid','tid','pid','uid','dateline','filename','filesize','attachment','remote','description','readperm','price','isimage','width','thumb');
			$query3			=	getinsertsql("{$discuz_tablepre}forum_attachmentfield", $field3);

			if($xc->db['discuz']->query($query1)) {
				$xc->db['discuz']->query($query2);
				if($xc->db['discuz']->query($query3)){
					$convertedrows ++;
				} else {
					$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}forum_attachment WHERE aid='$aid'");
					$xc->db['discuz']->query("UPDATE {$discuz_tablepre}forum_thread SET attachment=0 WHERE tid='$tid'");
					reportlog("无法转换附件扩展表 $filename ");
				}
			} else {
				reportlog("无法转换附件 $filename ");
			}
			$totalrows ++;
		}
	}
	$url = 'index.php?mod=dealdata&step='.$step.'&start='.($dvatt['F_ID']+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;
}
if($converted || $end < $maxid) {
	altertable('forum_attachment', 'aid');
	altertable('forum_attachmentfield', 'aid');
}
?>