OSDN Git Service

BugTrack/2438 Fasten get_existpages() for ls2 plugin
authorumorigu <umorigu@gmail.com>
Sun, 22 Oct 2017 18:21:45 +0000 (03:21 +0900)
committerumorigu <umorigu@gmail.com>
Sun, 22 Oct 2017 18:21:45 +0000 (03:21 +0900)
Cache get_existpages() result (page list) of wiki/*.txt
Enable cache after calling read plugin (plugin_read_action())

lib/file.php
plugin/read.inc.php

index fbc9dde..ec82420 100644 (file)
@@ -685,12 +685,41 @@ function get_existfiles($dir = DATA_DIR, $ext = '.txt')
        return $aryret;
 }
 
+/**
+ * Get/Set pagelist cache enabled for get_existpages()
+ *
+ * @param $newvalue Set true when the system can cache the page list
+ * @return true if can use page list cache
+ */
+function is_pagelist_cache_enabled($newvalue = null)
+{
+       static $cache_enabled = null;
+
+       if (!is_null($newvalue)) {
+               $cache_enabled = $newvalue;
+               return; // Return nothing on setting newvalue call
+       }
+       if (is_null($cache_enabled)) {
+               return false;
+       }
+       return $cache_enabled;
+}
+
 // Get a page list of this wiki
 function get_existpages($dir = DATA_DIR, $ext = '.txt')
 {
+       static $cached_list = null; // Cached wikitext page list
+       $use_cache = false;
+
+       if ($dir === DATA_DIR && $ext === '.txt' && is_pagelist_cache_enabled()) {
+               // Use pagelist cache for "wiki/*.txt" files
+               if (!is_null($cached_list)) {
+                       return $cached_list;
+               }
+               $use_cache = true;
+       }
        $aryret = array();
        $pattern = '/^((?:[0-9A-F]{2})+)' . preg_quote($ext, '/') . '$/';
-
        $dp = @opendir($dir) or die_message($dir . ' is not found or not readable.');
        $matches = array();
        while (($file = readdir($dp)) !== FALSE) {
@@ -699,7 +728,9 @@ function get_existpages($dir = DATA_DIR, $ext = '.txt')
                }
        }
        closedir($dp);
-
+       if ($use_cache) {
+               $cached_list = $aryret;
+       }
        return $aryret;
 }
 
index 5614903..1e4ec71 100644 (file)
@@ -1,6 +1,8 @@
 <?php
 // PukiWiki - Yet another WikiWikiWeb clone.
-// $Id: read.inc.php,v 1.9 2011/01/25 15:01:01 henoheno Exp $
+// read.inc.php
+// Copyright 2003-2017 PukiWiki Development Team
+// License: GPL v2 or (at your option) any later version
 //
 // Read plugin: Show a page and InterWiki
 
@@ -9,22 +11,22 @@ function plugin_read_action()
        global $vars, $_title_invalidwn, $_msg_invalidiwn;
 
        $page = isset($vars['page']) ? $vars['page'] : '';
-
        if (is_page($page)) {
-               // ページを表示
+               // Show this page
                check_readable($page, true, true);
                header_lastmod($page);
+               is_pagelist_cache_enabled(true); // Enable get_existpage() cache
                return array('msg'=>'', 'body'=>'');
 
        } else if (! PKWK_SAFE_MODE && is_interwiki($page)) {
-               return do_plugin_action('interwiki'); // InterWikiNameを処理
+               return do_plugin_action('interwiki'); // Process InterWikiName
 
        } else if (is_pagename($page)) {
                $vars['cmd'] = 'edit';
-               return do_plugin_action('edit'); // 存在しないので、編集フォームを表示
+               return do_plugin_action('edit'); // Page not found, then show edit form
 
        } else {
-               // 無効なページ名
+               // Invalid page name
                return array(
                        'msg'=>$_title_invalidwn,
                        'body'=>str_replace('$1', htmlsc($page),
@@ -32,4 +34,3 @@ function plugin_read_action()
                );
        }
 }
-?>