Jump to content

Module:Sensitive IP addresses/blocktext: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
implement renderMessageTemplate
render messages as a MediaWiki message instead of as a template - this seems more natural seeing as this module is specifically intended for use in the MediaWiki namespace
Line 66: Line 66:
end
end


local function renderMessageTemplate(templateTitle, templateArgs)
local function renderMessage(rawMessage, messageArgs)
if not templateTitle then
if rawMessage then
message = mw.message.newRawMessage(rawMessage)
templateTitle = 'Sensitive IP addresses/blocktext'
else
message = mw.message.new('sensitive-ip-block-text')
end
end
wikitext = message:params(
messageArgs.type,
messageArgs.type == 'ip' and messageArgs.ip or messageArgs.range,
messageArgs['matches-range'],
messageArgs['entity-id'],
messageArgs['entity-name'],
messageArgs['entity-description'],
messageArgs.reason
):plain()
local frame = mw.getCurrentFrame()
local frame = mw.getCurrentFrame()
return frame:preprocess(wikitext)
return frame:expandTemplate{title = templateTitle, args = templateArgs}
end
end


Line 77: Line 88:


function p._main(args, title)
function p._main(args, title)
args = args or {}
if not title then
title = mw.title.getCurrentTitle()
title = title or mw.title.getCurrentTitle()
end
local ipOrSubnet = parseTitle(title)
local ipOrSubnet = parseTitle(title)
if not ipOrSubnet then
if not ipOrSubnet then
Line 88: Line 98:
return nil
return nil
end
end
templateArgs = parseSensitivityData(sensitivityData)
messageArgs = parseSensitivityData(sensitivityData)
return renderMessageTemplate(args.template, templateArgs)
return renderMessage(args.message, messageArgs)
end
end


Line 102: Line 112:
fetchSensitivityData = fetchSensitivityData,
fetchSensitivityData = fetchSensitivityData,
parseSensitivityData = parseSensitivityData,
parseSensitivityData = parseSensitivityData,
renderMessage = renderMessage,
renderMessageTemplate = renderMessageTemplate,
}
}
end
end

Revision as of 06:56, 4 November 2018

-- This module creates a warning about sensitive IP addresses that is intended
-- to be placed in [[Special:Block]] via the [[Mediawiki:Blockiptext]] message.

local querySensitiveIPs = require('Module:Sensitive IP addresses').query
local mIP = require('Module:IP')
local IPAddress = mIP.IPAddress
local Subnet = mIP.Subnet

local function normalizeIPOrSubnet(ipOrSubnet)
	-- Normalize an IP address or subnet.
	-- If ipOrSubnet is not a valid IP address or subnet, returns nil.
	local ipSuccess, ipObj = pcall(IPAddress.new, ipOrSubnet)
	if ipSuccess and ipObj then
		return tostring(ipObj)
	end
	local subnetSuccess, subnetObj = pcall(Subnet.new, ipOrSubnet)
	if subnetSuccess and subnetObj then
		return tostring(subnetObj)
	end
	return nil
end

local function parseTitle(title)
	-- Parse a title. If the subpage(s) of the root page are a valid IP
	-- address or subnet, then return the normalized IP address or subnet.
	local ipOrSubnet = title.text:gsub('^.-/', '')
	return normalizeIPOrSubnet(ipOrSubnet)
end

local function fetchSensitivityData(ipOrSubnet)
	return querySensitiveIPs{test = {ipOrSubnet}}
end

local function parseSensitivityData(sensitivityData)
	-- Parse the data from the sensitive IP API call, and return a table with
	-- information about the first match that we can use as arguments to a
	-- template call.
	local function parse(sensitivityData)
		sensitivityData = sensitivityData.sensitiveips
		if #sensitivityData.matches == 0 then
			return nil
		end
		local ret = {}
		ret.type = sensitivityData.matches[1].type
		if ret.type == 'ip' then
			ret.ip = sensitivityData.matches[1].ip
		elseif ret.type == 'range' then
			ret.range = sensitivityData.matches[1].range
		else
			return nil
		end
		ret['entity-id'] = sensitivityData.matches[1]['entity-id']
		ret['matches-range'] = sensitivityData.matches[1]['matches-range']
		ret['entity-name'] = sensitivityData.entities[ret['entity-id']].name
		ret['entity-description'] = sensitivityData.entities[ret['entity-id']].description
		ret.reason = sensitivityData.entities[ret['entity-id']].reason
		return ret
	end

	local success, ret = pcall(parse, sensitivityData)
	if success and ret then
		return ret
	else
		return nil
	end
end

local function renderMessage(rawMessage, messageArgs)
	if rawMessage then
		message = mw.message.newRawMessage(rawMessage)
	else
		message = mw.message.new('sensitive-ip-block-text')
	end
	wikitext = message:params(
		messageArgs.type,
		messageArgs.type == 'ip' and messageArgs.ip or messageArgs.range,
		messageArgs['matches-range'],
		messageArgs['entity-id'],
		messageArgs['entity-name'],
		messageArgs['entity-description'],
		messageArgs.reason
	):plain()
	local frame = mw.getCurrentFrame()
	return frame:preprocess(wikitext)
end

local p = {}

function p._main(args, title)
	args = args or {}
	title = title or mw.title.getCurrentTitle()
	local ipOrSubnet = parseTitle(title)
	if not ipOrSubnet then
		return nil
	end
	local sensitivityData = fetchSensitivityData(ipOrSubnet)
	if not sensitivityData then
		return nil
	end
	messageArgs = parseSensitivityData(sensitivityData)
	return renderMessage(args.message, messageArgs)
end

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame)
	return p._main(args)
end

function p._exportFunctions()
	return {
		parseTitle = parseTitle,
		fetchSensitivityData = fetchSensitivityData,
		parseSensitivityData = parseSensitivityData,
		renderMessage = renderMessage,
	}
end

return p