Jump to content

Module:See also if exists: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Fix bug when warning is provided without namespace
Don't Lua error when out of expensive parser function calls
Line 3: Line 3:
local p = {}
local p = {}
local yesno = require("Module:yesno")
local yesno = require("Module:yesno")
function exists(title)

local success, result = pcall(function() return title.exists end)
if success then
return result
else
return true
end
end
function p.main(frame)
function p.main(frame)
local rawpages = {}
local rawpages = {}
Line 13: Line 20:
if (thisArg ~= "") then
if (thisArg ~= "") then
local title = mw.title.new(thisArg, namespace)
local title = mw.title.new(thisArg, namespace)
if title ~= nil and title.exists then
if title ~= nil and exists(title) then
table.insert(rawpages, title.fullText)
table.insert(rawpages, title.fullText)
nvalid = nvalid + 1
nvalid = nvalid + 1

Revision as of 20:40, 9 January 2025

--[[ v1.0
]]
local p = {}
local yesno = require("Module:yesno")
function exists(title)
	local success, result = pcall(function() return title.exists end)
	if success then
		return result
	else
		return true
	end
end
function p.main(frame)
	local rawpages = {}
	local nvalid = 0
	local namespace = frame.args.ns
	for i, v in ipairs(frame:getParent().args) do
		if (v ~= nil) then
			local thisArg = mw.text.trim(v)
			if (thisArg ~= "") then
				local title = mw.title.new(thisArg, namespace)
				if title ~= nil and exists(title) then
					table.insert(rawpages, title.fullText)
					nvalid = nvalid + 1
				end
			end
		end
	end
	if (nvalid == 0) then
		if yesno(frame.args.warning) then
			if namespace == nil then
				namespace = "page"
			elseif namespace:sub(-1) == "y" then
				namespace = namespace:sub(0, -2) .. "ie"
			end
			mw.addWarning(string.format("'''[[%s]] — no output, because none of the %ss currently exist.'''",
				frame:getParent():getTitle(),namespace))
		end
		return ""
	end
	local mLabelledList = require('Module:Labelled list hatnote')
	local pages = mLabelledList._labelledList(rawpages, "See also", "")
	return pages
end

return p