Module:Version: Difference between revisions

From Path of Exile 2 Wiki
Jump to navigation Jump to search
>TheFrz
mNo edit summary
>TheFrz
mNo edit summary
Line 1: Line 1:
local getArgs = require('Module:Arguments').getArgs
local getArgs = require('Module:Arguments').getArgs
local util = require('Module:Util')
local util = require('Module:Util')
local tostring = tostring
local pairs = pairs
local string_format = string.format
local table_concat = table.concat
local mw_html = mw.html


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


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


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


     -- querying before and after
     -- querying before and after
     for arg in pairs(vargs) do
     for i in pairs(vargs) do
         if g_args[arg] ~= nil then
         if args[i] ~= nil then
             properties['Has version ' .. arg] = g_args[arg]
             local version = util.cast.version(args[i], {return_type = 'string'})
            props['Has version ' .. i] = version


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


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


             vargs[arg] = string.format('[[Version %s|%s %s]]<br>%s', g_args[arg], vargs[arg], g_args[arg], result)
             args[i .. '_format'] = string_format('[[Version %s|%s %s]]<br>%s', version, vargs[i], version, result)
         else
         else
             vargs[arg] = nil
             args[i .. '_format'] = nil
        end
    end
 
    props['Is version'] = args.patch
    props['Has release date'] = args.patchdate
 
    local version = util.cast.version(args.patch)
    local part_names = {'major', 'minor', 'patch', 'revision'}
    for i = 1, 4 do
        local part = version[i]
        if part then
            props[string_format('Has %s version part', part_names[i])] = part
         end
         end
     end
     end


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


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


     local tbl = mw.html.create('table')
     local tbl = mw_html.create('table')
     tbl
     tbl
         :attr('class', 'wikitable successionbox')
         :addClass('wikitable successionbox')
         :tag('tr')
         :tag('tr')
             :tag('th')
             :tag('th')
Line 67: Line 85:
         :tag('tr')
         :tag('tr')
             :tag('td')
             :tag('td')
                 :attr('style', 'width: 30%;')
                 :cssText('width: 30%')
                 :wikitext(vargs['before'])
                 :wikitext(args.before_format)
                 :done()
                 :done()
             :tag('td')
             :tag('td')
                 :attr('style', 'width: 40%;')
                 :cssText('width: 40%')
                 :wikitext(string.format('[[Version %s]]<br>%s', g_args['patch'], patch_date))
                 :wikitext(string_format('[[Version %s]]<br>%s', args.patch, patchdate))
                 :done()
                 :done()
             :tag('td')
             :tag('td')
                 :attr('style', 'width: 30%;')
                 :cssText('width: 30%')
                 :wikitext(vargs['after'])
                 :wikitext(args.after_format)


     local cats = {
     local cats = {
Line 91: Line 109:


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


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


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


     local query = {
     local query = {
         g_args.conditions,
         args.conditions,
         '?Is version',
         '?Is version',
         '?Has release date',
         '?Has release date',
Line 116: Line 135:
     local results = {}
     local results = {}
     repeat
     repeat
         local result = util.smw.query(query, g_frame)
         local result = util.smw.query(query, frame)
         query.offset = query.offset + #result
        local length = #result
         query.offset = query.offset + length


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


     local out = {}
     local out = {}
     local last_minor_version, current_list
     local last_minor_version, current_list


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


         local v = util.cast.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
         local minor_version = table_concat({v[1], v[2], v[3]}, '.') -- todo: rework it


         if minor_version ~= last_minor_version then
         if minor_version ~= last_minor_version then
Line 139: Line 160:
             end
             end


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


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


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


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



Revision as of 17:51, 5 January 2017

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


Templates

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

local tostring = tostring
local pairs = pairs

local string_format = string.format
local table_concat = table.concat

local mw_html = mw.html

local p = {}

-----

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

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

    --[[ test args
    = p.version({
        before = '2.4.1a',
        patch = '2.4.1b',
        patchdate = 'October 18, 2016',
        after = '2.4.2',
    })
    --]]

    local vargs = {before = '←', after = '→'}
    local props = {}

    -- querying before and after
    for i in pairs(vargs) do
        if args[i] ~= nil then
            local version = util.cast.version(args[i], {return_type = 'string'})
            props['Has version ' .. i] = version

            local query = {}
            query[#query + 1] = string_format('[[Is version::%s]][[Has release date::+]]', version)
            query[#query + 1] = '?Has release date'

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

            args[i .. '_format'] = string_format('[[Version %s|%s %s]]<br>%s', version, vargs[i], version, result)
        else
            args[i .. '_format'] = nil
        end
    end

    props['Is version'] = args.patch
    props['Has release date'] = args.patchdate

    local version = util.cast.version(args.patch)
    local part_names = {'major', 'minor', 'patch', 'revision'}
    for i = 1, 4 do
        local part = version[i]
        if part then
            props[string_format('Has %s version part', part_names[i])] = part
        end
    end

    util.smw.set(frame, props)

    -- Output
    -- todo: rework it somehow
    local patchdate = frame:callParserFunction('#show: Version ' .. args.patch, {'?Has release date'})

    local tbl = mw_html.create('table')
    tbl
        :addClass('wikitable successionbox')
        :tag('tr')
            :tag('th')
                :attr('colspan', 3)
                :wikitext('[[Version history|Version History]]')
                :done()
            :done()
        :tag('tr')
            :tag('td')
                :cssText('width: 30%')
                :wikitext(args.before_format)
                :done()
            :tag('td')
                :cssText('width: 40%')
                :wikitext(string_format('[[Version %s]]<br>%s', args.patch, patchdate))
                :done()
            :tag('td')
                :cssText('width: 30%')
                :wikitext(args.after_format)

    local cats = {
        'Versions',
    }

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

-----

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

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

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

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

    local query = {
        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, frame)
        local length = #result
        query.offset = query.offset + length

        for i = 1, length do
            results[#results + 1] = result[i]
        end
    until length < 1000

    local out = {}
    local last_minor_version, current_list

    for i = 1, #results do
        local result = results[i]
        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