:Module:Sandbox/Was a bee/getLinkedName

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 = '' .. currentText_NoBracket .. ''

return content ---- 1. Sitelink style

else

currentText = entity:getLabelWithLang(lang)

if currentText ~= nil then

content = '' .. currentText .. ''

return content ---- 2. Label style

else

content = id .. ' ' .. 'File:Blue pencil.svg'

return content ---- 3. Q123456 style

end

end

end

return p