Difference between revisions of "Module:See also if exists"

From blackwiki
Jump to navigation Jump to search
blackwiki>BrownHairedGirl
(convert)
blackwiki>Pppery
Line 2: Line 2:
 
]]
 
]]
 
local p = {}
 
local p = {}
 
+
local yesno = require("Module:yesno")
function doesPageExist(thisPage)
 
local thisPage = mw.title.new( thisPage )
 
if thisPage == nil then
 
return false
 
end
 
if thisPage.exists then
 
return true
 
end
 
return false
 
end
 
 
 
  
 
function p.main(frame)
 
function p.main(frame)
 
local rawpages = {}
 
local rawpages = {}
 
local nvalid = 0
 
local nvalid = 0
for i, v in ipairs(mw.getCurrentFrame():getParent().args) do
+
local namespace = frame.args.ns
 +
for i, v in ipairs(frame:getParent().args) do
 
if (v ~= nil) then
 
if (v ~= nil) then
 
local thisArg = mw.text.trim(v)
 
local thisArg = mw.text.trim(v)
 
if (thisArg ~= "") then
 
if (thisArg ~= "") then
if ( doesPageExist(thisArg)) then
+
local title = mw.title.new(thisArg, namespace)
table.insert(rawpages, thisArg)
+
if title ~= nil and title.exists then
 +
table.insert(rawpages, title.fullText)
 
nvalid = nvalid + 1
 
nvalid = nvalid + 1
 
end
 
end
Line 30: Line 21:
 
end
 
end
 
if (nvalid == 0) then
 
if (nvalid == 0) then
 +
if yesno(frame.args.warning) then
 +
if namespace:sub(-1) == "y" then
 +
namespace = namespace:sub(0, -2) .. "ie"
 +
elseif namespace == nil then
 +
namespace = "page"
 +
end
 +
mw.addWarning(string.format("'''[[%s]] — no output, because none of the %ss currently exist.'''",
 +
frame:getParent():getTitle(),namespace))
 +
end
 
return ""
 
return ""
 
end
 
end

Revision as of 21:38, 24 March 2019

{{Module rating }}

Usage

Used by Template:see also if exists and Template:Category see also if exists.

{{#invoke:See also if exists| main}}


--[[ v1.0
]]
local p = {}
local yesno = require("Module:yesno")

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 title.exists 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:sub(-1) == "y" then
				namespace = namespace:sub(0, -2) .. "ie"
			elseif namespace == nil then
				namespace = "page"
			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