Module:Wikidata link

local p = {}

-- Get a link to the given Wikidata item's page on the first of the following places: -- 1. This wiki -- 2. Wikipedia -- 3. Wikisource -- 4. Commons -- 5. Wikidata function p.link( frame ) -- Check input. if frame == nil or frame.args.wikidata == nil or frame.args.wikidata == '' then return " Please specify 'wikidata' parameter. " end local itemId = frame.args.wikidata local item = mw.ext.UnlinkedWikibase.getEntity( itemId ) if not item then return " " .. itemId .. "' could not be found. " end -- Get label. local label = itemId local localTitle = mw.ext.UnlinkedWikibase.getLocalTitle( itemId ) if frame.args.label ~= nil and frame.args.label ~= '' then label = frame.args.label elseif localTitle then label = localTitle.text elseif item.labels.en ~= nil then label = item.labels.en.value end

if localTitle then return ' ' .. label .. ' '; end

-- Look through the site hierarchy for a matching sitelink, in this order. -- The first elements are sitelink IDs and the second are local interwiki prefixes. local sitelinks = { { 'enwiki', 'wikipedia' }, { 'enwikisource', 'wikisource' }, { 'commonswiki', 'commons' }, }	for i,v in ipairs( sitelinks ) do		local sitelink = sitelinks[i][1] local interwiki = sitelinks[i][2] if item.sitelinks[ sitelink ] ~= nil then return ' ' .. label .. ' '; end end

-- Fall back on Wikidata if no sitelink found. return ' ' .. label .. ' ' end

return p