| Alpha | This module is rated as alpha. It is ready for third-party input, and may be used on a few pages to see if problems arise, but should be watched. Suggestions for new features or changes in their input and output mechanisms are welcome. | 
Retrieving text from Wikidata and formatting it in wiki-linked style. Algorithm of this module is as follows.
- Case 1. If there is sitelink, then use sitelink.
- Case 2 If sitelink has disambiguation brackets, then remove the brackets.
- Case 3. If there is no sitelink, then use label.
- Case 4. If there is no sitelink and label, then use item ID (with editing pencil like this "File:Blue pencil.svg").
 
 
 - Case 3. If there is no sitelink, then use label.
 
 - Case 2 If sitelink has disambiguation brackets, then remove the brackets.
 
Usage edit source
{{#invoke:Sandbox/Was a bee/getLinkedName|getLinkedName|QID}}
Examples edit source
Case 1: Sitelink exists edit source
- Input
 
{{#invoke:Sandbox/Was a bee/getLinkedName|getLinkedName|Q42}}
- Output
 
Lua error at line 27: attempt to index field 'wikibase' (a nil value).
Case 2: Sitelink exists but which has disambiguation brackets edit source
- Input
 
{{#invoke:Sandbox/Was a bee/getLinkedName|getLinkedName|Q6258348}}
- Output
 
Lua error at line 27: attempt to index field 'wikibase' (a nil value).
Case 3: Sitelink doesn't exist but label exists edit source
- Input
 
{{#invoke:Sandbox/Was a bee/getLinkedName|getLinkedName|Q23366230}}
- Output
 
Lua error at line 27: attempt to index field 'wikibase' (a nil value).
Case 4: Both sitelink and label doesn't exist edit source
- Input
 
{{#invoke:Sandbox/Was a bee/getLinkedName|getLinkedName|Q11485627}}
- Output
 
Lua error at line 27: attempt to index field 'wikibase' (a nil value).
Parameters edit source
alias edit source
|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
 
Lua error at line 27: attempt to index field 'wikibase' (a nil value).
Lua error at line 27: attempt to index field 'wikibase' (a nil value).
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