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

Content deleted Content added
AnomieBOT (talk | contribs)
Updating published sources: TemplateUnsubstifier: * Log templates being unsubstified.
AnomieBOT (talk | contribs)
Updating published sources: General: * Update for the addition of 'rvslots'. DatedCategoryDeleterTest: * Disable. It's clear that task won't be needed. BrokenRedirectDeleter: * Handle pages with newlines before the <code>#REDIRECT</code>.
 
(5 intermediate revisions by the same user not shown)
Line 82:
@templates = @{$self->{'templates'}};
} else {
$api->log( "Loading templates list from Wikipedia:AutoWikiBrowser/Dated templates" );
$res = $api->query(
titles => 'Wikipedia:AutoWikiBrowser/Dated templates',
prop => 'revisions',
rvprop => 'ids|timestamp|content',
rvslots => 'main',
rvlimit => 1,
);
Line 102 ⟶ 104:
return $t if $t > 0;
}
my $txt = $res->{'slots'}{'main'}{'*'};
$txt = $api->strip_nowiki($txt);
$txt =~ s/_/ /g;
Line 129 ⟶ 131:
next unless $p->{'ns'} == 10; # Sanity check
next unless exists( $p->{'pageid'} ) && exists( $p->{'lastrevid'} ); # Page missing or invalid?
if ( exists( $p->{'redirect'} ) ) { # Redirect?
$api->warn( "How did we manage to get a redirect ($p->{title}) in here? Skipping it." );
next;
}
next if $p->{'lastrevid'} eq ( $api->store->{'lastrev ' . $p->{'pageid'}} // 0 );
push @templates, $p->{'title'};
Line 139 ⟶ 145:
my $endtime = time() + 300;
my @retry = ();
my $re = $api->redirect_regex();
while ( @templates ) {
return 0 if $api->halting;
Line 147 ⟶ 154:
prop => 'revisions',
rvprop => 'ids|content',
rvslots => 'main',
rvlimit => 1,
);
Line 156 ⟶ 164:
my $pageid = $res->{'pageid'};
my $revid = $res->{'revisions'}[0]{'revid'};
my $intxt = $res->{'revisions'}[0]{'slots'}{'main'}{'*'};
(my $name = $title) =~ s/^Template://;
my $outtxt = undef;
 
# Sanity check
if ( $intxt =~ /$re/ ) {
$api->warn( "HELP: $title looks like a redirect, refusing to edit\n" );
goto skip;
}
 
# Split into template and noinclude parts, then process
if ( $intxt =~ /^\s*+(.+?)((?><noinclude>(?>[^<]+|<(?!\/?noinclude))*(?:<\/noinclude>|$))*)\s*$/s ) {
my $trail = $2;
my ($txt, $params) = $self->upgrade_unsubst( $api, $title, $1 );
$outtxt = "{{ {{{|safesubst:}}}#invoke:Unsubst||\$N=$name $params |\$B=\n$txt\n}}$trail" if defined( $txt );
} else {
$api->warn( "HELP: $title doesn't match the basic regular expression, refusing to edit\n" );
goto skip;
}
 
Line 196 ⟶ 211:
}
}
 
skip:
$api->store->{"lastrev $pageid"} = $revid;
$self->{'templates'} = [@templates, @retry];
Line 216 ⟶ 233:
 
# If it doesn't have "unsubst", it's fine to wrap.
returnunless ($txt, $params) unless $txt =~ /unsubst/i; ) {
# sanity-check that the existing template code doesn't break template
# syntax, see https://en.wikipedia.org/w/index.php?diff=prev&oldid=582081088
my $tmp = $api->process_templates( "{{\x02foo\x03|1=$txt}}", sub {
my $name = shift;
my $params = shift;
return undef unless $name eq "\x02foo\x03";
return 'bad' unless @$params == 1;
return 'ok';
} );
return ($txt, $params) if $tmp eq 'ok';
$api->warn( "HELP: $title contains unwrappable content" );
return undef;
}
 
# If it already uses Module:Unsubst, then we don't need to do anything to
Line 231 ⟶ 261:
my $params = shift;
$name =~ s!<includeonly>safesubst:</includeonly>!!;
$name =~ s!\{\{\{\|safesubst:\}\}\}!!;
return undef unless $name =~ /^\s*[iI]fsubst\s*$/;
foreach ($api->process_paramlist(@$params)) {
Line 259 ⟶ 289:
 
$name =~ s!^<includeonly>(?:safe)?subst:</includeonly>!!i;
$name =~ s!^\{\{\{\|(?:safe)?subst:\}\}\}!!i;
$name =~ s!^(?:safe)?subst:!!i;
return undef unless $name =~ /^\s*[uU]nsubst\s*$/;