2 // PukiWiki - Yet another WikiWikiWeb clone
3 // $Id: calendar_viewer.inc.php,v 1.34 2005/06/05 12:46:59 henoheno Exp $
5 // Calendar viewer plugin - List pages that calendar/calnedar2 plugin created
6 // (Based on calendar and recent plugin)
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)'
21 define('PLUGIN_CALENDAR_VIEWER_USAGE',
22 '#calendar_viewer(pagename,this|yyyy-mm|n|x*y[,mode[,separater]])');
25 * - A working root of calendar or calendar2 plugin
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)
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
42 * - Specify separator of yyyy/mm/dd
43 * - Default: '-' (yyyy-mm-dd)
46 * Stop showing links 'next month' and 'previous month' with past/future mode for 'this month'
47 * #calendar_viewer(pagename,this,past)
50 function plugin_calendar_viewer_convert()
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;
56 static $viewed = array();
58 if (func_num_args() < 2)
59 return PLUGIN_CALENDAR_VIEWER_USAGE . '<br />' . "\n";
61 $func_args = func_get_args();
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¤Ê¤é ''
72 // Check $func_args[1]
74 if (preg_match('/[0-9]{4}' . $date_sep . '[0-9]{2}/', $func_args[1])) {
76 $page_YM = $func_args[1];
78 } else if (preg_match('/this/si', $func_args[1])) {
80 $page_YM = get_date('Y' . $date_sep . 'm');
82 } else if (preg_match('/^[0-9]+$/', $func_args[1])) {
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]; // ÆɤßÈô¤Ð¤¹ + ɽ¼¨¤¹¤ë
92 return '#calendar_viewer(): ' . $_err_calendar_viewer_param2 . '<br />' . "\n";
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];
99 // $func_args[3]: Change default delimiter
100 if (isset($func_args[3])) $date_sep = $func_args[3];
103 if (isset($viewed[$pagename])) {
104 $s_page = htmlspecialchars($pagename);
105 return "#calendar_viewer(): You already view: $s_page<br />";
107 $viewed[$pagename] = TRUE; // Valid
110 // °ìÍ÷ɽ¼¨¤¹¤ë¥Ú¡¼¥¸Ì¾¤È¥Õ¥¡¥¤¥ë̾¤Î¥Ñ¥¿¡¼¥ó¡¡¥Õ¥¡¥¤¥ë̾¤Ë¤Ïǯ·î¤ò´Þ¤à
111 if ($pagename == '') {
112 // pagename̵¤·¤Îyyyy-mm-dd¤ËÂбþ¤¹¤ë¤¿¤á¤Î½èÍý
114 $pagepattern_len = 0;
115 $filepattern = encode($page_YM);
116 $filepattern_len = strlen($filepattern);
118 $pagepattern = strip_bracket($pagename) . '/';
119 $pagepattern_len = strlen($pagepattern);
120 $filepattern = encode($pagepattern . $page_YM);
121 $filepattern_len = strlen($filepattern);
124 // ¥Ú¡¼¥¸¥ê¥¹¥È¤Î¼èÆÀ
126 if ($dir = @opendir(DATA_DIR)) {
127 $_date = get_date('Y' . $date_sep . 'm' . $date_sep . 'd');
129 while($file = readdir($dir)) {
130 if ($file == '..' || $file == '.') continue;
131 if (substr($file, 0, $filepattern_len) != $filepattern) continue;
133 $page = decode(trim(preg_replace('/\.txt$/', ' ', $file)));
134 $page_date = substr($page, $pagepattern_len);
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')))
149 if ($mode == 'past') {
150 rsort($pagelist); // New => Old
152 sort($pagelist); // Old => New
156 $tmppage = $vars['page'];
159 // $limit_page ¤Î·ï¿ô¤Þ¤Ç¥¤¥ó¥¯¥ë¡¼¥É
160 $tmp = max($limit_base, 0); // Skip minus
161 while ($tmp < $limit_page) {
162 if (! isset($pagelist[$tmp])) break;
164 $page = $pagelist[$tmp];
165 $get['page'] = $post['page'] = $vars['page'] = $page;
167 // ¸½¾õ¤Ç±ÜÍ÷µö²Ä¤¬¤¢¤ë¾ì¹ç¤À¤±É½¼¨¤¹¤ë
168 if (check_readable($page, FALSE, FALSE)) {
169 $body = convert_html(get_source($page));
171 $body = str_replace('$1', $page, $_msg_calendar_viewer_restrict);
174 $r_page = rawurlencode($page);
176 if (PLUGIN_CALENDAR_VIEWER_DATE_FORMAT !== FALSE) {
177 $time = strtotime(basename($page)); // $date_sep must be assumed '-' or ''!
179 $s_page = htmlspecialchars($page); // Failed. Why?
181 $week = $weeklabels[date('w', $time)];
182 $s_page = htmlspecialchars(str_replace(
185 date(PLUGIN_CALENDAR_VIEWER_DATE_FORMAT, $time)
189 $s_page = htmlspecialchars($page);
193 $link = $script . '?' . $r_page;
195 $link = $script . '?cmd=edit&page=' . $r_page;
197 $link = '<a href="' . $link . '">' . $s_page . '</a>';
199 $head = '<h1>' . $link . '</h1>' . "\n";
200 $return_body .= $head . $body;
205 // ¤³¤³¤Ç¡¢Á°¸å¤Î¥ê¥ó¥¯¤òɽ¼¨
206 // ?plugin=calendar_viewer&file=¥Ú¡¼¥¸Ì¾&date=yyyy-mm
207 $enc_pagename = rawurlencode(substr($pagepattern, 0, $pagepattern_len - 1));
209 if ($page_YM != '') {
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);
216 $next_year = $this_year;
217 $next_month = $this_month + 1;
218 if ($next_month > 12) {
222 $next_YM = sprintf('%04d%s%02d', $next_year, $date_sep, $next_month);
225 $prev_year = $this_year;
226 $prev_month = $this_month - 1;
227 if ($prev_month < 1) {
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 . '>>'; // >>
236 $left_text = '<<' . $next_YM; // <<
239 $left_text = '<<' . $prev_YM; // <<
240 $right_YM = $next_YM;
241 $right_text = $next_YM . '>>'; // >>
245 if ($limit_base <= 0) {
246 $left_YM = ''; // ɽ¼¨¤·¤Ê¤¤ (¤½¤ì¤è¤êÁ°¤Î¹àÌܤϤʤ¤)
248 $left_YM = $limit_base - $limit_pitch . '*' . $limit_pitch;
249 $left_text = sprintf($_msg_calendar_viewer_left, $limit_pitch);
252 if ($limit_base + $limit_pitch >= count($pagelist)) {
253 $right_YM = ''; // ɽ¼¨¤·¤Ê¤¤ (¤½¤ì¤è¤ê¸å¤Î¹àÌܤϤʤ¤)
255 $right_YM = $limit_base + $limit_pitch . '*' . $limit_pitch;
256 $right_text = sprintf($_msg_calendar_viewer_right, $limit_pitch);
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&mode=' . $mode .
265 '&file=' . $enc_pagename . '&date_sep=' . $s_date_sep . '&';
267 $left_link = '<a href="' . $link .
268 'date=' . $left_YM . '">' . $left_text . '</a>';
270 $right_link = '<a href="' . $link .
271 'date=' . $right_YM . '">' . $right_text . '</a>';
272 // past mode¤Ï<<¿· µì>> ¾¤Ï<<µì ¿·>>
274 '<div class="calendar_viewer">' .
275 '<span class="calendar_viewer_left">' . $left_link . '</span>' .
276 '<span class="calendar_viewer_right">' . $right_link . '</span>' .
280 $get['page'] = $post['page'] = $vars['page'] = $tmppage;
285 function plugin_calendar_viewer_action()
287 global $vars, $get, $post, $script;
291 $return_vars_array = array();
293 $page = strip_bracket($vars['page']);
295 if (isset($vars['file'])) $vars['page'] = $vars['file'];
297 $date_sep = $vars['date_sep'];
299 $page_YM = $vars['date'];
300 if ($page_YM == '') $page_YM = get_date('Y' . $date_sep . 'm');
301 $mode = $vars['mode'];
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);
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·ïɽ¼¨¤Î»þ¤Ï¤Ê¤ó¤Æ¥Ú¡¼¥¸Ì¾¤Ë¤·¤¿¤é¤¤¤¤¡©
312 $return_vars_array['msg'] .= htmlspecialchars($page_YM);
315 $vars['page'] = $page;
316 return $return_vars_array;
319 function plugin_calendar_viewer_isValidDate($aStr, $aSepList = '-/ .')
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]);