Module:User:AnomieBOT/LanguageCategoryCreator
Appearance
-- A module used by AnomieBOT's LanguageCategoryCreator task.
-- See [[Wikipedia:Bots/Requests for approval/AnomieBOT 84]]
local p = {}
-- Map of category patterns to handler types.
local cats = {
['Articles containing (.+)-language text'] = 'lang',
['Articles with text in (.+) languages'] = 'lang',
['Articles with (.+)-language sources %((.+)%)'] = 'in_lang',
['Articles with sources in (.+) languages %((.+)%)'] = 'in_lang',
['Pages with (.+) IPA'] = 'ipa',
}
-- List SQL LIKE syntax for supported categories.
function p.list_cat_likes()
local keys = {}
for k, v in pairs( cats ) do
-- Convert pattern to LIKE syntax.
k = k:gsub( '%%', '' ):gsub( ' ', '_' ):gsub( '%(%.%+%)', '%%' )
table.insert( keys, k )
end
return table.concat( keys, '\n' )
end
-- Generate the wikitext for a category page.
function p.generate_cat_wikitext( frame )
local cat = frame.args.cat
for k, v in pairs( cats ) do
local m = { cat:match( '^' .. k .. '$' ) }
if #m then
return p[ v ]( cat, unpack( m ) )
end
end
return '<span class="error">Unrecognized category</span>'
end
-- Generate the wikitext for a {{lang}} category page
function p.lang( cat, name )
local mLang = require( 'Module:Lang' )
local tag = mLang._tag_from_name{ name }
if tag:find( '^<span' ) then
return 'ERROR: language name not recognized'
end
local cat2 = mLang._category_from_tag{ tag }
if cat2:find( '^<span' ) then
return 'ERROR: no category for code ' .. tag
end
if cat2 ~= 'Category:' .. cat then
return 'ERROR: category for code ' .. tag .. ' is ' .. cat2 .. ', not Category:' .. cat
end
return '{{Non-English-language text category}}'
end
-- Generate the wikitext for an {{in lang}} category page
function p.in_lang( cat, name, tag )
local mLang = require( 'Module:In lang' )
local cat2 = mLang._in_lang{ tag, ['list-cats'] = 'yes' }
if cat2 == '' then
return 'ERROR: no category for code ' .. tag
end
if cat2 ~= 'Category:' .. cat then
return 'ERROR: category for code ' .. tag .. ' is ' .. cat2 .. ', not Category:' .. cat
end
return '{{Non-English-language sources category}}'
end
-- Generate the wikitext for an {{IPA}} category page
function p.ipa( cat, name )
-- Code here adapted from [[Module:IPA/category documentation]]
local mLang = require( 'Module:Lang' )
local lects = mw.loadData('Module:IPA/data/export')
local code = nil
if not code then
for _, lect in ipairs(lects) do
if ( lect.name or lect.extName ) == name then
code = lect.code
break
end
end
end
if not code then
code = mLang._tag_from_name({ name })
if code:find('^<span') then
code = nil
else
local regionCode = code:match('-(.+)')
if regionCode and regionCode:sub(1, 2) ~= 'x-' then
code = code:sub(1, #code - #regionCode) .. regionCode:upper()
end
end
end
if not code then
return 'ERROR: language name not recognized'
end
return '{{IPA language category}}'
end
return p