Module:Check for unknown parameters
Jump to navigation
Jump to search
Documentation for this module may be created at Module:Check for unknown parameters/doc
1 -- This module may be used to compare the arguments passed to the parent
2 -- with a list of arguments, returning a specified result if an argument is
3 -- not on the list
4 local p = {}
5
6 local function trim(s)
7 return s:match('^%s*(.-)%s*$')
8 end
9
10 local function isnotempty(s)
11 return s and s:match('%S')
12 end
13
14 local function clean(text)
15 -- Return text cleaned for display and truncated if too long.
16 -- Strip markers are replaced with dummy text representing the original wikitext.
17 local pos, truncated
18 local function truncate(text)
19 if truncated then
20 return ''
21 end
22 if mw.ustring.len(text) > 25 then
23 truncated = true
24 text = mw.ustring.sub(text, 1, 25) .. '...'
25 end
26 return mw.text.nowiki(text)
27 end
28 local parts = {}
29 for before, tag, remainder in text:gmatch('([^\127]*)\127[^\127]*%-(%l+)%-[^\127]*\127()') do
30 pos = remainder
31 table.insert(parts, truncate(before) .. '<' .. tag .. '>...</' .. tag .. '>')
32 end
33 table.insert(parts, truncate(text:sub(pos or 1)))
34 return table.concat(parts)
35 end
36
37 function p._check(args, pargs)
38 if type(args) ~= "table" or type(pargs) ~= "table" then
39 -- TODO: error handling
40 return
41 end
42
43 -- create the list of known args, regular expressions, and the return string
44 local knownargs = {}
45 local regexps = {}
46 for k, v in pairs(args) do
47 if type(k) == 'number' then
48 v = trim(v)
49 knownargs[v] = 1
50 elseif k:find('^regexp[1-9][0-9]*$') then
51 table.insert(regexps, '^' .. v .. '$')
52 end
53 end
54
55 -- loop over the parent args, and make sure they are on the list
56 local ignoreblank = isnotempty(args['ignoreblank'])
57 local showblankpos = isnotempty(args['showblankpositional'])
58 local values = {}
59 for k, v in pairs(pargs) do
60 if type(k) == 'string' and knownargs[k] == nil then
61 local knownflag = false
62 for _, regexp in ipairs(regexps) do
63 if mw.ustring.match(k, regexp) then
64 knownflag = true
65 break
66 end
67 end
68 if not knownflag and ( not ignoreblank or isnotempty(v) ) then
69 table.insert(values, clean(k))
70 end
71 elseif type(k) == 'number' and knownargs[tostring(k)] == nil then
72 local knownflag = false
73 for _, regexp in ipairs(regexps) do
74 if mw.ustring.match(tostring(k), regexp) then
75 knownflag = true
76 break
77 end
78 end
79 if not knownflag and ( showblankpos or isnotempty(v) ) then
80 table.insert(values, k .. ' = ' .. clean(v))
81 end
82 end
83 end
84
85 -- add results to the output tables
86 local res = {}
87 if #values > 0 then
88 local unknown_text = args['unknown'] or 'Found _VALUE_, '
89
90 if mw.getCurrentFrame():preprocess( "{{REVISIONID}}" ) == "" then
91 local preview_text = args['preview']
92 if isnotempty(preview_text) then
93 preview_text = require('Module:If preview')._warning({preview_text})
94 elseif preview == nil then
95 preview_text = unknown_text
96 end
97 unknown_text = preview_text
98 end
99 for _, v in pairs(values) do
100 -- Fix odd bug for | = which gets stripped to the empty string and
101 -- breaks category links
102 if v == '' then v = ' ' end
103
104 -- avoid error with v = 'example%2' ("invalid capture index")
105 local r = unknown_text:gsub('_VALUE_', {_VALUE_ = v})
106 table.insert(res, r)
107 end
108 end
109
110 return table.concat(res)
111 end
112
113 function p.check(frame)
114 local args = frame.args
115 local pargs = frame:getParent().args
116 return p._check(args, pargs)
117 end
118
119 return p