User:ZacharyBot/source

From Wikinews, the free news source you can write!
Jump to navigation Jump to search
#!/usr/bin/perl
#
## ZacharyBot, for routine maintenance tasks on the English Wikinews
## This bot is GPL--that's GNU Public License

use MediaWiki;
my $c = MediaWiki->new();
$c->setup("$ENV{'HOME'}/etc/zacharybot.ini");

my $speed = 15;

sub updateWikinews {
    my ($title, $content, $opts) = @_;
    my $pg = $c->get($title);
    $pg->load;
    if ($opts->{mode} eq 'prepend' || $opts->{mode} eq 'append') {
        # add prepend/append code
        my $original = $pg->content();
        if ($opts->{mode} eq 'prepend') { $content = $content . $original; }
        if ($opts->{mode} eq 'append')  { $content = $original . $content; }
    }
    $pg->{content} = $content;
    $pg->{summary} = $opts->{summary};
    $pg->{minor} = $opts->{minor} || 0;
    $pg->save();
}

sub fixDoubleRedirects {
    my $pg = $c->get("Special:DoubleRedirects");
    $pg->load;
    my $input = $pg->content();
    $input =~ s/[\n\r\t]//g;
    $input =~ s/^.*<ol[^>]*><li>(.*)<\/li><\/ol>.*$/$1/;
    $input =~ s/<[\/]?a[^>]*>//g;
    $input =~ s/ \(Edit\) →‎ /|/g;
    $input =~ s/ →‎ /|/g;
    my @pages = split("</li><li>", $input);
    foreach (@pages) {
        next if $_ =~ /^<s>/;
        my ($first, $second, $target) = split(/\|/, $_);
        updateWikinews($first, "#REDIRECT [[$target]]", {
            mode    => '',
            summary => "Fixing double redirects",
            minor   => 1,
        });
        sleep $speed;
    }
    exit;
}

sub flagBrokenRedirects {
    my $pg = $c->get("Special:BrokenRedirects");
    $pg->load;
    my $input = $pg->content();
    $input =~ s/[\n\t\r]//g;
    $input =~ s/^.*<ol[^>]*><li>(.*)<\/ol>.*$/$1/;
    $input =~ s/<[\/]?a[^>]*>//g;
    $input =~ s/ \(edit\)[^<]*<\/li>//g;
    my @pages = split("<li>", $input);
    foreach (@pages) {
        next if $_ =~ /^User\:/;
        next if $_ =~ /^User talk\:/;
        updateWikinews($_, "{{delete|Broken redirect flagged with ZacharyBot}}", {
            mode    => 'prepend',
            summary => "Flagging broken redirect for deletion"
        });
        sleep $speed;
    }
}

fixDoubleRedirects();
flagBrokenRedirects();

1;