2 // PukiWiki - Yet another WikiWikiWeb clone
3 // $Id: calendar_viewer.inc.php,v 1.31 2005/01/26 13:09:58 henoheno Exp $
5 // Calendar viewer plugin - List pages that calendar/calnedar2 plugin created
6 // (Based on calendar and recent plugin)
8 define('PLUGIN_CALENDAR_VIEWER_USAGE',
9 '#calendar_viewer(pagename,this|yyyy-mm|n|x*y[,mode[,separater]])');
12 * - A working root of calendar or calendar2 plugin
18 * this - Show 'this month'
19 * yyyy-mm - Show pages at year:yyyy and month:mm
20 * n - Show first n pages
21 * x*n - Show first n pages from x-th page (0 = first)
24 * past - Show today, and the past below. Recommended for ChangeLogs and diaries (default)
25 * future - Show today, and the future below. Recommended for event planning and scheduling
26 * view - Show all, from the past to the future
29 * - Specify separator of yyyy/mm/dd
30 * - Default: '-' (yyyy-mm-dd)
33 * Stop showing links 'next month' and 'previous month' with past/future mode for 'this month'
34 * #calendar_viewer(pagename,this,past)
37 function plugin_calendar_viewer_convert()
39 global $vars, $get, $post, $script;
40 global $_msg_calendar_viewer_right, $_msg_calendar_viewer_left;
41 global $_msg_calendar_viewer_restrict, $_err_calendar_viewer_param2;
43 static $viewed = array();
45 if (func_num_args() < 2)
46 return PLUGIN_CALENDAR_VIEWER_USAGE . '<br />' . "\n";
48 $func_args = func_get_args();
51 $pagename = $func_args[0]; // ´ð½à¤È¤Ê¤ë¥Ú¡¼¥¸Ì¾
52 $page_YM = ''; // °ìÍ÷ɽ¼¨¤¹¤ëǯ·î
53 $limit_base = 0; // ÀèƬ¤«¤é¿ô¤¨¤Æ²¿¥Ú¡¼¥¸Ìܤ«¤éɽ¼¨¤¹¤ë¤« (ÀèƬ)
54 $limit_pitch = 0; // ²¿·ï¤Å¤Äɽ¼¨¤¹¤ë¤«
55 $limit_page = 0; // ¥µ¡¼¥Á¤¹¤ë¥Ú¡¼¥¸¿ô
56 $mode = 'past'; // Æ°ºî¥â¡¼¥É
57 $date_sep = '-'; // ÆüÉդΥ»¥Ñ¥ì¡¼¥¿ calendar2¤Ê¤é '-', calendar¤Ê¤é ''
59 // Check $func_args[1]
61 if (preg_match('/[0-9]{4}' . $date_sep . '[0-9]{2}/', $func_args[1])) {
63 $page_YM = $func_args[1];
65 } else if (preg_match('/this/si', $func_args[1])) {
67 $page_YM = get_date('Y' . $date_sep . 'm');
69 } else if (preg_match('/^[0-9]+$/', $func_args[1])) {
71 $limit_pitch = $func_args[1];
72 $limit_page = $func_args[1];
73 } else if (preg_match('/(-?[0-9]+)\*([0-9]+)/', $func_args[1], $matches)) {
74 // ÀèƬ¤è¤ê¿ô¤¨¤Æ x ¥Ú¡¼¥¸Ìܤ«¤é¡¢y·ï¤Å¤Äɽ¼¨
75 $limit_base = $matches[1];
76 $limit_pitch = $matches[2];
77 $limit_page = $matches[1] + $matches[2]; // ÆɤßÈô¤Ð¤¹ + ɽ¼¨¤¹¤ë
79 return '#calendar_viewer(): ' . $_err_calendar_viewer_param2 . '<br />' . "\n";
82 // $func_args[2]: Mode setting
83 if (isset($func_args[2]) && preg_match('/^(past|view|future)$/si', $func_args[2]))
84 $mode = $func_args[2];
86 // $func_args[3]: Change default delimiter
87 if (isset($func_args[3])) $date_sep = $func_args[3];
90 if (isset($viewed[$pagename])) {
91 $s_page = htmlspecialchars($pagename);
92 return "#calendar_viewer(): You already view: $s_page<br />";
94 $viewed[$pagename] = TRUE; // Valid
97 // °ìÍ÷ɽ¼¨¤¹¤ë¥Ú¡¼¥¸Ì¾¤È¥Õ¥¡¥¤¥ë̾¤Î¥Ñ¥¿¡¼¥ó¡¡¥Õ¥¡¥¤¥ë̾¤Ë¤Ïǯ·î¤ò´Þ¤à
98 if ($pagename == '') {
99 // pagename̵¤·¤Îyyyy-mm-dd¤ËÂбþ¤¹¤ë¤¿¤á¤Î½èÍý
101 $pagepattern_len = 0;
102 $filepattern = encode($page_YM);
103 $filepattern_len = strlen($filepattern);
105 $pagepattern = strip_bracket($pagename) . '/';
106 $pagepattern_len = strlen($pagepattern);
107 $filepattern = encode($pagepattern . $page_YM);
108 $filepattern_len = strlen($filepattern);
111 // ¥Ú¡¼¥¸¥ê¥¹¥È¤Î¼èÆÀ
113 if ($dir = @opendir(DATA_DIR)) {
114 $_date = get_date('Y' . $date_sep . 'm' . $date_sep . 'd');
116 while($file = readdir($dir)) {
117 if ($file == '..' || $file == '.') continue;
118 if (substr($file, 0, $filepattern_len) != $filepattern) continue;
120 $page = decode(trim(preg_replace('/\.txt$/', ' ', $file)));
121 $page_date = substr($page, $pagepattern_len);
123 // Verify the $page_date pattern (Default: yyyy-mm-dd).
124 // Past-mode hates the future, and
125 // Future-mode hates the past.
126 if ((plugin_calendar_viewer_isValidDate($page_date, $date_sep) == FALSE) ||
127 ($page_date > $_date && ($mode == 'past')) ||
128 ($page_date < $_date && ($mode == 'future')))
136 if ($mode == 'past') {
137 rsort($pagelist); // New => Old
139 sort($pagelist); // Old => New
143 $tmppage = $vars['page'];
146 // $limit_page ¤Î·ï¿ô¤Þ¤Ç¥¤¥ó¥¯¥ë¡¼¥É
147 $tmp = max($limit_base, 0); // Skip minus
148 while ($tmp < $limit_page) {
149 if (! isset($pagelist[$tmp])) break;
151 $page = $pagelist[$tmp];
152 $get['page'] = $post['page'] = $vars['page'] = $page;
154 // ¸½¾õ¤Ç±ÜÍ÷µö²Ä¤¬¤¢¤ë¾ì¹ç¤À¤±É½¼¨¤¹¤ë
155 if (check_readable($page, FALSE, FALSE)) {
156 $body = convert_html(get_source($page));
158 $body = str_replace('$1', $page, $_msg_calendar_viewer_restrict);
161 $r_page = rawurlencode($page);
162 $s_page = htmlspecialchars($page);
165 $link = $script . '?' . $r_page;
167 $link = $script . '?cmd=edit&page=' . $r_page;
169 $link = '<a href="' . $link . '">' . $s_page . '</a>';
171 $head = '<h1>' . $link . '</h1>' . "\n";
172 $return_body .= $head . $body;
177 // ¤³¤³¤Ç¡¢Á°¸å¤Î¥ê¥ó¥¯¤òɽ¼¨
178 // ?plugin=calendar_viewer&file=¥Ú¡¼¥¸Ì¾&date=yyyy-mm
179 $enc_pagename = rawurlencode(substr($pagepattern, 0, $pagepattern_len - 1));
181 if ($page_YM != '') {
183 $date_sep_len = strlen($date_sep);
184 $this_year = substr($page_YM, 0, 4);
185 $this_month = substr($page_YM, 4 + $date_sep_len, 2);
188 $next_year = $this_year;
189 $next_month = $this_month + 1;
190 if ($next_month > 12) {
194 $next_YM = sprintf('%04d%s%02d', $next_year, $date_sep, $next_month);
197 $prev_year = $this_year;
198 $prev_month = $this_month - 1;
199 if ($prev_month < 1) {
203 $prev_YM = sprintf('%04d%s%02d', $prev_year, $date_sep, $prev_month);
204 if ($mode == 'past') {
205 $right_YM = $prev_YM;
206 $right_text = $prev_YM . '>>'; // >>
208 $left_text = '<<' . $next_YM; // <<
211 $left_text = '<<' . $prev_YM; // <<
212 $right_YM = $next_YM;
213 $right_text = $next_YM . '>>'; // >>
217 if ($limit_base <= 0) {
218 $left_YM = ''; // ɽ¼¨¤·¤Ê¤¤ (¤½¤ì¤è¤êÁ°¤Î¹àÌܤϤʤ¤)
220 $left_YM = $limit_base - $limit_pitch . '*' . $limit_pitch;
221 $left_text = sprintf($_msg_calendar_viewer_left, $limit_pitch);
224 if ($limit_base + $limit_pitch >= count($pagelist)) {
225 $right_YM = ''; // ɽ¼¨¤·¤Ê¤¤ (¤½¤ì¤è¤ê¸å¤Î¹àÌܤϤʤ¤)
227 $right_YM = $limit_base + $limit_pitch . '*' . $limit_pitch;
228 $right_text = sprintf($_msg_calendar_viewer_right, $limit_pitch);
232 // ¥Ê¥Ó¥²¡¼¥ÈÍѤΥê¥ó¥¯¤òËöÈø¤ËÄɲÃ
233 if ($left_YM != '' || $right_YM != '') {
234 $s_date_sep = htmlspecialchars($date_sep);
235 $left_link = $right_link = '';
236 $link = $script . '?plugin=calendar_viewer&mode=' . $mode .
237 '&file=' . $enc_pagename . '&date_sep=' . $s_date_sep . '&';
239 $left_link = '<a href="' . $link .
240 'date=' . $left_YM . '">' . $left_text . '</a>';
242 $right_link = '<a href="' . $link .
243 'date=' . $right_YM . '">' . $right_text . '</a>';
244 // past mode¤Ï<<¿· µì>> ¾¤Ï<<µì ¿·>>
246 '<div class="calendar_viewer">' .
247 '<span class="calendar_viewer_left">' . $left_link . '</span>' .
248 '<span class="calendar_viewer_right">' . $right_link . '</span>' .
252 $get['page'] = $post['page'] = $vars['page'] = $tmppage;
257 function plugin_calendar_viewer_action()
259 global $vars, $get, $post, $script;
263 $return_vars_array = array();
265 $page = strip_bracket($vars['page']);
267 if (isset($vars['file'])) $vars['page'] = $vars['file'];
269 $date_sep = $vars['date_sep'];
271 $page_YM = $vars['date'];
272 if ($page_YM == '') $page_YM = get_date('Y' . $date_sep . 'm');
273 $mode = $vars['mode'];
275 $args_array = array($vars['page'], $page_YM, $mode, $date_sep);
276 $return_vars_array['body'] = call_user_func_array('plugin_calendar_viewer_convert', $args_array);
278 //$return_vars_array['msg'] = 'calendar_viewer ' . $vars['page'] . '/' . $page_YM;
279 $return_vars_array['msg'] = 'calendar_viewer ' . htmlspecialchars($vars['page']);
280 if ($vars['page'] != '') $return_vars_array['msg'] .= '/';
281 if (preg_match('/\*/', $page_YM)) {
282 // ¤¦¡¼¤ó¡¢n·ïɽ¼¨¤Î»þ¤Ï¤Ê¤ó¤Æ¥Ú¡¼¥¸Ì¾¤Ë¤·¤¿¤é¤¤¤¤¡©
284 $return_vars_array['msg'] .= htmlspecialchars($page_YM);
287 $vars['page'] = $page;
288 return $return_vars_array;
291 function plugin_calendar_viewer_isValidDate($aStr, $aSepList = '-/ .')
294 if ($aSepList == '') {
295 // yyymmdd¤È¤·¤Æ¥Á¥§¥Ã¥¯¡Ê¼êÈ´¤(^^;¡Ë
296 return checkdate(substr($aStr, 4, 2), substr($aStr, 6, 2), substr($aStr, 0, 4));
297 } else if (ereg("^([0-9]{2,4})[$aSepList]([0-9]{1,2})[$aSepList]([0-9]{1,2})$", $aStr, $matches) ) {
298 return checkdate($matches[2], $matches[3], $matches[1]);