Jump to content

Module:Multiple releases/sandbox: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
No edit summary
No edit summary
Line 10: Line 10:
}
}


local release_type = {
local version_types = {
pre_release = "Q51930650",
pre_release = "Q51930650",
stable = "Q2804309",
stable = "Q2804309",
Line 66: Line 66:
end
end


--- Returns a date formatted with the {{Start date and age}} template.
local function get_wikidata_version(software, platform, version_type)
---
return wikidata._property({
--- @param date string
"reference",
--- @return string
"edit",
local function get_formatted_date(date)
software,
local formatted_date = mw.getCurrentFrame():expandTemplate{title = "Start date and age", args = {date}}
properties.software_version_identifier,
return formatted_date
[properties.platform] = platform,
[properties.version_type] = version_type,
})
end
end


--- Returns a Wikidata qualifier request for the date.
local function get_wikidata_date(software, platform, version_type)
--- @param args []
local date = wikidata._qualifier({
--- @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,
software,
properties.software_version_identifier,
properties.software_version_identifier,
[properties.platform] = platform,
[properties.version_type] = version_type,
[properties.version_type] = version_type,
}
properties.publication_date,
})


if not date or date == "" then
if use_platform then
args[properties.platform] = platform
return nil
end
end


return args
return mw.getCurrentFrame():expandTemplate{title = "Start date and age", args = {date}}
end
end


--- Creates an infobox row with a the label and either a version number or a version number and date value.
local function _main(args)
--- Returns the number of the next infobox row.
if not args.version_type or not args.platforms then
---
return
--- 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
end


local version_type = release_type[args.version_type]
local version_type = version_types[args.version_type]
if not version_type then
if not version_type then
return
return ""
end
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 = {}
local used_platforms = {}
for platform in string.gmatch(args.platforms, "[^,]+") do
for platform in string.gmatch(args.platforms, "[^,]+") do
used_platforms[platform] = true
used_platforms[platform] = true
end
end
local software_id = args.software or ""


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

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

if version and version ~= "" then
platform_number = platform_number + 1
infobox_args["label" .. platform_number] = platforms[platform].label
if date then
infobox_args["data" .. platform_number] = version .. " / " .. date
else
infobox_args["data" .. platform_number] = version
end
end
end
end


if platform_number == 0 then
if row_number == 0 then
return
return ""
end
end


Line 136: Line 241:
end
end


--- Returns an infobox row for a programming language release.
function p.main(frame)
---
--- @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 getArgs = require('Module:Arguments').getArgs
local args = getArgs(frame)
local args = getArgs(frame)
return _main(args)
return _software(args)
end
end



Revision as of 10:29, 23 December 2021

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