Module:Skill link: Difference between revisions
Jump to navigation
Jump to search
>OmegaK2 No edit summary |
>Illviljan (Making it easier to find the skills.) |
||
Line 5: | Line 5: | ||
local m_util = require('Module:Util') | local m_util = require('Module:Util') | ||
local getArgs = require('Module:Arguments').getArgs | local getArgs = require('Module:Arguments').getArgs | ||
local m_cargo = require('Module:cargo') | |||
local p = {} | local p = {} | ||
Line 15: | Line 16: | ||
errors = { | errors = { | ||
missing_id_parameter = 'Id parameter must be specified.', | missing_id_parameter = 'Id parameter must be specified.', | ||
incorrect_id = 'Please change the id from "%s" to any of these ids:<br>%s', | |||
multiple_results = 'Please choose only one of these ids:<br>%s', | |||
no_results = 'No results found.', | |||
category = 'Pages with skill infobox errors', | |||
}, | }, | ||
} | } | ||
Line 21: | Line 26: | ||
-- Helper functions and globals | -- Helper functions and globals | ||
-- ---------------------------------------------------------------------------- | -- ---------------------------------------------------------------------------- | ||
h = {} | |||
function h.disambiguate_skill(results) | |||
--[[ | |||
Disambiguates results from a skill query. | |||
]] | |||
local str = {} | |||
for i,v in pairs(results) do | |||
str[#str+1] = string.format( | |||
'%s - %s ([[%s|page]])', | |||
v['skill.skill_id'] or v['skill._pageName'] or '', | |||
string.gsub( | |||
v['skill.stat_text'] or 'N/A', | |||
'<br>', | |||
', ' | |||
) or '', | |||
v['skill._pageName'] or '' | |||
) | |||
end | |||
return table.concat(str, '<br>') | |||
end | |||
-- ---------------------------------------------------------------------------- | -- ---------------------------------------------------------------------------- | ||
Line 29: | Line 56: | ||
function p.skill_infobox_link(frame) | function p.skill_infobox_link(frame) | ||
--[[ | |||
Finds and shows the infobox of a skill. | |||
Examples: | |||
= p.skill_infobox_link{id="IcestormUniqueStaff12"} | |||
= p.skill_infobox_link{"Icestorm"} | |||
= p.skill_infobox_link{q_where = 'skill.active_skill_name="Icestorm"'} | |||
]] | |||
-- Get args | -- Get args | ||
tpl_args = getArgs(frame, { | tpl_args = getArgs(frame, { | ||
Line 35: | Line 72: | ||
frame = m_util.misc.get_frame(frame) | frame = m_util.misc.get_frame(frame) | ||
if tpl_args.id == | tpl_args.id = tpl_args.id or tpl_args[1] | ||
if not tpl_args.q_where and tpl_args.id then | |||
tpl_args.q_where = string.format( | |||
'skill.skill_id = "%s" OR skill.active_skill_name LIKE "%%%s%%"', | |||
tpl_args.id, | |||
tpl_args.id | |||
) | |||
elseif not (tpl_args.q_where and not tpl_args.id) then | |||
error(i18n.errors.missing_id_parameter) | error(i18n.errors.missing_id_parameter) | ||
end | end | ||
local results = | local results = m_cargo.query( | ||
{'skill'}, | {'skill'}, | ||
{ | { | ||
'skill.html', | 'skill.html', | ||
'skill.skill_screenshot', | 'skill.skill_screenshot', | ||
'skill.active_skill_name', | |||
'skill.skill_id', | |||
'skill.stat_text', | |||
'skill._pageName', | |||
}, | }, | ||
{ | { | ||
where= | where=tpl_args.q_where, | ||
} | } | ||
) | ) | ||
-- Helpful error handling: | |||
local err_tbl = { | |||
{ | |||
bool = #results == 0, | |||
disp = { | |||
i18n.errors.no_results, | |||
} | |||
}, | |||
{ | |||
bool = #results > 1, | |||
disp = { | |||
i18n.errors.multiple_results, | |||
h.disambiguate_skill(results), | |||
}, | |||
}, | |||
{ | |||
bool = tpl_args.id ~= results['skill.skill_id'], | |||
disp = { | |||
string.gsub( | |||
i18n.errors.incorrect_id, | |||
'%%s', | |||
tpl_args.id or '', | |||
1 | |||
), | |||
h.disambiguate_skill(results), | |||
}, | |||
}, | |||
} | |||
for _,v in ipairs(err_tbl) do | |||
if v.bool then | |||
local cats = {i18n.errors.category} | |||
return m_util.html.error( | |||
{msg = string.format(v.disp[1], v.disp[2]) .. m_util.misc.add_category(cats)} | |||
) | |||
end | |||
end | |||
results = results[1] | results = results[1] | ||
local container = mw.html.create('span') | local container = mw.html.create('span') | ||
container:attr('class', 'skill-box-page-container') | container | ||
:attr('class', 'skill-box-page-container') | |||
:wikitext(results['skill.html']) | |||
if results['skill.skill_screenshot'] then | if results['skill.skill_screenshot'] then | ||
container:wikitext(string.format('[[%s]]', results['skill.skill_screenshot'])) | container | ||
:wikitext(string.format( | |||
'[[%s]]', | |||
results['skill.skill_screenshot'] | |||
) | |||
) | |||
end | end | ||
Revision as of 05:16, 16 March 2019
Overview
Module for creating links to skills via cargo.
Skill templates
Module:Item
All templates defined in Module:Skill:
{{Skill}}
{{Skill progression}}
Module:Skill link
All templates defined in Module:Skill link:
The above documentation is transcluded from Module:Skill link/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 for skill linking
--
local m_util = require('Module:Util')
local getArgs = require('Module:Arguments').getArgs
local m_cargo = require('Module:cargo')
local p = {}
-- ----------------------------------------------------------------------------
-- Strings
-- ----------------------------------------------------------------------------
local i18n = {
errors = {
missing_id_parameter = 'Id parameter must be specified.',
incorrect_id = 'Please change the id from "%s" to any of these ids:<br>%s',
multiple_results = 'Please choose only one of these ids:<br>%s',
no_results = 'No results found.',
category = 'Pages with skill infobox errors',
},
}
-- ----------------------------------------------------------------------------
-- Helper functions and globals
-- ----------------------------------------------------------------------------
h = {}
function h.disambiguate_skill(results)
--[[
Disambiguates results from a skill query.
]]
local str = {}
for i,v in pairs(results) do
str[#str+1] = string.format(
'%s - %s ([[%s|page]])',
v['skill.skill_id'] or v['skill._pageName'] or '',
string.gsub(
v['skill.stat_text'] or 'N/A',
'<br>',
', '
) or '',
v['skill._pageName'] or ''
)
end
return table.concat(str, '<br>')
end
-- ----------------------------------------------------------------------------
-- Page functions
-- ----------------------------------------------------------------------------
local p = {}
function p.skill_infobox_link(frame)
--[[
Finds and shows the infobox of a skill.
Examples:
= p.skill_infobox_link{id="IcestormUniqueStaff12"}
= p.skill_infobox_link{"Icestorm"}
= p.skill_infobox_link{q_where = 'skill.active_skill_name="Icestorm"'}
]]
-- Get args
tpl_args = getArgs(frame, {
parentFirst = true
})
frame = m_util.misc.get_frame(frame)
tpl_args.id = tpl_args.id or tpl_args[1]
if not tpl_args.q_where and tpl_args.id then
tpl_args.q_where = string.format(
'skill.skill_id = "%s" OR skill.active_skill_name LIKE "%%%s%%"',
tpl_args.id,
tpl_args.id
)
elseif not (tpl_args.q_where and not tpl_args.id) then
error(i18n.errors.missing_id_parameter)
end
local results = m_cargo.query(
{'skill'},
{
'skill.html',
'skill.skill_screenshot',
'skill.active_skill_name',
'skill.skill_id',
'skill.stat_text',
'skill._pageName',
},
{
where=tpl_args.q_where,
}
)
-- Helpful error handling:
local err_tbl = {
{
bool = #results == 0,
disp = {
i18n.errors.no_results,
}
},
{
bool = #results > 1,
disp = {
i18n.errors.multiple_results,
h.disambiguate_skill(results),
},
},
{
bool = tpl_args.id ~= results['skill.skill_id'],
disp = {
string.gsub(
i18n.errors.incorrect_id,
'%%s',
tpl_args.id or '',
1
),
h.disambiguate_skill(results),
},
},
}
for _,v in ipairs(err_tbl) do
if v.bool then
local cats = {i18n.errors.category}
return m_util.html.error(
{msg = string.format(v.disp[1], v.disp[2]) .. m_util.misc.add_category(cats)}
)
end
end
results = results[1]
local container = mw.html.create('span')
container
:attr('class', 'skill-box-page-container')
:wikitext(results['skill.html'])
if results['skill.skill_screenshot'] then
container
:wikitext(string.format(
'[[%s]]',
results['skill.skill_screenshot']
)
)
end
return tostring(container)
end
-- ----------------------------------------------------------------------------
-- End
-- ----------------------------------------------------------------------------
return p