Module:Sensitive IP addresses/API: Difference between revisions

Content deleted Content added
m fix bad variable name
start writing the query function
Line 2:
 
-- Load modules
local mIP = require('Module:IP')
local IPAddress = mIP.IPAddress
local Subnet = mIP.Subnet
local IPv4Collection = mIP.IPv4Collection
local IPv6Collection = mIP.IPv6Collection
local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType
 
local sensitivityReasons = {
political = true,
technical = true,
 
-------------------------------------------------------------------------------
Line 19:
local SensitiveEntity = {}
SensitiveEntity.__index = SensitiveEntity
 
SensitiveEntity.reasons = {
-- The reasons that an entity may be sensitive. Used to verify data in
-- Module:Sensitive IP addresses/list.
political = true,
technical = true,
 
do
Line 42 ⟶ 49:
return self
end
end
 
function SensitiveEntity:getDataItem(key)
return self.data[key]
end
 
Line 178 ⟶ 181:
-- }
-- }
 
local function query(options)
-- Make entity objects
local entities, entityIds = {}, {}
local data = mw.loadData('Module:Sensitive IP addresses/list')
for i, entityData in ipairs(data) do
entities[entityData.id] = SensitiveEntity.new(entityData)
table.insert(entityIds, entityData.id)
end
 
local function makeError(code, info)
return {
code = code,
info = info,
['*'] = 'See https://en.wikipedia.org/wiki/Module:Sensitive_IP_addresses for API usage',
}
end
 
-- Construct result
local result = {}
 
if type(options) ~= 'table' then
return makeError(
'sipa-options-type-error',
string.format(
"type error in argument #1 of 'query' (expected table, received %s)",
type(options)
)
)
end
 
if options.test then
if type(options.test) ~= 'table' then
return makeError(
'sipa-test-type-error',
string.format("'test' key was type %s (expected table)", type(options.test))
)
end
 
for i, testString in ipairs(options.test) do
if type(testString) ~= 'string' then
return makeError(
'sipa-test-string-type-error',
string.format(
"type error in item #%d in the 'test' array (expected string, received %s)",
i,
type(testString)
)
)
end
end
end
end
 
 
--------------------------------------------------------------------------------
Line 342 ⟶ 399:
 
function p.isValidSensitivityReason(s)
-- Return true if s is a valid sensitivity reason; otherwise return false.
checkType('isValidSensitivityReason', 1, s, 'string')
return sensitivityReasonsSensitiveEntity.reasons[s] ~= nil
end
 
function p.getSensitivityReasons()
-- Return an array of valid sensitivity reasons, ordered alphabetically.
local ret = {}
for reason in pairs(sensitivityReasonsSensitiveEntity.reasons) do
ret[#ret + 1] = reason
end