跳转到内容

模組:Namespace detect2

本页使用了标题或全文手工转换
维基百科,自由的百科全书

这是本页的一个历史版本,由SunAfterRain留言 | 贡献2021年2月8日 (一) 02:25 建立内容为“local p = {} local mNS = require('Module:Namespace') local mError = require('Module:Error') local yesno = require('Module:Yesno') local function in…”的新页面)编辑。这可能和当前版本存在着巨大的差异。

(差异) ←上一修订 | 最后版本 (差异) | 下一修订→ (差异)

local p = {}
local mNS = require('Module:Namespace')
local mError = require('Module:Error')
local yesno = require('Module:Yesno')

local function inArray (str, arr)
	for i, v in ipairs(arr) do
		if str == v then
			return v
		end
	end
	return nil
end

local function isNamespace (id)
	id = tostring(id)
	return 
		mw.site.namespaces[tonumber(id) or id:lower()] and mw.site.namespaces[tonumber(id) or id:lower()].id
		or mNS._ispseudoNS(id)
		or id:lower() == 'main' and 0
		or nil
end

function p.main (frame)
	local ns
	local other
	local demospace
	local subjectns
	local CanOutputTalkValue
	local uselist = {}
	require('Module:Arguments').getArgs(frame, {
		valueFunc = function (key, value)
			local isNS = isNamespace(key)
			if isNS then
				if tostring(key):lower() == 'talk' then
					isNS = 'talk'
				end
				if not uselist[isNS] then
					uselist[isNS] = value
				else
					local warnmsg = '警告:命名空間<code>' .. (type(isNS) == 'number' and mNS.NamespacesDisplay('', isNS) or isNS) .. '</code>已被寫入過,無法再次寫入!'
					mw.log(warnmsg)
					mw.addWarning(mError.error(warnmsg))
				end
			elseif tostring(key):lower() == 'other' then
				other = value
			elseif tostring(key):lower() == 'demospace' then
				demospace = isNamespace(value)
			elseif tostring(key):lower() == 'subjectns' then
				subjectns = yesno(value)
			elseif tostring(key):lower() == 'talk' then
				CanOutputTalkValue = yesno(value)
			end
			return nil
		end
	})
	ns = demospace or mw.title.getCurrentTitle().namespace
	if mw.site.talkNamespaces[ns] then
		if subjectns and CanOutputTalkValue then
			return uselist[ns] or uselist[ns - 1] or uselist['other']
		elseif subjectns then
			return uselist[ns - 1] or uselist['other']
		elseif CanOutputTalkValue then
			return uselist[ns] or uselist['talk'] or uselist['other']
		else
			return uselist['talk'] or uselist['other']
		end
	else
		return uselist[ns] or uselist['other']
	end
end

return p