User:AnomieBOT/source/tasks/POTDPageCreator.pm: Difference between revisions

Content deleted Content added
AnomieBOT (talk | contribs)
Updating published sources: POTDPageCreator: * In trial, 10 days.
AnomieBOT (talk | contribs)
Updating published sources: POTDPageCreator: * Adjust regex, people have started not leaving a newline.
 
(8 intermediate revisions by the same user not shown)
Line 1:
{{ombox|type=notice|text= ApprovalBRFA requestedapproved 2017-02-10<br />[[Wikipedia:Bots/Requests for approval/AnomieBOT 77]]}}
<syntaxhighlight lang="perl">
package tasks::POTDPageCreator;
Line 10:
Task: POTDPageCreator
BRFA: Wikipedia:Bots/Requests for approval/AnomieBOT 77
Status: InApproved trial2017-02-10
Created: 2017-01-29
 
Line 29:
use POSIX qw/strftime/;
use Data::Dumper;
 
my $talk = 'Wikipedia talk:Picture of the day';
 
sub new {
Line 40 ⟶ 42:
 
=for info
ApprovalBRFA requestedapproved 2017-02-10<br />[[Wikipedia:Bots/Requests for approval/AnomieBOT 77]]
 
=cut
 
sub approved {
return 32;
}
 
Line 51 ⟶ 53:
my ($self, $api)=@_;
 
$api->task('POTDPageCreator', 0, 10, qw(d::Talk d::TrialNowiki));
 
my $trial = $api->check_trial( 1486560586, 'AnomieBOT 77' );
return $$trial if $trial;
 
my @now = gmtime;
Line 64 ⟶ 63:
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 @edits = ();
 
my ($res, $fatal, $tok, $outtxt) = $self->process( $api, $page, $page, $srcpage );
return $$trialres if $trialres;
 
if ( $outtxt=~s!^\s*\Q{{POTD/$date/{{#invoke:random|number|\E(\d+)\Q}}|{{{1|{{{style|default}}}}}}}}\E\s*!{{POTD protected/$date/{{#invoke:random|number|$1}}|{{{1|{{{style|default}}}}}}}}! ) {
returnmy 60$n = $1;
for ( my $i = 1; $i <= $n; $i++ ) {
my $srcpage2 = $srcpage . '/' . $i;
my $page2 = $page . '/' . $i;
my ($res2, $fatal, $tok2, $outtxt2) = $self->process( $api, $page, $page2, $srcpage2 );
return $res2 if $res2 && $fatal;
next if $res2;
 
unless ( $txtouttxt2=~s/^\s*\Q{{POTD {{{1|{{{style|default}}}}}}\E\s*\n/{{subst:POTD row|date=$date\n/ ) {
$api->warn( "$srcpagesrcpage2 doesn't begin with \"{{POTDexpected {{{1|{{{style|default}}}}}}\"!wikitext\n$txtouttxt2" );
$api->whine( "[[$srcpagesrcpage2]] has unexpected content", "While attempting to create [[$page]], I found that [[$srcpagesrcpage2]] does not begin with <code><nowiki>{{POTD {{{1|{{{style|default}}}}}}</nowiki></code>. Please fix it, or create [[$page]] and its subpages manually.", Pagename => $talk );
return 60;
}
 
push @edits, [ $page2, $tok2, $outtxt2, "Creating protected version of a [[WP:POTD]] template by substing from {{[[$srcpage2|POTD/$date/$i]]|row}}" ];
}
 
$res = push @edits, [ $api->edit(page, $tok, $txtouttxt, '"Creating protected version of a [[WP:POTD]] template', 0,by 1)adjusting [[$srcpage]]" ];
} elsif ( $outtxt=~s/^\s*\Q{{POTD {{{1|{{{style|default}}}}}}\E\s*\n/{{subst:POTD row\n/ ) {
push @edits, [ $page, $tok, $outtxt, "Creating protected version of a [[WP:POTD]] template by substing from {{[[$srcpage|POTD/$date]]|row}}" ];
} else {
$api->warn( "$srcpage doesn't begin with expected wikitext\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> or <code><nowiki>{{POTD/$date/{{#invoke:random|number|<N>}}|{{{1|{{{style|default}}}}}}}}</nowiki></code>. Please fix it, or create [[$page]] manually.", Pagename => $talk );
return 60;
}
 
foreach my $edit (@edits) {
my ($page, $tok, $outtxt, $summary) = @$edit;
my $res = $api->edit($tok, $outtxt, $summary, 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 process {
my ( $self, $api, $mainpage, $page, $srcpage ) = @_;
 
my $tok = $api->edittoken( $page, EditRedir => 1 );
if($tok->{'code'} eq 'shutoff'){
$api->warn("Task disabled: " . $tok->{'content'} . "\n");
return (300, 1, undef, undef);
}
if($tok->{'code'} eq 'pageprotected'){
Line 77 ⟶ 122:
my $res = $api->query( titles => $page, formatversion => 2 );
if ( $res->{'query'}{'pages'}[0]{'missing'} // 0 ) {
my $andsub = $mainpage eq $page ? '' : ' and its subpages';
$api->whine( "[[$pagemainpage]] was not created", "It seems that I'm too late to create [[$page]], it's already protected. Please create it$andsub manually.", Pagename => $talk );
return ($self->nexttime(), 1, undef, undef);
}
return ($self->nexttime(), 0, undef, undef);
}
if($tok->{'code'} ne 'success'){
$api->warn("Failed to retrieve edit token for $page: " . $tok->{'error'});
return (60, 1, undef, undef);
}
unless( exists( $tok->{'missing'} ) ) {
$api->log("$page already exists, nothing to do");
return ($self->nexttime(), 0, undef, undef);
}
 
Line 94 ⟶ 141:
prop => 'revisions',
rvprop => 'content',
rvslots => 'main',
rvlimit => 1,
formatversion => 2,
Line 99 ⟶ 147:
if ( $res->{'query'}{'pages'}[0]{'missing'} // 0 ) {
$api->warn( "$srcpage is missing!" );
$api->whine( "[[$srcpage]] does not exist", "While attempting to create [[$pagemainpage]], I found that [[$srcpage]] does not exist. Please create it!", Pagename => $talk );
return (60, 1, undef, undef);
}
my $txt = $res->{'query'}{'pages'}[0]{'revisions'}[0]{'content'} // '';
unless ( $txt=~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$txt" );
$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;
}
 
my $txtintxt = $res->{'query'}{'pages'}[0]{'revisions'}[0]{'slots'}{'main'}{'content'} // '';
$res = $api->edit($tok, $txt, 'Creating protected version of a [[WP:POTD]] template', 0, 1);
# Act like preloading does
if($res->{'code'} ne 'success'){
my ($outtxt,$nowiki) = $api->strip_nowiki($intxt);
$api->warn("Write for $page failed: " . $res->{'error'});
$outtxt = join( '', $outtxt=~m!<onlyinclude>(.*?)</onlyinclude>!sg ) if $outtxt=~m!<onlyinclude>.*?</onlyinclude>!s;
return 60;
$outtxt =~ s!<noinclude>.*?</noinclude>!!sg;
}
$outtxt =~ s!</?includeonly>!!g;
$outtxt =~ s!<(?:includeonly|noinclude)\s*/>!!g;
$outtxt = $api->replace_nowiki($outtxt, $nowiki);
 
$api->logreturn ("Created0, 0, $tok, $page"outtxt);
return $self->nexttime();
}