User:Dantheox/pmlinks.pl

use strict; use LWP;
 * 1) !/usr/local/bin/perl -w

if (@ARGV < 2) { print "Usage:\n"; print "$0 \n"; exit(1); }

my $ua = LWP::UserAgent->new(       agent => '' );

my $srcfile = shift;
 * 1) First get the wiki source

my $url = shift; my $resp = $ua->get($url) or die "Can't get page '$url'"; my $html = $resp->content;
 * 1) Then get the rendered page to establish whether a link is blue or red

my %links = ;
 * 1) Extract the relevant links, taking title and text

while ($html =~ m!(.*?)!gsi) { #"   unless ($1) { $links{$2}= $3 } }


 * 1) Now check the source against the text
 * 2) Only pull out the first link on a line beginning with ":"
 * 3) Any subsequent lines which appear "under" this line are included with it
 * 4) This means everything up to either a blank line or a line beginning with a lone '#'

my $good = ""; my $bad = ""; my $lst = 0;

open IN, "$srcfile" or die "Can't open $srcfile"; while () { if (/^#[^:#*;]/) { my ($lt) = /\[\[(.*?)\]\]/; unless ($lt) { $lst = 0; next } my ($link, $text); if ($lt =~ /\|/) { ($link, $text) = $lt =~ /([^|]+)\|(.+)/ } else            {  $link = $text = $lt }

$lst = $links{$link} ? \$good : \$bad;

$$lst.=$_; } elsif (/^\s*$/) { $lst = 0; } elsif ($lst) { $$lst.=$_; } } close IN;

print "Red links:\n"; print "$bad\n"; print "Blue links:\n"; print "$good\n";