Jump to content

Module:Lockbox: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
ns, rest
playing dirty
Line 1: Line 1:
local export = {}
local export = {}

-- XXX: OUTRAGEOUS ABUSE OF SCRIBUNTO API
-- generates a transclusion without incrementing the "expensive function" count
local generate_transclusion
do
local mock_title = mw.title.new(mw.title.getCurrentTitle().id)
rawset(mock_title, 'getContent', mock_title.getContent)
function generate_transclusion(title)
rawset(mock_title, 'fullText', title)
mock_title:getContent()
end
end


function export.lock(frame)
function export.lock(frame)
Line 12: Line 24:
local match = string.match
local match = string.match
local args = { }


for _, item in ipairs(frame.args) do
for _, item in ipairs(frame.args) do
Line 19: Line 30:
local ns, rest = match(item, "^(.-):(.*)")
local ns, rest = match(item, "^(.-):(.*)")
if not ns or not mw.site.namespaces[ns] then
if not ns or not mw.site.namespaces[ns] then
mw.title.makeTitle('Template', item):getContent()
generate_transclusion('Template:' .. item)
table.insert(output, '* [[Template:' .. item .. ']]\n')
table.insert(output, '* [[Template:' .. item .. ']]\n')
elseif (ns == "File") or (ns == "Image") then
elseif (ns == "File") or (ns == "Image") then
generate_transclusion(item)
mw.title.makeTitle('File', rest):getContent()
table.insert(output, '* [[:' .. item .. ']]\n')
table.insert(output, '* [[:' .. item .. ']]\n')
elseif ns == "Category" then
elseif ns == "Category" then
generate_transclusion(item)
mw.title.makeTitle('Category', rest):getContent()
table.insert(output, '* [[:' .. item .. ']]\n')
table.insert(output, '* [[:' .. item .. ']]\n')
elseif rest ~= '' then
elseif rest ~= '' then
generate_transclusion(item)
mw.title.makeTitle(ns, rest):getContent()
table.insert(output, '* [[' .. item .. ']]\n')
table.insert(output, '* [[' .. item .. ']]\n')
end
end

Revision as of 06:27, 5 August 2014

local export = {}

-- XXX: OUTRAGEOUS ABUSE OF SCRIBUNTO API
-- generates a transclusion without incrementing the "expensive function" count
local generate_transclusion
do 
	local mock_title = mw.title.new(mw.title.getCurrentTitle().id)
	rawset(mock_title, 'getContent', mock_title.getContent)
	function generate_transclusion(title)
		rawset(mock_title, 'fullText', title)
		mock_title:getContent()
	end
end

function export.lock(frame)
	local output = {}
	
	-- check if the current page is cascade-protected
	-- XXX: unfortunately there is no other way; title.protectionLevels does not report cascading protection status
	mw.title.getCurrentTitle():getContent() -- self-transclude; see [[mw:Extension:Scribunto/Lua reference manual#Title objects]]
	if frame:preprocess "{{CASCADINGSOURCES}}" == "" then
		table.insert(output, '<strong class="warning">Warning: this page (' .. mw.title.getCurrentTitle().fullText .. ') is not cascade-protected.</strong>\n')
	end
	
	local match = string.match

	for _, item in ipairs(frame.args) do
		item = mw.text.trim(item)
		
		local ns, rest = match(item, "^(.-):(.*)")
		if not ns or not mw.site.namespaces[ns] then
			generate_transclusion('Template:' .. item)
			table.insert(output, '* [[Template:' .. item .. ']]\n')
		elseif (ns == "File") or (ns == "Image") then
			generate_transclusion(item)
			table.insert(output, '* [[:' .. item .. ']]\n')
		elseif ns == "Category" then
			generate_transclusion(item)
			table.insert(output, '* [[:' .. item .. ']]\n')
		elseif rest ~= '' then
			generate_transclusion(item)
			table.insert(output, '* [[' .. item .. ']]\n')
		end
	end

	if frame.args.silent then
		return ""
	else
		return table.concat(output)
	end
end

return export