Naar inhoud springen

Module:Kijkwijzer

Uit Wikipedia, de vrije encyclopedie
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 ratingItems = {
	'Q23649980', -- AL
	'Q23649981', --  6
	'Q23649982', --  9
	'Q23649983', -- 12
	'Q83154238', -- 14
	'Q23649984', -- 16
	'Q83154241', -- 18
	'Q98034437', -- Angst
	'Q98034451', -- Discriminatie
	'Q98038572', -- Roken, alcohol en drugs
	'Q125165898',-- Gevaarlijke challenges of stunts
	'Q98034423', -- Geweld
	'Q98042270', -- Grof taalgebruik
	'Q98040930', -- Seks
}

local itemId2infix = {
	Q23649980='AL',
	Q23649981='6',
	Q23649982='9',
	Q23649983='12',
	Q83154238='14',
	Q23649984='16',
	Q83154241='18'
}

local function icon(qid, halfOpacity)
	local infix = itemId2infix[qid]
	if not infix then
		mw.log('icon: not found: ', qid)
		return ''
	end
	local 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

local function listItem(qid, halfOpacity)
	local r = '<span class="KwCd'
	if halfOpacity then
		r = r .. ' opacity-half'
	end
	r = r .. '"> '
	r = r .. mw.wikibase.getEntity(qid):getLabel()
	r = r .. '</span>'
	return r
end

-- Fetches an array of Kijkwijzer rating claims from Wikidata and
--   generates a series of age classification icons and
--   content descriptor labels.
-- returns '' if no entity found
local function getText(qid)
	local entity = mw.wikibase.getEntity(qid)
	if not entity then
		return ''
	end
	qid = entity.id
	local ratingCount = 0	-- number of statements
	local cdCount = 0		-- number of content descriptors
	local acCounts = {}	-- key: item ID of age classification, value: count
	local cdCounts = {}	-- key: item ID of content descriptor, value: count
	for _,v in ipairs(ratingItems) do
		acCounts[v] = 0
		cdCounts[v] = 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
			acCounts[ratingId] = acCounts[ratingId] + 1
		end
		if s.qualifiers and s.qualifiers['P7367'] then
			for k,v in pairs(s.qualifiers['P7367']) do
				if v.snaktype == "value" then
					local cdId = v.datavalue.value.id
					if cdCounts[cdId] then
						cdCounts[cdId] = cdCounts[cdId] + 1
						cdCount = cdCount + 1
					else
						mw.log('fetchRating: ignoring: ', cdId)
					end
				end
			end
		end
	end
	local r = ''
	for k,v in pairs(acCounts) do
		if v > 0 then
			mw.log('fetchRating: acCounts: ', k, '=', v)
			local halfOpacity = v < ratingCount
			r = r .. icon(k, halfOpacity)
		end
	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
	if cdCount > 0 then
		r = '<templatestyles src="Module:Kijkwijzer/styles.css" />' .. r
		r = r .. '<div class="KwCd-list">'
		for k,v in pairs(cdCounts) do
			if v > 0 then
				mw.log('fetchRating: cdCounts: ', k, '=', v)
				local halfOpacity = v < ratingCount
				r = r .. listItem(k, halfOpacity)
			end
		end
		r = r .. '</div>'
	end
	return r
end

function p.main(frame)
	return frame:preprocess(getText())
end

return p