User:AnomieBOT/source/tasks/EditorReviewArchiver.pm

.");                   next;                }                my $dt=strftime("%e %B %Y", gmtime(time+$notification_days*86400));                $dt=~s/^\s+//;                $res=$api->whine('Automatic processing of your editor review', "This is an automated message. Your editor review is scheduled to be closed on $dt because it will have been open for more than $archive_days days and inactive for more than $inactive_days days. You can keep it open longer by posting a comment to the review page requesting more input. Adding  to the review page will prevent further automated actions. ", Summary=>"Your review is scheduled to be closed on $dt", Pagename=>"User talk:$user", NoSmallPrint=>1);               if($res->{'code'} eq 'shutoff'){                    $api->warn("Task disabled: ".$res->{'content'}."\n");                    return 300;                }                if($res->{'code'} eq 'pageprotected' || $res->{'code'} eq 'botexcluded'){                    # If they really don't want messages, let them.                    $api->warn("Cannot notify $user: ".$res->{'error'}."\n");                } elsif($res->{'code'} ne 'success'){                    $api->warn("Failed to get edit User talk:$user: ".$res->{'error'}."\n");                    $broken=1;                    next;                }                $rdata->{'notified'}=time;                $api->store->{$rdata->{'name'}}=$rdata;            } else {                # Time to close?                next unless $rdata->{'notified'}+$notification_days*86400 < time; next unless $t+$inactive_days*86400 < time;

# Mark for archival and removal from the main page $archive{$rdata->{'page'}}=[ (gmtime $rdata->{'timestamp'})[5]+1900, { page=>$rdata->{'page'}, name=>$rdata->{'name'}, txt=>', '.strftime('%e %B %Y', gmtime $rdata->{'timestamp'}) } ]; $api->store->{'archive'}=\%archive; }       }    }

# reconstruct the main page my $r=join("\n\n", map "", grep !exists($archive{$_->{'page'}}), @requests); unless($outtxt=~s/.*/$1\n$r\n$2/s){ $api->whine('WP:Editor review is broken', 'In order to process WP:Editor review, I rely on the comments  and   to know where the reviews are supposed to go. Please fix it!'); return 300; }   if($intxt ne $outtxt){ my @summary=; push @summary, "listing $new new review".($new==1?'':'s') if $new; my $ct=scalar keys %archive; push @summary, "archiving $ct closed review".($ct==1?'':'s') if $ct; push @summary, "removing $rmredir redirect".($rmredir==1?'':'s') if $rmredir; push @summary, 'correcting page formatting'; $summary[$#summary]='and '.$summary[$#summary] if @summary>1; my $summary=ucfirst(join(@summary>2?', ':' ', @summary)); $res=$api->edit($tok, $outtxt, "$summary. $screwup", 0, 0); if($res->{'code'} ne 'success'){ $api->warn("Edit of WP:Editor review failed: ".$res->{'error'}."\n"); return 60; }   }

# Need to archive? if(%archive){ # First, split archivals by year my %years=; while(my ($page,$x)=each %archive){ $years{$x->[0]}{$x->[1]{'page'}}=$x->[1]; }

while(my ($year,$data)=each %years){ my $tok=$api->edittoken("Wikipedia:Editor review/Archive ($year)"); if($tok->{'code'} eq 'shutoff'){ $api->warn("Task disabled: ".$tok->{'content'}."\n"); return 300; }           if($tok->{'code'} ne 'success'){ $api->warn("Failed to get edit token for Wikipedia:Editor review/Archive ($year): ".$tok->{'error'}."\n"); return 60; }           my $ct=scalar keys %$data;

# Load or create archive page text my $intxt=$tok->{'revisions'}[0]{'*'} // "\n\nThese are links to reviews of editors from Editor review from $year. They are in alphabetical order and include multiple submissions. The date listed is the date of the original submission by the editor.\n\n\n"; my $outtxt=$intxt; $outtxt=~s/\s*\n==.*/\n/s;

# Load all existing links from the page my @l=($intxt=~m!(?<=\n)(?:[:*]+\s*)\[\[((?i:Wikipedia|WP):[eE]ditor[ _]review/([^|\]]+))(?:\|[^]]+)?\]\]([^\n]*)!g); for(my $i=0; $i<@l; $i+=3){ $l[$i]=~s/_/ /g; $l[$i+1]=~s/_/ /g; $data->{$l[$i]}={ page=>$l[$i], name=>$l[$i+1], txt=>$l[$i+2] }; }

# Sort the mess, and reconstruct the page @l=sort { uc($a->{'name'}) cmp uc($b->{'name'}) } values %$data; my ($x,$k,$prev)=(,,''); foreach (@l) { if($k ne substr(uc($_->{'name'}),0,1)){ $k=substr(uc($_->{'name'}),0,1); $outtxt.="\n==$k==\n"; }               ($x=$_->{'name'})=~s/\s*\(\d+\)$//; $outtxt.='*' if $x eq $prev; $outtxt.="*$_->{name}$_->{txt}\n"; $prev=$x; }

if($intxt ne $outtxt){ $res=$api->edit($tok, $outtxt, "Archiving $ct closed review".($ct==1?'':'s').". $screwup", 0, 0); if($res->{'code'} ne 'success'){ $api->warn("Edit of WP:Editor review/Archive ($year) failed: ".$res->{'error'}."\n"); return 60; }           }        }

# Done archiving, clear store delete $api->store->{'archive'}; }

# Save timestamp and broken flag $self->{'lasttime'}=$starttime; $self->{'broken'}=$broken; $api->store->{'lasttime'}=$starttime; $api->store->{'broken'}=$broken; return $starttime+($broken?600:7200)-time; }

1;