OSDN Git Service

Convert character code of the source code to UTF-8 from EUC-JP
[pukiwiki/pukiwiki.git] / plugin / calendar_viewer.inc.php
index 29a9e5e..3b82823 100644 (file)
@@ -1,80 +1,95 @@
 <?php
+// PukiWiki - Yet another WikiWikiWeb clone
+// $Id: calendar_viewer.inc.php,v 1.37 2011/01/25 15:01:01 henoheno Exp $
+// Copyright (C) 2002-2005, 2007 PukiWiki Developers Team
+// License: GPL v2 or (at your option) any later version
+//
+// Calendar viewer plugin - List pages that calendar/calnedar2 plugin created
+// (Based on calendar and recent plugin)
+
+// Page title's date format
+//  * See PHP date() manual for detail
+//  * '$\w' = weeklabel defined in $_msg_week
+define('PLUGIN_CALENDAR_VIEWER_DATE_FORMAT',
+       //      FALSE         // 'pagename/2004-02-09' -- As is
+       //      'D, d M, Y'   // 'Mon, 09 Feb, 2004'
+       //      'F d, Y'      // 'February 09, 2004'
+       //      '[Y-m-d]'     // '[2004-02-09]'
+               'Y/n/j ($\w)' // '2004/2/9 (Mon)'
+       );
+
+// ----
+
+define('PLUGIN_CALENDAR_VIEWER_USAGE',
+       '#calendar_viewer(pagename,this|yyyy-mm|n|x*y[,mode[,separater]])');
 /*
- * PukiWiki calendar_viewer plubin
- * $Id: calendar_viewer.inc.php,v 1.26 2004/12/30 09:21:05 henoheno Exp $
- * Based on calendarrecent plugin
- */
-
-/*
- * ³µÍ×
- * calendar¥×¥é¥°¥¤¥ó¤äcalendar2¥×¥é¥°¥¤¥ó¤ÇºîÀ®¤·¤¿¥Ú¡¼¥¸¤ò°ìÍ÷ɽ¼¨¤¹¤ë¤¿¤á¤Î¥×¥é¥°¥¤¥ó¤Ç¤¹¡£
- * »È¤¤Êý
- *  #calendar_viewer(pagename,(this|yyyy-mm|n|x*y),[mode],[separater])
- *
  ** pagename
- * - calendar or calendar2¥×¥é¥°¥¤¥ó¤òµ­½Ò¤·¤Æ¤ë¥Ú¡¼¥¸Ì¾
+ * - A working root of calendar or calendar2 plugin
+ *   pagename/2004-12-30
+ *   pagename/2004-12-31
+ *   ...
  *
  ** (yyyy-mm|n|this)
- * this    - º£·î¤Î¥Ú¡¼¥¸¤ò°ìÍ÷ɽ¼¨
- * yyyy-mm - yyyy-mm¤Ç»ØÄꤷ¤¿Ç¯·î¤Î¥Ú¡¼¥¸¤ò°ìÍ÷ɽ¼¨
- * n       - ÀèƬ¤«¤én·ï¤ò°ìÍ÷ɽ¼¨
- * x*n     - ÀèƬ¤è¤ê¿ô¤¨¤Æ x ¥Ú¡¼¥¸ÌÜ(ÀèƬ¤Ï0)¤«¤é¡¢y·ï¤Å¤Ä°ìÍ÷ɽ¼¨
+ * this    - Show 'this month'
+ * yyyy-mm - Show pages at year:yyyy and month:mm
+ * n       - Show first n pages
+ * x*n     - Show first n pages from x-th page (0 = first)
  *
  ** [mode]
- * Default:past
- * past   - º£Æü°ÊÁ°¤Î¥Ú¡¼¥¸¤Î°ìÍ÷ɽ¼¨¥â¡¼¥É¡£¹¹¿·ÍúÎò¤äÆüµ­¸þ¤­
- * future - º£Æü°Ê¹ß¤Î¥Ú¡¼¥¸¤Î°ìÍ÷ɽ¼¨¥â¡¼¥É¡£¥¤¥Ù¥ó¥ÈͽÄê¤ä¥¹¥±¥¸¥å¡¼¥ë¸þ¤­
- * view   - ²áµî¤«¤é̤Íè¤Ø¤Î°ìÍ÷ɽ¼¨¥â¡¼¥É¡£É½¼¨Í޻ߤ¹¤ë¥Ú¡¼¥¸¤Ï¤¢¤ê¤Þ¤»¤ó
+ * past   - Show today, and the past below. Recommended for ChangeLogs and diaries (default)
+ * future - Show today, and the future below. Recommended for event planning and scheduling
+ * view   - Show all, from the past to the future
  *
  ** [separater]
- * - Ç¯·îÆü¤ò¶èÀڤ륻¥Ñ¥ì¡¼¥¿¤ò»ØÄê¡£
- * - ¾Êά²Äǽ¡£¥Ç¥Õ¥©¥ë¥È¤Ï-¡£¡Êcalendar2¤Ê¤é¾Êά¤ÇOK¡Ë
+ * - Specify separator of yyyy/mm/dd
+ * - Default: '-' (yyyy-mm-dd)
  *
  * TODO
- *  past or future ¤Ç·îñ°Ìɽ¼¨¤¹¤ë¤È¤­¤Ë¡¢¤½¤ì¤¾¤ìÍè·î¡¢Àè·î¤Î°ìÍ÷¤Ø¤Î¥ê¥ó¥¯¤òɽ¼¨¤·¤Ê¤¤¤è¤¦¤Ë¤¹¤ë
+ *  Stop showing links 'next month' and 'previous month' with past/future mode for 'this month'
+ *    #calendar_viewer(pagename,this,past)
  */
 
 function plugin_calendar_viewer_convert()
 {
-       global $vars, $get, $post, $hr, $script;
-       global $_err_calendar_viewer_param, $_err_calendar_viewer_param2;
+       global $vars, $get, $post, $script, $weeklabels;
        global $_msg_calendar_viewer_right, $_msg_calendar_viewer_left;
-       global $_msg_calendar_viewer_restrict;
+       global $_msg_calendar_viewer_restrict, $_err_calendar_viewer_param2;
 
        static $viewed = array();
 
        if (func_num_args() < 2)
-               return '#calendar_viewer(): ' . $_err_calendar_viewer_param . '<br />' . "\n";
+               return PLUGIN_CALENDAR_VIEWER_USAGE . '<br />' . "\n";
 
        $func_args = func_get_args();
 
        // Default values
-       $pagename    = $func_args[0];   // ´ð½à¤È¤Ê¤ë¥Ú¡¼¥¸Ì¾
-       $page_YM     = '';      // °ìÍ÷ɽ¼¨¤¹¤ëǯ·î
-       $limit_base  = 0;       // ÀèƬ¤«¤é¿ô¤¨¤Æ²¿¥Ú¡¼¥¸Ìܤ«¤éɽ¼¨¤¹¤ë¤« (ÀèƬ)
-       $limit_pitch = 0;       // ²¿·ï¤Å¤Äɽ¼¨¤¹¤ë¤«
-       $limit_page  = 0;       // ¥µ¡¼¥Á¤¹¤ë¥Ú¡¼¥¸¿ô
-       $mode        = 'past';  // Æ°ºî¥â¡¼¥É
-       $date_sep    = '-';     // ÆüÉդΥ»¥Ñ¥ì¡¼¥¿ calendar2¤Ê¤é '-', calendar¤Ê¤é ''
+       $pagename    = $func_args[0];   // 基準となるページ名
+       $page_YM     = '';      // 一覧表示する年月
+       $limit_base  = 0;       // 先頭から数えて何ページ目から表示するか (先頭)
+       $limit_pitch = 0;       // 何件づつ表示するか
+       $limit_page  = 0;       // サーチするページ数
+       $mode        = 'past';  // 動作モード
+       $date_sep    = '-';     // 日付のセパレータ calendar2なら '-', calendarなら ''
 
        // Check $func_args[1]
+       $matches = array();
        if (preg_match('/[0-9]{4}' . $date_sep . '[0-9]{2}/', $func_args[1])) {
-               // »ØÄêǯ·î¤Î°ìÍ÷ɽ¼¨
+               // 指定年月の一覧表示
                $page_YM     = $func_args[1];
                $limit_page  = 31;
        } else if (preg_match('/this/si', $func_args[1])) {
-               // º£·î¤Î°ìÍ÷ɽ¼¨
+               // 今月の一覧表示
                $page_YM     = get_date('Y' . $date_sep . 'm');
                $limit_page  = 31;
        } else if (preg_match('/^[0-9]+$/', $func_args[1])) {
-               // nÆüʬɽ¼¨
+               // n日分表示
                $limit_pitch = $func_args[1];
                $limit_page  = $func_args[1];
        } else if (preg_match('/(-?[0-9]+)\*([0-9]+)/', $func_args[1], $matches)) {
-               // ÀèƬ¤è¤ê¿ô¤¨¤Æ x ¥Ú¡¼¥¸Ìܤ«¤é¡¢y·ï¤Å¤Äɽ¼¨
+               // 先頭より数えて x ページ目から、y件づつ表示
                $limit_base  = $matches[1];
                $limit_pitch = $matches[2];
-               $limit_page  = $matches[1] + $matches[2]; // ÆɤßÈô¤Ð¤¹ + É½¼¨¤¹¤ë
+               $limit_page  = $matches[1] + $matches[2]; // 読み飛ばす + 表示する
        } else {
                return '#calendar_viewer(): ' . $_err_calendar_viewer_param2 . '<br />' . "\n";
        }
@@ -88,15 +103,15 @@ function plugin_calendar_viewer_convert()
 
        // Avoid Loop etc.
        if (isset($viewed[$pagename])) {
-               $s_page = htmlspecialchars($pagename);
+               $s_page = htmlsc($pagename);
                return "#calendar_viewer(): You already view: $s_page<br />";
        } else {
                $viewed[$pagename] = TRUE; // Valid
        }
 
-       // °ìÍ÷ɽ¼¨¤¹¤ë¥Ú¡¼¥¸Ì¾¤È¥Õ¥¡¥¤¥ë̾¤Î¥Ñ¥¿¡¼¥ó¡¡¥Õ¥¡¥¤¥ë̾¤Ë¤Ïǯ·î¤ò´Þ¤à
+       // 一覧表示するページ名とファイル名のパターン ファイル名には年月を含む
        if ($pagename == '') {
-               // pagename̵¤·¤Îyyyy-mm-dd¤ËÂбþ¤¹¤ë¤¿¤á¤Î½èÍý
+               // pagename無しのyyyy-mm-ddに対応するための処理
                $pagepattern     = '';
                $pagepattern_len = 0;
                $filepattern     = encode($page_YM);
@@ -108,7 +123,7 @@ function plugin_calendar_viewer_convert()
                $filepattern_len = strlen($filepattern);
        }
 
-       // ¥Ú¡¼¥¸¥ê¥¹¥È¤Î¼èÆÀ
+       // ページリストの取得
        $pagelist = array();
        if ($dir = @opendir(DATA_DIR)) {
                $_date = get_date('Y' . $date_sep . 'm' . $date_sep . 'd');
@@ -134,16 +149,16 @@ function plugin_calendar_viewer_convert()
        closedir($dir);
 
        if ($mode == 'past') {
-               rsort($pagelist);       // New => Old
+               rsort($pagelist, SORT_STRING);  // New => Old
        } else {
-               sort($pagelist);        // Old => New
+               sort($pagelist, SORT_STRING);   // Old => New
        }
 
        // Include start
        $tmppage     = $vars['page'];
        $return_body = '';
 
-       // $limit_page ¤Î·ï¿ô¤Þ¤Ç¥¤¥ó¥¯¥ë¡¼¥É
+       // $limit_page の件数までインクルード
        $tmp = max($limit_base, 0); // Skip minus
        while ($tmp < $limit_page) {
                if (! isset($pagelist[$tmp])) break;
@@ -151,7 +166,7 @@ function plugin_calendar_viewer_convert()
                $page = $pagelist[$tmp];
                $get['page'] = $post['page'] = $vars['page'] = $page;
 
-               // ¸½¾õ¤Ç±ÜÍ÷µö²Ä¤¬¤¢¤ë¾ì¹ç¤À¤±É½¼¨¤¹¤ë
+               // 現状で閲覧許可がある場合だけ表示する
                if (check_readable($page, FALSE, FALSE)) {
                        $body = convert_html(get_source($page));
                } else {
@@ -159,25 +174,47 @@ function plugin_calendar_viewer_convert()
                }
 
                $r_page = rawurlencode($page);
-               $s_page = htmlspecialchars($page);
-               $link   = '<a href="' . $script . '?cmd=edit&amp;page=' . $r_page . '">' . $s_page . '</a>';
+
+               if (PLUGIN_CALENDAR_VIEWER_DATE_FORMAT !== FALSE) {
+                       $time = strtotime(basename($page)); // $date_sep must be assumed '-' or ''!
+                       if ($time == -1) {
+                               $s_page = htmlsc($page); // Failed. Why?
+                       } else {
+                               $week   = $weeklabels[date('w', $time)];
+                               $s_page = htmlsc(str_replace(
+                                               array('$w' ),
+                                               array($week),
+                                               date(PLUGIN_CALENDAR_VIEWER_DATE_FORMAT, $time)
+                                       ));
+                       }
+               } else {
+                       $s_page = htmlsc($page);
+               }
+
+               if (PKWK_READONLY) {
+                       $link   = $script . '?' . $r_page;
+               } else {
+                       $link   = $script . '?cmd=edit&amp;page=' . $r_page;
+               }
+               $link   = '<a href="' . $link . '">' . $s_page . '</a>';
+
                $head   = '<h1>' . $link . '</h1>' . "\n";
                $return_body .= $head . $body;
 
                ++$tmp;
        }
 
-       // ¤³¤³¤Ç¡¢Á°¸å¤Î¥ê¥ó¥¯¤òɽ¼¨
-       // ?plugin=calendar_viewer&file=¥Ú¡¼¥¸Ì¾&date=yyyy-mm
+       // ここで、前後のリンクを表示
+       // ?plugin=calendar_viewer&file=ページ名&date=yyyy-mm
        $enc_pagename = rawurlencode(substr($pagepattern, 0, $pagepattern_len - 1));
 
        if ($page_YM != '') {
-               // Ç¯·îɽ¼¨»þ
+               // 年月表示時
                $date_sep_len = strlen($date_sep);
                $this_year    = substr($page_YM, 0, 4);
                $this_month   = substr($page_YM, 4 + $date_sep_len, 2);
 
-               // ¼¡·î
+               // 次月
                $next_year  = $this_year;
                $next_month = $this_month + 1;
                if ($next_month > 12) {
@@ -186,7 +223,7 @@ function plugin_calendar_viewer_convert()
                }
                $next_YM = sprintf('%04d%s%02d', $next_year, $date_sep, $next_month);
 
-               // Á°·î
+               // 前月
                $prev_year  = $this_year;
                $prev_month = $this_month - 1;
                if ($prev_month < 1) {
@@ -206,25 +243,25 @@ function plugin_calendar_viewer_convert()
                        $right_text = $next_YM . '&gt;&gt;'; // >>
                }
        } else {
-               // n·ïɽ¼¨»þ
-               if ($limit_base < 0) {
-                       $left_YM = ''; // É½¼¨¤·¤Ê¤¤ (¤½¤ì¤è¤êÁ°¤Î¹àÌܤϤʤ¤)
+               // n件表示時
+               if ($limit_base <= 0) {
+                       $left_YM = ''; // 表示しない (それより前の項目はない)
                } else {
                        $left_YM   = $limit_base - $limit_pitch . '*' . $limit_pitch;
                        $left_text = sprintf($_msg_calendar_viewer_left, $limit_pitch);
 
                }
                if ($limit_base + $limit_pitch >= count($pagelist)) {
-                       $right_YM = ''; // É½¼¨¤·¤Ê¤¤ (¤½¤ì¤è¤ê¸å¤Î¹àÌܤϤʤ¤)
+                       $right_YM = ''; // 表示しない (それより後の項目はない)
                } else {
                        $right_YM   = $limit_base + $limit_pitch . '*' . $limit_pitch;
                        $right_text = sprintf($_msg_calendar_viewer_right, $limit_pitch);
                }
        }
 
-       // ¥Ê¥Ó¥²¡¼¥ÈÍѤΥê¥ó¥¯¤òËöÈø¤ËÄɲÃ
+       // ナビゲート用のリンクを末尾に追加
        if ($left_YM != '' || $right_YM != '') {
-               $s_date_sep = htmlspecialchars($date_sep);
+               $s_date_sep = htmlsc($date_sep);
                $left_link = $right_link = '';
                $link = $script . '?plugin=calendar_viewer&amp;mode=' . $mode .
                        '&amp;file=' . $enc_pagename . '&amp;date_sep=' . $s_date_sep . '&amp;';
@@ -234,12 +271,12 @@ function plugin_calendar_viewer_convert()
                if ($right_YM != '')
                        $right_link = '<a href="' . $link .
                                'date=' . $right_YM . '">' . $right_text . '</a>';
-               // past mode¤Ï<<¿· µì>> Â¾¤Ï<<µì ¿·>>
+               // past modeは<<新 旧>> 他は<<旧 新>>
                $return_body .=
-                       '<table width ="100%"><tr>' .
-                       '<td align="left">'  . $left_link  . '</td>' .
-                       '<td align="right">' . $right_link . '</td>' .
-                       '</tr></table>';
+                       '<div class="calendar_viewer">' .
+                       '<span class="calendar_viewer_left">'  . $left_link  . '</span>' .
+                       '<span class="calendar_viewer_right">' . $right_link . '</span>' .
+                       '</div>';
        }
 
        $get['page'] = $post['page'] = $vars['page'] = $tmppage;
@@ -249,7 +286,7 @@ function plugin_calendar_viewer_convert()
 
 function plugin_calendar_viewer_action()
 {
-       global $vars, $get, $post, $hr, $script;
+       global $vars, $get, $post, $script;
 
        $date_sep = '-';
 
@@ -269,12 +306,12 @@ function plugin_calendar_viewer_action()
        $return_vars_array['body'] = call_user_func_array('plugin_calendar_viewer_convert', $args_array);
 
        //$return_vars_array['msg'] = 'calendar_viewer ' . $vars['page'] . '/' . $page_YM;
-       $return_vars_array['msg'] = 'calendar_viewer ' . htmlspecialchars($vars['page']);
+       $return_vars_array['msg'] = 'calendar_viewer ' . htmlsc($vars['page']);
        if ($vars['page'] != '') $return_vars_array['msg'] .= '/';
        if (preg_match('/\*/', $page_YM)) {
-               // ¤¦¡¼¤ó¡¢n·ïɽ¼¨¤Î»þ¤Ï¤Ê¤ó¤Æ¥Ú¡¼¥¸Ì¾¤Ë¤·¤¿¤é¤¤¤¤¡©
+               // うーん、n件表示の時はなんてページ名にしたらいい?
        } else {
-               $return_vars_array['msg'] .= htmlspecialchars($page_YM);
+               $return_vars_array['msg'] .= htmlsc($page_YM);
        }
 
        $vars['page'] = $page;
@@ -283,14 +320,14 @@ function plugin_calendar_viewer_action()
 
 function plugin_calendar_viewer_isValidDate($aStr, $aSepList = '-/ .')
 {
+       $matches = array();
        if ($aSepList == '') {
-               // yyymmdd¤È¤·¤Æ¥Á¥§¥Ã¥¯¡Ê¼êÈ´¤­(^^;¡Ë
+               // yyymmddとしてチェック(手抜き(^^;)
                return checkdate(substr($aStr, 4, 2), substr($aStr, 6, 2), substr($aStr, 0, 4));
-       } else if (ereg("^([0-9]{2,4})[$aSepList]([0-9]{1,2})[$aSepList]([0-9]{1,2})$", $aStr, $m) ) {
-               return checkdate($m[2], $m[3], $m[1]);
+       } else if (ereg("^([0-9]{2,4})[$aSepList]([0-9]{1,2})[$aSepList]([0-9]{1,2})$", $aStr, $matches) ) {
+               return checkdate($matches[2], $matches[3], $matches[1]);
        } else {
                return FALSE;
        }
 }
-
 ?>