Jump to content

Module:URL to diff: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
add some validation stuff
start work on the encoding function
Line 5: Line 5:
local validDiffValues = {
local validDiffValues = {
cur = true,
cur = true,
prev = true,
['next'] = true
['next'] = true
}
}


local function isValidId(s)
local function validateId(s)
local id = tonumber(s)
local id = tonumber(s)
return id and id > 0
if id and id > 0 then
return id
else
return nil
end
end
end


local function isValidDiffParam(s)
local function validateDiffParam(s)
if not s then
return s and (validDiffValues[s] or isValidId(s))
return nil
end
if validDiffValues[s] then
return s
else
return validateId(s)
end
end
end


Line 23: Line 34:
end
end
url = mw.uri.new(url)
url = mw.uri.new(url)
if not url or url.host ~= 'en.wikipedia.org' or not isValidDiffParam(url.query.diff) then
if not url or url.host ~= 'en.wikipedia.org' then
return nil
return nil
end
end
local data = {}
local data = {}
data.diff = validateDiffParam(url.query.diff)
if not data.diff then
return nil
end
data.title = url.query.title and mw.uri.decode(url.query.title, 'WIKI')
data.oldid = validateId(url.query.oldid)
mw.logObject(data)
mw.logObject(data)
return data
end
end


local function encodeDiffTemplate(data)
local function encodeDiffTemplate(data)
local isNamed = false -- Track whether we need to use named parameters
for _, key in ipairs{'title', 'label'} do
if data[key] and string.find(data[key], '=') then
isNamed = true
break
end
end
local ret = {}
end
end


function p._url(args)
function p._url(args)
local url = args[1]
local data = decodeUrl(args.url)
local data = decodeUrl(url)
if data then
if data then
data.label = args.label
return encodeDiffTemplate(data)
return encodeDiffTemplate(data)
else
else

Revision as of 15:55, 1 January 2015

-- This module converts Wikipedia diff URLs to the {{diff}} template format.

local p = {}

local validDiffValues = {
	cur = true,
	
	['next'] = true
}

local function validateId(s)
	local id = tonumber(s)
	if id and id > 0 then
		return id
	else
		return nil
	end
end

local function validateDiffParam(s)
	if not s then
		return nil
	end
	if validDiffValues[s] then
		return s
	else
		return validateId(s)
	end
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' then
		return nil
	end
	local data = {}
	data.diff = validateDiffParam(url.query.diff)
	if not data.diff then
		return nil
	end
	data.title = url.query.title and mw.uri.decode(url.query.title, 'WIKI')
	data.oldid = validateId(url.query.oldid)
	mw.logObject(data)
	return data
end

local function encodeDiffTemplate(data)
	local isNamed = false -- Track whether we need to use named parameters
	for _, key in ipairs{'title', 'label'} do
		if data[key] and string.find(data[key], '=') then
			isNamed = true
			break
		end
	end
	local ret = {}
end

function p._url(args)
	local data = decodeUrl(args.url)
	if data then
		data.label = args.label
		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