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)'
23 define('PLUGIN_CALENDAR_VIEWER_USAGE',
24 '#calendar_viewer(pagename,this|yyyy-mm|n|x*y[,mode[,separater]])');
27 * - A working root of calendar or calendar2 plugin
33 * this - Show 'this month'
34 * yyyy-mm - Show pages at year:yyyy and month:mm
35 * n - Show first n pages
36 * x*n - Show first n pages from x-th page (0 = first)
39 * past - Show today, and the past below. Recommended for ChangeLogs and diaries (default)
40 * future - Show today, and the future below. Recommended for event planning and scheduling
41 * view - Show all, from the past to the future
44 * - Specify separator of yyyy/mm/dd
45 * - Default: '-' (yyyy-mm-dd)
48 * Stop showing links 'next month' and 'previous month' with past/future mode for 'this month'
49 * #calendar_viewer(pagename,this,past)
52 function plugin_calendar_viewer_convert()
54 global $vars, $get, $post, $script, $weeklabels;
55 global $_msg_calendar_viewer_right, $_msg_calendar_viewer_left;
56 global $_msg_calendar_viewer_restrict, $_err_calendar_viewer_param2;
58 static $show_count = array();
60 if (func_num_args() < 2)
61 return PLUGIN_CALENDAR_VIEWER_USAGE . '<br />' . "\n";
63 $func_args = func_get_args();
66 $pagename = $func_args[0]; // 基準となるページ名
67 $page_YM = ''; // 一覧表示する年月
68 $limit_base = 0; // 先頭から数えて何ページ目から表示するか (先頭)
69 $limit_pitch = 0; // 何件づつ表示するか
70 $limit_page = 0; // サーチするページ数
71 $mode = 'past'; // 動作モード
72 $date_sep = '-'; // 日付のセパレータ calendar2なら '-', calendarなら ''
74 // Check $func_args[1]
76 if (preg_match('/[0-9]{4}' . $date_sep . '[0-9]{2}/', $func_args[1])) {
78 $page_YM = $func_args[1];
80 } else if (preg_match('/this/si', $func_args[1])) {
82 $page_YM = get_date('Y' . $date_sep . 'm');
84 } else if (preg_match('/^[0-9]+$/', $func_args[1])) {
86 $limit_pitch = $func_args[1];
87 $limit_page = $func_args[1];
88 } else if (preg_match('/(-?[0-9]+)\*([0-9]+)/', $func_args[1], $matches)) {
89 // 先頭より数えて x ページ目から、y件づつ表示
90 $limit_base = $matches[1];
91 $limit_pitch = $matches[2];
92 $limit_page = $matches[1] + $matches[2]; // 読み飛ばす + 表示する
94 return '#calendar_viewer(): ' . $_err_calendar_viewer_param2 . '<br />' . "\n";
97 // $func_args[2]: Mode setting
98 if (isset($func_args[2]) && preg_match('/^(past|view|future)$/si', $func_args[2]))
99 $mode = $func_args[2];
101 // $func_args[3]: Change default delimiter
102 if (isset($func_args[3])) $date_sep = $func_args[3];
105 if (!isset($show_count[$pagename])) {
106 $show_count[$pagename] = 0;
108 $show_count[$pagename] += 1;
109 if ($show_count[$pagename] > 4) {
110 $s_page = htmlsc($pagename);
111 return "#calendar_viewer(): Exceeded the limit of show count: $s_page<br />";
114 $pagepattern = strip_bracket($pagename) . '/';
115 if ($pagename === '') {
116 // Support non-pagename yyyy-mm-dd pattern
119 $pagepattern_len = strlen($pagepattern);
122 $_date = get_date('Y' . $date_sep . 'm' . $date_sep . 'd');
123 foreach (get_existpages() as $page) {
124 if (strncmp($page, $pagepattern, $pagepattern_len) !== 0) continue;
125 $page_date = substr($page, $pagepattern_len);
126 // Verify the $page_date pattern (Default: yyyy-mm-dd).
127 // Past-mode hates the future, and
128 // Future-mode hates the past.
129 if ((plugin_calendar_viewer_isValidDate($page_date, $date_sep) === FALSE) ||
130 ($page_date > $_date && ($mode === 'past')) ||
131 ($page_date < $_date && ($mode === 'future'))) {
136 if ($mode == 'past') {
137 rsort($pagelist, SORT_STRING); // New => Old
139 sort($pagelist, SORT_STRING); // Old => New
142 $tmppage = $vars['page'];
145 // $limit_page の件数までインクルード
146 $tmp = max($limit_base, 0); // Skip minus
147 while ($tmp < $limit_page) {
148 if (! isset($pagelist[$tmp])) break;
150 $page = $pagelist[$tmp];
151 $get['page'] = $post['page'] = $vars['page'] = $page;
153 // 現状で閲覧許可がある場合だけ表示する
154 if (check_readable($page, FALSE, FALSE)) {
155 $body = convert_html(get_source($page));
157 $body = str_replace('$1', $page, $_msg_calendar_viewer_restrict);
160 $r_page = pagename_urlencode($page);
162 if (PLUGIN_CALENDAR_VIEWER_DATE_FORMAT !== FALSE) {
163 $time = strtotime(basename($page)); // $date_sep must be assumed '-' or ''!
164 if ($time === FALSE || $time === -1) {
165 $s_page = htmlsc($page); // Failed. Why?
167 $week = $weeklabels[date('w', $time)];
168 $s_page = htmlsc(str_replace(
171 date(PLUGIN_CALENDAR_VIEWER_DATE_FORMAT, $time)
175 $s_page = htmlsc($page);
179 $link = $script . '?' . $r_page;
181 $link = $script . '?cmd=edit&page=' . $r_page;
183 $link = '<a href="' . $link . '">' . $s_page . '</a>';
185 $head = '<h1>' . $link . '</h1>' . "\n";
186 $return_body .= $head . $body;
192 // ?plugin=calendar_viewer&file=ページ名&date=yyyy-mm
193 $enc_pagename = rawurlencode(substr($pagepattern, 0, $pagepattern_len - 1));
195 if ($page_YM != '') {
197 $date_sep_len = strlen($date_sep);
198 $this_year = substr($page_YM, 0, 4);
199 $this_month = substr($page_YM, 4 + $date_sep_len, 2);
202 $next_year = $this_year;
203 $next_month = $this_month + 1;
204 if ($next_month > 12) {
208 $next_YM = sprintf('%04d%s%02d', $next_year, $date_sep, $next_month);
211 $prev_year = $this_year;
212 $prev_month = $this_month - 1;
213 if ($prev_month < 1) {
217 $prev_YM = sprintf('%04d%s%02d', $prev_year, $date_sep, $prev_month);
218 if ($mode == 'past') {
219 $right_YM = $prev_YM;
220 $right_text = $prev_YM . '>>'; // >>
222 $left_text = '<<' . $next_YM; // <<
225 $left_text = '<<' . $prev_YM; // <<
226 $right_YM = $next_YM;
227 $right_text = $next_YM . '>>'; // >>
231 if ($limit_base <= 0) {
232 $left_YM = ''; // 表示しない (それより前の項目はない)
234 $left_YM = $limit_base - $limit_pitch . '*' . $limit_pitch;
235 $left_text = sprintf($_msg_calendar_viewer_left, $limit_pitch);
238 if ($limit_base + $limit_pitch >= count($pagelist)) {
239 $right_YM = ''; // 表示しない (それより後の項目はない)
241 $right_YM = $limit_base + $limit_pitch . '*' . $limit_pitch;
242 $right_text = sprintf($_msg_calendar_viewer_right, $limit_pitch);
247 if ($left_YM != '' || $right_YM != '') {
248 $s_date_sep = htmlsc($date_sep);
249 $left_link = $right_link = '';
250 $link = $script . '?plugin=calendar_viewer&mode=' . $mode .
251 '&file=' . $enc_pagename . '&date_sep=' . $s_date_sep . '&';
253 $left_link = '<a href="' . $link .
254 'date=' . $left_YM . '">' . $left_text . '</a>';
256 $right_link = '<a href="' . $link .
257 'date=' . $right_YM . '">' . $right_text . '</a>';
258 // past modeは<<新 旧>> 他は<<旧 新>>
260 '<div class="calendar_viewer">' .
261 '<span class="calendar_viewer_left">' . $left_link . '</span>' .
262 '<span class="calendar_viewer_right">' . $right_link . '</span>' .
266 $get['page'] = $post['page'] = $vars['page'] = $tmppage;
271 function plugin_calendar_viewer_action()
273 global $vars, $get, $post, $script;
277 $return_vars_array = array();
279 $page = strip_bracket($vars['page']);
281 if (isset($vars['file'])) $vars['page'] = $vars['file'];
283 $date_sep = $vars['date_sep'];
285 $page_YM = $vars['date'];
286 if ($page_YM == '') $page_YM = get_date('Y' . $date_sep . 'm');
287 $mode = $vars['mode'];
289 $args_array = array($vars['page'], $page_YM, $mode, $date_sep);
290 $return_vars_array['body'] = call_user_func_array('plugin_calendar_viewer_convert', $args_array);
292 //$return_vars_array['msg'] = 'calendar_viewer ' . $vars['page'] . '/' . $page_YM;
293 $return_vars_array['msg'] = 'calendar_viewer ' . htmlsc($vars['page']);
294 if ($vars['page'] != '') $return_vars_array['msg'] .= '/';
295 if (preg_match('/\*/', $page_YM)) {
296 // うーん、n件表示の時はなんてページ名にしたらいい?
298 $return_vars_array['msg'] .= htmlsc($page_YM);
301 $vars['page'] = $page;
302 return $return_vars_array;
305 function plugin_calendar_viewer_isValidDate($aStr, $aSepList = '-/ .')
308 if ($aSepList == '') {
309 // yyymmddとしてチェック(手抜き(^^;)
310 return checkdate(substr($aStr, 4, 2), substr($aStr, 6, 2), substr($aStr, 0, 4));
311 } else if (preg_match("#^([0-9]{2,4})[$aSepList]([0-9]{1,2})[$aSepList]([0-9]{1,2})$#", $aStr, $matches) ) {
312 return checkdate($matches[2], $matches[3], $matches[1]);