2 // PukiWiki - Yet another WikiWikiWeb clone
3 // calendar_viewer.inc.php
4 // Copyright 2002-2017 PukiWiki Development Team
5 // License: GPL v2 or (at your option) any later version
7 // Calendar viewer plugin - List pages that calendar/calnedar2 plugin created
8 // (Based on calendar and recent plugin)
10 // Page title's date format
11 // * See PHP date() manual for detail
12 // * '$\w' = weeklabel defined in $_msg_week
13 define('PLUGIN_CALENDAR_VIEWER_DATE_FORMAT',
14 // FALSE // 'pagename/2004-02-09' -- As is
15 // 'D, d M, Y' // 'Mon, 09 Feb, 2004'
16 // 'F d, Y' // 'February 09, 2004'
17 // '[Y-m-d]' // '[2004-02-09]'
18 'Y/n/j ($\w)' // '2004/2/9 (Mon)'
21 * Limit of show count per pagename
23 define('PLUGIN_CALENDAR_VIEWER_MAX_SHOW_COUNT', 4);
27 define('PLUGIN_CALENDAR_VIEWER_USAGE',
28 '#calendar_viewer(pagename,this|yyyy-mm|n|x*y[,mode[,separater]])');
31 * - A working root of calendar or calendar2 plugin
37 * this - Show 'this month'
38 * yyyy-mm - Show pages at year:yyyy and month:mm
39 * n - Show first n pages
40 * x*n - Show first n pages from x-th page (0 = first)
43 * past - Show today, and the past below. Recommended for ChangeLogs and diaries (default)
44 * future - Show today, and the future below. Recommended for event planning and scheduling
45 * view - Show all, from the past to the future
48 * - Specify separator of yyyy/mm/dd
49 * - Default: '-' (yyyy-mm-dd)
52 * Stop showing links 'next month' and 'previous month' with past/future mode for 'this month'
53 * #calendar_viewer(pagename,this,past)
56 function plugin_calendar_viewer_convert()
58 global $vars, $get, $post, $script, $weeklabels;
59 global $_msg_calendar_viewer_right, $_msg_calendar_viewer_left;
60 global $_msg_calendar_viewer_restrict, $_err_calendar_viewer_param2;
62 static $show_count = array();
64 if (func_num_args() < 2)
65 return PLUGIN_CALENDAR_VIEWER_USAGE . '<br />' . "\n";
67 $func_args = func_get_args();
70 $pagename = $func_args[0]; // 基準となるページ名
71 $page_YM = ''; // 一覧表示する年月
72 $limit_base = 0; // 先頭から数えて何ページ目から表示するか (先頭)
73 $limit_pitch = 0; // 何件づつ表示するか
74 $limit_page = 0; // サーチするページ数
75 $mode = 'past'; // 動作モード
76 $date_sep = '-'; // 日付のセパレータ calendar2なら '-', calendarなら ''
78 // Check $func_args[1]
80 if (preg_match('/[0-9]{4}' . $date_sep . '[0-9]{2}/', $func_args[1])) {
82 $page_YM = $func_args[1];
84 } else if (preg_match('/this/si', $func_args[1])) {
86 $page_YM = get_date('Y' . $date_sep . 'm');
88 } else if (preg_match('/^[0-9]+$/', $func_args[1])) {
90 $limit_pitch = $func_args[1];
91 $limit_page = $func_args[1];
92 } else if (preg_match('/(-?[0-9]+)\*([0-9]+)/', $func_args[1], $matches)) {
93 // 先頭より数えて x ページ目から、y件づつ表示
94 $limit_base = $matches[1];
95 $limit_pitch = $matches[2];
96 $limit_page = $matches[1] + $matches[2]; // 読み飛ばす + 表示する
98 return '#calendar_viewer(): ' . $_err_calendar_viewer_param2 . '<br />' . "\n";
101 // $func_args[2]: Mode setting
102 if (isset($func_args[2]) && preg_match('/^(past|view|future)$/si', $func_args[2]))
103 $mode = $func_args[2];
105 // $func_args[3]: Change default delimiter
106 if (isset($func_args[3])) $date_sep = $func_args[3];
109 if (!isset($show_count[$pagename])) {
110 $show_count[$pagename] = 0;
112 $show_count[$pagename] += 1;
113 if ($show_count[$pagename] > PLUGIN_CALENDAR_VIEWER_MAX_SHOW_COUNT) {
114 $s_page = htmlsc($pagename);
115 return "#calendar_viewer(): Exceeded the limit of show count: $s_page<br />";
118 $pagepattern = strip_bracket($pagename) . '/';
119 if ($pagename === '') {
120 // Support non-pagename yyyy-mm-dd pattern
123 $pagepattern_len = strlen($pagepattern);
126 $_date = get_date('Y' . $date_sep . 'm' . $date_sep . 'd');
127 foreach (get_existpages() as $page) {
128 if (strncmp($page, $pagepattern, $pagepattern_len) !== 0) continue;
129 $page_date = substr($page, $pagepattern_len);
130 // Verify the $page_date pattern (Default: yyyy-mm-dd).
131 // Past-mode hates the future, and
132 // Future-mode hates the past.
133 if ((plugin_calendar_viewer_isValidDate($page_date, $date_sep) === FALSE) ||
134 ($page_date > $_date && ($mode === 'past')) ||
135 ($page_date < $_date && ($mode === 'future'))) {
140 if ($mode == 'past') {
141 rsort($pagelist, SORT_STRING); // New => Old
143 sort($pagelist, SORT_STRING); // Old => New
146 $tmppage = $vars['page'];
149 // $limit_page の件数までインクルード
150 $tmp = max($limit_base, 0); // Skip minus
151 while ($tmp < $limit_page) {
152 if (! isset($pagelist[$tmp])) break;
154 $page = $pagelist[$tmp];
155 $get['page'] = $post['page'] = $vars['page'] = $page;
157 // 現状で閲覧許可がある場合だけ表示する
158 if (check_readable($page, FALSE, FALSE)) {
159 $body = convert_html(get_source($page));
161 $body = str_replace('$1', $page, $_msg_calendar_viewer_restrict);
164 $r_page = pagename_urlencode($page);
166 if (PLUGIN_CALENDAR_VIEWER_DATE_FORMAT !== FALSE) {
167 $time = strtotime(basename($page)); // $date_sep must be assumed '-' or ''!
168 if ($time === FALSE || $time === -1) {
169 $s_page = htmlsc($page); // Failed. Why?
171 $week = $weeklabels[date('w', $time)];
172 $s_page = htmlsc(str_replace(
175 date(PLUGIN_CALENDAR_VIEWER_DATE_FORMAT, $time)
179 $s_page = htmlsc($page);
183 $link = $script . '?' . $r_page;
185 $link = $script . '?cmd=edit&page=' . $r_page;
187 $link = '<a href="' . $link . '">' . $s_page . '</a>';
189 $head = '<h1>' . $link . '</h1>' . "\n";
190 $return_body .= $head . $body;
196 // ?plugin=calendar_viewer&file=ページ名&date=yyyy-mm
197 $enc_pagename = rawurlencode(substr($pagepattern, 0, $pagepattern_len - 1));
199 if ($page_YM != '') {
201 $date_sep_len = strlen($date_sep);
202 $this_year = substr($page_YM, 0, 4);
203 $this_month = substr($page_YM, 4 + $date_sep_len, 2);
206 $next_year = $this_year;
207 $next_month = $this_month + 1;
208 if ($next_month > 12) {
212 $next_YM = sprintf('%04d%s%02d', $next_year, $date_sep, $next_month);
215 $prev_year = $this_year;
216 $prev_month = $this_month - 1;
217 if ($prev_month < 1) {
221 $prev_YM = sprintf('%04d%s%02d', $prev_year, $date_sep, $prev_month);
222 if ($mode == 'past') {
223 $right_YM = $prev_YM;
224 $right_text = $prev_YM . '>>'; // >>
226 $left_text = '<<' . $next_YM; // <<
229 $left_text = '<<' . $prev_YM; // <<
230 $right_YM = $next_YM;
231 $right_text = $next_YM . '>>'; // >>
235 if ($limit_base <= 0) {
236 $left_YM = ''; // 表示しない (それより前の項目はない)
238 $left_YM = $limit_base - $limit_pitch . '*' . $limit_pitch;
239 $left_text = sprintf($_msg_calendar_viewer_left, $limit_pitch);
242 if ($limit_base + $limit_pitch >= count($pagelist)) {
243 $right_YM = ''; // 表示しない (それより後の項目はない)
245 $right_YM = $limit_base + $limit_pitch . '*' . $limit_pitch;
246 $right_text = sprintf($_msg_calendar_viewer_right, $limit_pitch);
251 if ($left_YM != '' || $right_YM != '') {
252 $s_date_sep = htmlsc($date_sep);
253 $left_link = $right_link = '';
254 $link = $script . '?plugin=calendar_viewer&mode=' . $mode .
255 '&file=' . $enc_pagename . '&date_sep=' . $s_date_sep . '&';
257 $left_link = '<a href="' . $link .
258 'date=' . $left_YM . '">' . $left_text . '</a>';
260 $right_link = '<a href="' . $link .
261 'date=' . $right_YM . '">' . $right_text . '</a>';
262 // past modeは<<新 旧>> 他は<<旧 新>>
264 '<div class="calendar_viewer">' .
265 '<span class="calendar_viewer_left">' . $left_link . '</span>' .
266 '<span class="calendar_viewer_right">' . $right_link . '</span>' .
270 $get['page'] = $post['page'] = $vars['page'] = $tmppage;
275 function plugin_calendar_viewer_action()
277 global $vars, $get, $post, $script;
281 $return_vars_array = array();
283 $page = strip_bracket($vars['page']);
285 if (isset($vars['file'])) $vars['page'] = $vars['file'];
287 $date_sep = $vars['date_sep'];
289 $page_YM = $vars['date'];
290 if ($page_YM == '') $page_YM = get_date('Y' . $date_sep . 'm');
291 $mode = $vars['mode'];
293 $args_array = array($vars['page'], $page_YM, $mode, $date_sep);
294 $return_vars_array['body'] = call_user_func_array('plugin_calendar_viewer_convert', $args_array);
296 //$return_vars_array['msg'] = 'calendar_viewer ' . $vars['page'] . '/' . $page_YM;
297 $return_vars_array['msg'] = 'calendar_viewer ' . htmlsc($vars['page']);
298 if ($vars['page'] != '') $return_vars_array['msg'] .= '/';
299 if (preg_match('/\*/', $page_YM)) {
300 // うーん、n件表示の時はなんてページ名にしたらいい?
302 $return_vars_array['msg'] .= htmlsc($page_YM);
305 $vars['page'] = $page;
306 return $return_vars_array;
309 function plugin_calendar_viewer_isValidDate($aStr, $aSepList = '-/ .')
312 if ($aSepList == '') {
313 // yyymmddとしてチェック(手抜き(^^;)
314 return checkdate(substr($aStr, 4, 2), substr($aStr, 6, 2), substr($aStr, 0, 4));
315 } else if (preg_match("#^([0-9]{2,4})[$aSepList]([0-9]{1,2})[$aSepList]([0-9]{1,2})$#", $aStr, $matches) ) {
316 return checkdate($matches[2], $matches[3], $matches[1]);