မာတိကာသို့ ခုန်သွားရန်

မဝ်ဂျူ:phi-placelist

နူ ဝိက်ရှေန်နရဳ

This module is used by {{list:places in the Philippines/en}}.


-- Author: [[User:Yivan000]]

-- This is the maximum number of levels/divisions
local maxLevel = 10

-----------------

local function getKeysOfTable(table)
	return require("Module:table").keysToList(table);
end

-----------------


-- Main function
local function getTableFromData( args )
	
	local inputtedDivisions = {} -- save here the inputted divisions, in order of less specific to more specific
	
	local level = 0
	while ( level < maxLevel ) do
		
		argLevel = args[level]
		if argLevel ~= nil then  -- get all positional arguments from 0 to `maxLevel`
			inputtedDivisions[level] = argLevel
		end
		level = level + 1
	end
	
	-- get the division from data modules
	local list = {} 
	local _region = ""
	for i, v in ipairs( inputtedDivisions ) do
		if (i == 1 and #inputtedDivisions == 1) then  -- if the first and only the first one, get from region data module
			list = require("Module:phi-placelist/data/" .. v)
		elseif (i == 1) then --if the first one, it is the region, store it
			_region = v
		elseif (i == 2) then --if the second one, get from data module
			list = require("Module:phi-placelist/data/" .. _region .. "/" .. v)
		elseif (i == #inputtedDivisions and args["type"] == "hyponyms") then  
			--special case if hyponyms, get the "hyponyms" table
			list = list["hyponyms"][v]
		else 
			list = list[v]
		end
	
	end
	
	-- error check so that the last inputted parameter is one division up
	if type(list) ~= "table" then
		error(inputtedDivisions[#inputtedDivisions] .. " has no divisions.")
	end
	
	-- get the keys from the data module & prepare for template call
	local divisionName = list["divisions"]
	if args["type"] == "hyponyms" then 
		divisionName = "specific places"
	end
	
	local excludesText = nil
	if list["excludes"] ~= nil then
		excludesText =  list["excludes"]
	end
	
	-- erase from list before processing the final list
	list["divisions"] = nil
	list["hyponyms"] = nil
	list["excludes"] = nil
	
	-- get final list
	local finalList = nil
	if args["type"] == "hyponyms" then
		finalList = list 
	else 
		finalList = getKeysOfTable(list)
	end
	
	-- I am mixed on whether to autosort the list. 
	-- For now, let's not sort due to performance.
	--table.sort(finalList)

    return finalList, inputtedDivisions, divisionName, excludesText
	
end

local a = {}


-- for use in the definition lines
function a.linksInDefinition( frame )

	local args = frame:getParent().args
	
	local templateToCall = args["type"]
	if templateToCall == nil then
		templateToCall = "coordinate terms"
	end
	

    -- get table
	local finalList, inputtedDivisions, divisionName, excludesText = getTableFromData(args)


	-- prepare parameters for the {{coordinate terms}} (or others) template call
	table.insert(finalList, 1, "en") -- the language parameter
	
	finalList["lb"] = divisionName .. " of " .. inputtedDivisions[#inputtedDivisions] -- the label parameter
	
	if excludesText ~= nil then
		finalList["lb"] = finalList["lb"] .. " <small>(excluding " .. excludesText .. ")</small>"
	end
	
	return frame:expandTemplate{ title = templateToCall , args = finalList }
	
end


-- for use in `See also` headings
function a.linksInSeeAlso( frame )
	
	local args = frame:getParent().args
	
    -- get table
	local finalList, inputtedDivisions, divisionName, excludesText = getTableFromData(args)
	
	-- prepare parameters for the {{col3}} template call
	table.insert(finalList, 1, "en") -- the language parameter
	
	local label = divisionName .. " of " .. inputtedDivisions[#inputtedDivisions] -- label
	
	
	if excludesText ~= nil then
		label = label .. " <small>(excluding " .. excludesText .. ")</small>"
	end
	
	-- no need to use {{q}} to reduce lua usage
	return "(''" .. label .. "''): " .. frame:expandTemplate{ title = "col3-u" , args = finalList }
	
end


return a;