Module:Skill link: Difference between revisions
		
		
		
		Jump to navigation
		Jump to search
		
>Illviljan mNo edit summary  | 
				>Illviljan  mNo edit summary  | 
				||
| Line 15: | Line 15: | ||
local i18n = {  | local i18n = {  | ||
     errors = {  |      errors = {  | ||
         missing_id_parameter = '  |          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',  |          multiple_results = 'Please choose only one of these ids:<br>%s',  | ||
         no_results = 'No results found.',  |          no_results = 'No results found.',  | ||
| Line 71: | Line 70: | ||
     })  |      })  | ||
     frame = m_util.misc.get_frame(frame)  |      frame = m_util.misc.get_frame(frame)  | ||
     tpl_args.id = tpl_args.id   |      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(  |          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   |         )  | ||
     elseif (tpl_args.id == nil) and (tpl_args.q_where == nil) then  | |||
         error(i18n.errors.missing_id_parameter)  |          error(i18n.errors.missing_id_parameter)  | ||
     end  |      end  | ||
| Line 98: | Line 101: | ||
         }  |          }  | ||
     )  |      )  | ||
     -- Helpful error handling:  |      -- Helpful error handling:  | ||
     local err_tbl = {  |      local err_tbl = {  | ||
         {  |          {  | ||
             bool = #results == 0,  |              bool = function()   | ||
                return #results == 0    | |||
                 i18n.errors.no_results  |             end,  | ||
             display = function()   | |||
                 return i18n.errors.no_results  | |||
             end,  | |||
         },  |          },  | ||
         {  |          {  | ||
             bool = #results > 1,  |              bool = function()  | ||
                return #results > 1  | |||
                 i18n.errors.multiple_results,  |             end,  | ||
             display = function()  | |||
                 return string.format(  | |||
                    i18n.errors.multiple_results,    | |||
                    h.disambiguate_skill(results)  | |||
                )  | |||
             end,  | |||
         },  |          },  | ||
         {  |          {  | ||
             bool = tpl_args.id   |              bool = function()  | ||
                return tpl_args.id == nil  | |||
                 string.  |             end,  | ||
                     i18n.errors.  |              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    |      for _,v in ipairs(err_tbl) do    | ||
         if v.bool then  |          if v.bool() then  | ||
             cats[#cats+1] = i18n.errors.category  | |||
             out[#out+1] = m_util.html.error({msg = v.display()})  | |||
             break  | |||
         end  |          end  | ||
     end  |      end  | ||
     local container = mw.html.create('span')  |      local container = mw.html.create('span')  | ||
     for _,v in ipairs(results) do   | |||
         container  |          container  | ||
             :wikitext(string.format(  |              :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  |      end  | ||
     return tostring(container)  |      return tostring(container) .. table.concat(out, '') .. m_util.misc.add_category(cats)  | ||
end  | end  | ||
Revision as of 08:49, 16 March 2019
Module for creating links to skills via cargo.
Implemented templates
- {{Skill link}}
 - {{Query skill infobox}}
 
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',
    },
}
-- ----------------------------------------------------------------------------
-- 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)
        
    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
-- ----------------------------------------------------------------------------
-- End
-- ----------------------------------------------------------------------------
return p