<?php
/**
 * 转换微博内容
 * pre_forum_forum
 * pre_forum_thread
 * pre_forum_post
 * pre_forum_attachment
 * pre_forum_threadpreview
 * pre_home_follow_feed
 */

if($start <= 1) {
	truncatetable('home_follow_feed');
	truncatetable('forum_threadpreview');
	$xc->db['discuz']->query("INSERT INTO {$discuz_tablepre}forum_forum VALUES (null, 0, 'group', 'guangbo_cate', 0, 0, 0, 0, 0, 0, '', '', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0)");
	$fup = $xc->db['discuz']->insert_id();
	$xc->db['discuz']->query("INSERT INTO {$discuz_tablepre}forum_forum VALUES (null, $fup, 'forum', 'guangbo_forum', 1, 0, 0, 2, 2, 2, '', '', 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0)");
	$fid = $xc->db['discuz']->insert_id();
	$xc->db['discuz']->query("INSERT INTO {$discuz_tablepre}forum_forumfield (fid) VALUES ($fup),($fid)");
	$xc->db['discuz']->query("REPLACE INTO {$discuz_tablepre}common_setting (skey,svalue) VALUES ('guangbofid','$fid')");
	$xc->db['discuz']->query("UPDATE {$discuz_tablepre}common_setting SET svalue ='$fid' WHERE skey='followforumid'");
	$newsql = "
CREATE TABLE IF NOT EXISTS {$discuz_tablepre}mid2tid (
 `mid` INT( 10 ) UNSIGNED NOT NULL ,
 `tid` INT( 10 ) UNSIGNED NOT NULL ,
INDEX (  `tid` ) ,
UNIQUE (
`mid`
)
) ENGINE = MYISAM";
	truncatetable('mid2tid');
	$xc->db['discuz']->query($newsql);
	truncatetable('mid2tid');
	$maxpid = $xc->db['discuz']->result_first("SELECT MAX(pid) FROM {$discuz_tablepre}forum_post");
	$xc->db['discuz']->query("INSERT INTO {$discuz_tablepre}forum_post_tableid VALUES ('".($maxpid+1)."')");
}

$sqlfilenum++;
$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") or dexit();
while($weibo = $xc->db['source']->fetch_array($query)) {
	if($weibo['type'] != 0) {
		continue;
	}
	$weibo['extra'] = unserialize($weibo['extra']);

	$tid 			=	null;
	$fid			=	$fid;
	$posttableid 		=	0;
	$typeid			=	0;
	$sortid			=	0;
	$readprem		= 	0;
	$author			=	addslashes(getusername($weibo['uid']));
	$authorid		=	intval($weibo['uid']);
	$subject		=	addslashes(cutstr($weibo['content'], 75, ''));
	$dateline		=	$weibo['postdate'];
	$lastpost 		= 	$dateline;
	$lastposter 		= 	$author;
	$views			=	0;
	$replies		=	$xc->db['source']->result_first("SELECT COUNT(*) FROM {$source_tablepre}weibo_comment WHERE mid='{$weibo[mid]}'");
	$displayorder = $highlight = $digest = $rate = $special = 0;
	$attachment 		= 	$weibo['extra']['photos'] ? 2 : 0;
	$moderated = $closed = $stickreply = $recommends = $recommend_add = $recommend_sub = $heats = $isgroup = 0;
	$status 		=	251;
	$message = addslashes(_parseLink($weibo['content']));


	if($weibo['type'] == 0) {
		//原创
		$field1	=	array('tid','fid','posttableid','typeid','sortid','readperm','price','author','authorid','subject','dateline','lastpost','lastposter','views','replies','displayorder','highlight','digest','rate','special','attachment','moderated','closed','stickreply','recommends','recommend_add','recommend_sub','heats','isgroup');
		$query1 =	getinsertsql("{$discuz_tablepre}forum_thread", $field1);
		if($xc->db['discuz']->query($query1)) {
			$tid = $xc->db['discuz']->insert_id();
			$reply = $xc->db['source']->result_first("SELECT COUNT(*) FROM {$source_tablepre}weibo_content WHERE type=1 AND objectid='{$weibo[mid]}'");
			$query2 = "INSERT INTO {$discuz_tablepre}mid2tid VALUES ('{$weibo[mid]}','$tid')";
			$xc->db['discuz']->query($query2);
			

			$xc->db['discuz']->query("INSERT INTO {$discuz_tablepre}forum_post_tableid VALUES (NULL)");
			$pid		= $xc->db['discuz']->insert_id();
			$first		= 1;
			$position	= 1;
			$field3 = array('pid','fid','tid','first','author','authorid','subject','dateline','message','attachment','position');
			$query3 = getinsertsql("{$discuz_tablepre}forum_post", $field3);
			if($xc->db['discuz']->query($query3)) {
				$feed_attach = '';
				if($weibo['extra']['photos']) {
					$i = 0;
					foreach($weibo['extra']['photos'] as $attach) {
						$attach_tableid = getattachtableid($tid);
						$xc->db['discuz']->query("INSERT INTO {$discuz_tablepre}forum_attachment VALUES (NULL,'$tid','$pid','$authorid','$attach_tableid',0)");
						$aid = $xc->db['discuz']->insert_id();
						$uid = $authorid;
						$filename = $attach['pintro'];
						$attachment = 'pw/'.$attach['path'];
						$isimage = 1;
						$field6 = array('aid','tid','pid','uid','dateline','filename','attachment','isimage');
						$query6 = getinsertsql("{$discuz_tablepre}forum_attachment_{$attach_tableid}", $field6);
						$xc->db['discuz']->query($query6);

						$i++;
						if($i > 4) continue;
						$feed_attach .= '<li><img id="aimg_7o8pO11" src="data/attachment/forum/'.$attachment.'" border="0" alt="'.$filename.'" onclick="changefeed('.$tid.', '.$pid.', 1, this)" style="cursor: pointer;" /></li>';
					}
				}
				if($feed_attach) {
					$subject .= $feed_attach;
					$subject = addslashes('<div class="flw_image flw_image_1"><ul>'.$subject.'</ul></div>');
				}
				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 = '';
				$field5 = array('uid','username','tid','note','dateline');
				$query5 = getinsertsql("{$discuz_tablepre}home_follow_feed", $field5);
				$xc->db['discuz']->query($query5);
				$convertedrows++;
			} else {
				reportlog("广播内容转换出错  mid = $mid, subject = '$subject'");
			}
		} 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);

}


?>
