not-found-handler.pl 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 2 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, write to the
  15. # Free Software Foundation, Inc.
  16. # 59 Temple Place, Suite 330
  17. # Boston, MA 02111-1307 USA
  18. use strict;
  19. use v5.10;
  20. AddModuleDescription('not-found-handler.pl', '404 Handler Extension');
  21. our ($q, $OpenPageName, %Page, %Action, $DataDir, $FreeLinkPattern, $PermanentAnchors);
  22. use File::Glob ':glob';
  23. our ($NotFoundHandlerDir, $LinkFile, %LinkDb, $LinkDbInit);
  24. $NotFoundHandlerDir = '/tmp/oddmuse/cache';
  25. $LinkFile = "$DataDir/linkdb";
  26. $LinkDbInit = 0;
  27. $Action{linkdb} = \&DoLinkDb;
  28. $Action{clearcache} = \&DoClearCache;
  29. sub DoClearCache {
  30. print GetHeader('', QuoteHtml(T('Clearing Cache')), '');
  31. Unlink(Glob("$NotFoundHandlerDir/*"));
  32. print $q->p(T('Done.'));
  33. PrintFooter();
  34. }
  35. # file handling
  36. sub ReadLinkDb {
  37. return if $LinkDbInit;
  38. $LinkDbInit = 1;
  39. return if not IsFile($LinkFile);
  40. my $data = ReadFileOrDie($LinkFile);
  41. map { my ($id, @links) = split; $LinkDb{$id} = \@links } split(/\n/, $data);
  42. }
  43. sub WriteLinkDb { # call within the main lock!
  44. my $str = join("\n", map { join(' ', $_, @{$LinkDb{$_}}) } keys %LinkDb);
  45. WriteStringToFile($LinkFile, $str);
  46. return $str;
  47. }
  48. # create link database
  49. sub DoLinkDb {
  50. print GetHeader('', QuoteHtml(T('Generating Link Database')), '');
  51. RequestLockOrError(); # fatal
  52. %LinkDb = %{GetFullLinkList(1, 0, 0, 1)};
  53. print $q->pre(WriteLinkDb());
  54. ReleaseLock();
  55. PrintFooter();
  56. }
  57. # refresh link database with data from the current open page
  58. sub RefreshLinkDb {
  59. if (not defined(&GetLinkList)) {
  60. ReportError(T('The 404 handler extension requires the link data extension (links.pl).'));
  61. return;
  62. }
  63. if ($Page{revision} > 0 and not ($Page{blocks} && $Page{flags})) { #
  64. # make sure we have a cache! We just discard this output, because
  65. # in a multilingual setting we would need to determine the correct
  66. # filename in which to store it in order to get headers
  67. # etc. right.
  68. ToString(sub { PrintWikiToHTML($Page{text}, 1, 0, 1) }); # revision 0, is already locked
  69. }
  70. my @links = GetLinkList(1, 0, 0, 1); # works on cached blocks...
  71. ReadLinkDb();
  72. if (@links) {
  73. $LinkDb{$OpenPageName} = \@links;
  74. } else {
  75. delete $LinkDb{$OpenPageName};
  76. }
  77. WriteLinkDb();
  78. }
  79. # maintain link database
  80. *OldNotFoundHandlerSave = \&Save;
  81. *Save = \&NewNotFoundHandlerSave;
  82. sub NewNotFoundHandlerSave {
  83. my @args = @_;
  84. my $id = $args[0];
  85. OldNotFoundHandlerSave(@args);
  86. RefreshLinkDb(); # for the open page
  87. if (not IsDir($NotFoundHandlerDir)) {
  88. CreateDir($NotFoundHandlerDir);
  89. } elsif ($Page{revision} == 1) {
  90. NotFoundHandlerCacheUpdate($id);
  91. } else {
  92. # unlink PageName, PageName.en, PageName.de, etc.
  93. Unlink("$NotFoundHandlerDir/$id", Glob("$NotFoundHandlerDir/$id.[a-z][a-z]"));
  94. }
  95. }
  96. sub NotFoundHandlerCacheUpdate {
  97. my $id = shift;
  98. # new or deleted page: regenerate all pages that link to this page,
  99. # or to the permanent anchors defined on this page.
  100. ReadLinkDb();
  101. # we will check for the current page, and for all the anchors defined on it.
  102. my @targets = ($id);
  103. if ($PermanentAnchors) {
  104. foreach ($Page{text} =~ m/\[::$FreeLinkPattern\]/g) {
  105. push(@targets, $1); # harmless: potentially adds duplicates
  106. }
  107. }
  108. # if any of the potential targets is the target of a link in the
  109. # link database, then the source page must be rendered anew. in
  110. # other words, delete the cached version of the source page.
  111. my $target = '^(' . join('|', @targets) . ')$';
  112. warn "Unlinking pages pointing to $target\n";
  113. $target = qr($target);
  114. foreach my $source (keys %LinkDb) {
  115. warn "Examining $source\n";
  116. if (grep(/$target/, @{$LinkDb{$source}})) {
  117. Unlink("$NotFoundHandlerDir/$source", Glob("$NotFoundHandlerDir/$source.[a-z][a-z]"));
  118. warn "Unlinking $source\n";
  119. }
  120. }
  121. }
  122. *OldNotFoundHandlerDeletePage = \&DeletePage;
  123. *DeletePage = \&NewNotFoundHandlerDeletePage;
  124. sub NewNotFoundHandlerDeletePage {
  125. my $id = shift;
  126. OpenPage($id); # Page{text} is required to find permanent anchors defined on this page
  127. if (DeletePage($id) eq '') {
  128. NotFoundHandlerCacheUpdate($id);
  129. }
  130. }