[dismiss]
The wiki is currently a work in progress. If you'd like to help out, please check the Community Portal and our getting started guide. Also, check out our sister project on poewiki.net.
Module:Skill link: Difference between revisions
Jump to navigation
Jump to search
>Illviljan mNo edit summary |
>Illviljan m (Add support for skill links.) |
||
Line 19: | Line 19: | ||
no_results = 'No results found.', | no_results = 'No results found.', | ||
category = 'Pages with skill infobox errors', | category = 'Pages with skill infobox errors', | ||
-- Skill link: | |||
invalid_args = 'id, page or name must be specified', | |||
too_many_results = 'Too many skills found with q_where = %s', | |||
no_results_found = 'No skills found with q_where = %s', | |||
invalid_format = 'Invalid return format specified: %s', | |||
skill_link_cats = 'Pages with skill link errors', | |||
}, | }, | ||
} | } | ||
-- ---------------------------------------------------------------------------- | |||
-- Constants & Data | |||
-- ---------------------------------------------------------------------------- | |||
local c = {} | |||
-- c.image_size = 39 | |||
-- c.image_size_full = c.image_size * 2 | |||
c.parameters = { | |||
name = 'skill.active_skill_name', | |||
icon = 'skill.icon', | |||
html = 'skiill.html', | |||
} | |||
c.selectors = {'id', 'page', 'name'} | |||
-- ---------------------------------------------------------------------------- | -- ---------------------------------------------------------------------------- | ||
Line 46: | Line 70: | ||
end | end | ||
return table.concat(str, '<br>') | return table.concat(str, '<br>') | ||
end | |||
function h.format_skill_icon(skill, tpl_args) | |||
--[[ | |||
Add a skill image. | |||
]] | |||
if tpl_args.icon ~= nil then | |||
skill['skill.skill_icon'] = tpl_args.icon | |||
end | |||
if skill['skill.skill_icon'] == nil then | |||
return '' | |||
end | |||
local out | |||
if tpl_args.large then | |||
out = string.format( | |||
'[[%s|link=%s]]', | |||
skill['skill.skill_icon'], | |||
tpl_args.main_page | |||
) | |||
else | |||
-- Inline size | |||
out = string.format( | |||
'[[%s|14px|link=%s]]', | |||
skill['skill.skill_icon'], | |||
tpl_args.main_page | |||
) | |||
end | |||
return out | |||
end | end | ||
Line 163: | Line 219: | ||
end | end | ||
function p.skill_link(frame) | |||
--[[ | |||
Links a skill | |||
Examples | |||
-------- | |||
= p.skill_link{id="IcestormUniqueStaff12"} | |||
= p.skill_link{ | |||
skip_query=true, | |||
page='Skill:IcestormUniqueStaff12', | |||
name='test', | |||
icon='File:IcestormUniqueStaff12 skill icon.png', | |||
large=1, | |||
} | |||
]] | |||
-- Get args | |||
tpl_args = getArgs(frame, { | |||
parentFirst = true | |||
}) | |||
frame = m_util.misc.get_frame(frame) | |||
-- Check if the correct parameters have been set: | |||
if m_util.table.has_all_value(tpl_args, c.selectors) and tpl_args.skip_query == nil then | |||
return m_util.html.error{msg=i18n.errors.invalid_args .. m_util.misc.add_category(i18n.errors.cats)} | |||
end | |||
-- | |||
local skill | |||
if m_util.table.has_one_value(tpl_args, c.selectors, nil) and tpl_args.skip_query == nil then | |||
tpl_args.name = tpl_args.name or tpl_args[1] | |||
-- Create q_where depending on the input: | |||
if tpl_args.name then | |||
tpl_args.q_where = string.format('skill.active_skill_name="%s"', tpl_args.name) | |||
elseif tpl_args.id then | |||
tpl_args.q_where = string.format('skill.skill_id="%s"', tpl_args.id) | |||
elseif tpl_args.q_where then | |||
-- Use tpl_args.q_where. | |||
else | |||
return m_util.html.error{ | |||
msg=i18n.errors.invalid_args .. m_util.misc.add_category(i18n.errors.cats) | |||
} | |||
end | |||
-- Query cargo: | |||
local results = m_cargo.query( | |||
{'skill', 'main_pages'}, | |||
{ | |||
'skill._pageName', | |||
'skill.stat_text', | |||
-- 'skill.main_page', | |||
'skill.active_skill_name', | |||
'skill.skill_icon', | |||
-- 'passive_skills.html', | |||
'main_pages._pageName', | |||
}, | |||
{ | |||
join='skill.skill_id=main_pages.id', | |||
where=string.format( | |||
'(%s)', | |||
tpl_args.q_where | |||
), | |||
orderBy='skill.stat_text', | |||
limit=2, | |||
} | |||
) | |||
-- Check number of results, there should only be one result: | |||
if #results > 1 then | |||
return m_util.html.error{ | |||
msg=string.format( | |||
i18n.errors.too_many_results, | |||
tostring(tpl_args.q_where) .. m_util.misc.add_category(i18n.errors.cats) | |||
) | |||
} | |||
elseif #results < 1 then | |||
return m_util.html.error{ | |||
msg=string.format( | |||
i18n.errors.no_results_found, | |||
tostring(tpl_args.q_where) .. m_util.misc.add_category(i18n.errors.cats) | |||
) | |||
} | |||
end | |||
skill = results[1] | |||
else | |||
skill = { | |||
['skill.main_page']=tpl_args.page or tpl_args.name | |||
} | |||
end | |||
-- Add allowed parameters: | |||
for k, prop in pairs(c.parameters) do | |||
if tpl_args[k] ~= nil then | |||
skill[prop] = tpl_args[k] | |||
end | |||
end | |||
-- Set the link to the main page: | |||
tpl_args.main_page = skill['skill.main_page'] | |||
or skill['main_pages._pageName'] | |||
or skill['skill._pageName'] | |||
-- Format the skill icon: | |||
local img = h.format_skill_icon(skill, tpl_args) | |||
--------------------------------------------------------------------------- | |||
-- Output | |||
--------------------------------------------------------------------------- | |||
-- Normal inline link: | |||
if tpl_args.format == nil then | |||
local container = mw.html.create('span') | |||
container:addClass('c-item-hoverbox') | |||
if tpl_args.large then | |||
container:addClass('c-item-hoverbox--large') | |||
end | |||
local activator = mw.html.create('span') | |||
activator:addClass('c-item-hoverbox__activator') | |||
if img and not tpl_args.large then | |||
activator:wikitext(img) | |||
end | |||
activator:wikitext(string.format( | |||
'[[%s|%s]]', | |||
tpl_args.main_page, | |||
skill['skill.active_skill_name'] or tpl_args.main_page | |||
) | |||
) | |||
local display = mw.html.create('span') | |||
display:attr('class', 'c-item-hoverbox__display') | |||
if skill['skill.html'] ~= nil then | |||
display:wikitext(skill['skill.html']) | |||
if img then | |||
display:wikitext(img) | |||
end | |||
end | |||
if img and tpl_args.large then | |||
activator:wikitext(img) | |||
end | |||
container | |||
:node(activator) | |||
:node(display) | |||
:done() | |||
return tostring(container) | |||
else | |||
return m_util.html.error{ | |||
msg=string.format( | |||
i18n.error.invalid_format, | |||
tpl_args.format .. m_util.misc.add_category(i18n.errors.skill_link_cats) | |||
) | |||
} | |||
end | |||
end | |||
-- ---------------------------------------------------------------------------- | -- ---------------------------------------------------------------------------- | ||
-- End | -- End |
Revision as of 19:36, 8 October 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, please choose only one 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',
-- Skill link:
invalid_args = 'id, page or name must be specified',
too_many_results = 'Too many skills found with q_where = %s',
no_results_found = 'No skills found with q_where = %s',
invalid_format = 'Invalid return format specified: %s',
skill_link_cats = 'Pages with skill link errors',
},
}
-- ----------------------------------------------------------------------------
-- Constants & Data
-- ----------------------------------------------------------------------------
local c = {}
-- c.image_size = 39
-- c.image_size_full = c.image_size * 2
c.parameters = {
name = 'skill.active_skill_name',
icon = 'skill.icon',
html = 'skiill.html',
}
c.selectors = {'id', 'page', 'name'}
-- ----------------------------------------------------------------------------
-- 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
function h.format_skill_icon(skill, tpl_args)
--[[
Add a skill image.
]]
if tpl_args.icon ~= nil then
skill['skill.skill_icon'] = tpl_args.icon
end
if skill['skill.skill_icon'] == nil then
return ''
end
local out
if tpl_args.large then
out = string.format(
'[[%s|link=%s]]',
skill['skill.skill_icon'],
tpl_args.main_page
)
else
-- Inline size
out = string.format(
'[[%s|14px|link=%s]]',
skill['skill.skill_icon'],
tpl_args.main_page
)
end
return out
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)
if tpl_args.id and (tpl_args.q_where == nil) then
tpl_args.q_where = string.format(
'skill.skill_id = "%s"',
tpl_args.id
)
elseif tpl_args[1] and (tpl_args.q_where == nil) then
tpl_args.q_where = string.format(
'skill.skill_id = "%s" OR skill.active_skill_name LIKE "%%%s%%" OR skill.stat_text LIKE "%%%s%%"',
tpl_args[1],
tpl_args[1],
tpl_args[1]
)
elseif (tpl_args.id == nil) and (tpl_args.q_where == nil) 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 = function()
return #results == 0
end,
display = function()
return i18n.errors.no_results
end,
},
{
bool = function()
return #results > 1
end,
display = function()
return string.format(
i18n.errors.multiple_results,
h.disambiguate_skill(results)
)
end,
},
{
bool = function()
return tpl_args.id == nil
end,
display = function()
return string.format(
i18n.errors.missing_id_parameter,
h.disambiguate_skill(results)
)
end,
},
}
out = {}
local cats = {}
for _,v in ipairs(err_tbl) do
if v.bool() then
cats[#cats+1] = i18n.errors.category
out[#out+1] = m_util.html.error({msg = v.display()})
break
end
end
local container = mw.html.create('span')
for _,v in ipairs(results) do
container
:attr('class', 'skill-box-page-container')
:wikitext(v['skill.html'])
if v['skill.skill_screenshot'] then
container
:wikitext(string.format(
'[[%s]]',
v['skill.skill_screenshot']
)
)
end
end
return tostring(container) .. table.concat(out, '') .. m_util.misc.add_category(cats)
end
function p.skill_link(frame)
--[[
Links a skill
Examples
--------
= p.skill_link{id="IcestormUniqueStaff12"}
= p.skill_link{
skip_query=true,
page='Skill:IcestormUniqueStaff12',
name='test',
icon='File:IcestormUniqueStaff12 skill icon.png',
large=1,
}
]]
-- Get args
tpl_args = getArgs(frame, {
parentFirst = true
})
frame = m_util.misc.get_frame(frame)
-- Check if the correct parameters have been set:
if m_util.table.has_all_value(tpl_args, c.selectors) and tpl_args.skip_query == nil then
return m_util.html.error{msg=i18n.errors.invalid_args .. m_util.misc.add_category(i18n.errors.cats)}
end
--
local skill
if m_util.table.has_one_value(tpl_args, c.selectors, nil) and tpl_args.skip_query == nil then
tpl_args.name = tpl_args.name or tpl_args[1]
-- Create q_where depending on the input:
if tpl_args.name then
tpl_args.q_where = string.format('skill.active_skill_name="%s"', tpl_args.name)
elseif tpl_args.id then
tpl_args.q_where = string.format('skill.skill_id="%s"', tpl_args.id)
elseif tpl_args.q_where then
-- Use tpl_args.q_where.
else
return m_util.html.error{
msg=i18n.errors.invalid_args .. m_util.misc.add_category(i18n.errors.cats)
}
end
-- Query cargo:
local results = m_cargo.query(
{'skill', 'main_pages'},
{
'skill._pageName',
'skill.stat_text',
-- 'skill.main_page',
'skill.active_skill_name',
'skill.skill_icon',
-- 'passive_skills.html',
'main_pages._pageName',
},
{
join='skill.skill_id=main_pages.id',
where=string.format(
'(%s)',
tpl_args.q_where
),
orderBy='skill.stat_text',
limit=2,
}
)
-- Check number of results, there should only be one result:
if #results > 1 then
return m_util.html.error{
msg=string.format(
i18n.errors.too_many_results,
tostring(tpl_args.q_where) .. m_util.misc.add_category(i18n.errors.cats)
)
}
elseif #results < 1 then
return m_util.html.error{
msg=string.format(
i18n.errors.no_results_found,
tostring(tpl_args.q_where) .. m_util.misc.add_category(i18n.errors.cats)
)
}
end
skill = results[1]
else
skill = {
['skill.main_page']=tpl_args.page or tpl_args.name
}
end
-- Add allowed parameters:
for k, prop in pairs(c.parameters) do
if tpl_args[k] ~= nil then
skill[prop] = tpl_args[k]
end
end
-- Set the link to the main page:
tpl_args.main_page = skill['skill.main_page']
or skill['main_pages._pageName']
or skill['skill._pageName']
-- Format the skill icon:
local img = h.format_skill_icon(skill, tpl_args)
---------------------------------------------------------------------------
-- Output
---------------------------------------------------------------------------
-- Normal inline link:
if tpl_args.format == nil then
local container = mw.html.create('span')
container:addClass('c-item-hoverbox')
if tpl_args.large then
container:addClass('c-item-hoverbox--large')
end
local activator = mw.html.create('span')
activator:addClass('c-item-hoverbox__activator')
if img and not tpl_args.large then
activator:wikitext(img)
end
activator:wikitext(string.format(
'[[%s|%s]]',
tpl_args.main_page,
skill['skill.active_skill_name'] or tpl_args.main_page
)
)
local display = mw.html.create('span')
display:attr('class', 'c-item-hoverbox__display')
if skill['skill.html'] ~= nil then
display:wikitext(skill['skill.html'])
if img then
display:wikitext(img)
end
end
if img and tpl_args.large then
activator:wikitext(img)
end
container
:node(activator)
:node(display)
:done()
return tostring(container)
else
return m_util.html.error{
msg=string.format(
i18n.error.invalid_format,
tpl_args.format .. m_util.misc.add_category(i18n.errors.skill_link_cats)
)
}
end
end
-- ----------------------------------------------------------------------------
-- End
-- ----------------------------------------------------------------------------
return p