Module:User:Mr. Stradivarius/portalCheck

-- This module checks subpages of [[Module:Portal/images]].

-- We can't do all 1,412 portal aliases at once, as we run into the expensive 
-- function count limit. So here we define start and end numbers so that we
-- can run this in multiple batches.

local tableTools = require('Module:TableTools')

local p = {}

local function getImageKeyArray()
	-- Returns an array containing all image subpages (minus aliases) as loaded by mw.loadData.
	local keyTables = {}
	local subpages = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'other'}
	for i, subpage in ipairs(subpages) do
		keyTables[#keyTables + 1] = mw.loadData('Module:Portal/images/' .. subpage)
	end
	local keys = {}
	for i, keyTable in ipairs(keyTables) do
		for key in pairs(keyTable) do
			keys[#keys + 1] = key
		end
	end
	local aliases = mw.loadData('Module:Portal/images/aliases')
	for k, aliasTable in pairs(aliases) do
		for i, alias in ipairs(aliasTable) do
			keys[#keys + 1] = alias
		end
	end
	table.sort(keys)
	return keys
end

function p.main(frame)
	local startNum = frame.args[1]
	local endNum = frame.args[2]
	startNum = tonumber(startNum)
	endNum = tonumber(endNum)
	local lang = mw.language.getContentLanguage()
	local keys = getImageKeyArray()
	local flaggedKeys = {}
	for i = startNum, endNum do
		local key = keys[i]
		local firstUpper = lang:ucfirst(key)
		local upper = mw.ustring.upper(key)
		local wordFirstUpper = mw.ustring.gsub(key, '%w*', function (match) return lang:ucfirst(match) end)
		local permutations = {firstUpper, upper, wordFirstUpper}
		permutations = tableTools.removeDuplicates(permutations)
		local matchExists = false
		for i, permutation in ipairs(permutations) do
			local title = mw.title.new('Portal:' .. permutation)
			if title.exists then
				matchExists = true
			end
		end
		if not matchExists then
			flaggedKeys[#flaggedKeys + 1] = key
		end
	end
	local ret = {}
	for i, flaggedKey in ipairs(flaggedKeys) do
		ret[#ret + 1] = '* [[Portal:' .. lang:ucfirst(flaggedKey) .. ']]'
	end
	return table.concat(ret, '\n')
end

return p