links.pl 3.8 KB

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