Module:Lockbox: Difference between revisions

Content deleted Content added
trim
... I told you so! ...
 
(18 intermediate revisions by 4 users not shown)
Line 1:
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)
local full_text = type(title) == 'table' and title.fullText or title
rawset(mock_title, 'fullText', full_text)
getContent(mock_title)
end
end
 
local function make_wikitext_warning(msg)
return string.format('<strong class="error">Warning: %s.</strong>', msg)
end
 
function export.lock(frame)
local outputwarnings, transclusion_list = {}, {}
-- checkCheck if the currenttranscluding page is cascade-protected.
--
-- XXX: unfortunately there is no other way; title.protectionLevels does not report cascading protection status
-- Only pages transcluded from a cascade-protected page appear in
mw.title.getCurrentTitle():getContent() -- self-transclude; see [[mw:Extension:Scribunto/Lua reference manual#Title objects]]
if-- frame:preprocess "{{CASCADINGSOURCES}}", ==so normally we would not be able to tell if ""the thenlockbox
-- itself is cascade-protected. To work around this, we generate a
table.insert(output, '<strong class="warning">Warning: this page (' .. mw.title.getCurrentTitle().fullText .. ') is not cascade-protected.</strong>')
-- transclusion from the lockbox to itself, so that it will have an entry
end
-- for itself in CASCADINGSOURCES.
--
-- We cannot generate this transclusion using the title object for the
-- parent title (the lockbox), as if the lockbox is transcluded on another
-- page, we will generate a transclusion *from* the lockbox *to* that page
-- as well, and the page will be cascade-protected. Instead we generate it
-- with the title object for the current title.
--
-- When the current title is the parent title (i.e. we are rendering the
-- lockbox page), this creates the required entry in the link table in the
-- database. When the current title is the grandparent title or up (i.e. we
-- are rendering a page transcluding the lockbox), transclusions are only
-- created from the page itself, not from the lockbox, and it is not
-- cascade-protected.
--
-- This creates an extaneous self-transclusion for all pages using the
-- module, but we treat that as a necessary evil.
--[[ This entire block no longer works following https://gerrit.wikimedia.org/r/c/mediawiki/extensions/Scribunto/+/1074581
-- transclude everything, and discard the output
do
for _, item in ipairs(frame.args) do
mw.title.getCurrentTitle():getContent() -- Generate self-transclusion
item = mw.text.trim(item)
local parent_title = frame:getParent():getTitle()
if not mw.title.new(parent_title).cascadingProtection.sources[1] then
-- getContent() on a title object might be cleaner, but getContent() and title objects
warnings[#warnings + 1] = make_wikitext_warning(string.format(
-- are considered "expensive". even though they are probably cheaper than actual transclusion
'the page "%s" is not cascade-protected',
frame:expandTemplate { title = item }
parent_title
))
end
end
--]]
 
-- Generate transclusions to the templates, and build the output list.
if not item:match(":") then
for i, item in ipairs(frame.args) do
item = "Template:" .. item
item = mw.text.trim(item)
elseif item:match("^File:") or item:match("^Image:") then
local title = mw.title.new(item)
-- XXX: leaving this separate just in case it needs special handling
if title then
item = ":" .. item
local ns = title.namespace
elseif item:match("^Category:") then
local prefixed_text = title.prefixedText
item = ":" .. item
if ns == 0 then
-- The item had no namespace text. If the item starts with a
-- colon, assume it is a mainspace page. Otherwise, assume it is
-- a template.
if item:sub(1, 1) == ':' then
generate_transclusion(title)
table.insert(transclusion_list, '* [[' .. prefixed_text .. ']]')
else
generate_transclusion('Template:' .. prefixed_text)
table.insert(transclusion_list, '* [[Template:' .. prefixed_text .. ']]')
end
elseif ns == 6 or ns == 14 then -- File or Category namespace
generate_transclusion(title)
table.insert(transclusion_list, '* [[:' .. prefixed_text .. ']]')
else
generate_transclusion(title)
table.insert(transclusion_list, '* [[' .. prefixed_text .. ']]')
end
else
warnings[#warnings + 1] = make_wikitext_warning(string.format(
'invalid title "%s" in argument #%d',
item,
i
))
end
table.insert(output, ('* [[%s]]'):format(item))
end
 
if frame.args.silent then
return ""''
else
-- If there were any warnings, show them at the top. Then show the list
return table.concat(output, "\n")
-- of transcluded pages.
local ret = ''
if #warnings > 0 then
if #warnings > 1 then
for i, warning in ipairs(warnings) do
warnings[i] = '* ' .. warning
end
end
ret = ret .. table.concat(warnings, '\n') .. '\n\n'
end
return ret .. table.concat(transclusion_list, '\n')
end
end