Jump to content

Module:User:AnomieBOT/LanguageCategoryCreator

Permanently protected module
From Wikipedia, the free encyclopedia

-- 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',
    ['CS1 (.+)-language sources %((.+)%)'] = 'cs1_sources',
}

-- 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 > 0 then
    		return p[ v ]( cat, unpack( m ) )
    	end
    end
	return 'ERROR: Unrecognized category'
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 )
	local mIpa = require( 'Module:IPA/category documentation' )
	local code = mIpa.lookup_name( name )
	if not code then
		return 'ERROR: language name not recognized'
	end
	
	return '{{IPA language category}}'
end

-- Generate the wikitext for "sources" categories from CS1 templates
function p.cs1_sources( cat, name, tag )
	local mCs1 = require( 'Module:Cs1 documentation support' )
	if mCs1.code_name_pair_exists( { tag, name } ) == nil then
		return 'ERROR: code/name pair does not exist'
	end

	return '{{CS1 language sources}}'
end

return p