Retrieving text from Wikidata and formatting it in wiki-linked style. Algorithm of this module is as follows.

UsageEdit

{{#invoke:Sandbox/Was a bee/getLinkedName|getLinkedName|QID}}

ExamplesEdit

Case 1: Sitelink existsEdit

Input

{{#invoke:Sandbox/Was a bee/getLinkedName|getLinkedName|Q42}}

Output

Douglas Adams


Case 2: Sitelink exists but which has disambiguation bracketsEdit

Input

{{#invoke:Sandbox/Was a bee/getLinkedName|getLinkedName|Q6258348}}

Output

John Smith

Case 3: Sitelink doesn't exist but label existsEdit

Input

{{#invoke:Sandbox/Was a bee/getLinkedName|getLinkedName|Q23366230}}

Output

Lua error at line 33: attempt to index local 'entity' (a nil value).


Case 4: Both sitelink and label doesn't existEdit

Input

{{#invoke:Sandbox/Was a bee/getLinkedName|getLinkedName|Q11485627}}

Output

Hirose Shrine

ParametersEdit

aliasEdit

|alias= parameter specifies text shown as alias without changing the link target.

Input

{{#invoke:Sandbox/Was a bee/getLinkedName|getLinkedName|Q30}}

{{#invoke:Sandbox/Was a bee/getLinkedName|getLinkedName|Q30|alias=US}}

Output

United States of America

United States of America



local p = {}

function p.rmvDisambigBracket ( text )
	text = string.gsub( text, '%s%(.+%)$', '' )
	return text
end

function p.getLinkedName( frame )
    local id = frame.args[1]
	local alias = frame.args['alias']
	
	if id == nil or id == '' then
		--If QID is not defined, using the QID of the item which is connected to the current page.
		id = mw.wikibase.getEntityIdForCurrentPage() 
		if not id then
			return 'No item passed as parameter' --If there is no connected wikidata page, abort.
		end
    end
	
	if tonumber(id) then --legacy format
		id = 'Q'.. tostring(id)
	end
	
    local lang = mw.language.getContentLanguage().code
	local globalSiteId = lang .. 'wiki'
	
	local entity = mw.wikibase.getEntity( id )
	
	local content
	local currentText = nil
	local currentText_NoBracket = nil
	
	currentText = entity:getSitelink( globalSiteId )	
	if currentText ~= nil then
		if alias ~= nil and alias ~= '' then
			currentText_NoBracket = alias
		else
			currentText_NoBracket = p.rmvDisambigBracket(currentText)
		end
		content = '[[:' .. lang .. ':' .. currentText .. '|' .. currentText_NoBracket .. ']]'
		return content ---- 1. [[:en:Sitelink|Sitelink]] style 
	else
		currentText = entity:getLabelWithLang(lang)
		if currentText ~= nil then
			content = '[[:' .. lang .. ':' .. currentText .. '|' .. currentText .. ']]'
			return content ---- 2. [[:en:Label|Label]] style
		else
			content = id .. ' ' .. '[[File:Blue pencil.svg|frameless|text-top|10px|link=https://www.wikidata.org/wiki/' .. id .. ']]'
			return content ---- 3. Q123456 style
		end
	end
	
end

return p