Jump to content

Module:Maskin: Difference between revisions

From Programvareverkstedet
La templaten selv kunne spesifisere hostname
Underoverskrifter
Line 42: Line 42:
end
end


-- { arg, label , optional render function(value, args) }
-- { arg, label , optional function(value, args) }, or { heading = text } starting a section
local FIELDS = {
local FIELDS = {
{ "image", nil, image_cell },
{ "image", nil, image_cell },
{ heading = "Generelt" },
{ "role", "Funksjon" },
{ "role", "Funksjon" },
{ "form_factor", "Formfaktor", form_factor_label },
{ "form_factor", "Formfaktor", form_factor_label },
Line 51: Line 52:
{ "host", "Kjører på" },
{ "host", "Kjører på" },
{ "location", "Plassering" },
{ "location", "Plassering" },
{ heading = "Maskinvare" },
{ "model", "Modell" },
{ "model", "Modell" },
{ "cpu", "CPU" },
{ "cpu", "CPU" },
Line 57: Line 59:
{ "gpu", "GPU" },
{ "gpu", "GPU" },
{ "hardware_other", "Annen maskinvare" },
{ "hardware_other", "Annen maskinvare" },
{ heading = "Nettverk" },
{ "services", "Tjenester" },
{ "services", "Tjenester" },
{ "ipv4", "IPv4" },
{ "ipv4", "IPv4" },
{ "ipv6", "IPv6" },
{ "ipv6", "IPv6" },
{ "network_location", "Nettverkslokasjon" },
{ "network_location", "Nettverkslokasjon" },
{ heading = "Livsløp" },
{ "named_after", "Navneopphav" },
{ "named_after", "Navneopphav" },
{ "origin", "Opphav" },
{ "origin", "Opphav" },
Line 122: Line 126:
head:tag("span"):addClass("pvv-maskin-status"):wikitext(status_label(status))
head:tag("span"):addClass("pvv-maskin-status"):wikitext(status_label(status))
return '|-\n! colspan=2 class="pvv-maskin-top" | ' .. tostring(head)
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
end


Line 140: Line 148:
local out = { '{| class="wikitable" style="' .. style .. '"' }
local out = { '{| class="wikitable" style="' .. style .. '"' }


local status = arg("status", args)
local heading
for _, f in ipairs(FIELDS) do
for _, f in ipairs(FIELDS) do
local v = arg(f[1], args)
if f.heading then
if v then
heading = f.heading
table.insert(out, table_row(f[2], f[3] and f[3](v, args) or v))
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
table.insert(out, table_row(f[2], f[3] and f[3](v, args) or v))
end
end
end
end
end


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


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


table.insert(out, "|}")
table.insert(out, "|}")

Revision as of 13:15, 28 November 2026

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",
}

local function status_label(v)
	return STATUS[v] or v
end

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

local function machine_links(v)
	return (v:gsub("[^%s*]+", "[[Maskiner/%0|%0]]"))
end

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

local function os_with_version(v, args)
	local version = arg("os_version", args)
	return version and v .. " " .. version or v
end

-- { arg, label , optional function(value, args) }, or { heading = text } starting a section
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" },
	{ "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 },
	{ "successor", "Etterfølger", machine_links },
}

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" 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
				table.insert(out, table_row(f[2], 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