<?php
function DelArticle( $ids, $tbnum, $ishide = false ){
		if ( !is_array( $ids ) || count( $ids ) == 0 ) return;
		global $saxueConfigs;
		global $article_handler;
		global $data_handler;
		$_ids = implode( ',', $ids );
		$_tagcount = $_topiccount = $_prenext = $_htmlurl = $_htmlsurl = array();
		// 统计专题文章数
		$articles = array();
		$article_handler -> queryobjects( new criteria( 'aid', '(' . $_ids . ')', 'IN' ) );
		while ( $v = $article_handler -> getobject() ) {
				$aid = $v -> getvar( 'aid' );
				$articles[$aid] = $v -> getvars( 'n' );
				if ( $articles[$aid]['topicid'] > 0  ) {
						if ( isset( $_topiccount[$articles[$aid]['topicid']] ) ) {
								$_topiccount[$articles[$aid]['topicid']] += 1;
						} else {
								$_topiccount[$articles[$aid]['topicid']] = 1;
						}
				}
		}
		// 统计标签文章数，上下篇文章ID，HTML文件地址
		global $attachs_handler;
		if ( !is_object( $attachs_handler ) ) {
				include_once SAXUE_ROOT_PATH . "/model/attachs.php";
				$attachs_handler = saxueattachshandler :: getinstance( "saxueattachshandler" );
		}
		$data_handler -> setdatatable( $tbnum );
		$data_handler -> queryobjects( new criteria( 'aid', '(' . $_ids . ')', 'IN' ) );
		while ( $v = $data_handler -> getobject() ) {
				$aid = $v -> getvar( 'aid' );
				$articles[$aid] = ( $articles[$aid] + ( $v -> getvars( 'n' ) ) );
				$_prenext[] = $articles[$aid]['preid'];
				$_prenext[] = $articles[$aid]['nextid'];
				$_htmlurl[] = $articles[$aid]['url'];
				$_htmlsurl[] = $articles[$aid]['surl'];
				$_tagids = $articles[$aid]['tagids'];
				if ( !empty( $_tagids ) ) {
						$_arr = explode( ',', $_tagids );
						foreach ( $_arr as $_tagid ) {
								if ( isset( $_tagcount[$_tagid] ) ) ++$_tagcount[$_tagid];
								else $_tagcount[$_tagid] = 1;
						}
				}
				// 删除附件
				if ( !$ishide ) $attachs_handler -> delarticle( $aid );
		}
		// 统计章节信息
		include_once SAXUE_ROOT_PATH . "/model/article_chapter.php";
		$chapter_handler = saxuearticlechapterhandler :: getinstance( "saxuearticlechapterhandler" );
		$chapter_handler -> queryobjects( new criteria( 'aid', '(' . $_ids . ')', 'IN' ) );
		while ( $v = $chapter_handler -> getobject() ) {
				$aid = $v -> getvar( 'aid' );
				$chapterid = $v -> getvar( 'chapterid' );
				$articles[$aid]['chapters'][$chapterid] = $v -> getvars( 'n' );
		}
		// 删除文章处理动作
		if ( !$ishide ) {
				//saxue_obflush( '开始删除TXT<br>' );
				// 删除TXT
				foreach ( $ids as $aid ) {
						saxue_delfile( $saxueConfigs['article']['txtdir'] . saxue_getsubdir( $aid ) . '/' . $aid . '.txt' );
						saxue_delfile( $saxueConfigs['article']['txtdir'] . saxue_getsubdir( $aid ) . '/' . $aid . '.slide' );
						saxue_delfile( $saxueConfigs['article']['txtdir'] . saxue_getsubdir( $aid ) . '/' . $aid . '.expand' );
						saxue_delfile( $saxueConfigs['article']['txtdir'] . saxue_getsubdir( $aid ) . '/' . $aid . '.zip' );
						if ( file_exists( $saxueConfigs['article']['txtdir'] . saxue_getsubdir( $aid ) . '/' . $aid ) ) {
								saxue_delfolder( $saxueConfigs['article']['txtdir'] . saxue_getsubdir( $aid ) . '/' . $aid );
						}
				}
				//saxue_obflush( '开始删除HTML<br>' );
				// 删除HTML
				if ( $saxueConfigs['article']['url_detail'] > 2 ) {
						foreach ( $_htmlurl as $v ) {
								$_file = str_replace( $saxueConfigs['article']['htmlurl'], '', $v );
								if ( is_file( $saxueConfigs['article']['htmldir'] . $_file ) ) {
										saxue_delfile( $saxueConfigs['article']['htmldir'] . $_file );
								} elseif ( is_dir( $saxueConfigs['article']['htmldir'] . $_file ) ) {
										saxue_delfolder( $saxueConfigs['article']['htmldir'] . $_file );
								}
								if ( is_file( $saxueConfigs['article']['mobile_htmldir'] . $_file ) ) {
										saxue_delfile( $saxueConfigs['article']['mobile_htmldir'] . $_file );
								} elseif ( is_dir( $saxueConfigs['article']['mobile_htmldir'] . $_file ) ) {
										saxue_delfolder( $saxueConfigs['article']['mobile_htmldir'] . $_file );
								}
						}
						if ( count( $_htmlsurl ) > 0 ) {
								foreach ( $_htmlsurl as $v ) {
										$_file = str_replace( $saxueConfigs['article']['htmlurl'], '', $v );
										saxue_delfile( $saxueConfigs['article']['htmldir'] . $_file );
										saxue_delfile( $saxueConfigs['article']['mobile_htmldir'] . $_file );
								}
						}
				}
				//saxue_obflush( '开始更新专题文章数<br>' );
				// 更新专题文章数
				if ( count( $_topiccount ) > 0) {
						include SAXUE_ROOT_PATH . "/model/topic.php";
						$topic_handler = &saxuetopichandler :: getinstance("saxuetopichandler");
						foreach ( $_topiccount as $topicid => $counts ) {
								$topic_handler -> updatefields( 'counts=counts-' . $counts, 'topicid=' . $topicid );
						}
				}
				//saxue_obflush( '开始更新标签文章数<br>' );
				// 更新标签文章数
				if ( count( $_tagcount ) > 0) {
						include SAXUE_ROOT_PATH . "/model/tag.php";
						$tag_handler = &saxuetaghandler :: getinstance("saxuetaghandler");
						foreach ( $_tagcount as $tagid => $counts ) {
								$tag_handler -> updatefields( 'counts=counts-' . $counts, 'tagid=' . $tagid );
						}
				}
				//saxue_obflush( '开始删除数据库记录<br>' );
				// 删除数据库记录
				$article_handler -> db -> query( "DELETE FROM " . saxue_dbprefix( "member_doing" ) . " WHERE aid IN(" . $_ids . ")" );
				$article_handler -> db -> query( "DELETE FROM " . saxue_dbprefix( "tag_article" ) . " WHERE aid IN(" . $_ids . ")" );
				$article_handler -> db -> query( "DELETE FROM " . saxue_dbprefix( "article_report" ) . " WHERE aid IN(" . $_ids . ")" );
				$article_handler -> db -> query( "DELETE FROM " . saxue_dbprefix( "article_chapter" ) . " WHERE aid IN(" . $_ids . ")" );
				$article_handler -> db -> query( "DELETE FROM " . saxue_dbprefix( "article_data_" . $tbnum ) . " WHERE aid IN(" . $_ids . ")" );
				$article_handler -> db -> query( "DELETE FROM " . saxue_dbprefix( "article" ) . " WHERE aid IN(" . $_ids . ")" );
		}
		//saxue_obflush( '开始处理上下篇<br>' );
		// 重新处理上下篇
		$_prenext = array_unique( $_prenext );
		$_prenext = array_diff( $_prenext, $ids );
		PreNextArticle( $_prenext, $tbnum );
		// 页面处理
		if ( count( $articles ) > 0 ) {
				if ( $saxueConfigs['article']['url_detail'] <= 2 ) {
						if ( !is_object( $saxueTpl ) ) {
								include_once( SAXUE_ROOT_PATH . "/lib/template/template.php" );
								$saxueTpl = &saxuetpl :: getinstance();
						} 
						$isrewrite = true;
				} else {
						$isrewrite = false;
				}
				foreach ( $articles as $aid => $article ) {
						if ( $isrewrite ) {
								$saxueTpl -> clear_cache( SAXUE_THEME_PATH . '/article/detail.0.tpl', $aid );
						} elseif ( !empty( $saxueConfigs['article']['show404'] ) ) {
								$article['status'] = 4;
								$packarticle = new saxuepackarticle( $article );
								$packarticle -> make404( false );
						}
						if ( $article['ischapter'] && is_array( $article['chapters'] ) && count( $article['chapters'] ) > 0 ) {
								foreach ( $article['chapters'] as $chapterid => $chapter ) {
										if ( $isrewrite ) {
												$saxueTpl -> clear_cache( SAXUE_THEME_PATH . '/article/chapter.tpl', $chapterid );
										} elseif ( !empty( $saxueConfigs['article']['show404'] ) ) {
												$packarticle -> make404( false, 0, $chapterid, $chapter['chaptername'] );
										}
								}
						}
				}
		}
}
function PreNextArticle( $ids, $tbnum, $check = false ){
		if ( !is_array( $ids ) || count( $ids ) == 0 ) return;
		global $saxueConfigs;
		global $article_handler;
		global $data_handler;
		$checkids = array();
		$data_handler -> setdatatable( $tbnum );
		foreach ( $ids as $aid ) {
				if ( !$aid ) continue;
				$_res = $article_handler -> db -> query( "SELECT aid FROM " . saxue_dbprefix( "article" ) . " WHERE aid<" . $aid . " AND status=1 AND tbnum=" . $tbnum . " ORDER BY aid DESC LIMIT 1" );
				$_row = $article_handler -> db -> fetcharray( $_res ); 
				$_preid = intval( $_row['aid'] );
				$_res = $article_handler -> db -> query( "SELECT aid FROM " . saxue_dbprefix( "article" ) . " WHERE aid>" . $aid . " AND status=1 AND tbnum=" . $tbnum . " ORDER BY aid ASC LIMIT 1" );
				$_row = $article_handler -> db -> fetcharray( $_res ); 
				$_nextid = intval( $_row['aid'] );
				$data_handler -> updatefields( array( 'preid' => $_preid, 'nextid' => $_nextid ), 'aid=' . $aid );
				// 生成HTML
				if ( $saxueConfigs['article']['url_detail'] > 2 ) {
						$packarticle = new saxuepackarticle( $aid );
						$packarticle -> makearticle();
				}
				$checkids[] = $_preid;
				$checkids[] = $_nextid;
		}
		if ( $check ) {
				$checkids = array_unique( $checkids );
				$checkids = array_diff( $checkids, $ids );
				PreNextArticle( $checkids, $tbnum );
		}
}
function EditTagArticle( $aid, $fromtag, $totag ){
		global $saxueConfigs;
		global $article_handler;
		global $data_handler;
		$ret = array( 'flag' => 0 );
		$article = $article_handler -> get( $aid );
		if ( !is_object( $article ) ) {
				return $ret;
		} 
		$row = $article -> getvars( 'n' );
		$data_handler -> setdatatable( $row['tbnum'] );
		$data = $data_handler -> get( $aid );
		$row = $row + ( $data -> getvars( 'n' ) );
		$tags = trim( str_replace( ',' . $fromtag . ',', ',' . $totag . ',', ',' . $row['tags'] . ',' ), ',' );
		if ( $tags != $row['tags'] ) {
				$data_handler -> updatefields( array( 'tags' => $tags ), 'aid=' . $aid );
				if ( $row['status'] == 1 && $saxueConfigs['article']['url_detail'] > 2 ) {
						$row['tags'] = $tags;
						$packarticle = new saxuepackarticle( $row );
						$packarticle -> makearticle();
				}
		}
		return array( 'flag' => 1, 'title' => $row['title'] );
}
function DelTagArticle( $aid, $tagid, $tagname ){
		global $saxueConfigs;
		global $article_handler;
		global $data_handler;
		$ret = array( 'flag' => 0 );
		$article = $article_handler -> get( $aid );
		if ( !is_object( $article ) ) {
				return $ret;
		} 
		$row = $article -> getvars( 'n' );
		$data_handler -> setdatatable( $row['tbnum'] );
		$data = $data_handler -> get( $aid );
		$row = $row + ( $data -> getvars( 'n' ) );
		$tags = trim( str_replace( ',' . $tagname . ',', ',', ',' . $row['tags'] . ',' ), ',' );
		if ( $tags != $row['tags'] ) {
				$tagids = trim( str_replace( ',' . $tagid . ',', ',', ',' . $row['tagids'] . ',' ), ',' );
				$data_handler -> updatefields( array( 'tags' => $tags, 'tagids' => $tagids ), 'aid=' . $aid );
				if ( $row['status'] == 1 && $saxueConfigs['article']['url_detail'] > 2 ) {
						$row['tags'] = $tags;
						$row['tagids'] = $tagids;
						$packarticle = new saxuepackarticle( $row );
						$packarticle -> makearticle();
				}
		}
		return array( 'flag' => 1, 'title' => $row['title'] );
}
function MergeTagArticle( $aid, $fromtag, $totag ){
		global $saxueConfigs;
		global $article_handler;
		global $data_handler;
		$ret = array( 'flag' => 0 );
		$article = $article_handler -> get( $aid );
		if ( !is_object( $article ) ) {
				return $ret;
		} 
		$row = $article -> getvars( 'n' );
		$data_handler -> setdatatable( $row['tbnum'] );
		$data = $data_handler -> get( $aid );
		$row = $row + ( $data -> getvars( 'n' ) );
		if ( false === strpos( ',' . $row['tags'] . ',', ',' . $totag['tagname'] . ',' ) ) {
				$tags = trim( str_replace( ',' . $fromtag['tagname'] . ',', ',' . $totag['tagname'] . ',', ',' . $row['tags'] . ',' ), ',' );
				$tagids = trim( str_replace( ',' . $fromtag['tagid'] . ',', ',' . $totag['tagid'] . ',', ',' . $row['tagids'] . ',' ), ',' );
				$ret['flag'] = 3;
		} else {
				$tags = trim( str_replace( ',' . $fromtag['tagname'] . ',', ',', ',' . $row['tags'] . ',' ), ',' );
				$tagids = trim( str_replace( ',' . $fromtag['tagid'] . ',', ',', ',' . $row['tagids'] . ',' ), ',' );
				$ret['flag'] = 1;
		}
		if ( $tags != $row['tags'] ) {
				$data_handler -> updatefields( array( 'tags' => $tags, 'tagids' => $tagids ), 'aid=' . $aid );
				++$ret['flag'];
				if ( $row['status'] == 1 && $saxueConfigs['article']['url_detail'] > 2 ) {
						$row['tags'] = $tags;
						$row['tagids'] = $tagids;
						$packarticle = new saxuepackarticle( $row );
						$packarticle -> makearticle();
				}
		}
		$ret['title'] = $row['title'];
		return $ret;
}
function BaiduPush( $urls ){
		global $saxueConfigs;
		$ch = curl_init();
		$options =  array(
			CURLOPT_URL => $saxueConfigs['article']['baiduapi'],
			CURLOPT_POST => true,
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_POSTFIELDS => implode("\n", $urls),
			CURLOPT_HTTPHEADER => array( 'Content-Type: text/plain' ),
		);
		curl_setopt_array( $ch, $options );
		$result = curl_exec( $ch );
}
if ( !isset( $saxueConfigs['article'] ) ) {
		saxue_getconfigs( "configs", "article" );
}
if ( !is_object( $article_handler ) ) {
		include( SAXUE_ROOT_PATH . "/model/article.php" );
		$article_handler = saxuearticlehandler :: getinstance( "saxuearticlehandler" );
}
if ( !is_object( $data_handler ) ) {
		include SAXUE_ROOT_PATH . "/model/article_data.php";
		$data_handler = saxuearticledatahandler :: getinstance( "saxuearticledatahandler" );
}
include SAXUE_ROOT_PATH . "/common/packarticle.php";