跳转到内容

模組:Namespace pagename

被永久保护的模块
维基百科,自由的百科全书

这是本页的一个历史版本,由SunAfterRain留言 | 贡献2022年10月14日 (五) 08:29编辑。这可能和当前版本存在着巨大的差异。

local p = {}
local yesno = require('Module:Yesno')
local mError = require('Module:Error')
local mNS = require('Module:Namespace')
local data = mw.loadData('Module:Namespace_pagename/data')

local lan = mNS._lan

local function getNSbyID(key, lang) 
	return mNS._NamespacesDisplay({lang, key})
end

local function msg(group, key, lang) 
	return lan(data[tostring(group)][tostring(key)] or {}, lang)
end

local function makeError(msg)
	return mError.error{'[[Module:Namespace_pagename]]錯誤:' .. msg}
end

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

local no_need_suffix = {-2, 0, 14, 100, 118, 119, 2300, 2600}

local can_add_extend_suffix = {2, 10, 828}
	
local function def_suffix (ns, lang)
	if ns == -1 then
		return lan({
			['en'] = ' page',
			['zh-hans'] = '页面',
			['zh-hant'] = '頁面'
		}, lang)
	elseif ns == 2 then
		return lan({
			['en'] = ' page',
			['zh-hans'] = '页',
			['zh-hant'] = '頁',
			['zh-tw'] = '頁面' -- 使用者頁面
		}, lang)
	elseif inArray(ns, {-2, 0, 6, 10, 14, 100, 102, 118, 828, 2300}) then
		return ''
	else
		return lan({
			['en'] = ' page',
			['zh-hans'] = '页',
			['zh-hant'] = '頁'
		}, lang)
	end
end

p._ispseudo = mNS._ispseudo
p.ispseudo = mNS.ispseudo

function p._namespace(arg, nsid, lang)
	arg = arg or (nsid and '' or mw.title.getCurrentTitle().fullText)
	nsid = nsid or mw.title.getCurrentTitle().namespace
	if p._ispseudo(arg) then --偽命名空間
		return getNSbyID(nsid == 1 and 'Talk:' .. p._ispseudo(arg) or p._ispseudo(arg), lang)
	end
	local statue, namespace = pcall(getNSbyID, nsid, lang)
	if not statue then
		error(string.format(msg('Error', 'ns-error', lang), 'namespace number = ' .. nsid))
	end
	return namespace
end

function p._namespace_pagename(title, lang)
	title = title or mw.title.getCurrentTitle()
	local namespace = p._namespace(title.fullText, title.namespace, lang)
	local nsid = title.namespace
	local content = title:getContent() or ''
	local contentModel = title.contentModel
	local titletext = title.fullText
	local subtext = title.subpageText
	if contentModel == 'css' or contentModel == 'sanitized-css' then
		return namespace .. msg('Type', 'style', lang)
	elseif contentModel == 'javascript' then
		return namespace .. msg('Type', 'script', lang)
	elseif contentModel == 'json' then
		return namespace .. msg('Type', 'JSON', lang)
	elseif contentModel == 'json' then
		return namespace .. msg('Type', 'JSON', lang)
	elseif title.isRedirect then
		return namespace .. msg('Type', 'redirect', lang)
	end
	if inArray(nsid, no_need_suffix) then
		return namespace 
	end
	if 
		(
			content:match('%{%{[Dd]ocumentation[_%s]subpage')
			or content:match('%{%{[Tt]emplate[_%s]doc[_%s]inline')
			or content:match('%{%{[Tt]emplate[_%s]doc[_%s]viewed[_%s]directly')
			or subtext:match('^[Dd]oc$')
		) and inArray(nsid, can_add_extend_suffix)
	then
		return namespace .. msg('Type', 'doc', lang)
	elseif 
		content:match('%{%{[Ss]andbox')
		or content:match('%{%{[Tt]emplate[_%s]sandbox[_%s]notice')
		or content:match('%{%{請注意:請在這行文字底下進行您的測試,請不要刪除或變更這行文字以及這行文字以上的部份。')
		or content:match('%{%{请注意:请在这行文字底下进行您的测试,请不要删除或变更这行文字以及这行文字以上的部分。')
		or content:match('%{%{[Pp]D%-self%/沙盒%|')
		or nsid > 1 and titletext:gsub('^[A-z][a-z]+%:', '') == '沙盒'
		or titletext:match('^File%:沙盒%.[A-Za-z]+$')
		or titletext:match('^Template%:沙盒%/')
		or titletext:match('^Template%:X%d')
		or (
			(
				subtext:match('^[Ss]andbox$')
				or subtext == '沙盒'
			)
			and inArray(nsid, can_add_extend_suffix)
		)
	then
		return namespace .. msg('Type', 'sandbox', lang)
	elseif 
		(
			content:match('%{%{[Tt]emplate[_%s]testcases[_%s]notice')
			or content:match('%{%{[Tt]est[_%s]cases[_%s]notice')
			or content:match('%{%{测试样例')
			or content:match('%{%{測試樣例')
			or subtext:match('^[Tt]estcases$')
			or subtext == '测试样例'
			or subtext == '測試樣例'
		)
		and inArray(nsid, can_add_extend_suffix)
	then
		return namespace .. msg('Type', 'testcases', lang)
	elseif 
		(
			titletext:match('%/[Tt]emp$')
			or titletext:match('%/[Tt]emporary$')
		)
		and inArray(nsid, {2, 4, 10, 118, 828})
	then
		return namespace .. msg('Type', 'temp', lang)
	elseif
		nsid == 10
		and (
			subtext:match('^[Ii]ntro$')
			or subtext:match('^[Ii]ntroduction$')
			or subtext == '说明'
			or subtext == '說明'
		)
	then
		return namespace .. msg('Type', 'intro', lang)
	elseif
		titletext:match('^Template%:Editnotices%/')
		or titletext:match('^User%:[^/]+%/Editnotice$')
		or titletext:match('^User[_%s]talk%:[^/]+%/Editnotice$')
	then
		return msg('Type', 'editnotice', lang)
	elseif
		titletext:match('^User%:[^/]+%/Emailnotice$')
	then
		local user = titletext:gsub('^User%:([^/]+)%/Emailnotice$', '%1')
		return '[[User:' .. user .. '|' .. user .. ']]' .. lan({
			['en'] = "'s ",
			['zh-hans'] = '的',
			['zh-hant'] = '的'
		}, lang) .. msg('Type', 'mailnotice', lang)
	elseif
		titletext:match('{{捷徑重定向([^}]+)}}')
		or titletext:match('{{捷径重定向([^}]+)}}')
		or titletext:match('{{快捷重定向([^}]+)}}')
		or titletext:match('{{快捷方式重定向([^}]+)}}')
		or titletext:match('{{RTL([^}]+)%|捷徑')
	then
		return string.format(msg('Type', 'shortcut', lang), wt)
	end

	local output = (namespace .. def_suffix(nsid, lang) or ''):gsub('talk page', 'talkpage')
	return output
end

-- function p._main(lang, arg) -> string
-- function p._main({lang, arg}) -> string
function p._main(lang, arg)
	if type(lang) == type({}) then
		arg = lang[2] or lang['2'] or nil
		lang = lang[1] or lang['1'] or nil
	end
	local success, title
	local nsid
	if arg then
		if arg == '' then
			return getNSbyID(0, lang)
		elseif arg:match('^([Pp]age):') then -- 兼容:早期版本
			arg = arg:gsub('^[Pp]age:', '')
			success, title = pcall(mw.title.new, arg)
			if not success or not title then
				return makeError(string.format(msg('Error', 'pg-error', lang), arg))
			end
			nsid = title.namespace
		elseif arg:match('^([Nn]ame):') then -- 兼容:早期版本
			title = nil
			arg = arg:gsub('^([Nn]ame):', '')
			nsid_info = mw.site.namespaces[tonumber(arg)] or mw.site.namespaces[arg]
			if mw.text.split(arg, ':')[2] and not nsid_info and not p._ispseudo(arg) then
				return makeError(string.format(msg('Error', 'ns-error', lang), arg))
			end
			nsid = nsid_info and nsid_info.id
		else
			success, title = pcall(mw.title.new, arg)
			local ns = mw.site.namespaces[tonumber(arg)] or mw.site.namespaces[arg]
			if not success or not title then -- args2 無法被mediawiki正確解析(aka 標題不可能存在)
				return makeError(string.format(msg('Error', 'pg-error', lang), arg))
			elseif ns then -- args2 是{{ns:}}能解析的
				title = nil
				nsid = ns.id
			else -- 其他正常標題,無效的命名空間也被當條目處理
				nsid = title.namespace
			end
		end
	else -- 以標題讀取
		arg = mw.title.getCurrentTitle().fullText
		title = mw.title.getCurrentTitle()
		nsid = title.namespace
	end

	if title and title.isExternal then
		return msg('Type', 'External', lang)
	end

	local statue, output
	if title then
		statue, output = pcall(p._namespace_pagename, title, lang)
	else
		statue, output = pcall(p._namespace, arg, nsid, lang)
	end
	return statue and output or makeError(output)
end

function p.main(frame) 
	local args = require('Module:Arguments').getArgs(frame, {
		valueFunc = function (key, value)
			if key == 2 then
				return type(value) == 'string' and mw.text.trim(value) or value
			elseif value then
				value = mw.text.trim(value)
				if value ~= '' then
					return value
				end
			end
			return nil
		end
	})
	return p._main(args)
end

return p