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: * Use continuation in database query, so the bot doesn't wind up uselessly looping forever if more than 500 invalid pages pile up at the beginning of the list. |
||
Line 79:
}
my $where = join( ' OR ', @where );
my ($dbh, $schema);
eval {
($dbh, $schema) = $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;
Line 96 ⟶ 100:
my @rows;
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 107 ⟶ 117:
my %redirects = ();
for my $row (@rows) {
my $title = $rns{$row->{'ns'}+1} . ':' . $row->{'title'};
$title =~ s/_/ /g;
Line 118 ⟶ 128:
$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 $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;
|