模組:Special wikitext/Template

local p={}
--{{#invoke:Special wikitext/Template|contentModel}}
function p.contentModel(frame)
	local page_name = frame.args[1] or frame.args['1'] or ''
	local default_namespace = mw.text.trim(frame.args[2] or frame.args['2'] or '')
	local page_data = (mw.text.trim(page_name)==''and mw.title.getCurrentTitle() or mw.title.new(page_name,default_namespace))
	return (page_data or {}).contentModel or 'wikitext'
end
--{{#invoke:Special wikitext/Template|luaContentFromPage}}
function p.luaContentFromPage(frame)
	local page_name = frame.args[1] or frame.args['1'] or ''
	local default_namespace = mw.text.trim(frame.args[2] or frame.args['2'] or '')
	local page_data = (mw.text.trim(page_name)==''and mw.title.getCurrentTitle() or mw.title.new(page_name, default_namespace))
	local contentModel = mw.ustring.lower((page_data or {}).contentModel or 'wikitext')
	if contentModel == 'scribunto' or contentModel == 'lua' then
		pcall(require, page_data.prefixedText) -- if it fails, we don't care
		local moduleWikitext = package.loaded["Module:Module wikitext"]
		if moduleWikitext then
			return moduleWikitext.main()
		end
	end
	return ''
end

--{{#invoke:Special wikitext/Template|markinclude}}
function p.markinclude(frame)
	local page_name = frame.args[1] or frame.args['1'] or ''
	local detail = mw.text.trim(frame.args[2] or frame.args['2'] or '')
	local page_data = (mw.text.trim(page_name)==''and mw.title.getCurrentTitle() or mw.title.new(page_name))
	if page_data:getContent() then -- recorded as a transclusion, and check exist.
		return '*[[:'..page_name..']]' .. (detail ~= '' and ('('..detail..')') or '')
	elseif mw.text.trim(page_name) ~= '' then
		return '*'..page_name.. (detail ~= '' and ('('..detail..')') or '')
	end
	return ''
end

--{{#invoke:Special wikitext/Template|checkPage}}
function p.checkPage(frame)
	local chk_name = frame.args[1] or frame.args['1'] or ''
	local cur_name = frame.args[2] or frame.args['2'] or ''
	local default_namespace = mw.text.trim(frame.args[3] or frame.args['3'] or '')
	if mw.text.trim(chk_name or '')==''then return '' end
	return p._checkPage(chk_name, cur_name, default_namespace) and '1' or ''
end
function p._checkPage(page, srcpage, default_namespace)
	local chk_page, cur_page = mw.title.new(page, default_namespace), (mw.text.trim(srcpage or '')=='' and mw.title.getCurrentTitle() or mw.title.new(srcpage, default_namespace))
	local chk_name, cur_name = '##:'..chk_page.fullText, '##:'..cur_page.fullText
	local chk_reg = mw.ustring.gsub(chk_name,'([%%\\])','%1%1')
	local chk_regs, cur_names = chk_reg..(mw.ustring.sub(chk_reg,-1,-1)=='/'and''or'/'), cur_name..(mw.ustring.sub(cur_name,-1,-1)=='/'and''or'/')
	local findpos = mw.ustring.find(cur_name, chk_reg)
	if (tonumber(findpos) or -1) > 0 then findpos = mw.ustring.find(cur_names, chk_regs)end
	return (tonumber(findpos) or -1) > 0
end

function p.int(frame)
	local int_name = frame.args[1] or frame.args['1'] or '' 
	local cur_frame = mw.getCurrentFrame()
	local body = mw.text.decode(cur_frame:preprocess("{{msgnw:int:"..int_name.."}}"))
	body = mw.ustring.gsub(body,'#%s*invoke%s*:%s*Special%s+wikitext%|','void|')
	return cur_frame:preprocess(body)
end

function p.getNotices(frame)
	local page_name = frame.args[1] or frame.args['1'] or '' 
	local cur_frame = mw.getCurrentFrame()
	local page_subname = frame.args[2] or frame.args['2'] or '' 
	local title = mw.title.new('Template:Editnotices/Page/' .. page_name)
	local subtitle = mw.title.new('Template:Editnotices/Page/' .. page_name .. page_subname)
	if subtitle.exists then return frame:expandTemplate{ title = subtitle.text }
	elseif title.exists then return frame:expandTemplate{ title = title.text }
	else return '' end
end

return p