Difference between revisions of "Module:PrevalenceData"

From blackwiki
Jump to navigation Jump to search
blackwiki>Pppery
(Remove commented-out code, comments at top (moved to doc page), and "untested" warning (it's used in a high-risk template. Does that not count as "tested" enought?))
blackwiki>RexxS
(sync from sandbox)
Line 2: Line 2:
  
 
p.main = function ( frame )
 
p.main = function ( frame )
local entity = mw.wikibase.getEntity( frame.args.qId or nil )
+
local qid = frame.args.qId
if entity then
+
if qid == "" then qid = nil end
local prevalenceClaims = entity.claims and entity.claims.P1193
+
local prevalenceClaims = mw.wikibase.getBestStatements(qid, "P1193")
-- TODO: Get best claim, not always just the first one.
+
local pRange = ''
-- Probably use getBestStatements.
+
-- Run through all prevalence claims - the table prevalenceClaims always exists but may be empty
if prevalenceClaims then
+
for i, prevalenceClaim in ipairs( prevalenceClaims ) do
local pRange = ''
+
local prevalenceValue = prevalenceClaim.mainsnak.datavalue and prevalenceClaim.mainsnak.datavalue.value
-- Run through all prevalence claims
+
if prevalenceValue then
for i, prevalenceClaim in pairs( prevalenceClaims ) do
+
if string.len( pRange ) > 0 then
local prevalenceValue = prevalenceClaim.mainsnak.datavalue.value
+
-- Split multiple claims
if prevalenceValue then
+
-- Maybe line break instead?
if string.len( pRange ) > 0 then
+
pRange = pRange .. ', '
-- Split multiple claims
+
end
-- Maybe line break instead?
+
if prevalenceValue.lowerBound and prevalenceValue.upperBound then
pRange = pRange .. ', '
+
local lowerBound = prevalenceValue.lowerBound * 100
end
+
local upperBound = prevalenceValue.upperBound * 100
local lowerBound = prevalenceValue.lowerBound * 100
+
pRange = pRange .. lowerBound
local upperBound = prevalenceValue.upperBound * 100
+
if lowerBound ~= upperBound then
pRange = pRange .. lowerBound
+
pRange = pRange .. '' .. upperBound
if lowerBound ~= upperBound then
+
end
pRange = pRange .. ' to ' .. upperBound
+
else
end
+
local amount = prevalenceValue.amount * 100
+
pRange = pRange .. amount
pRange = pRange .. '%'
+
end
if prevalenceClaim.qualifiers then
+
pRange = pRange .. '%'
-- Qualifiers for prevalence are currently unstandardized.
+
if prevalenceClaim.qualifiers then
-- Keep guessing until the right one is found.
+
-- Qualifiers for prevalence are currently unstandardized.
local quals = prevalenceClaim.qualifiers.P276 or -- location
+
-- Keep guessing until the right one is found.
prevalenceClaim.qualifiers.P1001 or          -- applies to jurisdiction
+
local quals = prevalenceClaim.qualifiers.P276 or -- location
prevalenceClaim.qualifiers.P17              -- country
+
prevalenceClaim.qualifiers.P1001 or          -- applies to jurisdiction
if quals then
+
prevalenceClaim.qualifiers.P17              -- country
pRange = pRange .. ' ('
+
if quals then
for k, qual in pairs(quals) do
+
pRange = pRange .. ' ('
if k > 1 then
+
for k, qual in pairs(quals) do
pRange = pRange .. ', '
+
if k > 1 then
end
+
pRange = pRange .. ', '
local qualId = qual.datavalue.value[ 'numeric-id' ]
+
end
local link = mw.wikibase.sitelink( 'Q' .. qualId )
+
local qualId = qual.datavalue.value[ 'numeric-id' ]
local label = ({
+
local link = mw.wikibase.sitelink( 'Q' .. qualId )
-- Certain geographic locales might need a
+
local label = ({
-- manual-ish override for labels.  
+
-- Certain geographic locales might need a
[ 132453 ] = 'developed world'
+
-- manual-ish override for labels.  
})[ qualId ] or mw.wikibase.label( 'Q' .. qualId )
+
[ 132453 ] = 'developed world'
if link then
+
})[ qualId ] or mw.wikibase.label( 'Q' .. qualId )
label = '[[' .. link .. '|' .. label .. ']]'
+
if link then
end
+
label = '[[' .. link .. '|' .. label .. ']]'
pRange = pRange .. label
 
 
end
 
pRange = pRange .. ')'
 
 
end
 
end
 +
pRange = pRange .. label
 
end
 
end
 +
pRange = pRange .. ')'
 
end
 
end
--[[
 
-- Todo: References
 
if prevalenceClaim.references then
 
 
end
 
]]--
 
 
 
end
 
end
return pRange
 
 
end
 
end
 +
--[[ Todo: References
 +
if prevalenceClaim.references then
 +
end
 +
--]]
 
end
 
end
return ''
+
return pRange
 
end
 
end
  
 
return p
 
return p

Revision as of 18:29, 24 May 2020

This module is specifically for the Wikidata property Template:Wikidata property link, due to its particular need for ranges and area-based qualifiers and the lack of support for these in the main Wikidata module. It is used by Template:infobox medical condition.


local p = {}

p.main = function ( frame )
	local qid = frame.args.qId
	if qid == "" then qid = nil end
	local prevalenceClaims = mw.wikibase.getBestStatements(qid, "P1193")
	local pRange = ''
	-- Run through all prevalence claims - the table prevalenceClaims always exists but may be empty
	for i, prevalenceClaim in ipairs( prevalenceClaims ) do
		local prevalenceValue = prevalenceClaim.mainsnak.datavalue and prevalenceClaim.mainsnak.datavalue.value
		if prevalenceValue then
			if string.len( pRange ) > 0 then
				-- Split multiple claims
				-- Maybe line break instead?
				pRange = pRange .. ', '
			end
			if prevalenceValue.lowerBound and prevalenceValue.upperBound then
				local lowerBound = prevalenceValue.lowerBound * 100
				local upperBound = prevalenceValue.upperBound * 100
				pRange = pRange .. lowerBound
				if lowerBound ~= upperBound then
					pRange = pRange .. '—' .. upperBound
				end
			else
				local amount = prevalenceValue.amount * 100
				pRange = pRange .. amount
			end
			pRange = pRange .. '%'
			if prevalenceClaim.qualifiers then
				-- Qualifiers for prevalence are currently unstandardized.
				-- Keep guessing until the right one is found.
				local quals = prevalenceClaim.qualifiers.P276 or -- location
					prevalenceClaim.qualifiers.P1001 or          -- applies to jurisdiction
					prevalenceClaim.qualifiers.P17               -- country
				if quals then
					pRange = pRange .. ' ('
					for k, qual in pairs(quals) do
						if k > 1 then
							pRange = pRange .. ', '
						end
						local qualId = qual.datavalue.value[ 'numeric-id' ]
						local link = mw.wikibase.sitelink( 'Q' .. qualId )
						local label = ({
							-- Certain geographic locales might need a
							-- manual-ish override for labels. 
							[ 132453 ] = 'developed world'
						})[ qualId ] or mw.wikibase.label( 'Q' .. qualId )
						if link then
							label = '[[' .. link .. '|' .. label .. ']]'
						end
						pRange = pRange .. label
					end
					pRange = pRange .. ')'
				end
			end
		end
		--[[ Todo: References
		if prevalenceClaim.references then
		end
		--]]
	end
	return pRange
end

return p