<?php

// total_comment.inc.php, 0.3 2006/12/24	taru        plugin

function plugin_total_comment_convert()
{
	global $non_list;

	$args = func_get_args();
	$body = array();
	$sort_line = array();
	$foot = 0;
	$sort = 0;
	$sum  = 0;
	$set_tc = new TotalComment;

	foreach ($args as $opt) {
		$count = 1;
		list($content, $page, $page_filter, $section) = explode('>', $opt);
		if (! isset( $page_filter ) ) $page_filter = '';
		if (! isset( $section ) ) $section = '';
		if ( preg_match('|^head$|',$content) ) {
			$body[] = "|" . htmlspecialchars($page) . "|h";
		} else if ( preg_match('|^sort(\d)*$|', $content, $sort_num) ) {
			$sort = 1;
			$sort_ad = ($page == 'd') ? SORT_DESC : SORT_ASC;
			$sort_type = $set_tc->sort_type_set($page_filter);
			$sort_key = $sort_num[1];
		} else if ( preg_match('|^stratum(\d)*$|',$content,$short) ) {

			if (! class_exists('auth')) {	//sonots '07.01.23
				class auth {
					function get_existpages()
					{
						return get_existpages();
					}
				}
			}
			$page_list = auth::get_existpages();

			foreach ( $page_list as $page_l ){
				if ( preg_match("/$non_list/", $page_l) ) continue;
				if (! preg_match("/$page_filter/", $page_l) && $page_filter !== '' ) continue;
				if ( preg_match(']^' . $page . ']', $page_l) ){
					$count = $set_tc->comment_count($page_l, $section);
					$sum += $count;
					$link_page = ($short[1]) ? array_pop( explode("/", $page_l) ) . ">" . $page_l : $page_l;
					$body[] = "|[[" . $link_page . "]]|"
						. "$count|";
				}
			}
			
		} else if ( preg_match('|^foot$|',$content) ) {
			$foot = 1;
			$foot_comment = htmlspecialchars($page);
		} else {
			$count = $set_tc->comment_count($page,$section);
			$sum += $count;
			$body[] = "|[[" . htmlspecialchars($content) . ">" . htmlspecialchars($page) . "]]|"
				. "$count|";
		}
	}
	if ( $sort ){
		$line_s = array();
		foreach ( $body as $key => $line ){
			$line_s = explode('|', $line);
			$sort_line[$key] = $line_s[$sort_key + 1];
		};
		array_multisort($sort_line,$sort_ad,$sort_type,$body);
/*
		if ( $sort_natural ) {
			natcasesort($body);
		} else {
			sort($body);
		}
*/
	}
	if ( $foot ) $body[] = "|$foot_comment|$sum|f";

	return convert_html($body);
}
class TotalComment
{
	function sort_type_set($type)
	{
		if ($type == 's'){
			$sort_type =  SORT_STRING;
		} else if ($type == 'n'){
			$sort_type =  SORT_NUMERIC;
		} else {
			$sort_type =  SORT_REGULAR;
		}
		return $sort_type;
	}
	function comment_count($page, $section = '')
	{
		$comment_count = 0;
		$section_chk = ($section === '') ? 1 : 0;
		$page_source = get_source($page);
		foreach ($page_source as $line_chk) {
			if (preg_match("/^\*{1,3}/",$line_chk) && $section !== '') {
				if ( preg_match("|$section|", $line_chk) ) {
					$section_chk = 1;
				} else {
					$section_chk = 0;
				}
			}
			if (preg_match('/\&new\{.*?\}\;$/', $line_chk) && $section_chk === 1) $comment_count++;
		}
		return $comment_count;
	}
}
function plugin_total_comment_inline()
{
	$args = func_get_args();
	$set_tc = new TotalComment;

	$section = (isset($args[1])) ? $args[1] : '';
	$body = $set_tc->comment_count($args[0],$section);
	return convert_html($body);
}
?>
