User:DeadLinkBOT/source
Appearance
use Perlwikipedia;
my $user = 'DeadLinkBOT'; my $pass = 'XXX';
my $editor=Perlwikipedia->new($user);
$editor->login($user, $pass);
open (INF, "ReadyLinks.lst");
@data = <INF>;
close(INF);
foreach $line (@data) {
chomp($line);
undef(@cond);
undef(@newtext);
($oldlink, $mode, $cond[0], $newtext[0], $cond[1], $newtext[1], $cond[2], $newtext[2], $cond[3], $newtext[3], $cond[4], $newtext[4], $cond[5], $newtext[5]) = split(/\|\|/, $line);
$res = $editor->_get( 'Special:LinkSearch', 'view', "&target=$oldlink&limit=10" );
$content = $res->decoded_content;
while ($content =~ m{from <a href="[^"]+" title="([^"]+)">}g) {
($title, $junk) = split(/Archive/, $1);
if ($junk eq "") { push(@articles, $title); }
}
foreach $title (@articles) {
$text = $editor->get_text($title);
$start = time;
$replace = "";
$c = 0;
while ($c <= 5) {
($type, $regrex, $regrex2) = split(/==/, $cond[$c]);
if ($type eq "*") {
$replace = $newtext[$c];
last;
}
elsif ($type eq "InTitleAndText") {
if (($title =~ m/$regrex/) && ($text =~ m/$regrex2/)) {
$replace = $newtext[$c];
last;
}
}
elsif ($type eq "InTitle") {
if ($title =~ m/$regrex/) {
$replace = $newtext[$c];
last;
}
}
elsif ($type eq "NotInTitle") {
if ($title !~ m/$regrex/) {
$replace = $newtext[$c];
last;
}
}
elsif ($type eq "InText") {
if ($text =~ m/$regrex/) {
$replace = $newtext[$c];
last;
}
}
elsif ($type eq "NotInText") {
if ($text !~ m/$regrex/) {
$replace = $newtext[$c];
last;
}
}
elsif ($type) {
print "invalid condition type '$type' found in link '$oldlink' - trying next condition (if available)";
}
$c++;
}
if ($replace) {
$oldtext = $text;
$replace =~ s/\\n/\n/g;
if ($mode == 1) {
$text =~ s/$oldlink/$replace/g;
}
elsif ($mode == 2) {
$text =~ s/\[?$oldlink.*?\]/$replace/g;
$text =~ s/$oldlink/$replace/g;
}
else {
print "article '$title' skipped due to invalid mode";
$mode = 999;
}
if ($text eq $oldtext) {
if ($mode != 999) { print "article '$title' skipped due to no changes made ('$oldlink' -> '$replace')"; }
}
else {
$lapsed = time - $start;
if ($lapsed > 5) { $lapsed = 5; }
sleep(5 - $lapsed);
$start = time;
$title =~ s/[:\/()]/-/g;
open (OUTF, ">$title.new.txt");
print OUTF $text;
close OUTF;
$lapsed = time - $start;
if ($lapsed > 5) { $lapsed = 5; }
sleep(5 - $lapsed);
$start = time;
}
}
else {
print "article '$title' skipped due to no matching conditions for link '$oldlink'";
}
}
}