Jump to content
Menü aufrufen
Toggle preferences menu
Persönliches Menü aufrufen
Nicht angemeldet
Ihre IP-Adresse wird öffentlich sichtbar sein, wenn Sie Änderungen vornehmen.

Die Dokumentation für dieses Modul kann unter Modul:Entity/doc erstellt werden

require('strict')

local data = require('Module:Entity/Data')
local assembly = require('Module:Entity/Assembly')
local structuredData = require('Module:Entity/StructuredData')
local entityInfobox = require('Module:Entity/Infobox')
local categories = require('Module:Entity/Categories')
local msg = require('Module:Entity/Message').msg

-- Maintenance category page name, German like the Categories module's; a
-- page name, never msg()-wrapped.
local CATEGORY_ENTITY_ERROR = '[[Category:Seiten mit Entity-Fehlern]]'

local p = {}

--- Collects structured data from the chain and facets and persists it via the backend,
--- then the chain's rows for the kind's several-rows-per-page tables.
---
--- @param chain table[]
--- @param facets table[]
--- @param ctx EntityHookContext
--- @param editorialData table|nil Pre-projected key-value pairs from the editorial layer
--- @param kind string|nil Kind name, selects the Bucket tables the page may write; nil when no kind
---   matched (e.g. no uuid), in which case only the entity-routable keys are written
--- @return boolean success True if the backend accepted the data
--- @return string[]|nil unregistered Emitter keys not registered in properties.json, or nil
local function storeStructuredData(chain, facets, ctx, editorialData, kind)
	local chainData = assembly.run(chain, 'getStructuredData', ctx)
	local facetData = assembly.run(facets, 'getStructuredData', ctx)
	local merged = assembly.mergeStructuredData({ chainData, facetData })
	-- The item_weapon `damage` value's source picks its German property: the
	-- weapon chain's per-shot alpha total writes Alphaschaden, the grenade and
	-- magazine facets' damage keeps Schaden. Context for the SMW translation
	-- only; the Bucket column stays the upstream union of all three sources.
	local weaponAlphaDamage = chainData.damage ~= nil and facetData.damage == nil
	for k, v in pairs(editorialData or {}) do
		merged[k] = v
	end
	-- `subject_type` is the page's most-specific structural type (fine-grained:
	-- "Gun", "Cooler", …), deliberately distinct from the coarse `result.kind`
	-- (Item / Vehicle / …). It is the same value that drives the structural
	-- category, persisted as a queryable property. An emitter key maps to the
	-- manifest key with underscores as spaces, so `subject_type` is "Subject type".
	if ctx.typeInfo and ctx.typeInfo.name then
		merged.subject_type = ctx.typeInfo.name
	end
	-- The storage route for the kind itself, so a sibling renderer on a page
	-- with no uuid can recover the kind this infobox declared
	-- (Module:Entity/Data's parseArgs).
	if kind then
		merged.kind = kind
	end
	local missionApi = kind == 'Mission' and ctx.apiData or nil
	if kind == 'Mission' and editorialData and (editorialData.orders ~= nil or editorialData.rewards ~= nil) then
		missionApi = false
	end
	local success, _err, unregistered = structuredData.store(merged, kind, missionApi, weaponAlphaDamage)
	-- subject_type rides along into the %rows subobjects: an SMW ask cannot
	-- join a page-level property onto subobject rows, so each row carries a
	-- copy (the dimensions Fit gadget's group filter).
	local rowsSuccess, _rowsErr, rowsUnregistered =
		structuredData.storeRows(assembly.run(chain, 'getStructuredRows', ctx), kind, merged.subject_type)
	if rowsUnregistered then
		unregistered = unregistered or {}
		for _, key in ipairs(rowsUnregistered) do
			unregistered[#unregistered + 1] = key
		end
	end
	return success and rowsSuccess, unregistered
end

--- Resolves the page's short description: the chain's getShortDescription,
--- composing in the first facet adjective, by the policies in
--- Module:Entity/Assembly. Falls back to the type display name.
---
--- @param chain table[]
--- @param facets table[]
--- @param ctx EntityHookContext
--- @return string|nil nil when nothing resolves
local function resolveShortDescription(chain, facets, ctx)
	if not ctx.typeInfo then
		return nil
	end

	-- prefix exists only for this call: set here and cleared after the resolve.
	ctx.prefix = assembly.run(facets, 'getShortDescriptionPrefix', ctx)
	local desc = assembly.run(chain, 'getShortDescription', ctx)
	ctx.prefix = nil
	if desc == nil then
		desc = ctx.typeInfo.name
	end
	if desc == nil or desc == '' then
		return nil
	end
	return desc
end

--- Sets the page's short description via the SHORTDESC parser function.
--- #shortdesc rejects an absent value with a Lua error that takes the whole
--- render down with it, and a page with nothing to describe is better off
--- with no short description than with the infobox replaced by an error.
---
--- @param frame table
--- @param desc string|nil
local function setShortDescription(frame, desc)
	if desc == nil then
		return
	end

	frame:callParserFunction('SHORTDESC', desc)
end

--- The `#seo` parser function's arguments for an Entity page: the resolved
--- short description as og:description and the infobox image as og:image.
--- og:title and og:url are correct sitewide defaults and are deliberately
--- not overridden. The positional argument is the Lua form of
--- {{#seo:|...}}; callParserFunction throws without one.
---
--- @param args table Parsed wikitext args
--- @param shortDesc string|nil The resolved short description
--- @param locale string {{PAGELANGUAGE}}, expanded by the caller
--- @return table
local function seoArgs(args, shortDesc, locale)
	local seo = {
		'',
		type = 'article',
		locale = locale,
	}
	if shortDesc ~= nil then
		seo.description = shortDesc
	end
	if args.image ~= nil then
		seo.image = args.image
	end
	return seo
end

--- og:locale takes ll_CC form; a bare {{PAGELANGUAGE}} (de, en) is not a
--- valid value, so the known languages map to their locale form and anything
--- else passes through untouched.
--- @param lang string page language code
--- @return string
local function ogLocale(lang)
	if lang == 'de' then
		return 'de_DE'
	elseif lang == 'en' then
		return 'en_US'
	end
	return lang
end

--- Emits the page's `#seo` metadata via the WikiSEO extension.
---
--- @param frame table
--- @param args table Parsed wikitext args
--- @param shortDesc string|nil The resolved short description
local function setSeo(frame, args, shortDesc)
	frame:callParserFunction('#seo', seoArgs(args, shortDesc, ogLocale(frame:preprocess('{{PAGELANGUAGE}}'))))
end

--- The interlanguage link to the English wiki's counterpart: the same title.
--- Title parity is the contract (upstream's Patch links `de:` the same way);
--- where this wiki holds a designation and the English wiki a name
--- (Stanton II / Crusader), an English-wiki-side parity redirect resolves it.
--- Base pages only, and only on the SMW backend wiki: the .tools original
--- must not link to itself.
---
--- @param title table mw.title.getCurrentTitle(), passed in for testability
--- @param backend string StructuredData.backend()
--- @return string wikitext, '' when no link applies
local function interlanguageLink(title, backend)
	if backend ~= 'smw' or title.isSubpage then
		return ''
	end
	return '[[en:' .. title.text .. ']]'
end

--- Can this invocation identify the entity it is describing? Three ways: a
--- `uuid`, a name (curated or from the API record), or a **kind that claimed the
--- page**. The last is the kind-declared case: the `{{Location}}` facade injects
--- `|kind=Location` and a lore system deliberately carries no uuid and often no
--- `|name=`, deriving its identity from the page title — `Infobox.render` titles
--- from `mw.title.getCurrentTitle().text` and `Location.resolveLookupName` looks
--- the starmap record up by the same title, so such a page renders correctly.
---
--- The test is `result.matchedKind` (set by Data's editorial fork when `|kind=`
--- names an opted-in kind), not the raw `args.kind`: a misspelled kind resolves
--- to nothing and must still hit the error rather than silently render a
--- title-only shell.
---
--- @param args table Parsed wikitext args
--- @param result table The Module:Entity/Data.get result
--- @return boolean
local function isIdentifiable(args, result)
	return args.uuid ~= nil or args.name ~= nil or result.apiData.name ~= nil or result.matchedKind ~= nil
end

--- Main entry point for the Entity module. Renders the infobox and owns
--- page-metadata responsibilities (structured-data storage, SHORTDESC, #seo
--- metadata, tracking categories). Sibling renderers on the same page should
--- consume Module:Entity/Data directly and leave page metadata to this template.
---
--- @param frame table The MediaWiki frame object
--- @return string HTML output with optional tracking categories
function p.main(frame)
	local args = data.parseArgs(frame)
	local result = data.get(args)

	if not isIdentifiable(args, result) then
		return '<span class="error">'
			.. msg('Entity module error: no uuid, name, or kind provided')
			.. '</span>'
			.. CATEGORY_ENTITY_ERROR
	end

	local html = entityInfobox.render(result, args)
	-- The Bucket route is the kind that actually matched, never result.kind's
	-- 'Item' fallback: a page whose kind never resolved writes its entity-routable
	-- keys only, rather than another kind's columns.
	local storeSuccess, unregistered = storeStructuredData(
		result.chain,
		result.facets,
		result.ctx,
		result.editorialData,
		result.matchedKind and result.matchedKind.name or nil
	)

	local shortDesc = resolveShortDescription(result.chain, result.facets, result.ctx)
	setShortDescription(frame, shortDesc)
	setSeo(frame, args, shortDesc)
	local langLink = interlanguageLink(mw.title.getCurrentTitle(), structuredData.backend())

	return html
		.. categories.build(
			result.typeInfo,
			result.chainCategories,
			result.apiData,
			args,
			result.hasApiError,
			not storeSuccess,
			result.hasManualApiData,
			result.unresolvedReference,
			unregistered and #unregistered > 0 or false,
			result.matchedKind
		)
		.. langLink
end

-- Test-only exports. Not part of the public API.
p._internal = {
	isIdentifiable = isIdentifiable,
	resolveShortDescription = resolveShortDescription,
	setShortDescription = setShortDescription,
	seoArgs = seoArgs,
	ogLocale = ogLocale,
	interlanguageLink = interlanguageLink,
	storeStructuredData = storeStructuredData,
}

return p
Zuletzt geändert
Urheberrecht
Cookies helfen uns bei der Bereitstellung dieses Wikis. Durch die Nutzung des Star Citizen Wiki erklärst du dich damit einverstanden, dass wir Cookies speichern.