Module:Guide: Difference between revisions

From Path of Exile 2 Wiki
Jump to navigation Jump to search
No edit summary
m (13 revisions imported)
 
(10 intermediate revisions by the same user not shown)
Line 6: Line 6:
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------


require('Module:No globals')
require('strict')
local m_util = require('Module:Util')
local m_util = require('Module:Util')


Line 26: Line 26:
local h = {}
local h = {}


function h.get_last_revision_date()
function h.format_date(value, format)
     return mw.getCurrentFrame():preprocess('{{#time:F j, Y|{{REVISIONTIMESTAMP}}}}')
    format = format or 'display'
    local formats = {
        display = 'F j, Y',
        cargo = 'Y-m-d',
    }
     return mw.getContentLanguage():formatDate(formats[format], value)
end
 
function h.get_page_revision_date(page_id)
    local date
    if page_id ~= nil then
        local page = m_cargo.query(
            {'_pageData'},
            {'_modificationDate'},
            {
                where = string.format('_pageID = %s', page_id),
                limit = 1,
            }
        )
        if #page > 0 then
            date = page[1]['_modificationDate']
        end
    end
    return date
end
 
function h.error_text(text)
    return tostring(
        mw.html.create('strong')
            :addClass('error')
            :wikitext(text)
    )
end
end


Line 43: Line 74:
             field = 'subject',
             field = 'subject',
             type = 'Text',
             type = 'Text',
            default = '',
         },
         },
         version = {
         version = {
             field = 'version',
             field = 'version',
             type = 'String',
             type = 'String',
             func = function (tpl_args, value)
             func = function (args, value)
                 if value == nil then
                 if value ~= nil then
                     return value
                     value = m_util.cast.version(value, {return_type='string'})
                 end
                 end
                 return m_util.cast.version(value, {return_type='string'})
                 return value
             end,
             end,
         },
         },
Line 57: Line 89:
             field = 'date',
             field = 'date',
             type = 'Date',
             type = 'Date',
            func = function (args, value)
                if value ~= nil then
                    value = h.format_date(value, 'cargo')
                end
                return value
            end,
         },
         },
     }
     }
}
-- ----------------------------------------------------------------------------
-- Display
-- ----------------------------------------------------------------------------
local display = {}
display.table_map = {
    {
        header = i18n.table.guide,
        sort_type = 'text',
        func = function (data)
            return mw.html.create('td')
                :wikitext(m_util.html.wikilink(data['guides._pageName'], data['guides._pageTitle']))
        end,
    },
    {
        header = i18n.table.subject,
        sort_type = 'text',
        func = function (data)
            local subject = data['guides.subject']
            return mw.html.create('td')
                :wikitext(subject)
        end,
    },
    {
        header = i18n.table.version,
        sort_type = 'text',
        func = function (data)
            local version = data['guides.version']
            if version then
                return mw.html.create('td')
                    :attr('data-sort-value', version)
                    :wikitext(string.format(i18n.table.version_link, version, version))
            end
            return m_util.html.table_cell('na')
        end,
    },
    {
        header = i18n.table.date,
        sort_type = 'date',
        func = function (data)
            local date = data['guides.date'] or data['_pageData._modificationDate']
            return mw.html.create('td')
                :attr('data-sort-value', h.format_date(date, 'cargo'))
                :wikitext(h.format_date(date, 'display'))
        end,
    },
}
}


Line 65: Line 152:
-- ----------------------------------------------------------------------------
-- ----------------------------------------------------------------------------


local function _main(tpl_args)
local function _main(args)
     tpl_args = tpl_args or {}
     args = args or {}
     tpl_args.date = tpl_args.date or h.get_last_revision_date()
     local frame = mw.getCurrentFrame()
     local page_title = mw.title.getCurrentTitle()
     local title_obj = mw.title.getCurrentTitle()
     if page_title:inNamespaces(cfg.guide_namespace) then
     if title_obj:inNamespaces(cfg.guide_namespace) then
        tpl_args.subject = tpl_args.subject or m_util.html.error{msg=i18n.errors.no_subject}
 
         -- Store Cargo data
         -- Store Cargo data
         m_cargo.store_mapped_args{
         m_cargo.store_mapped_args{
             tpl_args = tpl_args,
             tpl_args = {
                subject = args.subject,
                version = args.version,
                date = args.date,
            },
             table_map = tables.guides,
             table_map = tables.guides,
         }
         }


         -- Attach
         -- Attach
         mw.getCurrentFrame():expandTemplate{title = cfg.attach_templates.guides}
         frame:expandTemplate{title = cfg.attach_templates.guides}
     end
     end


     -- Make message box
     -- Make message box
     local title = mw.html.create()
     local title = mw.html.create()
     if tpl_args.subject then
     local subject = args.subject or h.error_text(i18n.errors.no_subject)
        title
    title
            :wikitext(string.format(i18n.message_box.guide_subject, tpl_args.subject))
        :wikitext(string.format(i18n.message_box.guide_subject, subject))
    else
        title
            :wikitext(i18n.message_box.guide)
    end
     local text = mw.html.create()
     local text = mw.html.create()
     if tpl_args.note then
     if args.note then
         text
         text
             :wikitext(tpl_args.note)
             :wikitext(args.note)
             :newline()
             :newline()
             :newline()
             :newline()
     end
     end
     if tpl_args.version then
     if args.version then
         text
         text
             :wikitext(string.format(i18n.message_box.version_valid, tpl_args.version))
             :wikitext(string.format(i18n.message_box.version_valid, args.version))
             :wikitext(' ')
             :wikitext(' ')
     end
     end
    local date = args.date or h.get_page_revision_date(title_obj.id)
     text
     text
         :wikitext(string.format(
         :wikitext(string.format(
             i18n.message_box.last_updated,
             i18n.message_box.last_updated,
             string.format('{{#dateformat:%s|mdy}}', tpl_args.date)
             h.format_date(date, 'display')
         ))
         ))
     local mbox = mw.getCurrentFrame():expandTemplate{title = i18n.templates.message_box, args = {
     local mbox = frame:expandTemplate{title = i18n.templates.message_box, args = {
         type = 'info',
         type = 'info',
         image = i18n.icons.guide,
         image = i18n.icons.guide,
Line 122: Line 208:
end
end


local function _guide_list(tpl_args)
local function _guide_table(args)
      
     -- Build Cargo query
    local tables = {'guides', '_pageData'}
    local fields = {
        'guides._pageID',
        'guides._pageName',
        'guides._pageTitle',
        'guides.subject',
        'guides.version',
        'guides.date',
        '_pageData._modificationDate',
    }
    local query = {
        where = args.where,
        join = table.concat({'guides._pageID=_pageData._pageID', args.join}, ', '),
        groupBy = table.concat({'guides._pageID', args.groupBy}, ', '),
        having = args.having,
        orderBy = args.orderBy or 'guides._pageName ASC',
        limit = args.limit,
        offset = args.offset,
    }
 
    -- Append additional tables
    args.tables = m_util.cast.table(args.tables)
    if type(args.tables) == 'table' and #args.tables > 0 then
        tables = m_util.table.merge(tables, args.tables)
    end
 
    -- Query results
    local results = m_cargo.query(tables, fields, query)
    if #results == 0 then
        return tostring(
            mw.html.create('em')
                :wikitext(i18n.table.no_results)
        )
    end
 
    -- Build table
    local tbl = mw.html.create('table')
        :addClass('wikitable sortable guide-table')
    local tr = tbl:tag('tr')
    for _, col in ipairs(display.table_map) do
        tr:tag('th')
            :wikitext(col.header)
    end
    for _, row in ipairs(results) do
        tr = tbl:tag('tr')
        for _, col in ipairs(display.table_map) do
            tr:node(col.func(row))
        end
    end
    return tostring(tbl)
end
end


Line 143: Line 279:


--  
--  
-- Template:Guide list
-- Template:Guide table
--  
--  
p.guide_list = m_util.misc.invoker_factory(_guide_list, {
p.guide_table = m_util.misc.invoker_factory(_guide_table, {
     wrappers = cfg.wrappers.guide_list,
     wrappers = cfg.wrappers.guide_table,
})
})


return p
return p

Latest revision as of 07:10, 7 December 2025

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


Lua logo

This module depends on the following other modules:

Templates

-------------------------------------------------------------------------------
-- 
--                             Module:Guide
-- 
-- This module implements Template:Guide.
-------------------------------------------------------------------------------

require('strict')
local m_util = require('Module:Util')

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

local m_cargo = use_sandbox and require('Module:Cargo/sandbox') or require('Module:Cargo')

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

local i18n = cfg.i18n

-- ----------------------------------------------------------------------------
-- Helper functions
-- ----------------------------------------------------------------------------

local h = {}

function h.format_date(value, format)
    format = format or 'display'
    local formats = {
        display = 'F j, Y',
        cargo = 'Y-m-d',
    }
    return mw.getContentLanguage():formatDate(formats[format], value)
end

function h.get_page_revision_date(page_id)
    local date
    if page_id ~= nil then
        local page = m_cargo.query(
            {'_pageData'},
            {'_modificationDate'},
            {
                where = string.format('_pageID = %s', page_id),
                limit = 1,
            }
        )
        if #page > 0 then
            date = page[1]['_modificationDate']
        end
    end
    return date
end

function h.error_text(text)
    return tostring(
        mw.html.create('strong')
            :addClass('error')
            :wikitext(text)
    )
end

-- ----------------------------------------------------------------------------
-- Cargo tables
-- ----------------------------------------------------------------------------

local tables = {}

tables.guides = {
    table = 'guides',
    order = {'subject', 'version', 'date'},
    fields = {
        subject = {
            field = 'subject',
            type = 'Text',
            default = '',
        },
        version = {
            field = 'version',
            type = 'String',
            func = function (args, value)
                if value ~= nil then
                    value = m_util.cast.version(value, {return_type='string'})
                end
                return value
            end,
        },
        date = {
            field = 'date',
            type = 'Date',
            func = function (args, value)
                if value ~= nil then
                    value = h.format_date(value, 'cargo')
                end
                return value
            end,
        },
    }
}

-- ----------------------------------------------------------------------------
-- Display
-- ----------------------------------------------------------------------------

local display = {}

display.table_map = {
    {
        header = i18n.table.guide,
        sort_type = 'text',
        func = function (data)
            return mw.html.create('td')
                :wikitext(m_util.html.wikilink(data['guides._pageName'], data['guides._pageTitle']))
        end,
    },
    {
        header = i18n.table.subject,
        sort_type = 'text',
        func = function (data)
            local subject = data['guides.subject']
            return mw.html.create('td')
                :wikitext(subject)
        end,
    },
    {
        header = i18n.table.version,
        sort_type = 'text',
        func = function (data)
            local version = data['guides.version']
            if version then
                return mw.html.create('td')
                    :attr('data-sort-value', version)
                    :wikitext(string.format(i18n.table.version_link, version, version))
            end
            return m_util.html.table_cell('na')
        end,
    },
    {
        header = i18n.table.date,
        sort_type = 'date',
        func = function (data)
            local date = data['guides.date'] or data['_pageData._modificationDate']
            return mw.html.create('td')
                :attr('data-sort-value', h.format_date(date, 'cargo'))
                :wikitext(h.format_date(date, 'display'))
        end,
    },
}

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

local function _main(args)
    args = args or {}
    local frame = mw.getCurrentFrame()
    local title_obj = mw.title.getCurrentTitle()
    if title_obj:inNamespaces(cfg.guide_namespace) then
        -- Store Cargo data
        m_cargo.store_mapped_args{
            tpl_args = {
                subject = args.subject,
                version = args.version,
                date = args.date,
            },
            table_map = tables.guides,
        }

        -- Attach
        frame:expandTemplate{title = cfg.attach_templates.guides}
    end

    -- Make message box
    local title = mw.html.create()
    local subject = args.subject or h.error_text(i18n.errors.no_subject)
    title
        :wikitext(string.format(i18n.message_box.guide_subject, subject))
    local text = mw.html.create()
    if args.note then
        text
            :wikitext(args.note)
            :newline()
            :newline()
    end
    if args.version then
        text
            :wikitext(string.format(i18n.message_box.version_valid, args.version))
            :wikitext(' ')
    end
    local date = args.date or h.get_page_revision_date(title_obj.id)
    text
        :wikitext(string.format(
            i18n.message_box.last_updated,
            h.format_date(date, 'display')
        ))
    local mbox = frame:expandTemplate{title = i18n.templates.message_box, args = {
        type = 'info',
        image = i18n.icons.guide,
        title = tostring(title),
        text = tostring(text),
    }}
    local cats = m_util.misc.add_category(i18n.categories.guides)
    local html = mw.html.create()
        :wikitext(mbox)
        :wikitext(cats)

    return tostring(html)
end

local function _guide_table(args)
    -- Build Cargo query
    local tables = {'guides', '_pageData'}
    local fields = {
        'guides._pageID',
        'guides._pageName',
        'guides._pageTitle',
        'guides.subject',
        'guides.version',
        'guides.date',
        '_pageData._modificationDate',
    }
    local query = {
        where = args.where,
        join = table.concat({'guides._pageID=_pageData._pageID', args.join}, ', '),
        groupBy = table.concat({'guides._pageID', args.groupBy}, ', '),
        having = args.having,
        orderBy = args.orderBy or 'guides._pageName ASC',
        limit = args.limit,
        offset = args.offset,
    }

    -- Append additional tables
    args.tables = m_util.cast.table(args.tables)
    if type(args.tables) == 'table' and #args.tables > 0 then
        tables = m_util.table.merge(tables, args.tables)
    end

    -- Query results
    local results = m_cargo.query(tables, fields, query)
    if #results == 0 then
        return tostring(
            mw.html.create('em')
                :wikitext(i18n.table.no_results)
        )
    end

    -- Build table
    local tbl = mw.html.create('table')
        :addClass('wikitable sortable guide-table')
    local tr = tbl:tag('tr')
    for _, col in ipairs(display.table_map) do
        tr:tag('th')
            :wikitext(col.header)
    end
    for _, row in ipairs(results) do
        tr = tbl:tag('tr')
        for _, col in ipairs(display.table_map) do
            tr:node(col.func(row))
        end
    end
    return tostring(tbl)
end

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

local p = {}

p.table_guides = m_cargo.declare_factory{data=tables.guides}

-- 
-- Template:Guide
-- 
p.main = m_util.misc.invoker_factory(_main, {
    wrappers = cfg.wrappers.guide,
})
p.guide = p.main

-- 
-- Template:Guide table
-- 
p.guide_table = m_util.misc.invoker_factory(_guide_table, {
    wrappers = cfg.wrappers.guide_table,
})

return p