Jump to content

Module:URL to diff: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
hmm, looks like "prev" got lost somewhere
finish the encoding function
Line 1: Line 1:
-- This module converts Wikipedia diff URLs to the {{diff}} template format.
-- This module converts Wikipedia diff URLs to the {{diff}} template format.

local newBuffer = require('Module:OutputBuffer')


local p = {}
local p = {}
Line 39: Line 41:
local data = {}
local data = {}
data.diff = validateDiffParam(url.query.diff)
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.title = url.query.title and mw.uri.decode(url.query.title, 'WIKI')
data.oldid = validateId(url.query.oldid)
data.oldid = validateId(url.query.oldid)
data.diffonly = url.query.diffonly
mw.logObject(data)
return data
return data
end
end


local function encodeDiffTemplate(data)
local function encodeDiffTemplate(data)
if not data.title and not data.diff and not data.oldid then
return nil
end
local isNamed = false -- Track whether we need to use named parameters
local isNamed = false -- Track whether we need to use named parameters
for _, key in ipairs{'title', 'label'} do
for k, v in pairs(data) do
if data[key] and string.find(data[key], '=') then
if string.find(v, '=') then
isNamed = true
isNamed = true
break
break
end
end
end
end
local ret = {}
local getBuffer, print, printf = newBuffer()
print('diff')
printf('%s%s', isNamed and 'page=' or '', data.title or '')
printf('%s%s', isNamed and 'diff=' or '', data.diff or '')
printf('%s%s', isNamed and 'oldid=' or '', data.oldid or '')
if data.label then
printf('%s%s', isNamed and 'label=' or '', data.label)
end
if data.diffonly then
printf('diffonly=%s', data.diffonly)
end
local ret = getBuffer('|')
ret = '{{' .. ret .. '}}'
return ret
end
end



Revision as of 16:18, 1 January 2015

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

local newBuffer = require('Module:OutputBuffer')

local p = {}

local validDiffValues = {
	cur = true,
	prev = 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)
	data.title = url.query.title and mw.uri.decode(url.query.title, 'WIKI')
	data.oldid = validateId(url.query.oldid)
	data.diffonly = url.query.diffonly
	return data
end

local function encodeDiffTemplate(data)
	if not data.title and not data.diff and not data.oldid then
		return nil
	end
	local isNamed = false -- Track whether we need to use named parameters
	for k, v in pairs(data) do
		if string.find(v, '=') then
			isNamed = true
			break
		end
	end
	local getBuffer, print, printf = newBuffer()
	print('diff')
	printf('%s%s', isNamed and 'page=' or '', data.title or '')
	printf('%s%s', isNamed and 'diff=' or '', data.diff or '')
	printf('%s%s', isNamed and 'oldid=' or '', data.oldid or '')
	if data.label then
		printf('%s%s', isNamed and 'label=' or '', data.label)
	end
	if data.diffonly then
		printf('diffonly=%s', data.diffonly)
	end
	local ret = getBuffer('|')
	ret = '{{' .. ret .. '}}'
	return 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