User:AnomieBOT/source/tasks/POTDPageCreator.pm

. 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 " ]; }

push @edits, [ $page, $tok, $outtxt, "Creating protected version of a WP:POTD template by 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 {{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  or  . 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'){ $api->warn("I'm too late for $page"); 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( "$mainpage 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); }

my $res = $api->query(       titles => $srcpage,        prop => 'revisions',        rvprop => 'content',        rvslots => 'main',        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 $mainpage, I found that $srcpage does not exist. Please create it!", Pagename => $talk ); return (60, 1, undef, undef); }

my $intxt = $res->{'query'}{'pages'}[0]{'revisions'}[0]{'slots'}{'main'}{'content'} // ''; # Act like preloading does my ($outtxt,$nowiki) = $api->strip_nowiki($intxt); $outtxt = join( '', $outtxt=~m! (.*?) !sg ) if $outtxt=~m! .*? !s; $outtxt =~ s! .*? !!sg; $outtxt =~ s!!!g; $outtxt =~ s!<(?:includeonly|noinclude)\s*/>!!g; $outtxt = $api->replace_nowiki($outtxt, $nowiki);

return (0, 0, $tok, $outtxt); }

sub nexttime { my $t = 86400 - ( time % 86400 ) - 7200; return $t < 60 ? 60 : $t; }

1;