Module:Namespace

来自Kyoshihira Encyclopedia
Lhscogs​(留言 | 贡献)2026年10月20日 (二) 01:25的版本 (导入1个版本)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转到导航 跳转到搜索

此模块的文档可以在Module:Namespace/doc创建

require('strict')

local p = {}
local data = mw.loadData('Module:Namespace/data')
local mLan = require('Module:Lan2')

local getArgs

local function ifempty(ret, val)
	return ret ~= '' and ret or val
end

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

-- @internal
-- @see [[Module:Namespace pagename]]
function p._lan(args, lang)
	return mLan.main(args,
		ifempty(lang and lang:lower(), mw.getCurrentFrame():callParserFunction { name = 'int', args = { 'Lang' } }), {
		['def'] = { 'en' }
	})
end

-- 檢查一個輸入是不是偽命名空間或真實命名空間,返回偽命名空間、命名空間編號或 nil。
-- @param string|number id
-- @return string|number|nil
function p._isNamespace(id)
	id = tostring(id)
	local PNS, _ = p._isPseudoNamespace(id)
	return
		PNS
		or mw.site.namespaces[tonumber(id) or id:lower()] and mw.site.namespaces[tonumber(id) or id:lower()].id
		or nil
end

function p.namespace(frame)
	local args
	if frame == mw.getCurrentFrame() then
		-- We're being called via #invoke. The args are passed through to the module
		-- from the template page, so use the args that were passed into the template.
		if not getArgs then
			getArgs = require('Module:Arguments').getArgs
		end
		args = getArgs(frame, { parentFirst = true })
	else
		-- We're being called from another module or from the debug console, so assume
		-- the args are passed in directly.
		args = frame
		if type(args) ~= type({}) then args = { frame } end
	end
	local id = tostring(args.namespace or args[1])
	local PNS, _ = p._isPseudoNamespace()
	return (PNS or mw.title.getCurrentTitle().namespace) == p._isNamespace(id) and (args['then'] or args[2]) or (args['else'] or args[3])
end

local UNKNOWN_NAMESPACE = -3

for key, func in pairs({
	namespaceName = function(nsid)
		return data.Namespace[tostring(nsid)]
	end,
	conversionNamespaceName = function(nsid)
		return data.Namespace[tostring(nsid) .. '-display'] or data.Namespace[tostring(nsid)]
	end,
}) do
	-- @param string|nil [lang]
	-- @param string|nil [ns]
	-- @throw
	-- @return string
	p['_' .. key] = function(lang, ns)
		local PNS, PNSIsTalk
		local nsid
		if ns ~= nil then
			if ns == '' then
				nsid = UNKNOWN_NAMESPACE
			else
				local nsid_info = mw.site.namespaces[tonumber(ns)] or mw.site.namespaces[ns]
				if not nsid_info then
					PNS, PNSIsTalk = p._isPseudoNamespace(ns, false)
					nsid = UNKNOWN_NAMESPACE
				else
					nsid = nsid_info.id
				end
			end
		else
			local title = mw.title.getCurrentTitle()
			nsid = title.namespace
			if nsid == 0 or nsid == 1 then
				PNS, PNSIsTalk = p._isPseudoNamespace(title, true)
			end
		end
		if PNS then
			return p._pseudoNamespaceDisplay(lang, PNS, PNSIsTalk)
		elseif nsid <= UNKNOWN_NAMESPACE then
			error('Input namespace error.')
		end
		return p._lan(func(nsid), lang)
	end

	p[key] = function(frame)
		if not getArgs then
			getArgs = require('Module:Arguments').getArgs
		end

		local args = getArgs(frame, {
			valueFunc = function(k, v)
				if k == 2 then
					return type(v) == 'string' and mw.text.trim(v) or v
				elseif v then
					v = mw.text.trim(v)
					if v ~= '' then
						return v
					end
				end
				return nil
			end
		})
		local lang = args[1]
		local ns = args[2]
		local statue, wt = pcall(p['_' .. key], lang, ns)
		if statue then
			return wt
		end
		return tostring(mw.html.create('span'):attr('style', 'color:red;'):wikitext(
			p._lan({
				['en'] = 'Input namespace error',
				['zh-hans'] = '输入命名空间错误',
				['zh-hant'] = '輸入命名空間錯誤'
			}, lang)
		):done())
	end
end

-- @deprecated 命名不當
p.NamespacesDisplay = p.namespaceName
-- @deprecated 命名不當
p._NamespacesDisplay = p._namespaceName
-- @deprecated 命名不當
p.NamespacesConversionDisplay = p.conversionNamespaceName
-- @deprecated 命名不當
p._NamespacesConversionDisplay = p._conversionNamespaceName

-- @param string|nil ns
-- @param false|nil [isPage]
-- @throw
-- @return nil|string, boolean
-- @overload function _isPseudoNamespace(ns, isPage)
-- ----
-- @param string|mw.title|nil page
-- @param true isPage
-- @throw
-- @return nil|string, boolean
-- @overload function _isPseudoNamespace(page, isPage)
function p._isPseudoNamespace(nsOrPage, isPage)
	if not nsOrPage then
		nsOrPage = mw.title.getCurrentTitle()
		isPage = true
	elseif nsOrPage == '' then
		return nil, false
	end

	local success, titleObj
	if type(nsOrPage) == type('') then
		if not nsOrPage:match(':') then
			nsOrPage = nsOrPage .. ':'
		end
		success, titleObj = pcall(mw.title.new, nsOrPage)
		if not success or not titleObj or not titleObj:inNamespaces(0, 1) then
			return nil, false
		end

		local split = mw.text.split(titleObj.text, ':')
		if not isPage and (#split > 2 or (split[2] ~= '' and split[2] ~= nil)) then
			-- isPage = false 時僅允許:
			-- 1. PNS
			-- 2. PNS:
			return nil, false
		end
		nsOrPage = split[1] -- 移除冒號後的東西
	elseif type(nsOrPage) == type({}) and type(nsOrPage.text) == type('') and isPage then
		-- 如果傳入頁面物件,則 isPage 必然要是 true
		if not nsOrPage:inNamespaces(0, 1) then
			return nil, false
		end

		titleObj = nsOrPage
		nsOrPage = mw.text.split(nsOrPage.text, ':')[1] -- 移除冒號後的東西
	else
		error(string.format(
			'bad argument #1 to \'_isPseudoNamespace[isPage=%s]\' (%s expected, got %s)',
			isPage and 'true' or 'false',
			isPage and 'string or mw.title object' or 'string',
			type(nsOrPage)
		))
	end
	if inArray(nsOrPage, data['PseudoNamespace']['list']) then
		return nsOrPage, titleObj.namespace == 1
	end
	return nil, false
end

function p.isPseudoNamespace(frame)
	local arg = require('Module:Arguments').getArgs(frame)['1']
	local PNS, _ = p._isPseudoNamespace(arg, true)
	return PNS or ''
end

-- @alias
p.ispseudo = p.isPseudoNamespace

-- @internal
-- @see [[Module:Namespace pagename]]
function p._pseudoNamespaceDisplay(lang, ns, isTalk)
	if not inArray(ns, data['PseudoNamespace']['list']) then
		error('Input pseudo namespace error.')
	end

	local display = p._lan(data.PseudoNamespace.messages[isTalk and 'talk' or 'main'], lang)
	display = mw.ustring.gsub(display, '$1', p._lan(data.PseudoNamespace[ns], lang))
	return display
end

-- @deprecated 命名不當
p._PseudoNamespaceDisplay = p._pseudoNamespaceDisplay

return p