OSDN Git Service

BugTrack/2446 Fasten calendar_viewer plugin with get_existpage cache
[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 // ----
22
23 define('PLUGIN_CALENDAR_VIEWER_USAGE',
24         '#calendar_viewer(pagename,this|yyyy-mm|n|x*y[,mode[,separater]])');
25 /*
26  ** pagename
27  * - A working root of calendar or calendar2 plugin
28  *   pagename/2004-12-30
29  *   pagename/2004-12-31
30  *   ...
31  *
32  ** (yyyy-mm|n|this)
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)
37  *
38  ** [mode]
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
42  *
43  ** [separater]
44  * - Specify separator of yyyy/mm/dd
45  * - Default: '-' (yyyy-mm-dd)
46  *
47  * TODO
48  *  Stop showing links 'next month' and 'previous month' with past/future mode for 'this month'
49  *    #calendar_viewer(pagename,this,past)
50  */
51
52 function plugin_calendar_viewer_convert()
53 {
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;
57
58         static $show_count = array();
59
60         if (func_num_args() < 2)
61                 return PLUGIN_CALENDAR_VIEWER_USAGE . '<br />' . "\n";
62
63         $func_args = func_get_args();
64
65         // Default values
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なら ''
73
74         // Check $func_args[1]
75         $matches = array();
76         if (preg_match('/[0-9]{4}' . $date_sep . '[0-9]{2}/', $func_args[1])) {
77                 // 指定年月の一覧表示
78                 $page_YM     = $func_args[1];
79                 $limit_page  = 31;
80         } else if (preg_match('/this/si', $func_args[1])) {
81                 // 今月の一覧表示
82                 $page_YM     = get_date('Y' . $date_sep . 'm');
83                 $limit_page  = 31;
84         } else if (preg_match('/^[0-9]+$/', $func_args[1])) {
85                 // n日分表示
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]; // 読み飛ばす + 表示する
93         } else {
94                 return '#calendar_viewer(): ' . $_err_calendar_viewer_param2 . '<br />' . "\n";
95         }
96
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];
100
101         // $func_args[3]: Change default delimiter
102         if (isset($func_args[3])) $date_sep = $func_args[3];
103
104         // Avoid Loop etc.
105         if (!isset($show_count[$pagename])) {
106                 $show_count[$pagename] = 0;
107         }
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 />";
112         }
113         // page name pattern
114         $pagepattern = strip_bracket($pagename) . '/';
115         if ($pagename === '') {
116                 // Support non-pagename yyyy-mm-dd pattern
117                 $pagepattern = '';
118         }
119         $pagepattern_len = strlen($pagepattern);
120         // Get pagelist
121         $pagelist = array();
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'))) {
132                                 continue;
133                 }
134                 $pagelist[] = $page;
135         }
136         if ($mode == 'past') {
137                 rsort($pagelist, SORT_STRING);  // New => Old
138         } else {
139                 sort($pagelist, SORT_STRING);   // Old => New
140         }
141         // Include start
142         $tmppage     = $vars['page'];
143         $return_body = '';
144
145         // $limit_page の件数までインクルード
146         $tmp = max($limit_base, 0); // Skip minus
147         while ($tmp < $limit_page) {
148                 if (! isset($pagelist[$tmp])) break;
149
150                 $page = $pagelist[$tmp];
151                 $get['page'] = $post['page'] = $vars['page'] = $page;
152
153                 // 現状で閲覧許可がある場合だけ表示する
154                 if (check_readable($page, FALSE, FALSE)) {
155                         $body = convert_html(get_source($page));
156                 } else {
157                         $body = str_replace('$1', $page, $_msg_calendar_viewer_restrict);
158                 }
159
160                 $r_page = pagename_urlencode($page);
161
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?
166                         } else {
167                                 $week   = $weeklabels[date('w', $time)];
168                                 $s_page = htmlsc(str_replace(
169                                                 array('$w' ),
170                                                 array($week),
171                                                 date(PLUGIN_CALENDAR_VIEWER_DATE_FORMAT, $time)
172                                         ));
173                         }
174                 } else {
175                         $s_page = htmlsc($page);
176                 }
177
178                 if (PKWK_READONLY) {
179                         $link   = $script . '?' . $r_page;
180                 } else {
181                         $link   = $script . '?cmd=edit&amp;page=' . $r_page;
182                 }
183                 $link   = '<a href="' . $link . '">' . $s_page . '</a>';
184
185                 $head   = '<h1>' . $link . '</h1>' . "\n";
186                 $return_body .= $head . $body;
187
188                 ++$tmp;
189         }
190
191         // ここで、前後のリンクを表示
192         // ?plugin=calendar_viewer&file=ページ名&date=yyyy-mm
193         $enc_pagename = rawurlencode(substr($pagepattern, 0, $pagepattern_len - 1));
194
195         if ($page_YM != '') {
196                 // 年月表示時
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);
200
201                 // 次月
202                 $next_year  = $this_year;
203                 $next_month = $this_month + 1;
204                 if ($next_month > 12) {
205                         ++$next_year;
206                         $next_month = 1;
207                 }
208                 $next_YM = sprintf('%04d%s%02d', $next_year, $date_sep, $next_month);
209
210                 // 前月
211                 $prev_year  = $this_year;
212                 $prev_month = $this_month - 1;
213                 if ($prev_month < 1) {
214                         --$prev_year;
215                         $prev_month = 12;
216                 }
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 . '&gt;&gt;'; // >>
221                         $left_YM    = $next_YM;
222                         $left_text  = '&lt;&lt;' . $next_YM; // <<
223                 } else {
224                         $left_YM    = $prev_YM;
225                         $left_text  = '&lt;&lt;' . $prev_YM; // <<
226                         $right_YM   = $next_YM;
227                         $right_text = $next_YM . '&gt;&gt;'; // >>
228                 }
229         } else {
230                 // n件表示時
231                 if ($limit_base <= 0) {
232                         $left_YM = ''; // 表示しない (それより前の項目はない)
233                 } else {
234                         $left_YM   = $limit_base - $limit_pitch . '*' . $limit_pitch;
235                         $left_text = sprintf($_msg_calendar_viewer_left, $limit_pitch);
236
237                 }
238                 if ($limit_base + $limit_pitch >= count($pagelist)) {
239                         $right_YM = ''; // 表示しない (それより後の項目はない)
240                 } else {
241                         $right_YM   = $limit_base + $limit_pitch . '*' . $limit_pitch;
242                         $right_text = sprintf($_msg_calendar_viewer_right, $limit_pitch);
243                 }
244         }
245
246         // ナビゲート用のリンクを末尾に追加
247         if ($left_YM != '' || $right_YM != '') {
248                 $s_date_sep = htmlsc($date_sep);
249                 $left_link = $right_link = '';
250                 $link = $script . '?plugin=calendar_viewer&amp;mode=' . $mode .
251                         '&amp;file=' . $enc_pagename . '&amp;date_sep=' . $s_date_sep . '&amp;';
252                 if ($left_YM != '')
253                         $left_link = '<a href="' . $link .
254                                 'date=' . $left_YM . '">' . $left_text . '</a>';
255                 if ($right_YM != '')
256                         $right_link = '<a href="' . $link .
257                                 'date=' . $right_YM . '">' . $right_text . '</a>';
258                 // past modeは<<新 旧>> 他は<<旧 新>>
259                 $return_body .=
260                         '<div class="calendar_viewer">' .
261                         '<span class="calendar_viewer_left">'  . $left_link  . '</span>' .
262                         '<span class="calendar_viewer_right">' . $right_link . '</span>' .
263                         '</div>';
264         }
265
266         $get['page'] = $post['page'] = $vars['page'] = $tmppage;
267
268         return $return_body;
269 }
270
271 function plugin_calendar_viewer_action()
272 {
273         global $vars, $get, $post, $script;
274
275         $date_sep = '-';
276
277         $return_vars_array = array();
278
279         $page = strip_bracket($vars['page']);
280         $vars['page'] = '*';
281         if (isset($vars['file'])) $vars['page'] = $vars['file'];
282
283         $date_sep = $vars['date_sep'];
284
285         $page_YM = $vars['date'];
286         if ($page_YM == '') $page_YM = get_date('Y' . $date_sep . 'm');
287         $mode = $vars['mode'];
288
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);
291
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件表示の時はなんてページ名にしたらいい?
297         } else {
298                 $return_vars_array['msg'] .= htmlsc($page_YM);
299         }
300
301         $vars['page'] = $page;
302         return $return_vars_array;
303 }
304
305 function plugin_calendar_viewer_isValidDate($aStr, $aSepList = '-/ .')
306 {
307         $matches = array();
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]);
313         } else {
314                 return FALSE;
315         }
316 }