Module:Version

From Path of Exile 2 Wiki
Revision as of 23:01, 25 October 2016 by >TheFrz
Jump to navigation Jump to search
Module documentation[view] [edit] [history] [purge]


Templates

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

local p = {}
local g_frame, g_args

-- -----

-- ----------------------------------------------------------------------------------------------------
-- Template: Version

-- todo: rework, add a mapping for fields
p.version = function(frame)
    g_args = getArgs(frame, { parentFirst = true })
    g_frame = util.misc.get_frame(frame)

    --[[ test args
    = p.version{
        before = '2.4.0h',
        patch = '2.4.1',
        patchdate = 'October 18, 2016',
        after = '2.4.1b',
    }
    --]]

    -- Parser
    local vargs = { before = '←', after = '→' }
    local properties = {}

    -- querying before and after
    for arg in pairs(vargs) do
        if g_args[arg] ~= nil then
            properties['Has version ' .. arg] = g_args[arg]

            local query = {}
            query[#query + 1] = string.format('[[Is version::%s]] [[Has release date::+]]', g_args[arg])
            query[#query + 1] = '?Has release date'

            local result = ''
            local results = util.smw.query(query, g_frame)
            if #results ~= 0 then
                result = results[1]['Has release date']
            end

            vargs[arg] = string.format('[[Version %s|%s %s]]<br>%s', g_args[arg], vargs[arg], g_args[arg], result)
        else
            vargs[arg] = nil
        end
    end

    properties['Is version'] = g_args['patch']
    properties['Has release date'] = g_args['patchdate']
    util.smw.set(g_frame, properties)

    -- Output
    -- todo: rework it somehow
    local patch_date = g_frame:callParserFunction('#show: Version ' .. g_args['patch'], { '?Has release date' })

    local tbl = mw.html.create('table')
    tbl
        :attr('class', 'wikitable successionbox')
        :tag('tr')
            :tag('th')
                :attr('colspan', 3)
                :wikitext('[[Version history|Version History]]')
                :done()
            :done()
        :tag('tr')
            :tag('td')
                :attr('style', 'width: 30%;')
                :wikitext(vargs['before'])
                :done()
            :tag('td')
                :attr('style', 'width: 40%;')
                :wikitext(string.format('[[Version %s]]<br>%s', g_args['patch'], patch_date))
                :done()
            :tag('td')
                :attr('style', 'width: 30%;')
                :wikitext(vargs['after'])

    local cats = {
        'Category:Versions',
    }

    return tostring(tbl) .. util.misc.add_category(cats)
end

-- -----

-- ----------------------------------------------------------------------------------------------------
-- Template: SMW version history

p.version_history_list = function(frame)
    g_args = getArgs(frame, { parentFirst = true })
    g_frame = util.misc.get_frame(frame)

    -- test args
    -- = p.version_history_list{conditions='[[Is version::~0.9*]]'}
    -- = p.version_history_list{conditions='[[Is version::~1*||~2*]]'}

    if g_args.conditions then
        g_args.conditions = g_args.conditions .. '[[Has release date::+]]'
    else
        g_args.conditions = '[[Is version::+]][[Has release date::+]]'
    end

    local query = {
        g_args.conditions,
        '?Is version',
        '?Has release date',
        sort = 'Has release date, Is version',
        order = 'desc, desc',
        link = 'none',
        offset = 0,
    }

    local results = {}
    repeat
        local result = util.smw.query(query, g_frame)
        query.offset = query.offset + #result

        for _, v in ipairs(result) do -- todo
        results[#results + 1] = v
        end
    until #result < 1000

    local out = {}
    local last_minor_version, current_list

    for i, result in ipairs(results) do
        local date = result['Has release date']
        local version = result['Is version']

        local v = util.cast.version(result['Is version'])
        local minor_version = table.concat({ v[1], v[2], v[3] }, '.') -- todo: rework it

        if minor_version ~= last_minor_version then

            if current_list ~= nil then
                out[#out + 1] = tostring(current_list)
            end

            out[#out + 1] = string.format('===Version %s===', minor_version)
            current_list = mw.html.create('ul')
        end

        current_list:tag('li'):wikitext(string.format('%s &ndash; [[Version %s]]', date, version))

        -- save the last list
        if (i == #results) and (current_list ~= nil) then
            out[#out + 1] = tostring(current_list)
        end

        last_minor_version = minor_version
    end

    return table.concat(out, '\n')
end

-- -----

return p