2 // PukiWiki - Yet another WikiWikiWeb clone.
5 // 2002-2016 PukiWiki Development Team
6 // 2002 Y.MASUI GPL2 http://masui.net/pukiwiki/ masui@masui.net
11 define('PLUGIN_BUGTRACK_NUMBER_FORMAT', '%d'); // Like 'page/1'
12 //define('PLUGIN_BUGTRACK_NUMBER_FORMAT', '%03d'); // Like 'page/001'
14 function plugin_bugtrack_init()
16 global $_plugin_bugtrack;
19 if (isset($init)) return; // Already init
20 if (isset($_plugin_bugtrack)) die('Global $_plugin_bugtrack had been init. Why?');
23 $_plugin_bugtrack = array(
24 'priority_list' => array('緊急', '重要', '普通', '低'),
25 'state_list' => array('提案', '着手', 'CVS待ち', '完了', '保留', '却下'),
26 'state_sort' => array('着手', 'CVS待ち', '保留', '完了', '提案', '却下'),
27 'state_bgcolor' => array('#ccccff', '#ffcc99', '#ccddcc', '#ccffcc', '#ffccff', '#cccccc', '#ff3333'),
28 'header_bgcolor' => '#ffffcc',
31 'nosummary'=> 'ここにサマリを記入して下さい',
38 'category' => 'カテゴリー',
40 'pagename_comment' => '空欄のままだと自動的にページ名が振られます。',
41 'version_comment' => '空欄でも構いません',
47 // #bugtrack: Show bugtrack form
48 function plugin_bugtrack_convert()
52 if (PKWK_READONLY) return ''; // Show nothing
54 $base = $vars['page'];
56 if (func_num_args()) {
57 $category = func_get_args();
58 $_base = get_fullname(strip_bracket(array_shift($category)), $base);
59 if (is_pagename($_base)) $base = $_base;
62 return plugin_bugtrack_print_form($base, $category);
65 function plugin_bugtrack_print_form($base, $category)
67 global $_plugin_bugtrack;
72 $select_priority = "\n";
73 $count = count($_plugin_bugtrack['priority_list']);
75 for ($i = 0; $i < $count; ++$i) {
76 if ($i == ($count - 1)) $selected = ' selected="selected"'; // The last one
77 $priority_list = htmlsc($_plugin_bugtrack['priority_list'][$i]);
78 $select_priority .= ' <option value="' . $priority_list . '"' .
79 $selected . '>' . $priority_list . '</option>' . "\n";
83 for ($i = 0; $i < count($_plugin_bugtrack['state_list']); ++$i) {
84 $state_list = htmlsc($_plugin_bugtrack['state_list'][$i]);
85 $select_state .= ' <option value="' . $state_list . '">' .
86 $state_list . '</option>' . "\n";
89 if (empty($category)) {
90 $encoded_category = '<input name="category" id="_p_bugtrack_category_' . $id .
93 $encoded_category = '<select name="category" id="_p_bugtrack_category_' . $id . '">';
94 foreach ($category as $_category) {
95 $s_category = htmlsc($_category);
96 $encoded_category .= '<option value="' . $s_category . '">' .
97 $s_category . '</option>' . "\n";
99 $encoded_category .= '</select>';
102 $script = get_script_uri();
103 $s_base = htmlsc($base);
104 $s_name = htmlsc($_plugin_bugtrack['name']);
105 $s_category = htmlsc($_plugin_bugtrack['category']);
106 $s_priority = htmlsc($_plugin_bugtrack['priority']);
107 $s_state = htmlsc($_plugin_bugtrack['state']);
108 $s_pname = htmlsc($_plugin_bugtrack['pagename']);
109 $s_pnamec = htmlsc($_plugin_bugtrack['pagename_comment']);
110 $s_version = htmlsc($_plugin_bugtrack['version']);
111 $s_versionc = htmlsc($_plugin_bugtrack['version_comment']);
112 $s_summary = htmlsc($_plugin_bugtrack['summary']);
113 $s_body = htmlsc($_plugin_bugtrack['body']);
114 $s_submit = htmlsc($_plugin_bugtrack['submit']);
116 <form action="$script" method="post">
119 <th><label for="_p_bugtrack_name_$id">$s_name</label></th>
120 <td><input id="_p_bugtrack_name_$id" name="name" size="20" type="text" /></td>
123 <th><label for="_p_bugtrack_category_$id">$s_category</label></th>
124 <td>$encoded_category</td>
127 <th><label for="_p_bugtrack_priority_$id">$s_priority</label></th>
128 <td><select id="_p_bugtrack_priority_$id" name="priority">$select_priority </select></td>
131 <th><label for="_p_bugtrack_state_$id">$s_state</label></th>
132 <td><select id="_p_bugtrack_state_$id" name="state">$select_state </select></td>
135 <th><label for="_p_bugtrack_pagename_$id">$s_pname</label></th>
136 <td><input id="_p_bugtrack_pagename_$id" name="pagename" size="20" type="text" />
137 <small>$s_pnamec</small></td>
140 <th><label for="_p_bugtrack_version_$id">$s_version</label></th>
141 <td><input id="_p_bugtrack_version_$id" name="version" size="10" type="text" />
142 <small>$s_versionc</small></td>
145 <th><label for="_p_bugtrack_summary_$id">$s_summary</label></th>
146 <td><input id="_p_bugtrack_summary_$id" name="summary" size="60" type="text" /></td>
149 <th><label for="_p_bugtrack_body_$id">$s_body</label></th>
150 <td><textarea id="_p_bugtrack_body_$id" name="body" cols="60" rows="6"></textarea></td>
153 <td colspan="2" align="center">
154 <input type="submit" value="$s_submit" />
155 <input type="hidden" name="plugin" value="bugtrack" />
156 <input type="hidden" name="mode" value="submit" />
157 <input type="hidden" name="base" value="$s_base" />
168 function plugin_bugtrack_action()
172 if (PKWK_READONLY) die_message('PKWK_READONLY prohibits editing');
173 if ($post['mode'] != 'submit') return FALSE;
175 $page = plugin_bugtrack_write($post['base'], $post['pagename'], $post['summary'],
176 $post['name'], $post['priority'], $post['state'], $post['category'],
177 $post['version'], $post['body']);
180 header('Location: ' . get_script_uri() . '?' . pagename_urlencode($page));
184 function plugin_bugtrack_write($base, $pagename, $summary, $name, $priority, $state, $category, $version, $body)
188 $base = strip_bracket($base);
189 $pagename = strip_bracket($pagename);
191 $postdata = plugin_bugtrack_template($base, $summary, $name, $priority,
192 $state, $category, $version, $body);
194 $page_list = plugin_bugtrack_get_page_list($base, false);
195 usort($page_list, '_plugin_bugtrack_list_paganame_compare');
196 if (count($page_list) == 0) {
199 $latest_page = $page_list[count($page_list) - 1]['name'];
200 $id = intval(substr($latest_page, strlen($base) + 1)) + 1;
202 $page = $base . '/' . sprintf(PLUGIN_BUGTRACK_NUMBER_FORMAT, $id);
204 if ($pagename == '') {
205 page_write($page, $postdata);
207 $pagename = get_fullname($pagename, $base);
208 if (is_page($pagename) || ! is_pagename($pagename)) {
209 $pagename = $page; // Set default
211 page_write($page, 'move to [[' . $pagename . ']]');
213 page_write($pagename, $postdata);
219 // Generate new page contents
220 function plugin_bugtrack_template($base, $summary, $name, $priority, $state, $category, $version, $body)
222 global $_plugin_bugtrack, $WikiName;
224 if (! preg_match("/^$WikiName$$/",$base)) $base = '[[' . $base . ']]';
225 if ($name != '' && ! preg_match("/^$WikiName$$/",$name)) $name = '[[' . $name . ']]';
227 if ($name == '') $name = $_plugin_bugtrack['noname'];
228 if ($summary == '') $summary = $_plugin_bugtrack['nosummary'];
233 - ${_plugin_bugtrack['base' ]}: $base
234 - ${_plugin_bugtrack['name' ]}: $name
235 - ${_plugin_bugtrack['priority']}: $priority
236 - ${_plugin_bugtrack['state' ]}: $state
237 - ${_plugin_bugtrack['category']}: $category
238 - ${_plugin_bugtrack['date' ]}: &now;
239 - ${_plugin_bugtrack['version' ]}: $version
241 ** ${_plugin_bugtrack['body']}
249 // ----------------------------------------
250 // BugTrack-List plugin
252 function _plugin_bugtrack_list_paganame_compare($a, $b)
254 return strnatcmp($a['name'], $b['name']);
259 * Get page list for "$page/"
261 function plugin_bugtrack_get_page_list($page, $needs_filetime) {
262 $page_list = array();
263 $pattern = $page . '/';
264 $pattern_len = strlen($pattern);
265 foreach (get_existpages() as $p) {
266 if (strncmp($p, $pattern, $pattern_len) === 0 && pkwk_ctype_digit(substr($p, $pattern_len))) {
267 if ($needs_filetime) {
268 $page_list[] = array('name'=>$p,'filetime'=>get_filetime($p));
270 $page_list[] = array('name'=>$p);
278 * #bugtrack_list plugin itself
280 * Cache specification
281 * * Enable only for PHP5.4+ (Because of JSON_UNESCAPE_UNICODE)
282 * * Use cached values for articles that have the unchaged filemtime
283 * * Invalidate all cache data everyday
285 function plugin_bugtrack_list_convert()
287 global $script, $vars, $_plugin_bugtrack;
288 $cache_format_version = 1;
289 $cache_expire_time = 60 * 60 * 24;
290 $cache_refresh_time_prev;
291 static $cache_enabled;
292 if (!isset($cache_enabled)) {
293 $cache_enabled = defined('JSON_UNESCAPED_UNICODE'); // PHP 5.4+
295 $page = $vars['page'];
296 if (func_num_args()) {
297 list($_page) = func_get_args();
298 $_page = get_fullname(strip_bracket($_page), $page);
299 if (is_pagename($_page)) $page = $_page;
302 $page_list = plugin_bugtrack_get_page_list($page, true);
303 usort($page_list, '_plugin_bugtrack_list_paganame_compare');
304 $count_list = count($_plugin_bugtrack['state_list']);
306 if ($cache_enabled) {
308 $data_updated = true;
309 $cache_filepath = CACHE_DIR . encode($page) . '.bugtrack';
310 $json_cached = pkwk_file_get_contents($cache_filepath);
312 $wrapdata = json_decode($json_cached);
313 if (is_object($wrapdata)) {
314 if (isset($wrapdata->version, $wrapdata->pages, $wrapdata->refreshed_at)) {
315 $cache_time_prev = $wrapdata->refreshed_at;
316 if ($cache_format_version == $wrapdata->version
317 && time() < $cache_time_prev + $cache_expire_time) {
318 $data = $wrapdata->pages;
319 $cache_refresh_time_prev = $cache_time_prev;
323 if (is_array($data) && !empty($data)) {
325 foreach ($page_list as $i=>$page_info) {
326 list($page_name, $no, $summary, $name, $priority, $state, $category, $filetime) = $data[$i];
327 if ($filetime !== $page_info['filetime'] || $page_name !== $page_info['name']) {
334 foreach ($data as $d) {
336 $data_map[$page_name] = $d;
344 if (empty($data) || !empty($data_map)) {
346 foreach ($page_list as $page_info) {
347 $page_name = $page_info['name'];
349 if (isset($data_map[$page_name])) {
350 $d = $data_map[$page_name];
351 list($page_name, $no, $summary, $name, $priority, $state, $category, $filetime) = $d;
352 if ($filetime == $page_info['filetime']) {
358 $data[] = plugin_bugtrack_list_pageinfo($page_info['name'], null, true, $page_info['filetime']);
361 foreach ($data as $i=>$line) {
362 list($page_name, $no, $summary, $name, $priority, $state, $category, $filetime, $state_no_cached, $html_cached) = $line;
363 if (isset($state_no_cached) && isset($html_cached)) {
366 foreach (array('summary', 'name', 'priority', 'state', 'category') as $item)
367 $$item = htmlsc($$item);
368 $page_link = make_pagelink($page_name);
370 $state_no = array_search($state, $_plugin_bugtrack['state_sort']);
371 if ($state_no === NULL || $state_no === FALSE) $state_no = $count_list;
372 $bgcolor = htmlsc($_plugin_bugtrack['state_bgcolor'][$state_no]);
376 <td style="background-color:$bgcolor">$page_link</td>
377 <td style="background-color:$bgcolor">$state</td>
378 <td style="background-color:$bgcolor">$priority</td>
379 <td style="background-color:$bgcolor">$category</td>
380 <td style="background-color:$bgcolor">$name</td>
381 <td style="background-color:$bgcolor">$summary</td>
385 $rec[] = $state_no; // color number
386 $rec[] = $row; // HTML content
388 if ($cache_enabled) {
390 if (isset($cache_refresh_time_prev)) {
391 $refreshed_at = $cache_refresh_time_prev;
393 $refreshed_at = time();
395 $json = array('refreshed_at'=>$refreshed_at, 'pages'=>$data, 'version'=>$cache_format_version);
396 $cache_body = json_encode($json, JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES);
397 file_put_contents($cache_filepath, $cache_body, LOCK_EX);
401 for ($i = 0; $i <= $count_list + 1; ++$i) $table[$i] = array();
402 foreach ($data as $line) {
403 list($page_name, $no, $summary, $name, $priority, $state, $category, $filetime, $state_no, $html) = $line;
404 $table[$state_no][$no] = $html;
406 $table_html = ' <tr>' . "\n";
407 $bgcolor = htmlsc($_plugin_bugtrack['header_bgcolor']);
408 foreach (array('pagename', 'state', 'priority', 'category', 'name', 'summary') as $item)
409 $table_html .= ' <th style="background-color:' . $bgcolor . '">' .
410 htmlsc($_plugin_bugtrack[$item]) . '</th>' . "\n";
411 $table_html .= ' </tr>' . "\n";
412 for ($i = 0; $i <= $count_list; ++$i) {
413 ksort($table[$i], SORT_NUMERIC);
414 $table_html .= join("\n", $table[$i]);
416 return '<table border="1" width="100%">' . "\n" .
421 // Get one set of data from a page (or a page moved to $page)
422 function plugin_bugtrack_list_pageinfo($page, $no = NULL, $recurse = TRUE, $filetime = NULL)
424 global $WikiName, $InterWikiName, $BracketName, $_plugin_bugtrack;
427 $no = preg_match('/\/([0-9]+)$/', $page, $matches) ? $matches[1] : 0;
429 $source = get_source($page);
431 // Check 'moved' page _just once_
432 $regex = "/move\s*to\s*($WikiName|$InterWikiName|\[\[$BracketName\]\])/";
434 if ($recurse && preg_match($regex, $source[0], $match))
435 return plugin_bugtrack_list_pageinfo(strip_bracket($match[1]), $no, FALSE, $filetime);
437 $body = join("\n", $source);
438 foreach(array('summary', 'name', 'priority', 'state', 'category') as $item) {
439 $regex = '/-\s*' . preg_quote($_plugin_bugtrack[$item], '/') . '\s*:(.*)/';
440 if (preg_match($regex, $body, $matches)) {
441 if ($item == 'name') {
442 $$item = strip_bracket(trim($matches[1]));
444 $$item = trim($matches[1]);
447 $$item = ''; // Data not found
451 if (preg_match("/\*([^\n]*)/", $body, $matches)) {
452 $summary = $matches[1];
453 make_heading($summary);
456 return array($page, $no, $summary, $name, $priority, $state, $category, $filetime);