Module:New content

From Path of Exile 2 Wiki
Revision as of 03:53, 20 December 2020 by Vinifera7 (talk | contribs)
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}}.

local getArgs = require('Module:Arguments').getArgs
local util = require('Module:Util')
local messageBox = require('Module:Message box')

local cargo = mw.ext.cargo
local gFrame
local h = {}

local p = {}

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

function h.mainBlurb()
    local node = mw.html.create()
    local text
    if h.args.released then -- Newly released content
        if h.args.part then
            if h.args.part == 'section' then
                text = string.format('This section describes new Path of Exile content released in [[version %s]].', h.args.version)
            else
                text = string.format('Parts of this %s related to %s describe new Path of Exile content released in [[version %s]].', h.args.space, h.args.part, h.args.version)
            end
        else
            text = string.format('This %s describes new Path of Exile content released in [[version %s]].', h.args.space, h.args.version)
        end
    else -- Upcoming content
        if h.args.part then
            if h.args.part == 'section' then
                text = string.format('This section describes Path of Exile content coming in [[version %s]].', h.args.version)
            else
                text = string.format('Parts of this %s related to %s describe Path of Exile content coming in [[version %s]].', h.args.space, h.args.part, h.args.version)
            end
        else
            text = string.format('This %s describes Path of Exile content coming in [[version %s]].', h.args.space, h.args.version)
        end
    end
    node
        :tag('b')
            :node(text)
            :done()
        :wikitext(' ')
        :wikitext('Information may be missing or incorrect.')
        :tag('br')
            :done()
        :newline()
    return node
end

function h.editBlurb()
    local node = mw.html.create()
    local title = mw.title.getCurrentTitle()
    local link = string.format('[%s %s]', title:fullUrl{action = 'edit'}, 'expand this ' .. h.args.space)
    node
        :wikitext(string.format('Please %s so that it provides complete and accurate information.', link))
        :wikitext(' ')
    return node
end

function h.talkBlurb()
    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 h.args.talk then
            text = 'This is discussed in greater detail on the %s.'
            link = string.format('[%s#%s %s]', url, h.args.talk, 'talk page')
        else
            text = 'Relevant discussion may be found on the %s.'
            link = string.format('[%s %s]', url, 'talk page')
        end
        node
            :wikitext(string.format(text, link))
            :wikitext(' ')
    end
    return node
end

function h.image()
    local image
    if h.args.released then
        image = '[[File:Albino Rhoa Feather inventory icon.png|48px|alt=|link=]]'
    else
        image = '[[File:Divination card inventory icon.png|48px|alt=|link=]]'
    end
    return image
end

function h.messageBox()
    local text = mw.html.create()
    text
        :node(h.mainBlurb())
        :node(h.editBlurb())
        :node(h.talkBlurb())
    return messageBox.main('mbox', {
        type = 'content',
        image = h.image(),
        text = tostring(text),
    })
end

function h.categories()
    local cat
    if h.args.released then
        cat = util.misc.add_category('Newly released content')
    else
        cat = util.misc.add_category('Upcoming content')
    end
    return cat
end

----------------------------------------------------------------------------
-- main() - Implements Template:New content
----------------------------------------------------------------------------

function p.main(frame)
    local args = getArgs(frame, {
        parentFirst = true
    })
    gFrame = frame
    return p._main(args)
end

function p._main(args)
    local frame = util.misc.get_frame(gFrame)
    args = args or {}
    args.part = args[1]
    args.type = args.type or frame:expandTemplate{title = 'SUBJECTSPACE formatted'}
    args.space = args.space or args.type
    args.talksection = args.talksection or args.discuss
    args.talk = args.talk or args.talksection
    if type(args.version) ~= 'string' then -- Version is required
        error(string.format('Argument "%s" is required.', 'version'))
    end
    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"', util.cast.version(args.version, {return_type = 'string'}), now),
            limit = 1,
            groupBy = '_pageID',
        }
    )
    if #version > 0 then -- Version has already been released
        args.released = true
    else -- Version does not exist or has not been released yet
        args.released = false
    end
    h.args = args
    local html = mw.html.create()
        :wikitext(h.messageBox())
        :wikitext(h.categories())
    return tostring(html)
end

return p