Module:Version: Difference between revisions

From Path of Exile Wiki
Jump to navigation Jump to search
>TheFrz
mNo edit summary
No edit summary
 
(25 intermediate revisions by 5 users not shown)
Line 1: Line 1:
local getArgs = require('Module:Arguments').getArgs
-------------------------------------------------------------------------------
local util = require('Module:Util')
--
--                                Module:Version
--
-- This module implements Template:Version, Template:Version history list, and
-- Template:Timeline of items
-------------------------------------------------------------------------------


local error = error
require('Module:No globals')
local tostring = tostring
local m_util = require('Module:Util')
local tonumber = tonumber
local m_cargo = require('Module:Cargo')
local pairs = pairs
local m_item_util -- Lazy load require('Module:Item util')


local string_format = string.format
-- Should we use the sandbox version of our submodules?
local table_concat = table.concat
local use_sandbox = m_util.misc.maybe_sandbox('Version')


local mw_html = mw.html
-- 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:Version/config/sandbox') or mw.loadData('Module:Version/config')


local p = {}
local i18n = cfg.i18n
 
-- ---------------------------------------------------------------------
-- Helper functions
-- ---------------------------------------------------------------------
 
local h = {}
 
function h.date(value, args)
    --[[
    Format dates in correct and useable form.
   
    Parameters
    ----------
    value : String, required
        Date
    args : Table
        Table with extra formatting args.
   
    ]]
   
    local args = args or {}
   
    -- List of allowed extra arguments:
    local arg_list = {
        format = {
            default = 'F j, Y H:i:s',
            cargo  = 'Y-m-d H:i:s',
            no_time = 'F j, Y',
        },
    }


-----
    local lang = mw.getContentLanguage()
    local date_format = arg_list['format']['default']
    local timestamp = lang:formatDate(date_format, value)
   
    -- If the time is 00:00:00 then assume that the time isn't defined:
    if lang:formatDate('H:i:s', timestamp) == '00:00:00' then
        date_format = arg_list['format']['no_time']
    end
   
    -- Add the extra arguments:
    for i,v in pairs(args) do
        if i == 'format' then
            date_format = arg_list[i][v]           
        end
    end
   
    -- Return the final timestamp format:
    local out
    if value ~= nil then
        out = lang:formatDate(date_format, timestamp)
    end
   
    return out
end


------------------------------------------------------------------------------------------------------
function h.validate_version(value)
-- Template: Version
    if value == nil then
        return value
    end
    return m_util.cast.version(value, {return_type='string'})
end


local version_map = {
function h.show_date(args)
     version = {
     return function(targs)
         datatype = 'String',
         local version = targs[args.key]
        property = 'Is version',
         local date = targs[string.format('%s_date', args.key)]
        validate = tostring,
         if version and date then
    },
            date = h.date(date) or ''
    release_date = {
            if args.key == 'before' then
         datatype = 'String',
                return string.format(i18n.show_date.before, version, version, date)
        property = 'Has release date',
             elseif args.key == 'after' then
        validate = tostring,
                 return string.format(i18n.show_date.after, version, version, date)
    },
    major_part = {
        datatype = 'Integer',
         property = 'Has major version part',
        validate = tonumber,
    },
    minor_part = {
        datatype = 'Integer',
        property = 'Has minor version part',
        validate = tonumber,
    },
    patch_part = {
        datatype = 'Integer',
        property = 'Has patch version part',
        validate = tonumber,
    },
    revision_part = {
        datatype = 'String',
        property = 'Has revision version part',
        validate = tostring,
    },
    before = {
        datatype = 'String',
        property = 'Has version before',
        validate = tostring,
        show = function(this)
             local version = this.value
            local date = this.release_date
            if version and date then
                 return string_format('← [[Version %s|%s]]<br>%s', version, version, date)
            else
                return ''
             end
             end
         end,
        else
     },
            return ''
     after = {
         end
         datatype = 'String',
     end
         property = 'Has version after',
end
         validate = tostring,
 
         show = function(this)
-- ----------------------------------------------------------------------------
             local version = this.value
-- Cargo tables
             local date = this.release_date
-- ----------------------------------------------------------------------------
             if version and date then
 
                return string_format('[[Version %s|%s]] →<br>%s', version, version, date)
local tables = {}
             else
 
                return ''
tables.versions ={
             end
    table = 'versions',
         end,
     fields = {
         patch = {
            field = 'version',
            type = 'String',
            func = h.validate_version,
         },
        patchdate = {
            field = 'release_date',
            type = 'Datetime',
            func = tostring,
        },
        major_part = {
            field = 'major_part',
            type = 'Integer',
        },
        minor_part = {
            field = 'minor_part',
            type = 'Integer',
         },
        patch_part = {
            field = 'patch_part',
            type = 'Integer',
        },
         revision_part = {
             field = 'revision_part',
             type = 'String',
        },
        before = {
             field = 'previous',
            type = 'String',
            func = h.validate_version,
            show = h.show_date{key='before'},
        },
        after = {
             field = 'after',
            type = 'String',
             func = h.validate_version,
            show = h.show_date{key='after'},
         },
     },
     },
}
}


local temp_map_for_cargo = {'version', 'release_date', 'major_part'}
-- ----------------------------------------------------------------------------
 
-- Main functions
 
-- ----------------------------------------------------------------------------
p.version = function(frame)
    local args = getArgs(frame, {parentFirst = true})
    local frame = util.misc.get_frame(frame)


local function _version(args)
     --[[
     --[[
     = p.version({
     Creates a version succession box and stores the data in a cargo table
         before = '2.4.1a',
   
    Example:
    p.version{
         before = '2.4.1',
         patch = '2.4.1b',
         patch = '2.4.1b',
         patchdate = 'October 18, 2016',
         patchdate = 'October 18, 2016',
         after = '2.4.2',
         after = '2.4.2',
     })
     }
     --]]
     --]]


 
    -- Unpack args and validate
    for k, arg_def in pairs(tables.versions.fields) do
        if arg_def.func ~= nil then
            args[k] = arg_def.func(args[k])
        end
    end
     if not args.patch or not args.patchdate then
     if not args.patch or not args.patchdate then
         error('Arguments "patch" and "patchdate" are required')
         error(i18n.version.required_args)
     end
     end
 
   
     local version = util.cast.version(args.patch)
     local version_parts = m_util.cast.version(args.patch, {return_type='table'})
 
     args.major_part = tonumber(version_parts[1])
     local v_struct = util.Struct(version_map)
     args.minor_part = tonumber(version_parts[2])
 
     args.patch_part = tonumber(version_parts[3])
     v_struct:set('version', args.patch)
    if version_parts[4] then
     v_struct:set('release_date', args.patchdate)
        args.revision_part = version_parts[4]
 
    local part_names = {'major_part', 'minor_part', 'patch_part', 'revision_part'}
    for i = 1, #part_names do
        local part = version[i]
 
        if part then
            v_struct:set(part_names[i], part)
        end
     end
     end


 
     -- Validate 'before' and 'after' versions and query their release dates
     -- Check and set 'before' and 'after' args
     for _, key in ipairs({'before', 'after'}) do
     local edge_names = {'before', 'after'}
         local version_number = args[key]
    for i = 1, #edge_names do
         if version_number then
         local v = args[edge_names[i]]
             local results = m_cargo.query(
         if v then
                {'versions'},
             local edge_version = util.cast.version(v, {return_type = 'string'})
                {'versions.release_date=date'},
 
                {
            local query = {
                    where = string.format('versions.version="%s"', version_number)
                string_format('[[Is version::%s]] [[Has release date::+]]', edge_version),
                 }
                 '?Has release date',
             )
            }
             local results = util.smw.query(query, frame)
             if #results == 1 then
             if #results == 1 then
                 v_struct:set(edge_names[i], edge_version)
                 args[string.format('%s_date', key)] = results[1].date
                v_struct:set_prop(edge_names[i], 'release_date', results[1]['Has release date'])
             elseif #results > 1 then
             elseif #results > 1 then
                 error('There are versions with the same name')
                 error(i18n.version.multiple_versions)
             end
             end
         end
         end
     end
     end
 
   
 
     -- Store cargo data
     -- Set SMW and Cargo data
     local data = {
     local props_smw = {}
         _table = tables.versions.table,
    local props_cargo = {
         _table = 'Versions',
     }
     }
     for i, _ in pairs(v_struct.map) do
     for k, v in pairs(tables.versions.fields) do
         local value = v_struct:get(i)
         if args[k] ~= nil then
        if value then
             data[v.field] = args[k]
             props_smw[v_struct:get_prop(i, 'property')] = value
      end
        end
        --props_cargo[i] = value or ''
     end
     end
    m_cargo.store(data)


     util.smw.set(frame, props_smw)
     mw.getCurrentFrame():expandTemplate{
 
         title = 'Template:Version/cargo/versions/attach'
 
     }
    for i = 1, #temp_map_for_cargo do
         local v = temp_map_for_cargo[i]
        props_cargo[v] = v_struct:get(v) or ''
     end
 
    -- mw.logObject(props_cargo)
    util.cargo.store(frame, props_cargo)
 


     -- Generate output
     -- Generate output
    -- todo: rework it somehow
     local release_date = h.date(args.patchdate)
     local release_date = frame:callParserFunction('#show: Version ' .. v_struct:get('version'), {'?Has release date'})
     local tbl = mw.html.create('table')
 
     local tbl = mw_html.create('table')
     tbl
     tbl
         :addClass('wikitable successionbox')
         :addClass('wikitable successionbox')
Line 175: Line 227:
             :tag('th')
             :tag('th')
                 :attr('colspan', 3)
                 :attr('colspan', 3)
                 :wikitext('[[Version history|Version History]]')
                 :wikitext(i18n.version.header)
                 :done()
                 :done()
             :done()
             :done()
Line 181: Line 233:
             :tag('td')
             :tag('td')
                 :cssText('width: 30%')
                 :cssText('width: 30%')
                 :wikitext(v_struct:show('before'))
                 :wikitext(tables.versions.fields.before.show(args))
                 :done()
                 :done()
             :tag('td')
             :tag('td')
                 :cssText('width: 40%')
                 :cssText('width: 40%')
                 :wikitext(string_format('<b>%s</b><br>%s', v_struct:get('version'), release_date))
                 :wikitext(string.format('<b>%s</b><br>%s', args.patch, release_date))
                 :done()
                 :done()
             :tag('td')
             :tag('td')
                 :cssText('width: 30%')
                 :cssText('width: 30%')
                 :wikitext(v_struct:show('after'))
                 :wikitext(tables.versions.fields.after.show(args))


    local cats = {
     return tostring(tbl) .. m_util.misc.add_category({i18n.categories.versions})
        'Versions',
    }
 
     return tostring(tbl) .. util.misc.add_category(cats)
end
end


-----
local function _timeline(args)
    --[[
    Creates a version timeline and optionally lists items added to the game for each version
   
    Examples:
    p.timeline{
        where = 'versions.major_part = 0 AND versions.minor_part < 9',
    }
   
    p.timeline{
        list_items = true
        where = 'items.class_id = "DivinationCard"',
    }
    --]]


p.version_declare = function(frame)
     local tables = {'versions'}
     -- local args = getArgs(frame, {parentFirst = true})
     local fields = {
     local frame = util.misc.get_frame(frame)
        'versions.version',
 
         'versions.release_date',
    local props = {
         _table = 'Versions',
     }
     }
--    for i, _ in pairs(version_map) do
--        props[i] = _.datatype
--    end
    for i = 1, #temp_map_for_cargo do
        local v = temp_map_for_cargo[i]
        local _ = version_map[v]
        props[v] = _.datatype
    end
    --mw.logObject(props)
    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 = {
     local query = {
         args.conditions,
         orderBy = 'versions.major_part DESC, versions.minor_part DESC, versions.patch_part DESC, versions.revision_part DESC'
        '?Is version',
        '?Has release date',
        sort = 'Has release date, Is version',
        order = 'desc, desc',
        link = 'none',
        offset = 0,
     }
     }
    args.list_items = m_util.cast.boolean(args.list_items)
    if args.list_items then
        m_item_util = m_item_util or require('Module:Item util')
        table.insert(tables, 'items')
        fields = m_util.table.merge(fields, {'items._pageName', 'items.name'})
        query.join = 'versions.version=items.release_version'
        query.where = 'items.release_version IS NOT NULL'
        query.orderBy = query.orderBy .. ', items.name ASC'


    local results = {}
        -- Namespace condition
    repeat
        -- This is mainly to prevent items from user pages or other testing pages
        local result = util.smw.query(query, frame)
        -- from being returned in the query results.
        local length = #result
        if args.namespaces ~= 'any' then
        query.offset = query.offset + length
            local namespaces = m_util.cast.table(args.namespaces, {callback=m_util.cast.number})
 
            if #namespaces > 0 then
        for i = 1, length do
                namespaces = table.concat(namespaces, ',')
             results[#results + 1] = result[i]
            else
                namespaces = m_item_util.get_item_namespaces{format = 'list'}
            end
             query.where = string.format('%s AND items._pageNamespace IN (%s)', query.where, namespaces)
         end
         end
     until length < 1000
     end
 
    if args.where then
        -- m_util.table.merge rebuilds the table, which removes empty values
        -- TODO: Use a better function than m_util.table.merge
        query.where = table.concat(m_util.table.merge({query.where, args.where}), ' AND ')
    end
    local results = m_cargo.query(tables, fields, query)
   
     local out = {}
     local out = {}
     local last_minor_version, current_list
    local last_main_version
 
     local last_minor_version
     for i = 1, #results do
    local current_version
         local result = results[i]
    local list
        local date = result['Has release date']
   
        local version = result['Is version']
    -- Loop through all the results from the query
 
     for i, row in ipairs(results) do
         local v = util.cast.version(result['Is version'])
         local release_version = row['versions.version']
         local minor_version = table_concat({v[1], v[2], v[3]}, '.') -- todo: rework it
         local v = m_util.cast.version(release_version)
 
         local version_h2 = table.concat({v[1], v[2]}, '.')
         if minor_version ~= last_minor_version then
         if release_version ~= last_minor_version then
             if current_list ~= nil then
             if version_h2 ~= last_main_version then
                 out[#out + 1] = tostring(current_list)
                if current_version ~= nil then
                    out[#out + 1] = tostring(current_version)
                end
               
                 out[#out+1] = string.format(
                    '===%s %s===',
                    i18n.timeline.version,
                    table.concat({v[1], v[2], 0}, '.')
                )
                current_version = mw.html.create('ul')
             end
             end
            current_version
                :tag('li')
                    :wikitext(string.format(
                        '%s - [[%s %s]]',
                        h.date(row['versions.release_date']),
                        i18n.timeline.version,
                        release_version,
                        row['versions.version'])
                    )
            list = current_version:tag('ol')
        end


             out[#out + 1] = string_format('===Version %s===', minor_version)
        -- List items
            current_list = mw_html.create('ul')
        if args.list_items then
             list
                :tag('li')
                    :wikitext(m_util.html.wikilink(row['items._pageName'], row['items.name']))
         end
         end
 
          
         current_list:tag('li'):wikitext(string_format('%s &ndash; [[Version %s]]', date, version))
         -- Save the last list
 
         if i == #results and current_version ~= nil then  
         -- save the last list
             out[#out + 1] = tostring(current_version)
         if i == #results and current_list ~= nil then
             out[#out + 1] = tostring(current_list)
         end
         end
 
       
         last_minor_version = minor_version
        last_main_version = version_h2
         last_minor_version = release_version
     end
     end


     return table_concat(out, '\n')
     return table.concat(out, '\n') .. m_util.misc.add_category({i18n.categories.timelines})
end
end


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


p.version_history_list_2 = function(frame)
local p = {}
    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 = {}
p.table_versions = m_cargo.declare_factory{data=tables.versions}
    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]
-- Template:Version
        end
--
    until length < 1000
p.version = m_util.misc.invoker_factory(_version, {
 
    wrappers = cfg.wrappers.version,
--   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


-----
--
-- Template:Version history list, Template:Timeline of items
--
p.timeline = m_util.misc.invoker_factory(_timeline)


return p
return p

Latest revision as of 15:13, 9 April 2024

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


Templates

-------------------------------------------------------------------------------
-- 
--                                Module:Version
-- 
-- This module implements Template:Version, Template:Version history list, and 
-- Template:Timeline of items
-------------------------------------------------------------------------------

require('Module:No globals')
local m_util = require('Module:Util')
local m_cargo = require('Module:Cargo')
local m_item_util -- Lazy load require('Module:Item util')

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

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

local i18n = cfg.i18n

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

local h = {}

function h.date(value, args)
    --[[
    Format dates in correct and useable form.
    
    Parameters
    ----------
    value : String, required
        Date
    args : Table
        Table with extra formatting args.
    
    ]]
    
    local args = args or {}
    
    -- List of allowed extra arguments:
    local arg_list = {
        format = {
            default = 'F j, Y H:i:s',
            cargo   = 'Y-m-d H:i:s',
            no_time = 'F j, Y',
        },
    }

    local lang = mw.getContentLanguage()
    local date_format = arg_list['format']['default']
    local timestamp = lang:formatDate(date_format, value)
    
    -- If the time is 00:00:00 then assume that the time isn't defined:
    if lang:formatDate('H:i:s', timestamp) == '00:00:00' then 
        date_format = arg_list['format']['no_time']
    end
    
    -- Add the extra arguments:
    for i,v in pairs(args) do
        if i == 'format' then
            date_format = arg_list[i][v]            
        end
    end
    
    -- Return the final timestamp format:
    local out
    if value ~= nil then
        out = lang:formatDate(date_format, timestamp)
    end
    
    return out
end

function h.validate_version(value)
    if value == nil then
        return value
    end
    return m_util.cast.version(value, {return_type='string'})
end

function h.show_date(args)
    return function(targs)
        local version = targs[args.key]
        local date = targs[string.format('%s_date', args.key)]
        if version and date then
            date = h.date(date) or ''
            if args.key == 'before' then
                return string.format(i18n.show_date.before, version, version, date)
            elseif args.key == 'after' then
                return string.format(i18n.show_date.after, version, version, date)
            end
        else
            return ''
        end
    end
end

-- ----------------------------------------------------------------------------
-- Cargo tables
-- ----------------------------------------------------------------------------

local tables = {}

tables.versions ={
    table = 'versions',
    fields = {
        patch = {
            field = 'version',
            type = 'String',
            func = h.validate_version,
        },
        patchdate = {
            field = 'release_date',
            type = 'Datetime',
            func = tostring,
        },
        major_part = {
            field = 'major_part',
            type = 'Integer',
        },
        minor_part = {
            field = 'minor_part',
            type = 'Integer',
        },
        patch_part = {
            field = 'patch_part',
            type = 'Integer',
        },
        revision_part = {
            field = 'revision_part',
            type = 'String',
        },
        before = {
            field = 'previous',
            type = 'String',
            func = h.validate_version,
            show = h.show_date{key='before'},
        },
        after = {
            field = 'after',
            type = 'String',
            func = h.validate_version,
            show = h.show_date{key='after'},
        },
    },
}

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

local function _version(args)
    --[[
    Creates a version succession box and stores the data in a cargo table
    
    Example:
    p.version{
        before = '2.4.1',
        patch = '2.4.1b',
        patchdate = 'October 18, 2016',
        after = '2.4.2',
    }
    --]]

    -- Unpack args and validate
    for k, arg_def in pairs(tables.versions.fields) do
        if arg_def.func ~= nil then
            args[k] = arg_def.func(args[k])
        end
    end
    if not args.patch or not args.patchdate then
        error(i18n.version.required_args)
    end
    
    local version_parts = m_util.cast.version(args.patch, {return_type='table'})
    args.major_part = tonumber(version_parts[1])
    args.minor_part = tonumber(version_parts[2])
    args.patch_part = tonumber(version_parts[3])
    if version_parts[4] then
        args.revision_part = version_parts[4]
    end

    -- Validate 'before' and 'after' versions and query their release dates
    for _, key in ipairs({'before', 'after'}) do
        local version_number = args[key]
        if version_number then
            local results = m_cargo.query(
                {'versions'},
                {'versions.release_date=date'},
                {
                    where = string.format('versions.version="%s"', version_number)
                }
            )
            if #results == 1 then
                args[string.format('%s_date', key)] = results[1].date
            elseif #results > 1 then
                error(i18n.version.multiple_versions)
            end
        end
    end
    
    -- Store cargo data
    local data = {
        _table = tables.versions.table,
    }
    for k, v in pairs(tables.versions.fields) do
        if args[k] ~= nil then
            data[v.field] = args[k]
       end
    end
    m_cargo.store(data)

    mw.getCurrentFrame():expandTemplate{
        title = 'Template:Version/cargo/versions/attach'
    }

    -- Generate output
    local release_date = h.date(args.patchdate)
    local tbl = mw.html.create('table')
    tbl
        :addClass('wikitable successionbox')
        :tag('tr')
            :tag('th')
                :attr('colspan', 3)
                :wikitext(i18n.version.header)
                :done()
            :done()
        :tag('tr')
            :tag('td')
                :cssText('width: 30%')
                :wikitext(tables.versions.fields.before.show(args))
                :done()
            :tag('td')
                :cssText('width: 40%')
                :wikitext(string.format('<b>%s</b><br>%s', args.patch, release_date))
                :done()
            :tag('td')
                :cssText('width: 30%')
                :wikitext(tables.versions.fields.after.show(args))

    return tostring(tbl) .. m_util.misc.add_category({i18n.categories.versions})
end

local function _timeline(args)
    --[[ 
    Creates a version timeline and optionally lists items added to the game for each version
    
    Examples:
    p.timeline{
        where = 'versions.major_part = 0 AND versions.minor_part < 9',
    }
    
    p.timeline{
        list_items = true
        where = 'items.class_id = "DivinationCard"',
    }
    --]]

    local tables = {'versions'}
    local fields = {
        'versions.version',
        'versions.release_date',
    }
    local query = {
        orderBy = 'versions.major_part DESC, versions.minor_part DESC, versions.patch_part DESC, versions.revision_part DESC'
    }
    args.list_items = m_util.cast.boolean(args.list_items)
    if args.list_items then
        m_item_util = m_item_util or require('Module:Item util')
        table.insert(tables, 'items')
        fields = m_util.table.merge(fields, {'items._pageName', 'items.name'})
        query.join = 'versions.version=items.release_version'
        query.where = 'items.release_version IS NOT NULL'
        query.orderBy = query.orderBy .. ', items.name ASC'

        -- Namespace condition
        -- This is mainly to prevent items from user pages or other testing pages 
        -- from being returned in the query results.
        if args.namespaces ~= 'any' then
            local namespaces = m_util.cast.table(args.namespaces, {callback=m_util.cast.number})
            if #namespaces > 0 then
                namespaces = table.concat(namespaces, ',')
            else
                namespaces = m_item_util.get_item_namespaces{format = 'list'}
            end
            query.where = string.format('%s AND items._pageNamespace IN (%s)', query.where, namespaces)
        end
    end
    if args.where then
        -- m_util.table.merge rebuilds the table, which removes empty values
        -- TODO: Use a better function than m_util.table.merge
        query.where = table.concat(m_util.table.merge({query.where, args.where}), ' AND ')
    end
    local results = m_cargo.query(tables, fields, query)
    
    local out = {}
    local last_main_version 
    local last_minor_version
    local current_version
    local list
    
    -- Loop through all the results from the query
    for i, row in ipairs(results) do
        local release_version = row['versions.version']
        local v = m_util.cast.version(release_version)
        local version_h2 = table.concat({v[1], v[2]}, '.')
        if release_version ~= last_minor_version then
            if version_h2 ~= last_main_version then 
                if current_version ~= nil then
                    out[#out + 1] = tostring(current_version)
                end
                
                out[#out+1] = string.format(
                    '===%s %s===', 
                    i18n.timeline.version, 
                    table.concat({v[1], v[2], 0}, '.')
                ) 
                current_version = mw.html.create('ul')
            end
            current_version
                :tag('li')
                    :wikitext(string.format(
                        '%s - [[%s %s]]',
                        h.date(row['versions.release_date']),
                        i18n.timeline.version,
                        release_version,
                        row['versions.version'])
                    )
            list = current_version:tag('ol')
        end

        -- List items
        if args.list_items then
            list
                :tag('li')
                    :wikitext(m_util.html.wikilink(row['items._pageName'], row['items.name']))
        end
        
        -- Save the last list
        if i == #results and current_version ~= nil then 
            out[#out + 1] = tostring(current_version)
        end
        
        last_main_version = version_h2
        last_minor_version = release_version
    end

    return table.concat(out, '\n') .. m_util.misc.add_category({i18n.categories.timelines})
end

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

local p = {}

p.table_versions = m_cargo.declare_factory{data=tables.versions}

--
-- Template:Version
--
p.version = m_util.misc.invoker_factory(_version, {
    wrappers = cfg.wrappers.version,
})

--
-- Template:Version history list, Template:Timeline of items
--
p.timeline = m_util.misc.invoker_factory(_timeline)

return p