Module:Category tree/requests

From Multilingual Bookbinding Dictionary
< Module:Category tree
Revision as of 17:48, 19 September 2018 by Adminsuzy (talk | contribs) (Created page with "local m_languages = require("Module:languages") local export = {} local function show_pagelist() local recent = mw.getCurrentFrame():callParserFunction{ name = "#tag",...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Documentation for this module may be created at Module:Category tree/requests/doc

local m_languages = require("Module:languages")

local export = {}

local function show_pagelist()
	
	local recent = mw.getCurrentFrame():callParserFunction{
		name = "#tag",
		args = {
			"DynamicPageList",
			"category=" .. mw.title.getCurrentTitle().text .. "\n" ..
			"count=10\n" ..
			"mode=ordered\n" ..
			"ordermethod=categoryadd\n" ..
			"order=descending"
		}
	}
	
	local oldest = mw.getCurrentFrame():callParserFunction{
		name = "#tag",
		args = {
			"DynamicPageList",
			"category=" .. mw.title.getCurrentTitle().text .. "\n" ..
			"count=10\n" ..
			"mode=ordered\n" ..
			"ordermethod=lastedit\n" ..
			"order=ascending"
		}
	}
	
	return [=[
{| id="newest-and-oldest-pages" class="wikitable" style="float: right; clear: both; margin: 0 0 .5em 1em;"
! Recent additions to the category
|-
| id="recent-additions" style="font-size:0.9em;" | ]=] .. recent .. [=[

|-
! Oldest pages ordered by last edit
|-
| id="oldest-pages" style="font-size:0.9em;" | ]=] .. oldest .. [=[

|}]=]
end

function export.contents(frame)
	local items = {}
	
	local no_catfix = false
	local valid_category = false
	
	if type(frame) == "table" then
		items.pagename = mw.title.getCurrentTitle().text
	elseif type(frame) == "string" then
		items.pagename = frame
		frame = mw.getCurrentFrame()
	end
	
	local en = m_languages.getByCode("en")
	
	local categories = {
		{
			regex = "^Requests concerning (.+)$",
			request_sortkey = "requests",
			this_request_in_all_languages = "Requests by language",
			not_hidden_category = true
		},
		{
			regex = "^Requests for etymologies in (.+) entries$",
			request_sortkey = "etymology",
			template_name = "rfe",
			this_request_in_all_languages = "Requests for etymologies by language",
		},
		{
			regex = "^Requests for expansion of etymologies in (.+) entries$",
			request_sortkey = "etymology2",
			template_name = "etystub",
			this_request_in_all_languages = "Requests for expansion of etymologies by language",
		},
		{
			regex = "^Requests for pronunciation in (.+) entries$",
			request_sortkey = "pronunciation",
			template_name = "rfp",
			this_request_in_all_languages = "Requests for pronunciation by language",
		},
		{
			regex = "^Requests for audio pronunciation in (.+) entries$",
			request_sortkey = "audio pronunciation",
			template_name = "rfap",
			this_request_in_all_languages = "Requests for audio pronunciation by language",
		},
		{
			regex = "^Requests for definitions in (.+) entries$",
			request_sortkey = "definition",
			template_name = "rfdef",
			this_request_in_all_languages = "Requests for definitions by language",
		},
		{
			regex = "^Requests for clarification of definitions in (.+) entries$",
			request_sortkey = "definition2",
			template_name = "sense stub",
			this_request_in_all_languages = "Requests for clarification of definitions by language",
		},
		{
			regex = "^Requests for inflections in (.+) entries$",
			request_sortkey = "inflection",
			template_name = "rfinfl",
			this_request_in_all_languages = "Requests for inflections by language",
		},
		{
			regex = "^Requests for inflections in (.+) adjective entries$",
			request_sortkey = "adjective",
			template_name = "rfinfl",
			template_syntax = "{{rfinfl|{{{language_code}}}|adjective}}",
			one_level_above_in_the_same_language = "Requests for inflections in {{{language_name}}} entries",
			this_request_in_all_languages = "Requests for inflections of adjectives by language",
		},
		{
			regex = "^Requests for inflections in (.+) noun entries$",
			request_sortkey = "noun",
			template_name = "rfinfl",
			template_syntax = "{{rfinfl|{{{language_code}}}|noun}}",
			one_level_above_in_the_same_language = "Requests for inflections in {{{language_name}}} entries",
			this_request_in_all_languages = "Requests for inflections of nouns by language",
		},
		{
			regex = "^Requests for inflections in (.+) participle entries$",
			request_sortkey = "noun",
			template_name = "rfinfl",
			template_syntax = "{{rfinfl|{{{language_code}}}|participle}}",
			one_level_above_in_the_same_language = "Requests for inflections in {{{language_name}}} entries",
			this_request_in_all_languages = "Requests for inflections of participles by language",
		},
		{
			regex = "^Requests for inflections in (.+) pronoun entries$",
			request_sortkey = "pronoun",
			template_name = "rfinfl",
			template_syntax = "{{rfinfl|{{{language_code}}}|noun}}",
			one_level_above_in_the_same_language = "Requests for inflections in {{{language_name}}} entries",
			this_request_in_all_languages = "Requests for inflections of pronouns by language",
		},
		{
			regex = "^Requests for inflections in (.+) verb entries$",
			request_sortkey = "verb",
			template_name = "rfinfl",
			template_syntax = "{{rfinfl|{{{language_code}}}|verb}}",
			one_level_above_in_the_same_language = "Requests for inflections in {{{language_name}}} entries",
			this_request_in_all_languages = "Requests for inflections of verbs by language",
		},
		{
			regex = "^Requests for example sentences in (.+)$",
			request_sortkey = "example",
			template_name = "rfex",
			this_request_in_all_languages = "Requests for example sentences by language",
		},
		{
			regex = "^Requests for quotations in (.+)$",
			request_sortkey = "quotation",
			template_name = "rfquote",
			this_request_in_all_languages = "Requests for quotations by language",
		},
		{
			regex = "^Requests for translations into (.+)$",
			request_sortkey = "translation",
			template_name = "t-needed",
			this_request_in_all_languages = "Requests for translations by language",
			catfix_lang = en,
		},
		{
			regex = "^Requests for review of (.+) translations$",
			request_sortkey = "translation2",
			template_name = "t-check",
			template_syntax = "{{t-check|{{{language_code}}}|example}}",
			template_description = "",
			this_request_in_all_languages = "Requests for review of translations by language",
			catfix_lang = en,
		},
		{
			regex = "^Requests for images in (.+) entries$",
			request_sortkey = "image",
			template_name = "rfi",
			this_request_in_all_languages = "Requests for images by language",
		},
		{
			regex = "^Requests for references for (.+) terms$",
			request_sortkey = "reference",
			this_request_in_all_languages = "Requests for references by language",
		},
		{
			regex = "^Requests for references for etymologies in (.+) entries$",
			request_sortkey = "reference2",
			template_name = "rfv-etymology",
			one_level_above_in_the_same_language = "Requests for references for {{{language_name}}} terms",
			this_request_in_all_languages = "Requests for references for etymologies by language",
		},
		{
			regex = "^Requests for references for pronunciations in (.+) entries$",
			request_sortkey = "reference3",
			template_name = "rfv-pronunciation",
			one_level_above_in_the_same_language = "Requests for references for {{{language_name}}} terms",
			this_request_in_all_languages = "Requests for references for pronunciations by language",
		},
		{
			regex = "^Requests for attention concerning (.+)$",
			request_sortkey = "attention",
			template_name = "attention",
			template_description = "This template does not generate any text in entries.",
			this_request_in_all_languages = "Requests for attention by language",
			no_catfix = true,
		},
		{
			regex = "^Requests for cleanup in (.+) entries$",
			request_sortkey = "cleanup",
			template_name = "rfc",
			template_syntax_nocat = "{{rfc|{{{language_code}}}|nocat=1}}",
			template_nocat = true,
			this_request_in_all_languages = "Requests for cleanup by language",
		},
		{
			regex = "^Requests for deletion in (.+) entries$",
			request_sortkey = "deletion",
			template_name = "rfd",
			template_syntax_nocat = "{{rfd|{{{language_code}}}|nocat=1}}",
			this_request_in_all_languages = "Requests for deletion by language",
		},
		{
			regex = "^Requests for verification in (.+) entries$",
			request_sortkey = "verification",
			template_name = "rfv",
			this_request_in_all_languages = "Requests for verification by language",
		},
		{
			regex = "^Requests for attention in etymologies in (.+) entries$",
			request_sortkey = "attention etymology",
			this_request_in_all_languages = "Requests for attention in etymologies by language",
		},
	}
	
	if mw.ustring.find(items.pagename, "^Requests .* by language$") then
		for i, category in pairs(categories) do	
			if items.pagename == category.this_request_in_all_languages then
				valid_category = true
				
				items.request_sortkey = category.request_sortkey
			end
		end
	else
		for i, category in pairs(categories) do
			if mw.ustring.find(items.pagename, category.regex) then
				valid_category = true
				
				for key, value in pairs(category) do
					items[key] = value
				end
				
				if not category.not_hidden_category then
					items.hidden_category = "__HIDDENCAT__"
				end
			end
		end
	end

	if not valid_category then
		error("Category name is not recognized.")
	end
	
	if items.regex then
		items.language_name = mw.ustring.match(items.pagename, items.regex)
		items.language_object = m_languages.getByCanonicalName(items.language_name) or error ('The category title contains an invalid language name, "' .. items.language_name .. '". Choose a canonical name from the data modules of Module:languages.')
		items.language_code = items.language_object:getCode()
	end
	
	items.edit_link = "{| class='wikitable noprint plainlinks' style='float: right'\n| '''[https://en.wiktionary.org/w/index.php?title=Module:category_tree/requests&action=edit Edit category data]'''\n|}"
	
	items.description = ("\nThis category contains '''{{{pagename}}}'''.")

	if items.template_name then
		if items.template_syntax then
			items.template_syntax_with_language_code = items.template_syntax
		else
			items.template_syntax_with_language_code = ("{{{{{template_name}}}|{{{language_code}}}}}")
		end

		items.full_text_about_the_template = "\n" .. ("To make this request, in this specific language, use this code in the entry (see also the documentation at [[Template:{{{template_name}}}]]):\n\n<pre>{{{template_syntax_with_language_code}}}</pre>")

		if items.template_description then
			items.full_text_about_the_template = items.full_text_about_the_template .. " " .. items.template_description
		else
			if items.template_syntax_nocat then
				items.full_text_about_the_template = items.full_text_about_the_template .. "\nIt results in the message below:\n\n<<{{{template_syntax_nocat}}}>>"
			else
				items.full_text_about_the_template = items.full_text_about_the_template .. "\nIt results in the message below:\n\n<<{{{template_syntax_with_language_code}}}>>"
			end
		end
	end
	
	if not items.one_level_above_in_the_same_language then
		if mw.ustring.find(items.pagename, "^Requests .* by language$") then
			items.one_level_above_in_the_same_language = "Request subcategories by language"
		elseif mw.ustring.find(items.pagename, "^Requests concerning .*") then
			items.one_level_above_in_the_same_language = "{{{language_name}}} entry maintenance"
		else
			items.one_level_above_in_the_same_language = "Requests concerning {{{language_name}}}"
		end
	end

	if items.one_level_above_in_the_same_language then
		items.one_level_above_in_the_same_language_text = "[[Category:{{{one_level_above_in_the_same_language}}}|{{{request_sortkey}}}]]"
	end
	
	if items.regex then
		items.this_request_in_all_languages_text = ("[[Category:{{{this_request_in_all_languages}}}|{{{language_name}}}]]")
	end
	
	if items.catfix_lang or ( not items.no_catfix and items.language_object ) then
		items.catfix = require("Module:utilities").catfix(items.catfix_lang or items.language_object)
	end
	
	items.pagename = mw.ustring.gsub(items.pagename, "Requests", "requests")
	
	result = require("Module:table").compressSparseArray{ items.hidden_category, items.catfix, items.edit_link, show_pagelist(), items.description, items.full_text_about_the_template, items.one_level_above_in_the_same_language_text, items.this_request_in_all_languages_text }
	result = table.concat(result, "\n")
	
	--[[	Replaces pseudo-template code {{{ }}} with the corresponding member
			of the "items" table. Has to be done at least twice,
			since some of the items are nested:
			
			{{{template_syntax_with_language_code}}}
				⇓
			{{{{{template_name}}}|{{{language_code}}}}}
				⇓
			{{attention|en}}							]]
	
	while result:find("{{{") do
		result = mw.ustring.gsub(
			result,
			"{{{([^%}%{]+)}}}",
			function(item)
				if items[item] then
					if type(items[item]) == "string" or type(items[item]) == "number" then
						return items[item]
					else
						error('The item "{{{' .. item .. '}}}" is a ' .. type(item) .. ' and can\'t be concatenated. (Pagename: ' .. items.pagename .. '.)')
					end
				else
					error('The item "' .. item .. '" was not found in the "items" table. (Pagename: ' .. items.pagename .. '.)')
				end
			end
		)
	end
	
	-- Preprocess template code surrounded by << >>.
	result = mw.ustring.gsub(
		result,
		"<<([^>]+)>>",
		function (template_code)
			return frame:preprocess(template_code)
		end
	)
	
	return result
end

function export.test(frame)
	if type(frame) ~= "table" then
		error('The function "' .. test '" requires a frame object as argument.')
	end
	
	local pagenames = {}
	local results = {}
	
	for i, item in pairs(frame.args) do
		mw.text.trim(item)
		table.insert(pagenames, item)
		item = export.contents(item)
		item = frame:extensionTag{ name = "syntaxhighlight", content = item, args = { lang = "html" } }
		table.insert(results, item)
	end
	
	local output = {}
	
	table.insert(output, '{| class="wikitable"')
	
	for i, result in pairs(results) do
		table.insert(output, '\n|-\n| [[:Category:' .. pagenames[i] .. '|' .. pagenames[i] .. ']] || ' .. result)
	end
	
	table.insert(output, '\n|}')
	
	return table.concat(output)
end

return export