Module:Item link: Difference between revisions

From Path of Exile Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 40: Line 40:
     local img
     local img
     local result
     local result
     if args.skip_query == nil then
    local linked_page
     if m_util.cast.boolean(args.skip_query) then
        result = {
            _pageName = args.page or args.name
        }
        linked_page = args.link or result._pageName
    else
         local qargs = {}
         local qargs = {}
        qargs.tables = {'main_pages'}
        qargs.join = 'items._pageName = main_pages.data_page'
         qargs.fields = {
         qargs.fields = {
             'items.name=name',
             'items.name=name',
Line 49: Line 57:
             'items.size_x=size_x',
             'items.size_x=size_x',
             'items.size_y=size_y',
             'items.size_y=size_y',
            'main_pages._pageName=main_page',
         }
         }
        args.error_category = i18n.categories.broken_item_links
         result = m_item_util.query_item(args, qargs)
         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() .. m_util.misc.add_category({i18n.categories.broken_item_links})
            end
             return result.error:get_html()
        end
 
        -- If specifying an item by name, link to the main page; otherwise, link to where the item data lives.
        if result.main_page and m_util.table.has_any_key(args, {'item_name', 'item_name_exact'}) then
            linked_page = result.main_page
        else
            linked_page = result._pageName
         end
         end
    else
        result = {
            ['_pageName'] = args.page or args.name
        }
     end
     end
      
      
    -- Overrides from template parameters
     for k, prop in pairs(cfg.parameters) do
     for k, prop in pairs(cfg.parameters) do
         if args[k] ~= nil then
         if args[k] ~= nil then
Line 68: Line 83:
      
      
     if args.image ~= nil then
     if args.image ~= nil then
         if result['alternate_art_inventory_icons'] == nil then
         if result.alternate_art_inventory_icons == nil then
             return m_util.misc.raise_error_or_return{
             local err = m_util.Error{
                 raise_required = true,
                 message = string.format(
                args = args,
                msg = string.format(
                     i18n.errors.alt_art_undefined,
                     i18n.errors.alt_art_undefined,
                     result['_pageName']
                     result._pageName
                 ) .. m_util.misc.add_category({i18n.categories.broken_item_links})
                ),
             }
                code = 'alt_art_undefined',
                 issue = args.issue_all_errors or false,
                category = i18n.categories.broken_item_links,
            }:throw()
             return err:get_html() .. err:get_category()
         end
         end
          
          
         result['alternate_art_inventory_icons'] = m_util.string.split(
         result.alternate_art_inventory_icons = m_util.string.split(
             result['alternate_art_inventory_icons'],  
             result.alternate_art_inventory_icons,  
             ',%s*'
             ',%s*'
         )
         )
Line 86: Line 103:
         local index = tonumber(args.image)
         local index = tonumber(args.image)
         if index ~= nil then
         if index ~= nil then
             img = result['alternate_art_inventory_icons'][index]
             img = result.alternate_art_inventory_icons[index]
         else
         else
             -- offset 1 is needed
             -- offset 1 is needed
             local suffix = string.len(' inventory icon.png') + 1  
             local suffix = string.len(' inventory icon.png') + 1  
             -- add an extra offset by 1 to account for the space  
             -- add an extra offset by 1 to account for the space  
             local prefix = string.len(string.sub(result['inventory_icon'], 1, -suffix)) + 2
             local prefix = string.len(string.sub(result.inventory_icon, 1, -suffix)) + 2
              
              
             for _, filename in ipairs(result['alternate_art_inventory_icons']) do
             for _, filename in ipairs(result.alternate_art_inventory_icons) do
                 if string.sub(filename, prefix, -suffix) == args.image then
                 if string.sub(filename, prefix, -suffix) == args.image then
                     img = filename
                     img = filename
Line 102: Line 119:
          
          
         if img == nil then
         if img == nil then
             return m_util.misc.raise_error_or_return{
             local err = m_util.Error{
                 raise_required = true,
                 message = string.format(
                args = args,
                msg = string.format(
                     i18n.errors.alt_art_invalid_index,
                     i18n.errors.alt_art_invalid_index,
                     args.image, result['_pageName']
                     args.image,
                 ) .. m_util.misc.add_category({i18n.categories.broken_item_links})
                    result._pageName
             }
                ),
                code = 'alt_art_invalid_index',
                 issue = args.issue_all_errors or false,
                category = i18n.categories.broken_item_links,
            }:throw()
             return err:get_html() .. err:get_category()
         end
         end
     elseif result['inventory_icon'] ~= nil then
     elseif result.inventory_icon ~= nil then
         img = result['inventory_icon']
         img = result.inventory_icon
     end
     end
      
      
Line 118: Line 138:
     -- output
     -- output
     --
     --
   
    -- Maps have their main page on the item name now, link there instead.
    -- Hopefully there are no maps with identical names besides the series.
    local linked_page = args.link
    if result['class_id'] == 'Map' and m_util.table.has_any_key(args, {'item_name', 'item_name_exact'}) then
        linked_page = linked_page or args.item_name_exact or args.item_name
    else
        linked_page = linked_page or result['_pageName']
    end
      
      
     local container = mw.html.create('span')
     local container = mw.html.create('span')
Line 142: Line 153:
     end
     end
      
      
     if #result['name'] > 0 then
     if #result.name > 0 then
         activator:wikitext(string.format(
         activator:wikitext(string.format(
             '[[%s|%s]]',  
             '[[%s|%s]]',  
             linked_page,  
             linked_page,  
             result['name'] or result['_pageName']
             result.name or result._pageName
             )
             )
         )
         )
Line 154: Line 165:
     display:attr('class', 'c-item-hoverbox__display')
     display:attr('class', 'c-item-hoverbox__display')


     if result['html'] ~= nil then
     if result.html ~= nil then
         display:wikitext(result['html'])
         display:wikitext(result.html)
              
              
         if img then
         if img then
Line 163: Line 174:


     if img and args.large then
     if img and args.large then
         local width = tonumber(result['size_x']) or tonumber(args.width)
         local width = tonumber(result.size_x) or tonumber(args.width)
         local height = tonumber(result['size_y']) or tonumber(args.height)
         local height = tonumber(result.size_y) or tonumber(args.height)
         if width and height then
         if width and height then
             img = string.format(
             img = string.format(

Latest revision as of 18:57, 15 December 2022

Module documentation[view] [edit] [history] [purge]


This module is used on 45,000+ pages.

To avoid major disruption and server load, do not make unnecessary edits to this module. Test changes to this module first using its /sandbox and /testcases subpages . All of the changes can then be applied to this module in a single edit.

Consider discussing changes on the talk page or on Discord before implementing them.

Lua logo

This module depends on the following other modules:

The item module provides functionality for linking to items.

Overview

This module is responsible for {{Item link}} (({{Il}}) which create links to items exposed via cargo through Module:Item2.

Item templates

Module:Item2

All templates defined in Module:Item2:

Module:Item table

All templates defined in Module:Item table:

Module:Item link

All templates defined in Module:Item link:

Module:Item acquisition

ru:Модуль:Item link de:Modul:Item link

-------------------------------------------------------------------------------
-- 
--                             Module:Item link
-- 
-- This module implements Template:Item link.
-------------------------------------------------------------------------------

require('Module:No globals')
local m_util = require('Module:Util')
local m_item_util = require('Module:Item util')

-- Should we use the sandbox version of our submodules?
local use_sandbox = m_util.misc.maybe_sandbox('Item link')

-- 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 link/config/sandbox') or mw.loadData('Module:Item link/config')

local i18n = cfg.i18n

-- ----------------------------------------------------------------------------
-- Main functions
-- ----------------------------------------------------------------------------

local function _main(args)
    --[[
    Creates a link to the item and displays the item info box on hover 
    on the link.
    
    Examples
    --------
    = p.item_link{'Multistrike'}
    = p.item_link{'Multistrike Support'}
    --]]
    
    args.item_name = args.item_name or args[1]
    args.name = args.name or args[2]
    args.large = m_util.cast.boolean(args.large)
    
    local img
    local result
    local linked_page
    if m_util.cast.boolean(args.skip_query) then
        result = {
            _pageName = args.page or args.name
        }
        linked_page = args.link or result._pageName
    else
        local qargs = {}
        qargs.tables = {'main_pages'}
        qargs.join = 'items._pageName = main_pages.data_page'
        qargs.fields = {
            'items.name=name',
            'items.inventory_icon=inventory_icon',
            'items.html=html',
            'items.alternate_art_inventory_icons=alternate_art_inventory_icons',
            'items.size_x=size_x',
            'items.size_y=size_y',
            'main_pages._pageName=main_page',
        }
        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() .. m_util.misc.add_category({i18n.categories.broken_item_links})
            end
            return result.error:get_html()
        end

        -- If specifying an item by name, link to the main page; otherwise, link to where the item data lives.
        if result.main_page and m_util.table.has_any_key(args, {'item_name', 'item_name_exact'}) then
            linked_page = result.main_page
        else
            linked_page = result._pageName
        end
    end
    
    -- Overrides from template parameters
    for k, prop in pairs(cfg.parameters) do
        if args[k] ~= nil then
            result[prop] = args[k]
        end
    end
    
    if args.image ~= nil then
        if result.alternate_art_inventory_icons == nil then
            local err = m_util.Error{
                message = string.format(
                    i18n.errors.alt_art_undefined,
                    result._pageName
                ),
                code = 'alt_art_undefined',
                issue = args.issue_all_errors or false,
                category = i18n.categories.broken_item_links,
            }:throw()
            return err:get_html() .. err:get_category()
        end
        
        result.alternate_art_inventory_icons = m_util.string.split(
            result.alternate_art_inventory_icons, 
            ',%s*'
        )
        
        local index = tonumber(args.image)
        if index ~= nil then
            img = result.alternate_art_inventory_icons[index]
        else
            -- offset 1 is needed
            local suffix = string.len(' inventory icon.png') + 1 
            -- add an extra offset by 1 to account for the space 
            local prefix = string.len(string.sub(result.inventory_icon, 1, -suffix)) + 2
            
            for _, filename in ipairs(result.alternate_art_inventory_icons) do
                if string.sub(filename, prefix, -suffix) == args.image then
                    img = filename
                    break
                end
            end
        end
        
        if img == nil then
            local err = m_util.Error{
                message = string.format(
                    i18n.errors.alt_art_invalid_index,
                    args.image,
                    result._pageName
                ),
                code = 'alt_art_invalid_index',
                issue = args.issue_all_errors or false,
                category = i18n.categories.broken_item_links,
            }:throw()
            return err:get_html() .. err:get_category()
        end
    elseif result.inventory_icon ~= nil then
        img = result.inventory_icon
    end
    
    --
    -- output
    --
    
    local container = mw.html.create('span')
    container:addClass('c-item-hoverbox')

    if 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 args.large then
        activator:wikitext(string.format('[[%s|16x16px|link=|alt=]]', img))
    end
    
    if #result.name > 0 then
        activator:wikitext(string.format(
            '[[%s|%s]]', 
            linked_page, 
            result.name or result._pageName
            )
        )
    end
    
    local display = mw.html.create('span')
    display:attr('class', 'c-item-hoverbox__display')

    if result.html ~= nil then
        display:wikitext(result.html)
            
        if img then
            display:wikitext(string.format('[[%s|link=|alt=]]', img))
        end
    end

    if img and args.large then
        local width = tonumber(result.size_x) or tonumber(args.width)
        local height = tonumber(result.size_y) or tonumber(args.height)
        if width and height then
            img = string.format(
                '[[%s|%sx%spx|link=%s|alt=]]', 
                img, 
                width*cfg.image_size, 
                height*cfg.image_size, 
                linked_page
            )
        elseif width then
            img = string.format(
                '[[%s|%spx|link=%s|alt=]]', 
                img, 
                width*cfg.image_size, 
                linked_page
            )
        elseif height then
            img = string.format(
                '[[%s|x%spx|link=%s|alt=]]', 
                img, 
                height*cfg.image_size, 
                linked_page
            )
        else
            img = string.format(
                '[[%s|link=%s|alt=]]', 
                img, 
                linked_page
            )
        end
        activator:wikitext(img)
    end

    container
        :node(activator)
        :node(display)
        :done()
        
    return tostring(container)
end

-- ----------------------------------------------------------------------------
-- Exported functions
-- ----------------------------------------------------------------------------

local p = {}

-- 
-- Template:Item link
-- 
p.main = m_util.misc.invoker_factory(_main, {
    parentFirst = true,
    removeBlanks = false,
})

p.item_link = p.main

return p