<?php
/**
 * 转换微博内容 - 转发
 * pre_home_follow
 * pre_form_post
 * pre_forum_attachment
 */

if($start <= 1) {

}

$fid = $xc->db['discuz']->result_first("SELECT svalue FROM {$discuz_tablepre}common_setting WHERE skey='guangbofid'");
$maxmid = $xc->db['source']->result_first("SELECT MAX(mid) FROM {$source_tablepre}weibo_content");

$smilequery = $xc->db['discuz']->query("SELECT * FROM {$discuz_tablepre}common_smiley WHERE typeid=99");
while($smile = $xc->db['discuz']->fetch_array($smilequery)) {
	$smiles[] = $smile;
}

if($start < $maxmid) {
	$converted = 1;
}
$query = $xc->db['source']->query("SELECT * FROM {$source_tablepre}weibo_content WHERE mid>=$start AND mid<$start+$rpp AND type=1") or dexit();

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

	$weibo['extra'] 	= unserialize($weibo['extra']);
	$tid 			= $xc->db['discuz']->result_first("SELECT tid FROM {$discuz_tablepre}mid2tid WHERE mid='{$weibo[objectid]}'");
	if(!$tid) {
		continue;
	}

	$fid			=	$fid;
	$posttableid = $typeid = $sortid = $readperm = 0;
	$author			=	addslashes(getusername($weibo['uid']));
	$authorid		=	intval($weibo['uid']);
	$subject		=	addslashes(cutstr($weibo['content'], 75, ''));
	$dateline		=	$weibo['postdate'];
	$lastpost 		= 	$dateline;
	$lastposter 		= 	$author;
	$attachment		=	0;
	$moderated = $closed = $stickreply = $recommends = $recommend_add = $recommend_sub = $heats = $isgroup = 0;
	$position		= $xc->db['discuz']->result_first("SELECT maxposition FROM {$discuz_tablepre}forum_thread WHERE tid='{$tid}'");
	$position		=	$position ? $position + 1 : 2;

	$message = addslashes(_parseLink($weibo['content']));
	$xc->db['discuz']->query("INSERT INTO {$discuz_tablepre}forum_post_tableid VALUES (NULL)");
	$pid			= $xc->db['discuz']->insert_id();
	$first			= 0;
	$field3 = array('pid','fid','tid','first','author','authorid','dateline','message','attachment','position');
	$query3 = getinsertsql("{$discuz_tablepre}forum_post", $field3);
	$xc->db['discuz']->query("UPDATE {$discuz_tablepre}forum_thread SET maxposition='$position' WHERE tid='{$tid}'");
	$convertedrows++;
	if($xc->db['discuz']->query($query3)) {
		foreach($smiles as $smile) {
			$subject = str_replace($smile['code'], '<img src="static/image/smiley/pw_weibo/'.$smile['url'].'" border="0" class="s" alt="">', $subject);
		}
		$query4 = "INSERT INTO {$discuz_tablepre}forum_threadpreview VALUES ('$tid','$reply','$subject')";
		//$xc->db['discuz']->query($query4);

		$uid = $authorid;
		$username = $author;
		$note = $subject.'<br />';
		$field5 = array('uid','username','tid','note','dateline');
		$query5 = getinsertsql("{$discuz_tablepre}home_follow_feed", $field5);
		$xc->db['discuz']->query($query5);
		
	} else {
		reportlog("无法转换广播内容 mid = $mid, subject = '$subject'");
	}

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