Documentation for this module may be created at ਮੌਡਿਊਲ:Wikidata link/doc

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 "<span class='error'>Please specify 'wikidata' parameter.</span>"
	end
	local itemId = frame.args.wikidata
	local item = mw.wikibase.getEntity( itemId )
	if item == nil then
		return "<span class='error'>Unable to load Wikidata item '" .. itemId .. "'.</span>"
	end
	local label = 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.
	local sitelinks = {'pawikisource', 'pawiki', 'enwiki', 'enwikisource'}
	local interwikis = {'', ':pa:w', ':en:w',  ':en:'}
	for i = 1, #sitelinks do
		local sitelink = mw.wikibase.getSitelink(itemId, sitelinks[i])
		if sitelink then
			return '<span class="module-wikidata-link">[[' .. interwikis[i] .. ':' .. sitelink .. '|' .. label .. ']]</span>';
		end
	end

	-- Fall back on Reasonator if no sitelink found.
	return '<span class="plainlinks module-wikidata-link reasonator-link">[https://reasonator.toolforge.org/?q=' .. itemId .. '&lang=bn ' .. label .. ']</span>'
end

return p