Difference between revisions of "Module:ExistNotRedirect/sandbox"

From blackwiki
Jump to navigation Jump to search
blackwiki>Psiĥedelisto
(Better error handling)
blackwiki>Nardog
(sample)
Line 1: Line 1:
 
local p = {}
 
local p = {}
 
  
 
function p.main(args)
 
function p.main(args)
Line 7: Line 6:
 
local forceFileOnly = myArgs.forcefile -- force File: namespace check only
 
local forceFileOnly = myArgs.forcefile -- force File: namespace check only
 
 
if (myPageName == nil) or (myPageName == "") then
+
if not myPageName or myPageName == "" then
 
return ""
 
return ""
 
end
 
end
 
 
if string.match(myPageName, "^:?File:") and not forceFileOnly then
 
myPageName = myPageName:gsub("^:?File:", "Media:")
 
end
 
 
 
local myPageTitle = mw.title.makeTitle("", myPageName)
 
local myPageTitle = mw.title.makeTitle("", myPageName)
 
if not myPageTitle then
 
if not myPageTitle then
 
error("Invalid page title passed, MediaWiki cannot understand it", 1)
 
error("Invalid page title passed, MediaWiki cannot understand it", 1)
 
end
 
end
if myPageTitle.exists then
+
if myPageTitle.exists
if myPageTitle.isRedirect then
+
or not forceFileOnly and myPageTitle.file and myPageTitle.file.exists
return ""
+
then
else
+
if not myPageTitle.isRedirect then
 
return myArgs[1]
 
return myArgs[1]
 
end
 
end
 
end
 
end
 
+
 
return ""
 
return ""
 
end
 
end
  
 
return p
 
return p

Revision as of 11:48, 11 July 2020

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

local p = {}

function p.main(args)
	local myArgs = mw.getCurrentFrame():getParent().args
	local myPageName = myArgs[1]
	local forceFileOnly = myArgs.forcefile -- force File: namespace check only
	
	if not myPageName or myPageName == "" then
		return ""
	end
	
	local myPageTitle = mw.title.makeTitle("", myPageName)
	if not myPageTitle then
		error("Invalid page title passed, MediaWiki cannot understand it", 1)
	end
	if myPageTitle.exists
		or not forceFileOnly and myPageTitle.file and myPageTitle.file.exists
	then
		if not myPageTitle.isRedirect then
			return myArgs[1]
		end
	end
	
	return ""
end

return p