OSDN Git Service

BugTrack/2470 Lengthen Footnote title max-length to 100 chars
[pukiwiki/pukiwiki.git] / plugin / calendar_viewer.inc.php
1 <?php
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
6 //
7 // Calendar viewer plugin - List pages that calendar/calnedar2 plugin created
8 // (Based on calendar and recent plugin)
9
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)'
19         );
20 /**
21  * Limit of show count per pagename
22  */
23 define('PLUGIN_CALENDAR_VIEWER_MAX_SHOW_COUNT', 4);
24
25 // ----
26
27 define('PLUGIN_CALENDAR_VIEWER_USAGE',
28         '#calendar_viewer(pagename,this|yyyy-mm|n|x*y[,mode[,separater]])');
29 /*
30  ** pagename
31  * - A working root of calendar or calendar2 plugin
32  *   pagename/2004-12-30
33  *   pagename/2004-12-31
34  *   ...
35  *
36  ** (yyyy-mm|n|this)
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)
41  *
42  ** [mode]
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
46  *
47  ** [separater]
48  * - Specify separator of yyyy/mm/dd
49  * - Default: '-' (yyyy-mm-dd)
50  *
51  * TODO
52  *  Stop showing links 'next month' and 'previous month' with past/future mode for 'this month'
53  *    #calendar_viewer(pagename,this,past)
54  */
55
56 function plugin_calendar_viewer_convert()
57 {
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;
61
62         static $show_count = array();
63
64         if (func_num_args() < 2)
65                 return PLUGIN_CALENDAR_VIEWER_USAGE . '<br />' . "\n";
66
67         $func_args = func_get_args();
68
69         // Default values
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なら ''
77
78         // Check $func_args[1]
79         $matches = array();
80         if (preg_match('/[0-9]{4}' . $date_sep . '[0-9]{2}/', $func_args[1])) {
81                 // 指定年月の一覧表示
82                 $page_YM     = $func_args[1];
83                 $limit_page  = 31;
84         } else if (preg_match('/this/si', $func_args[1])) {
85                 // 今月の一覧表示
86                 $page_YM     = get_date('Y' . $date_sep . 'm');
87                 $limit_page  = 31;
88         } else if (preg_match('/^[0-9]+$/', $func_args[1])) {
89                 // n日分表示
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]; // 読み飛ばす + 表示する
97         } else {
98                 return '#calendar_viewer(): ' . $_err_calendar_viewer_param2 . '<br />' . "\n";
99         }
100
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];
104
105         // $func_args[3]: Change default delimiter
106         if (isset($func_args[3])) $date_sep = $func_args[3];
107
108         // Avoid Loop etc.
109         if (!isset($show_count[$pagename])) {
110                 $show_count[$pagename] = 0;
111         }
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 />";
116         }
117         // page name pattern
118         $pagepattern = strip_bracket($pagename) . '/';
119         if ($pagename === '') {
120                 // Support non-pagename yyyy-mm-dd pattern
121                 $pagepattern = '';
122         }
123         $pagepattern_len = strlen($pagepattern);
124         // Get pagelist
125         $pagelist = array();
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'))) {
136                                 continue;
137                 }
138                 $pagelist[] = $page;
139         }
140         if ($mode == 'past') {
141                 rsort($pagelist, SORT_STRING);  // New => Old
142         } else {
143                 sort($pagelist, SORT_STRING);   // Old => New
144         }
145         // Include start
146         $tmppage     = $vars['page'];
147         $return_body = '';
148
149         // $limit_page の件数までインクルード
150         $tmp = max($limit_base, 0); // Skip minus
151         while ($tmp < $limit_page) {
152                 if (! isset($pagelist[$tmp])) break;
153
154                 $page = $pagelist[$tmp];
155                 $get['page'] = $post['page'] = $vars['page'] = $page;
156
157                 // 現状で閲覧許可がある場合だけ表示する
158                 if (check_readable($page, FALSE, FALSE)) {
159                         $body = convert_html(get_source($page));
160                 } else {
161                         $body = str_replace('$1', $page, $_msg_calendar_viewer_restrict);
162                 }
163
164                 $r_page = pagename_urlencode($page);
165
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?
170                         } else {
171                                 $week   = $weeklabels[date('w', $time)];
172                                 $s_page = htmlsc(str_replace(
173                                                 array('$w' ),
174                                                 array($week),
175                                                 date(PLUGIN_CALENDAR_VIEWER_DATE_FORMAT, $time)
176                                         ));
177                         }
178                 } else {
179                         $s_page = htmlsc($page);
180                 }
181
182                 if (PKWK_READONLY) {
183                         $link   = $script . '?' . $r_page;
184                 } else {
185                         $link   = $script . '?cmd=edit&amp;page=' . $r_page;
186                 }
187                 $link   = '<a href="' . $link . '">' . $s_page . '</a>';
188
189                 $head   = '<h1>' . $link . '</h1>' . "\n";
190                 $return_body .= $head . $body;
191
192                 ++$tmp;
193         }
194
195         // ここで、前後のリンクを表示
196         // ?plugin=calendar_viewer&file=ページ名&date=yyyy-mm
197         $enc_pagename = rawurlencode(substr($pagepattern, 0, $pagepattern_len - 1));
198
199         if ($page_YM != '') {
200                 // 年月表示時
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);
204
205                 // 次月
206                 $next_year  = $this_year;
207                 $next_month = $this_month + 1;
208                 if ($next_month > 12) {
209                         ++$next_year;
210                         $next_month = 1;
211                 }
212                 $next_YM = sprintf('%04d%s%02d', $next_year, $date_sep, $next_month);
213
214                 // 前月
215                 $prev_year  = $this_year;
216                 $prev_month = $this_month - 1;
217                 if ($prev_month < 1) {
218                         --$prev_year;
219                         $prev_month = 12;
220                 }
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 . '&gt;&gt;'; // >>
225                         $left_YM    = $next_YM;
226                         $left_text  = '&lt;&lt;' . $next_YM; // <<
227                 } else {
228                         $left_YM    = $prev_YM;
229                         $left_text  = '&lt;&lt;' . $prev_YM; // <<
230                         $right_YM   = $next_YM;
231                         $right_text = $next_YM . '&gt;&gt;'; // >>
232                 }
233         } else {
234                 // n件表示時
235                 if ($limit_base <= 0) {
236                         $left_YM = ''; // 表示しない (それより前の項目はない)
237                 } else {
238                         $left_YM   = $limit_base - $limit_pitch . '*' . $limit_pitch;
239                         $left_text = sprintf($_msg_calendar_viewer_left, $limit_pitch);
240
241                 }
242                 if ($limit_base + $limit_pitch >= count($pagelist)) {
243                         $right_YM = ''; // 表示しない (それより後の項目はない)
244                 } else {
245                         $right_YM   = $limit_base + $limit_pitch . '*' . $limit_pitch;
246                         $right_text = sprintf($_msg_calendar_viewer_right, $limit_pitch);
247                 }
248         }
249
250         // ナビゲート用のリンクを末尾に追加
251         if ($left_YM != '' || $right_YM != '') {
252                 $s_date_sep = htmlsc($date_sep);
253                 $left_link = $right_link = '';
254                 $link = $script . '?plugin=calendar_viewer&amp;mode=' . $mode .
255                         '&amp;file=' . $enc_pagename . '&amp;date_sep=' . $s_date_sep . '&amp;';
256                 if ($left_YM != '')
257                         $left_link = '<a href="' . $link .
258                                 'date=' . $left_YM . '">' . $left_text . '</a>';
259                 if ($right_YM != '')
260                         $right_link = '<a href="' . $link .
261                                 'date=' . $right_YM . '">' . $right_text . '</a>';
262                 // past modeは<<新 旧>> 他は<<旧 新>>
263                 $return_body .=
264                         '<div class="calendar_viewer">' .
265                         '<span class="calendar_viewer_left">'  . $left_link  . '</span>' .
266                         '<span class="calendar_viewer_right">' . $right_link . '</span>' .
267                         '</div>';
268         }
269
270         $get['page'] = $post['page'] = $vars['page'] = $tmppage;
271
272         return $return_body;
273 }
274
275 function plugin_calendar_viewer_action()
276 {
277         global $vars, $get, $post, $script;
278
279         $date_sep = '-';
280
281         $return_vars_array = array();
282
283         $page = strip_bracket($vars['page']);
284         $vars['page'] = '*';
285         if (isset($vars['file'])) $vars['page'] = $vars['file'];
286
287         $date_sep = $vars['date_sep'];
288
289         $page_YM = $vars['date'];
290         if ($page_YM == '') $page_YM = get_date('Y' . $date_sep . 'm');
291         $mode = $vars['mode'];
292
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);
295
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件表示の時はなんてページ名にしたらいい?
301         } else {
302                 $return_vars_array['msg'] .= htmlsc($page_YM);
303         }
304
305         $vars['page'] = $page;
306         return $return_vars_array;
307 }
308
309 function plugin_calendar_viewer_isValidDate($aStr, $aSepList = '-/ .')
310 {
311         $matches = array();
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]);
317         } else {
318                 return FALSE;
319         }
320 }