OSDN Git Service

BugTrack2/62: Do remove the whole design, 'Showing TrackBack-ping list by html'.
[pukiwiki/pukiwiki.git] / plugin / calendar_viewer.inc.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone
3 // $Id: calendar_viewer.inc.php,v 1.34 2005/06/05 12:46:59 henoheno Exp $
4 //
5 // Calendar viewer plugin - List pages that calendar/calnedar2 plugin created
6 // (Based on calendar and recent plugin)
7
8 // Page title's date format
9 //  * See PHP date() manual for detail
10 //  * '$\w' = weeklabel defined in $_msg_week
11 define('PLUGIN_CALENDAR_VIEWER_DATE_FORMAT',
12         //      FALSE         // 'pagename/2004-02-09' -- As is
13         //      'D, d M, Y'   // 'Mon, 09 Feb, 2004'
14         //      'F d, Y'      // 'February 09, 2004'
15         //      '[Y-m-d]'     // '[2004-02-09]'
16                 'Y/n/j ($\w)' // '2004/2/9 (Mon)'
17         );
18
19 // ----
20
21 define('PLUGIN_CALENDAR_VIEWER_USAGE',
22         '#calendar_viewer(pagename,this|yyyy-mm|n|x*y[,mode[,separater]])');
23 /*
24  ** pagename
25  * - A working root of calendar or calendar2 plugin
26  *   pagename/2004-12-30
27  *   pagename/2004-12-31
28  *   ...
29  *
30  ** (yyyy-mm|n|this)
31  * this    - Show 'this month'
32  * yyyy-mm - Show pages at year:yyyy and month:mm
33  * n       - Show first n pages
34  * x*n     - Show first n pages from x-th page (0 = first)
35  *
36  ** [mode]
37  * past   - Show today, and the past below. Recommended for ChangeLogs and diaries (default)
38  * future - Show today, and the future below. Recommended for event planning and scheduling
39  * view   - Show all, from the past to the future
40  *
41  ** [separater]
42  * - Specify separator of yyyy/mm/dd
43  * - Default: '-' (yyyy-mm-dd)
44  *
45  * TODO
46  *  Stop showing links 'next month' and 'previous month' with past/future mode for 'this month'
47  *    #calendar_viewer(pagename,this,past)
48  */
49
50 function plugin_calendar_viewer_convert()
51 {
52         global $vars, $get, $post, $script, $weeklabels;
53         global $_msg_calendar_viewer_right, $_msg_calendar_viewer_left;
54         global $_msg_calendar_viewer_restrict, $_err_calendar_viewer_param2;
55
56         static $viewed = array();
57
58         if (func_num_args() < 2)
59                 return PLUGIN_CALENDAR_VIEWER_USAGE . '<br />' . "\n";
60
61         $func_args = func_get_args();
62
63         // Default values
64         $pagename    = $func_args[0];   // ´ð½à¤È¤Ê¤ë¥Ú¡¼¥¸Ì¾
65         $page_YM     = '';      // °ìÍ÷ɽ¼¨¤¹¤ëǯ·î
66         $limit_base  = 0;       // ÀèƬ¤«¤é¿ô¤¨¤Æ²¿¥Ú¡¼¥¸Ìܤ«¤éɽ¼¨¤¹¤ë¤« (ÀèƬ)
67         $limit_pitch = 0;       // ²¿·ï¤Å¤Äɽ¼¨¤¹¤ë¤«
68         $limit_page  = 0;       // ¥µ¡¼¥Á¤¹¤ë¥Ú¡¼¥¸¿ô
69         $mode        = 'past';  // Æ°ºî¥â¡¼¥É
70         $date_sep    = '-';     // ÆüÉդΥ»¥Ñ¥ì¡¼¥¿ calendar2¤Ê¤é '-', calendar¤Ê¤é ''
71
72         // Check $func_args[1]
73         $matches = array();
74         if (preg_match('/[0-9]{4}' . $date_sep . '[0-9]{2}/', $func_args[1])) {
75                 // »ØÄêǯ·î¤Î°ìÍ÷ɽ¼¨
76                 $page_YM     = $func_args[1];
77                 $limit_page  = 31;
78         } else if (preg_match('/this/si', $func_args[1])) {
79                 // º£·î¤Î°ìÍ÷ɽ¼¨
80                 $page_YM     = get_date('Y' . $date_sep . 'm');
81                 $limit_page  = 31;
82         } else if (preg_match('/^[0-9]+$/', $func_args[1])) {
83                 // nÆüʬɽ¼¨
84                 $limit_pitch = $func_args[1];
85                 $limit_page  = $func_args[1];
86         } else if (preg_match('/(-?[0-9]+)\*([0-9]+)/', $func_args[1], $matches)) {
87                 // ÀèƬ¤è¤ê¿ô¤¨¤Æ x ¥Ú¡¼¥¸Ìܤ«¤é¡¢y·ï¤Å¤Äɽ¼¨
88                 $limit_base  = $matches[1];
89                 $limit_pitch = $matches[2];
90                 $limit_page  = $matches[1] + $matches[2]; // ÆɤßÈô¤Ð¤¹ + É½¼¨¤¹¤ë
91         } else {
92                 return '#calendar_viewer(): ' . $_err_calendar_viewer_param2 . '<br />' . "\n";
93         }
94
95         // $func_args[2]: Mode setting
96         if (isset($func_args[2]) && preg_match('/^(past|view|future)$/si', $func_args[2]))
97                 $mode = $func_args[2];
98
99         // $func_args[3]: Change default delimiter
100         if (isset($func_args[3])) $date_sep = $func_args[3];
101
102         // Avoid Loop etc.
103         if (isset($viewed[$pagename])) {
104                 $s_page = htmlspecialchars($pagename);
105                 return "#calendar_viewer(): You already view: $s_page<br />";
106         } else {
107                 $viewed[$pagename] = TRUE; // Valid
108         }
109
110         // °ìÍ÷ɽ¼¨¤¹¤ë¥Ú¡¼¥¸Ì¾¤È¥Õ¥¡¥¤¥ë̾¤Î¥Ñ¥¿¡¼¥ó¡¡¥Õ¥¡¥¤¥ë̾¤Ë¤Ïǯ·î¤ò´Þ¤à
111         if ($pagename == '') {
112                 // pagename̵¤·¤Îyyyy-mm-dd¤ËÂбþ¤¹¤ë¤¿¤á¤Î½èÍý
113                 $pagepattern     = '';
114                 $pagepattern_len = 0;
115                 $filepattern     = encode($page_YM);
116                 $filepattern_len = strlen($filepattern);
117         } else {
118                 $pagepattern     = strip_bracket($pagename) . '/';
119                 $pagepattern_len = strlen($pagepattern);
120                 $filepattern     = encode($pagepattern . $page_YM);
121                 $filepattern_len = strlen($filepattern);
122         }
123
124         // ¥Ú¡¼¥¸¥ê¥¹¥È¤Î¼èÆÀ
125         $pagelist = array();
126         if ($dir = @opendir(DATA_DIR)) {
127                 $_date = get_date('Y' . $date_sep . 'm' . $date_sep . 'd');
128                 $page_date  = '';
129                 while($file = readdir($dir)) {
130                         if ($file == '..' || $file == '.') continue;
131                         if (substr($file, 0, $filepattern_len) != $filepattern) continue;
132
133                         $page      = decode(trim(preg_replace('/\.txt$/', ' ', $file)));
134                         $page_date = substr($page, $pagepattern_len);
135
136                         // Verify the $page_date pattern (Default: yyyy-mm-dd).
137                         // Past-mode hates the future, and
138                         // Future-mode hates the past.
139                         if ((plugin_calendar_viewer_isValidDate($page_date, $date_sep) == FALSE) || 
140                                 ($page_date > $_date && ($mode == 'past')) ||
141                                 ($page_date < $_date && ($mode == 'future')))
142                                         continue;
143
144                         $pagelist[] = $page;
145                 }
146         }
147         closedir($dir);
148
149         if ($mode == 'past') {
150                 rsort($pagelist);       // New => Old
151         } else {
152                 sort($pagelist);        // Old => New
153         }
154
155         // Include start
156         $tmppage     = $vars['page'];
157         $return_body = '';
158
159         // $limit_page ¤Î·ï¿ô¤Þ¤Ç¥¤¥ó¥¯¥ë¡¼¥É
160         $tmp = max($limit_base, 0); // Skip minus
161         while ($tmp < $limit_page) {
162                 if (! isset($pagelist[$tmp])) break;
163
164                 $page = $pagelist[$tmp];
165                 $get['page'] = $post['page'] = $vars['page'] = $page;
166
167                 // ¸½¾õ¤Ç±ÜÍ÷µö²Ä¤¬¤¢¤ë¾ì¹ç¤À¤±É½¼¨¤¹¤ë
168                 if (check_readable($page, FALSE, FALSE)) {
169                         $body = convert_html(get_source($page));
170                 } else {
171                         $body = str_replace('$1', $page, $_msg_calendar_viewer_restrict);
172                 }
173
174                 $r_page = rawurlencode($page);
175
176                 if (PLUGIN_CALENDAR_VIEWER_DATE_FORMAT !== FALSE) {
177                         $time = strtotime(basename($page)); // $date_sep must be assumed '-' or ''!
178                         if ($time == -1) {
179                                 $s_page = htmlspecialchars($page); // Failed. Why?
180                         } else {
181                                 $week   = $weeklabels[date('w', $time)];
182                                 $s_page = htmlspecialchars(str_replace(
183                                                 array('$w' ),
184                                                 array($week),
185                                                 date(PLUGIN_CALENDAR_VIEWER_DATE_FORMAT, $time)
186                                         ));
187                         }
188                 } else {
189                         $s_page = htmlspecialchars($page);
190                 }
191
192                 if (PKWK_READONLY) {
193                         $link   = $script . '?' . $r_page;
194                 } else {
195                         $link   = $script . '?cmd=edit&amp;page=' . $r_page;
196                 }
197                 $link   = '<a href="' . $link . '">' . $s_page . '</a>';
198
199                 $head   = '<h1>' . $link . '</h1>' . "\n";
200                 $return_body .= $head . $body;
201
202                 ++$tmp;
203         }
204
205         // ¤³¤³¤Ç¡¢Á°¸å¤Î¥ê¥ó¥¯¤òɽ¼¨
206         // ?plugin=calendar_viewer&file=¥Ú¡¼¥¸Ì¾&date=yyyy-mm
207         $enc_pagename = rawurlencode(substr($pagepattern, 0, $pagepattern_len - 1));
208
209         if ($page_YM != '') {
210                 // Ç¯·îɽ¼¨»þ
211                 $date_sep_len = strlen($date_sep);
212                 $this_year    = substr($page_YM, 0, 4);
213                 $this_month   = substr($page_YM, 4 + $date_sep_len, 2);
214
215                 // ¼¡·î
216                 $next_year  = $this_year;
217                 $next_month = $this_month + 1;
218                 if ($next_month > 12) {
219                         ++$next_year;
220                         $next_month = 1;
221                 }
222                 $next_YM = sprintf('%04d%s%02d', $next_year, $date_sep, $next_month);
223
224                 // Á°·î
225                 $prev_year  = $this_year;
226                 $prev_month = $this_month - 1;
227                 if ($prev_month < 1) {
228                         --$prev_year;
229                         $prev_month = 12;
230                 }
231                 $prev_YM = sprintf('%04d%s%02d', $prev_year, $date_sep, $prev_month);
232                 if ($mode == 'past') {
233                         $right_YM   = $prev_YM;
234                         $right_text = $prev_YM . '&gt;&gt;'; // >>
235                         $left_YM    = $next_YM;
236                         $left_text  = '&lt;&lt;' . $next_YM; // <<
237                 } else {
238                         $left_YM    = $prev_YM;
239                         $left_text  = '&lt;&lt;' . $prev_YM; // <<
240                         $right_YM   = $next_YM;
241                         $right_text = $next_YM . '&gt;&gt;'; // >>
242                 }
243         } else {
244                 // n·ïɽ¼¨»þ
245                 if ($limit_base <= 0) {
246                         $left_YM = ''; // É½¼¨¤·¤Ê¤¤ (¤½¤ì¤è¤êÁ°¤Î¹àÌܤϤʤ¤)
247                 } else {
248                         $left_YM   = $limit_base - $limit_pitch . '*' . $limit_pitch;
249                         $left_text = sprintf($_msg_calendar_viewer_left, $limit_pitch);
250
251                 }
252                 if ($limit_base + $limit_pitch >= count($pagelist)) {
253                         $right_YM = ''; // É½¼¨¤·¤Ê¤¤ (¤½¤ì¤è¤ê¸å¤Î¹àÌܤϤʤ¤)
254                 } else {
255                         $right_YM   = $limit_base + $limit_pitch . '*' . $limit_pitch;
256                         $right_text = sprintf($_msg_calendar_viewer_right, $limit_pitch);
257                 }
258         }
259
260         // ¥Ê¥Ó¥²¡¼¥ÈÍѤΥê¥ó¥¯¤òËöÈø¤ËÄɲÃ
261         if ($left_YM != '' || $right_YM != '') {
262                 $s_date_sep = htmlspecialchars($date_sep);
263                 $left_link = $right_link = '';
264                 $link = $script . '?plugin=calendar_viewer&amp;mode=' . $mode .
265                         '&amp;file=' . $enc_pagename . '&amp;date_sep=' . $s_date_sep . '&amp;';
266                 if ($left_YM != '')
267                         $left_link = '<a href="' . $link .
268                                 'date=' . $left_YM . '">' . $left_text . '</a>';
269                 if ($right_YM != '')
270                         $right_link = '<a href="' . $link .
271                                 'date=' . $right_YM . '">' . $right_text . '</a>';
272                 // past mode¤Ï<<¿· µì>> Â¾¤Ï<<µì ¿·>>
273                 $return_body .=
274                         '<div class="calendar_viewer">' .
275                         '<span class="calendar_viewer_left">'  . $left_link  . '</span>' .
276                         '<span class="calendar_viewer_right">' . $right_link . '</span>' .
277                         '</div>';
278         }
279
280         $get['page'] = $post['page'] = $vars['page'] = $tmppage;
281
282         return $return_body;
283 }
284
285 function plugin_calendar_viewer_action()
286 {
287         global $vars, $get, $post, $script;
288
289         $date_sep = '-';
290
291         $return_vars_array = array();
292
293         $page = strip_bracket($vars['page']);
294         $vars['page'] = '*';
295         if (isset($vars['file'])) $vars['page'] = $vars['file'];
296
297         $date_sep = $vars['date_sep'];
298
299         $page_YM = $vars['date'];
300         if ($page_YM == '') $page_YM = get_date('Y' . $date_sep . 'm');
301         $mode = $vars['mode'];
302
303         $args_array = array($vars['page'], $page_YM, $mode, $date_sep);
304         $return_vars_array['body'] = call_user_func_array('plugin_calendar_viewer_convert', $args_array);
305
306         //$return_vars_array['msg'] = 'calendar_viewer ' . $vars['page'] . '/' . $page_YM;
307         $return_vars_array['msg'] = 'calendar_viewer ' . htmlspecialchars($vars['page']);
308         if ($vars['page'] != '') $return_vars_array['msg'] .= '/';
309         if (preg_match('/\*/', $page_YM)) {
310                 // ¤¦¡¼¤ó¡¢n·ïɽ¼¨¤Î»þ¤Ï¤Ê¤ó¤Æ¥Ú¡¼¥¸Ì¾¤Ë¤·¤¿¤é¤¤¤¤¡©
311         } else {
312                 $return_vars_array['msg'] .= htmlspecialchars($page_YM);
313         }
314
315         $vars['page'] = $page;
316         return $return_vars_array;
317 }
318
319 function plugin_calendar_viewer_isValidDate($aStr, $aSepList = '-/ .')
320 {
321         $matches = array();
322         if ($aSepList == '') {
323                 // yyymmdd¤È¤·¤Æ¥Á¥§¥Ã¥¯¡Ê¼êÈ´¤­(^^;¡Ë
324                 return checkdate(substr($aStr, 4, 2), substr($aStr, 6, 2), substr($aStr, 0, 4));
325         } else if (ereg("^([0-9]{2,4})[$aSepList]([0-9]{1,2})[$aSepList]([0-9]{1,2})$", $aStr, $matches) ) {
326                 return checkdate($matches[2], $matches[3], $matches[1]);
327         } else {
328                 return FALSE;
329         }
330 }
331 ?>