Jump to content

User:AnomieBOT/source/tasks/PageMover.pm

From Wikipedia, the free encyclopedia
package tasks::PageMover;

=pod

=begin metadata

Bot:      AnomieBOT
Task:     PageMover
BRFA:     Wikipedia:Bots/Requests for approval/AnomieBOT 35
Status:   Completed 2009-12-14
Created:  2009-10-31

Move date articles (e.g. [[February 12, 2005]]) in [[:Category:Days in 2003]]
and [[:Category:Days in 2005]] from article space to subpages of
[[Portal:Current events]].

=end metadata

=cut

use utf8;
use strict;

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

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

=pod

=for info
Approved 2009-12-13, Completed 2009-12-14<br />[[Wikipedia:Bots/Requests for approval/AnomieBOT 35]]

=cut

sub approved {
    return -1;
}

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

    $api->task('PageMover',0,10,qw/d::Redirects/);
    my $re=$api->redirect_regex();

    my $iter=$api->iterator(
        list        => 'categorymembers',
        cmtitle     => ['Category:Days in 2003','Category:Days in 2005'],
        cmnamespace => 0,
        cmtype      => 'page',
        cmlimit     => 'max',
    );
    my $maybedone=1;
    my $endtime=time()+300;
    while(my $page=$iter->next){
        my $title=$page->{'title'};
        next unless $title=~/^(\S+ \d+), (\d+)$/;
        my $newtitle="Portal:Current events/$2 $1";

        my $tok=$api->movetoken($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 move token for $title: ".$tok->{'error'}."\n");
            next;
        }
        next if exists($tok->{'redirect'});

        $api->log("Moving $title to $newtitle");
        $res=$api->move($tok, $newtitle, "Moving $title to $newtitle per [[User:AnomieBOT/req/Current events date move|request]]", 1, 0);
        if($res->{'code'} ne 'success'){
            $api->warn("Failed to move $title: ".$res->{'error'}."\n");
            $maybedone=0;
            next;
        }
        if(time()>$endtime){ $maybedone=0; last; }
    }

    $api->log("PageMover may be DONE!") if $maybedone;

    # Check again in 10 minutes.
    return 600;
}

1;