Naar inhoud springen

Module:Kijkwijzer

Uit Wikipedia, de vrije encyclopedie
Dit is een oude versie van deze pagina, bewerkt door Bdijkstra (overleg | bijdragen) op 27 nov 2024 om 19:38. (bewerken hoeft niet)
Deze versie kan sterk verschillen van de huidige versie van deze pagina.
Moduledocumentatie​[bekijk] [bewerk] [ververs] [geschiedenis]

Voor het weergeven van Kijkwijzer-classificaties via Wikidata (Kijkwijzer-classificatie (P2684)).

Wordt gebruikt door Sjabloon:Infobox film en Sjabloon:Infobox televisieprogramma.

Maakt gebruik van Module:Kijkwijzer/styles.css.

local p = {}

local itemId2infix = {
	Q23649980='AL',
	Q23649981='6',
	Q23649982='9',
	Q23649983='12',
	Q83154238='14',
	Q23649984='16',
	Q83154241='18',
	Q98034437='Angst',
	Q98034451='Discriminatie',
	Q98038572='Drugs en-of alcoholmisbruik', -- Roken, alcohol en drugs
	Q125165898='Gevaarlijk Gedrag',			 -- Gevaarlijke challenges of stunts
	Q98034423='Geweld',
	Q98042270='Grof taalgebruik',
	Q98040930='Seks'
}

local function icon(qid, halfOpacity)
	local infix = itemId2infix[qid]
	if not infix then
		wd.log('icon: not found: ', qid)
		return ''
	end
	r = '[[File:Kijkwijzer ' .. infix .. '.svg|25px|class=nopageimage skin-invert-image noviewer'
	if halfOpacity then
		r = r .. ' opacity-half'
	end
	r = r .. ']]'
	return r
end

-- Fetches an array of Kijkwijzer rating claims from Wikidata and
--   generates a series of icons.
-- returns '' if no entity found
local function getIcons(qid)
	local entity = mw.wikibase.getEntity(qid)
	if not entity then
		return ''
	end
	qid = entity.id
	local ratingCount = 0
	local counts1 = {}
	local counts2 = {}
	for k,v in pairs(itemId2infix) do
		counts1[k] = 0
		counts2[k] = 0
	end
	
	for _, s in pairs(entity:getBestStatements('P2684')) do
		if s.mainsnak.snaktype == "value" then
			ratingCount = ratingCount + 1
			local ratingId = s.mainsnak.datavalue.value.id
			counts1[ratingId] = counts1[ratingId] + 1
		end
		if s.qualifiers then
			if s.qualifiers['P7367'] then
				for k,v in pairs(s.qualifiers['P7367']) do
					if v.snaktype == "value" then
						local cdId = v.datavalue.value.id
						counts2[cdId] = counts2[cdId] + 1
					end
				end
			end
		end
	end
	local needCss = false
	local r = ''
	for k,v in pairs(counts1) do
		if v > 0 then
			mw.log('fetchRating: counts1: ', k, '=', v)
			local halfOpacity = v < ratingCount
			needCss = needCss or halfOpacity
			r = r .. icon(k, halfOpacity)
		end
	end
	for k,v in pairs(counts2) do
		if v > 0 then
			mw.log('fetchRating: counts2: ', k, '=', v)
			local halfOpacity = v < ratingCount
			needCss = needCss or halfOpacity
			r = r .. icon(k, halfOpacity)
		end
	end
	if needCss then
		r = '<templatestyles src="Module:Kijkwijzer/styles.css" />' .. r
	end
	if #r > 0 then
		r = r .. '[[File:Blue pencil.svg |right |10px |link=https://www.wikidata.org/wiki/' .. qid .. '#P2684 |Bekijk/bewerk dit op Wikidata]]'
	end
	return r
end

function p.icons(frame)
	return frame:preprocess(getIcons())
end

return p