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

Content deleted Content added
AnomieBOT (talk | contribs)
Updating published sources: General: * Update for the addition of 'rvslots'. DatedCategoryDeleterTest: * Disable. It's clear that task won't be needed. BrokenRedirectDeleter: * Handle pages with newlines before the <code>#REDIRECT</code>.
AnomieBOT (talk | contribs)
Updating published sources: TFATitleSubpageCreator: * Update talk page message about a missing subpage, and add checking for the date being missing from Template:TFA title/data.json too.
 
(8 intermediate revisions by the same user not shown)
Line 1:
{{ombox|type=notice|text= Approved 2010-05-31<br />[[Wikipedia:Bots/Requests for approval/AnomieBOT II]]}}
{{ombox|type=notice|text= First supplemental BFRA, approved 2023-01-08<br />[[Wikipedia:Bots/Requests for approval/AnomieBOT II 4]]}}
<syntaxhighlight lang="perl">
package tasks::TFATitleSubpageCreator;
Line 11 ⟶ 12:
BRFA: Wikipedia:Bots/Requests for approval/AnomieBOT II
Status: Approved 2010-05-31
+BRFA: Wikipedia:Bots/Requests for approval/AnomieBOT II 4
+Status: Approved 2023-01-08
Created: 2010-05-26
 
Line 23 ⟶ 26:
 
use AnomieBOT::Task qw/:time/;
use Time::Piece;
use vars qw/@ISA/;
@ISA=qw/AnomieBOT::Task/;
 
my $jsontitle = 'Template:TFA title/data.json';
 
sub new {
Line 37 ⟶ 43:
=for info
Approved 2010-05-31<br />[[Wikipedia:Bots/Requests for approval/AnomieBOT II]]
 
=for info
First supplemental BFRA, approved 2023-01-08<br />[[Wikipedia:Bots/Requests for approval/AnomieBOT II 4]]
 
=cut
Line 48 ⟶ 57:
my $res;
 
$api->task('TFATitleSubpageCreator', 0, 10, qw/d::Talk d::Templates/);
 
my ( $jsontok, $json );
my @jsonupdates = ();
if ( $jsontitle ) {
$jsontok = $api->edittoken( $jsontitle );
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 $jsontitle: ".$jsontok->{'error'}."\n");
return 300;
}
 
eval { $json = JSON->new->decode( $jsontok->{'revisions'}[0]{'slots'}{'main'}{'*'} // '' ); };
myif ( $link=$1;@ ) {
$api->warn("Failed to parse $jsontitle: $@\n");
return 300;
}
}
 
my $broken = 0;
Line 79 ⟶ 108:
my $template="Template:TFA title/$date";
 
unless(my $link = undef;
$_api->{'revisions'}[0]{'slots'}{'main'}{'*'}=~/'''\s*\[\[\s*process_templates([^|\]]+?)\s*(?:\|[^]]+)?\]\][a-z]*\s*'''/ || $_->{'revisions'}[0]{'slots'}{'main'}{'*'}=~m!<b>\s*\[\[\s*([^|\]]+?)\s*(?:\|[^]]+)?\]\]\s*</b>!, ||sub {
my $name = shift;
$_->{'revisions'}[0]{'slots'}{'main'}{'*'}=~/\[\[\s*([^|\]]+?)\s*\|\s*'''[^]]+'''\s*\]\]/ || $_->{'revisions'}[0]{'slots'}{'main'}{'*'}=~m!\[\[\s*([^|\]]+?)\s*\|\s*<b>[^]]+</b>\s*\]\]!
){ my $params = shift;
$api->warn("Noreturn FAundef link inunless $title?\n")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;
}
 
my $link=$1;
# 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
Line 120 ⟶ 157:
next;
}
}
 
if ( $jsontitle ) {
my $dt = Time::Piece->strptime( $date, '%B %d, %Y' )->strftime( '%Y-%m-%d' );
my $cur = $json->{'titles'}{$dt} // '';
if ( ref( $cur ) eq '' && $link ne $cur ) {
$json->{'titles'}{$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 $jsontitle: " . $r->{'error'} . "\n" );
$broken = 1;
}
}
Line 147 ⟶ 202:
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{{tl|TFAFULL}} article linktemplate in [[$fa]] in order to populate [[$title]]. Please correct the link{{tl|TFAFULL}} or create the template[[$title]] manually.");
$broken = 1;
}
}
 
my @missingdates=();
@t=gmtime();
@t=@t[3..5];
my $dt;
my $i = 1;
do {
$dt = strftime('%Y-%m-%d', gmtime(timegm(0,0,0,$t[0]+$i,$t[1],$t[2])));
if ( ! defined($json->{'titles'}{$dt}) ) {
my $dt2 = strftime('%B %d, %Y', gmtime(timegm(0,0,0,$t[0]+$i,$t[1],$t[2])));
push @missingdates, "[[Wikipedia:Today's featured article/$dt2|$dt]]";
}
$i--;
} while( $dt ne '2004-02-22' );
if ( @missingdates ) {
$api->whine("Missing dates in [[Template:TFA title/data.json]]", "Help! I seem to have been unable to find the FAs for some dates to enter into [[Template:TFA title/data.json]]. Please add them manually. The missing dates are: " . join( " ", @missingdates ) . ".");
}