Module:Property navigation

From Wikidata
Jump to navigation Jump to search
Lua
CodeDiscussionLinksLink count SubpagesDocumentationTestsResultsSandboxLive code All modules

Each submodule is a Lua table, and keys are:

Name Top level only? Function Default
template Yes Name of the template without the Template: prefix. NAME properties
category Yes Categories that property talk pages using this navbox will be added to, without the Category: prefix. same as template
template_cat Yes Categories that the navbox itself will be added to, without the Category: prefix. Property by topic navigation templates
topic Yes QID of the topic of a navbox. Can not be used together with name or q. This will automatically generate a title like "Wikidata properties related to TOPIC". nil
name No Name (title) of a navbox or a specific navbox group. Can be either a string or a table of translations. nil
q No If specified, the label of this specific item in the user language is used as name/title, unless overrided by name. nil
below Yes Optional text to appear below the navbox. Any templates will be expanded. nil
style No The style that this Lua table is displayed, one of "navbox" (displayed as a navbox), "child" (displayed as a child navbox), "list" (displayed as list), "list2" (displayed as a second-level list).
  • Used as top-level: navbox
  • If parent is navbox: child if the first element is a table, otherwise list
  • If parent is child: child if the first element is a table and first element of the first element is also a table, otherwise list
  • If parent is list2 (which should not happen) or list: list2
separatorn No The separator before item n. Any templates will be expanded. (Ignored if style is navbox or child) "\n*" (list); "\n**" (list2)
prefixn No The prefix added before item n. Any templates will be expanded. (Ignored if style is navbox or child) ""
suffixn No The suffix added after item n. Any templates will be expanded. (Ignored if style is navbox or child) ""
unnamed parameter No Represents an element, which may be one of:
  • a Lua table - the content is rendered.
  • a string of format Pxxx - rendered as [[Property talk:Pxxx|label]]
  • a string of format Qxxx - rendered as [[Qxxx|label]]
  • any other string - displayed as-is
  • a Lua function - evaluate it with no parameter and process the result as above
    • Note: It is recommended to wrap any contents that requires accessing Wikibase entity data as a function, so they will not get evaluated if unnecessary.

Code

local p={i18n=mw.loadData("Module:Property navigation/i18n")}
local wdlabel=require('Module:Wikidata label')
local fb=require("Module:Fallback")
local lang = mw.getCurrentFrame():preprocess( "{{int:lang}}" )

local function navbox_render(data)
	return (function()	return prefix .. "[[" .. target .. "|" .. wdlabel._getLabel(q, nil, "-", "ucfirst") .. "]]" .. suffix
	end)
end

function p.lang_switch(x) return fb._langSwitch(x, lang) end

function p.link_q(target, q, params)
	params = params or {}
	local prefix = params.bold and "'''" or ""
	local suffix = prefix
	return (function()
		return prefix .. "[[" .. target .. "|" .. wdlabel._getLabel(q, nil, "-", "ucfirst") .. "]]" .. suffix
	end)
end

function p.name(content)
	local name
	if type(content.name) == "table" then
		name = content.name[lang] or (content.q and wdlabel._getLabel(content.q, nil, "-", "ucfirst") or nil) or fb._langSwitch(content.name, lang)
	else
		name = type(content.name) == "string" and content.name or (content.q and wdlabel._getLabel(content.q, nil, "-", "ucfirst") or nil)
	end
	if content.link then
		name='[[' .. (content.link=='1' and content.q or content.link) .. '|' .. name ..']]'
	end
	return name
end

function p.render(content, style)
	local style = content.style or style or "child"
	if style=="navbox" then
		local nav_data={
			name = content.template,
			listclass = "hlist",
			style = "font: arial;",
			bodystyle  = "background: #CDDDDD; width: 100%; vertical-align: middle;",
			titlestyle = "background: #CDDDDD; padding-left: 1em; padding-right: 1em; text-align: center; font-size: x-small;",
			groupstyle = "background: #CDDDDD; padding-left: 1em; padding-right: 1em; text-align: right; font-size: 11px;",
			liststyle  = "background: #EFFFFF; text-align: left/center; font-size: 12px;",
			state = "off",
			navbar = "on",
			
		}
		if content.topic then
			if type(content.topic)=="string" then
				nav_data.title=mw.ustring.format(fb._langSwitch(p.i18n.properties_related, lang), wdlabel._getLabel(content.topic, nil, "wikidata"))
			else
				for _, v in ipairs(content.topic) do
					nav_data.title=(nav_data.title and (nav_data.title .. '/') or '') .. wdlabel._getLabel(v, nil, "wikidata")
				end
				nav_data.title=mw.ustring.format(fb._langSwitch(p.i18n.properties_related, lang), nav_data.title)
			end
		else
			nav_data.title=p.name(content)
		end
		for i, v in ipairs( content ) do
			nav_data["group" .. i] = p.name(v)
			nav_data["list" .. i] = p.render(v, type(v[1])=="table" and "child" or "list") .. "\n"
		end
		if content.below then
			nav_data.below = mw.getCurrentFrame():preprocess(content.below)
		end
		return require("Module:Navbox")._navbox(nav_data)
	elseif style=="child" then
		local nav_data={"child"}
		for i, v in ipairs( content ) do
			nav_data["group" .. i] = p.name(v)
			nav_data["list" .. i] = p.render(v, type(v[1])=="table" and (type(v[1][1])=="table" and "child" or "list") or "list") .. "\n"
		end
		return require("Module:Navbox")._navbox(nav_data)
	elseif style=="list" or style=="list2" then
		local result = ""
		local item_prefix = style=="list2" and "\n**" or "\n*"
		for i, v in ipairs( content ) do
			local item = type(v)=="function" and v() or v
			local prefix=content["prefix" .. i] and mw.getCurrentFrame():preprocess(content["prefix" .. i]) or ""
			local separator=content["separator" .. i] and mw.getCurrentFrame():preprocess(content["separator" .. i]) or item_prefix
			local suffix=content["suffix" .. i] and mw.getCurrentFrame():preprocess(content["suffix" .. i]) or ""
			if type(item)=="string" then
				if mw.ustring.match(item, "^P%d+$") then
					result = result .. separator .. prefix .. wdlabel._getLabel(item, nil, "wikidata talk") .. suffix
				elseif mw.ustring.match(item, "^Q%d+$") then
					result = result .. separator .. prefix .. wdlabel._getLabel(item, nil, "wikidata") .. suffix
				else
					result = result .. separator .. prefix .. item .. suffix
				end
			else
				result = result .. (item.name or item.q and (separator .. prefix .. p.name(item) .. suffix) or '') .. p.render(item, "list2")
			end
		end
		return result
	else
		return "Unknown style:" .. str(style)
	end
end

function p.navbox(name)
	if type(name)=="table" then
	    name = name.args[1] or name:getParent().args[1]
	end
	if mw.ustring.sub(name, 1, 7) ~= "Module:" then
		name = "Module:Property navigation/" .. name
	end
	local data=require(name)
	local append=""
	local template_name=data.template or mw.getContentLanguage():ucfirst(name:gsub("Module:Property navigation/","") .. " properties")
	mw.log(template_name)
	local category=data.category or template_name
	local template_cat=data.template_cat or "Property by topic navigation templates"
	if mw.title.getCurrentTitle().namespace==121 and category~="" then
		append="[[Category:" .. category .. "]]"
	elseif mw.title.getCurrentTitle().namespace==10 and mw.title.getCurrentTitle().text==template_name and template_cat~="" then
		append=mw.getCurrentFrame():preprocess("{{Warning|To edit this navbox, please edit [[" .. name .. "]]}}") .. "[[Category:" .. template_cat .. "]]"
		if category~="" then
			append=append .. "[[Category:" .. category .. "|*]]"
		end
	end
	return p.render(data, "navbox") .. append
end

return p