From 986d175fbe48c8e7f0d07d5d6eee1d9356b28705 Mon Sep 17 00:00:00 2001 From: Oleg Sh Date: Sun, 19 Dec 2021 19:51:12 +0200 Subject: [PATCH] Add html cache for ru wiki --- wiki/.htaccess | 63 ++++++++++++-- wiki/cookbook/fastcache.php | 159 ++++++++++++++++++++++++++++++++++ wiki/local/config.php | 1 + wiki/pub/htmlcache/.gitignore | 1 + 4 files changed, 216 insertions(+), 8 deletions(-) create mode 100644 wiki/cookbook/fastcache.php create mode 100644 wiki/pub/htmlcache/.gitignore diff --git a/wiki/.htaccess b/wiki/.htaccess index 26d17e1..dadfab4 100755 --- a/wiki/.htaccess +++ b/wiki/.htaccess @@ -1,15 +1,62 @@ -# Use mod_rewrite to enable "Clean URLs" for a PmWiki installation. RewriteEngine On -# Define the rewrite base. +# Define the rewrite base. This is the URL of the directory that the +# .htaccess file (if using) resides in RewriteBase /wiki +# Catch requests for index.anything +#RewriteRule ^index / [R=301] +# Send requests without parameters to pmwiki.php. +RewriteRule ^$ pmwiki.php [L] +# Send requests for index.php to pmwiki.php. +RewriteRule ^index\.php$ pmwiki.php [L] + +# Non-capital first letter means that it's not a wiki page +RewriteRule ^[a-z].*$ - [L] # Auto redirect to https. RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] -# Send requests without parameters to pmwiki.php. -RewriteRule ^$ pmwiki.php [L] -# Send requests for index.php to pmwiki.php. -RewriteRule ^index\.php$ pmwiki.php [L] -# Send requests to pmwiki.php, appending the query string part. -RewriteRule ^([A-Z0-9\xa0-\xff].*)$ pmwiki.php?n=$1 [QSA,L] \ No newline at end of file + +## Define the rules that exclude things from the cache: +# If any of these conditions are true ... + +# Posting a form request +RewriteCond %{REQUEST_METHOD} POST [OR] +# A session exists -> the user is signed in +RewriteCond %{HTTP_COOKIE} PHPSESSID [OR] +# A query string exists; ie. we're not just viewing a page +RewriteCond %{QUERY_STRING} . + +# ... serve the file using pmwiki.php +RewriteRule ^(.*)$ pmwiki.php?n=$1 [QSA,L] + + +## If we're this far in the script it's ok to serve files from the cache + +# Root means that we want the wiki homepage +RewriteCond %{REQUEST_URI} ^/$ +# Does the file exist? +# Change 'Main.HomePage' here if you're using a different page +RewriteCond /wiki/htmlcache/Main.HomePage.html -f +# If so, serve it. +# Change 'Main.HomePage' here if you're using a different page +RewriteRule ^$ htmlcache/Main.HomePage.html [L] + +# No . or / in the URL means that we want the group main page +RewriteCond %{REQUEST_URI} ^/([^./?]+)[./]?$ +# Does the file exist? +# Change '%1.%1' here if you're using a different group main page format +RewriteCond /wiki/htmlcache/%1.%1.html -f +# If so, serve it. +RewriteRule ^. htmlcache/%1.%1.html [L] + +# We want Group.Page +RewriteCond %{REQUEST_URI} ^/([^./]+)[./]([^./]+)/?$ +# Does the file exist? +RewriteCond /wiki/htmlcache/%1.%2.html -f +# If so, serve it. +RewriteRule ^. htmlcache/%1.%2.html [L] + + +# Cache misses and anything that doesn't fit the above goes to pmwiki.php +RewriteRule ^(.*)$ pmwiki.php?n=$1 [QSA,L] \ No newline at end of file diff --git a/wiki/cookbook/fastcache.php b/wiki/cookbook/fastcache.php new file mode 100644 index 0000000..70b95bc --- /dev/null +++ b/wiki/cookbook/fastcache.php @@ -0,0 +1,159 @@ + + * + * Caches complete wiki pages for very fast retrieval. + * + * Developed and tested using the PmWiki 2.2.0 series. + * + * To install, add the following line to your local/config.php file : + include_once("$FarmD/cookbook/fastcache.php"); + * + * For more information, please see the online documentation at + * http://www.pmwiki.org/wiki/Cookbook/FastCache + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License, Version 2, as + * published by the Free Software Foundation. + * http://www.gnu.org/copyleft/gpl.html + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * Updated for PHP5.5-7.4 by Petko Yotov pmwiki.org/petko + */ + +$RecipeInfo['FastCache']['Version'] = '2020-10-17'; + +Markup( 'nofastcache', 'directives', '/\\(:nofastcache:\\)/i', "NoFastCache" ); +function NoFastCache(){ + $GLOBALS['FastCacheValid'] = FALSE; + return ""; +} + +SDV( $FastCacheDir, "$FarmD/htmlcache" ); +SDV( $FastCacheValid, ( + empty($_POST) && + ( empty($_GET) || ( array_keys($_GET) == array('n') ) ) && + ( empty($_SESSION) || ( $_SESSION == array( 'authid' => '' ) ) ) && + ( empty( $_REQUEST[ ini_get('session.name') ] ) ) # suggested by MagicBeanDip + && ( empty( $_COOKIE[ ini_get('session.name') ] ) ) # Petko: PHP5.3+ cookies are not in $_REQUEST +) ); + +if ( !IsEnabled( $EnableFastCache, TRUE ) || !$FastCacheDir ) return; + +if ($FastCacheValid) $HandleActions['browse'] = 'HandleFastCacheBrowse'; +$EditFunctions[] = 'FastCacheUpdate'; + + +function FastCacheUpdate($pagename, &$page, &$new) { + global $IsPagePosted; + global $FastCacheDir, $FastCacheInvalidateAllOnUpdate; + if ( !$IsPagePosted || !$FastCacheDir ) return; + if ( IsEnabled( $FastCacheInvalidateAllOnUpdate, TRUE ) ) { + if ( is_dir($FastCacheDir) && ( $fd = opendir($FastCacheDir) ) ) { + while ( ( $fh = readdir($fd) ) !== FALSE ) { + if ( $fh[0] == '.' ) continue; + if ( !unlink("$FastCacheDir/$fh") ) { + StopWatch("FastCacheUpdate: error unlinking file $FastCacheDir/$fh > stopping"); + return; + } + } + } else { + StopWatch("FastCacheUpdate: error opening directory $FastCacheDir"); + return; + } + } else { + SDV( $FastCacheSuffix, '.html' ); + $fcfile = "$FastCacheDir/$pagename$FastCacheSuffix"; + if ( is_file($fcfile) ) + if ( !unlink($fcfile) ) + StopWatch("FastCacheUpdate: error unlinking file $fcfile"); + } +} + + +## based on HandleBrowse in pmwiki.php +function HandleFastCacheBrowse($pagename, $auth = 'read') { + # handle display of a page + global $DefaultPageTextFmt, $PageNotFoundHeaderFmt, $HTTPHeaders, + $EnableHTMLCache, $NoHTMLCache, $PageCacheFile, $LastModTime, $IsHTMLCached, + $FmtV, $HandleBrowseFmt, $PageStartFmt, $PageEndFmt, $PageRedirectFmt; + ## begin added + global $FastCachePage, $FastCacheDir, $FastCacheValid, $FastCacheSuffix; + if ( !$FastCacheValid || !$FastCacheDir ) { HandleBrowse($pagename, $auth); return; } + SDV( $FastCacheSuffix, '.html' ); + $fcfile = "$FastCacheDir/$pagename$FastCacheSuffix"; + if ( @filemtime($fcfile) > $LastModTime ) { + if ( $FastCachePage = file_get_contents($fcfile) ) { + StopWatch("HandleFastCacheBrowse: using FastCached copy of $pagename"); + echo $FastCachePage; + } else { + $FastCacheValid = FALSE; + StopWatch("HandleFastCacheBrowse: read error on $fcfile"); + HandleBrowse($pagename, $auth); + } + return; + } + ## end added + $page = RetrieveAuthPage($pagename, $auth, true, READPAGE_CURRENT); + if (!$page) Abort("?cannot read $pagename"); + PCache($pagename,$page); + if (PageExists($pagename)) $text = @$page['text']; + else { + $FastCacheValid = FALSE; ## added + SDV($DefaultPageTextFmt,'(:include $[{$SiteGroup}.PageNotFound]:)'); + $text = FmtPageName($DefaultPageTextFmt, $pagename); + SDV($PageNotFoundHeaderFmt, 'HTTP/1.1 404 Not Found'); + SDV($HTTPHeaders['status'], $PageNotFoundHeaderFmt); + } + $opt = array(); + SDV($PageRedirectFmt,"

($[redirected from] {\$FullName})

\$HTMLVSpace\n"); + if (@!$_GET['from']) { $opt['redirect'] = 1; $PageRedirectFmt = ''; } + else $PageRedirectFmt = FmtPageName($PageRedirectFmt, $_GET['from']); + if (@$EnableHTMLCache && !$NoHTMLCache && $PageCacheFile && + @filemtime($PageCacheFile) > $LastModTime) { + list($ctext) = unserialize(file_get_contents($PageCacheFile)); + $FmtV['$PageText'] = "$ctext"; + $IsHTMLCached = 1; + StopWatch("HandleFastCacheBrowse: using HTMLCached copy"); ## modified + } else { + $IsHTMLCached = 0; + $text = '(:groupheader:)'.@$text.'(:groupfooter:)'; + $t1 = time(); + $FmtV['$PageText'] = MarkupToHTML($pagename, $text, $opt); + if (@$EnableHTMLCache > 0 && !$NoHTMLCache && $PageCacheFile + && (time() - $t1 + 1) >= $EnableHTMLCache) { + $fp = @fopen("$PageCacheFile,new", "x"); + if ($fp) { + StopWatch("HandleFastCacheBrowse: HTMLCaching page"); ## modified + fwrite($fp, serialize(array($FmtV['$PageText']))); fclose($fp); + rename("$PageCacheFile,new", $PageCacheFile); + } + } + } + SDV($HandleBrowseFmt,array(&$PageStartFmt, &$PageRedirectFmt, '$PageText', + &$PageEndFmt)); + ## begin modified + if ($FastCacheValid) { + ob_start(); + PrintFmt($pagename,$HandleBrowseFmt); + $FastCachePage = ob_get_contents(); + ob_end_flush(); + mkdirp(dirname($fcfile)); + if ( $FastCacheValid && ( $fc = fopen( "$fcfile,new", 'x' ) ) ) { + StopWatch( "HandleFastCacheBrowse: FastCaching $pagename" ); + fwrite( $fc, $FastCachePage ); + fclose($fc); + rename( "$fcfile,new", $fcfile ); + } else { + StopWatch( "HandleFastCacheBrowse: error writing cache to $fcfile,new" ); + } + } else { + PrintFmt($pagename,$HandleBrowseFmt); + } + ## end modified +} diff --git a/wiki/local/config.php b/wiki/local/config.php index c0e0a75..ed681b4 100755 --- a/wiki/local/config.php +++ b/wiki/local/config.php @@ -4,6 +4,7 @@ ## Also, be sure to take a look at http://www.pmwiki.org/wiki/Cookbook ## for more details on the types of customizations that can be added ## to PmWiki. +include_once("cookbook/fastcache.php"); ## $WikiTitle is the name that appears in the browser's title bar. $WikiTitle = 'Вики справка Graph Online'; diff --git a/wiki/pub/htmlcache/.gitignore b/wiki/pub/htmlcache/.gitignore new file mode 100644 index 0000000..2d19fc7 --- /dev/null +++ b/wiki/pub/htmlcache/.gitignore @@ -0,0 +1 @@ +*.html