Module:Item infobox: Difference between revisions
Jump to navigation
Jump to search
(Improved using item query from Module:Item util. The most recent version of a map can be queried using the name of the map.) |
No edit summary |
||
(5 intermediate revisions by the same user not shown) | |||
Line 9: | Line 9: | ||
local m_util = require('Module:Util') | local m_util = require('Module:Util') | ||
local m_item_util = require('Module:Item util') | local m_item_util = require('Module:Item util') | ||
local m_cargo = require('Module:Cargo') | |||
-- Should we use the sandbox version of our submodules? | -- Should we use the sandbox version of our submodules? | ||
Line 18: | Line 19: | ||
local i18n = cfg.i18n | local i18n = cfg.i18n | ||
-- ---------------------------------------------------------------------------- | |||
-- Helper functions | |||
-- ---------------------------------------------------------------------------- | |||
local h = {} | |||
function h.navbar(page) | |||
local title = mw.title.new(page) | |||
local view = m_util.html.wikilink(title.prefixedText, i18n.navbar.view) | |||
local edit = m_util.html.url_link(title:fullUrl{action = 'edit'}, i18n.navbar.edit) | |||
local html = mw.html.create('span') | |||
html | |||
:addClass('item-infobox__navbar mw-editsection-like plainlinks') | |||
:wikitext( string.format('[%s] [%s]', view, edit) ) | |||
return html | |||
end | |||
-- ---------------------------------------------------------------------------- | -- ---------------------------------------------------------------------------- | ||
Line 32: | Line 50: | ||
]] | ]] | ||
args.item_name = args.item_name or args[1] | |||
local qargs = {} | local qargs = {} | ||
qargs.tables = { | qargs.tables = { | ||
Line 39: | Line 58: | ||
qargs.join = 'items._pageID = maps._pageID, maps.area_id = areas.id' | qargs.join = 'items._pageID = maps._pageID, maps.area_id = areas.id' | ||
qargs.fields = { | qargs.fields = { | ||
'items. | 'items.name=name', | ||
'items.metadata_id=metadata_id', | |||
'items.infobox_html=infobox', | |||
'items.metabox_html=metabox', | |||
'areas.infobox_html=area_infobox', | 'areas.infobox_html=area_infobox', | ||
} | } | ||
local result = m_item_util.query_item(args, qargs) | local result = m_item_util.query_item(args, qargs) | ||
if result.error then | if result.error then | ||
return result.error | if not m_util.cast.boolean(args.nocat) then | ||
return result.error:get_html() .. result.error:get_category() | |||
end | |||
return result.error:get_html() | |||
end | end | ||
args.main = args.main and m_util.cast.boolean(args.main) or mw.title.getCurrentTitle():inNamespace(0) | |||
if args.main then | |||
m_cargo.store({ | |||
_table = 'main_pages', | |||
data_page = result._pageName, | |||
id = result.metadata_id, | |||
name = result.name, | |||
}) | |||
mw.getCurrentFrame():expandTemplate{title = cfg.attach_template} | |||
end | |||
local infobox = mw.html.create('span') | |||
local navbar = h.navbar(result._pageName) | |||
infobox | |||
:addClass('infobox-page-container') | |||
:node(navbar) | |||
:wikitext(result.infobox) | |||
:wikitext(result.metabox or '') | |||
local out = { | local out = { | ||
tostring(infobox), | |||
result | result.area_infobox or '', | ||
} | } | ||
return table.concat(out) | return table.concat(out) |
Latest revision as of 18:59, 15 December 2022
The above documentation is transcluded from Module:Item infobox/doc.
Editors can experiment in this module's sandbox and testcases pages.
Subpages of this module.
Editors can experiment in this module's sandbox and testcases pages.
Subpages of this module.
-------------------------------------------------------------------------------
--
-- Module:Item infobox
--
-- This module implements Template:Item infobox.
-------------------------------------------------------------------------------
require('Module:No globals')
local m_util = require('Module:Util')
local m_item_util = require('Module:Item util')
local m_cargo = require('Module:Cargo')
-- Should we use the sandbox version of our submodules?
local use_sandbox = m_util.misc.maybe_sandbox('Item infobox')
-- The cfg table contains all localisable strings and configuration, to make it
-- easier to port this module to another wiki.
local cfg = use_sandbox and mw.loadData('Module:Item infobox/config/sandbox') or mw.loadData('Module:Item infobox/config')
local i18n = cfg.i18n
-- ----------------------------------------------------------------------------
-- Helper functions
-- ----------------------------------------------------------------------------
local h = {}
function h.navbar(page)
local title = mw.title.new(page)
local view = m_util.html.wikilink(title.prefixedText, i18n.navbar.view)
local edit = m_util.html.url_link(title:fullUrl{action = 'edit'}, i18n.navbar.edit)
local html = mw.html.create('span')
html
:addClass('item-infobox__navbar mw-editsection-like plainlinks')
:wikitext( string.format('[%s] [%s]', view, edit) )
return html
end
-- ----------------------------------------------------------------------------
-- Main functions
-- ----------------------------------------------------------------------------
local function _query_item_infobox(args)
--[[
Queries for an item infobox.
Examples
--------
=p.query_item_infobox{page='Map:Beach Map (Betrayal)'}
]]
args.item_name = args.item_name or args[1]
local qargs = {}
qargs.tables = {
'maps',
'areas',
}
qargs.join = 'items._pageID = maps._pageID, maps.area_id = areas.id'
qargs.fields = {
'items.name=name',
'items.metadata_id=metadata_id',
'items.infobox_html=infobox',
'items.metabox_html=metabox',
'areas.infobox_html=area_infobox',
}
local result = m_item_util.query_item(args, qargs)
if result.error then
if not m_util.cast.boolean(args.nocat) then
return result.error:get_html() .. result.error:get_category()
end
return result.error:get_html()
end
args.main = args.main and m_util.cast.boolean(args.main) or mw.title.getCurrentTitle():inNamespace(0)
if args.main then
m_cargo.store({
_table = 'main_pages',
data_page = result._pageName,
id = result.metadata_id,
name = result.name,
})
mw.getCurrentFrame():expandTemplate{title = cfg.attach_template}
end
local infobox = mw.html.create('span')
local navbar = h.navbar(result._pageName)
infobox
:addClass('infobox-page-container')
:node(navbar)
:wikitext(result.infobox)
:wikitext(result.metabox or '')
local out = {
tostring(infobox),
result.area_infobox or '',
}
return table.concat(out)
end
-- ----------------------------------------------------------------------------
-- Exported functions
-- ----------------------------------------------------------------------------
local p = {}
--
-- Template:Item infobox
--
p.query_item_infobox = m_util.misc.invoker_factory(_query_item_infobox, {
wrappers = cfg.wrappers.query_item_infobox,
})
return p