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 18:49, 22 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 release_type = {
	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

local function get_wikidata_version(software, platform, version_type)
	return wikidata._property({
		"reference",
		"edit",
		software,
		properties.software_version_identifier,
		[properties.platform] = platform,
		[properties.version_type] = version_type,
	})
end

local function get_wikidata_date(software, platform, version_type)
	local date =  wikidata._qualifier({
		software,
		properties.software_version_identifier,
		[properties.platform] = platform,
		[properties.version_type] = version_type,
		properties.publication_date,
	})

	if not date or date == "" then
		return nil
	end

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

local function _main(args)
	if not args.software or not args.version_type or not args.platforms then
		return
	end

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

	local used_platforms = {}
	for platform in string.gmatch(args.platforms, "[^,]+") do
		used_platforms[platform] = true
	end

    local infobox_args = {subbox = "yes"}
	local platform_number = 0
	for platform, _ in p.orderedPairs(used_platforms) do
		local platform_id = platforms[platform].id
		local date = get_wikidata_date(args.software, platform_id, version_type)
		local version = get_wikidata_version(args.software, 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

	if platform_number == 0 then
		return
	end

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

function p.main(frame)
	local getArgs = require('Module:Arguments').getArgs
    local args = getArgs(frame)
	return _main(args)
end

return p