Module:New content

From Path of Exile 2 Wiki
Revision as of 20:57, 23 September 2024 by Vinifera7 (talk | contribs) (9 revisions imported)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
Module documentation[view] [edit] [history] [purge]


Lua logo

This module depends on the following other modules:

Implements {{New content}}.

-------------------------------------------------------------------------------
-- 
--                             Module:New content
-- 
-- This module implements Template:New content.
-------------------------------------------------------------------------------

local m_util = require('Module:Util')

local cargo = mw.ext.cargo
local m = {}

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

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

local i18n = cfg.i18n

----------------------------------------------------------------------------
-- Message box pseudo-class
----------------------------------------------------------------------------

function m:new(version, released, part, space, talk)
    self.version = version
    self.released = released or false
    self.part = part
    self.space = space
    self.talk = talk
    local text = mw.html.create()
        :node(self:getEditBlurb())
        :node(self:getTalkBlurb())
    local mbox = mw.getCurrentFrame():expandTemplate{title = i18n.templates.message_box, args = {
        type = 'content',
        image = self:getImage(),
        title = tostring(self:getMainBlurb()),
        text = tostring(text),
    }}
    local cats = self:getCategories()
    local html = mw.html.create()
        :wikitext(mbox)
        :wikitext(cats)
    return tostring(html)
end

function m:getMainBlurb()
    local node = mw.html.create()
    local text
    if self.released then -- Newly released content
        if self.part then
            if self.part == 'section' then
                text = string.format(i18n.section_new, self.version)
            else
                text = string.format(i18n.parts_new, self.space, self.part, self.version)
            end
        else
            text = string.format(i18n.page_new, self.space, self.version)
        end
    else -- Upcoming content
        if self.part then
            if self.part == 'section' then
                text = string.format(i18n.section_upcoming, self.version)
            else
                text = string.format(i18n.parts_upcoming, self.space, self.part, self.version)
            end
        else
            text = string.format(i18n.page_upcoming, self.space, self.version)
        end
    end
    node
        :wikitext(text)
        :wikitext(' ')
        :wikitext(i18n.missing_incorrect)
        :newline()
        :newline()
    return node
end

function m:getEditBlurb()
    local node = mw.html.create()
    local title = mw.title.getCurrentTitle()
    local link = mw.html.create('span')
        :addClass('plainlinks')
        :wikitext(string.format('[%s %s %s]', title:fullUrl{action = 'edit'}, i18n.expand_this, self.space))
    node
        :wikitext(string.format(i18n.please_edit, tostring(link)))
        :wikitext(' ')
    return node
end

function m:getTalkBlurb()
    local node = mw.html.create()
    local title = mw.title.getCurrentTitle()
    if title.canTalk and title.talkPageTitle.exists then
        local text, link
        local url = title.talkPageTitle:fullUrl()
        if self.talk then
            text = i18n.discussion
            link = string.format('[%s#%s %s]', url, self.talk, i18n.talk_page)
        else
            text = i18n.relevant_discussion
            link = string.format('[%s %s]', url, i18n.talk_page)
        end
        node
            :wikitext(string.format(text, link))
            :wikitext(' ')
    end
    return node
end

function m:getImage()
    local image
    if self.released then
        image = i18n.icons.newly_released_content
    else
        image = i18n.icons.upcoming_content
    end
    return image
end

function m:getCategories()
    local cat
    if self.released then
        cat = m_util.misc.add_category(i18n.categories.newly_released_content)
    else
        cat = m_util.misc.add_category(i18n.categories.upcoming_content)
    end
    return cat
end

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

local function _main(args)
    args = args or {}
    args.version = args.version or args[1]
    if type(args.version) ~= 'string' then -- Version is required
        error(i18n.errors.version_undefined)
    end
    args.part = args.part or args[2]
    args.space = args.space or mw.getCurrentFrame():expandTemplate{title = i18n.templates.subjectspace_formatted}
    local now = os.date('!%F %T') -- Current UTC timestamp in Y-m-d H:i:s format
    local version = cargo.query( -- Query specified version with release date before current date
        'versions', 
        '_pageName',
        {
            where = string.format('version = "%s" AND release_date < "%s"', m_util.cast.version(args.version, {return_type = 'string'}), now),
            limit = 1,
            groupBy = '_pageID',
        }
    )
    return m:new(args.version, #version > 0, args.part, args.space, args.talk)
end

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

local p = {}

-- 
-- Template:New content
-- 
p.main = m_util.misc.invoker_factory(_main, {
    wrappers = cfg.wrapper,
})

return p