Module:Navigation by countries

From Wikimedia Commons, the free media repository
Revision as of 16:39, 11 November 2019 by Reinhard Müller (talk | contribs) (New module to include the navigation by countries into category description boxes)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
Lua

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

Documentation

This module is only used by Template:Navigation by/administrative division and serves as a top-level navigation layer above all country subdivisions and sub-subdivisions. It is based on Module:Countries and its submodules, but the output is optimized for inclusion in category description boxes.

There is only one function defined here, navigationBlock, as an analogy to the navigationBlock function in Module:Navigation by Wikidata. The function arguments are:

item
Wikidata item ID of the geographic entity the current page relates to. If it is not a country (but a subdivision), nothing is output.
pattern
the
Like in Template:Navigation by/Wikidata or Template:Navigation by.

Red links will always be hidden in the country list.

Code

-- =============================================================================
-- Prepare a list of countries for the inclusion into a category description
-- template.
-- =============================================================================

local langSwitch           = require "Module:LangSwitch"
local wdStatements         = require "Module:Wikidata statements"
local navbox               = require "Module:Navbox"
local navigationByWikidata = require "Module:Navigation by Wikidata"

local p = {}

-- -----------------------------------------------------------------------------
-- Main routine
-- -----------------------------------------------------------------------------

function p.navigationBlock(frame)
	-- Compare the "Country" property of the Wikidata item with itself to find
	-- out whether it actually is a country. If it isn't, just return nothing.
	if wdStatements.getOneItemId(frame.args.item, "P17") ~= frame.args.item then
		return nil
	end
	-- Find out the user's language.
	local lang = frame:callParserFunction("int", "lang")
	-- Loop through all continents this country is located in. For each of them,
	-- build a Navbox subgroup:
	local result = ""
	for index, continentId in ipairs(wdStatements.getItemIdList(frame.args.item, "P30")) do
		-- First, add the name of the continent as a title of the subgroup.
		result = result .. "; " .. navigationByWikidata._getLink(continentId, frame.args.pattern, frame.args.the, false) or mw.wikibase.getLabel(continentId) .. "\n"
		-- Next, load the submodule of Module:Countries with the country list.
		local module = "Module:Countries/" .. mw.wikibase.getLabelByLang(continentId, "en")
		local status, data = pcall(require, module)
		assert(status, "Data could not be loaded from [[" .. module .."]]")
		-- Now, build the Navbox subgroup:
		local boxargs = {
			border = "subgroup"
		}
		-- Iterate through the various secions in the country list and create a
		-- Navbox group for each of them:
		local count = 1
		for index, section in ipairs{"main", "specific", "limited", "other"} do
			-- Not all sections exist in all continents.
			if data.titles[section] then
				-- Find out the group title for the user's language.
				boxargs["group" .. tostring(count)] = langSwitch._langSwitch(data.titles[section], lang)
				-- Let "Module:Navigation by Wikidata" do the actual hard work
				-- of compiling the link list:
				local listargs = {
					sort = "label",
					pattern = frame.args.pattern,
					the = frame.args.the,
					redlinks = frame.args.redlinks
				}
				-- Convert the data list from the Countries submodule into a
				-- simple list of Wikidata item IDs, possibly augmented with a
				-- marker.
				for code in data.lists[section].automatic:gmatch('%S+') do
					local info = data.countries[code]
					table.insert(listargs, info.qid .. (info.mark and ":after=⁽¹⁾" or ""))
				end
				-- Actually run the "Module:Navigation by Wikidata" magic.
				boxargs["list" .. tostring(count)] = navigationByWikidata._navigationList(listargs)
				count = count + 1
			end
		end
		-- As a separate group at the end of the Navbox subgroup, add an
		-- explanation for the markers.
		boxargs["group" .. tostring(count)] = ""
		boxargs["list" .. tostring(count)] = "* " .. "⁽¹⁾ " .. langSwitch._langSwitch(data.titles["partly"], lang)
		-- Actually build the Navbox subgroup.
		result = result .. navbox._navbox(boxargs) .. "\n" 
	end
	return result
end

return p