Jump to content

Module:Maskin

From Programvareverkstedet
Revision as of 14:27, 28 November 2026 by Oysteikt (talk | contribs) (Refaktorering, fjern punktlister fra "tjenester")

Denne templaten lager en tabell med metadata om de forskjellige maskinene våre.

Templaten vil først og fremst se hva du spesifiserte da du kalte den, etterfulgt av å sjekke Module:Maskin/hosts.json. Dersom raden ikke står noen av stedene vil den hoppe over raden og prøve neste.


local p = {}

local STATUS = {
	live = "I drift",
	planned = "Planlagt",
	unknown = "Ukjent status",
	stored = "Lagret",
	retired = "Utfaset",
}

local FORM_FACTOR = {
	rack = "Rack",
	tower = "Tårn",
	vm = "Virtuell maskin",
	container = "Container",
	other = "Annet",
}

-- (v : string) -> string
local function status_label(v)
	return STATUS[v] or v
end

-- (v : string, args : Args) -> string
local function form_factor_label(v, args)
	local units = arg("rack_units", args)
	local label = FORM_FACTOR[v] or v
	return units and label .. ", " .. units .. "U" or label
end

-- (v : string) -> { string }
local function bullet_items(v)
	local items = {}
	for line in v:gmatch("[^\n]+") do
		local item = line:match("^[*%s]*(.-)%s*$")
		if item ~= "" then
			table.insert(items, item)
		end
	end
	return items
end

-- (items : { string }) -> string
local function item_divs(items)
	local divs = {}
	for i, item in ipairs(items) do
		divs[i] = '<div class="pvv-maskin-item">' .. item .. "</div>"
	end
	return table.concat(divs)
end

-- (v : string) -> string
local function plain_lines(v)
	return item_divs(bullet_items(v))
end

-- (v : string) -> string
local function machine_links(v)
	local machine_name_regex = "^[A-Za-z0-9_-]+"
	local function link(name)
		local page = name:gsub("^%l", string.upper)
		return "[[Maskiner/" .. page .. "|" .. name .. "]]"
	end
	local items = bullet_items(v)
	for i, item in ipairs(items) do
		items[i] = item:gsub(machine_name_regex, link)
	end
	return item_divs(items)
end

-- (v : string) -> int
local function machine_count(v)
	return #bullet_items(v)
end

-- (v : string, args : Args) -> string
local function image_cell(v, args)
	local caption = arg("caption", args) or ""
	local text = caption ~= "" and '<div class="pvv-maskin-caption">' .. caption .. "</div>" or ""
	return "[[File:" .. v .. "|frameless|600px|" .. caption .. "]]" .. text
end

-- (v : string, args : Args) -> string
local function os_with_version(v, args)
	local version = arg("os_version", args)
	return version and v .. " " .. version or v
end

-- {
--   arg : string,
--   label : optional string,
--   render : optional function(value, args) -> string,
--   plural : optional string,
-- } | {
--   heading : string
-- }
local FIELDS = {
	{ "image", nil, image_cell },
	{ heading = "Generelt" },
	{ "role", "Funksjon" },
	{ "form_factor", "Formfaktor", form_factor_label },
	{ "os", "OS", os_with_version },
	{ "managed_by", "Konfigurasjon" },
	{ "host", "Kjører på" },
	{ "location", "Plassering" },
	{ heading = "Maskinvare" },
	{ "model", "Modell" },
	{ "cpu", "CPU" },
	{ "ram", "Minne" },
	{ "storage", "Lagring" },
	{ "gpu", "GPU" },
	{ "hardware_other", "Annen maskinvare" },
	{ heading = "Nettverk" },
	{ "services", "Tjenester", plain_lines },
	{ "ipv4", "IPv4" },
	{ "ipv6", "IPv6" },
	{ "network_location", "Nettverkslokasjon" },
	{ heading = "Livsløp" },
	{ "named_after", "Navneopphav" },
	{ "origin", "Opphav" },
	{ "date_in_service", "Satt i drift" },
	{ "date_retired", "Utfaset" },
	{ "predecessor", "Forgjenger", machine_links, plural = "Forgjengere" },
	{ "successor", "Etterfølger", machine_links, plural = "Etterfølgere" },
}

local DATA_PAGE = "Module:Maskin/hosts.json"

local function data_value(v)
	if type(v) == "table" then
		local items = {}
		for _, item in ipairs(v) do
			table.insert(items, "* " .. item)
		end
		return #items > 0 and table.concat(items, "\n") or nil
	end
	v = v ~= nil and mw.text.trim(tostring(v)) or ""
	return v ~= "" and v or nil
end

local IMAGE_EXTENSIONS = { "jpg", "jpeg", "png", "gif", "webp", "svg" }

local function host_image(host)
	for _, ext in ipairs(IMAGE_EXTENSIONS) do
		local name = host .. "." .. ext
		if mw.title.makeTitle("File", name).file.exists then
			return name
		end
	end
end

local FALLBACKS = {
	image = host_image,
}

function arg(key, args)
	local v = mw.text.trim(args.page[key] or "")
	if v ~= "" then
		return v
	end
	v = data_value(args.data[key])
	if v then
		return v
	end
	return FALLBACKS[key] and FALLBACKS[key](args.host)
end

local function table_row(label, value)
	local cell = label and ('! style="text-align:left" | ' .. label .. "\n|") or '| colspan=2 class="pvv-maskin-image" |'
	local sep = value:find("^[*#]") and "\n" or " "
	return "|-\n" .. cell .. sep .. value
end

local function header_row(host, status)
	local head = mw.html.create("div")
		:addClass("pvv-maskin-head pvv-status-" .. status)
	head:tag("span"):addClass("pvv-maskin-name"):wikitext(host)
	head:tag("span"):addClass("pvv-maskin-status"):wikitext(status_label(status))
	return '|-\n! colspan=2 class="pvv-maskin-top" | ' .. tostring(head)
end

local function section_row(heading, status)
	return '|-\n! colspan=2 class="pvv-maskin-section pvv-status-' .. status .. '" | ' .. heading
end

function p.infobox(frame)
	local hostname = mw.text.trim(frame:getParent().args.hostname or "")
	local host = (hostname ~= "" and hostname or mw.title.getCurrentTitle().text:match("[^/]+$")):lower()
	local args = {
		host = host,
		page = frame:getParent().args,
		data = mw.loadJsonData(DATA_PAGE)[host] or {},
	}
	local style = table.concat({
		"float:right",
		"clear:right",
		"width:22em",
		"margin:0 0 1em 1em",
	}, "; ")
	local out = { '{| class="wikitable pvv-maskin" style="' .. style .. '"' }

	local status = arg("status", args)
	local heading
	for _, f in ipairs(FIELDS) do
		if f.heading then
			heading = f.heading
		else
			local v = arg(f[1], args)
			if v then
				if heading then
					table.insert(out, section_row(heading, status or "unknown"))
					heading = nil
				end
				local label = f.plural and machine_count(v) > 1 and f.plural or f[2]
				table.insert(out, table_row(label, f[3] and f[3](v, args) or v))
			end
		end
	end

	if #out == 1 and not status then
	    return ""
	end

	table.insert(out, 2, header_row(host, status or "unknown"))

	table.insert(out, "|}")
	local styles = frame:extensionTag("templatestyles", "", { src = frame:getParent():getTitle() .. "/styles.css" })
	return styles .. "\n" .. table.concat(out, "\n")
end

return p