模組:EditState
外观
![]() | 此模块被引用於約705,000個頁面。 為了避免造成大規模的影響,所有對此模块的編輯應先於沙盒或測試樣例上測試。 測試後無誤的版本可以一次性地加入此模块中,但是修改前請務必於討論頁發起討論。 模板引用數量會自動更新。 |
本模組提供一些能偵測頁面被編輯之狀況的函數。
函數說明
isPreview
用來偵測頁面是否處於預覽狀態。
previewNotice
提供在頁面預覽狀態添加警告以提醒編者的功能。
isCreating
用來偵測頁面是否處於「建立中」的狀態。
hasParent
檢查模板呼叫是否存在外層。
getTitle
獲得當下模板呼叫的標題。
nextSubst
讓替換引用延遲運行的函數。
local p={}
local lib_arg={};
local yesno = {}
function p.isPreview()
local frame = mw.getCurrentFrame()
local Preview_mode = frame:preprocess('{{REVISIONID}}'); -- use magic word to get revision id
return (Preview_mode == nil or mw.text.trim(Preview_mode or '') == '') -- if there is a value then this is not a preiview
end
function p.isCreating()
local frame = mw.getCurrentFrame()
local Preview_mode = frame:preprocess('{{PAGEID}}');
return (Preview_mode == nil or mw.text.trim(Preview_mode or '') == '')
end
function p.hasParent(layer)
local frame = mw.getCurrentFrame()
local times = tonumber(layer or '') or 0
local Parent = frame:getParent();
for i=2,tonumber(times) do
Parent = Parent:getParent()
if Parent == nil then break end
end
return not (Parent == nil or Parent == '')
end
function p.getTitle(layer)
local frame = mw.getCurrentFrame()
local times = tonumber(layer or '') or 0
if times <= 0 then return frame:getTitle()end
local Parent = frame:getParent();
for i=2,tonumber(times) do
Parent = Parent:getParent()
if Parent == nil then return '' end
end
return Parent:getTitle();
end
function p.makeTemplateParameter(frame) --產生模板參數字串
local args, working_frame
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.
args = {}
for key,value in frame:argumentPairs() do
args[key] = value or ''
end
local Parent = frame:getParent();
if Parent then for key,value in Parent:argumentPairs() do
args[key] = args[key] or value or ''end
end
working_frame = frame
else
-- We're being called from another module or from the debug console, so assume
-- the args are passed in directly.
args = frame
working_frame = mw.getCurrentFrame()
if type(args) ~= type({}) then args = {frame} end
end
local argname = args[1] or args['1'] or args['name']
local argDefault = args[2] or args['2'] or args['default']
local default_text = ''
if argname then
argname = mw.text.trim(argname or '')
if argDefault then
default_text = '|' .. mw.text.trim(argDefault or '')
end
else
argname, default_text = '', '|' .. mw.text.trim(argDefault or '')
end
if mw.text.trim(argname .. default_text) == '' then return '' end
local result = '{{{' .. argname .. default_text .. '}}}'
if args[4] or args['4'] then return result end
if p.isPreview() or (mw.isSubsting() and (args[3] or args['3'])) then
return (working_frame:getParent() or working_frame):preprocess(result)
end
return result
end
function p.call(frame) --轉換模板參數為lua參數
local args, working_frame
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 lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
args = lib_arg.getArgs(frame, {parentFirst=true})
working_frame = frame
else
-- We're being called from another module or from the debug console, so assume
-- the args are passed in directly.
args = frame
working_frame = mw.getCurrentFrame()
if type(args) ~= type({}) then args = {frame} end
end
local max_arg = 0
local num_args = {}
local isJSON = false
if args.isJSON then
if type(yesno) ~= type(tonumber) then yesno = require('Module:Yesno') end
if yesno(args.isJSON or 'no') then --local delnowiki=require('Module:Delcat').delnowiki --備用
isJSON = true
end
end
for key,value in pairs( args ) do
local arg_id = tonumber(key or '')
if arg_id then
if arg_id > max_arg then max_arg = arg_id end
num_args[arg_id] = value
end
end
local func_name = num_args[1]
if mw.text.trim(func_name or '') == '' then return '' end
local call_args = {}
local get_obj = function(obj)return obj end
if isJSON then get_obj = function(obj)return mw.text.jsonDecode(obj)end end
if max_arg > 1 then
for i = 2,max_arg do
if num_args[i] then
local try_to_get_obj = mw.clone(num_args[i])
if not xpcall( function() --try
try_to_get_obj = get_obj(num_args[i])
end, function(msg)return msg end ) then --catch
call_args[i-1] = num_args[i]
else --finally
call_args[i-1] = try_to_get_obj
end
else call_args[i-1] = '' end
end
end
local func_path = mw.text.split(func_name,'%.') or { [1]=func_name }
local func_body = mw[func_path[1]] or p[func_path[1]] or _G[func_path[1]]
if mw.ustring.find(func_path[1],':') then
func_body = require(func_path[1]) or func_body
end
if func_body then
local old_obj = func_body
for i=2,#func_path do
func_body = func_body[func_path[i]]
if func_body then
old_obj = func_body
else
return ''
end
end
local func_type = type( func_body )
local times = 10
for ti = 1,times do
if func_type == type(nil) then
return ''
elseif func_type == type(0) then
return '' .. func_body
elseif func_type == type(true) then
if func_type then return '1' end
return ''
elseif func_type == type(type) then
if max_arg > 1 then
func_body = func_body(unpack(call_args))
if func_body == nil then func_body = call_args[1] end
else func_body = func_body() end
elseif func_type == type("string") then
return func_body
elseif func_type == type({}) then
return mw.text.jsonEncode(func_body)
end
func_type = type( func_body )
end
end
return ''
end
return p