モジュール:CargoIdLookup
ナビゲーションに移動
検索に移動
このモジュールについての説明文ページを モジュール:CargoIdLookup/doc に作成できます
local p = {}
local cargo = mw.ext.cargo
-- Scribunto (Lua main): https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual
-- Cargo Lua support: https://www.mediawiki.org/wiki/Extension:Cargo/Other_features#Lua_support
local function doMaterialLookup(fields, fieldName, fieldValue)
local where = string.format('%s="%s"', fieldName, fieldValue)
return cargo.query("Materials", fields, { where = where, limit = 1 })
end
-- Materials - Finds the first material with equivalent id, if none is found, searches for one with equivalent name.
-- This is a module function, referenced from the second argument to #invoke.
--
-- First arg: Thing to find.
-- fields: Fields to pass to the template.
-- template: Name of a template to render.
--
-- i.e. {{#invoke:CargoIdLookup|Materials|grass|fields=_pageName,name,id|template=Potion/row}}
-- pass in fields=one,two,three to specify custom field sets to return
function p.Materials(frame)
local materialToFind = frame.args[1]
local fields = frame.args["fields"]
local result = doMaterialLookup(fields, "id", materialToFind)
if #result == 0 then
result = doMaterialLookup(fields, "name", materialToFind)
end
if #result == 0 then
return "''Material '" .. (materialToFind or "") .. "' not found''"
end
return frame:expandTemplate{ title = frame.args["template"], args = result[1] }
end
return p