User:AnomieBOT/source/tasks/PageCreator.pm

package tasks::PageCreator;

=pod

=begin metadata

Bot:      AnomieBOT
Task:     PageCreator
BRFA:     Wikipedia:Bots/Requests for approval/AnomieBOT 32
Status:   Completed 2009-06-20
Created:  2009-06-19
OnDemand: true

Create redirects from isotopes for [[WP:WikiProject Elements]].

=end metadata

=cut

use utf8;
use strict;

use AnomieBOT::Task;
use Data::Dumper;
use vars qw/@ISA/;
@ISA=qw/AnomieBOT::Task/;

my $req="[[User:AnomieBOT/req/WikiProject Elements 1|request]]";

my %elements=(
    'Scandium' => [ 36, 60 ],
    'Titanium' => [ 38, 63 ],
    'Vanadium' => [ 40, 65 ],
    'Chromium' => [ 42, 67 ],
    'Manganese' => [ 44, 69 ],
    'Iron' => [ 45, 72 ],
    'Cobalt' => [ 47, 75 ],
    'Nickel' => [ 48, 78 ],
    'Copper' => [ 52, 80 ],
    'Zinc' => [ 54, 83 ],
    'Gallium' => [ 56, 86 ],
    'Germanium' => [ 58, 89 ],
    'Arsenic' => [ 60, 92 ],
    'Selenium' => [ 65, 94 ],
    'Bromine' => [ 67, 97 ],
    'Krypton' => [ 69, 100 ],
    'Rubidium' => [ 71, 102 ],
    'Strontium' => [ 73, 105 ],
    'Yttrium' => [ 76, 108 ],
    'Zirconium' => [ 78, 110 ],
    'Niobium' => [ 81, 113 ],
    'Molybdenum' => [ 83, 115 ],
    'Technetium' => [ 85, 118 ],
    'Ruthenium' => [ 87, 120 ],
    'Rhodium' => [ 89, 122 ],
    'Palladium' => [ 91, 124 ],
    'Silver' => [ 93, 130 ],
    'Cadmium' => [ 95, 132 ],
    'Indium' => [ 97, 135 ],
    'Tin' => [ 99, 137 ],
    'Antimony' => [ 103, 139 ],
    'Tellurium' => [ 105, 142 ],
    'Iodine' => [ 108, 144 ],
    'Xenon' => [ 110, 147 ],
    'Caesium' => [ 112, 151 ],
    'Barium' => [ 114, 153 ],
    'Lanthanum' => [ 117, 155 ],
    'Cerium' => [ 119, 157 ],
    'Praseodymium' => [ 121, 159 ],
    'Neodymium' => [ 124, 161 ],
    'Promethium' => [ 126, 163 ],
    'Samarium' => [ 128, 165 ],
    'Europium' => [ 130, 167 ],
    'Gadolinium' => [ 134, 169 ],
    'Terbium' => [ 135, 171 ],
    'Dysprosium' => [ 138, 173 ],
    'Holmium' => [ 140, 175 ],
    'Erbium' => [ 143, 177 ],
    'Thulium' => [ 145, 179 ],
    'Ytterbium' => [ 148, 181 ],
    'Lutetium' => [ 150, 184 ],
    'Hafnium' => [ 153, 188 ],
    'Tantalum' => [ 155, 190 ],
    'Tungsten' => [ 158, 192 ],
    'Rhenium' => [ 160, 194 ],
    'Osmium' => [ 162, 197 ],
    'Iridium' => [ 164, 199 ],
    'Platinum' => [ 166, 202 ],
    'Gold' => [ 169, 205 ],
    'Mercury' => [ 171, 210 ],
    'Thallium' => [ 176, 212 ],
    'Lead' => [ 178, 215 ],
    'Bismuth' => [ 184, 218 ],
    'Polonium' => [ 188, 220 ],
    'Astatine' => [ 191, 223 ],
    'Radon' => [ 195, 228 ],
    'Francium' => [ 199, 232 ],
    'Radium' => [ 202, 234 ],
    'Actinium' => [ 206, 236 ],
    'Thorium' => [ 209, 238 ],
    'Protactinium' => [ 212, 240 ],
    'Uranium' => [ 217, 242 ],
    'Neptunium' => [ 225, 244 ],
    'Plutonium' => [ 228, 247 ],
    'Americium' => [ 231, 249 ],
    'Curium' => [ 232, 252 ],
    'Berkelium' => [ 235, 254 ],
    'Californium' => [ 237, 256 ],
    'Einsteinium' => [ 240, 258 ],
    'Fermium' => [ 242, 260 ],
    'Mendelevium' => [ 245, 262 ],
    'Nobelium' => [ 248, 264 ],
    'Lawrencium' => [ 251, 266 ],
    'Rutherfordium' => [ 253, 268 ],
    'Dubnium' => [ 255, 270 ],
    'Seaborgium' => [ 258, 273 ],
    'Bohrium' => [ 260, 275 ],
    'Hassium' => [ 263, 277 ],
    'Meitnerium' => [ 265, 279 ],
    'Darmstadtium' => [ 267, 281 ],
    'Roentgenium' => [ 272, 283 ],
    'Ununbium' => [ 277, 285 ],
    'Ununtrium' => [ 278, 287 ],
    'Ununquadium' => [ 285, 289 ],
    'Ununpentium' => [ 287, 291 ],
    'Ununhexium' => [ 289, 293 ],
    'Ununseptium' => [ 291, 292 ],
    'Ununoctium' => [ 293, 294 ],
);

sub new {
    my $class=shift;
    my $self=$class->SUPER::new();
    bless $self, $class;
    return $self;
}

=pod

=for info
Approved 2009-06-19, completed 2009-06-20<br />[[Wikipedia:Bots/Requests for approval/AnomieBOT 32]]

=cut

sub approved {
    return -1;
}

sub run {
    my ($self, $api)=@_;
    my $res;

    $api->task('PageCreator', 0, 10, qw//);

    # Spend a max of 5 minutes on this task before restarting
    my $endtime=time()+300;

    while(my ($element,$range)=each %elements){
        my $l=length($range->[1]);
        for(my $iso=$range->[0]; $iso<=$range->[1]; $iso++){
            my $title="$element-$iso";
            next if exists($api->store->{$title});
            my $redir="Isotopes of ".lc($element)."#$element-$iso";
            my $page=sprintf("#REDIRECT [[Isotopes of %s#%s-%d]]\n\n[[Category:Isotopes of %s|%s-%0*d]]", lc($element), $element, $iso, lc($element), $element, $l, $iso);

            # Ok, check the page
            my $tok=$api->edittoken($title, EditRedir => 1);
            if($tok->{'code'} eq 'shutoff'){
                $api->warn("Task disabled: ".$tok->{'content'}."\n");
                return 300;
            }
            if($tok->{'code'} ne 'success'){
                $api->warn("Failed to get edit token for $title: ".$tok->{'error'}."\n");
                next;
            }
            if(!exists($tok->{'missing'})){
                $api->log("$title already exists, skipping");
                $api->store->{$title}=1;
                next;
            }

            # Create page
            my $summary="Creating redirect per $req";
            $api->log("$summary in $title");
            my $r=$api->edit($tok, $page, $summary, 0, 1);
            if($r->{'code'} ne 'success'){
                $api->warn("Write failed on $title: ".$r->{'error'}."\n");
                next;
            }
            $api->store->{$title}=1;

            # If we've been at it long enough, let another task have a go.
            return 0 if time()>=$endtime;
        }
    }

    return 600;
}

1;