Moduuli:WLM-upload-button-categories

Wikipediasta
Siirry navigaatioon Siirry hakuun

Tämän moduulin ohjeistuksen voi tehdä sivulle Moduuli:WLM-upload-button-categories/ohje

local commonscat = {}

-- Function tries to get items P373 value from wikidata
-- If P373 doesn't exist then it loops through properties
-- and tries to get their P373 values.

function commonscat.get(frame)
	local id= frame.args['wikidata']
	local conjtype= frame.args['conjtype']
	local catlist={}
	local targetproperties={"P31", "P131", "P84", "P361", "P1435", "P276", "P706", "P17"}
	local skip_P17 = {"P131", "P361", "P276", "P706"}

	if conjtype == nil or conjtype=="" then
		conjtype="|"
	end

	if id == nil or id == "" then
		entity = mw.wikibase.getEntityObject()
	else
		entity =  mw.wikibase.getEntityObject(id);
	end
	
	if not entity or not entity.claims then
		return nil
	end	
	
	-- use direct P373 value if it exists
	if entity.claims["P373"] then
		for i, j in pairs(entity.claims["P373"]) do
			catlist[j["mainsnak"]["datavalue"]["value"]]=true
		end
	else
		-- if P373 not exists then loop selected properties use their P373 values
		for n, property in pairs(targetproperties) do
			if (entity.claims[property]) then
				for p, claim in pairs(entity.claims[property]) do
					if claim["mainsnak"]["datatype"]=="wikibase-item" then
						tmp_id=claim["mainsnak"]["datavalue"]["value"]["id"]
						claim_entity =  mw.wikibase.getEntityObject(tmp_id);
						if claim_entity and claim_entity.claims and claim_entity.claims["P373"] then
							for i, j in pairs(claim_entity.claims["P373"]) do
								catlist[j["mainsnak"]["datavalue"]["value"]]=property
							end
						end
					end
				end
			end
		end
	end
	
	out={}
	local skip=false
	
	-- Filter out P17 (country) category if more detailed location category is known
	for i,j in pairs(catlist) do
		for k,l in pairs(skip_P17) do
			if j==l then
				skip=true
			end
		end
	end

	for i,j in pairs(catlist) do
		if not (j=="P17" and skip) then 
			table.insert(out,i)
		end
	end
	
	return table.concat(out, conjtype) ;
end 
return commonscat