跳转到内容

模組:TemplateExist

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

这是本页的一个历史版本,由Xiplus-abot留言 | 贡献2024年1月26日 (五) 20:51 (已保护“Module:TemplateExist”:​高風險模板:30187引用<!-- 機器人3 -->([编辑=仅允许模板编辑员](无限期)[移动=仅允许模板编辑员](无限期)))编辑。这可能和当前版本存在着巨大的差异。

local p = {}
local yesno = require("Module:Yesno")
function p.templateExist(input_data)
	local template_name = input_data
	local count = 1
	local return_count = false
	local currentTitle = mw.title.getCurrentTitle()
	if type(input_data) == type({}) and type(input_data.args) == type({}) then
		template_name = input_data.args[1] or input_data.args['1'] or ''
		local _count = tonumber(input_data.args.count)
		if type(_count) == type(nil) then --count參數不是數字時
			--檢查count參數是不是布林值
			return_count = yesno(tostring(input_data.args.count))
		end
		count = _count or count
		--是否檢查特定頁面
		local page = input_data.args.page
		if mw.text.trim(page or '') ~= '' then --有輸入頁面名稱
			--檢查頁面名稱是否合法
			local success, page_obj = pcall(mw.title.new, page)
			if success and type((page_obj or {}).getContent) == type(tostring) then
				currentTitle = page_obj
			end
		end
	end
	--取得判斷目標的模板
	local template_title = p.getTemplateTitle(template_name)
	--模板計數
	local template_count = 0
	if --如果模板和待判斷頁面都合法
		type((template_title or {}).getContent) == type(tostring) and
		type((currentTitle or {}).getContent) == type(tostring) 
	then
		--取得待判斷頁面內容
		local content = currentTitle:getContent()
		--檢查待判斷頁面中所有格式為 {{XXX 的內容
		for capture in mw.ustring.gmatch(content, "%{%{%s*([^%{%}%|]+)%s*[%}%|]") do
			--解析出 {{XXX 的模板名稱
			local check_template_name = mw.text.trim(capture or '')
			local check_module = mw.ustring.match(check_template_name, "%s*#%s*[Ii][Nn][Vv][Oo][Kk][Ee]%s*:%s*([^%{%}%|]+)%s*$")
			if check_module then check_template_name = "Module:"..check_module end
			--取得該模板名稱對應的模板物件
			local check_template_title = p.getTemplateTitle(check_template_name)
			if check_template_title then
				--看看該模板跟待判斷的模板是否為同一個
				if mw.title.equals(check_template_title, template_title) then
					template_count = template_count + 1
				end
			end
		end
	end
	return return_count --偵測完畢,返回結果
		and template_count --如果是模板計數,返回指定模板數量
		or (template_count >= count) --返回模板存在與否
end

function p.getTemplateTitle(input_data)
	local template_name = input_data
	local is_lua = true
	if type(input_data) == type({}) and type(input_data.args) == type({}) then
		--讀取模板呼叫的參數
		template_name = input_data.args[1] or input_data.args['1'] or ''
	end
	local function _return(_value) --區分模板呼叫或模組呼叫
		if is_lua then return _value
		else return _value and tostring(_value) or '' end
	end
	template_name = mw.text.trim(template_name or '')
	--未輸入,返回空
	if template_name == '' then return _return() end
	--嘗試取得待偵測模板
	local success, template_obj = pcall(mw.title.new, template_name, "Template")
	--取得失敗返回空
	if (not success) or type((template_obj or {}).getContent) ~= type(tostring) then return _return() end
	--取得模板原名 (模板可能是重定向)
	local page_name = tostring(template_obj)
	local template_title = (template_obj or{}).redirectTarget or template_obj
	local redircct_name = tostring(template_title)
	--如果模板不能讀取內容 (它是假的模板) 返回空
	if type((template_title or{}).getContent) ~= type(tostring)then return _return() end
	--如果模板存在,返回模板物件
	if type(template_title:getContent()) == type("string") then
		return _return(template_title)
	end
	local lib_zhcvt = require('Module:ZhConversion') --繁簡轉換
	for _, check_title in ipairs({ --列舉繁簡的可能
		lib_zhcvt.to_hant(page_name), lib_zhcvt.to_hans(page_name),
		lib_zhcvt.to_hant(redircct_name), lib_zhcvt.to_hans(redircct_name),
	}) do --檢查各種繁簡模式
		--嘗試取得該繁簡模式的模板
		local _success, check_page = pcall(mw.title.new, check_title, "Template")
		--如果是可讀取的模板
		if _success and type(check_page.getContent) == type(tostring) then
			--排除重新導向
			check_page = check_page.redirectTarget or check_page
			--模板存在,返回模板物件
			if type(check_page:getContent()) == type("string") then
				return _return(check_page)
			end
		end
	end
	--測試不通過,返回空
	return _return()
end
return p