<?php

// total_comment.inc.php, 0.2 2006/12/20	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;

	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 = sort_type($page_filter);
			$sort_key = $sort_num[1];
		} else if ( preg_match('|^stratum(\d)*$|',$content,$short) ) {

			$page_list = auth::get_existpages();	//PukiWiki Plus! i18n
//			$page_list = get_existpages();			//PukiWiki

			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 = 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 = 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);
}
function sort_type($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("/^\-{1,3}/", $line_chk) && $section_chk === 1) $comment_count++;
	}
	return $comment_count;
}
function plugin_total_comment_inline()
{
	$args = func_get_args();
	$section = (isset($args[1])) ? $args[1] : '';
	$body = comment_count($args[0],$section);
	return convert_html($body);
}
?>
