User:AnomieBOT/source/tasks/TFATitleSubpageCreator.pm

This is an old revision of this page, as edited by AnomieBOT (talk | contribs) at 14:13, 20 December 2022 (Updating published sources: TFATitleSubpageCreator: * Comment out new code for now, can't work at that title due to missing OAuth grants.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
package tasks::TFATitleSubpageCreator;

=pod

=begin metadata

Bot:     AnomieBOT II
Task:    TFATitleSubpageCreator
BRFA:    Wikipedia:Bots/Requests for approval/AnomieBOT II
Status:  Approved 2010-05-31
Created: 2010-05-26

Fill in the daily subpages of [[Template:TFA title]] with the title of each day's featured article.

=end metadata

=cut

use utf8;
use strict;

use AnomieBOT::Task qw/:time/;
use Time::Piece;
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 2010-05-31<br />[[Wikipedia:Bots/Requests for approval/AnomieBOT II]]

=cut

sub approved {
    return 200;
}

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

    $api->task('TFATitleSubpageCreator', 0, 10, qw/d::Talk d::Templates/);

    #my $jsontok = $api->edittoken( 'User:AnomieBOT II/TFA title.json' );
    #if($jsontok->{'code'} eq 'shutoff'){
    #    $api->warn("Task disabled: ".$jsontok->{'content'}."\n");
    #    return 300;
    #}
    #if($jsontok->{'code'} ne 'success'){
    #    $api->warn("Failed to get edit token for User:AnomieBOT II/TFA title.json: ".$jsontok->{'error'}."\n");
    #    return 300;
    #}

    #my $json;
    #eval { $json = JSON->new->decode( $jsontok->{'revisions'}[0]{'slots'}{'main'}{'*'} // '' ); };
    #if ( $@ ) {
    #    $api->warn("Failed to parse User:AnomieBOT II/TFA title.json: $@\n");
    #    return 300;
    #}
    #my @jsonupdates = ();

    my $broken = 0;
    my @titles=();
    my @t=gmtime();
    @t=@t[3..5];
    for(my $i=0; $i<7; $i++){
        push @titles, "Wikipedia:Today's featured article/".strftime("%B %-d, %Y", gmtime(timegm(0,0,0,$t[0]+$i,$t[1],$t[2])));
    }
    
    my $iter=$api->iterator(
        titles       => join('|',@titles),
        prop         => 'revisions',
        rvprop       => 'content',
        rvslots      => 'main',
    );
    while($_=$iter->next){
        return 0 if $api->halting;

        if(!$_->{'_ok_'}){
            $api->warn("Failed to retrieve pages: ".$_->{'error'}."\n");
            return 60;
        }

        next if exists($_->{'missing'});
        next if $_->{'revisions'}[0]{'slots'}{'main'}{'*'}=~/\{\{TFAempty\}\}/;

        my $title=$_->{'title'};
        my $date=$title; $date=~s/Wikipedia:Today\x27s featured article\///;
        my $template="Template:TFA title/$date";

        my $link = undef;
        $api->process_templates( $_->{'revisions'}[0]{'slots'}{'main'}{'*'}, sub {
            my $name = shift;
            my $params = shift;
            return undef unless $name eq 'TFAFULL';
            for my $p ( $api->process_paramlist( @$params ) ) {
                $link = $p->{'value'} if $p->{'name'} eq '1';
            }
        } );
        if ( ! defined( $link ) ) {
            $api->warn("No TFAFULL template in $title?\n");
            warn $_->{'revisions'}[0]{'slots'}{'main'}{'*'};
            next;
        }

        # WhyTF would people put a comment inside the template parameter? But they have, sigh.
        $link =~ s/<!--.*?-->//gs;

        # Ask MediaWiki to canonicalize the title for us, because the actual
        # normalization can depend on various factors.
        my $res=$api->query(titles=>$link);
        if($res->{'code'} ne 'success'){
            $api->warn("Failed to get canonical name for $link: ".$res->{'error'}."\n");
            $broken = 1;
            next;
        }
        $link=$res->{'query'}{'normalized'}[0]{'to'} // $link;

        # Ok, check the page
        my $tok=$api->edittoken($template, 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 $template: ".$tok->{'error'}."\n");
            $broken = 1;
            next;
        }

        # Need to edit?
        if(($tok->{'revisions'}[0]{'slots'}{'main'}{'*'} // '') ne $link){
            $api->log("Featured article for [[$title|$date]] is [[$link]] for $template");
            my $r=$api->edit($tok, $link, "Featured article for [[$title|$date]] is [[$link]]", 1, 1);
            if($r->{'code'} ne 'success'){
                $api->warn("Write failed on $template: ".$r->{'error'}."\n");
                $broken = 1;
                next;
            }
        }

        #my $dt = Time::Piece->strptime( $date, '%B %d, %Y' )->strftime( '%Y-%m-%d' );
        #if ( $link ne ( $json->{$dt} // '' ) ) {
        #    $json->{$dt} = $link;
        #    push @jsonupdates, "[[$title|$dt]] is [[$link]]";
        #}
    }

    #if ( @jsonupdates ) {
    #    my $outtxt = JSON->new->pretty->canonical->encode( $json );
    #    my $r = $api->edit( $jsontok, $outtxt, "Updates: " . join( '; ', @jsonupdates ), 1, 1 );
    #    if ( $r->{'code'} ne 'success' ) {
    #        $api->warn( "Write failed on User:AnomieBOT II/TFA title.json: " . $r->{'error'} . "\n" );
    #        $broken = 1;
    #    }
    #}

    @titles=();
    @t=gmtime();
    @t=@t[3..5];
    for(my $i=0; $i<=1; $i++){
        push @titles, "Template:TFA title/".strftime("%B %-d, %Y", gmtime(timegm(0,0,0,$t[0]+$i,$t[1],$t[2])));
    }

    $iter=$api->iterator(
        titles       => join('|',@titles),
        prop         => 'revisions',
        rvprop       => 'content',
        rvslots      => 'main',
    );
    while($_=$iter->next){
        return 0 if $api->halting;

        if(!$_->{'_ok_'}){
            $api->warn("Failed to retrieve pages: ".$_->{'error'}."\n");
            return 60;
        }

        if(exists($_->{'missing'}) || $_->{'revisions'}[0]{'slots'}{'main'}{'*'}=~/^\s*$/) {
            my $title=$_->{'title'};
            my $fa=$title; $fa=~s/^Template:TFA title\//Wikipedia:Today\x27s featured article\//;
            $api->whine("Cannot find featured article in [[$fa]]", "Help! I can't find the featured article link in [[$fa]] in order to populate [[$title]]. Please correct the link or create the template manually.");
            $broken = 1;
        }
    }

    # Retry soon if there was a failure.
    return 600 if $broken;

    # Ok, done. Run once at 11pm (UTC) and again right at midnight, and
    # otherwise every 6 hours.
    my $tt=time()%86400;
    my $t=82800-$tt;
    $t=86400-$tt if $t<0;
    $t=6*3600 if $t>6*3600;
    return $t;
}

1;