Jump to content

Module:Multiple releases/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Gonnym (talk | contribs) at 10:29, 23 December 2021. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}

local wikidata = require('Module:Wd')

local properties = {
	platform = "P400",
	publication_date = "P577",
	software_version_identifier = "P348",
	version_type = "P548",
}

local version_types = {
	pre_release = "Q51930650",
	stable = "Q2804309",
}

local platforms = {
	android = {id = "Q94", label = "[[Android (operating system)|Android]]"},
	ios = {id = "Q48493", label = "[[iOS]]"},
	linux = {id = "Q388", label = "[[Linux]]"},
	macos = {id = "Q14116", label = "[[macOS]]"},
	windows = {id = "Q1406", label = "[[Microsoft Windows|Windows]]"},
	web = {id = "Q6368", label = "[[Web application|Web]]"},
}

local function __genOrderedIndex(t)
    local orderedIndex = {}
    for key in pairs(t) do
        table.insert(orderedIndex, key)
    end
    table.sort(orderedIndex)
    return orderedIndex
end

local function orderedNext(t, state)
    -- Equivalent of the next function, but returns the keys in the alphabetic
    -- order. We use a temporary ordered key table that is stored in the
    -- table being iterated.

    local key = nil
    if state == nil then
        -- the first time, generate the index
        t.__orderedIndex = __genOrderedIndex(t)
        key = t.__orderedIndex[1]
    else
        -- fetch the next value
        for i = 1, #(t.__orderedIndex) do
            if t.__orderedIndex[i] == state then
                key = t.__orderedIndex[i + 1]
            end
        end
    end

    if key then
        return key, t[key]
    end

    -- no more value to return, cleanup
    t.__orderedIndex = nil
    return
end

function p.orderedPairs(t)
    -- Equivalent of the pairs() function on tables. Allows to iterate in order.
    return orderedNext, t, nil
end

--- Returns a date formatted with the {{Start date and age}} template.
---
--- @param date string
--- @return string
local function get_formatted_date(date)
	local formatted_date = mw.getCurrentFrame():expandTemplate{title = "Start date and age", args = {date}}
	return formatted_date
end

--- Returns a Wikidata qualifier request for the date.
--- @param args []
--- @return string
local function get_wikidata_date(args)
	table.remove(args, 1) -- Remove "reference"
	table.remove(args, 1) -- Remove "edit"
	table.insert(args, properties.publication_date)
	local result = wikidata._qualifier(args)
	return result
end

--- Returns a Wikidata property request for the version number.
---
--- @param args []
--- @return string
local function get_wikidata_version(args)
	table.insert(args, 1, "reference")
	table.insert(args, 2, "edit")
	local result = wikidata._property(args)
	return result
end

--- Returns an entity for Wikidata.
---
--- @param software string
--- @param platform string
--- @param version_type string
--- @param use_platform boolean
--- @return table
local function get_wikidata_args(software, platform, version_type, use_platform)
	local args = {
		software,
		properties.software_version_identifier,
		[properties.version_type] = version_type,
	}

	if use_platform then
		args[properties.platform] = platform
	end

	return args
end

--- Creates an infobox row with a the label and either a version number or a version number and date value.
--- Returns the number of the next infobox row.
---
--- param infobox_args table
--- @param software_id string
--- @param platform_id string
--- @param version_type string
--- @param use_platform boolean
--- @param row_number number
--- @param label string
--- @return number
local function get_infobox_row(infobox_args, software_id, platform_id, version_type, use_platform, row_number, label)
	local wikidata_args = get_wikidata_args(software_id, platform_id, version_type, use_platform)
	local version = get_wikidata_version(wikidata_args)
	if version and version ~= "" then
		row_number = row_number + 1
		infobox_args["label" .. row_number] = label
		local date = get_wikidata_date(wikidata_args)
		if date and date ~= "" then
			date = get_formatted_date(date)
			infobox_args["data" .. row_number] = version .. " / " .. date
		else
			infobox_args["data" .. row_number] = version
		end
	end

	return row_number
end

--- Returns a subbox Infobox with an infobox row for a programming language release.
---
--- Valid version types are listed at the version_types table at the top.
---
--- Infobox parameters used:
---- |discontinued=
---- |version_type=
---
--- Testing parameters used:
---- |language=
---
--- @param args table
--- @return string
local function _language(args)
	if not args.version_type then
		return ""
	end

	local version_type = version_types[args.version_type]
	if not version_type then
		return ""
	end

	local software_id = args.language or ""
	local infobox_args = {subbox = "yes"}

	local label = ""
	if version_type == version_types.stable then
		if args.discontinued then
			label = "[[Software release life cycle|Final release]]"
		else
			label = "[[Software release life cycle|Stable release]]"
		end
	elseif version_type == version_types.pre_release then
		label = "[[Software release life cycle#BETA|Preview release]]"
	end

	local number_of_results = get_infobox_row(infobox_args, software_id, nil, version_type, false, 0, label)

	if number_of_results == 0 then
		return ""
	end

	local infobox = require('Module:Infobox').infobox
	return infobox(infobox_args)
end

--- Returns a subbox Infobox with one or more infobox rows for a software release.
---
---	The list of platforms is a comma separated list. Valid platforms are listed in the platforms table at the top.
--- Valid version types are listed at the version_types table at the top.
---
--- Infobox parameters used:
---- |version_type=
---- |platforms=
---
--- Testing parameters used:
---- |software=
---
--- @param args table
--- @return string
local function _software(args)
	if not args.version_type or not args.platforms then
		return ""
	end

	local version_type = version_types[args.version_type]
	if not version_type then
		return ""
	end
	
	---@type table
	local used_platforms = {}
	for platform in string.gmatch(args.platforms, "[^,]+") do
		used_platforms[platform] = true
	end

	local software_id = args.software or ""
    local infobox_args = {subbox = "yes"}
	local row_number = 0

	for platform, _ in p.orderedPairs(used_platforms) do
		local platform_id = platforms[platform].id
		row_number = get_infobox_row(infobox_args, software_id, platform_id, version_type, true, row_number, platforms[platform].label)
	end

	if row_number == 0 then
		return ""
	end

	local infobox = require('Module:Infobox').infobox
	return infobox(infobox_args)
end

--- Returns an infobox row for a programming language release.
---
--- @param frame table
--- @return string
function p.language(frame)
	local getArgs = require('Module:Arguments').getArgs
    local args = getArgs(frame)
	return _language(args)
end

--- Returns one or more infobox rows for a software release.
---
--- @param frame table
--- @return string
function p.software(frame)
	local getArgs = require('Module:Arguments').getArgs
    local args = getArgs(frame)
	return _software(args)
end

return p