2004-06-05 00:31:23 +00:00
|
|
|
# Copyright (C) 2004 Alex Schroeder <alex@emacswiki.org>
|
|
|
|
|
#
|
|
|
|
|
# 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
|
2016-08-16 14:59:13 +02:00
|
|
|
# the Free Software Foundation; either version 3 of the License, or
|
2004-06-05 00:31:23 +00:00
|
|
|
# (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
|
2016-08-16 14:59:13 +02:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2004-06-05 00:31:23 +00:00
|
|
|
|
2015-03-29 18:45:14 +03:00
|
|
|
use strict;
|
2015-08-18 10:48:03 +02:00
|
|
|
use v5.10;
|
2015-03-27 03:01:01 +02:00
|
|
|
|
2014-08-21 22:23:23 +02:00
|
|
|
AddModuleDescription('not-found-handler.pl', '404 Handler Extension');
|
2004-06-05 00:31:23 +00:00
|
|
|
|
2015-04-10 13:31:28 +03:00
|
|
|
our ($q, $OpenPageName, %Page, %Action, $DataDir, $FreeLinkPattern, $PermanentAnchors);
|
2015-03-27 03:01:01 +02:00
|
|
|
|
2012-07-21 00:36:33 +02:00
|
|
|
use File::Glob ':glob';
|
2015-04-10 13:31:28 +03:00
|
|
|
our ($NotFoundHandlerDir, $LinkFile, %LinkDb, $LinkDbInit);
|
2004-06-05 00:31:23 +00:00
|
|
|
|
|
|
|
|
$NotFoundHandlerDir = '/tmp/oddmuse/cache';
|
2004-06-13 14:46:50 +00:00
|
|
|
$LinkFile = "$DataDir/linkdb";
|
|
|
|
|
$LinkDbInit = 0;
|
|
|
|
|
|
|
|
|
|
$Action{linkdb} = \&DoLinkDb;
|
|
|
|
|
$Action{clearcache} = \&DoClearCache;
|
|
|
|
|
|
|
|
|
|
sub DoClearCache {
|
|
|
|
|
print GetHeader('', QuoteHtml(T('Clearing Cache')), '');
|
2016-06-19 11:55:58 +02:00
|
|
|
Unlink(Glob("$NotFoundHandlerDir/*"));
|
2004-06-13 14:46:50 +00:00
|
|
|
print $q->p(T('Done.'));
|
|
|
|
|
PrintFooter();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# file handling
|
|
|
|
|
|
|
|
|
|
sub ReadLinkDb {
|
|
|
|
|
return if $LinkDbInit;
|
|
|
|
|
$LinkDbInit = 1;
|
2016-06-15 23:21:07 +02:00
|
|
|
return if not IsFile($LinkFile);
|
2004-06-13 14:46:50 +00:00
|
|
|
my $data = ReadFileOrDie($LinkFile);
|
|
|
|
|
map { my ($id, @links) = split; $LinkDb{$id} = \@links } split(/\n/, $data);
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-26 00:00:31 +00:00
|
|
|
sub WriteLinkDb { # call within the main lock!
|
2004-06-13 14:46:50 +00:00
|
|
|
my $str = join("\n", map { join(' ', $_, @{$LinkDb{$_}}) } keys %LinkDb);
|
|
|
|
|
WriteStringToFile($LinkFile, $str);
|
|
|
|
|
return $str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# create link database
|
|
|
|
|
|
|
|
|
|
sub DoLinkDb {
|
|
|
|
|
print GetHeader('', QuoteHtml(T('Generating Link Database')), '');
|
2004-06-26 00:00:31 +00:00
|
|
|
RequestLockOrError(); # fatal
|
2004-06-13 14:46:50 +00:00
|
|
|
%LinkDb = %{GetFullLinkList(1, 0, 0, 1)};
|
|
|
|
|
print $q->pre(WriteLinkDb());
|
2004-06-26 00:00:31 +00:00
|
|
|
ReleaseLock();
|
2004-06-13 14:46:50 +00:00
|
|
|
PrintFooter();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# refresh link database with data from the current open page
|
|
|
|
|
|
|
|
|
|
sub RefreshLinkDb {
|
2004-06-26 00:00:31 +00:00
|
|
|
if (not defined(&GetLinkList)) {
|
|
|
|
|
ReportError(T('The 404 handler extension requires the link data extension (links.pl).'));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if ($Page{revision} > 0 and not ($Page{blocks} && $Page{flags})) { #
|
|
|
|
|
# make sure we have a cache! We just discard this output, because
|
|
|
|
|
# in a multilingual setting we would need to determine the correct
|
|
|
|
|
# filename in which to store it in order to get headers
|
|
|
|
|
# etc. right.
|
2015-07-03 14:33:24 +02:00
|
|
|
ToString(sub { PrintWikiToHTML($Page{text}, 1, 0, 1) }); # revision 0, is already locked
|
2004-06-26 00:00:31 +00:00
|
|
|
}
|
|
|
|
|
my @links = GetLinkList(1, 0, 0, 1); # works on cached blocks...
|
2004-06-13 14:46:50 +00:00
|
|
|
ReadLinkDb();
|
2004-06-26 00:00:31 +00:00
|
|
|
if (@links) {
|
|
|
|
|
$LinkDb{$OpenPageName} = \@links;
|
|
|
|
|
} else {
|
|
|
|
|
delete $LinkDb{$OpenPageName};
|
|
|
|
|
}
|
2004-06-13 14:46:50 +00:00
|
|
|
WriteLinkDb();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# maintain link database
|
2004-06-05 00:31:23 +00:00
|
|
|
|
2015-04-11 23:41:33 +03:00
|
|
|
*OldNotFoundHandlerSave = \&Save;
|
|
|
|
|
*Save = \&NewNotFoundHandlerSave;
|
2004-06-05 00:31:23 +00:00
|
|
|
|
|
|
|
|
sub NewNotFoundHandlerSave {
|
|
|
|
|
my @args = @_;
|
|
|
|
|
my $id = $args[0];
|
|
|
|
|
OldNotFoundHandlerSave(@args);
|
2004-06-13 14:46:50 +00:00
|
|
|
RefreshLinkDb(); # for the open page
|
2016-06-15 23:21:07 +02:00
|
|
|
if (not IsDir($NotFoundHandlerDir)) {
|
|
|
|
|
CreateDir($NotFoundHandlerDir);
|
2004-06-26 00:00:31 +00:00
|
|
|
} elsif ($Page{revision} == 1) {
|
2004-06-13 14:46:50 +00:00
|
|
|
NotFoundHandlerCacheUpdate($id);
|
2004-06-05 00:31:23 +00:00
|
|
|
} else {
|
2004-06-13 14:46:50 +00:00
|
|
|
# unlink PageName, PageName.en, PageName.de, etc.
|
2016-06-19 11:55:58 +02:00
|
|
|
Unlink("$NotFoundHandlerDir/$id", Glob("$NotFoundHandlerDir/$id.[a-z][a-z]"));
|
2004-06-05 00:31:23 +00:00
|
|
|
}
|
|
|
|
|
}
|
2004-06-12 11:27:27 +00:00
|
|
|
|
2004-06-13 14:46:50 +00:00
|
|
|
sub NotFoundHandlerCacheUpdate {
|
|
|
|
|
my $id = shift;
|
|
|
|
|
# new or deleted page: regenerate all pages that link to this page,
|
|
|
|
|
# or to the permanent anchors defined on this page.
|
|
|
|
|
ReadLinkDb();
|
2004-06-26 00:00:31 +00:00
|
|
|
# we will check for the current page, and for all the anchors defined on it.
|
|
|
|
|
my @targets = ($id);
|
2004-06-13 14:46:50 +00:00
|
|
|
if ($PermanentAnchors) {
|
|
|
|
|
foreach ($Page{text} =~ m/\[::$FreeLinkPattern\]/g) {
|
2004-06-26 00:00:31 +00:00
|
|
|
push(@targets, $1); # harmless: potentially adds duplicates
|
2004-06-13 14:46:50 +00:00
|
|
|
}
|
|
|
|
|
}
|
2004-06-26 00:00:31 +00:00
|
|
|
# if any of the potential targets is the target of a link in the
|
|
|
|
|
# link database, then the source page must be rendered anew. in
|
|
|
|
|
# other words, delete the cached version of the source page.
|
|
|
|
|
my $target = '^(' . join('|', @targets) . ')$';
|
|
|
|
|
warn "Unlinking pages pointing to $target\n";
|
|
|
|
|
$target = qr($target);
|
|
|
|
|
foreach my $source (keys %LinkDb) {
|
|
|
|
|
warn "Examining $source\n";
|
|
|
|
|
if (grep(/$target/, @{$LinkDb{$source}})) {
|
2016-06-19 11:55:58 +02:00
|
|
|
Unlink("$NotFoundHandlerDir/$source", Glob("$NotFoundHandlerDir/$source.[a-z][a-z]"));
|
2004-06-26 00:00:31 +00:00
|
|
|
warn "Unlinking $source\n";
|
|
|
|
|
}
|
2004-06-13 14:46:50 +00:00
|
|
|
}
|
|
|
|
|
}
|
2004-06-12 11:27:27 +00:00
|
|
|
|
2015-04-11 23:41:33 +03:00
|
|
|
*OldNotFoundHandlerDeletePage = \&DeletePage;
|
|
|
|
|
*DeletePage = \&NewNotFoundHandlerDeletePage;
|
2004-06-12 11:40:06 +00:00
|
|
|
|
2004-06-13 14:46:50 +00:00
|
|
|
sub NewNotFoundHandlerDeletePage {
|
|
|
|
|
my $id = shift;
|
|
|
|
|
OpenPage($id); # Page{text} is required to find permanent anchors defined on this page
|
|
|
|
|
if (DeletePage($id) eq '') {
|
|
|
|
|
NotFoundHandlerCacheUpdate($id);
|
|
|
|
|
}
|
2004-06-12 11:27:27 +00:00
|
|
|
}
|