Module:Requested move: Difference between revisions

Content deleted Content added
fix error check order - this was causing script errors for invocations without currentn params and invocations where the currentn param was an invalid title
add additional error checks – see Module talk:Requested move and the Module:Requested move/sandbox revision history
Line 1:
-- This module implements {{requested move}} and {{move-multi}}.
 
-- Load necessary modules
local getArgs = require('Module:Arguments').getArgs
local tableTools = require('Module:TableTools')
local yesno = require('Module:Yesno')
 
-- Set static values
local defaultNewPagename = '?' -- Name of new pages that haven't been specified
 
local p = {}
Line 14 ⟶ 18:
-- Generates a wikitext error message
return string.format('{{error|%s}}', msg)
end
 
local function validateTitle(page, paramName, paramNum)
-- Validates a page name, and if it is valid, returns true and the title
-- object for that page. If it is not valid, returns false and the
-- appropriate error message.
 
-- Check for a small subset of characters that cannot be used in MediaWiki
-- titles. For the full set of restrictions, see
-- [[Wikipedia:Page name#Technical restrictions and limitations]]. This is
-- also covered by the invalid title check, but with this check we can give
-- a more specific error message.
local invalidChar = page:match('[#<>%[%]|{}]')
if invalidChar then
local msg = 'Invalid character "'
.. invalidChar
.. '" found in the "'
.. paramName
.. paramNum
.. '" parameter'
return false, msg
end
 
-- Get the title object. This also checks for invalid titles that aren't
-- covered by the previous check.
local titleObj = mw.title.new(page)
if not titleObj then
local msg = 'Invalid title detected in parameter "'
.. paramName
.. paramNum
.. '"; check for [[Wikipedia:Page name#'
.. 'Technical restrictions and limitations|invalid characters]]'
return false, msg
end
 
-- Check for interwiki links. Titles with interwikis make valid title
-- objects, but cannot be created on the local wiki.
local interwiki = titleObj.interwiki
if interwiki and interwiki ~= '' then
local msg = 'Invalid title detected in parameter "'
.. paramName
.. paramNum
.. '"; has [[Help:Interwiki linking|interwiki prefix]] "'
.. titleObj.interwiki
.. ':"'
return false, msg
end
 
return true, titleObj
end
 
Line 25 ⟶ 78:
----------------------------------------------------------------------------
local args = getArgs(frame, {parentOnly = true})
local title = mw.title.getCurrentTitle()
 
Line 56 ⟶ 109:
--]]
args.current1 = title.subjectPageTitle.prefixedText
 
args.new1 = args.new1 or args[1]
-- Find the first new page title, if specified, and keep a record of the
-- prefix used to make it; the prefix will be used later to make error
-- messages.
local firstNewParam
if args.new1 then
firstNewParamPrefix = 'new'
elseif args[1] then
args.new1 = args[1]
firstNewParamPrefix = ''
else
firstNewParamPrefix = ''
end
 
-- Build the sorted argument table.
local argsByNum = {}
for k, v in pairs(args) do
Line 70 ⟶ 137:
end
argsByNum = tableTools.compressSparseArray(argsByNum)
 
-- Calculate the number of arguments and whether we are dealing with a
-- multiple nomination.
local argsByNumCount = #argsByNum
local multi
Line 88 ⟶ 158:
local num = t.num
if not new or new == 'New title for page ' .. tostring(num) then
argsByNum[i].new = '?'defaultNewPagename
end
end
Line 94 ⟶ 164:
local new = argsByNum[1].new
if not new or new == 'NewName' then
argsByNum[1].new = '?'defaultNewPagename
end
end
Line 122 ⟶ 192:
-- Check the arguments
local currentDupes, newDupes = {}, {}
for i, t in ipairs(argsByNum) do
local current = t.current
local new = t.new
local num = t.num
local validCurrent
local currentTitle
local subjectSpace
Line 139 ⟶ 211:
end
 
-- CheckGet the currentn title object, and check for invalid currentn titles. This check
-- This check must come before the namespace and existence checks, and theas checkthey forwill
-- produce script errors if the title object doesn't exist.
-- non-existent titles, as we cannot extract the relevant data from
validCurrent, currentTitle = validateTitle(current, 'current', num)
-- the title object if the title object does not exist.
if not validCurrent then
currentTitle = mw.title.new(current)
-- If invalid, the second parameter is the error message.
if not currentTitle then
local msg = currentTitle
local msg = 'Invalid title detected in parameter "current' .. num .. '";'
.. ' check for [[Wikipedia:Page name#'
.. 'Technical restrictions and limitations|invalid characters]] and'
.. ' incorrectly formatted'
.. ' [[Help:Interwiki linking|interwiki prefixes]]'
return err(msg)
end
 
-- Category namespace checkscheck
subjectSpace = mw.site.namespaces[currentTitle.namespace].subject.id
if subjectSpace == 14 then
Line 198 ⟶ 266:
else
currentDupes[currentId] = true
end
 
-- Check for invalid new titles. This check must come before the
-- duplicate title check for new titles, as it will produce a script
-- error if the title object doesn't exist.
local validNew, newTitle = validateTitle(
new,
multi and 'new' or firstNewParamPrefix,
num
)
if not validNew then
-- If invalid, the second parameter is the error message.
local msg = newTitle
return err(msg)
end
 
-- Check for duplicate new titles.
-- We can't use the page_id, as new pages might not exist, and therefore
-- multiple pages may have an id of 0. Use the prefixedText as a
-- reasonable fallback. We also need to check that we aren't using the
-- default new page name, as we don't want it to be treated as a duplicate
-- page if more than one new page name has been omitted.
local newPrefixedText = newTitle.prefixedText
if newPrefixedText ~= defaultNewPagename then
if newDupes[newPrefixedText] then
local msg = 'Duplicate title detected ("'
.. newTitle.prefixedText
.. '"); cannot move two different pages to the same place'
return err(msg)
else
newDupes[newPrefixedText] = true
end
end
end