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

Content deleted Content added
AnomieBOT (talk | contribs)
Updating published sources: LanguageCategoryCreator: * New task!
 
AnomieBOT (talk | contribs)
Updating published sources: LanguageCategoryCreator: * Approved!
 
(5 intermediate revisions by the same user not shown)
Line 1:
{{ombox|type=notice|text= ApprovalBRFA requestedapproved 2025-05-31.<br />[[Wikipedia:Bots/Requests for approval/AnomieBOT 84]]}}
<syntaxhighlight lang="perl">
package tasks::LanguageCategoryCreator;
Line 10:
Task: LanguageCategoryCreator
BRFA: Wikipedia:Bots/Requests for approval/AnomieBOT 84
Status: BRFAApproved 2025-05-31
Created: 2025-04-10
 
Create needed categories underfor [[:Category:Wikipedialanguage maintenancetemplates categoriessuch sortedas by month]] and [[:Category:Wikipedia categories sorted by month]]{{tl|lang}}.
 
=end metadata
Line 29:
@ISA=qw/AnomieBOT::Task/;
 
my $screwup;
my @textcats = (
'Articles containing (?<n>.+)-language text',
'Articles with text in (?<n>.+) languages',
);
my @sourcecats = (
'Articles with (?<n>.+)-language sources \((?<c>.+)\)',
);
 
sub new {
Line 48 ⟶ 42:
 
=for info
ApprovalBRFA requestedapproved 2025-05-31.<br />[[Wikipedia:Bots/Requests for approval/AnomieBOT 84]]
 
=cut
 
sub approved {
return 02;
}
 
Line 60 ⟶ 54:
 
$api->task('LanguageCategoryCreator',0,0,qw/d::Talk d::Redirects d::Templates/);
$screwup='Report errors at [[User:'.$api->user.'/shutoff/LanguageCategoryCreator]]';
 
my $cont = $self->{'dbcontinue'} // '';
Line 73 ⟶ 68:
$dbh->do( q{SET NAMES 'utf8'} );
 
# Load data from module
my $catsql = join( ' OR ', map { my $c = $_; $c =~ s/\\//g; $c =~ s/ /_/g; $c =~ s/\(\?<[nc]>\.\+\)/%/g; "cat_title LIKE " . $dbh->quote( $c ) } ( @textcats, @sourcecats ) );
my $res = $api->query(
cmlimitaction => 1'parse',
listtitle => 'categorymembersUser:AnomieBOT',
text => '{{subst:#invoke:User:AnomieBOT/LanguageCategoryCreator|list_cat_likes}}',
formatversiononlypst => 21,
disablelimitreportformatversion => 12,
);
if ( $res->{'code'} ne 'success' ) {
$api->warn( "Failed to getfetch categorydata membersfrom forUser:AnomieBOT/LanguageCategoryCreator $titlelist_cat_likes: " . $res->{'errorcontent'} . "\n" );
return 60;
}
my $catsql = join( ' OR ', map { my $c = $_; $c =~ s/\\//g; $c =~ s/ /_/g; $c =~ s/\(\?<[nc]>\.\+\)/%/g; "cat_title LIKE " . $dbh->quote( $c_ ) } split( @textcats"\n", @sourcecats$res->{'parse'}{'text'} ) );
 
# Spend a max of 5 minutes on this task before restarting
Line 146 ⟶ 153:
my ($self,$api,$title)=@_;
 
# FigureCheck outif the languagecategory nameactually orhas codemembers.
my ($n,$c,$src)res = $api->query('', '', '');
if ( $src eq ''list )=> {'categorymembers',
forcmtitle my=> "Category:$cat (@textcats) {title",
if ( $titlecmlimit =~ /^$cat$/ )> {1,
$nformatversion => $+{'n'} // '';2,
});
$c = $+{'c'} // '';
if ( $nres->{'code'} ne 'success' ) {
$src = '{{Non-English-language text category}}';
$api->warn( "Failed to get category members for Category:$title: " . $res->{'error'} . "\n" );
last;
return }60;
}
}
ifunless ( @{$src eq res->{'query'}{'categorymembers'}} ) {
$api->log( "Skipping Category:$title, has no members" );
for my $cat (@sourcecats) {
if ( $title =~ /^$cat$/ ) {
$n = $+{'n'} // '';
$c = $+{'c'} // '';
$src = '{{Non-English-language sources category}}';
last;
}
}
}
if ( $src eq '' ) {
$api->log( "Skipping $title, not a valid category" );
return 0;
}
 
# AskGet Module:Langwikitext iffor thecategory namepage and/orfrom tag arethe knownmodule.
my $res = $api->query(
action => 'parse',
title => 'FoobarUser:AnomieBOT',
text => "<p>Tag: {{subst:#invoke:Lang|tag_from_name|$n}}<User:AnomieBOT/p>\n<p>Name: {{#invoke:LangLanguageCategoryCreator|name_from_taggenerate_cat_wikitext|cat=$ctitle}}</p>\n",
proponlypst => 'text'1,
disablelimitreport => 1,
formatversion => 2,
);
Line 188 ⟶ 183:
 
my $txt = $res->{'parse'}{'text'};
if ( $titletxt =~ /^$cat$ERROR: (.*)/ ) {
 
$api->log( "SanityFailed checkto faileddetermine content for Category:$title: For $n, $c !== $1\n" );
if ( $n ne '' ) {
if ( $txt =~ /Error: language: .* not found/ ) {
$api->warn( "Sanity check failed for $title: Language $n not found\n" );
return 0;
}
unless ( $txt =~ /<p>Tag: ([a-z-]+)<\/p>/ ) {
$api->warn( "For $title: Failed to check name $n: $txt\n" );
return 60;
}
if ( $c ne '' && $c ne $1 ) {
$api->log( "Sanity check failed for $title: For $n, $c !== $1\n" );
return 0;
}
}
if ( $c ne '' ) {
if ( $txt =~ /Error: unrecognized language tag/ ) {
$api->log( "Sanity check failed for $title: Language tag $c not found\n" );
return 0;
}
unless ( $txt =~ /<p>Name: ([^<]+)<\/p>/ ) {
$api->warn( "For $title: Failed to check tag $c: $txt\n" );
return 60;
}
if ( $n ne '' && $n ne $1 ) {
$api->log( "Sanity check failed for $title: For $c, $n !== $1\n" );
return 0;
}
}
 
# Check if the category has members.
$res = $api->query(
list => 'categorymembers',
cmtitle => "Category:$title",
cmlimit => 1,
formatversion => 2,
);
if ( $res->{'code'} ne 'success' ) {
$api->warn( "Failed to get category members for $title: " . $res->{'error'} . "\n" );
return 60;
}
unless ( @{$res->{'query'}{'categorymembers'}} ) {
$api->log( "Skipping $title, has no members" );
return 0;
}
 
# GoodDo enough.it!
my $tok=$api->edittoken( "Category:$title", EditRedir => 1 );
if ( $tok->{'code'} eq 'shutoff' ) {
Line 255 ⟶ 209:
 
$api->log( "Creating language category in Category:$title" );
my $r = $api->edit( $tok, $srctxt, "Creating non-empty language category. $screwup", 1, 1 );
if ( $r->{'code'} ne 'success' ) {
$api->warn( "Write failed on Category:$title: " . $r->{'error'} . "\n" );