コミットメタ情報

リビジョン5ceaa0f8894e837864befeab2883a6f1e15b867c (tree)
日時2016-12-02 08:19:33
作者umorigu <umorigu@gmai...>
コミッターumorigu

ログメッセージ

BugTrack/2401 Cache bugtrack_list

変更サマリ

差分

--- a/plugin/bugtrack.inc.php
+++ b/plugin/bugtrack.inc.php
@@ -1,8 +1,8 @@
11 <?php
22 // PukiWiki - Yet another WikiWikiWeb clone.
3-// $Id: bugtrack.inc.php,v 1.27 2011/01/25 15:01:01 henoheno Exp $
4-// Copyright (C)
5-// 2002-2005, 2007 PukiWiki Developers Team
3+// bugtrack.inc.php
4+// Copyright
5+// 2002-2016 PukiWiki Development Team
66 // 2002 Y.MASUI GPL2 http://masui.net/pukiwiki/ masui@masui.net
77 //
88 // BugTrack plugin
@@ -250,41 +250,120 @@ EOD;
250250 // ----------------------------------------
251251 // BugTrack-List plugin
252252
253+function _plugin_bugtrack_list_paganame_compare($a, $b)
254+{
255+ return strnatcmp($a['name'], $b['name']);
256+}
257+
258+function __pkwk_ctype_digit($s) {
259+ static $ctype_digits_exists;
260+ if (!isset($ctype_digits_exists)) {
261+ $ctype_digits_exists = function_exists('ctype_digit');
262+ }
263+ if ($ctype_digits_exists) {
264+ return ctype_digit($s);
265+ }
266+ return preg_match('/^[0-9]+$/', $s) ? true : false;
267+}
268+
253269 // #bugtrack_list plugin itself
254270 function plugin_bugtrack_list_convert()
255271 {
256272 global $script, $vars, $_plugin_bugtrack;
257-
273+ $cache_format_version = 1;
274+ $cache_expire_time = 60 * 60 * 24;
275+ $cache_refresh_time_prev;
276+ static $cache_enabled;
277+ if (!isset($cache_enabled)) {
278+ $cache_enabled = defined('JSON_UNESCAPED_UNICODE'); // PHP 5.4+
279+ }
258280 $page = $vars['page'];
259281 if (func_num_args()) {
260282 list($_page) = func_get_args();
261283 $_page = get_fullname(strip_bracket($_page), $page);
262284 if (is_pagename($_page)) $page = $_page;
263285 }
264-
265286 $data = array();
287+ $page_list = array();
266288 $pattern = $page . '/';
267289 $pattern_len = strlen($pattern);
268- foreach (get_existpages() as $page)
269- if (strpos($page, $pattern) === 0 && is_numeric(substr($page, $pattern_len)))
270- array_push($data, plugin_bugtrack_list_pageinfo($page));
271-
290+ foreach (get_existpages() as $p) {
291+ if (strncmp($p, $pattern, $pattern_len) === 0 && __pkwk_ctype_digit(substr($p, $pattern_len))) {
292+ $page_list[] = array('name'=>$p,'filetime'=>get_filetime($p));
293+ }
294+ }
295+ usort($page_list, '_plugin_bugtrack_list_paganame_compare');
272296 $count_list = count($_plugin_bugtrack['state_list']);
297+ $data_map = array();
298+ if ($cache_enabled) {
299+ // Cache management
300+ $data_updated = true;
301+ $cache_filepath = CACHE_DIR . encode($page) . '.bugtrack';
302+ $json_cached = file_get_contents($cache_filepath);
303+ if ($json_cached) {
304+ $wrapdata = json_decode($json_cached);
305+ if (is_object($wrapdata)) {
306+ if (isset($wrapdata->version, $wrapdata->pages, $wrapdata->refreshed_at)) {
307+ $cache_time_prev = $wrapdata->refreshed_at;
308+ if ($cache_format_version == $wrapdata->version
309+ && time() < $cache_time_prev + $cache_expire_time) {
310+ $data = $wrapdata->pages;
311+ $cache_refresh_time_prev = $cache_time_prev;
312+ }
313+ }
314+ }
315+ if (is_array($data) && !empty($data)) {
316+ $all_ok = true;
317+ foreach ($page_list as $i=>$page_info) {
318+ list($page_name, $no, $summary, $name, $priority, $state, $category, $filetime) = $data[$i];
319+ if ($filetime !== $page_info['filetime'] || $page_name !== $page_info['name']) {
320+ $all_ok = false;
321+ break;
322+ }
323+ }
324+ if (!$all_ok) {
325+ // Clear cache
326+ foreach ($data as $d) {
327+ $page_name = $d[0];
328+ $data_map[$page_name] = $d;
329+ }
330+ $data = array();
331+ }
332+ }
333+ }
334+ }
273335
274- $table = array();
275- for ($i = 0; $i <= $count_list + 1; ++$i) $table[$i] = array();
276-
277- foreach ($data as $line) {
278- list($page, $no, $summary, $name, $priority, $state, $category) = $line;
279- foreach (array('summary', 'name', 'priority', 'state', 'category') as $item)
280- $$item = htmlsc($$item);
281- $page_link = make_pagelink($page);
336+ if (empty($data) || !empty($data_map)) {
337+ // No cache
338+ foreach ($page_list as $page_info) {
339+ $page_name = $page_info['name'];
340+ $filled = false;
341+ if (isset($data_map[$page_name])) {
342+ $d = $data_map[$page_name];
343+ list($page_name, $no, $summary, $name, $priority, $state, $category, $filetime) = $d;
344+ if ($filetime == $page_info['filetime']) {
345+ $data[] = $d;
346+ $filled = true;
347+ }
348+ }
349+ if (!$filled) {
350+ $data[] = plugin_bugtrack_list_pageinfo($page_info['name'], null, true, $page_info['filetime']);
351+ }
352+ }
353+ foreach ($data as $i=>$line) {
354+ list($page_name, $no, $summary, $name, $priority, $state, $category, $filetime, $state_no_cached, $html_cached) = $line;
355+ if (isset($state_no_cached) && isset($html_cached)) {
356+ continue;
357+ }
358+ foreach (array('summary', 'name', 'priority', 'state', 'category') as $item)
359+ $$item = htmlsc($$item);
360+ $page_link = make_pagelink($page_name);
282361
283- $state_no = array_search($state, $_plugin_bugtrack['state_sort']);
284- if ($state_no === NULL || $state_no === FALSE) $state_no = $count_list;
285- $bgcolor = htmlsc($_plugin_bugtrack['state_bgcolor'][$state_no]);
362+ $state_no = array_search($state, $_plugin_bugtrack['state_sort']);
363+ if ($state_no === NULL || $state_no === FALSE) $state_no = $count_list;
364+ $bgcolor = htmlsc($_plugin_bugtrack['state_bgcolor'][$state_no]);
286365
287- $row = <<<EOD
366+ $row = <<<EOD
288367 <tr>
289368 <td style="background-color:$bgcolor">$page_link</td>
290369 <td style="background-color:$bgcolor">$state</td>
@@ -294,28 +373,45 @@ function plugin_bugtrack_list_convert()
294373 <td style="background-color:$bgcolor">$summary</td>
295374 </tr>
296375 EOD;
297- $table[$state_no][$no] = $row;
376+ $rec = &$data[$i];
377+ $rec[] = $state_no; // color number
378+ $rec[] = $row; // HTML content
379+ }
380+ if ($cache_enabled) {
381+ // Save cache
382+ if (isset($cache_refresh_time_prev)) {
383+ $refreshed_at = $cache_refresh_time_prev;
384+ } else {
385+ $refreshed_at = time();
386+ }
387+ $json = array('refreshed_at'=>$refreshed_at, 'pages'=>$data, 'version'=>$cache_format_version);
388+ $cache_body = json_encode($json, JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES);
389+ file_put_contents($cache_filepath, $cache_body);
390+ }
391+ }
392+ $table = array();
393+ for ($i = 0; $i <= $count_list + 1; ++$i) $table[$i] = array();
394+ foreach ($data as $line) {
395+ list($page_name, $no, $summary, $name, $priority, $state, $category, $filetime, $state_no, $html) = $line;
396+ $table[$state_no][$no] = $html;
298397 }
299-
300398 $table_html = ' <tr>' . "\n";
301399 $bgcolor = htmlsc($_plugin_bugtrack['header_bgcolor']);
302400 foreach (array('pagename', 'state', 'priority', 'category', 'name', 'summary') as $item)
303401 $table_html .= ' <th style="background-color:' . $bgcolor . '">' .
304402 htmlsc($_plugin_bugtrack[$item]) . '</th>' . "\n";
305403 $table_html .= ' </tr>' . "\n";
306-
307404 for ($i = 0; $i <= $count_list; ++$i) {
308405 ksort($table[$i], SORT_NUMERIC);
309406 $table_html .= join("\n", $table[$i]);
310407 }
311-
312408 return '<table border="1" width="100%">' . "\n" .
313409 $table_html . "\n" .
314410 '</table>';
315411 }
316412
317413 // Get one set of data from a page (or a page moved to $page)
318-function plugin_bugtrack_list_pageinfo($page, $no = NULL, $recurse = TRUE)
414+function plugin_bugtrack_list_pageinfo($page, $no = NULL, $recurse = TRUE, $filetime = NULL)
319415 {
320416 global $WikiName, $InterWikiName, $BracketName, $_plugin_bugtrack;
321417
@@ -328,7 +424,7 @@ function plugin_bugtrack_list_pageinfo($page, $no = NULL, $recurse = TRUE)
328424 $regex = "/move\s*to\s*($WikiName|$InterWikiName|\[\[$BracketName\]\])/";
329425 $match = array();
330426 if ($recurse && preg_match($regex, $source[0], $match))
331- return plugin_bugtrack_list_pageinfo(strip_bracket($match[1]), $no, FALSE);
427+ return plugin_bugtrack_list_pageinfo(strip_bracket($match[1]), $no, FALSE, $filetime);
332428
333429 $body = join("\n", $source);
334430 foreach(array('summary', 'name', 'priority', 'state', 'category') as $item) {
@@ -349,6 +445,5 @@ function plugin_bugtrack_list_pageinfo($page, $no = NULL, $recurse = TRUE)
349445 make_heading($summary);
350446 }
351447
352- return array($page, $no, $summary, $name, $priority, $state, $category);
448+ return array($page, $no, $summary, $name, $priority, $state, $category, $filetime);
353449 }
354-
--- a/plugin/bugtrack_list.inc.php
+++ b/plugin/bugtrack_list.inc.php
@@ -1,11 +1,11 @@
11 <?php
2-// $Id: bugtrack_list.inc.php,v 1.6 2005/04/03 02:51:07 henoheno Exp $
3-//
4-// PukiWiki BugTrack-list plugin - A part of BugTrack plugin
5-//
2+// PukiWiki - Yet another WikiWikiWeb clone.
3+// bugtrack_list.inc.php
64 // Copyright
7-// 2002-2005 PukiWiki Developers Team
8-// 2002 Y.MASUI GPL2 http://masui.net/pukiwiki/ masui@masui.net
5+// 2002-2016 PukiWiki Development Team
6+// 2002 Y.MASUI GPL2 http://masui.net/pukiwiki/ masui@masui.net
7+//
8+// BugTrack List plugin
99
1010 require_once(PLUGIN_DIR . 'bugtrack.inc.php');
1111
@@ -13,4 +13,3 @@ function plugin_bugtrack_list_init()
1313 {
1414 plugin_bugtrack_init();
1515 }
16-?>
旧リポジトリブラウザで表示