User:AnomieBOT/source/tasks/TemplateTalkRedirectCreator.pm

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

=pod

=begin metadata

Bot:      AnomieBOT
Task:     TemplateTalkRedirectCreator
BRFA:     Wikipedia:Bots/Requests for approval/AnomieBOT 71
Status:   Approved 2013-11-05
Created:  2013-10-08

Create redirects for non-existing talk pages of certain pages:
* Template pages ending in "/doc", "/sandbox", "/testcases", or "/TemplateData"
* Module pages ending in "/doc" or "/sandbox"

=end metadata

=cut

use utf8;
use strict;

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

my %patterns = (
    'Template' => [
        '%/doc',
        '%/sandbox',
        '%/testcases',
        '%/TemplateData',
        '%/styles.css',
    ],
    'Module' => [
        '%/doc',
        '%/sandbox',
        # Not %/testcases though, those talk pages are often (ab)used to run the tests.
        '%/styles.css',
    ],
);

sub mapTitle {
    my $title = shift;
    return $1 if $title=~m!^(Template talk:.+)/(?:doc|sandbox|testcases|TemplateData|styles\.css)$!;
    return $1 if $title=~m!^(Module talk:.+)/(?:doc|sandbox|styles\.css)$!;
    return undef;
}

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

=pod

=for info
Approved 2013-11-05<br />[[Wikipedia:Bots/Requests for approval/AnomieBOT 71]]

=cut

sub approved {
    return 3;
}

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

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

    my %ns = $api->namespace_map();
    my %rns = $api->namespace_reverse_map();

    my @where = ();
    while ( my ($ns, $pats) = each %patterns ) {
        push @where, 'p1.page_namespace = ' . $ns{$ns} . ' AND (' . join( ' OR ', map "p1.page_title LIKE '$_'", @$pats ) . ')';
    }
    my $where = join( ' OR ', @where );

    my ($dbh);
    eval {
        ($dbh) = $api->connectToReplica( 'enwiki' );
    };
    if ( $@ ) {
        $api->warn( "Error connecting to replica: $@\n" );
        return 300;
    }

    my $cont = $self->{'dbcontinue'} // '';

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

    while ( 1 ) {
        return 0 if $api->halting;

        # Load the list of redirects needing creation
        my @rows;
        eval {
            @rows = @{ $dbh->selectall_arrayref( qq{
                SELECT p1.page_namespace AS ns, p1.page_title AS title
                FROM page as p1
                    LEFT JOIN page as p2 ON( p2.page_namespace = p1.page_namespace + 1 and p2.page_title = p1.page_title )
                WHERE p2.page_id IS NULL AND ( $where ) $cont
                ORDER BY p1.page_namespace, p1.page_title
                LIMIT 500
            }, { Slice => {} } ) };
        };
        if ( $@ ) {
            $api->warn( "Error fetching page list from replica: $@\n" );
            return 300;
        }
        last unless @rows;

        my %redirects = ();
        for my $row (@rows) {
            utf8::decode( $row->{'title'} ); # Data from database is binary
            my $title = $rns{$row->{'ns'}+1} . ':' . $row->{'title'};
            $title =~ s/_/ /g;

            my $basetitle = mapTitle( $title );
            unless ( $basetitle ) {
                $api->warn( "Could not find redirect target for $title\n" );
                next;
            }

            $redirects{$title} = $basetitle;
        }

        if ( %redirects ) {
            # Bypass double redirects and remove missing target pages
            my $res = $api->query(
                titles => join('|', values %redirects),
                redirects => 1
            );
            if($res->{'code'} ne 'success'){
                $api->warn("Failed to retrieve redirect list: ".$res->{'error'}."\n");
                return 60;
            }
            my %map = ();
            if ( exists($res->{'query'}{'normalized'} ) ) {
                $map{$_->{'from'}} = $_->{'to'} foreach @{$res->{'query'}{'normalized'}};
            }
            if ( exists($res->{'query'}{'redirects'} ) ) {
                $map{$_->{'from'}} = $_->{'to'} foreach @{$res->{'query'}{'redirects'}};
            }
            my %exists = ();
            if ( exists($res->{'query'}{'pages'} ) ) {
                for my $p (values %{$res->{'query'}{'pages'}}) {
                    $exists{$p->{'title'}} = 1 if $p->{'pageid'}//0;
                }
            }
            while( my ($redir, $target) = each( %redirects ) ) {
                my %seen=( $target => 1 );
                while ( exists( $map{$target} ) ) {
                    $target = $map{$target};
                    $redirects{$redir} = $target;
                    if ( exists( $seen{$target} ) ) {
                        $api->warn("Redirect loop involving [[$target]]");
                        delete $redirects{$redir};
                        last;
                    }
                    $seen{$target}=1;
                }
                delete $redirects{$redir} unless exists( $exists{$target} );
            }

            # Now, create the redirects
            while( my ($redir, $target) = each( %redirects ) ) {
                return 0 if $api->halting;

                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");
                    next;
                }

                my $txt = "#REDIRECT [[$target]]\n\n{{Redirect category shell|\n{{R from remote talk page}}\n}}";
                my $summary="Redirecting to [[$target]] to avoid decentralized discussion";

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

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

        # On the next time around, skip any we've already processed this run
        my ($ns, $title) = @{$rows[$#rows]}{'ns','title'};
        $title = $dbh->quote( $title );
        $cont = " AND (p1.page_namespace > $ns OR p1.page_namespace = $ns AND p1.page_title > $title)";
        $self->{'dbcontinue'} = $cont;

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

    $self->{'dbcontinue'} = '';

    return 21600;
}

1;