#!/usr/bin/perl # TmNetWiki version 0.4 (September 11, 2002) # Copyright (C) 2002 Adam Brate # # Based on the GPLed UseModWiki 0.92 # Copyright (C) 2000-2001 Clifford A. Adams # or # Based on the GPLed AtisWiki 0.3 (C) 1998 Markus Denker # # ...which was based on # the LGPLed CVWiki CVS-patches (C) 1997 Peter Merel # and The Original WikiWikiWeb (C) Ward Cunningham # (code reused with permission) # Email and ThinLine options by Jim Mahoney # # 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 # the Free Software Foundation; either version 2 of the License, or # (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 # along with this program; if not, write to the # Free Software Foundation, Inc. # 59 Temple Place, Suite 330 # Boston, MA 02111-1307 USA package Wiki::links; use strict; # needs subs Wiki::template: GetHeader, T, GetMinimumFooter, # GetEditLink # Wiki::index: AllPagesList # Wiki::utility: GetParam, FreeToNormal, QuoteHtml # Wiki::page: OpenPage # Wiki::text: OpenDefaultText # Wiki::wiki: UrlLink, InterPageLink, StripUrlPunct # Wiki::translate: T # needs consts $UrlPattern, $FreeLinks, $FreeLinkPattern, $InterLinkPattern # needs vars %LinkIndex, %Text use base 'Exporter'; use Wiki::consts; use Wiki::template; use Wiki::index; use Wiki::utility; use Wiki::page; use Wiki::text; use Wiki::wiki; use Wiki::translate; #if ($^V and $^V gt v5.6.0) { # our @EXPORT; #else { use vars qw(@EXPORT); #} @EXPORT = qw( DoLinks BuildLinkIndex BuildLinkIndexPage ); sub PrintLinkList; sub GetFullLinkList; sub GetPageLinks; sub DoLinks { print &GetHeader('', &QuoteHtml(T('Full Link List')), ''); print "
\n\n\n\n\n";  # Extra lines to get below the logo
  &PrintLinkList(&GetFullLinkList());
  print "
\n"; print &GetMinimumFooter(); } sub PrintLinkList { my ($pagelines, $page, $names, $editlink); my ($link, $extra, @links, %pgExists); %pgExists = (); foreach $page (&AllPagesList()) { $pgExists{$page} = 1; } $names = &GetParam("names", 1); $editlink = &GetParam("editlink", 0); foreach $pagelines (@_) { @links = (); foreach $page (split(' ', $pagelines)) { if ($page =~ /\:/) { # URL or InterWiki form if ($page =~ /$UrlPattern/) { ($link, $extra) = &UrlLink($page); } else { ($link, $extra) = &InterPageLink($page); } } else { if ($pgExists{$page}) { $link = &GetPageLink($page); } else { $link = $page; if ($editlink) { $link .= &GetEditLink($page, "?"); } } } push(@links, $link); } if (!$names) { shift(@links); } print join(' ', @links), "\n"; } } sub GetFullLinkList { my ($name, $unique, $sort, $exists, $empty, $link, $search); my ($pagelink, $interlink, $urllink); my (@found, @links, @newlinks, @pglist, %pgExists, %seen); $unique = &GetParam("unique", 1); $sort = &GetParam("sort", 1); $pagelink = &GetParam("page", 1); $interlink = &GetParam("inter", 0); $urllink = &GetParam("url", 0); $exists = &GetParam("exists", 2); $empty = &GetParam("empty", 0); $search = &GetParam("search", ""); if (($interlink == 2) || ($urllink == 2)) { $pagelink = 0; } %pgExists = (); @pglist = &AllPagesList(); foreach $name (@pglist) { $pgExists{$name} = 1; } %seen = (); foreach $name (@pglist) { @newlinks = (); if ($unique != 2) { %seen = (); } @links = &GetPageLinks($name, $pagelink, $interlink, $urllink); foreach $link (@links) { $seen{$link}++; if (($unique > 0) && ($seen{$link} != 1)) { next; } if (($exists == 0) && ($pgExists{$link} == 1)) { next; } if (($exists == 1) && ($pgExists{$link} != 1)) { next; } if (($search ne "") && !($link =~ /$search/)) { next; } push(@newlinks, $link); } @links = @newlinks; if ($sort) { @links = sort(@links); } unshift (@links, $name); if ($empty || ($#links > 0)) { # If only one item, list is empty. push(@found, join(' ', @links)); } } return @found; } sub BuildLinkIndex { my (@pglist, $page, @links, $link, %seen); @pglist = &AllPagesList(); %LinkIndex = (); foreach $page (@pglist) { &BuildLinkIndexPage($page); } } sub BuildLinkIndexPage { my ($page) = @_; my (@links, $link, %seen); @links = &GetPageLinks($page, 1, 0, 0); %seen = (); foreach $link (@links) { if (defined($LinkIndex{$link})) { if (!$seen{$link}) { $LinkIndex{$link} .= " " . $page; } } else { $LinkIndex{$link} .= " " . $page; } $seen{$link} = 1; } } sub GetPageLinks { my ($name, $pagelink, $interlink, $urllink) = @_; my ($text, @links); @links = (); &OpenPage($name); &OpenDefaultText(); $text = $Text{'text'}; $text =~ s/((.|\n)*?)<\/html>/ /ig; $text =~ s/(.|\n)*?\<\/nowiki>/ /ig; $text =~ s/
(.|\n)*?\<\/pre>/ /ig;
  $text =~ s/(.|\n)*?\<\/code>/ /ig;
  if ($interlink) {
    $text =~ s/''+/ /g;  # Quotes can adjacent to inter-site links
    $text =~ s/$InterLinkPattern/push(@links, &StripUrlPunct($1)), ' '/ge;
  } else {
    $text =~ s/$InterLinkPattern/ /g;
  }
  if ($urllink) {
    $text =~ s/''+/ /g;  # Quotes can adjacent to URLs
    $text =~ s/$UrlPattern/push(@links, &StripUrlPunct($1)), ' '/ge;
  } else {
    $text =~ s/$UrlPattern/ /g;
  }
  if ($pagelink) {
    if ($FreeLinks) {
      my $fl = $FreeLinkPattern;
      $text =~ s/\[\[$fl\|[^\]]+\]\]/push(@links, &FreeToNormal($1)), ' '/ge;
      $text =~ s/\[\[$fl\]\]/push(@links, &FreeToNormal($1)), ' '/ge;
    }
    if ($WikiLinks) {
      $text =~ s/$LinkPattern/push(@links, &StripUrlPunct($1)), ' '/ge;
    }
  }
  return @links;
}