Saltar al conteníu

Módulu:WikidataCommonsCat

De Wikipedia


Ver la documentación del módulu en ca:Mòdul:WikidataCommonsCat.


local p = {}

function p.getCommonslink(frame)
	local args = frame.args or frame
	local qid = string.upper(mw.text.trim(args[1] or ""))
	if qid == "" then qid = mw.wikibase.getEntityIdForCurrentPage() end
	if not qid then return nil end
	
	local sitelink_cat
	local sitelink = mw.wikibase.getSitelink(qid, "commonswiki")
	if not sitelink or sitelink:sub(1,9) ~= "Category:" then
		-- check for topic's main category
		local catid
		local prop = mw.wikibase.getBestStatements(qid, "P910")[1]
		if prop then
			catid = prop.mainsnak.datavalue and prop.mainsnak.datavalue.value.id
			sitelink_cat = mw.wikibase.getSitelink(catid, "commonswiki")
		end
		if not sitelink_cat then
			-- check for list's main category
			prop = mw.wikibase.getBestStatements(qid, "P1754")[1]
			if prop then
				catid = prop.mainsnak.datavalue and prop.mainsnak.datavalue.value.id
				sitelink_cat = mw.wikibase.getSitelink(catid, "commonswiki")
			end
			if not sitelink_cat then
				-- check for Commons category (string value)
				prop = mw.wikibase.getBestStatements(qid, "P373")[1]
				if prop then
					sitelink_cat = prop.mainsnak.datavalue and prop.mainsnak.datavalue.value
					if sitelink_cat then
						sitelink_cat = "Category:" .. sitelink_cat
					end
				end
			end
		end
	end
	
	return sitelink_cat or sitelink or args.default
end

return p