<?php
/////////////////////////////////////////////////
// PukiWiki - Yet another WikiWikiWeb clone.
//
// $Id: popular2.inc.php,v 1.11 2004/09/04 15:00:56 henoheno Exp $
//

/*
**説明 [#t57baf29]
標準プラグイン [[popular>http://pukiwiki.org/index.php?cmd=read&page=PukiWiki%2F1.4%2F%E3%83%9E%E3%83%8B%E3%83%A5%E3%82%A2%E3%83%AB%2F%E3%83%97%E3%83%A9%E3%82%B0%E3%82%A4%E3%83%B3%2Fp#content_1_2]]
の拡張((popular v1.12の拡張))です。popular の機能はそのまま使えるのでとりあえず置き換えておいても損はないのかも。

人気のある(アクセス数の多い)ページの一覧を表示します。
通算および今日および昨日および最近(昨日+今日)に別けて一覧を作ることができます。
counter プラグインのアクセスカウント情報を使っています。

関連:([[dev:BugTrack/770]])([[dev:BugTrack/623]])([[dev:BugTrack/683]])

***標準プラグインからの変更点(初版) [#g9b6d783]
-counter には昨日の情報もあり、せっかくなので昨日の n 件、最近(昨日+今日)の n 件も表示できるようにした

***その後の追加機能 [#mf3ac7b6]
-最下層のページだけをリスト候補とする機能
-人気の n 件などのタイトル出力をしない機能
-ページ名を短く表示する機能

*書式 [#c82249f2]

-第一引数 - 表示する件数を指定。default 10
-第二引数 - 表示させないページの正規表現。default なし
-第三引数 - 通算(all,false)か今日(today,true)か昨日(yesterday)か最近(recent)((counterプラグインの仕様上、昨日 + 今日程度しかできませんが))の一覧かのフラグ 。default all
-第四引数以降
--deepest~
最下層のページだけをリスト候補とする。上の階層のページがただの一覧用のページでアクセス数が多いのは当たり前だったりする時に使用。
--notitle~
人気の n 件などタイトル出力をしない。
--short~
表示ページ名を短く。具体的には /hoge/foo/bar が bar とだけ表示される。

*使用例 [#x9fe893b]
-#popular2
-#popular2(20)
-#popular2(20,FrontPage|MenuBar)
-#popular2(20,FrontPage|MenuBar,today)
-#popular2(20,FrontPage|MenuBar,today,deepest)
-#popular2(20,,today,short,deepest)
-#popular2(,,,,notitle)

*設定 [#d531254a]
ja.lng ファイルに
 $_popular_plugin_frame = '<h5>人気の%d件</h5><div>%s</div>';
 $_popular_plugin_today_frame = '<h5>今日の%d件</h5><div>%s</div>';
 $_popular_plugin_yesterday_frame = '<h5>昨日の%d件</h5><div>%s</div>';
 $_popular_plugin_recent_frame = '<h5>最近の%d件</h5><div>%s</div>';
 $_popular_plugin_notitle_frame = '<div>%s</div>';
en.lng ファイルに
 $_popular_plugin_frame = '<h5>popular(%d)</h5><div>%s</div>';
 $_popular_plugin_today_frame = '<h5>today\'s(%d)</h5><div>%s</div>';
 $_popular_plugin_yesterday_frame = '<h5>yesterday\'s(%d)</h5><div>%s</div>';
 $_popular_plugin_recent_frame = '<h5>recent\'s pop(%d)</h5><div>%s</div>';
 $_popular_plugin_notitle_frame = '<div>%s</div>';
のように追加してください。
lng ファイルで設定していなくても一応上のように動くようにしています。
が、やはり lng ファイルで設定するのが本筋です。
*/

function plugin_popular2_init()
{
	global $_popular_plugin_frame, $_popular_plugin_today_frame, $_popular_plugin_yesterday_frame, 
	       $_popular_plugin_recent_frame, $_popular_plugin_notitle_frame;

	// lng ファイルで定義されていない場合の一時しのぎ。できるかぎり lng ファイルで定義してください。
	if ( LANG == "ja" )
	{
		if( !$_popular_plugin_frame )
			$_popular_plugin_frame = '<h5>人気の%d件</h5><div>%s</div>';
		if( !$_popular_plugin_today_frame )
			$_popular_plugin_today_frame = '<h5>今日の%d件</h5><div>%s</div>';
		if( !$_popular_plugin_yesterday_frame )
			$_popular_plugin_yesterday_frame = '<h5>昨日の%d件</h5><div>%s</div>';
		if( !$_popular_plugin_recent_frame )
			$_popular_plugin_recent_frame = '<h5>最近の%d件</h5><div>%s</div>';
		if( !$_popular_plugin_notitle_frame )
			 $_popular_plugin_notitle_frame = '<div>%s</div>';
	}
	else
	{
		if( !$_popular_plugin_frame )
			$_popular_plugin_frame = '<h5>popular(%d)</h5><div>%s</div>';
		if( !$_popular_plugin_today_frame )
			$_popular_plugin_today_frame = '<h5>today\'s(%d)</h5><div>%s</div>';
		if( !$_popular_plugin_yesterday_frame )
			$_popular_plugin_yesterday_frame = '<h5>yesterday\'s(%d)</h5><div>%s</div>';
		if( !$_popular_plugin_recent_frame )
			$_popular_plugin_recent_frame = '<h5>recent\'s pop(%d)</h5><div>%s</div>';
		if( !$_popular_plugin_notitle_frame )
			 $_popular_plugin_notitle_frame = '<div>%s</div>';
	}
}

function plugin_popular2_convert()
{
	global $vars, $whatsnew, $non_list;
	global $_popular_plugin_frame, $_popular_plugin_today_frame, $_popular_plugin_yesterday_frame, 
	       $_popular_plugin_recent_frame, $_popular_plugin_notitle_frame;

	$args = func_get_args();
	$params = array(
		'deepest'    => FALSE,
		'notitle'   => FALSE,
		'short' => FALSE,
	);
	if( func_num_args() <= 3 ) 
	{
		switch (func_num_args()) {
		case 3: $list = $args[2]; 
		case 2: $except = $args[1];
		case 1: $max = $args[0];
		}
	}
	else
	{
		$max = array_shift($args);
		$except = array_shift($args);
		$list = array_shift($args);
		array_walk($args, 'plugin_popular2_check_arg', &$params);
	}
	if($list == '') $list = 'all';
	if($except == '') $except = '';
	if($max == '') $max = 10;
	$today = get_date('Y/m/d');
	$yesterday = get_date('Y/m/d',strtotime('yesterday',UTIME));
	
	$counters = array();
	// COUNTER_DIR には消去されたページのカウンタがまだあるので具合が悪い
	// $pages = get_existpages(COUNTER_DIR, '.count');
	$pages = get_existpages(DATA_DIR, '.txt');
	if ( $params['deepest'] )
	{
		asort($pages);
	}		
	while( list( $file, $page ) = each ( $pages ) )
	{
		if( $params['deepest'] ) {
			if( list( $nfile, $npage ) = each ( $pages ) )
			{
				prev( $pages );
				if( strpos( $npage , "$page/" ) !== false ) {
					continue;
				}
			}
		}
		
		if (($except != '' && ereg($except, $page)) ||
		    $page == $whatsnew || preg_match("/$non_list/", $page) ||
		    ! is_page($page)) {
			continue;
		}
		
		$file = substr( $file, 0, strlen($file)-strlen(".txt") ) . ".count";
		$array = file(COUNTER_DIR . $file);
		$count = rtrim($array[0]);
		$date  = rtrim($array[1]);
		$today_count = rtrim($array[2]); //$date に対しての today
		$yesterday_count = rtrim($array[3]); //$date に対しての yesterday

		if ($list == "today" or $list == "true" or $list == "recent" ) {
			// $pageが数値に見える(たとえばencode('BBS')=424253)とき、
			// array_splice()によってキー値が変更されてしまうのを防ぐ
			if ($today == $date) $counters["_$page"] = $today_count;
		} if ($list == "yesterday" or $list == "recent" ) {
			if ($today == $date and $yesterday_count > 0 ) {
				$counters["_$page"] += $yesterday_count;
			}
			else if ( $yesterday == $date ) {
				$counters["_$page"] = $today_count;
			}
		} else if ($list == "all" or $list == "false") {
			$counters["_$page"] = $count;
		}
	}

	asort($counters, SORT_NUMERIC);
	$counters = array_splice(array_reverse($counters, TRUE), 0, $max);

	$items = '';
	if (! empty($counters)) {
		$items = "<ul class=\"popular_list\">\n";

		foreach ($counters as $page=>$count) {
			$page = substr($page, 1);

			$s_page = htmlspecialchars($page);
			$ss_page = $s_page;
			if( $params['short'] )
			{
				if( ( $pos = strrpos($s_page,"/") ) !== false )
				{
					$ss_page = substr( $s_page, $pos+1 );
				}
			}
			
			if ($page == $vars['page']) {
				// No need to link itself, notifies where you just read
				$pg_passage = get_pg_passage($page,FALSE);
				$items .= " <li><span title=\"$s_page $pg_passage\">$ss_page<span class=\"counter\">($count)</span></span></li>\n";
			} else {
				$items .= ' <li>' . make_pagelink($page, "$ss_page<span class=\"counter\">($count)</span>") . "</li>\n";
			}
		}
		$items .= "</ul>\n";
	}

	if( $params['notitle'] )
	{
		$frame = $_popular_plugin_notitle_frame;
		return sprintf("<div>%s</div>",$items);
	}
	else
	{
		if($list == "today" or $list == "true" )
			$frame = $_popular_plugin_today_frame;
		else if($list == "yesterday")
			$frame = $_popular_plugin_yesterday_frame;
		else if($list == "all" or $list == "false" )
			$frame = $_popular_plugin_frame;
		else if($list == "recent")
			$frame = $_popular_plugin_recent_frame;
		return sprintf($frame,count($counters),$items);
	}
}

//オプションを解析する
function plugin_popular2_check_arg($pval, $pkey, &$params)
{
	if ($pval == '')
	{
		return;
	}
	$key = $pval;
	if( array_key_exists( $key, $params ) )
	{
		$params[$key] = TRUE;
	}
}
?>
