Difference between revisions of "Module:PrevalenceData"

From blackwiki
Jump to navigation Jump to search
blackwiki>Yair rand
blackwiki>Yair rand
Line 15: Line 15:
 
-- TODO: Get best claim, not always just the first one.
 
-- TODO: Get best claim, not always just the first one.
 
-- Probably use getBestStatements.
 
-- Probably use getBestStatements.
local prevalenceClaim = prevalenceClaims and prevalenceClaims[ 1 ]
+
if prevalenceClaims then
if prevalenceClaim then
+
local pRange = ''
local prevalenceValue = prevalenceClaim.mainsnak.datavalue.value
+
-- Run through all prevalence claims
if prevalenceValue then
+
for i, prevalenceClaim in pairs( prevalenceClaims ) do
local lowerBound = prevalenceValue.lowerBound * 100
+
local prevalenceValue = prevalenceClaim.mainsnak.datavalue.value
local upperBound = prevalenceValue.upperBound * 100
+
if prevalenceValue then
local pRange = lowerBound
+
if string.len( pRange ) > 0 then
if lowerBound ~= upperBound then
+
-- Split multiple claims
pRange = pRange .. ' to ' .. upperBound
+
-- Maybe line break instead?
end
+
pRange = pRange .. ', '
+
end
pRange = pRange .. '%'
+
local lowerBound = prevalenceValue.lowerBound * 100
if prevalenceClaim.qualifiers then
+
local upperBound = prevalenceValue.upperBound * 100
-- Qualifiers for prevalence are currently unstandardized.
+
local pRange = pRange .. lowerBound
-- Keep guessing until the right one is found.
+
if lowerBound ~= upperBound then
local quals = prevalenceClaim.qualifiers.P276 or -- location
+
pRange = pRange .. ' to ' .. upperBound
prevalenceClaim.qualifiers.P1001 or          -- applies to jurisdiction
+
end
prevalenceClaim.qualifiers.P17              -- country
+
if quals then
+
pRange = pRange .. '%'
pRange = pRange .. ' ('
+
if prevalenceClaim.qualifiers then
for k, qual in pairs(quals) do
+
-- Qualifiers for prevalence are currently unstandardized.
if k > 1 then
+
-- Keep guessing until the right one is found.
pRange = pRange .. ', '
+
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
 
end
local qualId = qual.datavalue.value[ 'numeric-id' ]
+
pRange = pRange .. ')'
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
 
end
pRange = pRange .. ')'
 
 
end
 
end
 
end
 
end
 
 
--[[
 
--[[
 
-- Todo: References
 
-- Todo: References
Line 63: Line 70:
 
]]--
 
]]--
 
 
return pRange
 
 
end
 
end
 +
return pRange
 
end
 
end
 
end
 
end

Revision as of 22:49, 18 February 2016

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.


-- This module is specifically the Wikidata property "prevalence" (P1193), due
-- to its particular need for ranges and area-based qualifiers, and the lack of
-- support for these in the main Wikidata module.

-- Completely untested.
local p = {}

--local wikidata = require( 'Module:Wikidata' )

p.main = function ( frame )
	--local entity = mw.wikibase.getEntity( 'Q133087' )
	local entity = mw.wikibase.getEntity( frame.args.qId or nil )
	if entity then
		local prevalenceClaims = entity.claims.P1193
		-- TODO: Get best claim, not always just the first one.
		-- Probably use getBestStatements.
		if prevalenceClaims then
			local pRange = ''
			-- Run through all prevalence claims
			for i, prevalenceClaim in pairs( prevalenceClaims ) do
				local prevalenceValue = prevalenceClaim.mainsnak.datavalue.value
				if prevalenceValue then
					if string.len( pRange ) > 0 then
						-- Split multiple claims
						-- Maybe line break instead?
						pRange = pRange .. ', '
					end
					local lowerBound = prevalenceValue.lowerBound * 100
					local upperBound = prevalenceValue.upperBound * 100
					local pRange = pRange .. lowerBound
					if lowerBound ~= upperBound then
						pRange = pRange .. ' to ' .. upperBound
					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
	end
	return ''
end

return p