Modul:MediaWiki message helper

A Wikiszótárból, a nyitott szótárból

A modult a Modul:MediaWiki message helper/doc lapon tudod dokumentálni

local export = {}

local function print_suggestions(suggestions)
	if #suggestions == 0 then
		return ""
	else
		local prefix = "* Did you mean "
		local suffix
		if #suggestions > 1 then
			prefix = prefix .. " any of these?\n"
			suggestions = suggestions:map(function(link) return "** " .. link end)
			suffix = ""
		else
			suffix = "?"
		end
		return prefix .. suggestions:concat "\n" .. suffix
	end
end

-- For [[MediaWiki:Noarticletext]] on uncreated category pages.
function export.category_suggestions(frame)
	local output = require "Module:array"()
	
	local function make_suggestion(title, suffix)
		output:insert("'''[[:Category:"
			.. title .. "]]'''" .. (suffix or ""))
	end

	local title = frame.args.title and mw.title.new(frame.args.title).text or mw.title.getCurrentTitle().text
	title = mw.ustring.gsub(title, "^.", mw.ustring.upper)
	
	local function check_for_page_with_suffix(suffix)
		local suggestion = title .. " " .. suffix
		if mw.title.new("Category:" .. suggestion).exists then
			make_suggestion(suggestion)
			return true
		end
		return false
	end
	
	local has_language_category = check_for_page_with_suffix("language")
	check_for_page_with_suffix("Language")
	check_for_page_with_suffix("languages")
	local has_script_category = check_for_page_with_suffix("script")
	
	local function check_other_names_of_languages(language_name)
		for code, data in pairs(require "Module:languages/alldata") do
			local function check_name_list(list)
				if list then
					for _, name in ipairs(list) do
						-- The aliases and varieties are recursive,
						-- with subtables that themselves contain names.
						if type(name) == "table" then
							check_name_list(name)
						else
							if name == language_name then
								local object = require "Module:languages".makeObject(code, data)
								make_suggestion(object:getCategoryName())
							end
						end
					end
				end
			end
			check_name_list(data.otherNames)
			check_name_list(data.aliases)
			check_name_list(data.varieties)
		end
	end
	
	-- If title looks like a language category, then check if the language name
	-- in it is a valid canonical name, or one of the otherNames for some
	-- language.
	-- If the title looks like a language code, check for a language or a script
	-- with that code.
	local function check_language_name(language_name, is_language_category, has_language_category)
		local ret = false
		if not has_language_category then
			if require "Module:languages/canonical names"[language_name] then
				if not is_language_category then
					make_suggestion(language_name .. " language")
				else
					output:insert("* '''" .. language_name .. "''' is a valid Wiktionary language name.")
				end
				ret = true
			end
		end
		
		-- Some otherNames are the canonical name of another language.
		check_other_names_of_languages(language_name)
		
		return ret
	end
	local language_name = title:match "^(.+) language$" or title:match "^(.+ Language)$"
	check_language_name(language_name or title, language_name ~= nil, has_language_category)
	
	-- Most languages (7965/8085 by last count) have uppercase letters at
	-- beginning of the name and after whitespace and punctuation characters,
	-- and lowercase everywhere else. Exceptions include languages
	-- with apostrophes, such as Yup'ik, and languages with tone letters,
	-- such as ǃXóõ.
	local fixed_capitalization = mw.ustring.gsub(
		mw.ustring.lower(language_name or title),
		"%f[^\0%s%p]%a", mw.ustring.upper)
	if fixed_capitalization ~= (language_name or title) then
		check_language_name(fixed_capitalization)
	end
	
	if title:find "^[%a-]+$" then
		local function check_for_valid_code(code, ...)
			for _, module_name in ipairs { ... } do
				local object = require("Module:" .. module_name).getByCode(code)
				if object then
					make_suggestion(object:getCategoryName(),
						" (code <code>" .. code .. "</code>)")
				end
			end
		end
		local code = title:lower()
		local proto_code = code .. "-pro"
		check_for_valid_code(code, "languages", "etymology languages", "scripts", "families")
		check_for_valid_code(code .. "-pro", "languages", "etymology languages")
	end
	
	local function check_script_name(script_name, is_script_category, has_script_category)
		if not has_script_category then
			local object = require "Module:scripts".getByCanonicalName(script_name)
			if object then
				if is_script_category then
					output:insert("* " .. script_name .. " is a valid Wiktionary script name.")
				else
					make_suggestion(object:getCategoryName())
				end
			end
		end
		for code, data in pairs(require "Module:scripts/data") do
			if data.otherNames then
				for _, name in ipairs(data.otherNames) do
					if script_name == name then
						local object = require "Module:scripts".makeObject(code, data)
						make_suggestion(object:getCategoryName())
					end
				end
			end
		end
	end
	
	local script_name = title:match "^(.+) script$"
	check_script_name(script_name or title, script_name ~= nil, has_script_category)
	
	return print_suggestions(output)
end

function export.template_suggestions(frame)
	local title = frame.args.title and mw.title.new(frame.args.title).text
		or mw.title.getCurrentTitle().text
	
	local output = require "Module:array"()
	
	local function make_suggestion(title, suffix)
		output:insert("'''[[:Template:"
			.. title .. "]]'''" .. (suffix or ""))
	end
	
	local function check_for_page_with_prefix(prefix)
		local suggestion = prefix .. title
		if mw.title.new("Template:" .. suggestion).exists then
			make_suggestion(suggestion)
			return true
		end
		return false
	end
	
	local prefixes = frame.args.prefixes
	if prefixes then
		local prefix_separator = frame.args.prefix_separator or ","
		for prefix in prefixes:gmatch("[^" .. prefix_separator .. "]+") do
			check_for_page_with_prefix(prefix)
		end
	end
	
	local prefix, rest = title:match "^([^:]+):(.+)$"
	if not prefix then
		prefix, rest = "", title
	end
	
	if prefix == "" or prefix == "R" or prefix == "RQ" then
		local templates = require "Module:MediaWiki message helper/R: and RQ: templates"
		local rest_pattern = require "Module:string".pattern_escape(rest)
			:gsub(
				"%l",
				function(letter)
					return "[" .. letter:upper() .. letter:lower() .. "]"
				end)
		
		for match in templates:gmatch("%f[^%z\n]RQ?:[a-z-]*[-:]?"
				.. rest_pattern
				.. "[ :]?%d?%d?%d?%d?[/-]?%d?%d?%d?%d?%l?%f[%z\n]") do
			make_suggestion(match)
		end
	end
	
	return print_suggestions(output)
end

return export