<?php
/**
 * pw_weibo_content
 */

$maxmid = $xc->db['source']->result($xc->db['source']->query("SELECT max(mid) FROM {$source_tablepre}weibo_content"), 0);
if($start < $maxmid) {
	$converted = 1;
}

$query = $xc->db['source']->query("SELECT * FROM {$source_tablepre}weibo_content WHERE type = '0' AND contenttype = '1' AND mid >= $start AND mid < $start + $rpp") or dexit();
while ($attach = $xc->db['source']->fetch_array($query)) {
	$attachimg		=	unserialize($attach['extra']);
	$imgs			=	$attachimg['photos']; 
	$attach			=	daddslashes($attach);
	$tid = $xc->db['discuz']->result($xc->db['discuz']->query("SELECT tid FROM {$discuz_tablepre}m_tids WHERE mid='{$attach['mid']}' LIMIT 1"), 0);
	$pid = $xc->db['discuz']->result($xc->db['discuz']->query("SELECT pid FROM {$discuz_tablepre}forum_post WHERE tid='{$tid}' AND first = 1 LIMIT 1"), 0);
	$uid			=	$attach['uid'];
	$downloads		=	'0';
	$atid			=	(string)$tid;
	$tableid		=	$atid{strlen($atid)-1};

	foreach($attachimg['photos'] as $attachimg) {
		$dateline		=	$attachimg['uptime'];
		$filename		=	daddslashes(cutstr(htmlspecialchars(trim($attachimg['pintro'])), 100));
		$attachment		=	'pw/'.$attachimg['path'];
		$remote			=	0;
		$description	=	'';
		$readperm		=	0;     
		$price			=	0;
		$filetype		=	getfiletype($attachimg['name']);
		$isimage		=	1;
		$width			=	0;
		$thumb			=	0;
		
		$field1			=	array('tid','pid','uid','tableid');
		$query1			=	getinsertsql("{$discuz_tablepre}forum_attachment", $field1);
		
		if($xc->config['info']['convert_type'] == 'file') {
			writesql('forum_attachment', $sqlfilenum, $query1);
			writesql("forum_attachment_{$tableid}", $sqlfilenum, $query2);
			$convertedrows ++;
		} else {
			if($xc->db['discuz']->query($query1)) {
				$aid = $xc->db['discuz']->insert_id();
				
				$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)) {
					$convertedrows++;
				} else {
					$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}forum_attachment WHERE aid='$aid'");
					reportlog("无法转换附件内容 mid = $mid filename = '$filename'");
				}
			} else {
				reportlog("无法转换附件 aid = $aid filename = '$filename'");
			}
		}
	}
	$count = count($imgs);
	$imagecode = '';
	if($count < 2) {
		$content = '<img id="aimg_'.$rimg_id.'" onclick=\"changefeed('.$tid.', '.$pid.', 0, this)\" src="data/attachment/forum/pw/'.$attachimg['path'].'" border="0" alt="'.$attachimg['pintro'].'" style="cursor: pointer;" />';
	} else {
		foreach($imgs as $attachimg) {
			$rimg_id = random(5).$attachimg['aid'];
			$imagecode .= '<li><img id="aimg_'.$rimg_id.'" onclick=\"changefeed('.$tid.', '.$pid.', 0, this)\" src="data/attachment/forum/pw/'.$attachimg['path'].'" border="0" alt="'.$attachimg['pintro'].'" style="cursor: pointer;" /></li>';
		}
		$content = '<div class="flw_image"><ul>'.$imagecode.'</ul></div>';
	}
	
	$content = nl2br(str_replace(array("\t", '   ', '  '), ' ', $content));
	$xc->db['discuz']->query("UPDATE {$discuz_tablepre}forum_threadpreview SET content = concat(content, '{$content}') WHERE tid='{$tid}'");

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

?>