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

Content deleted Content added
AnomieBOT (talk | contribs)
Updating published sources: AnomieBOT::API: * Add support for non-persistent caching (i.e. memcached). d::IWNS, d::Nowiki, d::Redirects, d::WikiProjectTagging: * Use new cache support instead of caching things in memory. FlagIconRemover, IFDCloser, Nob...
AnomieBOT (talk | contribs)
Updating published sources: OnThisDayTagger: * Don't skip "1" for {{tl|ArticleHistory}}.
 
(8 intermediate revisions by the same user not shown)
Line 39:
'\d{4}',
'List of historical anniversaries',
'List of days of the year',
);
$skip_links_re=qr/^(?:$skip_links_re)$/;
Line 78 ⟶ 79:
my $screwup=' Errors? [[User:'.$api->user.'/shutoff/OnThisDayTagger]]';
 
$api->store->{'nextday'}=26405643 unless exists($api->store->{'nextday'});
 
my $starttime=time;
Line 176 ⟶ 177:
}
 
my $intxt=$tok->{'revisions'}[0]{'slots'}{'main'}{'*'} // '';
my $outtxt=$self->tag($api, $intxt, %dates);
return 300 unless defined($outtxt);
Line 242 ⟶ 243:
 
# Strip out non-rendered content
my ($txt, $nowiki)=$api->strip_nowiki($page->{'revisions'}[0]{'slots'}{'main'}{'*'});
while(my ($k,$v)=each %$nowiki){
$nowiki->{$k}='' if $v=~/^<!--/;
Line 251 ⟶ 252:
 
# Replace time-varying templates
$txt=~s/\{\{IsLeapYear\}\}/{{IsLeapYear|{{CURRENTYEAR}}}}/g;
my $x=day("%-d", $self->{'nextday'});
$txt=~s/\{\{CURRENTDAY\}\}/$x/g;
$x=day("%w", $self->{'nextday'});
$txt=~s/\{\{CURRENTDOW\}\}/$x/g;
$x=day("%m", $self->{'nextday'});
$txt=~s/\{\{CURRENTMONTH\}\}/$x/g;
$x=day("%Y", $self->{'nextday'});
$txt=~s/\{\{CURRENTYEAR\}\}/$x/g;
 
$txt=$api->replace_nowiki($txt, $nowiki);
Line 268 ⟶ 269:
title => $page->{'title'},
text => $txt,
if($tok->{'code'}prop ne => 'successwikitext'){,
);
if($res->{'code'} eq 'shutoff'){
Line 280 ⟶ 282:
 
# Transform ''' to <b>
$txt=doAllQuotes($api, $res->{'expandtemplates'}{'*wikitext'});
 
# Extract just the bold parts
Line 352 ⟶ 354:
 
# Tag the talk pages
foreach my $titleotitle (keys %res){
my $title="Talk:$titleotitle";
$api->log("Tagging $title for ".day("%F", $day));
my $tok=$api->edittoken("$title", EditRedirect => 1);
Line 359 ⟶ 361:
$api->warn("Task disabled: ".$tok->{'content'}."\n");
return 300;
} elsif($tok->{'code'} eq 'botexcluded'){
push @err, "* I am excluded from editing [[$title]], cannot tag for [[".join(']] / [[', @{$res{$otitle}})."]]";
if($tok->{'code'} ne 'success'){
next;
} elsif($tok->{'code'} ne 'success'){
$api->warn("Failed to get edit token for $title: ".$tok->{'error'}."\n");
return 60;
}
if(exists($tok->{'redirect'})){
push @err, "* [[$title]] is a redirect, cannot tag for [[".join(']] / [[', @{$res{$titleotitle}})."]]";
next;
}
 
my $intxt=$tok->{'revisions'}[0]{'slots'}{'main'}{'*'} // '';
my $outtxt=$self->tag($api, $intxt, $fday => $revid);
return 300 unless defined($outtxt);
Line 457 ⟶ 461:
rvlimit => 1,
rvprop => 'ids|timestamp|content',
rvslots => 'main',
rvdir => 'older',
rvstart => $start,
Line 606 ⟶ 611:
my %dates=@_;
 
my %redir=$api->redirects_toredirects_to_resolved('Template:On this day');
if(exists($redir{''})){
$api->warn("Could not load list of redirects to Template:On this day: ".$redir{''}{'error'}."\n");
Line 612 ⟶ 617:
}
 
my %redir2=$api->redirects_to_resolved('Template:Article history');
# Update an existing template?
if(exists($redir2{''})){
$api->warn("Could not load list of redirects to Template:Article history: ".$redir2{''}{'error'}."\n");
return undef;
}
 
# Update an existing OnThisDay template?
my $done=0;
$txt=$api->process_templates($txt, sub {
Line 651 ⟶ 662:
push @$params, "date$i=$date", "oldid$i=".$dates{$date};
$i++;
}
$done=1;
return "{{$oname|".join('|', @$params)."}}";
});
return $txt if $done;
 
# Update an existing ArticleHistory template?
$txt=$api->process_templates($txt, sub {
my $name=shift;
my $params=shift;
shift; # $wikitext
shift; # $data
my $oname=shift;
 
return unless exists($redir2{"Template:$name"});
 
# Find next unused number, and check which dates aren't already used
my %p=();
my $mx=0;
foreach (@$params){
$mx=$1 if(/^\s*otd(\d+)(?:date|oldid)\s*=/ && $mx < $1);
$mx=1 if(/^\s*otd(?:date|oldid)\s*=/ && $mx < 1);
if(/^\s*(otd\d*date)\s*=\s*(\d{4}-\d{2}-\d{2})\s*$/){
$p{$1}=$2;
} elsif(/^\s*(otd\d*date)\s*=\s*((?i)$monthre)\s+(\d{1,2})(?:\s*,)?\s+(\d{4})\s*$/){
my $m; for($m=0; $m<@months; $m++){ last if lc($months[$m]) eq lc($2); }
$p{$1}=sprintf("%04d-%02d-%02d", $4, $m+1, $3);
} elsif(/^\s*(otd\d*date)\s*=\s*(\d{1,2})\s+((?i)$monthre)(?:\s*,)?\s+(\d{4})\s*$/){
my $m; for($m=0; $m<@months; $m++){ last if lc($months[$m]) eq lc($3); }
$p{$1}=sprintf("%04d-%02d-%02d", $4, $m+1, $2);
} elsif(/^\s*(otd\d*oldid)\s*=\s*(\d+)\s*$/){
$p{$1}=$2;
}
}
 
# Delete any already-listed dates
delete $dates{$p{"otddate"}} if(exists($p{"otddate"}) && exists($p{"otdoldid"}) && exists($dates{$p{"otddate"}}));
for(my $i=1; $i<=$mx; $i++){
delete $dates{$p{"otd${i}date"}} if(exists($p{"otd${i}date"}) && exists($p{"otd${i}oldid"}) && exists($dates{$p{"otd${i}date"}}));
}
 
# Add new parameters
foreach my $date (sort keys %dates){
++$mx;
push @$params, "otd${mx}date=$date", "otd${mx}oldid=".$dates{$date}."\n";
}
$done=1;
Line 658 ⟶ 714:
 
if(!exists($self->{'loaded skip redirects'})){
my %skip=$api->redirects_toredirects_to_resolved(@skip_templates);
if(exists($skip{''})){
$api->warn("Could not load list of redirects for skip templates: ".$skip{''}{'error'}."\n");
Line 692 ⟶ 748:
 
$wikitext=_unstrip_templates($wikitext,$data);
my $tagtmp ="\x02".sha256_base64( $wikitext)."\x03";
utf8::encode( $tmp ) if utf8::is_utf8( $tmp );
my $tag="\x02".sha256_base64($tmp)."\x03";
$tag=~tr!+/=!-_!d;
$data->{$tag}=$wikitext;