Module:Wikidata link

local p = {}

-- Get a link to the given Wikidata item's page on the first of the following places: -- 1. Wikisource -- 2. Wikipedia -- 3. Commons -- 4. Reasonator function p.link(frame) -- Check input. if frame.args.wikidata == nil or frame.args.wikidata == '' then return " Please specify 'wikidata' parameter. " end local itemId = frame.args.wikidata if not mw.wikibase.isValidEntityId(itemId) then return " " .. itemId .. "' is not a valid Wikidata item. " end if not mw.wikibase.entityExists(itemId) then return " " .. itemId .. "' does not exist on Wikidata. " end local label = mw.wikibase.getLabel(itemId) if frame.args.label ~= nil and frame.args.label ~= '' then label = frame.args.label end

-- Look through the site hierarchy for a matching sitelink. -- These two variables are in the same order. local sitelinks = {'enwikisource', 'enwiki', 'commonswiki'} local interwikis = {'', 'wikipedia', 'commons'} for i = 1, #sitelinks do		local sitelink = mw.wikibase.getSitelink(itemId, sitelinks[i]) if sitelink then return ' ' .. label .. ' '; end end

-- Fall back on Reasonator if no sitelink found. return ' [https://reasonator.toolforge.org/?q=' .. itemId .. ' ' .. label .. '] ' end

return p