Module:URL to diff: Difference between revisions
Appearance
Content deleted Content added
make a start at a URL to {{diff}} converter |
add some validation stuff |
||
Line 2: | Line 2: | ||
local p = {} |
local p = {} |
||
local validDiffValues = { |
|||
cur = true, |
|||
prev = true, |
|||
['next'] = true |
|||
} |
|||
local function isValidId(s) |
|||
local id = tonumber(s) |
|||
return id and id > 0 |
|||
⚫ | |||
local function isValidDiffParam(s) |
|||
return s and (validDiffValues[s] or isValidId(s)) |
|||
end |
|||
local function decodeUrl(url) |
local function decodeUrl(url) |
||
if type(url) ~= 'string' then |
if type(url) ~= 'string' then |
||
return nil |
|||
error('no URL provided', 2) |
|||
end |
end |
||
url = mw.uri.new(url) |
url = mw.uri.new(url) |
||
⚫ | |||
if not url then |
|||
return nil |
|||
error('invalid URL provided', 2) |
|||
end |
end |
||
local data = {} |
|||
⚫ | |||
⚫ | |||
error(string.format( |
|||
"invalid host '%s'; this template only accepts diff URLs from en.wikipedia.org", |
|||
tostring(url.host) |
|||
), 2) |
|||
⚫ | |||
⚫ | |||
end |
end |
||
Line 26: | Line 36: | ||
local url = args[1] |
local url = args[1] |
||
local data = decodeUrl(url) |
local data = decodeUrl(url) |
||
return encodeDiffTemplate(data) |
if data then |
||
return encodeDiffTemplate(data) |
|||
else |
|||
return nil |
|||
end |
|||
end |
end |
||
Revision as of 15:35, 1 January 2015
This module implements {{URL to diff}}. Please see the template page for documentation.
-- This module converts Wikipedia diff URLs to the {{diff}} template format.
local p = {}
local validDiffValues = {
cur = true,
prev = true,
['next'] = true
}
local function isValidId(s)
local id = tonumber(s)
return id and id > 0
end
local function isValidDiffParam(s)
return s and (validDiffValues[s] or isValidId(s))
end
local function decodeUrl(url)
if type(url) ~= 'string' then
return nil
end
url = mw.uri.new(url)
if not url or url.host ~= 'en.wikipedia.org' or not isValidDiffParam(url.query.diff) then
return nil
end
local data = {}
mw.logObject(data)
end
local function encodeDiffTemplate(data)
end
function p._url(args)
local url = args[1]
local data = decodeUrl(url)
if data then
return encodeDiffTemplate(data)
else
return nil
end
end
function p.url(frame)
local args = require('Module:Arguments').getArgs(frame, {
wrappers = 'Template:Make diff',
})
return p._url(args)
end
return p