Documentation for this module may be created at Module:Sandbox/Hike395/CommonsLink/doc

local getArgs = require('Module:Arguments').getArgs
local p = {}

function p.test(frame)
	local args = getArgs(frame)
	return p._test(args)
end

function p._test(args)
	local qid  = mw.wikibase.getEntityIdForTitle(args[1])
	if not qid then return nil end
	local result = '<tr><td>'..args[1]..'</td>'
	local P935 = mw.wikibase.getBestStatements(qid, "P935")[1]
	result = result..'<td>'
	local interesting = false
	local gallery = nil
	if P935 and P935.mainsnak.datavalue then
		gallery = P935.mainsnak.datavalue.value
		result = result..gallery
	end
	if not gallery then
		interesting = true
	end
	result = result..'</td><td>'
	local category = nil
	local P373 = mw.wikibase.getBestStatements(qid, "P373")[1]
	if P373 and P373.mainsnak.datavalue then
		category = 'Category:'..P373.mainsnak.datavalue.value
		result = result..category
	end
	if not category then
		interesting = true
	end
    result = result..'</td><td>'
	local commonsSitelink = mw.wikibase.getSitelink(qid,"commonswiki")
	local sitelinkCat = false
	if commonsSitelink then
		local comparison
		if mw.ustring.sub(commonsSitelink,1,9) == "Category:" then
			comparison = category
			sitelinkCat = true
		else
			comparison = gallery
		end
		if commonsSitelink ~= comparison then
			result = result..'<b>'..commonsSitelink..'</b>'
			interesting = true
        else
			result = result..commonsSitelink
		end
	else
		interesting = true
	end
    result = result..'</td><td>'
	local P910 = mw.wikibase.getBestStatements(qid, "P910")[1]
	if P910 then
		local tmcid = P910.mainsnak.datavalue and P910.mainsnak.datavalue.value.id
		local sitelink = mw.wikibase.getSitelink(tmcid, "commonswiki")
		if sitelink then
			if not sitelinkCat and sitelink ~= category then
				result = result..'<b>'..sitelink..'</b>'
				interesting = true
			else
				result = result..sitelink
			end
		end
	end
	result = result..'</td></tr>'
	if (args.interesting or args.int) and not interesting then
		return nil
	end
	return result
end

return p