<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wicri-demo.istex.fr/Wicri/SitDemIstex/V131/pool/index.php?action=history&amp;feed=atom&amp;title=Module%3AWikidata_label</id>
	<title>Module:Wikidata label - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wicri-demo.istex.fr/Wicri/SitDemIstex/V131/pool/index.php?action=history&amp;feed=atom&amp;title=Module%3AWikidata_label"/>
	<link rel="alternate" type="text/html" href="https://wicri-demo.istex.fr/Wicri/SitDemIstex/V131/pool/index.php?title=Module:Wikidata_label&amp;action=history"/>
	<updated>2026-04-19T11:14:55Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.31.10</generator>
	<entry>
		<id>https://wicri-demo.istex.fr/Wicri/SitDemIstex/V131/pool/index.php?title=Module:Wikidata_label&amp;diff=10910&amp;oldid=prev</id>
		<title>Jacques Ducloy: Created page with &quot;--[[     __  __           _       _      __        ___ _    _     _       _          _       _          _   |  \/  | ___   __| |_   _| | ___ \ \      / (_) | _(_) __| | __ _|...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wicri-demo.istex.fr/Wicri/SitDemIstex/V131/pool/index.php?title=Module:Wikidata_label&amp;diff=10910&amp;oldid=prev"/>
		<updated>2018-11-02T09:04:00Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;--[[     __  __           _       _      __        ___ _    _     _       _          _       _          _   |  \/  | ___   __| |_   _| | ___ \ \      / (_) | _(_) __| | __ _|...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;--[[  &lt;br /&gt;
  __  __           _       _      __        ___ _    _     _       _          _       _          _ &lt;br /&gt;
 |  \/  | ___   __| |_   _| | ___ \ \      / (_) | _(_) __| | __ _| |_ __ _  | | __ _| |__   ___| |&lt;br /&gt;
 | |\/| |/ _ \ / _` | | | | |/ _ (_) \ /\ / /| | |/ / |/ _` |/ _` | __/ _` | | |/ _` | '_ \ / _ \ |&lt;br /&gt;
 | |  | | (_) | (_| | |_| | |  __/_ \ V  V / | |   &amp;lt;| | (_| | (_| | || (_| | | | (_| | |_) |  __/ |&lt;br /&gt;
 |_|  |_|\___/ \__,_|\__,_|_|\___(_) \_/\_/  |_|_|\_\_|\__,_|\__,_|\__\__,_| |_|\__,_|_.__/ \___|_|&lt;br /&gt;
                                                                                                   &lt;br /&gt;
This module is intended to be the engine behind &amp;quot;Template:Label&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Please do not modify this code without applying the changes first at &amp;quot;Module:Wikidata label/sandbox&amp;quot; and testing &lt;br /&gt;
at &amp;quot;Module:Wikidata label/testcases&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Authors and maintainers:&lt;br /&gt;
* User:Jarekt - original version &lt;br /&gt;
]]&lt;br /&gt;
&lt;br /&gt;
-- use different sitelink call depending if you already have an entity or not&lt;br /&gt;
local function getSitelink(item, entity, lang)&lt;br /&gt;
	if entity then -- if we have entity than use it&lt;br /&gt;
		return entity:getSitelink(lang .. 'wiki') &lt;br /&gt;
	else -- if no entity than use different function&lt;br /&gt;
		return mw.wikibase.sitelink( item, lang .. 'wiki' )&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local p = {}&lt;br /&gt;
&lt;br /&gt;
--[[&lt;br /&gt;
_getLabel&lt;br /&gt;
&lt;br /&gt;
This function returns a label translated to desired language, created based on wikidata&lt;br /&gt;
&lt;br /&gt;
Inputs:&lt;br /&gt;
	1: item - wikidata's item's q-code or entity class&lt;br /&gt;
	2: lang - desired language of the label&lt;br /&gt;
	3: link_type - link style. Possible values: &amp;quot;wikipedia&amp;quot;, &amp;quot;Wikidata&amp;quot;, &amp;quot;Commons&amp;quot;, or &amp;quot;-&amp;quot; (no link)&lt;br /&gt;
	4: capitalization - can be &amp;quot;uc&amp;quot; (upper case), &amp;quot;lc&amp;quot; (lower case), &amp;quot;ucfirst&amp;quot; (upper case for the first letter), &lt;br /&gt;
			&amp;quot;lcfirst&amp;quot; (lower case for the first letter)&lt;br /&gt;
 &lt;br /&gt;
Error Handling:&lt;br /&gt;
	Bad q-code will result in displayed error&lt;br /&gt;
]]&lt;br /&gt;
function p._getLabel(item, lang, link_type, capitalization)&lt;br /&gt;
	local entity, s, link, label, language&lt;br /&gt;
&lt;br /&gt;
	-- clean up the input parameters&lt;br /&gt;
	if type(item)~='string' then -- &amp;quot;item&amp;quot; is not a q-code&lt;br /&gt;
		entity = item            -- &amp;quot;item&amp;quot; must be the entity&lt;br /&gt;
		item   = entity.id       -- look-up q-code&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	-- build language fallback list&lt;br /&gt;
	lang = string.lower(lang) or 'en'&lt;br /&gt;
	local langList = mw.language.getFallbacksFor(lang)&lt;br /&gt;
	table.insert(langList, 1, lang)&lt;br /&gt;
	&lt;br /&gt;
	-- get label (visible part of the link)&lt;br /&gt;
	for _, language in ipairs(langList) do  -- loop over language fallback list looking for label in the specific language&lt;br /&gt;
		if entity then&lt;br /&gt;
			label = entity:getLabel(language)&lt;br /&gt;
		else&lt;br /&gt;
			label = mw.wikibase.getLabelByLang(item, language)&lt;br /&gt;
		end&lt;br /&gt;
		if label then break end                    -- label found and we are done&lt;br /&gt;
	end&lt;br /&gt;
	if not label then                                  -- no labels found, so just show the q-code&lt;br /&gt;
		label = item&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	-- change capitalization of the label&lt;br /&gt;
	if capitalization=='ucfirst' then&lt;br /&gt;
		label = mw.language.new(lang):ucfirst(label)&lt;br /&gt;
	elseif capitalization=='lcfirst' then&lt;br /&gt;
		label = mw.language.new(lang):lcfirst(label)&lt;br /&gt;
	elseif capitalization=='uc' then&lt;br /&gt;
		label = mw.language.new(lang):uc(label)&lt;br /&gt;
	elseif capitalization=='lc' then&lt;br /&gt;
		label = mw.language.new(lang):lc(label)&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	-- create URL part of the link&lt;br /&gt;
	link_type = string.lower(link_type or '')&lt;br /&gt;
	local dLink = 'd:'..item; -- create fallback wikidata link&lt;br /&gt;
	if string.upper(string.sub(item, 1, 1)) == 'P' then&lt;br /&gt;
		dLink = 'd:Property:'.. item&lt;br /&gt;
	end&lt;br /&gt;
	if link_type=='-' then -- allow different link formats&lt;br /&gt;
		link = ''            -- no link&lt;br /&gt;
	elseif link_type=='wikidata'  then&lt;br /&gt;
		link = dLink         -- link to wikidata&lt;br /&gt;
	elseif link_type=='commons' then&lt;br /&gt;
		link = getSitelink(item, entity, 'commons')  -- look for sitelink to commons&lt;br /&gt;
		if link then &lt;br /&gt;
			link = 'c:'..link&lt;br /&gt;
		else -- try linking to P373 category&lt;br /&gt;
			entity = entity or mw.wikibase.getEntity(item);&lt;br /&gt;
			assert(entity, &amp;quot;Item ID &amp;quot; .. item .. &amp;quot; is not valid&amp;quot;)&lt;br /&gt;
			s = entity:getBestStatements( 'P373' )&lt;br /&gt;
			if s[1] and s[1].mainsnak.datavalue.value then &lt;br /&gt;
				 link = 'c:Category:' .. s[1].mainsnak.datavalue.value&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
		if not link then -- try linking to P935 gallery&lt;br /&gt;
			s = entity:getBestStatements( 'P935' )&lt;br /&gt;
			if s[1] then &lt;br /&gt;
				 link = s[1].mainsnak.datavalue.value&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	if not link then-- apply default &amp;quot;Wikipedia&amp;quot; link type&lt;br /&gt;
		for _, language in ipairs(langList) do &lt;br /&gt;
			local sitelink = getSitelink(item, entity, language)&lt;br /&gt;
			if sitelink then &lt;br /&gt;
				link = mw.ustring.format('w:%s:%s', language, sitelink)&lt;br /&gt;
				break &lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	link = link or dLink  -- no wiki sitelink, so link to wikidata&lt;br /&gt;
	&lt;br /&gt;
	-- return the results&lt;br /&gt;
	--if entity then label = label .. '_'	end -- this line is uncommented if it is needed for debugging&lt;br /&gt;
	if link~='' then&lt;br /&gt;
		return mw.ustring.format('[[%s|%s]]', link, label) -- return link&lt;br /&gt;
	else&lt;br /&gt;
		return label -- return just a label&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--[[&lt;br /&gt;
getLabel&lt;br /&gt;
 &lt;br /&gt;
This function returns a label translated to desired language, created based on wikidata&lt;br /&gt;
&lt;br /&gt;
Usage:&lt;br /&gt;
{{#invoke:Wikidata label|getLabel|item=Q...|lang=..|link_style=..|capitalization=..}}&lt;br /&gt;
&lt;br /&gt;
Parameters&lt;br /&gt;
	1: wikidata's item's q-code (required)&lt;br /&gt;
	2: language (optional; default {{int:lang}} )&lt;br /&gt;
	3: link_style: &amp;quot;wikipedia&amp;quot; (default), &amp;quot;Wikidata&amp;quot;, &amp;quot;Commons&amp;quot;, or &amp;quot;-&amp;quot; (no link)&lt;br /&gt;
	4: capitalization - can be &amp;quot;uc&amp;quot;, &amp;quot;lc&amp;quot;, &amp;quot;ucfirst&amp;quot;, &amp;quot;lcfirst&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Error Handling:&lt;br /&gt;
	Bad q-code will result in displayed error&lt;br /&gt;
]]&lt;br /&gt;
function p.getLabel(frame)&lt;br /&gt;
	local args = frame.args&lt;br /&gt;
	if not (args.lang and mw.language.isSupportedLanguage(args.lang)) then &lt;br /&gt;
		args.lang = frame:callParserFunction( &amp;quot;int&amp;quot;, &amp;quot;lang&amp;quot; ) -- get user's chosen language &lt;br /&gt;
	end&lt;br /&gt;
	if (not args.link) or (mw.text.trim(args.link) == &amp;quot;&amp;quot;) then&lt;br /&gt;
		args.link = &amp;quot;wikipedia&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
	if (not args.capitalization) or (mw.text.trim(args.capitalization) == &amp;quot;&amp;quot;) then&lt;br /&gt;
		args.capitalization = &amp;quot;none&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
	args.item = mw.text.trim(args.item or '')&lt;br /&gt;
	return p._getLabel(args.item, args.lang, args.link, args.capitalization)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--[[&lt;br /&gt;
_sitelinks&lt;br /&gt;
&lt;br /&gt;
This function returns a table of sitelinks for a single project organized by language&lt;br /&gt;
&lt;br /&gt;
Inputs:&lt;br /&gt;
	1: item - wikidata's item's q-code or entity class&lt;br /&gt;
	2: project - &amp;quot;wikipedia&amp;quot;, &amp;quot;wikisource&amp;quot;, &amp;quot;wikiquote&amp;quot;, &amp;quot;wikibooks&amp;quot;, &amp;quot;wikinews&amp;quot;, &amp;quot;wikiversity&amp;quot;, &lt;br /&gt;
			&amp;quot;wikivoyage&amp;quot;, &amp;quot;wiktionary&amp;quot;, &amp;quot;commons&amp;quot;, &amp;quot;mediawiki&amp;quot;, &amp;quot;wikispecies&amp;quot;, &amp;quot;wikidata&amp;quot;,  etc.&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
	Table with language fields&lt;br /&gt;
]]&lt;br /&gt;
function p._sitelinks( item, project )&lt;br /&gt;
	local entity, sitelink&lt;br /&gt;
	-- get entity&lt;br /&gt;
	if type(item)=='string' then -- &amp;quot;item&amp;quot; is a q-code&lt;br /&gt;
		entity = mw.wikibase.getEntity(item); &lt;br /&gt;
	else&lt;br /&gt;
		entity = item            -- &amp;quot;item&amp;quot; is the entity&lt;br /&gt;
		item   = entity.id       -- look-up q-code&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	-- get project code&lt;br /&gt;
	local LUT = {wikipedia='wiki', commons='commonswiki', mediawiki='mediawikiwiki', wikispecies='specieswiki', wikidata='wikidatawiki'}&lt;br /&gt;
	project = string.lower(project)&lt;br /&gt;
	if LUT[project] then -- correct the project name&lt;br /&gt;
		project=LUT[project]&lt;br /&gt;
	end&lt;br /&gt;
	local n = string.len(project);&lt;br /&gt;
	local s ={}&lt;br /&gt;
	if entity and entity.sitelinks then 					  -- See if entity exists, and that it has sitelinks&lt;br /&gt;
		for _, sitelink in pairs(entity.sitelinks) do 				-- loop over all sitelinks&lt;br /&gt;
		  local site = sitelink.site&lt;br /&gt;
			local lang = mw.ustring.sub( site, 1, mw.ustring.len(site) - n  )  -- language part of the siteID&lt;br /&gt;
			local proj = mw.ustring.sub( site, mw.ustring.len(site) - n + 1 )  -- project part of the siteID&lt;br /&gt;
			if proj == project then -- proj matches desired &amp;quot;project&amp;quot;&lt;br /&gt;
				s[lang] = sitelink.title&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	return s&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--[[&lt;br /&gt;
sitelinks&lt;br /&gt;
&lt;br /&gt;
This function returns a comma separated list of sitelinks for a single project organized by language&lt;br /&gt;
Its main purpose is to help with testing of _sitelinks function.&lt;br /&gt;
&lt;br /&gt;
Usage:&lt;br /&gt;
{{#invoke:Wikidata label|sitelinks|item=Q...|project=..}}&lt;br /&gt;
&lt;br /&gt;
Inputs:&lt;br /&gt;
	1: item - wikidata's item's q-code or entity class&lt;br /&gt;
	2: project - &amp;quot;wikipedia&amp;quot; (or &amp;quot;wiki&amp;quot;), &amp;quot;wikisource&amp;quot;, &amp;quot;wikiquote&amp;quot;, &amp;quot;wikibooks&amp;quot;, &amp;quot;wikinews&amp;quot;, &amp;quot;wikiversity&amp;quot;, &amp;quot;wikivoyage&amp;quot;, &amp;quot;wiktionary&amp;quot;, etc.&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
	comma separated list&lt;br /&gt;
]]&lt;br /&gt;
function p.sitelinks(frame)&lt;br /&gt;
	local sitelinks = p._sitelinks(frame.args.item, frame.args.project)&lt;br /&gt;
	local s = {}&lt;br /&gt;
	for i, j in pairs(sitelinks) do&lt;br /&gt;
		table.insert(s, i .. ':' .. j)&lt;br /&gt;
	end&lt;br /&gt;
	return table.concat(s, ', ')	&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--[[&lt;br /&gt;
_aliases&lt;br /&gt;
&lt;br /&gt;
This function returns a table of aliases for a single language&lt;br /&gt;
&lt;br /&gt;
Inputs:&lt;br /&gt;
	1: item - wikidata's item's q-code or entity class&lt;br /&gt;
	2: lang - language code, like 'en' or 'de'&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
	Table of aliases&lt;br /&gt;
]]&lt;br /&gt;
function p._aliases( item, lang )&lt;br /&gt;
	local entity&lt;br /&gt;
	if type(item)=='string' then -- &amp;quot;item&amp;quot; is a q-code&lt;br /&gt;
		entity = mw.wikibase.getEntity(item); &lt;br /&gt;
	else&lt;br /&gt;
		entity = item            -- &amp;quot;item&amp;quot; is the entity&lt;br /&gt;
		item   = entity.id       -- look-up q-code&lt;br /&gt;
	end&lt;br /&gt;
	local s = {}&lt;br /&gt;
	if entity and entity.aliases then						-- See if there is an entity and that is has aliases&lt;br /&gt;
		if entity.aliases[lang] then						-- See if it has English Aliases&lt;br /&gt;
			for i, j in pairs(entity.aliases[lang]) do		-- Make a loop around the English aliases&lt;br /&gt;
				table.insert(s, j.value)					-- Create a table of English aliases&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	return s&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--[[&lt;br /&gt;
aliases&lt;br /&gt;
&lt;br /&gt;
This function returns a comma separated list of aliases for a single language&lt;br /&gt;
Its main purpose is to help with testing of _aliases function.&lt;br /&gt;
&lt;br /&gt;
Usage:&lt;br /&gt;
{{#invoke:Wikidata label|aliases|item=Q...|lang=..}}&lt;br /&gt;
&lt;br /&gt;
Inputs:&lt;br /&gt;
	1: item - wikidata's item's q-code or entity class&lt;br /&gt;
	2: lang - language code, like 'en' or 'de'&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
	Comma separated list of aliases&lt;br /&gt;
]]&lt;br /&gt;
function p.aliases(frame)&lt;br /&gt;
	return table.concat(p._aliases(frame.args.item, frame.args.lang), ', ')	&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
return p&lt;/div&gt;</summary>
		<author><name>Jacques Ducloy</name></author>
		
	</entry>
</feed>