Module:Wikidata description

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

Code

--script that retrieves basic data stored in Wikidata, for the datamodel, see https://www.mediawiki.org/wiki/Extension:Wikibase_Client/Lua

local p = {}

local linguistic = require('Module:Linguistic')
local selectClaims = require "Module:Wikidata/GetClaims"
local tools = require "Module:Wikidata/Tools"
local entities = require "Module:Wikidata/FormatEntity"
local dates = require "Module:Wikidata/Dates"
local weblink = require "Module:Weblink"

local LangSwitch = require('Module:LangSwitch')
local fb = require('Module:Fallback')
local i18nmessages = mw.loadData('Module:i18n/wikidata')

-- Wiki-specific parameters
local defaultlang = mw.getCurrentFrame():preprocess("{{int:lang}}")
local defaultlink = 'wikidata'

local function i18n(str, lang)
	local message = i18nmessages[str]
	if type(message) == 'string' then
		return message
	end
	return LangSwitch._langSwitch(message, lang or defaultlang)
end

function p.getDescription(frame) -- simple for simple templates like {{Q|}}}
	local entity = frame.args.entity
	local lang = frame.args.lang
	return p._getDescription(entity, lang)
end

function p._getDescription(entity, lang)
	if not entity then
		return i18n('no description - not entity')
	end
	if type(entity) == "string" and ( (not lang) or (lang == defaultlang) ) then
		return mw.wikibase.description(entity)
	end
	entity = p.getEntity(entity)
	local descriptions = entity.descriptions
	if not descriptions then
		return i18n('no description - not descriptions')
	end
	local langlist = fb.fblist(lang or defaultlang) -- list of fallback languages if no label in the desired language
	for i, lg in pairs(langlist) do
		if descriptions[lg] then
			return descriptions[lg].value
		end
	end
	return i18n('no description - end')
end

return p