not-found-handler.pl 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # Copyright (C) 2004 Alex Schroeder <alex@emacswiki.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 3 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. use strict;
  16. use v5.10;
  17. AddModuleDescription('not-found-handler.pl', '404 Handler Extension');
  18. our ($q, $OpenPageName, %Page, %Action, $DataDir, $FreeLinkPattern, $PermanentAnchors);
  19. use File::Glob ':glob';
  20. our ($NotFoundHandlerDir, $LinkFile, %LinkDb, $LinkDbInit);
  21. $NotFoundHandlerDir = '/tmp/oddmuse/cache';
  22. $LinkFile = "$DataDir/linkdb";
  23. $LinkDbInit = 0;
  24. $Action{linkdb} = \&DoLinkDb;
  25. $Action{clearcache} = \&DoClearCache;
  26. sub DoClearCache {
  27. print GetHeader('', QuoteHtml(T('Clearing Cache')), '');
  28. Unlink(Glob("$NotFoundHandlerDir/*"));
  29. print $q->p(T('Done.'));
  30. PrintFooter();
  31. }
  32. # file handling
  33. sub ReadLinkDb {
  34. return if $LinkDbInit;
  35. $LinkDbInit = 1;
  36. return if not IsFile($LinkFile);
  37. my $data = ReadFileOrDie($LinkFile);
  38. map { my ($id, @links) = split; $LinkDb{$id} = \@links } split(/\n/, $data);
  39. }
  40. sub WriteLinkDb { # call within the main lock!
  41. my $str = join("\n", map { join(' ', $_, @{$LinkDb{$_}}) } keys %LinkDb);
  42. WriteStringToFile($LinkFile, $str);
  43. return $str;
  44. }
  45. # create link database
  46. sub DoLinkDb {
  47. print GetHeader('', QuoteHtml(T('Generating Link Database')), '');
  48. RequestLockOrError(); # fatal
  49. %LinkDb = %{GetFullLinkList(1, 0, 0, 1)};
  50. print $q->pre(WriteLinkDb());
  51. ReleaseLock();
  52. PrintFooter();
  53. }
  54. # refresh link database with data from the current open page
  55. sub RefreshLinkDb {
  56. if (not defined(&GetLinkList)) {
  57. ReportError(T('The 404 handler extension requires the link data extension (links.pl).'));
  58. return;
  59. }
  60. if ($Page{revision} > 0 and not ($Page{blocks} && $Page{flags})) { #
  61. # make sure we have a cache! We just discard this output, because
  62. # in a multilingual setting we would need to determine the correct
  63. # filename in which to store it in order to get headers
  64. # etc. right.
  65. ToString(sub { PrintWikiToHTML($Page{text}, 1, 0, 1) }); # revision 0, is already locked
  66. }
  67. my @links = GetLinkList(1, 0, 0, 1); # works on cached blocks...
  68. ReadLinkDb();
  69. if (@links) {
  70. $LinkDb{$OpenPageName} = \@links;
  71. } else {
  72. delete $LinkDb{$OpenPageName};
  73. }
  74. WriteLinkDb();
  75. }
  76. # maintain link database
  77. *OldNotFoundHandlerSave = \&Save;
  78. *Save = \&NewNotFoundHandlerSave;
  79. sub NewNotFoundHandlerSave {
  80. my @args = @_;
  81. my $id = $args[0];
  82. OldNotFoundHandlerSave(@args);
  83. RefreshLinkDb(); # for the open page
  84. if (not IsDir($NotFoundHandlerDir)) {
  85. CreateDir($NotFoundHandlerDir);
  86. } elsif ($Page{revision} == 1) {
  87. NotFoundHandlerCacheUpdate($id);
  88. } else {
  89. # unlink PageName, PageName.en, PageName.de, etc.
  90. Unlink("$NotFoundHandlerDir/$id", Glob("$NotFoundHandlerDir/$id.[a-z][a-z]"));
  91. }
  92. }
  93. sub NotFoundHandlerCacheUpdate {
  94. my $id = shift;
  95. # new or deleted page: regenerate all pages that link to this page,
  96. # or to the permanent anchors defined on this page.
  97. ReadLinkDb();
  98. # we will check for the current page, and for all the anchors defined on it.
  99. my @targets = ($id);
  100. if ($PermanentAnchors) {
  101. foreach ($Page{text} =~ m/\[::$FreeLinkPattern\]/g) {
  102. push(@targets, $1); # harmless: potentially adds duplicates
  103. }
  104. }
  105. # if any of the potential targets is the target of a link in the
  106. # link database, then the source page must be rendered anew. in
  107. # other words, delete the cached version of the source page.
  108. my $target = '^(' . join('|', @targets) . ')$';
  109. warn "Unlinking pages pointing to $target\n";
  110. $target = qr($target);
  111. foreach my $source (keys %LinkDb) {
  112. warn "Examining $source\n";
  113. if (grep(/$target/, @{$LinkDb{$source}})) {
  114. Unlink("$NotFoundHandlerDir/$source", Glob("$NotFoundHandlerDir/$source.[a-z][a-z]"));
  115. warn "Unlinking $source\n";
  116. }
  117. }
  118. }
  119. *OldNotFoundHandlerDeletePage = \&DeletePage;
  120. *DeletePage = \&NewNotFoundHandlerDeletePage;
  121. sub NewNotFoundHandlerDeletePage {
  122. my $id = shift;
  123. OpenPage($id); # Page{text} is required to find permanent anchors defined on this page
  124. if (DeletePage($id) eq '') {
  125. NotFoundHandlerCacheUpdate($id);
  126. }
  127. }