2 // PukiWiki - Yet another WikiWikiWeb clone
3 // $Id: calendar_viewer.inc.php,v 1.37 2011/01/25 15:01:01 henoheno Exp $
4 // Copyright (C) 2002-2005, 2007 PukiWiki Developers 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 $viewed = 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($viewed[$pagename])) {
106 $s_page = htmlsc($pagename);
107 return "#calendar_viewer(): You already view: $s_page<br />";
109 $viewed[$pagename] = TRUE; // Valid
112 // °ìÍ÷ɽ¼¨¤¹¤ë¥Ú¡¼¥¸Ì¾¤È¥Õ¥¡¥¤¥ë̾¤Î¥Ñ¥¿¡¼¥ó¡¡¥Õ¥¡¥¤¥ë̾¤Ë¤Ïǯ·î¤ò´Þ¤à
113 if ($pagename == '') {
114 // pagename̵¤·¤Îyyyy-mm-dd¤ËÂбþ¤¹¤ë¤¿¤á¤Î½èÍý
116 $pagepattern_len = 0;
117 $filepattern = encode($page_YM);
118 $filepattern_len = strlen($filepattern);
120 $pagepattern = strip_bracket($pagename) . '/';
121 $pagepattern_len = strlen($pagepattern);
122 $filepattern = encode($pagepattern . $page_YM);
123 $filepattern_len = strlen($filepattern);
126 // ¥Ú¡¼¥¸¥ê¥¹¥È¤Î¼èÆÀ
128 if ($dir = @opendir(DATA_DIR)) {
129 $_date = get_date('Y' . $date_sep . 'm' . $date_sep . 'd');
131 while($file = readdir($dir)) {
132 if ($file == '..' || $file == '.') continue;
133 if (substr($file, 0, $filepattern_len) != $filepattern) continue;
135 $page = decode(trim(preg_replace('/\.txt$/', ' ', $file)));
136 $page_date = substr($page, $pagepattern_len);
138 // Verify the $page_date pattern (Default: yyyy-mm-dd).
139 // Past-mode hates the future, and
140 // Future-mode hates the past.
141 if ((plugin_calendar_viewer_isValidDate($page_date, $date_sep) == FALSE) ||
142 ($page_date > $_date && ($mode == 'past')) ||
143 ($page_date < $_date && ($mode == 'future')))
151 if ($mode == 'past') {
152 rsort($pagelist); // New => Old
154 sort($pagelist); // Old => New
158 $tmppage = $vars['page'];
161 // $limit_page ¤Î·ï¿ô¤Þ¤Ç¥¤¥ó¥¯¥ë¡¼¥É
162 $tmp = max($limit_base, 0); // Skip minus
163 while ($tmp < $limit_page) {
164 if (! isset($pagelist[$tmp])) break;
166 $page = $pagelist[$tmp];
167 $get['page'] = $post['page'] = $vars['page'] = $page;
169 // ¸½¾õ¤Ç±ÜÍ÷µö²Ä¤¬¤¢¤ë¾ì¹ç¤À¤±É½¼¨¤¹¤ë
170 if (check_readable($page, FALSE, FALSE)) {
171 $body = convert_html(get_source($page));
173 $body = str_replace('$1', $page, $_msg_calendar_viewer_restrict);
176 $r_page = rawurlencode($page);
178 if (PLUGIN_CALENDAR_VIEWER_DATE_FORMAT !== FALSE) {
179 $time = strtotime(basename($page)); // $date_sep must be assumed '-' or ''!
181 $s_page = htmlsc($page); // Failed. Why?
183 $week = $weeklabels[date('w', $time)];
184 $s_page = htmlsc(str_replace(
187 date(PLUGIN_CALENDAR_VIEWER_DATE_FORMAT, $time)
191 $s_page = htmlsc($page);
195 $link = $script . '?' . $r_page;
197 $link = $script . '?cmd=edit&page=' . $r_page;
199 $link = '<a href="' . $link . '">' . $s_page . '</a>';
201 $head = '<h1>' . $link . '</h1>' . "\n";
202 $return_body .= $head . $body;
207 // ¤³¤³¤Ç¡¢Á°¸å¤Î¥ê¥ó¥¯¤òɽ¼¨
208 // ?plugin=calendar_viewer&file=¥Ú¡¼¥¸Ì¾&date=yyyy-mm
209 $enc_pagename = rawurlencode(substr($pagepattern, 0, $pagepattern_len - 1));
211 if ($page_YM != '') {
213 $date_sep_len = strlen($date_sep);
214 $this_year = substr($page_YM, 0, 4);
215 $this_month = substr($page_YM, 4 + $date_sep_len, 2);
218 $next_year = $this_year;
219 $next_month = $this_month + 1;
220 if ($next_month > 12) {
224 $next_YM = sprintf('%04d%s%02d', $next_year, $date_sep, $next_month);
227 $prev_year = $this_year;
228 $prev_month = $this_month - 1;
229 if ($prev_month < 1) {
233 $prev_YM = sprintf('%04d%s%02d', $prev_year, $date_sep, $prev_month);
234 if ($mode == 'past') {
235 $right_YM = $prev_YM;
236 $right_text = $prev_YM . '>>'; // >>
238 $left_text = '<<' . $next_YM; // <<
241 $left_text = '<<' . $prev_YM; // <<
242 $right_YM = $next_YM;
243 $right_text = $next_YM . '>>'; // >>
247 if ($limit_base <= 0) {
248 $left_YM = ''; // ɽ¼¨¤·¤Ê¤¤ (¤½¤ì¤è¤êÁ°¤Î¹àÌܤϤʤ¤)
250 $left_YM = $limit_base - $limit_pitch . '*' . $limit_pitch;
251 $left_text = sprintf($_msg_calendar_viewer_left, $limit_pitch);
254 if ($limit_base + $limit_pitch >= count($pagelist)) {
255 $right_YM = ''; // ɽ¼¨¤·¤Ê¤¤ (¤½¤ì¤è¤ê¸å¤Î¹àÌܤϤʤ¤)
257 $right_YM = $limit_base + $limit_pitch . '*' . $limit_pitch;
258 $right_text = sprintf($_msg_calendar_viewer_right, $limit_pitch);
262 // ¥Ê¥Ó¥²¡¼¥ÈÍѤΥê¥ó¥¯¤òËöÈø¤ËÄɲÃ
263 if ($left_YM != '' || $right_YM != '') {
264 $s_date_sep = htmlsc($date_sep);
265 $left_link = $right_link = '';
266 $link = $script . '?plugin=calendar_viewer&mode=' . $mode .
267 '&file=' . $enc_pagename . '&date_sep=' . $s_date_sep . '&';
269 $left_link = '<a href="' . $link .
270 'date=' . $left_YM . '">' . $left_text . '</a>';
272 $right_link = '<a href="' . $link .
273 'date=' . $right_YM . '">' . $right_text . '</a>';
274 // past mode¤Ï<<¿· µì>> ¾¤Ï<<µì ¿·>>
276 '<div class="calendar_viewer">' .
277 '<span class="calendar_viewer_left">' . $left_link . '</span>' .
278 '<span class="calendar_viewer_right">' . $right_link . '</span>' .
282 $get['page'] = $post['page'] = $vars['page'] = $tmppage;
287 function plugin_calendar_viewer_action()
289 global $vars, $get, $post, $script;
293 $return_vars_array = array();
295 $page = strip_bracket($vars['page']);
297 if (isset($vars['file'])) $vars['page'] = $vars['file'];
299 $date_sep = $vars['date_sep'];
301 $page_YM = $vars['date'];
302 if ($page_YM == '') $page_YM = get_date('Y' . $date_sep . 'm');
303 $mode = $vars['mode'];
305 $args_array = array($vars['page'], $page_YM, $mode, $date_sep);
306 $return_vars_array['body'] = call_user_func_array('plugin_calendar_viewer_convert', $args_array);
308 //$return_vars_array['msg'] = 'calendar_viewer ' . $vars['page'] . '/' . $page_YM;
309 $return_vars_array['msg'] = 'calendar_viewer ' . htmlsc($vars['page']);
310 if ($vars['page'] != '') $return_vars_array['msg'] .= '/';
311 if (preg_match('/\*/', $page_YM)) {
312 // ¤¦¡¼¤ó¡¢n·ïɽ¼¨¤Î»þ¤Ï¤Ê¤ó¤Æ¥Ú¡¼¥¸Ì¾¤Ë¤·¤¿¤é¤¤¤¤¡©
314 $return_vars_array['msg'] .= htmlsc($page_YM);
317 $vars['page'] = $page;
318 return $return_vars_array;
321 function plugin_calendar_viewer_isValidDate($aStr, $aSepList = '-/ .')
324 if ($aSepList == '') {
325 // yyymmdd¤È¤·¤Æ¥Á¥§¥Ã¥¯¡Ê¼êÈ´¤(^^;¡Ë
326 return checkdate(substr($aStr, 4, 2), substr($aStr, 6, 2), substr($aStr, 0, 4));
327 } else if (ereg("^([0-9]{2,4})[$aSepList]([0-9]{1,2})[$aSepList]([0-9]{1,2})$", $aStr, $matches) ) {
328 return checkdate($matches[2], $matches[3], $matches[1]);