![]() | Approval requested Wikipedia:Bots/Requests for approval/AnomieBOT 77 |
package tasks::POTDPageCreator;
=pod
=begin metadata
Bot: AnomieBOT
Task: POTDPageCreator
BRFA: Wikipedia:Bots/Requests for approval/AnomieBOT 77
Status: In trial
Created: 2017-01-29
Create Template:POTD protected/YYYY-MM-DD two days before the corresponding
date, in time for it to be transcluded onto [[WP:Main Page/Tomorrow]].
=end metadata
=cut
use utf8;
use strict;
use AnomieBOT::Task;
use vars qw/@ISA/;
@ISA=qw/AnomieBOT::Task/;
use POSIX qw/strftime/;
use Data::Dumper;
sub new {
my $class=shift;
my $self=$class->SUPER::new;
bless $self, $class;
return $self;
}
=pod
=for info
Approval requested<br />[[Wikipedia:Bots/Requests for approval/AnomieBOT 77]]
=cut
sub approved {
return 2;
}
sub run {
my ($self, $api)=@_;
$api->task('POTDPageCreator', 0, 10, qw(d::Talk d::Nowiki d::Trial));
my $trial = $api->check_trial( 1486560586, 'AnomieBOT 77' );
return $$trial if $trial;
my @now = gmtime;
if ( ( time % 86400 ) < 86400 - 7200 ) {
$now[3]+=1;
} else {
$now[3]+=2;
}
my $date = strftime( '%F', @now );
my $talk = 'Wikipedia talk:Picture of the day';
my $srcpage = "Template:POTD/$date";
my $page = "Template:POTD protected/$date";
my $tok = $api->edittoken( $page, EditRedir => 1 );
if($tok->{'code'} eq 'shutoff'){
$api->warn("Task disabled: " . $tok->{'content'} . "\n");
return 300;
}
if($tok->{'code'} eq 'pageprotected'){
$api->warn("I'm too late for $page");
my $res = $api->query( titles => $page, formatversion => 2 );
if ( $res->{'query'}{'pages'}[0]{'missing'} // 0 ) {
$api->whine( "[[$page]] was not created", "It seems that I'm too late to create [[$page]], it's already protected. Please create it manually.", Pagename => $talk );
}
return $self->nexttime();
}
if($tok->{'code'} ne 'success'){
$api->warn("Failed to retrieve edit token for $page: " . $tok->{'error'});
return 60;
}
unless( exists( $tok->{'missing'} ) ) {
$api->log("$page already exists, nothing to do");
return $self->nexttime();
}
my $res = $api->query(
titles => $srcpage,
prop => 'revisions',
rvprop => 'content',
rvlimit => 1,
formatversion => 2,
);
if ( $res->{'query'}{'pages'}[0]{'missing'} // 0 ) {
$api->warn( "$srcpage is missing!" );
$api->whine( "[[$srcpage]] does not exist", "While attempting to create [[$page]], I found that [[$srcpage]] does not exist. Please create it!", Pagename => $talk );
return 60;
}
my $intxt = $res->{'query'}{'pages'}[0]{'revisions'}[0]{'content'} // '';
# Act like preloading does
my ($outtxt,$nowiki) = $api->strip_nowiki($intxt);
$outtxt = join( '', $outtxt=~m!<onlyinclude>(.*?)</onlyinclude>!sg ) if $outtxt=~m!<onlyinclude>.*?</onlyinclude>!s;
$outtxt =~ s!<noinclude>.*?</noinclude>!!sg;
$outtxt =~ s!</?includeonly>!!g;
$outtxt =~ s!<(?:includeonly|noinclude)\s*/>!!g;
$outtxt = $api->replace_nowiki($outtxt, $nowiki);
unless ( $outtxt=~s/^\s*\Q{{POTD {{{1|{{{style|default}}}}}}\E\s*\n/{{subst:POTD row\n/ ) {
$api->warn( "$srcpage doesn't begin with \"{{POTD {{{1|{{{style|default}}}}}}\"!\n$outtxt" );
$api->whine( "[[$srcpage]] has unexpected content", "While attempting to create [[$page]], I found that [[$srcpage]] does not begin with <code><nowiki>{{POTD {{{1|{{{style|default}}}}}}</nowiki></code>. Please fix it, or create [[$page]] manually.", Pagename => $talk );
return 60;
}
$res = $api->edit($tok, $outtxt, 'Creating protected version of a [[WP:POTD]] template', 0, 1);
if($res->{'code'} ne 'success'){
$api->warn("Write for $page failed: " . $res->{'error'});
return 60;
}
$api->log("Created $page");
return $self->nexttime();
}
sub nexttime {
my $t = 86400 - ( time % 86400 ) - 7200;
return $t < 60 ? 60 : $t;
}
1;