Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Special pages
Random page
Wiki Discord
Contents
Neighbors
Galaxy
> Hexia
> Norvan
> Pashah
> Taffeh
Creatures
> Insects
> Fishes
> Shore-Dwellers
Items
> Dishes
> Furniture
Game Systems
> Dress Up
> Luca Arbor
> Loomix Footprints
> Loomix Vehicle Services
> Loomix Homes
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Editing
Module
:
BucketPut
Module
Discussion
English
Read
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit source
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Once you make an edit, a temporary account will be created for you.
Learn more
.
Log in
or
create an account
to continue receiving notifications after this account expires, and to access other features.
Anti-spam check. Do
not
fill this in!
-- Imported from: https://bucket.miraheze.org/wiki/Module:BucketPut local p = {} local getArgs = require('Module:Arguments').getArgs local yesno = require('Module:Yesno') local lang = mw.language.getContentLanguage() local O_TYPE = "type" local O_DELIM = "delimiter" local O_REQUIRED = "required" local OPTIONS = { O_TYPE, O_DELIM, O_REQUIRED } local O_ALIASES = { delim = 'delimiter', notnull = 'required', } local C_TARGS = "templateargs" local C_SUB = "sub" local C_NOSAVE = "nosave" local C_LOG = "log" local CONF_VALS = { C_TARGS, C_SUB, C_NOSAVE, C_LOG } ---Is `value` one of the `targets`? ---@param value string Type you want to check is in list. ---@param targets string[] List of targets that `value` could be. ---@return boolean local function oneof(value, targets) for _, target in ipairs(targets) do if value == target then return true end end return false end ---Parse a single value based on type name ---@param t string Type name ---@param v string Value to parse ---@return boolean success Did parsing succeed? ---@return any value Parsed value if success, string if fail. function p.parse_typed_value(t, v) -- probably should return an object instead if oneof(t, { "text" }) then -- possible TODO with page return true, v end if oneof(t, { "int", "integer" }) then local val = tonumber(v) if val ~= nil and val % 1 == 0 then return true, val else return false, "integer" end end if oneof(t, { "float", "double", "num", "number" }) then local val = tonumber(v) if val ~= nil then return true, val else return false, "number" end end if oneof(t, { "bool", "boolean", "yesno" }) then local val = yesno(v) if val ~= nil then return true, val else return false, "boolean" end end if oneof(t, { "date", "time", "datetime", "timestamp" }) then local s, r = pcall(lang.formatDate, lang, "c", v) if s then return s, r else return false, "timestamp" end end if oneof(t, { "unixtime" }) then local s, r = pcall(lang.formatDate, lang, "U", v) if s then r = tonumber(r) or error("Could not convert " .. r .. " to a number") return s, r else return false, "Unix timestamp" end end error("Unsupported type: " .. mw.dumpObject(t)) end ---Recursively and safely add items of `keylist` to `args`. --- ---`add_to_args({}, { "a", "b", "c" }, "d")` ---will yield ---`{ a = { b = { c = "d" } } }` ---@param args table ---@param keylist (string | number)[] ---@param value any local function add_to_args(args, keylist, value) local nextvalue = args for i, valuename in ipairs(keylist) do if i == #keylist then nextvalue[valuename] = value else nextvalue[valuename] = nextvalue[valuename] or {} nextvalue = nextvalue[valuename] end end end ---Parse specific key-value combination and modify output table. ---@param out_args table ---@param out_config table ---@param key string ---@param value string ---@return nil function p.parse_args(out_args, out_config, key, value) if type(key) ~= "string" then return end value = mw.text.trim(value) if value == '' then return end -- basic conditions for parser working local optkey, optname = key:match("^([%w_]+)%-(.+)$") -- [%w_] matches all valid bucket field names at time of writing -- (non-exhaustively, i.e. all valid names are matched by [%w_], but [%w_] -- also matches some invalid names) if optkey and optname then if O_ALIASES[optname] then optname = O_ALIASES[optname] end if not oneof(optname, OPTIONS) then local fmt = "Invalid option %s for parameter %s" error(fmt:format(optname, optkey)) end return add_to_args(out_args, { optkey, optname }, value) end local ckey = key:match("^%+(.+)$") if ckey then if not oneof(ckey, CONF_VALS) then error(ckey .. " not a recognised config option") end out_config[ckey] = value -- modify out_config rather than out_args return end return add_to_args(out_args, { key, 1 }, value) -- none of the above apply so treat as raw arg end ---Turn parsed argument object to single value to send to `mw.bucket.put`. ---@param argname string ---@param data table ---@return any function p.get_bucket_arg(argname, data) local item = data[1] if not item then if not yesno(data[O_REQUIRED]) then return end error(("Required argument %s was not given"):format(argname)) end item = mw.text.trim(item) local type = data[O_TYPE] or "text" if not data[O_DELIM] then local success, value_or_typename = p.parse_typed_value(type, item) if success then return value_or_typename -- value else local fmt = "Invalid %s given for parameter %s: %s" error(fmt:format(value_or_typename, mw.dumpObject(argname), mw.dumpObject(item))) -- typename end end local delim = data[O_DELIM] delim = delim:gsub('%f[\\]\\n', '\n') delim = delim:gsub('\\\\n', '\\n') --[[ removes a backslash from start, so: \n -> newline \\n -> \n \\\n -> \\n etc. ]] local items = {} for entry in mw.text.gsplit(item, delim) do entry = mw.text.trim(entry) if entry ~= "" then local success, value_or_typename = p.parse_typed_value(type, entry) if success then table.insert(items, value_or_typename) -- value else local fmt = "Invalid %s given for parameter %s: %s" error(fmt:format(value_or_typename, mw.dumpObject(argname), mw.dumpObject(entry))) -- typename end end end return items end ---Turn list of parsed arguments into `mw.bucket.put` arg table. ---@param out_args table ---@return table function p.get_bucket_args(out_args) local bucket_args = {} for arg, data in pairs(out_args) do bucket_args[arg] = p.get_bucket_arg(arg, data) end return bucket_args end ---Main invokable function. ---@param frame any function p.put(frame) local args = getArgs(frame, { frameOnly = true }) local parent_args = getArgs(frame, { parentOnly = true }) local bucket_name = mw.text.trim(args[1] or '') -- Replicate the behavior of $wgCargoStoreUseTemplateArgsFallback for specified arguments local implicit_store_args = mw.text.trim(args['+' .. C_TARGS] or '') for arg in implicit_store_args:gmatch("[^,]+") do arg = mw.text.trim(arg) args[arg] = parent_args[arg] end -- Convert all arguments to consistent format local out_args = {} local out_config = {} for k, v in pairs(args) do p.parse_args(out_args, out_config, k, v) end local bucket_args = p.get_bucket_args(out_args) if yesno(out_config[C_LOG]) then mw.log("Passed args: " .. mw.dumpObject(args)) mw.log("out_args: " .. mw.dumpObject(out_args)) mw.log("out_config: " .. mw.dumpObject(out_config)) mw.log("bucket_args: " .. mw.dumpObject(bucket_args)) end -- put changes to bucket if not yesno(out_config[C_NOSAVE]) then local bucket = mw.bucket(bucket_name) if out_config[C_SUB] then bucket.sub(out_config[C_SUB]) end bucket.put(bucket_args) end end return p
Summary:
Please note that all contributions to The Petit Planet Wiki are considered to be released under the Creative Commons Attribution-ShareAlike (see
Petit Planet Wiki:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Preview page with this template
Below are commonly used wikitext from
MediaWiki:Edittools
. Click on any to insert them in the edit box.
File Pages
==Summary==
==Licensing==
{{Fairuse}}
{{File|}}
Dynamic Page List
¦
²
«
»
²{¦}²
«»
«/»
«!----»
Array in DPL
¹
^2{
}^2
^2{¹}^2
Parser Functions
{{#if:||}}
{{#if:<condition>|<result if true>|<result if false>}}
{{#ifeq:|||}}
{{#ifeq:<text1>|<text2>|<result if text1=text2>|<result if text1≠text2>}}
{{#expr:}}
{{#expr:<mathematical expression>}}
{{#switch:||#default=}}
{{#switch:<text1>|<text2>=<result if text1=text2>|<text3>=<result if text1=text3>|#default=<result if no valid match>}}
{{#replace:||}}
{{#replace:<text1>|<plain text to find in text1>|<plain text to insert in place of the text found>}}
{{#titleparts:}}
{{#replace:<page name>}}
{{#tag:|}}
{{#tag:<name of html tag>|<content inside html tag>}}
Markup
{{}}
|
[]
[[]]
[[Category:]]
#REDIRECT [[]] [[Category:Redirect Pages]]
<code></code>
<includeonly></includeonly>
<noinclude></noinclude>
<nowiki></nowiki>
<!---->
<br>
§
Notices
{{Stub|}}
{{Stub Dialogue}}
{{Upcoming}}
{{Under Construction}}
{{Placeholder|}}
Magic Words
{{!}}
{{formatnum:}}
{{lc:}}
{{uc:}}
{{PAGENAME}}
{{FULLPAGENAME}}
{{ROOTPAGENAME}}
{{BASEPAGENAME}}
{{SUBPAGENAME}}
{{DISPLAYTITLE:|noreplace}}
__TOC__
__NOTOC__
__EXPECTUNUSEDCATEGORY__
__HIDDENCAT__
HTML Entities
—
,
­
Page included on this page:
Module:BucketPut/doc
(
view source
)
Search
Search
Editing
Module
:
BucketPut
Add topic