Content deleted Content added
make the getSensitivityReasons function work with both wikitext and Lua |
m Protected "Module:Sensitive IP addresses/API": High-risk Lua module: used in the MediaWiki interface, e.g. MediaWiki:Blockiptext via Template:Sensitive IP addresses ([Edit=Require administrator access] (indefinite... |
||
(4 intermediate revisions by the same user not shown) | |||
Line 7:
local IPv4Collection = mIP.IPv4Collection
local IPv6Collection = mIP.IPv6Collection
-- Lazily load the jf-JSON module
local JSON
-------------------------------------------------------------------------------
Line 24 ⟶ 27:
else
return val
end
local function deepCopyInto(source, dest)
-- Do a deep copy of a source table into a destination table, ignoring
-- self-references and metatables. If a table in source has a self-reference
-- you will get an infinite loop.
for k, v in pairs(source) do
if type(v) == 'table' then
dest[k] = {}
deepCopyInto(v, dest[k])
else
dest[k] = v
end
end
end
Line 153 ⟶ 170:
-- {
-- entities = {'all'}
-- }
--
-- Query all entities and format the result as a JSON string:
-- {
-- entities = {'all'},
-- format = 'json'
-- }
--
Line 234 ⟶ 257:
end
local function makeError(code, info, format)
code = code,
info = info,
['*'] = 'See https://en.wikipedia.org/wiki/Module:Sensitive_IP_addresses/API for API usage',
}}
if format == 'json' then
else
return ret
end
end
-- Construct result
local result = {
matches = {},
['matched-ranges'] = {},
entities = {},
}
if type(options) ~= 'table' then
Line 256 ⟶ 289:
return makeError(
'sipa-blank-options',
"the options table didn't contain a 'test' or an 'entities' key",
options.format
)
end
Line 267 ⟶ 301:
"'test' options key was type %s (expected table)",
type(options.test)
),
options.format
)
end
Line 279 ⟶ 314:
i,
type(testString)
),
options.format
)
end
Line 298 ⟶ 334:
i,
testString
),
options.format
)
end
if isMatch then
-- The string was a sensitive IP address or subnet.
-- Add match data
Line 353 ⟶ 384:
"'entities' options key was type %s (expected table)",
type(options.test)
),
options.format
)
end
Line 368 ⟶ 400:
i,
type(entityString)
),
options.format
)
end
Line 391 ⟶ 424:
-- Insert the entity and entity-id subtables if they aren't already
-- present.
▲ result['entity-ids'] = result['entity-ids'] or {}
for i, entityString in ipairs(options.entities) do
if entities[entityString] then
Line 409 ⟶ 440:
-- Add any missing reason fields from entities.
▲ end
end
Line 419 ⟶ 448:
if options.format == 'json' then
-- Load jf-JSON
▲ return mw.text.jsonEncode(result)
JSON = JSON or require('Module:jf-JSON')
JSON.strictTypes = true -- Necessary for correct blank-object encoding
-- Decode a skeleton result JSON string. This ensures that blank objects
-- are re-encoded as blank objects and not as blank arrays.
local jsonResult = JSON:decode([[{"sensitiveips": {
"matches": [],
"matched-ranges": {},
"entities": {},
"entity-ids": []
}}]])
for i, key in ipairs{'matches', 'matched-ranges', 'entities', 'entity-ids'} do
deepCopyInto(result.sensitiveips[key], jsonResult.sensitiveips[key])
end
return JSON:encode(jsonResult)
elseif options.format == nil or options.format == 'lua' then
return result
|