Module:UtilsVar: Difference between revisions
Jump to navigation
Jump to search
PhantomCaleb (talk | contribs) m (Protected "Module:UtilsVar": Critical wiki page ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite))) |
PhantomCaleb (talk | contribs) No edit summary |
||
Line 24: | Line 24: | ||
end | end | ||
function p.counter(name) | function p.counter(name, limit) | ||
name = name or "" | name = name or "" | ||
return { | return { | ||
Line 35: | Line 35: | ||
value = function() | value = function() | ||
return p.get(name) or 0 | return p.get(name) or 0 | ||
end | end, | ||
limit = limit | |||
} | } | ||
end | |||
function p.expensiveParserFunctionCounter() | |||
return p.counter("expensiveParserFunctions", 99) | |||
end | end | ||
return p | return p |
Revision as of 10:32, 27 October 2020
This module allows for data to be shared between invocations of a module on a given page, using Extension:VariablesLua. Use cases for this include counters and previewing Cargo storage.
Not to be confused with Module:UtilsArg.
local p = {}
VariablesLua = mw.ext.VariablesLua
function p.set(name, val)
local json = mw.text.jsonEncode(val)
VariablesLua.vardefine(name, json)
end
function p.get(name)
local json = VariablesLua.var(name)
if json == "" then
return nil
end
local val = mw.text.jsonDecode(json)
return val
end
function p.add(name, val)
local array = p.get(name) or {}
table.insert(array, val)
p.set(name, array)
return array
end
function p.counter(name, limit)
name = name or ""
return {
increment = function()
local val = p.get(name) or 0
val = val + 1
p.set(name, val)
return val
end,
value = function()
return p.get(name) or 0
end,
limit = limit
}
end
function p.expensiveParserFunctionCounter()
return p.counter("expensiveParserFunctions", 99)
end
return p