Jump to content

Module:Multiple releases/sandbox

From Wikipedia, the free encyclopedia
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 labels = {
	final = "[[Software release life cycle|Final release]]",
	stable = "[[Software release life cycle|Stable release]]",
	beta = "[[Software release life cycle#BETA|Preview release]]",
}

local date_template = "Start date and age"

--- 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 = date_template, args = {date}}
	return formatted_date
end

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

--- Returns a Wikidata property request for the version number.
---
--- @param args []
--- @return string
local function get_wikidata_version(args)
	local wikidata_args = mw.clone(args)
	table.insert(wikidata_args, 1, "references")
	table.insert(wikidata_args, 2, "preferred")
	table.insert(wikidata_args, 3, "sourced")
	table.insert(wikidata_args, 4, "edit")
	local result = wikidata._property(wikidata_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 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

--- Creates an infobox row.
--- A row consists of a label of the version type and a data value of either a version number or a version number and date value.
--- Returns the total number of rows created.
---
--- @param infobox_args table
--- @param software_id string
--- @param version_type string
--- @param isDiscontinued string
--- @return number
local function get_single_row(infobox_args, software_id, version_type, isDiscontinued)
	local label = ""
	if version_type == version_types.stable then
		if isDiscontinued then
			label = labels.final
		else
			label = labels.stable
		end
	elseif version_type == version_types.pre_release then
		label = labels.beta
	end

	return get_infobox_row(infobox_args, software_id, nil, version_type, false, 0, label)
end

--- Creates one or more infobox rows.
--- A row consists of a label of the platform name and a data value of either a version number or a version number and date value.
--- Returns the total number of rows created.
---
--- @param infobox_args table
--- @param software_id string
--- @param requested_platforms string
--- @param version_type string
--- @return number
local function get_multiple_rows(infobox_args, software_id, requested_platforms, version_type)
	---@type table
	local used_platforms = {}
	for platform in string.gmatch(requested_platforms, "[^,]+") do
		used_platforms[platform] = true
	end

	local ordered_pairs = require("Module:Ordered pairs").orderedPairs
	local row_number = 0
	for platform, _ in ordered_pairs(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

	return row_number
end

--- Returns a child Infobox with one or more infobox rows for a software release or an empty string.
---
---	The list of platforms is a comma separated list. Valid platforms are listed in the platforms table at the top.
--- If args.platforms is empty then only the latest release is returned.
--- Valid version types are listed at the version_types table at the top.
---
--- Infobox parameters used:
---- |discontinued=
---- |platforms=
---- |version_type=
---
--- Testing parameters used:
---- |software=
---
--- @param args table
--- @return string
local function _main(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.software or ""
    local infobox_args = {child = "yes"}

	local row_number
	if args.platforms then
		row_number = get_multiple_rows(infobox_args, software_id, args.platforms, version_type)
	else
		row_number = get_single_row(infobox_args, software_id, version_type, args.discontinued)
	end

	if row_number == 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 or an empty string.
---
--- @param frame table
--- @return string
function p.main(frame)
	local getArgs = require("Module:Arguments").getArgs
    local args = getArgs(frame)
	return _main(args)
end

return p