Jump to content

User:AnomieBOT/source/tasks/PageCreator2.pm

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

=pod

=begin metadata

Bot:      AnomieBOT
Task:     PageCreator2
BRFA:     Wikipedia:Bots/Requests for approval/AnomieBOT 39
Status:   Completed 2010-06-22
Created:  2010-06-16

Create redirects from titles with the correct (but not well supported) Romanian S-comma and/or T-comma diacritics to the existing titles using S-cedilla and/or T-cedilla.

=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/Romanian diacritics|request]]";

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

=pod

=for info
Approved 2010-06-21, Completed 2010-06-22<br />[[Wikipedia:Bots/Requests for approval/AnomieBOT 39]]

=cut

sub approved {
    return -1;
}

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

    $api->task('PageCreator2', 0, 10, qw/d::Redirects/);

    my $re=$api->redirect_regex();

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

    my $iter=$api->iterator(generator=>'links',gpllimit=>'100',titles=>'User:Strainu/ro',prop=>'revisions',rvprop=>'content',rvslots=>'main',rvsection=>0);
    while(my $page=$iter->next){
        if(!$page->{'_ok_'}){
            $api->warn("Could not retrieve page from iterator: ".$page->{'error'}."\n");
            return 60;
        }

        my $title=$page->{'title'};
        my $redir=$title;
        $redir=~y/ŞşŢţ/ȘșȚț/;
        next if exists($api->store->{$redir});

        if($redir eq $title){
            $api->warn("Skipping title '$title' without cedillas\n");
            $api->store->{$redir}=1;
            next;
        }
        if(exists($page->{'missing'})){
            $api->warn("Page $title does not exist?!\n");
            $api->store->{$redir}=1;
            next;
        }

        my $tok=$api->edittoken($redir, 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 $redir: ".$tok->{'error'}."\n");
            next;
        }
        if(!exists($tok->{'missing'})){
            $api->log("$redir already exists, skipping");
            $api->store->{$redir}=1;
            next;
        }

        my $txt=$page->{'revisions'}[0]{'slots'}{'main'}{'*'};
        my $summary;
        if($txt=~$re){
            $summary="Copying redirect [[$title]] with S-cedilla and/or T-cedilla diacritics to [[$redir]] with correct Romanian S-comma and/or T-comma diacritics per $req";
        } else {
            $txt="#REDIRECT [[$title]] {{R from alternative name}}\n";
            $summary="Creating redirect to [[$title]] (with S-cedilla and/or T-cedilla diacritics) from [[$redir]] with correct Romanian S-comma and/or T-comma diacritics per $req";
        }

        # Create page
        $api->log("$summary in $title");
        my $r=$api->edit($tok, $txt, $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;
    }

    $api->log("Task may be complete!");

    return 600;
}

1;