Module:PortalList

From No Subject
Jump to navigation Jump to search

Documentation for this module may be created at Module:PortalList/doc

-- Module:PortalList
-- Renders a list of wiki links from a data module.
-- Metadata-aware (future-proof), but metadata-agnostic (no UI change).

local p = {}

----------------------------------------------------------------------
-- Internal helpers
----------------------------------------------------------------------

-- Safely get page title from an item
local function getPage(item)
	if type(item) == "table" then
		return item.page
	end
	return nil
end

-- Safely get display label
local function getLabel(item)
	if type(item) == "table" then
		return item.label or item.page
	end
	return nil
end

-- Metadata accessor (unused for now, reserved for future steps)
local function getMeta(item, key)
	if type(item) == "table" then
		return item[key]
	end
	return nil
end

----------------------------------------------------------------------
-- Main render function
----------------------------------------------------------------------

function p.render(frame)
	local args = frame.args
	local moduleName = args.module
	local key = args.key

	if not moduleName or not key then
		return "<!-- PortalList error: module and key required -->"
	end

	-- Load data module safely
	local ok, data = pcall(require, "Module:" .. moduleName)
	if not ok or type(data) ~= "table" then
		return "<!-- PortalList error: module not found -->"
	end

	local list = data[key]
	if type(list) ~= "table" then
		return "<!-- PortalList error: key not found -->"
	end

	local out = {}
	local hasMissing = false

	for _, item in ipairs(list) do
		local page = getPage(item)
		if page then
			local label = getLabel(item) or page
			local title = mw.title.new(page)

			if title and not title.exists then
				hasMissing = true
			end

			table.insert(
				out,
				"* [[" .. page .. "|" .. label .. "]]"
			)
		end
	end

	-- Silent maintenance category
	if hasMissing then
		table.insert(out, "[[Category:Portals with missing pages]]")
	end

	return table.concat(out, "\n")
end

return p