Jump to content

Module:User:MewBot

From Wiktionary, the free dictionary

This is a support module used to run certain functions of the bot User:MewBot.


local export = {}

local concat = table.concat
local insert = table.insert
local pluralize = require("Module:en-utilities").pluralize
local process_params = require("Module:parameters").process
local remove = table.remove
local to_json = require("Module:JSON").toJSON
local ulen = mw.ustring.len

local function get_data(frame, main_type, data_modname)
	local number = {type = "number"}
	local iargs = process_params(frame.args, {
		["first"] = number,
		["last"] = number,
		["getsize"] = {type = "boolean"},
	})
	local obj_module = require("Module:" .. pluralize(main_type))
	local ret, i, first, last, json_opts = {}, 0, iargs.first or 1, iargs.last or math.huge, {lua_table = true}
	for code, data in pairs(require("Module:" .. data_modname)) do
		i = i + 1
		if i >= first and i <= last then
			local obj = obj_module.makeObject(code, data, "don't canonicalize aliases"):toJSON(json_opts)
			-- Remove blank aliases/varieties/otherNames/ancestors
			for _, to_remove in ipairs {"aliases", "varieties", "otherNames", "ancestors"} do
				if obj[to_remove] and not obj[to_remove][1] then
					obj[to_remove] = nil
				end
			end
			-- For full objects, "full" is always or almost always the same as the lang code; if so, remove it.
			if obj.full == obj.code then
				obj.full = nil
			end
			local types = obj.type
			-- For full objects, "type" should always contain the main type and "full"; remove them.
			for j, obj_type in ipairs(types) do
				while obj_type == main_type or obj_type == "full" do
					remove(types, j)
					obj_type = types[j]
				end
			end
			insert(ret, to_json(obj, {compress = true}))
		end
	end
	local retval = "[" .. concat(ret, ", ") .. "]"
	if iargs.getsize then
		return tostring(ulen(retval))
	else
		return retval
	end
end

function export.getLanguageData(frame)
	return get_data(frame, "language", "languages/data/all")
end

function export.getEtymLanguageData(frame)
	return get_data(frame, "language", "etymology languages/data")
end

function export.getFamilyData(frame)
	return get_data(frame, "family", "families/data")
end

function export.getScriptData(frame)
	return get_data(frame, "script", "scripts/data")
end

function export.getWritingSystemData(frame)
	return get_data(frame, "writing system", "writing systems/data")
end

function export.getAliasData()
	return to_json(require("Module:languages/data").aliases)
end

return export