Modul:Arguments

Modul ini menyediakan pemprosesan argumen yang mudah dihantar dari #invoke. Ini adalah meta-modul, yang dimaksudkan untuk digunakan oleh modul lain, dan tidak boleh dipanggil langsung dari #invoke. Ciri-cirinya merangkumi:

  • Pemangkasan argumen dan penghapusan argumen kosong dengan mudah.
  • Argumen dapat dilalui oleh bingkai semasa dan bingkai induk pada masa yang sama. (Maklumat lebih lanjut di bawah.)
  • Argumen boleh disampaikan terus dari modul Lua lain atau dari konsol debug.
  • Argumen diambil mengikut keperluan, yang dapat membantu mengelakkan (beberapa) masalah dengan tag <ref>...</ref>.
  • Sebilangan besar ciri boleh disesuaikan.

Penggunaan asas

Pertama, anda perlu memuatkan modul. Ia mengandungi satu fungsi, yang dinamakan getArgs.

local getArgs = require('Modul:Arguments').getArgs

Dalam senario paling asas, anda boleh menggunakan getArgs di dalam fungsi utama anda. Pemboleh ubah args adalah meja/jadual yang mengandungi hujah dari #invoke. (Lihat di bawah untuk maklumat lanjut.)

local getArgs = require('Modul:Arguments').getArgslocal p = {}function p.main(frame)local args = getArgs(frame)-- Main module code goes here.endreturn p

Walau bagaimanapun, amalan yang disarankan adalah menggunakan fungsi hanya untuk memproses argumen dari #invoke. Ini bermaksud bahawa jika seseorang memanggil modul anda dari modul Lua yang lain, anda tidak perlu menyediakan objek bingkai, yang meningkatkan prestasi.

local getArgs = require('Modul:Arguments').getArgslocal p = {}function p.main(frame)local args = getArgs(frame)return p._main(args)endfunction p._main(args)-- Main module code goes here.endreturn p

Sekiranya anda mahu beberapa fungsi menggunakan argumen, dan anda juga mahu fungsi tersebut dapat diakses dari #invoke, anda boleh menggunakan fungsi pembalut.

local getArgs = require('Modul:Arguments').getArgslocal function makeInvokeFunc(funcName)return function (frame)local args = getArgs(frame)return p[funcName](args)endendlocal p = {}p.func1 = makeInvokeFunc('_func1')function p._func1(args)-- Code for the first function goes here.endp.func2 = makeInvokeFunc('_func2')function p._func2(args)-- Code for the second function goes here.endreturn p

Pilihan

Pilihan berikut ada. Ianya dijelaskan dalam bahagian di bawah.

local args = getArgs(frame, {trim = false,removeBlanks = false,valueFunc = function (key, value)-- Code for processing one argumentend,frameOnly = true,parentOnly = true,parentFirst = true,wrappers = {'Templat:A wrapper template','Templat:Another wrapper template'},readOnly = true,noOverwrite = true})

Memotong dan mengeluarkan kosong

Argumen kosong sering kali membuat pengekod baru untuk menukar templat MediaWiki ke Lua. Dalam sintaks templat, rentetan dan rentetan kosong yang hanya terdiri daripada ruang kosong dianggap salah. Namun, di Lua, tali dan tali kosong yang terdiri daripada ruang kosong dianggap benar. Ini bermaksud bahawa jika anda tidak memperhatikan hujah-hujah tersebut semasa anda menulis modul Lua anda, anda mungkin memperlakukan sesuatu yang benar yang sebenarnya harus dianggap sebagai salah. Untuk mengelakkannya, secara lalai modul ini membuang semua argumen kosong.

Begitu juga, ruang kosong boleh menyebabkan masalah ketika berhadapan dengan posisi argumen. Walaupun ruang kosong dipangkas untuk argumen bernama yang berasal #invoke, ia disimpan untuk posisi argumen. Selalunya ruang kosong tambahan ini tidak diingini, jadi modul ini memotongnya secara lalai.

Walau bagaimanapun, kadang-kadang anda mahu menggunakan argumen kosong sebagai input, dan kadang-kadang anda mahu mengekalkan ruang kosong tambahan. Ini boleh menjadi perlu untuk menukar beberapa templat anda sama seperti yang ditulis. Sekiranya anda mahu melakukan ini, anda boleh menetapkan trim dan removeBlanks argumen kepada false.

local args = getArgs(frame, {trim = false,removeBlanks = false})

Pemformatan argumen khusus

Kadang-kadang anda ingin membuang beberapa argumen kosong tetapi tidak yang lain, atau mungkin anda mungkin mahu meletakkan semua posisi argumen dengan huruf kecil. Untuk melakukan perkara seperti ini, anda boleh menggunakan pilihan valueFunc. Input untuk pilihan ini mestilah fungsi yang mengambil dua parameter, key dan value, dan mengembalikan satu nilai. Nilai ini adalah apa yang akan anda perolehi semasa anda memasuki lapangan key dalam args meja/jadual.

Contoh 1: fungsi ini mengekalkan ruang kosong untuk argumen kedudukan pertama, tetapi memotong semua argumen lain dan membuang semua argumen kosong yang lain.

local args = getArgs(frame, {valueFunc = function (key, value)if key == 1 thenreturn valueelseif value thenvalue = mw.text.trim(value)if value ~= '' thenreturn valueendendreturn nilend})

Contoh 2: fungsi ini membuang argumen kosong dan menukar semua argumen menjadi huruf kecil, tetapi tidak memotong ruang kosong dari posisi parameter.

local args = getArgs(frame, {valueFunc = function (key, value)if not value thenreturn nilendvalue = mw.ustring.lower(value)if mw.ustring.find(value, '%S') thenreturn valueendreturn nilend})

Nota: fungsi di atas akan gagal sekiranya input yang dikeluarkan bukan jenis string atau nil. Ini mungkin berlaku jika anda menggunakan fungsi getArgs dalam fungsi utama modul anda, dan fungsi itu dipanggil oleh modul Lua yang lain. Dalam kes ini, anda perlu memeriksa jenis input anda. Ini tidak menjadi masalah jika anda menggunakan fungsi khas untuk argumen dari #invoke (iaitu anda perlu fungsi p.main dan p._main, atau yang serupa).

Contoh 1 dan 2 dengan jenis pemeriksaan

Contoh 1:

local args = getArgs(frame, {valueFunc = function (key, value)if key == 1 thenreturn valueelseif type(value) == 'string' thenvalue = mw.text.trim(value)if value ~= '' thenreturn valueelsereturn nilendelsereturn valueendend})

Contoh 2:

local args = getArgs(frame, {valueFunc = function (key, value)if type(value) == 'string' thenvalue = mw.ustring.lower(value)if mw.ustring.find(value, '%S') thenreturn valueelsereturn nilendelsereturn valueendend})

Juga, harap maklum bahawa fungsi valueFunc dipanggil lebih kurang setiap kali argumen diminta dari meja/jadual args, jadi jika anda mementingkan prestasi, anda harus memastikan bahawa anda tidak melakukan sesuatu yang tidak cekap dengan kod anda.

Bingkai dan bingkai induk (parent)

Argumen dalam args meja/jadual boleh dilalui dari bingkai semasa atau dari bingkai induknya pada masa yang sama. Untuk memahami maksudnya, adalah paling mudah untuk memberi contoh. Katakan bahawa kita mempunyai modul yang dipanggil Modul:ExampleArgs. Modul ini mencetak dua argumen kedudukan pertama yang diluluskan.

Modul:Kod ContohArgs
local getArgs = require('Modul:Arguments').getArgslocal p = {}function p.main(frame)local args = getArgs(frame)return p._main(args)endfunction p._main(args)local first = args[1] or ''local second = args[2] or ''return first .. ' ' .. secondendreturn p

Modul:ExampleArgs kemudian dipanggil oleh Templat:ExampleArgs, yang mengandungi kod {{#invoke:ExampleArgs|main|firstInvokeArg}}. Ini menghasilkan keputusan "firstInvokeArg".

Sekarang jika kita memanggil Templat:ExampleArgs, perkara berikut akan berlaku:

KodKeputusan
{{ExampleArgs}}firstInvokeArg
{{ExampleArgs|firstTemplateArg}}firstInvokeArg
{{ExampleArgs|firstTemplateArg|secondTemplateArg}}firstInvokeArg secondTemplateArg

Terdapat tiga pilihan yang boleh anda tetapkan untuk mengubah tingkah laku ini:frameOnly, parentOnly dan parentFirst. Jika anda menetapkan frameOnly maka hanya argumen yang diluluskan dari kerangka semasa yang akan diterima; jika anda menetapkan parentOnly maka hanya argumen yang dikeluarkan dari bingkai induk yang akan diterima; dan jika anda menetapkan parentFirst maka argumen akan dilalui dari kedua-dua bingkai semasa dan induk, tetapi bingkai induk akan mempunyai keutamaan daripada bingkai semasa. Berikut adalah hasil dari segi Templat:ExampleArgs:

frameOnly
KodKeputusan
{{ExampleArgs}}firstInvokeArg
{{ExampleArgs|firstTemplateArg}}firstInvokeArg
{{ExampleArgs|firstTemplateArg|secondTemplateArg}}firstInvokeArg
parentOnly
KodKeputusan
{{ExampleArgs}}
{{ExampleArgs|firstTemplateArg}}firstTemplateArg
{{ExampleArgs|firstTemplateArg|secondTemplateArg}}firstTemplateArg secondTemplateArg
parentFirst
KodKeputusan
{{ExampleArgs}}firstInvokeArg
{{ExampleArgs|firstTemplateArg}}firstTemplateArg
{{ExampleArgs|firstTemplateArg|secondTemplateArg}}firstTemplateArg secondTemplateArg

Nota:

  1. Jika anda menetapkan kedua-dua pilihan frameOnly dan parentOnly, modul tidak akan sama sekali mengeluarkan argumen dari #invoke. Ini mungkin bukan yang anda mahukan.
  2. Dalam beberapa keadaan, bingkai induk mungkin tidak tersedia, cth. jika getArgs diluluskan bingkai induk bukan bingkai semasa. Dalam kes ini, hanya kerangka argumen yang akan digunakan (kecuali jika parentOnly ditetapkan, dalam hal ini tidak ada argumen yang akan digunakan) dan pilihan parentFirst dan frameOnly tidak akan memberi kesan..

Wrappers

The wrappers option is used to specify a limited number of templates as wrapper templates, that is, templates whose only purpose is to call a module. If the module detects that it is being called from a wrapper template, it will only check for arguments in the parent frame; otherwise it will only check for arguments in the frame passed to getArgs. This allows modules to be called by either #invoke or through a wrapper template without the loss of performance associated with having to check both the frame and the parent frame for each argument lookup.

For example, the only content of Template:Side box (excluding content in <noinclude>...</noinclude> tags) is {{#invoke:Side box|main}}. There is no point in checking the arguments passed directly to the #invoke statement for this template, as no arguments will ever be specified there. We can avoid checking arguments passed to #invoke by using the parentOnly option, but if we do this then #invoke will not work from other pages either. If this were the case, the |text=Some text in the code {{#invoke:Side box|main|text=Some text}} would be ignored completely, no matter what page it was used from. By using the wrappers option to specify 'Template:Side box' as a wrapper, we can make {{#invoke:Side box|main|text=Some text}} work from most pages, while still not requiring that the module check for arguments on the Template:Side box page itself.

Wrappers can be specified either as a string, or as an array of strings.

local args = getArgs(frame, {wrappers = 'Template:Wrapper template'})


local args = getArgs(frame, {wrappers = {'Template:Wrapper 1','Template:Wrapper 2',-- Any number of wrapper templates can be added here.}})

Notes:

  1. The module will automatically detect if it is being called from a wrapper template's /sandbox subpage, so there is no need to specify sandbox pages explicitly.
  2. The wrappers option effectively changes the default of the frameOnly and parentOnly options. If, for example, parentOnly were explicitly set to false with wrappers set, calls via wrapper templates would result in both frame and parent arguments being loaded, though calls not via wrapper templates would result in only frame arguments being loaded.
  3. If the wrappers option is set and no parent frame is available, the module will always get the arguments from the frame passed to getArgs.

Menulis di meja/jadual args

Kadang-kadang berguna untuk menulis nilai baru ke meja/jadual args. Ini mungkin dilakukan dengan tetapan lalai modul ini. (Namun, ingatlah bahawa gaya pengkodan biasanya lebih baik untuk membuat jadual baru dengan nilai baru anda dan menyalin argumen dari jadual argumen jika diperlukan.)

args.foo = 'some value'

Adalah mungkin untuk mengubah tingkah laku ini dengan pilihan readOnly dan noOverwrite. Jika readOnly diatur maka tidak mustahil untuk menulis nilai ke meja/jadual argumen sama sekali. Jika noOverwrite ditetapkan, maka ia adalah mungkin untuk menambah nilai-nilai baru ke meja/jadual, tetapi ia tidak mungkin untuk menambah nilai jika ia akan menulis ganti sebarang argumen yang diluluskan dari #invoke.

Ref tags

This module uses metatables to fetch arguments from #invoke. This allows access to both the frame arguments and the parent frame arguments without using the pairs() function. This can help if your module might be passed <ref>...</ref> tags as input.

As soon as <ref>...</ref> tags are accessed from Lua, they are processed by the MediaWiki software and the reference will appear in the reference list at the bottom of the article. If your module proceeds to omit the reference tag from the output, you will end up with a phantom reference - a reference that appears in the reference list, but no number that links to it. This has been a problem with modules that use pairs() to detect whether to use the arguments from the frame or the parent frame, as those modules automatically process every available argument.

This module solves this problem by allowing access to both frame and parent frame arguments, while still only fetching those arguments when it is necessary. The problem will still occur if you use pairs(args) elsewhere in your module, however.

Batasan yang diketahui

Penggunaan metatables juga mempunyai kelemahannya. Sebilangan besar alat meja Lua biasa tidak akan berfungsi dengan baik di meja args, termasuk operasi fungsi #, next(), dan fungsi di perpustakaan meja/jadual. Sekiranya menggunakan ini penting untuk modul anda, anda harus menggunakan fungsi pemprosesan argumen anda sendiri dan bukannya modul ini.


-- Modul ini menyediakan pemprosesan argumen yang mudah disampaikan kepada Scribunto dari-- #invoke. Ia dimaksudkan untuk digunakan oleh modul Lua yang lain, dan tidak boleh-- dipanggil dari #invoke secara langsung.local libraryUtil = require('libraryUtil')local checkType = libraryUtil.checkTypelocal arguments = {}-- Generate four different tidyVal functions, so that we don't have to check the-- options every time we call it.local function tidyValDefault(key, val)if type(val) == 'string' thenval = val:match('^%s*(.-)%s*$')if val == '' thenreturn nilelsereturn valendelsereturn valendendlocal function tidyValTrimOnly(key, val)if type(val) == 'string' thenreturn val:match('^%s*(.-)%s*$')elsereturn valendendlocal function tidyValRemoveBlanksOnly(key, val)if type(val) == 'string' thenif val:find('%S') thenreturn valelsereturn nilendelsereturn valendendlocal function tidyValNoChange(key, val)return valendlocal function matchesTitle(given, title)local tp = type( given )return (tp == 'string' or tp == 'number') and mw.title.new( given ).prefixedText == titleendlocal translate_mt = { __index = function(t, k) return k end }function arguments.getArgs(frame, options)checkType('getArgs', 1, frame, 'table', true)checkType('getArgs', 2, options, 'table', true)frame = frame or {}options = options or {}--[[-- Set up argument translation.--]]options.translate = options.translate or {}if getmetatable(options.translate) == nil thensetmetatable(options.translate, translate_mt)endif options.backtranslate == nil thenoptions.backtranslate = {}for k,v in pairs(options.translate) dooptions.backtranslate[v] = kendendif options.backtranslate and getmetatable(options.backtranslate) == nil thensetmetatable(options.backtranslate, {__index = function(t, k)if options.translate[k] ~= k thenreturn nilelsereturn kendend})end--[[-- Get the argument tables. If we were passed a valid frame object, get the-- frame arguments (fargs) and the parent frame arguments (pargs), depending-- on the options set and on the parent frame's availability. If we weren't-- passed a valid frame object, we are being called from another Lua module-- or from the debug console, so assume that we were passed a table of args-- directly, and assign it to a new variable (luaArgs).--]]local fargs, pargs, luaArgsif type(frame.args) == 'table' and type(frame.getParent) == 'function' thenif options.wrappers then--[[-- The wrappers option makes Module:Arguments look up arguments in-- either the frame argument table or the parent argument table, but-- not both. This means that users can use either the #invoke syntax-- or a wrapper template without the loss of performance associated-- with looking arguments up in both the frame and the parent frame.-- Module:Arguments will look up arguments in the parent frame-- if it finds the parent frame's title in options.wrapper;-- otherwise it will look up arguments in the frame object passed-- to getArgs.--]]local parent = frame:getParent()if not parent thenfargs = frame.argselselocal title = parent:getTitle():gsub('/sandbox$', '')local found = falseif matchesTitle(options.wrappers, title) thenfound = trueelseif type(options.wrappers) == 'table' thenfor _,v in pairs(options.wrappers) doif matchesTitle(v, title) thenfound = truebreakendendend-- We test for false specifically here so that nil (the default) acts like true.if found or options.frameOnly == false thenpargs = parent.argsendif not found or options.parentOnly == false thenfargs = frame.argsendendelse-- options.wrapper isn't set, so check the other options.if not options.parentOnly thenfargs = frame.argsendif not options.frameOnly thenlocal parent = frame:getParent()pargs = parent and parent.args or nilendendif options.parentFirst thenfargs, pargs = pargs, fargsendelseluaArgs = frameend-- Set the order of precedence of the argument tables. If the variables are-- nil, nothing will be added to the table, which is how we avoid clashes-- between the frame/parent args and the Lua args.local argTables = {fargs}argTables[#argTables + 1] = pargsargTables[#argTables + 1] = luaArgs--[[-- Generate the tidyVal function. If it has been specified by the user, we-- use that; if not, we choose one of four functions depending on the-- options chosen. This is so that we don't have to call the options table-- every time the function is called.--]]local tidyVal = options.valueFuncif tidyVal thenif type(tidyVal) ~= 'function' thenerror("bad value assigned to option 'valueFunc'".. '(function expected, got '.. type(tidyVal).. ')',2)endelseif options.trim ~= false thenif options.removeBlanks ~= false thentidyVal = tidyValDefaultelsetidyVal = tidyValTrimOnlyendelseif options.removeBlanks ~= false thentidyVal = tidyValRemoveBlanksOnlyelsetidyVal = tidyValNoChangeendend--[[-- Set up the args, metaArgs and nilArgs tables. args will be the one-- accessed from functions, and metaArgs will hold the actual arguments. Nil-- arguments are memoized in nilArgs, and the metatable connects all of them-- together.--]]local args, metaArgs, nilArgs, metatable = {}, {}, {}, {}setmetatable(args, metatable)local function mergeArgs(tables)--[[-- Accepts multiple tables as input and merges their keys and values-- into one table. If a value is already present it is not overwritten;-- tables listed earlier have precedence. We are also memoizing nil-- values, which can be overwritten if they are 's' (soft).--]]for _, t in ipairs(tables) dofor key, val in pairs(t) doif metaArgs[key] == nil and nilArgs[key] ~= 'h' thenlocal tidiedVal = tidyVal(key, val)if tidiedVal == nil thennilArgs[key] = 's'elsemetaArgs[key] = tidiedValendendendendend--[[-- Define metatable behaviour. Arguments are memoized in the metaArgs table,-- and are only fetched from the argument tables once. Fetching arguments-- from the argument tables is the most resource-intensive step in this-- module, so we try and avoid it where possible. For this reason, nil-- arguments are also memoized, in the nilArgs table. Also, we keep a record-- in the metatable of when pairs and ipairs have been called, so we do not-- run pairs and ipairs on the argument tables more than once. We also do-- not run ipairs on fargs and pargs if pairs has already been run, as all-- the arguments will already have been copied over.--]]metatable.__index = function (t, key)--[[-- Fetches an argument when the args table is indexed. First we check-- to see if the value is memoized, and if not we try and fetch it from-- the argument tables. When we check memoization, we need to check-- metaArgs before nilArgs, as both can be non-nil at the same time.-- If the argument is not present in metaArgs, we also check whether-- pairs has been run yet. If pairs has already been run, we return nil.-- This is because all the arguments will have already been copied into-- metaArgs by the mergeArgs function, meaning that any other arguments-- must be nil.--]]if type(key) == 'string' thenkey = options.translate[key]endlocal val = metaArgs[key]if val ~= nil thenreturn valelseif metatable.donePairs or nilArgs[key] thenreturn nilendfor _, argTable in ipairs(argTables) dolocal argTableVal = tidyVal(key, argTable[key])if argTableVal ~= nil thenmetaArgs[key] = argTableValreturn argTableValendendnilArgs[key] = 'h'return nilendmetatable.__newindex = function (t, key, val)-- This function is called when a module tries to add a new value to the-- args table, or tries to change an existing value.if type(key) == 'string' thenkey = options.translate[key]endif options.readOnly thenerror('could not write to argument table key "'.. tostring(key).. '"; the table is read-only',2)elseif options.noOverwrite and args[key] ~= nil thenerror('could not write to argument table key "'.. tostring(key).. '"; overwriting existing arguments is not permitted',2)elseif val == nil then--[[-- If the argument is to be overwritten with nil, we need to erase-- the value in metaArgs, so that __index, __pairs and __ipairs do-- not use a previous existing value, if present; and we also need-- to memoize the nil in nilArgs, so that the value isn't looked-- up in the argument tables if it is accessed again.--]]metaArgs[key] = nilnilArgs[key] = 'h'elsemetaArgs[key] = valendendlocal function translatenext(invariant)local k, v = next(invariant.t, invariant.k)invariant.k = kif k == nil thenreturn nilelseif type(k) ~= 'string' or not options.backtranslate thenreturn k, velselocal backtranslate = options.backtranslate[k]if backtranslate == nil then-- Skip this one. This is a tail call, so this won't cause stack overflowreturn translatenext(invariant)elsereturn backtranslate, vendendendmetatable.__pairs = function ()-- Called when pairs is run on the args table.if not metatable.donePairs thenmergeArgs(argTables)metatable.donePairs = trueendreturn translatenext, { t = metaArgs }endlocal function inext(t, i)-- This uses our __index metamethodlocal v = t[i + 1]if v ~= nil thenreturn i + 1, vendendmetatable.__ipairs = function (t)-- Called when ipairs is run on the args table.return inext, t, 0endreturn argsendreturn arguments