#!/usr/bin/perl # TmNetWiki version 0.4 (September 11, 2002) # Copyright (C) 2002 Adam Brate # # Based on the GPLed UseModWiki 0.92 # Copyright (C) 2000-2001 Clifford A. Adams # or # Based on the GPLed AtisWiki 0.3 (C) 1998 Markus Denker # # ...which was based on # the LGPLed CVWiki CVS-patches (C) 1997 Peter Merel # and The Original WikiWikiWeb (C) Ward Cunningham # (code reused with permission) # Email and ThinLine options by Jim Mahoney # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # 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. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the # Free Software Foundation, Inc. # 59 Temple Place, Suite 330 # Boston, MA 02111-1307 USA package Wiki::cache; use strict; # needs subs Wiki::page: GetPageDirectory, CreatePageDir # Wiki::file: ReadFile, WriteStringToFile # Wiki::lock: RequestLockDir, ReleaseLockDir # needs consts $UseCache, $HomePage, $ENV{QUERY_STRING}, $ENV{REQUEST_METHOD}, # $LinkPattern, $FreeLinks, $FreeLinkPattern, $HtmlDir use base 'Exporter'; use Wiki::consts; use Wiki::page; use Wiki::file; use Wiki::lock; #if ($^V and $^V gt v5.6.0) { # our @EXPORT; #else { use vars qw(@EXPORT); #} @EXPORT=qw( DoCacheBrowse NewPageCacheClear UpdateHtmlCache ); # local routines sub UnlinkHtmlCache; sub GetHtmlCacheFile; sub RequestCacheLock; sub ReleaseCacheLock; # Simple HTML cache sub DoCacheBrowse { my ($query, $idFile, $text, $success); return 0 if (!$UseCache); $query = $ENV{QUERY_STRING}; if (($query eq "") && ($ENV{REQUEST_METHOD} eq "GET")) { $query = $HomePage; # Allow caching of home page. } if (!($query =~ /^$LinkPattern$/)) { if (!($FreeLinks && ($query =~ /^$FreeLinkPattern$/))) { return 0; # Only use cache for simple links } } $idFile = &GetHtmlCacheFile($query); if (-f $idFile) { ($success, $text) = ReadFile($idFile); print $text if $success; return $success; } return 0; } sub UpdateHtmlCache { my ($id, $html) = @_; my $idFile; $idFile = &GetHtmlCacheFile($id); &CreatePageDir($HtmlDir, $id); if (&RequestCacheLock()) { &WriteStringToFile($idFile, $html); &ReleaseCacheLock(); } } sub UnlinkHtmlCache { my ($id) = @_; my $idFile; $idFile = &GetHtmlCacheFile($id); if (-f $idFile) { unlink($idFile); } } sub NewPageCacheClear { my ($id) = @_; my $name; return if (!$UseCache); $id =~ s|.+/|/|; # If subpage, search for just the subpage # The following code used to search the body for the $id foreach $name (&AllPagesList()) { # Remove all to be safe &UnlinkHtmlCache($name); } } sub GetHtmlCacheFile { my ($id) = @_; return $HtmlDir . "/" . &GetPageDirectory($id) . "/$id.htm"; } sub RequestCacheLock { # 4 tries, 2 second wait, do not die on error return &RequestLockDir('cache', 4, 2, 0); } sub ReleaseCacheLock { &ReleaseLockDir('cache'); } 1;