7 * LICENSE: This source file is licensed under the terms of the GNU General Public License.
9 * @package Magic3 Framework
10 * @author 平田直毅(Naoki Hirata) <naoki@aplo.co.jp>
11 * @copyright Copyright 2006-2008 Magic3 Project.
12 * @license http://www.gnu.org/copyleft/gpl.html GPL License
13 * @version SVN: $Id: calendar_viewer.inc.php 1148 2008-10-28 07:16:55Z fishbone $
14 * @link http://www.magic3.org
16 // Calendar viewer plugin - List pages that calendar/calnedar2 plugin created
17 // (Based on calendar and recent plugin)
19 // Page title's date format
20 // * See PHP date() manual for detail
21 // * '$\w' = weeklabel defined in $_msg_week
22 define('PLUGIN_CALENDAR_VIEWER_DATE_FORMAT',
23 // FALSE // 'pagename/2004-02-09' -- As is
24 // 'D, d M, Y' // 'Mon, 09 Feb, 2004'
25 // 'F d, Y' // 'February 09, 2004'
26 // '[Y-m-d]' // '[2004-02-09]'
27 'Y/n/j ($\w)' // '2004/2/9 (Mon)'
32 define('PLUGIN_CALENDAR_VIEWER_USAGE',
33 '#calendar_viewer(pagename,this|yyyy-mm|n|x*y[,mode[,separater]])');
36 * - A working root of calendar or calendar2 plugin
42 * this - Show 'this month'
43 * yyyy-mm - Show pages at year:yyyy and month:mm
44 * n - Show first n pages
45 * x*n - Show first n pages from x-th page (0 = first)
48 * past - Show today, and the past below. Recommended for ChangeLogs and diaries (default)
49 * future - Show today, and the future below. Recommended for event planning and scheduling
50 * view - Show all, from the past to the future
53 * - Specify separator of yyyy/mm/dd
54 * - Default: '-' (yyyy-mm-dd)
57 * Stop showing links 'next month' and 'previous month' with past/future mode for 'this month'
58 * #calendar_viewer(pagename,this,past)
61 function plugin_calendar_viewer_convert()
63 //global $vars, $get, $post, $script, $weeklabels;
64 global $script, $weeklabels;
65 global $_msg_calendar_viewer_right, $_msg_calendar_viewer_left;
66 global $_msg_calendar_viewer_restrict, $_err_calendar_viewer_param2;
68 static $viewed = array();
70 if (func_num_args() < 2)
71 return PLUGIN_CALENDAR_VIEWER_USAGE . '<br />' . "\n";
73 $func_args = func_get_args();
76 $pagename = $func_args[0]; // 基準となるページ名
77 $page_YM = ''; // 一覧表示する年月
78 $limit_base = 0; // 先頭から数えて何ページ目から表示するか (先頭)
79 $limit_pitch = 0; // 何件づつ表示するか
80 $limit_page = 0; // サーチするページ数
81 $mode = 'past'; // 動作モード
82 $date_sep = '-'; // 日付のセパレータ calendar2なら '-', calendarなら ''
84 // Check $func_args[1]
86 if (preg_match('/[0-9]{4}' . $date_sep . '[0-9]{2}/', $func_args[1])) {
88 $page_YM = $func_args[1];
90 } else if (preg_match('/this/si', $func_args[1])) {
92 $page_YM = get_date('Y' . $date_sep . 'm');
94 } else if (preg_match('/^[0-9]+$/', $func_args[1])) {
96 $limit_pitch = $func_args[1];
97 $limit_page = $func_args[1];
98 } else if (preg_match('/(-?[0-9]+)\*([0-9]+)/', $func_args[1], $matches)) {
99 // 先頭より数えて x ページ目から、y件づつ表示
100 $limit_base = $matches[1];
101 $limit_pitch = $matches[2];
102 $limit_page = $matches[1] + $matches[2]; // 読み飛ばす + 表示する
104 return '#calendar_viewer(): ' . $_err_calendar_viewer_param2 . '<br />' . "\n";
107 // $func_args[2]: Mode setting
108 if (isset($func_args[2]) && preg_match('/^(past|view|future)$/si', $func_args[2]))
109 $mode = $func_args[2];
111 // $func_args[3]: Change default delimiter
112 if (isset($func_args[3])) $date_sep = $func_args[3];
115 if (isset($viewed[$pagename])) {
116 $s_page = htmlspecialchars($pagename);
117 return "#calendar_viewer(): You already view: $s_page<br />";
119 $viewed[$pagename] = TRUE; // Valid
122 // 一覧表示するページ名とファイル名のパターン ファイル名には年月を含む
123 if ($pagename == '') {
124 // pagename無しのyyyy-mm-ddに対応するための処理
126 $pagepattern_len = 0;
127 // $filepattern = encode($page_YM);
128 // $filepattern_len = strlen($filepattern);
129 $pageMatchPattern = $page_YM;
130 $pageMatchPattern_len = strlen($pageMatchPattern);
132 $pagepattern = strip_bracket($pagename) . '/';
133 $pagepattern_len = strlen($pagepattern);
134 // $filepattern = encode($pagepattern . $page_YM);
135 // $filepattern_len = strlen($filepattern);
136 $pageMatchPattern = $pagepattern . $page_YM;
137 $pageMatchPattern_len = strlen($pageMatchPattern);
143 if ($dir = @opendir(DATA_DIR)) {
144 $_date = get_date('Y' . $date_sep . 'm' . $date_sep . 'd');
146 while($file = readdir($dir)) {
147 if ($file == '..' || $file == '.') continue;
148 if (substr($file, 0, $filepattern_len) != $filepattern) continue;
150 $page = decode(trim(preg_replace('/\.txt$/', ' ', $file)));
151 $page_date = substr($page, $pagepattern_len);
153 // Verify the $page_date pattern (Default: yyyy-mm-dd).
154 // Past-mode hates the future, and
155 // Future-mode hates the past.
156 if ((plugin_calendar_viewer_isValidDate($page_date, $date_sep) == FALSE) ||
157 ($page_date > $_date && ($mode == 'past')) ||
158 ($page_date < $_date && ($mode == 'future')))
166 $_date = get_date('Y' . $date_sep . 'm' . $date_sep . 'd');
167 $pages = WikiPage::getPages();
168 foreach ($pages as $page){
169 if (substr($page, 0, $pageMatchPattern_len) != $pageMatchPattern) continue;
171 $page_date = substr($page, $pagepattern_len);
173 // Verify the $page_date pattern (Default: yyyy-mm-dd).
174 // Past-mode hates the future, and
175 // Future-mode hates the past.
176 if ((plugin_calendar_viewer_isValidDate($page_date, $date_sep) == FALSE) ||
177 ($page_date > $_date && ($mode == 'past')) ||
178 ($page_date < $_date && ($mode == 'future')))
183 if ($mode == 'past') {
184 rsort($pagelist); // New => Old
186 sort($pagelist); // Old => New
190 //$tmppage = $vars['page'];
191 $tmppage = WikiParam::getPage();
194 // $limit_page の件数までインクルード
195 $tmp = max($limit_base, 0); // Skip minus
196 while ($tmp < $limit_page) {
197 if (! isset($pagelist[$tmp])) break;
199 $page = $pagelist[$tmp];
200 //$get['page'] = $post['page'] = $vars['page'] = $page;
201 WikiParam::setPage($page);
203 // 現状で閲覧許可がある場合だけ表示する
204 if (check_readable($page, FALSE, FALSE)) {
205 $body = convert_html(get_source($page));
207 $body = str_replace('$1', $page, $_msg_calendar_viewer_restrict);
210 $r_page = rawurlencode($page);
212 if (PLUGIN_CALENDAR_VIEWER_DATE_FORMAT !== FALSE) {
213 $time = strtotime(basename($page)); // $date_sep must be assumed '-' or ''!
215 $s_page = htmlspecialchars($page); // Failed. Why?
217 $week = $weeklabels[date('w', $time)];
218 $s_page = htmlspecialchars(str_replace(
221 date(PLUGIN_CALENDAR_VIEWER_DATE_FORMAT, $time)
225 $s_page = htmlspecialchars($page);
229 //$link = $script . '?' . $r_page;
230 $link = $script . WikiParam::convQuery('?' . $r_page);
232 //$link = $script . '?cmd=edit&page=' . $r_page;
233 $link = $script . WikiParam::convQuery('?cmd=edit&page=' . $r_page);
235 $link = '<a href="' . $link . '">' . $s_page . '</a>';
237 $head = '<h1>' . $link . '</h1>' . "\n";
238 $return_body .= $head . $body;
244 // ?plugin=calendar_viewer&file=ページ名&date=yyyy-mm
245 $enc_pagename = rawurlencode(substr($pagepattern, 0, $pagepattern_len - 1));
247 if ($page_YM != '') {
249 $date_sep_len = strlen($date_sep);
250 $this_year = substr($page_YM, 0, 4);
251 $this_month = substr($page_YM, 4 + $date_sep_len, 2);
254 $next_year = $this_year;
255 $next_month = $this_month + 1;
256 if ($next_month > 12) {
260 $next_YM = sprintf('%04d%s%02d', $next_year, $date_sep, $next_month);
263 $prev_year = $this_year;
264 $prev_month = $this_month - 1;
265 if ($prev_month < 1) {
269 $prev_YM = sprintf('%04d%s%02d', $prev_year, $date_sep, $prev_month);
270 if ($mode == 'past') {
271 $right_YM = $prev_YM;
272 $right_text = $prev_YM . '>>'; // >>
274 $left_text = '<<' . $next_YM; // <<
277 $left_text = '<<' . $prev_YM; // <<
278 $right_YM = $next_YM;
279 $right_text = $next_YM . '>>'; // >>
283 if ($limit_base <= 0) {
284 $left_YM = ''; // 表示しない (それより前の項目はない)
286 $left_YM = $limit_base - $limit_pitch . '*' . $limit_pitch;
287 $left_text = sprintf($_msg_calendar_viewer_left, $limit_pitch);
290 if ($limit_base + $limit_pitch >= count($pagelist)) {
291 $right_YM = ''; // 表示しない (それより後の項目はない)
293 $right_YM = $limit_base + $limit_pitch . '*' . $limit_pitch;
294 $right_text = sprintf($_msg_calendar_viewer_right, $limit_pitch);
299 if ($left_YM != '' || $right_YM != '') {
300 $s_date_sep = htmlspecialchars($date_sep);
301 $left_link = $right_link = '';
302 /* $link = $script . '?plugin=calendar_viewer&mode=' . $mode . '&file=' . $enc_pagename . '&date_sep=' . $s_date_sep . '&';
303 if ($left_YM != '') $left_link = '<a href="' . $link . 'date=' . $left_YM . '">' . $left_text . '</a>';
304 if ($right_YM != '') $right_link = '<a href="' . $link . 'date=' . $right_YM . '">' . $right_text . '</a>';*/
306 $linkL = $script . WikiParam::convQuery('?plugin=calendar_viewer&mode=' . $mode . '&file=' . $enc_pagename . '&date_sep=' . $s_date_sep . '&' . 'date=' . $left_YM);
307 $linkR = $script . WikiParam::convQuery('?plugin=calendar_viewer&mode=' . $mode . '&file=' . $enc_pagename . '&date_sep=' . $s_date_sep . '&' . 'date=' . $right_YM);
308 if ($left_YM != '') $left_link = '<a href="' . $linkL . '">' . $left_text . '</a>';
309 if ($right_YM != '') $right_link = '<a href="' . $linkR . '">' . $right_text . '</a>';
311 // past modeは<<新 旧>> 他は<<旧 新>>
313 '<div class="calendar_viewer">' .
314 '<span class="calendar_viewer_left">' . $left_link . '</span>' .
315 '<span class="calendar_viewer_right">' . $right_link . '</span>' .
319 //$get['page'] = $post['page'] = $vars['page'] = $tmppage;
320 WikiParam::setPage($tmppage);
325 function plugin_calendar_viewer_action()
327 //global $vars, $get, $post, $script;
332 $return_vars_array = array();
334 $page = strip_bracket($vars['page']);
336 if (isset($vars['file'])) $vars['page'] = $vars['file'];*/
337 $savePage = WikiParam::getPage();
339 if (WikiParam::getVar('file') != '') $page = WikiParam::getVar('file');
340 WikiParam::setPage($page);
342 //$date_sep = $vars['date_sep'];
343 $date_sep = WikiParam::getVar('date_sep');
345 //$page_YM = $vars['date'];
346 $page_YM = WikiParam::getVar('date');
347 if ($page_YM == '') $page_YM = get_date('Y' . $date_sep . 'm');
348 //$mode = $vars['mode'];
349 $mode = WikiParam::getVar('mode');
351 //$args_array = array($vars['page'], $page_YM, $mode, $date_sep);
352 $args_array = array($page, $page_YM, $mode, $date_sep);
353 $return_vars_array['body'] = call_user_func_array('plugin_calendar_viewer_convert', $args_array);
355 //$return_vars_array['msg'] = 'calendar_viewer ' . $vars['page'] . '/' . $page_YM;
356 //$return_vars_array['msg'] = 'calendar_viewer ' . htmlspecialchars($vars['page']);
357 $return_vars_array['msg'] = 'calendar_viewer ' . htmlspecialchars($page);
358 //if ($vars['page'] != '') $return_vars_array['msg'] .= '/';
359 if ($page != '') $return_vars_array['msg'] .= '/';
360 if (preg_match('/\*/', $page_YM)) {
361 // うーん、n件表示の時はなんてページ名にしたらいい?
363 $return_vars_array['msg'] .= htmlspecialchars($page_YM);
366 //$vars['page'] = $page;
367 WikiParam::setPage($savePage);
368 return $return_vars_array;
371 function plugin_calendar_viewer_isValidDate($aStr, $aSepList = '-/ .')
374 if ($aSepList == '') {
375 // yyymmddとしてチェック(手抜き(^^;)
376 return checkdate(substr($aStr, 4, 2), substr($aStr, 6, 2), substr($aStr, 0, 4));
377 } else if (ereg("^([0-9]{2,4})[$aSepList]([0-9]{1,2})[$aSepList]([0-9]{1,2})$", $aStr, $matches) ) {
378 return checkdate($matches[2], $matches[3], $matches[1]);