links.pl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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('links.pl', 'Link Data Extension');
  18. our ($q, %InterSite, %IndexHash, %Page, %Action, $FS, $LinkPattern, $InterLinkPattern, $FreeLinks, $FreeLinkPattern, $FreeInterLinkPattern, $UrlPattern, $FullUrlPattern, $BracketWiki, $BracketText, $WikiLinks);
  19. $Action{links} = \&DoLinks;
  20. sub DoLinks {
  21. my @args = (GetParam('raw', 0), GetParam('url', 0), GetParam('inter', 0), GetParam('links', 1));
  22. if (GetParam('raw', 0)) {
  23. print GetHttpHeader('text/plain');
  24. PrintLinkList(GetFullLinkList(@args));
  25. } else {
  26. print GetHeader('', QuoteHtml(T('Full Link List')), '');
  27. PrintLinkList(GetFullLinkList(@args));
  28. PrintFooter();
  29. }
  30. }
  31. sub PrintLinkList {
  32. my %links = %{(shift)};
  33. my $existingonly = GetParam('exists', 0);
  34. if (GetParam('raw', 0)) {
  35. foreach my $page (sort keys %links) {
  36. foreach my $link (@{$links{$page}}) {
  37. print "\"$page\" -> \"$link\"\n" if not $existingonly or $IndexHash{$link};
  38. }
  39. }
  40. } else {
  41. foreach my $page (sort keys %links) {
  42. print $q->p(GetPageLink($page) . ': ' . join(' ', @{$links{$page}}));
  43. }
  44. }
  45. }
  46. sub GetFullLinkList { # opens all pages!
  47. my ($raw, $url, $inter, $link) = @_;
  48. my @pglist = AllPagesList();
  49. my %result;
  50. InterInit();
  51. foreach my $name (@pglist) {
  52. OpenPage($name);
  53. my @links = GetLinkList($raw, $url, $inter, $link);
  54. @{$result{$name}} = @links if @links;
  55. }
  56. return \%result;
  57. }
  58. sub GetLinkList { # for the currently open page
  59. my ($raw, $url, $inter, $link) = @_;
  60. my @blocks = split($FS, $Page{blocks});
  61. my @flags = split($FS, $Page{flags});
  62. my %links;
  63. foreach my $block (@blocks) {
  64. if (shift(@flags)) { # dirty block and interlinks or normal links
  65. if ($inter and ($BracketText && $block =~ m/^(\[$InterLinkPattern\s+([^\]]+?)\])$/
  66. or $BracketText && $block =~ m/^(\[\[$FreeInterLinkPattern\|([^\]]+?)\]\])$/
  67. or $block =~ m/^(\[$InterLinkPattern\])$/
  68. or $block =~ m/^(\[\[\[$FreeInterLinkPattern\]\]\])$/
  69. or $block =~ m/^($InterLinkPattern)$/
  70. or $block =~ m/^(\[\[$FreeInterLinkPattern\]\])$/)) {
  71. $links{$raw ? $2 : GetInterLink($2, $3)} = 1 if $InterSite{substr($2,0,index($2, ':'))};
  72. } elsif ($link
  73. and (($WikiLinks and $block !~ m/!$LinkPattern/
  74. and ($BracketWiki && $block =~ m/^(\[$LinkPattern\s+([^\]]+?)\])$/
  75. or $block =~ m/^(\[$LinkPattern\])$/
  76. or $block =~ m/^($LinkPattern)$/))
  77. or ($FreeLinks
  78. and ($BracketWiki && $block =~ m/^(\[\[$FreeLinkPattern\|([^\]]+)\]\])$/
  79. or $block =~ m/^(\[\[\[$FreeLinkPattern\]\]\])$/
  80. or $block =~ m/^(\[\[$FreeLinkPattern\]\])$/)))) {
  81. $links{$raw ? FreeToNormal($2) : GetPageOrEditLink($2, $3)} = 1;
  82. } elsif ($url and $block =~ m/^\[$FullUrlPattern\]$/g) {
  83. $links{$raw ? $1 : GetUrl($1)} = 1;
  84. }
  85. } elsif ($url) { # clean block and url
  86. while ($block =~ m/$UrlPattern/g) {
  87. $links{$raw ? $1 : GetUrl($1)} = 1;
  88. }
  89. while ($block =~ m/\[$FullUrlPattern\s+[^\]]+?\]/g) {
  90. $links{$raw ? $1 : GetUrl($1)} = 1;
  91. }
  92. }
  93. }
  94. my @result = sort keys %links;
  95. return @result;
  96. }