模組:沙盒/Sunny00217/Namespace detect

维基百科,自由的百科全书
文档图示 模块文档[创建]
local p = {}
local arguments = require('Module:Arguments')

local function FatchNS (ns) 
	ns = tonumber(ns) or ns
	if ns then
		local nsobj = mw.site.namespaces[ns]
		if nsobj then
			return nsobj.id
		else
			error('無法識別的命名空間「' .. ns .. '」')
		end
	else
		return mw.title.getCurrentTitle().namespace
	end
end

local function isTalkPage (id)
	return id > -3 and id % 2 ~= 0
end

local function isInArray (array, id)
	for i, v in ipairs(array) do
		if v == id then
			return i
		end
	end
	return 0
end

function p._main (args)
	local nsmsg = {}
	local demons
	nsmsg.default = ''
	for k, v in args do
		k = k:lower()
		if k == 'demospace' then
			demons = FetchNS(v)
		elseif k == 'default' then
			nsmsg['default'] = v
		elseif k == 'main' then -- main 不是有效的命名空間,要手動指定
			nsmsg['0'] = v
		elseif k == 'talk' then -- 所有討論頁
			nsmsg['talk'] = v
		elseif k == 'main talk' then -- 專指條目的討論頁
			nsmsg['1'] = v
		else
			nsmsg[tostring(FetchNS(k))] = v
		end
	end
end

return p