Module:Version: Difference between revisions

From Path of Exile 2 Wiki
Jump to navigation Jump to search
(Removed unused code)
m (48 revisions imported)
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
local getArgs = require('Module:Arguments').getArgs
-------------------------------------------------------------------------------
--
--                                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_util = require('Module:Util')
local m_cargo = require('Module:Cargo')
local m_cargo = require('Module:Cargo')
local f_item_link = require('Module:Item link').item_link
local m_item_util -- Lazy load require('Module:Item util')
local cargo = mw.ext.cargo


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 mw_language = mw.getLanguage('en')
local i18n = cfg.i18n
 
local p = {}
 
local i18n = {
    categories = {
        versions = 'Versions',
        timelines = 'Timelines',
    },
   
    show_date = {
        before = '← [[Version %s|%s]]<br>%s',
        after = '[[Version %s|%s]] →<br>%s',
    },
    version = {
        required_args = 'Arguments "patch" and "patchdate" are required',
        multiple_versions = 'There are multiple versions with the same name',
        header = '[[Version history|Version History]]',
    },
    timeline = {
        version = 'Version',
    },
}


-- ---------------------------------------------------------------------
-- ---------------------------------------------------------------------
Line 39: Line 26:


local h = {}
local h = {}
function h.cargo_query(tpl_args)
    --[[
    Returns a Cargo query of all the results.
   
    tpl_args should include these keys:
    tpl_args.tables
    tpl_args.fields
    tpl_args.q_*
   
    ]]
   
    local tables = m_util.string.split(tpl_args.tables, ', ')
    local fields = m_util.string.split(tpl_args.fields, ', ')
   
    -- Parse query arguments
    local query = {
    }
    for key, value in pairs(tpl_args) do
        if string.sub(key, 0, 2) == 'q_' then
            query[string.sub(key, 3)] = value
        end
    end
   
    -- Query cargo rows:
    local results = m_cargo.query(tables, fields, query)
   
    return results
end


function h.date(value, args)
function h.date(value, args)
Line 92: Line 50:
         },
         },
     }
     }
      
 
     local lang = mw.getContentLanguage()
     local date_format = arg_list['format']['default']
     local date_format = arg_list['format']['default']
     local timestamp = mw_language:formatDate(date_format, value)
     local timestamp = lang:formatDate(date_format, value)
      
      
     -- If the time is 00:00:00 then assume that the time isn't defined:
     -- If the time is 00:00:00 then assume that the time isn't defined:
     if mw_language:formatDate('H:i:s', timestamp) == '00:00:00' then  
     if lang:formatDate('H:i:s', timestamp) == '00:00:00' then  
         date_format = arg_list['format']['no_time']
         date_format = arg_list['format']['no_time']
     end
     end
Line 111: Line 70:
     local out
     local out
     if value ~= nil then
     if value ~= nil then
         out = mw_language:formatDate(date_format, timestamp)
         out = lang:formatDate(date_format, timestamp)
     end
     end
      
      
Line 117: Line 76:
end
end


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


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


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


local version_map = {
local tables = {}
 
tables.versions ={
     table = 'versions',
     table = 'versions',
     fields = {
     fields = {
Line 155: Line 112:
             field = 'version',
             field = 'version',
             type = 'String',
             type = 'String',
             validate = validate_version,
             func = h.validate_version,
         },
         },
         patchdate = {
         patchdate = {
             field = 'release_date',
             field = 'release_date',
             type = 'Datetime',
             type = 'Datetime',
             validate = tostring,
             func = tostring,
         },
         },
         major_part = {
         major_part = {
Line 181: Line 138:
             field = 'previous',
             field = 'previous',
             type = 'String',
             type = 'String',
             validate = validate_version,
             func = h.validate_version,
             show = show_date{key='before'},
             show = h.show_date{key='before'},
         },
         },
         after = {
         after = {
             field = 'after',
             field = 'after',
             type = 'String',
             type = 'String',
             validate = validate_version,
             func = h.validate_version,
             show = show_date{key='after'},
             show = h.show_date{key='after'},
         },
         },
     },
     },
}
}


p.table_versions = m_cargo.declare_factory{data=version_map}
-- ----------------------------------------------------------------------------
-- Main functions
-- ----------------------------------------------------------------------------


p.version = function(frame)
local function _version(args)
     --[[
     --[[
     This function creates a infobox and stores the data in a cargo table.
     Creates a version succession box and stores the data in a cargo table
      
      
     Examples:
     Example:
     = p.version{
     p.version{
         before = '2.4.1',
         before = '2.4.1',
         patch = '2.4.1b',
         patch = '2.4.1b',
Line 208: Line 167:
     --]]
     --]]


     local tpl_args = getArgs(frame, {parentFirst = true})
     -- Unpack args and validate
    local frame = m_util.misc.get_frame(frame)
     for k, arg_def in pairs(tables.versions.fields) do
 
         if arg_def.func ~= nil then
     for k, data in pairs(version_map.fields) do
             args[k] = arg_def.func(args[k])
         if data.validate ~= nil then
             tpl_args[k] = data.validate(tpl_args[k])
         end
         end
     end
     end
   
     if not args.patch or not args.patchdate then
    -- Workaround for patchdate returning the string 'nil' when it's nil,
    -- not sure if that's intentional:
     if not tpl_args.patch or not tpl_args.patchdate or tpl_args.patchdate == 'nil' then
         error(i18n.version.required_args)
         error(i18n.version.required_args)
     end
     end
      
      
     local version_parts = m_util.cast.version(tpl_args.patch, {return_type='table'})
     local version_parts = m_util.cast.version(args.patch, {return_type='table'})
     tpl_args.major_part = tonumber(version_parts[1])
     args.major_part = tonumber(version_parts[1])
     tpl_args.minor_part = tonumber(version_parts[2])
     args.minor_part = tonumber(version_parts[2])
     tpl_args.patch_part = tonumber(version_parts[3])
     args.patch_part = tonumber(version_parts[3])
     if version_parts[4] then
     if version_parts[4] then
         tpl_args.revision_part = version_parts[4]
         args.revision_part = version_parts[4]
     end
     end


     -- Check and set 'before' and 'after' tpl_args
     -- Validate 'before' and 'after' versions and query their release dates
     local edge_names = {'before', 'after'}
     for _, key in ipairs({'before', 'after'}) do
    for _, key in ipairs(edge_names) do
         local version_number = args[key]
         local v = tpl_args[key]
         if version_number then
         if v then
             local results = m_cargo.query(
             local results = cargo.query(
                 {'versions'},
                 'versions',  
                 {'versions.release_date=date'},
                 'versions.release_date',  
                 {
                 {
                     where=string.format('version="%s"', v),
                     where = string.format('versions.version="%s"', version_number)
                    -- Cargo bug work around
                    groupBy='versions._pageID',
                 }
                 }
             )
             )
             if #results == 1 then
             if #results == 1 then
                 tpl_args[string.format('%s_date', key)] = results[1]['versions.release_date']
                 args[string.format('%s_date', key)] = results[1].date
             elseif #results > 1 then
             elseif #results > 1 then
                 error(i18n.version.multiple_versions)
                 error(i18n.version.multiple_versions)
Line 252: Line 203:
         end
         end
     end
     end
      
      
     -- Set Cargo data
     -- Store cargo data
     local _properties = {
     local data = {
         _table = version_map.table,
         _table = tables.versions.table,
     }
     }
     for key, data in pairs(version_map.fields) do
     for k, v in pairs(tables.versions.fields) do
         if tpl_args[key] ~= nil then
         if args[k] ~= nil then
             _properties[data.field] = tpl_args[key]
             data[v.field] = args[k]
       end
       end
     end
     end
   
     m_cargo.store(data)
     m_cargo.store(_properties)


     frame:expandTemplate{
     mw.getCurrentFrame():expandTemplate{
         title = string.format('Template:Version/cargo/attach/%s', 'versions'),
         title = 'Template:Version/cargo/versions/attach'
        args = {}
     }
     }


     -- Generate output
     -- Generate output
     local release_date = h.date(tpl_args.patchdate)
     local release_date = h.date(args.patchdate)
     local tbl = mw_html.create('table')
     local tbl = mw.html.create('table')
     tbl
     tbl
         :addClass('wikitable successionbox')
         :addClass('wikitable successionbox')
Line 285: Line 233:
             :tag('td')
             :tag('td')
                 :cssText('width: 30%')
                 :cssText('width: 30%')
                 :wikitext(version_map.fields.before.show(tpl_args, frame))
                 :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', tpl_args.patch, 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(version_map.fields.after.show(tpl_args, frame))
                 :wikitext(tables.versions.fields.after.show(args))


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


-- ---------------------------------------------------------------------
local function _timeline(args)
-- Timeline
-- ---------------------------------------------------------------------
 
 
function p.timeline(frame)  
     --[[  
     --[[  
     Add a timeline when versions or items were added to the game.
     Creates a version timeline and optionally lists items added to the game for each version
      
      
     Examples:
     Examples:
     = p.timeline{
     p.timeline{
         tables = 'versions',
         where = 'versions.major_part = 0 AND versions.minor_part < 9',
        fields = 'versions.version, versions.release_date',
        q_where = 'versions.version <> ""',
        q_orderBy = 'versions.version DESC, versions.release_date ASC'
     }
     }
      
      
     = p.timeline{
     p.timeline{
         tables = 'versions, items',       
         list_items = true
        fields = 'versions.version, versions.release_date, versions._pageName, items.class, items._pageName, items.name, items.release_version, items.inventory_icon, items.html',
         where = 'items.class_id = "DivinationCard"',
        q_join = 'versions.version=items.release_version',
         q_where = 'versions.version IS NOT NULL AND items.release_version IS NOT NULL AND items.rarity = "Unique"',
        q_orderBy = 'versions.version DESC, versions.release_date ASC, items.name ASC',
        q_groupBy = 'versions._pageID, items.name',
        q_limit = 5000,
     }
     }
      
     --]]
    ]]
 
      
     local tables = {'versions'}
      
     local fields = {
     -- Get args:
        'versions.version',
     local tpl_args = getArgs(frame, {parentFirst = true})
        'versions.release_date',
     local frame = m_util.misc.get_frame(frame)
     }
     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'


     -- Query results:
        -- Namespace condition
     local results = h.cargo_query(tpl_args)
        -- 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)
      
      
    -- Preallocate:
     local out = {}
     local out = {}
     local last_main_version  
     local last_main_version  
     local last_minor_version
     local last_minor_version
     local current_version
     local current_version
     local result_list
     local list
      
      
     -- Loop through all the results from the query:
     -- Loop through all the results from the query
     for i, result in ipairs(results) do  
     for i, row in ipairs(results) do
         local release_version = result['versions.version']
         local release_version = row['versions.version']
       
         local v = m_util.cast.version(release_version)
         local v = m_util.cast.version(release_version)
         local version_h2 = table.concat({v[1], v[2]}, '.')
         local version_h2 = table.concat({v[1], v[2]}, '.')
       
         if release_version ~= last_minor_version then
         if release_version ~= last_minor_version then
           
             if version_h2 ~= last_main_version then  
             if version_h2 ~= last_main_version then  
                 if current_version ~= nil then
                 if current_version ~= nil then
Line 367: Line 322:
                 current_version = mw.html.create('ul')
                 current_version = mw.html.create('ul')
             end
             end
       
             current_version
             current_version
                 :tag('li')
                 :tag('li')
                     :wikitext(string.format(
                     :wikitext(string.format(
                         '%s - [[%s %s]]',  
                         '%s - [[%s %s]]',
                         h.date(result['versions.release_date']),  
                         h.date(row['versions.release_date']),
                         i18n.timeline.version,  
                         i18n.timeline.version,
                         release_version,  
                         release_version,
                         result['versions.version'])
                         row['versions.version'])
                     )
                     )
             result_list = current_version:tag('ol')
             list = current_version:tag('ol')
         end  
         end
       
 
             
         -- List items
         -- If the result has an item class, then add another list with
         if args.list_items then
        -- the results.
             list
         if result['items.class'] ~= nil then  
                :tag('li')
             -- result_list:tag('li'):wikitext(string.format('%s',
                    :wikitext(m_util.html.wikilink(row['items._pageName'], row['items.name']))
                -- f_item_link{
                    -- page=result['items._pageName'],  
                    -- name=result['items.name'],
                    -- inventory_icon=result['items.inventory_icon'] or '',
                    -- html=result['items.html'] or '',
                    -- skip_query=true
                    -- }
                -- )
            -- )
            result_list:tag('li'):wikitext(string.format('[[%s]]',
                result['items._pageName']
                )
            )
         end
         end
          
          
         -- Save the last list:
         -- Save the last list
         if (i == #results) and (current_version ~= nil) then  
         if i == #results and current_version ~= nil then  
             out[#out + 1] = tostring(current_version)
             out[#out + 1] = tostring(current_version)
         end
         end
Line 408: Line 349:
         last_minor_version = release_version
         last_minor_version = release_version
     end
     end
   
 
    -- Add categories:
     return table.concat(out, '\n') .. m_util.misc.add_category({i18n.categories.timelines})
    local cats = {
        i18n.categories.timelines,
    }
   
     return table.concat(out, '\n') .. m_util.misc.add_category(cats)
end
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
return p

Latest revision as of 22:01, 24 September 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