<?php
/*
广播数据：
pre_forum_post
*/
if($start <= 1) {
	truncatetable('home_follow_feed');
	$m_fid = $xc->db['discuz']->result($xc->db['discuz']->query("SELECT fid FROM {$discuz_tablepre}forum_forum where name = '广播专版'"), 0);
	if(!$m_fid) {
		$xc->db['discuz']->query("INSERT INTO {$discuz_tablepre}forum_forum (`fup`,`type`,`name`,`status`,`allowfeed`) VALUES ('0','group','广播专区','0','1')");
		$add_fid = $xc->db['discuz']->insert_id();
		$xc->db['discuz']->query("INSERT INTO {$discuz_tablepre}forum_forumfield (`fid`) VALUES ('$add_fid')");
		$xc->db['discuz']->query("INSERT INTO {$discuz_tablepre}forum_forum (`fup`,`type`,`name`,`status`,`threads`,`posts`,`todayposts`,`allowsmilies`,`allowbbcode`,`allowimgcode`,`allowfeed`,`alloweditpost`,`allowglobalstick`) VALUES ('".$add_fid."','forum','广播专版','1','1','1','1','1','1','1','1','1','1')");
		$m_fid = mysql_insert_id();
		$xc->db['discuz']->query("INSERT INTO {$discuz_tablepre}forum_forumfield (`fid`) VALUES ('$m_fid')");
		$xc->db['discuz']->query("REPLACE INTO {$discuz_tablepre}common_setting (`skey`,`svalue`) VALUES ('followforumid','$m_fid')");
	}
	$xc->db['discuz']->query("CREATE TABLE IF NOT EXISTS {$discuz_tablepre}m_tids (mid int(10) unsigned NOT NULL DEFAULT '0' PRIMARY KEY, tid int(10) unsigned NOT NULL DEFAULT '0')");
	$sqlfilenum = 0;
	$extrastr = '';
} else {
	$m_fid = $xc->db['discuz']->result($xc->db['discuz']->query("SELECT fid FROM {$discuz_tablepre}forum_forum where name = '广播专版'"), 0);
}
$sqlfilenum ++;
$extrastr = '&sqlfilenum='.$sqlfilenum;

$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 mid >= $start AND mid < $start + $rpp") or dexit();
while ($weibo_item	=	$xc->db['source']->fetch_array($query)) {
    $weibo_item	=	daddslashes($weibo_item);
	$mid		=	$weibo_item['mid'];
	$fid		=	$m_fid;
	$authorid	=	$weibo_item['uid'];
	$author		=	getusername($authorid);
	$dateline	=	$weibo_item['postdate'];
	$objectid	=	$weibo_item['objectid'];
	$attachment	=	$weibo_item['contenttype'] == 1 ? 2 : 0;

	$uid	=	$authorid;
	$username	=	$author;
	if($weibo_item['type'] == '0') {
		$posttableid		=	0;
		$subject		=	daddslashes(cutstr(htmlspecialchars(trim(@strip_tags($weibo_item['content']))), 80));//formatstr去除字符串结尾的'\'
		$message		=	$weibo_item['content'];
		$replies		=	$weibo_item['replies'];
		$first			=	'1';

		$field1 = array('fid','posttableid','author','authorid','subject','dateline','replies','attachment');
		$query1 = getinsertsql("{$discuz_tablepre}forum_thread", $field1);

		$field2 = array('');
		$query2 = getinsertsql("{$discuz_tablepre}forum_post_tableid", $field2);

		if($xc->db['discuz']->query($query1)) {
			$tid = $xc->db['discuz']->insert_id();
			$field4 = array('mid','tid');
			$query4 = getinsertsql("{$discuz_tablepre}m_tids", $field4);
			if (!$xc->db['discuz']->query($query4)) {
				reportlog("记录临时表tid tid='$tid'失败！");
			}

			$field8 = array('uid','username','tid','dateline');
			$query8 = getinsertsql("{$discuz_tablepre}home_follow_feed", $field8);
			if (!$xc->db['discuz']->query($query8)) {
				reportlog("记录到广播转发表home_follow_feed  mid='$mid'失败！");
			}

			if ($xc->db['discuz']->query($query2)) {
				$pid = $xc->db['discuz']->insert_id();
				$field3 = array('pid','fid','tid','first','author','authorid','subject','dateline','message','invisible','anonymous','usesig','htmlon','bbcodeoff','smileyoff','parseurloff','attachment','rate','ratetimes','status','tags','comment', 'replycredit');
				$query3 = getinsertsql("{$discuz_tablepre}forum_post", $field3);
				if ($xc->db['discuz']->query($query3)) {
					$relay = 0;
					$content = formatstr(convertbbcode($weibo_item['content']));

					$field7 = array('tid','relay','content');
					$query7 = getinsertsql("{$discuz_tablepre}forum_threadpreview", $field7);
					if ($xc->db['discuz']->query($query7)) {
						reportlog("无法转换转到转播表 mid = $mid subject = '$subject'");
					}

					$convertedrows++;
				} else {
					$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}forum_post_tableid WHERE pid='$pid'");
					reportlog("无法转换帖子 pid = $pid subject = '$subject'");
				}
			} else {
				reportlog("无法转换广播到帖子协调表 mid=$mid");
			}
		} else {
			reportlog("无法转换主题 tid = $tid subject = '$subject'，SQL 语句如下 ：<br><textarea rows=\"3\" style=\"width: 100%;\">".$sql."</textarea>");
		}
	} elseif($weibo_item['type'] == '1') {
		$note		=	$weibo_item['content'];
		$tid = $xc->db['discuz']->result($xc->db['discuz']->query("SELECT tid FROM {$discuz_tablepre}m_tids WHERE mid='{$objectid}' LIMIT 1"), 0);
		if(empty($tid)){
			$wtype = $xc->db['source']->result($xc->db['source']->query("SELECT type FROM {$source_tablepre}weibo_content WHERE mid='{$weibo_item[objectid]}' LIMIT 1"), 0);
			if($wtype != 0){continue;}
		}
		$first		=	'0';
		$field6 = array('uid','username','tid','note','dateline');
		$query6 = getinsertsql("{$discuz_tablepre}home_follow_feed", $field6);
		if($xc->config['info']['convert_type'] == 'file') {
			writesql('home_follow_feed', $sqlfilenum, $query6);
		} else {
			if ($xc->db['discuz']->query($query6)) {
				if ($xc->db['discuz']->query("UPDATE {$discuz_tablepre}forum_threadpreview SET relay=relay+1 where tid='{$tid}'")) {
					reportlog("无法转换转到转播表 mid = $mid subject = '$subject'");
				}

				$convertedrows++;
			} else {
				$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}forum_post_tableid WHERE pid='$pid'");
				reportlog("无法转换转播 mid = $mid subject = '$subject'");
			}
		}
	} else {
		continue;
	}

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