Revision | 5ceaa0f8894e837864befeab2883a6f1e15b867c (tree) |
---|---|
Time | 2016-12-02 08:19:33 |
Author | umorigu <umorigu@gmai...> |
Commiter | umorigu |
BugTrack/2401 Cache bugtrack_list
@@ -1,8 +1,8 @@ | ||
1 | 1 | <?php |
2 | 2 | // 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 | |
6 | 6 | // 2002 Y.MASUI GPL2 http://masui.net/pukiwiki/ masui@masui.net |
7 | 7 | // |
8 | 8 | // BugTrack plugin |
@@ -250,41 +250,120 @@ EOD; | ||
250 | 250 | // ---------------------------------------- |
251 | 251 | // BugTrack-List plugin |
252 | 252 | |
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 | + | |
253 | 269 | // #bugtrack_list plugin itself |
254 | 270 | function plugin_bugtrack_list_convert() |
255 | 271 | { |
256 | 272 | 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 | + } | |
258 | 280 | $page = $vars['page']; |
259 | 281 | if (func_num_args()) { |
260 | 282 | list($_page) = func_get_args(); |
261 | 283 | $_page = get_fullname(strip_bracket($_page), $page); |
262 | 284 | if (is_pagename($_page)) $page = $_page; |
263 | 285 | } |
264 | - | |
265 | 286 | $data = array(); |
287 | + $page_list = array(); | |
266 | 288 | $pattern = $page . '/'; |
267 | 289 | $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'); | |
272 | 296 | $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 | + } | |
273 | 335 | |
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); | |
282 | 361 | |
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]); | |
286 | 365 | |
287 | - $row = <<<EOD | |
366 | + $row = <<<EOD | |
288 | 367 | <tr> |
289 | 368 | <td style="background-color:$bgcolor">$page_link</td> |
290 | 369 | <td style="background-color:$bgcolor">$state</td> |
@@ -294,28 +373,45 @@ function plugin_bugtrack_list_convert() | ||
294 | 373 | <td style="background-color:$bgcolor">$summary</td> |
295 | 374 | </tr> |
296 | 375 | 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; | |
298 | 397 | } |
299 | - | |
300 | 398 | $table_html = ' <tr>' . "\n"; |
301 | 399 | $bgcolor = htmlsc($_plugin_bugtrack['header_bgcolor']); |
302 | 400 | foreach (array('pagename', 'state', 'priority', 'category', 'name', 'summary') as $item) |
303 | 401 | $table_html .= ' <th style="background-color:' . $bgcolor . '">' . |
304 | 402 | htmlsc($_plugin_bugtrack[$item]) . '</th>' . "\n"; |
305 | 403 | $table_html .= ' </tr>' . "\n"; |
306 | - | |
307 | 404 | for ($i = 0; $i <= $count_list; ++$i) { |
308 | 405 | ksort($table[$i], SORT_NUMERIC); |
309 | 406 | $table_html .= join("\n", $table[$i]); |
310 | 407 | } |
311 | - | |
312 | 408 | return '<table border="1" width="100%">' . "\n" . |
313 | 409 | $table_html . "\n" . |
314 | 410 | '</table>'; |
315 | 411 | } |
316 | 412 | |
317 | 413 | // 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) | |
319 | 415 | { |
320 | 416 | global $WikiName, $InterWikiName, $BracketName, $_plugin_bugtrack; |
321 | 417 |
@@ -328,7 +424,7 @@ function plugin_bugtrack_list_pageinfo($page, $no = NULL, $recurse = TRUE) | ||
328 | 424 | $regex = "/move\s*to\s*($WikiName|$InterWikiName|\[\[$BracketName\]\])/"; |
329 | 425 | $match = array(); |
330 | 426 | 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); | |
332 | 428 | |
333 | 429 | $body = join("\n", $source); |
334 | 430 | foreach(array('summary', 'name', 'priority', 'state', 'category') as $item) { |
@@ -349,6 +445,5 @@ function plugin_bugtrack_list_pageinfo($page, $no = NULL, $recurse = TRUE) | ||
349 | 445 | make_heading($summary); |
350 | 446 | } |
351 | 447 | |
352 | - return array($page, $no, $summary, $name, $priority, $state, $category); | |
448 | + return array($page, $no, $summary, $name, $priority, $state, $category, $filetime); | |
353 | 449 | } |
354 | - |
@@ -1,11 +1,11 @@ | ||
1 | 1 | <?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 | |
6 | 4 | // 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 | |
9 | 9 | |
10 | 10 | require_once(PLUGIN_DIR . 'bugtrack.inc.php'); |
11 | 11 |
@@ -13,4 +13,3 @@ function plugin_bugtrack_list_init() | ||
13 | 13 | { |
14 | 14 | plugin_bugtrack_init(); |
15 | 15 | } |
16 | -?> |