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

Content deleted Content added
AnomieBOT (talk | contribs)
Updating published sources: OnThisDayTagger: * Finished processing the list, change to live mode for 7-day trial. * Track the maximum oldid/date parameter used, and complain if {{tl|OnThisDay}} can't handle it. * Include the date in the edit summary
AnomieBOT (talk | contribs)
Updating published sources: OnThisDayTagger: * Don't skip "1" for {{tl|ArticleHistory}}.
 
(22 intermediate revisions by the same user not shown)
Line 1:
{{ombox|type=notice|text= InApproved trial2009-05-26<br />[[Wikipedia:Bots/Requests for approval/AnomieBOT 30]]}}
<sourcesyntaxhighlight lang="perl">
package tasks::OnThisDayTagger;
 
Line 7:
=begin metadata
 
Bot: AnomieBOT
Task: OnThisDayTagger
BRFA: Wikipedia:Bots/Requests for approval/AnomieBOT 30
Status: InApproved trial2009-05-26
Rate: Max 6 edits/minute
Created: 2009-05-14
 
Line 25:
use strict;
 
use AnomieBOT::Task qw/:time/;
use Data::Dumper;
use POSIX;
Line 39:
'\d{4}',
'List of historical anniversaries',
'List of days of the year',
);
$skip_links_re=qr/^(?:$skip_links_re)$/;
Line 56 ⟶ 57:
$self->{'order'}=100;
$self->{'nextday'}=0;
$self->{'redir'}={};
bless $self, $class;
return $self;
Line 64:
 
=for info
InApproved trial2009-05-26<br />[[Wikipedia:Bots/Requests for approval/AnomieBOT 30]]
 
=cut
 
sub approved {
return 42;
}
 
Line 76:
my $res;
 
$api->task('OnThisDayTagger', 0, 10, qw/d::TrialTalk d::Timestamp d::Templates d::Redirects/);
my $screwup=' Errors? [[User:'.$api->user.'/shutoff/OnThisDayTagger]]';
 
$api->store->{'maxotdnextday'}=15643 unless exists($api->store->{'maxotdnextday'});
$api->store->{'nextday'}=1904 unless exists($api->store->{'nextday'});
 
my $x=$api->check_trial(1243457635, 'AnomieBOT 30');
return $$x if $x;
 
my $starttime=time;
my $today=day_from_timestamp($starttime);
my %redir=%{$self->{'redir'}};
 
if($mode eq 'list'){
Line 96 ⟶ 91:
for(my $d=1; $d<=31; $d++){
my $md=sprintf("%02d-%02d",$m,$d);
next unless POSIX::strftime("%m-%d",0,0,0,$d,$m-1,100) eq $md;
my $res=$api->query([],
titles => POSIX::strftime("Wikipedia:Selected anniversaries/%B %-d",0,0,0,$d,$m-1,100),
prop => 'revisions',
rvprop => 'ids|timestamp',
Line 182 ⟶ 177:
}
 
my $intxt=exists($tok->{'revisions'}[0]{'*slots'})?$tok->{'revisionsmain'}[0]{'*'}: // '';
my $outtxt=$self->tag($api, $intxt, %dates);
return 300 unless defined($outtxt);
if($intxt ne $outtxt){
$res=$api->edit($tok, $outtxt, "Adding/updating {{OnThisDay}}. $screwup", 0, 1);
Line 194 ⟶ 190:
}
close X;
 
my $res=check_maxotd($api);
return $res if $res;
 
return undef;
Line 250 ⟶ 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=~/^<!--/;
}
$txt=$1 if $txt=~m!<onlyinclude>(.*?)</onlyinclude>!s;
$txt=~s!<noinclude>(.*?)</noinclude>!!ggs;
$txt=~s!</?includeonly>!!g;
 
# 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 276 ⟶ 269:
title => $page->{'title'},
text => $txt,
prop => 'wikitext',
);
if($res->{'code'} eq 'shutoff'){
Line 288 ⟶ 282:
 
# Transform ''' to <b>
$txt=doAllQuotes($api, $res->{'expandtemplates'}{'*wikitext'});
 
# Extract just the bold parts
Line 338 ⟶ 332:
my %pages=map { $_->{'title'}=>$_ } values %{$res->{'query'}{'pages'}};
foreach my $l (@l){
my $t=$api->apply_redirect_map( $l, \%map );
$t=$map{$t} while(exists($map{$t}));
if(!exists($pages{$t})){
$api->warn("No result for $l ".day("(%F)", $day)."\n");
Line 361 ⟶ 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 368 ⟶ 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=exists($tok->{'revisions'}[0]{'*slots'})?$tok->{'revisionsmain'}[0]{'*'}: // '';
my $outtxt=$self->tag($api, $intxt, $fday => $revid);
return 300 unless defined($outtxt);
if($intxt ne $outtxt){
$res=$api->edit($tok, $outtxt, "Adding/updating {{OnThisDay}} for $fday. $screwup", 0, 1);
Line 407 ⟶ 403:
$api->store->{"nextday"}=++$self->{'nextday'};
}
 
my $res=check_maxotd($api);
return $res if $res;
 
my $next=86400-($starttime%86400)-(time-$starttime);
Line 420 ⟶ 413:
 
# The anniversary pages were created 2004-02-26 through 2004-02-28; We count
# 2004-02-26 as day 0. This function does POSIX::strftime on the day number.
# Thankfully, it correctly converts
sub day {
my $fmt=shift;
my $day=shift;
return POSIX::strftime($fmt,0,0,0,$day+26,1,104);
}
 
Line 431 ⟶ 424:
my $ts=shift;
my ($min,$max)=(0,5000);
my $target=POSIX::strftime("%F", gmtime $ts);
while(1){
my $day=day("%F",$max);
Line 464 ⟶ 457:
 
my $start=$api->ISO2timestamp(day("%Y-%m-%dT00:00:00Z",$day+1));
my %q$iter=$api->iterator(
titles => day("Wikipedia:Selected anniversaries/%B %-d",$day),
rvlimit => 1,
rvprop => 'ids|timestamp|content',
rvslots => 'main',
rvdir => 'older',
rvstart => $start,
prop => 'revisions',
);
while(1my $page=$iter->next){
myreturn $res=page unless $apipage->query(%q){'_ok_'};
return $res unless $res->{'code'} eq 'success';
my $page=(values(%{$res->{'query'}{'pages'}}))[0];
$page->{'code'}='success';
my $t=$api->ISO2timestamp($page->{'revisions'}[0]{'timestamp'});
Line 481 ⟶ 473:
$api->store->{"day $day"}=$page;
$api->store->{"day $day"}{'cached'}=1;
$respage->{'cached'}=0;
return $page;
}
return undef unless exists($res->{'query-continue'});
$q{'rvstartid'}=$res->{'query-continue'}{'revisions'}{'rvstartid'};
delete $q{'rvstart'};
}
}
Line 614 ⟶ 603:
$output.="<b><i>$buffer</i></b>" if($state eq 'both' && $buffer ne '');
return $output;
}
 
sub check_maxotd {
my $api=shift;
 
# Load template content
my $res=$api->rawpage('Template:OnThisDay');
if($res->{'code'} eq 'shutoff'){
$api->warn("Task disabled: ".$res->{'content'}."\n");
return 300;
}
if($res->{'code'} ne 'success'){
$api->warn("Could not load Template:OnThisDay: ".$res->{'error'}."\n");
return 60;
}
$res=$res->{'content'};
 
# Count date/oldid parameters available
my $i=1;
$i++ while(index($res, "{{{date$i|}}}")>0 && index($res, "{{{oldid$i|}}}")>0);
$i--;
 
# Check if it's enough
my $max=$api->store->{'maxotd'};
return 0 if $max<=$i;
 
# No, complain
$api->log("Whining about insufficient parameters in {{OnThisDay}}");
my $summ="{{[[Template:OnThisDay]]}} needs more parameters";
$res=$api->whine($summ, "It appears that {{tl|OnThisDay}} only supports parameters up to <nowiki>{{{date$i}}} and {{{oldid$i}}}</nowiki>, and I have seen at least one page that needs $max. Please edit the template to include more calls to {{tl|OnThisDay/link}}.", Summary => $summ, Pagename => $whine_to);
if($res->{'code'} eq 'shutoff'){
$api->warn("Task disabled: ".$res->{'content'}."\n");
return 300;
}
if($res->{'code'} ne 'success'){
$api->warn("Could not complain about {{OnThisDay}}: ".$res->{'error'}."\n");
return 60;
}
return 0;
}
 
Line 661 ⟶ 611:
my %dates=@_;
 
my %redir=%{$selfapi->{redirects_to_resolved('redirTemplate:On this day'}});
if(!%exists($redir{''})){
%redir=$api->redirects_towarn('"Could not load list of redirects to Template:OnThisDayOn this day: ".$redir{''}{'error'}."\n");
if(exists($redir{''})){return undef;
}
$api->warn("Could not load list of redirects to Template:OnThisDay: ".$redir{''}{'error'}."\n");
 
return undef;
my %redir2=$api->redirects_to_resolved('Template:Article history');
}
if(exists($self->redir2{'redir'}=\%redir;)){
$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 711 ⟶ 663:
$i++;
}
$done=1;
$api->store->{'maxotd'}=$i-1 if $api->store->{'maxotd'}<$i-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;
return "{{$oname|".join('|', @$params)."}}";
Line 718 ⟶ 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 734 ⟶ 730:
$i++;
}
$api->store->{'maxotd'}=$i-1 if $api->store->{'maxotd'}<$i-1;
$templ.="}}";
 
Line 753 ⟶ 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;
Line 763 ⟶ 760:
my $templ=shift;
 
$wikitext=~s!(\x02[a-zA-Z0-9_-]+\x03)! exists($templ->{$1})?$templ->{$1}: // $1 !gioe;
return $wikitext;
}
Line 769 ⟶ 766:
1;
 
</syntaxhighlight>
</source>