模組:TemplateExist
外观
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 mw.title.equals(check_template_title, template_title) then
template_count = template_count + 1
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 then return _return() end
local page_name = tostring(template_obj)
local template_title = template_obj.redirectTarget or template_obj
local redircct_name = tostring(template_title)
if type(template_title.getContent) ~= type(tostring)then return _return() end
if (template_title:getContent() or ''):len() > 0 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 then
check_page = check_page.redirectTarget or check_page
return _return(check_page)
end
end
return _return()
end
return p