OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / widgets / wiki_main / include / plugin / calendar_viewer.inc.php
1 <?php
2 /**
3  * calendar_viewerプラグイン
4  *
5  * PHP versions 5
6  *
7  * LICENSE: This source file is licensed under the terms of the GNU General Public License.
8  *
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
15  */
16 // Calendar viewer plugin - List pages that calendar/calnedar2 plugin created
17 // (Based on calendar and recent plugin)
18
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)'
28         );
29
30 // ----
31
32 define('PLUGIN_CALENDAR_VIEWER_USAGE',
33         '#calendar_viewer(pagename,this|yyyy-mm|n|x*y[,mode[,separater]])');
34 /*
35  ** pagename
36  * - A working root of calendar or calendar2 plugin
37  *   pagename/2004-12-30
38  *   pagename/2004-12-31
39  *   ...
40  *
41  ** (yyyy-mm|n|this)
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)
46  *
47  ** [mode]
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
51  *
52  ** [separater]
53  * - Specify separator of yyyy/mm/dd
54  * - Default: '-' (yyyy-mm-dd)
55  *
56  * TODO
57  *  Stop showing links 'next month' and 'previous month' with past/future mode for 'this month'
58  *    #calendar_viewer(pagename,this,past)
59  */
60
61 function plugin_calendar_viewer_convert()
62 {
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;
67
68         static $viewed = array();
69
70         if (func_num_args() < 2)
71                 return PLUGIN_CALENDAR_VIEWER_USAGE . '<br />' . "\n";
72
73         $func_args = func_get_args();
74
75         // Default values
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なら ''
83
84         // Check $func_args[1]
85         $matches = array();
86         if (preg_match('/[0-9]{4}' . $date_sep . '[0-9]{2}/', $func_args[1])) {
87                 // 指定年月の一覧表示
88                 $page_YM     = $func_args[1];
89                 $limit_page  = 31;
90         } else if (preg_match('/this/si', $func_args[1])) {
91                 // 今月の一覧表示
92                 $page_YM     = get_date('Y' . $date_sep . 'm');
93                 $limit_page  = 31;
94         } else if (preg_match('/^[0-9]+$/', $func_args[1])) {
95                 // n日分表示
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]; // 読み飛ばす + 表示する
103         } else {
104                 return '#calendar_viewer(): ' . $_err_calendar_viewer_param2 . '<br />' . "\n";
105         }
106
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];
110
111         // $func_args[3]: Change default delimiter
112         if (isset($func_args[3])) $date_sep = $func_args[3];
113
114         // Avoid Loop etc.
115         if (isset($viewed[$pagename])) {
116                 $s_page = htmlspecialchars($pagename);
117                 return "#calendar_viewer(): You already view: $s_page<br />";
118         } else {
119                 $viewed[$pagename] = TRUE; // Valid
120         }
121
122         // 一覧表示するページ名とファイル名のパターン ファイル名には年月を含む
123         if ($pagename == '') {
124                 // pagename無しのyyyy-mm-ddに対応するための処理
125                 $pagepattern     = '';
126                 $pagepattern_len = 0;
127 //              $filepattern     = encode($page_YM);
128 //              $filepattern_len = strlen($filepattern);
129                 $pageMatchPattern     = $page_YM;
130                 $pageMatchPattern_len = strlen($pageMatchPattern);
131         } else {
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);
138         }
139
140         // ページリストの取得
141         /*
142         $pagelist = array();
143         if ($dir = @opendir(DATA_DIR)) {
144                 $_date = get_date('Y' . $date_sep . 'm' . $date_sep . 'd');
145                 $page_date  = '';
146                 while($file = readdir($dir)) {
147                         if ($file == '..' || $file == '.') continue;
148                         if (substr($file, 0, $filepattern_len) != $filepattern) continue;
149
150                         $page      = decode(trim(preg_replace('/\.txt$/', ' ', $file)));
151                         $page_date = substr($page, $pagepattern_len);
152
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')))
159                                         continue;
160
161                         $pagelist[] = $page;
162                 }
163         }
164         closedir($dir);*/
165         $pagelist = array();
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;
170
171                 $page_date = substr($page, $pagepattern_len);
172
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')))
179                                 continue;
180
181                 $pagelist[] = $page;
182         }
183         if ($mode == 'past') {
184                 rsort($pagelist);       // New => Old
185         } else {
186                 sort($pagelist);        // Old => New
187         }
188
189         // Include start
190         //$tmppage     = $vars['page'];
191         $tmppage     = WikiParam::getPage();
192         $return_body = '';
193
194         // $limit_page の件数までインクルード
195         $tmp = max($limit_base, 0); // Skip minus
196         while ($tmp < $limit_page) {
197                 if (! isset($pagelist[$tmp])) break;
198
199                 $page = $pagelist[$tmp];
200                 //$get['page'] = $post['page'] = $vars['page'] = $page;
201                 WikiParam::setPage($page);
202
203                 // 現状で閲覧許可がある場合だけ表示する
204                 if (check_readable($page, FALSE, FALSE)) {
205                         $body = convert_html(get_source($page));
206                 } else {
207                         $body = str_replace('$1', $page, $_msg_calendar_viewer_restrict);
208                 }
209
210                 $r_page = rawurlencode($page);
211
212                 if (PLUGIN_CALENDAR_VIEWER_DATE_FORMAT !== FALSE) {
213                         $time = strtotime(basename($page)); // $date_sep must be assumed '-' or ''!
214                         if ($time == -1) {
215                                 $s_page = htmlspecialchars($page); // Failed. Why?
216                         } else {
217                                 $week   = $weeklabels[date('w', $time)];
218                                 $s_page = htmlspecialchars(str_replace(
219                                                 array('$w' ),
220                                                 array($week),
221                                                 date(PLUGIN_CALENDAR_VIEWER_DATE_FORMAT, $time)
222                                         ));
223                         }
224                 } else {
225                         $s_page = htmlspecialchars($page);
226                 }
227
228                 if (PKWK_READONLY) {
229                         //$link   = $script . '?' . $r_page;
230                         $link   = $script . WikiParam::convQuery('?' . $r_page);
231                 } else {
232                         //$link   = $script . '?cmd=edit&amp;page=' . $r_page;
233                         $link   = $script . WikiParam::convQuery('?cmd=edit&amp;page=' . $r_page);
234                 }
235                 $link   = '<a href="' . $link . '">' . $s_page . '</a>';
236
237                 $head   = '<h1>' . $link . '</h1>' . "\n";
238                 $return_body .= $head . $body;
239
240                 ++$tmp;
241         }
242
243         // ここで、前後のリンクを表示
244         // ?plugin=calendar_viewer&file=ページ名&date=yyyy-mm
245         $enc_pagename = rawurlencode(substr($pagepattern, 0, $pagepattern_len - 1));
246
247         if ($page_YM != '') {
248                 // 年月表示時
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);
252
253                 // 次月
254                 $next_year  = $this_year;
255                 $next_month = $this_month + 1;
256                 if ($next_month > 12) {
257                         ++$next_year;
258                         $next_month = 1;
259                 }
260                 $next_YM = sprintf('%04d%s%02d', $next_year, $date_sep, $next_month);
261
262                 // 前月
263                 $prev_year  = $this_year;
264                 $prev_month = $this_month - 1;
265                 if ($prev_month < 1) {
266                         --$prev_year;
267                         $prev_month = 12;
268                 }
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 . '&gt;&gt;'; // >>
273                         $left_YM    = $next_YM;
274                         $left_text  = '&lt;&lt;' . $next_YM; // <<
275                 } else {
276                         $left_YM    = $prev_YM;
277                         $left_text  = '&lt;&lt;' . $prev_YM; // <<
278                         $right_YM   = $next_YM;
279                         $right_text = $next_YM . '&gt;&gt;'; // >>
280                 }
281         } else {
282                 // n件表示時
283                 if ($limit_base <= 0) {
284                         $left_YM = ''; // 表示しない (それより前の項目はない)
285                 } else {
286                         $left_YM   = $limit_base - $limit_pitch . '*' . $limit_pitch;
287                         $left_text = sprintf($_msg_calendar_viewer_left, $limit_pitch);
288
289                 }
290                 if ($limit_base + $limit_pitch >= count($pagelist)) {
291                         $right_YM = ''; // 表示しない (それより後の項目はない)
292                 } else {
293                         $right_YM   = $limit_base + $limit_pitch . '*' . $limit_pitch;
294                         $right_text = sprintf($_msg_calendar_viewer_right, $limit_pitch);
295                 }
296         }
297
298         // ナビゲート用のリンクを末尾に追加
299         if ($left_YM != '' || $right_YM != '') {
300                 $s_date_sep = htmlspecialchars($date_sep);
301                 $left_link = $right_link = '';
302 /*              $link = $script . '?plugin=calendar_viewer&amp;mode=' . $mode . '&amp;file=' . $enc_pagename . '&amp;date_sep=' . $s_date_sep . '&amp;';
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>';*/
305                 
306                 $linkL = $script . WikiParam::convQuery('?plugin=calendar_viewer&amp;mode=' . $mode . '&amp;file=' . $enc_pagename . '&amp;date_sep=' . $s_date_sep . '&amp;' . 'date=' . $left_YM);
307                 $linkR = $script . WikiParam::convQuery('?plugin=calendar_viewer&amp;mode=' . $mode . '&amp;file=' . $enc_pagename . '&amp;date_sep=' . $s_date_sep . '&amp;' . '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>';
310                 
311                 // past modeは<<新 旧>> 他は<<旧 新>>
312                 $return_body .=
313                         '<div class="calendar_viewer">' .
314                         '<span class="calendar_viewer_left">'  . $left_link  . '</span>' .
315                         '<span class="calendar_viewer_right">' . $right_link . '</span>' .
316                         '</div>';
317         }
318
319         //$get['page'] = $post['page'] = $vars['page'] = $tmppage;
320         WikiParam::setPage($tmppage);
321
322         return $return_body;
323 }
324
325 function plugin_calendar_viewer_action()
326 {
327         //global $vars, $get, $post, $script;
328         global $script;
329
330         $date_sep = '-';
331
332         $return_vars_array = array();
333 /*
334         $page = strip_bracket($vars['page']);
335         $vars['page'] = '*';
336         if (isset($vars['file'])) $vars['page'] = $vars['file'];*/
337         $savePage = WikiParam::getPage();
338         $page = '*';
339         if (WikiParam::getVar('file') != '') $page = WikiParam::getVar('file');
340         WikiParam::setPage($page);
341
342         //$date_sep = $vars['date_sep'];
343         $date_sep = WikiParam::getVar('date_sep');
344
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');
350
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);
354
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件表示の時はなんてページ名にしたらいい?
362         } else {
363                 $return_vars_array['msg'] .= htmlspecialchars($page_YM);
364         }
365
366         //$vars['page'] = $page;
367         WikiParam::setPage($savePage);
368         return $return_vars_array;
369 }
370
371 function plugin_calendar_viewer_isValidDate($aStr, $aSepList = '-/ .')
372 {
373         $matches = array();
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]);
379         } else {
380                 return FALSE;
381         }
382 }
383 ?>