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

Content deleted Content added
AnomieBOT (talk | contribs)
Updating published sources: IMONumberRedirectCreator: * Approved!
AnomieBOT (talk | contribs)
Updating published sources: IMONumberRedirectCreator: * Write conflicting numbers to User:AnomieBOT/IMO number conflicts
 
(3 intermediate revisions by the same user not shown)
Line 13:
Created: 2015-04-02
 
Create redirects for [[IMO numbers]], based on categorization in [[:Category:IMO Numbernumbers]].
 
=end metadata
Line 66:
$iter = $api->iterator(
list => 'categorymembers',
cmtitle => 'Category:IMO Numbernumbers',
cmprop => 'title|sortkeyprefix',
cmlimit => 'max100',
);
$self->{'iter'} = $iter;
$self->{'conflicts'} = {};
$self->{'doconflicts'} = 1;
}
 
Line 77 ⟶ 79:
if(!$n->{'_ok_'}) {
$api->warn("Failed to retrieve category members: " . $n->{'error'} . "\n");
$self->{'doconflicts'} = 0;
return 60;
}
Line 90 ⟶ 93:
if($tok->{'code'} eq 'shutoff') {
$api->warn("Task disabled: " . $tok->{'content'} . "\n");
$self->{'doconflicts'} = 0;
return 300;
}
if($tok->{'code'} ne 'success') {
$api->warn("Failed to get edit token for $title: " . $tok->{'error'} . "\n");
$self->{'doconflicts'} = 0;
next;
}
Line 103 ⟶ 108:
} else {
# Check if it's a redirect
my $txt = $tok->{'revisions'}[0]{'slots'}{'main'}{'*'};
unless($txt=~/$redir\[\[\s*(.+?)\s*\]\]/) {
$api->log("Skipping $title: Not a redirect");
$self->{'conflicts'}{$title}{$target} = 1;
$self->{'conflicts'}{$title}{$title} = 1;
next;
}
Line 117 ⟶ 124:
redirects => 1,
prop => 'categories',
clcategories => 'Category:IMO Numbernumbers',
clprop => 'sortkey',
cllimit => 'max',
Line 123 ⟶ 130:
if($res->{'code'} ne 'success') {
$api->warn("Could not resolve target page $target2: " . $res->{'error'} . "\n");
$self->{'doconflicts'} = 0;
return 60;
}
Line 131 ⟶ 139:
# Target page doesn't exist, so we can safely overwrite it
$api->log("Overwriting $title => $target2: target doesn't exist");
} elsif(!(($c)=grep { $_->{'title'} eq 'Category:IMO Numbernumbers' } @{$p->{'categories'} // []})) {
# Target page doesn't have the IMO Number template, so we can safely overwrite it
$api->log("Overwriting $title => $target2: target doesn't have [[Category:IMO Numbernumbers]]");
} elsif($c->{'sortkeyprefix'} ne $number) {
# Target page doesn't have the IMO Number template, so we can safely overwrite it
$api->log("Overwriting $title => $target2: target has [[Category:IMO Numbernumbers]] pointing to IMO " . $c->{'sortkeyprefix'});
} elsif(exists($res->{'query'}{'redirects'})) {
# Target page is a redirect. Overwrite if it's a double redir to our target.
if($res->{'query'}{'redirects'}[0]{'to'} ne $target) {
$api->log("Skipping $title => $target: Already a double redirect to " . $res->{'query'}{'redirects'}[0]{'to'});
$self->{'conflicts'}{$title}{$target} = 1;
$self->{'conflicts'}{$title}{$target2} = 1;
next;
}
Line 147 ⟶ 157:
if(!exists($res->{'query'}{'normalized'}) || $res->{'query'}{'normalized'}[0]{'to'} ne $target) {
$api->log("Skipping $title => $target: Already a redirect to $target2");
$self->{'conflicts'}{$title}{$target} = 1;
$self->{'conflicts'}{$title}{$target2} = 1;
}
next;
Line 165 ⟶ 177:
}
 
if ( $self->{'doconflicts'} ) {
my %conflicts = %{$self->{'conflicts'}};
my $txt = "List of IMO numbers being claimed by multiple articles (via {{tl|IMO Number}} / [[:Category:IMO numbers]])\n\n";
$txt .= "This page is updated by a bot. Any changes will be overwritten periodically.\n\n";
if ( %conflicts ) {
for my $n (sort keys %conflicts) {
$txt .= "* [[$n]]: " . join( ', ', map { "[[$_]]" } sort keys %{$conflicts{$n}} ) . "\n";
}
} else {
$txt .= "There are no conflicts at this time.\n";
}
my $tok = $api->edittoken("User:AnomieBOT/IMO number conflicts", EditRedir => 1);
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 User:AnomieBOT/IMO number conflicts: " . $tok->{'error'} . "\n");
next;
}
if ( $txt ne ($tok->{'revisions'}[0]{'*'}//'') ) {
my $res=$api->edit($tok, $txt, "Updating list of IMO number conflicts. $screwup", 0, 1);
if($res->{'code'} ne 'success'){
$api->warn("Write for User:AnomieBOT/IMO number conflicts failed: " . $res->{'error'} . "\n");
return 60;
}
}
}
 
$self->{'iter'} = undef;
$self->{'conflicts'} = {};
return 3600 if !$self->{'doconflicts'};
$self->{'doconflicts'} = 0;
$api->store->{'lastrun'} = $starttime;
return $starttime + 86400 - time();