Jump to content

Module:Lockbox/sandbox: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
don't transclude the current page - there should already be a transclusion from the lockbox from using #invoke there
add a comment about what is going on
Line 21: Line 21:
do
do
local parent_title = frame:getParent():getTitle()
local parent_title = frame:getParent():getTitle()
-- Only pages transcluded from a cascade-protected page appear in
-- CASCADINGSOURCES, so normally we would not be able to tell if the
-- parent title is cascade-protected. To work around this, make the
-- current page transclude itself. If the current title is the parent
-- title, this will add an entry for it to CASCADINGSOURCES. If the
-- current title is the grandparent title or up, it will not be
-- cascade-protected itself as the page will not be parsed. (And that's
-- a good job, as parsing it would cause a template loop.)
mw.title.getCurrentTitle():getContent() -- Generate self-transclusion
generate_transclusion(parent_title)
if frame:preprocess("{{CASCADINGSOURCES:" .. parent_title .. "}}") == "" then
if frame:preprocess("{{CASCADINGSOURCES:" .. parent_title .. "}}") == "" then
output[#output + 1] = '<strong class="warning">Warning: the page "' .. parent_title .. '" is not cascade-protected.</strong>\n'
output[#output + 1] = '<strong class="warning">Warning: the page "' .. parent_title .. '" is not cascade-protected.</strong>\n'

Revision as of 01:16, 17 March 2017

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)
	local getContent = mock_title.getContent
	function generate_transclusion(title)
		rawset(mock_title, 'fullText', title)
		getContent(mock_title)
	end
	-- I can do the same thing without rawset.
end

function export.lock(frame)
	local output = {}
	
	-- check if the transcluding page is cascade-protected
	-- XXX: unfortunately there is no other way; title.protectionLevels does not report cascading protection status
	do
		local parent_title = frame:getParent():getTitle()
		-- Only pages transcluded from a cascade-protected page appear in
		-- CASCADINGSOURCES, so normally we would not be able to tell if the
		-- parent title is cascade-protected. To work around this, make the
		-- current page transclude itself. If the current title is the parent
		-- title, this will add an entry for it to CASCADINGSOURCES. If the
		-- current title is the grandparent title or up, it will not be
		-- cascade-protected itself as the page will not be parsed. (And that's
		-- a good job, as parsing it would cause a template loop.)
		mw.title.getCurrentTitle():getContent() -- Generate self-transclusion
		generate_transclusion(parent_title)
		if frame:preprocess("{{CASCADINGSOURCES:" .. parent_title .. "}}") == "" then
			output[#output + 1] = '<strong class="warning">Warning: the page "' .. parent_title .. '" is not cascade-protected.</strong>\n'
		end
	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)
			output[#output + 1] = '* [[Template:' .. item .. ']]'
		elseif (ns == "File") or (ns == "Image") then
			generate_transclusion(item)
			output[#output + 1] = '* [[:' .. item .. ']]'
		elseif ns == "Category" then
			generate_transclusion(item)
			output[#output + 1] = '* [[:' .. item .. ']]'
		elseif rest ~= '' then
			generate_transclusion(item)
			output[#output + 1] = '* [[' .. item .. ']]'
		end
	end

	if frame.args.silent then
		return ""
	else
		return table.concat(output, '\n')
	end
end

return export