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 2: Line 2:
local util = require('Module:Util')
local util = require('Module:Util')


local error = error
local tostring = tostring
local tostring = tostring
local pairs = pairs
local pairs = pairs
Line 17: Line 18:
-- Template: Version
-- Template: Version


local version_map = {
local version_map
version_map = {
     version = {
     version = {
         datatype = 'String',
         datatype = 'String',
Line 45: Line 47:
         datatype = 'String',
         datatype = 'String',
         property = 'Has version before',
         property = 'Has version before',
        show = function()
            local version = version_map.before.value
            if version then
                return '← ' .. version
            else
                return ''
            end
        end,
     },
     },
     after = {
     after = {
         datatype = 'String',
         datatype = 'String',
         property = 'Has version after',
         property = 'Has version after',
        show = function()
            local version = version_map.after.value
            if version then
                return version .. ' →'
            else
                return ''
            end
        end,
     },
     },
}
}
Line 66: Line 84:
     --]]
     --]]


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


     -- querying before and after
     if not args.patch or not args.patchdate then
     for i in pairs(vargs) do
        error('Parameters "patch" and "patchdate" are required')
        if args[i] ~= nil then
     end
            local version = util.cast.version(args[i], {return_type = 'string'})
 
            props['Has version ' .. i] = version
    local version = util.cast.version(args.patch)


            local query = {}
    version_map.version.value = args.patch
            query[#query + 1] = string_format('[[Is version::%s]] [[Has release date::+]]', version)
    version_map.release_date.value = args.patchdate
            query[#query + 1] = '?Has release date'


            local result = ''
    local part_names = {'major_part', 'minor_part', 'patch_part', 'revision_part'}
            local results = util.smw.query(query, frame)
    for i = 1, 4 do
            if #results ~= 0 then
        local part = version[i]
                result = results[1]['Has release date']
            end


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


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


     local version = util.cast.version(args.patch)
    -- Check and set 'before' and 'after' args
    local part_names = {'major', 'minor', 'patch', 'revision'}
     local edge_names = {'before', 'after' }
    for i = 1, 4 do
    for i = 1, 2 do
         local part = version[i]
        local v = args[edge_names[i]]
        if not v then
            break
        end
 
        local edge_version = util.cast.version(v, {return_type = 'string'})
 
        local query = {
            string_format('[[Is version::%s]] [[Has release date::+]]', edge_version),
            '?Has release date',
        }
        local results = util.smw.query(query, frame)
         if #results ~= 0 then
            version_map[edge_names[i]].value = edge_version
        end
    end
 


         if part then
    -- Set SMW and Cargo data
             props[string_format('Has %s version part', part_names[i])] = part
    local props_smw = {}
    local props_cargo = {
        _table = 'Versions',
    }
    for i, _ in pairs(version_map) do
         if _.value then
             props_smw[_.property] = _.value
         end
         end
        props_cargo[i] = _.value or ''
     end
     end


     util.smw.set(frame, props)
     util.smw.set(frame, props_smw)
    util.cargo.store(frame, props_cargo)
 


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


     local tbl = mw_html.create('table')
     local tbl = mw_html.create('table')
Line 122: Line 157:
             :tag('td')
             :tag('td')
                 :cssText('width: 30%')
                 :cssText('width: 30%')
                 :wikitext(args.before_format)
                 :wikitext(version_map.before.show())
                 :done()
                 :done()
             :tag('td')
             :tag('td')
                 :cssText('width: 40%')
                 :cssText('width: 40%')
                 :wikitext(string_format('[[Version %s]]<br>%s', args.patch, patchdate))
                 :wikitext(string_format('[[Version %s]]<br>%s', version_map.version.value, release_version))
                 :done()
                 :done()
             :tag('td')
             :tag('td')
                 :cssText('width: 30%')
                 :cssText('width: 30%')
                 :wikitext(args.after_format)
                 :wikitext(version_map.after.show())


     local cats = {
     local cats = {

Revision as of 20:33, 21 January 2017

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


Templates

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

local error = error
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

local version_map
version_map = {
    version = {
        datatype = 'String',
        property = 'Is version',
    },
    release_date = {
        datatype = 'Date',
        property = 'Has release date',
    },
    major_part = {
        datatype = 'Integer',
        property = 'Has major version part',
    },
    minor_part = {
        datatype = 'Integer',
        property = 'Has minor version part',
    },
    patch_part = {
        datatype = 'Integer',
        property = 'Has patch version part',
    },
    revision_part = {
        datatype = 'String',
        property = 'Has revision version part',
    },
    before = {
        datatype = 'String',
        property = 'Has version before',
        show = function()
            local version = version_map.before.value
            if version then
                return '← ' .. version
            else
                return ''
            end
        end,
    },
    after = {
        datatype = 'String',
        property = 'Has version after',
        show = function()
            local version = version_map.after.value
            if version then
                return version .. ' →'
            else
                return ''
            end
        end,
    },
}

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

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


    if not args.patch or not args.patchdate then
        error('Parameters "patch" and "patchdate" are required')
    end

    local version = util.cast.version(args.patch)

    version_map.version.value = args.patch
    version_map.release_date.value = args.patchdate

    local part_names = {'major_part', 'minor_part', 'patch_part', 'revision_part'}
    for i = 1, 4 do
        local part = version[i]

        if part then
            version_map[part_names[i]].value = part
        end
    end


    -- Check and set 'before' and 'after' args
    local edge_names = {'before', 'after' }
    for i = 1, 2 do
        local v = args[edge_names[i]]
        if not v then
            break
        end

        local edge_version = util.cast.version(v, {return_type = 'string'})

        local query = {
            string_format('[[Is version::%s]] [[Has release date::+]]', edge_version),
            '?Has release date',
        }
        local results = util.smw.query(query, frame)
        if #results ~= 0 then
            version_map[edge_names[i]].value = edge_version
        end
    end


    -- Set SMW and Cargo data
    local props_smw = {}
    local props_cargo = {
        _table = 'Versions',
    }
    for i, _ in pairs(version_map) do
        if _.value then
            props_smw[_.property] = _.value
        end
        props_cargo[i] = _.value or ''
    end

    util.smw.set(frame, props_smw)
    util.cargo.store(frame, props_cargo)


    -- Generate output
    -- todo: rework it somehow
    local release_version = frame:callParserFunction('#show: Version ' .. version_map.version.value, {'?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(version_map.before.show())
                :done()
            :tag('td')
                :cssText('width: 40%')
                :wikitext(string_format('[[Version %s]]<br>%s', version_map.version.value, release_version))
                :done()
            :tag('td')
                :cssText('width: 30%')
                :wikitext(version_map.after.show())

    local cats = {
        'Versions',
    }

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

-----

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

    local props = {
        _table = 'Versions',
    }

    for i, v in pairs(version_map) do
        props[i] = v.datatype
    end

    return util.cargo.declare(frame, props)
end

-----

------------------------------------------------------------------------------------------------------
-- Template: Version history list

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

    -- = 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

-----

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

    -- = 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',
        '?Has major version part',
        '?Has minor version part',
        '?Has patch version part',
--        '?Has revision version part',
        sort = 'Has major version part, Has minor version part, Has patch version part, Is version',
        order = 'desc, desc, 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

--    mw.logObject(results)

    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 patch_version = string_format('%s.%s.%s',
            result['Has major version part'], result['Has minor version part'], result['Has patch version part'])

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

            out[#out + 1] = string_format('===Version %s===', patch_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 = patch_version
    end

    return table_concat(out, '\n')
end

-----

return p