OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / widgets / wiki_main / include / plugin / bugtrack.inc.php
1 <?php
2 /**
3  * bugtrackプラグイン
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-2009 Magic3 Project.
12  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
13  * @version    SVN: $Id: bugtrack.inc.php 1601 2009-03-21 05:51:06Z fishbone $
14  * @link       http://www.magic3.org
15  */
16 // Copyright:
17 // 2002-2005 PukiWiki Developers Team
18 // 2002 Y.MASUI GPL2  http://masui.net/pukiwiki/ masui@masui.net
19
20 // Numbering format
21 define('PLUGIN_BUGTRACK_NUMBER_FORMAT', '%d'); // Like 'page/1'
22 //define('PLUGIN_BUGTRACK_NUMBER_FORMAT', '%03d'); // Like 'page/001'
23
24 function plugin_bugtrack_init()
25 {
26         global $_plugin_bugtrack;
27         static $init;
28
29         if (isset($init)) return; // Already init
30         if (isset($_plugin_bugtrack)) die('Global $_plugin_bugtrack had been init. Why?');
31         $init = TRUE;
32
33         $_plugin_bugtrack = array(
34                 'priority_list'  => array('緊急', '重要', '普通', '低'),
35                 'state_list'     => array('提案', '着手', 'CVS待ち', '完了', '保留', '却下'),
36                 'state_sort'     => array('着手', 'CVS待ち', '保留', '完了', '提案', '却下'),
37                 'state_bgcolor'  => array('#ccccff', '#ffcc99', '#ccddcc', '#ccffcc', '#ffccff', '#cccccc', '#ff3333'),
38                 'header_bgcolor' => '#ffffcc',
39                 'base'     => 'ページ',
40                 'summary'  => 'サマリ',
41                 'nosummary'=> 'ここにサマリを記入して下さい',
42                 'priority' => '優先順位',
43                 'state'    => '状態',
44                 'name'     => '投稿者',
45                 'noname'   => '名無しさん',
46                 'date'     => '投稿日',
47                 'body'     => 'メッセージ',
48                 'category' => 'カテゴリー',
49                 'pagename' => 'ページ名',
50                 'pagename_comment' => '空欄のままだと自動的にページ名が振られます。',
51                 'version_comment'  => '空欄でも構いません',
52                 'version'  => 'バージョン',
53                 'submit'   => '追加'
54                 );
55 }
56
57 // #bugtrack: Show bugtrack form
58 function plugin_bugtrack_convert()
59 {
60         //global $vars;
61
62         if (PKWK_READONLY) return ''; // Show nothing
63
64         //$base = $vars['page'];
65         $base = WikiParam::getPage();
66         $category = array();
67         if (func_num_args()) {
68                 $category = func_get_args();
69                 $_base    = get_fullname(strip_bracket(array_shift($category)), $base);
70                 if (is_pagename($_base)) $base = $_base;
71         }
72
73         return plugin_bugtrack_print_form($base, $category);
74 }
75
76 function plugin_bugtrack_print_form($base, $category)
77 {
78         global $_plugin_bugtrack;
79         static $id = 0;
80
81         ++$id;
82
83         $select_priority = "\n";
84         $count = count($_plugin_bugtrack['priority_list']);
85         $selected = '';
86         for ($i = 0; $i < $count; ++$i) {
87                 if ($i == ($count - 1)) $selected = ' selected="selected"'; // The last one
88                 $priority_list = htmlspecialchars($_plugin_bugtrack['priority_list'][$i]);
89                 $select_priority .= '    <option value="' . $priority_list . '"' .
90                         $selected . '>' . $priority_list . '</option>' . "\n";
91         }
92
93         $select_state = "\n";
94         for ($i = 0; $i < count($_plugin_bugtrack['state_list']); ++$i) {
95                 $state_list = htmlspecialchars($_plugin_bugtrack['state_list'][$i]);
96                 $select_state .= '    <option value="' . $state_list . '">' .
97                         $state_list . '</option>' . "\n";
98         }
99
100         if (empty($category)) {
101                 $encoded_category = '<input name="category" id="_p_bugtrack_category_' . $id .
102                         '" type="text" />';
103         } else {
104                 $encoded_category = '<select name="category" id="_p_bugtrack_category_' . $id . '">';
105                 foreach ($category as $_category) {
106                         $s_category = htmlspecialchars($_category);
107                         $encoded_category .= '<option value="' . $s_category . '">' .
108                                 $s_category . '</option>' . "\n";
109                 }
110                 $encoded_category .= '</select>';
111         }
112
113         //$script     = get_script_uri();
114         $script     = get_script_uri() . WikiParam::convQuery('?');
115         $s_base     = htmlspecialchars($base);
116         $s_name     = htmlspecialchars($_plugin_bugtrack['name']);
117         $s_category = htmlspecialchars($_plugin_bugtrack['category']);
118         $s_priority = htmlspecialchars($_plugin_bugtrack['priority']);
119         $s_state    = htmlspecialchars($_plugin_bugtrack['state']);
120         $s_pname    = htmlspecialchars($_plugin_bugtrack['pagename']);
121         $s_pnamec   = htmlspecialchars($_plugin_bugtrack['pagename_comment']);
122         $s_version  = htmlspecialchars($_plugin_bugtrack['version']);
123         $s_versionc = htmlspecialchars($_plugin_bugtrack['version_comment']);
124         $s_summary  = htmlspecialchars($_plugin_bugtrack['summary']);
125         $s_body     = htmlspecialchars($_plugin_bugtrack['body']);
126         $s_submit   = htmlspecialchars($_plugin_bugtrack['submit']);
127         $body = <<<EOD
128 <form action="$script" method="post" class="form">
129  <table border="0">
130   <tr>
131    <th><label for="_p_bugtrack_name_$id">$s_name</label></th>
132    <td><input  id="_p_bugtrack_name_$id" name="name" size="20" type="text" /></td>
133   </tr>
134   <tr>
135    <th><label for="_p_bugtrack_category_$id">$s_category</label></th>
136    <td>$encoded_category</td>
137   </tr>
138   <tr>
139    <th><label for="_p_bugtrack_priority_$id">$s_priority</label></th>
140    <td><select id="_p_bugtrack_priority_$id" name="priority">$select_priority   </select></td>
141   </tr>
142   <tr>
143    <th><label for="_p_bugtrack_state_$id">$s_state</label></th>
144    <td><select id="_p_bugtrack_state_$id" name="state">$select_state   </select></td>
145   </tr>
146   <tr>
147    <th><label for="_p_bugtrack_pagename_$id">$s_pname</label></th>
148    <td><input  id="_p_bugtrack_pagename_$id" name="pagename" size="20" type="text" />
149     <small>$s_pnamec</small></td>
150   </tr>
151   <tr>
152    <th><label for="_p_bugtrack_version_$id">$s_version</label></th>
153    <td><input  id="_p_bugtrack_version_$id" name="version" size="10" type="text" />
154     <small>$s_versionc</small></td>
155   </tr>
156   <tr>
157    <th><label for="_p_bugtrack_summary_$id">$s_summary</label></th>
158    <td><input  id="_p_bugtrack_summary_$id" name="summary" size="60" type="text" /></td>
159   </tr>
160   <tr>
161    <th><label   for="_p_bugtrack_body_$id">$s_body</label></th>
162    <td><textarea id="_p_bugtrack_body_$id" name="body" class="wiki_edit" cols="60" rows="6"></textarea></td>
163   </tr>
164   <tr>
165    <td colspan="2" align="right">
166     <input type="submit" class="button" class="button" value="$s_submit" />
167     <input type="hidden" name="plugin" value="bugtrack" />
168     <input type="hidden" name="mode"   value="submit" />
169     <input type="hidden" name="base"   value="$s_base" />
170    </td>
171   </tr>
172  </table>
173 </form>
174 EOD;
175         return $body;
176 }
177
178 // Add new issue
179 function plugin_bugtrack_action()
180 {
181         //global $post;
182         global $gPageManager;
183         
184         if (PKWK_READONLY) die_message('PKWK_READONLY prohibits editing');
185         //if ($post['mode'] != 'submit') return FALSE;
186         if (WikiParam::getPostVar('mode') != 'submit') return FALSE;
187
188 /*      $page = plugin_bugtrack_write($post['base'], $post['pagename'], $post['summary'],
189                 $post['name'], $post['priority'], $post['state'], $post['category'],
190                 $post['version'], $post['body']);*/
191                 
192         $page = plugin_bugtrack_write(WikiParam::getPostVar('base'), WikiParam::getPostVar('pagename'), WikiParam::getPostVar('summary'),
193                 WikiParam::getPostVar('name'), WikiParam::getPostVar('priority'), WikiParam::getPostVar('state'), WikiParam::getPostVar('category'),
194                 WikiParam::getPostVar('version'), WikiParam::getPostVar('body'));
195
196         // ページ作成処理中断
197         $gPageManager->abortPage();
198         
199         pkwk_headers_sent();
200         //header('Location: ' . get_script_uri() . '?' . rawurlencode($page));
201         header('Location: ' . get_script_uri() . WikiParam::convQuery('?' . rawurlencode($page), false));
202         //exit;
203         
204         // システム強制終了
205         $gPageManager->exitSystem();
206 }
207
208 function plugin_bugtrack_write($base, $pagename, $summary, $name, $priority, $state, $category, $version, $body)
209 {
210         //global $post;
211
212         $base     = strip_bracket($base);
213         $pagename = strip_bracket($pagename);
214
215         $postdata = plugin_bugtrack_template($base, $summary, $name, $priority,
216                 $state, $category, $version, $body);
217
218         $id = $jump = 1;
219         $page = $base . '/' . sprintf(PLUGIN_BUGTRACK_NUMBER_FORMAT, $id);
220         while (is_page($page)) {
221                 $id   = $jump;
222                 $jump += 50;
223                 $page = $base . '/' . sprintf(PLUGIN_BUGTRACK_NUMBER_FORMAT, $jump);
224         }
225         $page = $base . '/' . sprintf(PLUGIN_BUGTRACK_NUMBER_FORMAT, $id);
226         while (is_page($page))
227                 $page = $base . '/' . sprintf(PLUGIN_BUGTRACK_NUMBER_FORMAT, ++$id);
228
229         if ($pagename == '') {
230                 page_write($page, $postdata);
231         } else {
232                 $pagename = get_fullname($pagename, $base);
233                 if (is_page($pagename) || ! is_pagename($pagename)) {
234                         $pagename = $page; // Set default
235                 } else {
236                         page_write($page, 'move to [[' . $pagename . ']]');
237                 }
238                 page_write($pagename, $postdata);
239         }
240
241         return $page;
242 }
243
244 // Generate new page contents
245 function plugin_bugtrack_template($base, $summary, $name, $priority, $state, $category, $version, $body)
246 {
247         global $_plugin_bugtrack, $WikiName;
248
249         if (! preg_match("/^$WikiName$$/",$base)) $base = '[[' . $base . ']]';
250         if ($name != '' && ! preg_match("/^$WikiName$$/",$name)) $name = '[[' . $name . ']]';
251
252         if ($name    == '') $name    = $_plugin_bugtrack['noname'];
253         if ($summary == '') $summary = $_plugin_bugtrack['nosummary'];
254
255          return <<<EOD
256 * $summary
257
258 - ${_plugin_bugtrack['base'    ]}: $base
259 - ${_plugin_bugtrack['name'    ]}: $name
260 - ${_plugin_bugtrack['priority']}: $priority
261 - ${_plugin_bugtrack['state'   ]}: $state
262 - ${_plugin_bugtrack['category']}: $category
263 - ${_plugin_bugtrack['date'    ]}: now?
264 - ${_plugin_bugtrack['version' ]}: $version
265
266 ** ${_plugin_bugtrack['body']}
267 $body
268 --------
269
270 #comment
271 EOD;
272 }
273
274 // ----------------------------------------
275 // BugTrack-List plugin
276
277 // #bugtrack_list plugin itself
278 function plugin_bugtrack_list_convert()
279 {
280         //global $script, $vars, $_plugin_bugtrack;
281         global $_plugin_bugtrack;
282
283         //$page = $vars['page'];
284         $page = WikiParam::getPage();
285         if (func_num_args()) {
286                 list($_page) = func_get_args();
287                 $_page = get_fullname(strip_bracket($_page), $page);
288                 if (is_pagename($_page)) $page = $_page;
289         }
290
291         $data = array();
292         $pattern = $page . '/';
293         $pattern_len = strlen($pattern);
294         foreach (get_existpages() as $page)
295                 if (strpos($page, $pattern) === 0 && is_numeric(substr($page, $pattern_len)))
296                         array_push($data, plugin_bugtrack_list_pageinfo($page));
297
298         $count_list = count($_plugin_bugtrack['state_list']);
299
300         $table = array();
301         for ($i = 0; $i <= $count_list + 1; ++$i) $table[$i] = array();
302
303         foreach ($data as $line) {
304                 list($page, $no, $summary, $name, $priority, $state, $category) = $line;
305                 foreach (array('summary', 'name', 'priority', 'state', 'category') as $item)
306                         $$item = htmlspecialchars($$item);
307                 $page_link = make_pagelink($page);
308
309                 $state_no = array_search($state, $_plugin_bugtrack['state_sort']);
310                 if ($state_no === NULL || $state_no === FALSE) $state_no = $count_list;
311                 $bgcolor = htmlspecialchars($_plugin_bugtrack['state_bgcolor'][$state_no]);
312
313                 $row = <<<EOD
314  <tr>
315   <td style="background-color:$bgcolor">$page_link</td>
316   <td style="background-color:$bgcolor">$state</td>
317   <td style="background-color:$bgcolor">$priority</td>
318   <td style="background-color:$bgcolor">$category</td>
319   <td style="background-color:$bgcolor">$name</td>
320   <td style="background-color:$bgcolor">$summary</td>
321  </tr>
322 EOD;
323                 $table[$state_no][$no] = $row;
324         }
325
326         $table_html = ' <tr>' . "\n";
327         $bgcolor = htmlspecialchars($_plugin_bugtrack['header_bgcolor']);
328         foreach (array('pagename', 'state', 'priority', 'category', 'name', 'summary') as $item)
329                 $table_html .= '  <th style="background-color:' . $bgcolor . '">' .
330                         htmlspecialchars($_plugin_bugtrack[$item]) . '</th>' . "\n";
331         $table_html .= ' </tr>' . "\n";
332
333         for ($i = 0; $i <= $count_list; ++$i) {
334                 ksort($table[$i], SORT_NUMERIC);
335                 $table_html .= join("\n", $table[$i]);
336         }
337
338         return '<table border="1" width="100%">' . "\n" .
339                 $table_html . "\n" .
340                 '</table>';
341 }
342
343 // Get one set of data from a page (or a page moved to $page)
344 function plugin_bugtrack_list_pageinfo($page, $no = NULL, $recurse = TRUE)
345 {
346         global $WikiName, $InterWikiName, $BracketName, $_plugin_bugtrack;
347
348         if ($no === NULL)
349                 $no = preg_match('/\/([0-9]+)$/', $page, $matches) ? $matches[1] : 0;
350
351         $source = get_source($page);
352
353         // Check 'moved' page _just once_
354         $regex  = "/move\s*to\s*($WikiName|$InterWikiName|\[\[$BracketName\]\])/";
355         $match  = array();
356         if ($recurse && preg_match($regex, $source[0], $match))
357                 return plugin_bugtrack_list_pageinfo(strip_bracket($match[1]), $no, FALSE);
358
359         $body = join("\n", $source);
360         foreach(array('summary', 'name', 'priority', 'state', 'category') as $item) {
361                 $regex = '/-\s*' . preg_quote($_plugin_bugtrack[$item], '/') . '\s*:(.*)/';
362                 if (preg_match($regex, $body, $matches)) {
363                         if ($item == 'name') {
364                                 $$item = strip_bracket(trim($matches[1]));
365                         } else {
366                                 $$item = trim($matches[1]);
367                         }
368                 } else {
369                                 $$item = ''; // Data not found
370                 }
371         }
372
373         if (preg_match("/\*([^\n]*)/", $body, $matches)) {
374                 $summary = $matches[1];
375                 make_heading($summary);
376         }
377
378         return array($page, $no, $summary, $name, $priority, $state, $category);
379 }
380 ?>