<?php
/*
文章及文章附件表：
portal_article_title
portal_article_count
portal_article_content
portal_attachment
*/
if($start <= 1) {
	truncatetable('portal_article_title');
	truncatetable('portal_article_count');
	truncatetable('portal_article_content');
	truncatetable('portal_attachment');
    
    /* modified niexinyuan */
    truncatetable('portal_comment');
    /* modified niexinyuan */
    
}

$maxtid = $xc->db['source']->result($xc->db['source']->query("SELECT max(article_id) FROM {$source_tablepre}cms_article"), 0);
if($start < $maxtid){
	$converted = 1;
}
$query = $xc->db['source']->query("SELECT * FROM {$source_tablepre}cms_article ca LEFT JOIN {$source_tablepre}cms_articleextend cae ON ca.article_id=cae.article_id LEFT JOIN {$source_tablepre}cms_articlecontent cac ON ca.article_id=cac.article_id WHERE ca.article_id >= $start AND ca.article_id <$start + $rpp") or dexit();

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

	//文章标题表：portal_article_title
	$aid				=	$pwcmstitle['article_id'];
	$catid				=	$pwcmstitle['column_id'];
	$bid				=	0;
	$uid				=	$pwcmstitle['userid'];
	$username			=	$pwcmstitle['username'];
	$title				=	$pwcmstitle['subject'];
	$shorttitle			=	$pwcmstitle['subject'];
	$author				=	cutstr(htmlspecialchars(trim($pwcmstitle['author'])), 15);
	$from				=	$pwcmstitle['frominfo'];
	$fromurl			=	$pwcmstitle['fromurl'];
	$url				=	'';
	$summary			=	$pwcmstitle['descrip'];
	$id					=	$pwcmstitle['sourceid'];
	$idtype				=	$pwcmstitle['sourcetype'] == 'thread' ? 'tid' : ($pwcmstitle['sourcetype'] == 'diary' ? 'blogid' : '');
	$dateline			=	$pwcmstitle['postdate'];
    /* modified niexinyuan */
    $allowcomment       =   1;
    /* modified niexinyuan */
    
	//文章额外内容：portal_article_count
	$viewnum			=	$pwcmstitle['hits'];
	$favtimes			=	0;
	$sharetimes			=	0;

	//文章表内容：portal_article_content
	$aid				=	$pwcmstitle['article_id'];
	$pwcmstitle['content'] = str_replace("\r\n", '<br />', $pwcmstitle['content']);

		//处理附件
	if(strpos($pwcmstitle['content'], '[attachment=') !== false) {
		$attach_query = $xc->db['source']->query("SELECT * FROM {$source_tablepre}cms_attach WHERE article_id='$aid'");
		while($attach_arr = $xc->db['source']->fetch_array($attach_query)) {
			$search_str = '[attachment='.$attach_arr['attach_id'].']';
			if($attach_arr['type'] == 'img') {
				$replace_str = '<a href="data/attachment/portal/'.$attach_arr['attachurl'].'" target="_blank"><img src="data/attachment/portal/'.$attach_arr['attachurl'].'"></a>';
			} else {
				$replace_str = '<a href="portal.php?mod=attachment&amp;id='.$attach_arr['attach_id'].'" target="_blank" class="attach">'.$attach_arr['name'].'</a>';
			}
			$pwcmstitle['content'] = str_replace($search_str, $replace_str, $pwcmstitle['content']);
			$filename = $attach_arr['name'];
			$filetype = substr($attach_arr['name'], '-3');
			$filesize = $attach_arr['size'];
			$attachment = $attach_arr['attachurl'];
			$isimage = $attach_arr['type'] == 'img' ? '1' : '0';
			$thumb = 0;
			$remote = 0;
			$field_attach = array('uid','dateline','filename','filetype','filesize','attachment','isimage','thumb','remote','aid');
			$query_attach = getinsertsql("{$discuz_tablepre}portal_attachment", $field_attach);
			$xc->db['discuz']->query($query_attach);
		}
	}

		//处理表情
	if(strpos($pwcmstitle['content'], '[s:') !== false) {
		$smile_query = $xc->db['source']->query("SELECT a.*, b.path AS bpath FROM {$source_tablepre}smiles a LEFT JOIN {$source_tablepre}smiles b ON a.type=b.id");
		while($smile_arr = $xc->db['source']->fetch_array($smile_query)) {
			if($smile_arr['bpath']) {
				$search_str = '[s:'.$smile_arr['id'].']';
				$replace_str = '<img src="static/image/smiley/'.($smile_arr['bpath'] == 'default' ? 'pw_default' : $smile_arr['bpath']).'/'.$smile_arr['path'].'">';
				$pwcmstitle['content'] = str_replace($search_str, $replace_str, $pwcmstitle['content']);
			}
		}
	}
		//处理分页
	$contents_arr		=	explode("[###page###]", $pwcmstitle['content']);
	$contents			=	count($contents_arr);

    /* modified niexinyuan */
    //处理文章评论
    $query_comment = $xc->db['source']->query("SELECT * FROM {$source_tablepre}cms_comment WHERE article_id='$aid'");
    while($comment = $xc->db['source']->fetch_array($query_comment)) {
        $cid_comment = $comment['commentid'];
        $uid_comment = $comment['uid'];
        $username_comment = getusername($uid_comment);
        $id_comment = $comment['article_id'];
        $idtype_comment = 'aid';
        $postip_comment = $comment['ip'];
        $dateline_comment = $comment['postdate'];
        $message_comment = $comment['content'];
        $insert_list[] = "('$cid_comment', '$uid_comment', '$username_comment', '$id_comment', '$idtype_comment', '$postip_comment', '$dateline_comment', '$message_comment')";
    }
    if($insert_list) {
        $sql_comment = "INSERT INTO {$discuz_tablepre}portal_comment (cid, uid, username, id, idtype, postip, dateline, message) VALUES ";
        $sql_comment .= implode(', ', $insert_list);
        unset($insert_list);
        $xc->db['discuz']->query($sql_comment);
    }
    /* modified niexinyuan */

	$field1				=	array('aid','catid','bid','uid','username','title','shorttitle','author','`from`','fromurl','url','summary','contents','id','idtype','dateline','allowcomment');
	$query1				=	getinsertsql("{$discuz_tablepre}portal_article_title", $field1);

	$field2 = array('aid','catid','viewnum','dateline','favtimes','sharetimes');
	$query2 = getinsertsql("{$discuz_tablepre}portal_article_count", $field2);

	$field3 = array('aid','id','idtype','content','pageorder','dateline');

	if ($xc->db['discuz']->query($query1)) {
		if ($xc->db['discuz']->query($query2)) {
			foreach($contents_arr as $k => $v) {
				$pageorder = $k+1;
				$content = $v;
				$query3 = getinsertsql("{$discuz_tablepre}portal_article_content", $field3);
				if ($xc->db['discuz']->query($query3)) {
					$convertedrows ++;
				}else{
					reportlog("无法转换文章内容 aid = $aid ，SQL 语句如下 ：<br><textarea rows=\"3\" style=\"width: 100%;\">".$query1."</textarea>");
				}
			}
		}else{
			reportlog("无法转换文章附加内容 aid = $aid ，SQL 语句如下 ：<br><textarea rows=\"3\" style=\"width: 100%;\">".$query1."</textarea>");
		}
	} else {
		reportlog("无法转换文章 aid = $aid title = '$title'，SQL 语句如下 ：<br><textarea rows=\"3\" style=\"width: 100%;\">".$sql."</textarea>");
	}
	$totalrows = $totalrows + $contents;
}
?>