User:AnomieBOT/source/tasks/TemplateTalkRedirectCreator.pm: Difference between revisions
Content deleted Content added
Updating published sources: TFDClerk, IFDCloser, PUICloser: * The "metadata" class was removed from the closing templates, so remove it from the is_closed regex * Add a sanity check to make sure the bot doesn't screw up if this sort of thing happens ag... |
Updating published sources: TemplateTalkRedirectCreator: * Update summary to mention /styles.css subpages. |
||
(11 intermediate revisions by the same user not shown) | |||
Line 1:
{{ombox|type=notice|text=
<syntaxhighlight lang="perl">
package tasks::TemplateTalkRedirectCreator;
Line 10:
Task: TemplateTalkRedirectCreator
BRFA: Wikipedia:Bots/Requests for approval/AnomieBOT 71
Status:
Created: 2013-10-08
Create redirects for non-existing talk pages of certain pages:
* Template pages ending in "/doc", "/sandbox", "/testcases", "/TemplateData", or "/
* Module pages ending in "/doc", "/sandbox", or "/styles.css"
=end metadata
Line 26:
use AnomieBOT::Task;
use Data::Dumper;
use Time::HiRes;
use vars qw/@ISA/;
@ISA=qw/AnomieBOT::Task/;
Line 35 ⟶ 36:
'%/testcases',
'%/TemplateData',
'%/styles.css',
],
'Module' => [
'%/doc',
'%/sandbox',
# Not %/testcases though, those talk pages are often (ab)used to run the tests.
'%/styles.css',
],
);
Line 43 ⟶ 48:
sub mapTitle {
my $title = shift;
return $1 if $title=~m!^(Template talk:.+)/(?:doc|sandbox|testcases|TemplateData|styles\.css)$!;
return $1 if $title=~m!^(Module talk:.+)/(?:doc|sandbox|styles\.css)$!;
return undef;
}
Line 58 ⟶ 63:
=for info
=cut
sub approved {
return
}
Line 79 ⟶ 84:
}
my $where = join( ' OR ', @where );
my ($dbh);
eval {
($dbh) = $api->connectToReplica( 'enwiki' );
};
if ( $@ ) {
$api->warn( "Error connecting to replica: $@\n" );
return 300;
}
my $cont = $self->{'dbcontinue'} // '';
# Spend a max of 5 minutes on this task before restarting
my $endtime=time()+300;
while (
return 0 if $api->halting;
# Load the list of redirects needing creation
my @rows;
my $t0 = Time::HiRes::time();
eval {
FROM page as p1
LEFT JOIN page as p2 ON( p2.page_namespace = p1.page_namespace + 1 and p2.page_title = p1.page_title )
WHERE p2.page_id IS NULL AND ( $where ) $cont
ORDER BY p1.page_namespace, p1.page_title
LIMIT 500
}, { Slice => {} } ) };
};
if ( $@ ) {
Line 103 ⟶ 119:
return 300;
}
my $t1 = Time::HiRes::time();
$api->log( 'DB query took ' . ($t1-$t0) . ' seconds' );
last unless @rows;
my %redirects = ();
for my $row (@rows) {
my $title = $rns{$row->{'ns'}+1} . ':' . $row->{'title'};
$title =~ s/_/ /g;
Line 118 ⟶ 137:
$redirects{$title} = $basetitle;
}
if ( %redirects ) {
# Bypass double redirects and remove missing target pages my $res = $api->query(
titles => join('|', values %redirects),
redirects => 1
);
if($res->{'code'} ne 'success'){
$api->warn("Failed to retrieve redirect list: ".$res->{'error'}."\n");
return 60;
}
}
$map{$_->{'from'}} = $_->{'to'} foreach @{$res->{'query'}{'redirects'}};
}
if (
$exists{$p->{'title'}} = 1 if $p->{'pageid'}//0;
}
}
$target = $map{$target};
$redirects{$redir} = $target;
if ( exists( $seen{$target} ) ) {
$api->warn("Redirect loop involving [[$target]]");
delete $redirects{$redir};
last;
}
$seen{$target}=1;
}
delete $redirects{$redir} unless exists( $exists{$target} );
}
while( my ($
return 0 if $api->halting;
my $tok=$api->edittoken($redir, EditRedir => 1);
if($
$api->warn("Failed to get edit token for $redir: ".$tok->{'error'}."\n");
next;
}
if ( !exists($tok->{'missing'} ) ) {
$api->log("$redir already exists, skipping");
next;
}
my $txt = "#REDIRECT [[$target]]\n\n{{Redirect category shell|\n{{R from remote talk page}}\n}}";
my $summary="Redirecting to [[$target]] to avoid decentralized discussion";
# Create page
$api->log("$summary in $redir");
my $r = $api->edit($tok, $txt, $summary, 0, 1);
if($r->{'code'} ne 'success'){
$api->warn("Write failed on $redir: ".$r->{'error'}."\n");
next;
}
# If we've been at it long enough, let another task have a go.
return 0 if time()>=$endtime;
}
}
# On the next time around, skip any we've already processed this run
my ($ns, $title) = @{$rows[$#rows]}{'ns','title'};
$title = $dbh->quote( $title );
$cont = " AND (p1.page_namespace > $ns OR p1.page_namespace = $ns AND p1.page_title > $title)";
$self->{'dbcontinue'} = $cont;
# If we've been at it long enough, let another task have a go.
return 0 if time()>=$endtime;
}
$self->{'dbcontinue'} = '';
return 21600;
|