Module:Wd

From Outreach Wiki
Jump to navigation Jump to search
local p = {}

State = {}
State.__index = State

-- allows for recursive calls
function State.new()
	local stt = {}
	setmetatable(stt, State)
	
	stt.outPreferred = {}
	stt.outNormal = {}
	stt.outDeprecated = {}
	
	stt.bestRank = true
	stt.foundRank = 3
	stt.maxRank = nil
	stt.minRank = nil
	
	stt.linked = false
	stt.propertyWithQualifier = false
	
	stt.withUnit = false
	stt.shortName = false
	stt.singleValue = false
	
	stt.langCode = mw.language.getContentLanguage().code
	stt.langObj = mw.language.new(stt.langCode)
	
	stt:setRankBoundaries("best")
	
	return stt
end

function State:unknownDatatypeError(type)
	return "<strong class=\"error\">Unknown or unsupported datatype '" .. type .. "'</strong>"
end

function State:parseDate(dateStr, precision)
	precision = precision or "d"
	local i, j, ptr, year, month, day
	
	local from = 1
	
	if dateStr:sub(1,1) == "-" then
		-- this is a negative number, look further ahead
		from = 2
	end
	
	i, j = dateStr:find("-", from)
	
	if i then
		year = tonumber(mw.ustring.gsub(dateStr:sub(1, i-1), "^\+(.+)$", "%1"), 10)
		
		-- apparently, 0 can have a negative sign in Lua... so do this to get rid of it
		if year == 0 then
			year = 0
		end
		
		ptr = i + 1
	else
		year = nil
	end
	
	if precision == "y" then
		return year
	end
	
	i, j = dateStr:find("-", ptr)
	
	if i then
		month = tonumber(dateStr:sub(ptr, i-1), 10)
		ptr = i + 1
	else
		month = nil
	end
	
	if precision == "m" then
		return year, month
	end
	
	i, j = dateStr:find("T", ptr)
	
	if i then
		day = tonumber(dateStr:sub(ptr, i-1), 10)
	else
		day = nil
	end
	
	return year, month, day
end

function State:convertUnit(unit, addLink)
	addLink = addLink or false
	local i, j, itemID, label, target
	
	if unit == "" or unit == "1" then
		return nil
	end
	
	if unit:match('^http[s]?://') then
		i, j = unit:find("Q")
		
		if i then
			itemID = unit:sub(i)
			
			if itemID == "Q11229" then  -- 'percentage'
				return "%"
			else
				label = mw.wikibase.label(itemID)
				target = nil
				
				if addLink or label == nil then
					target = mw.wikibase.sitelink(itemID)
				end
				
				if addLink then
					if target then
						return " " .. "[[" .. target .. "|" .. (label or target) .. "]]"
					end
					
					if not label then
						return " " .. "[[:d:" .. itemID .. "|" .. itemID .. "]]"
					end
				end
				
				return " " .. (label or target or itemID)
			end
		end
	end
	
	return " " .. unit
end

function State:getShortName(itemID)
	return p.property({args={"single", itemID, "P1813"}})  -- 'short name'
end

function State:getOrdinalSuffix(num)
	if tostring(num):sub(-2,-2) == '1' then
		return "th"  -- 10th, 11th, 12th, 13th, ... 19th
	end
	
	num = tostring(num):sub(-1)
	
	if num == '1' then
		return "st"
	elseif num == '2' then
		return "nd"
	elseif num == '3' then
		return "rd"
	else
		return "th"
	end
end

function State:getValue(snak, addUnit, addLink)
	addUnit = addUnit or false
	addLink = addLink or false
	
	if snak.snaktype == 'value' then
		if snak.datavalue.type == 'string' then
			return snak.datavalue.value
		elseif snak.datavalue.type == 'monolingualtext' then
			if snak.datavalue.value['language'] == self.langCode then
				return snak.datavalue.value['text']
			else
				return nil
			end
		elseif snak.datavalue.type == 'quantity' then
			-- strip + signs from front
			local value = mw.ustring.gsub(snak.datavalue.value['amount'], "^\+(.+)$", "%1")
			
			if addUnit then
				local unit = self:convertUnit(snak.datavalue.value['unit'], addLink)
				if unit then
					value = value .. unit
				end
			end
			
			return value
		elseif snak.datavalue.type == 'time' then
			local y, m, d, p, i, j, yDiv, yRound, yFull, value, calendarID
			local yFactor = 1
			local sign = 1
			local suffix = ""
			local mayAddCalendar = false
			local calendar = ""
			local precision = snak.datavalue.value['precision']
			
			if precision == 11 then
				p = "d"
			elseif precision == 10 then
				p = "m"
			else
				p = "y"
				yFactor = 10^(9-precision)
			end
			
			y, m, d = self:parseDate(snak.datavalue.value['time'], p)
			
			if y < 0 then
				sign = -1
				y = y * sign
			end
			
			-- if precision is tens/hundreds/thousands/millions/billions of years
			if precision <= 8 then
				yDiv = y / yFactor
				
				-- if precision is tens/hundreds/thousands of years
				if precision >= 6 then
					-- if unit added then round centuries/millenniums up (e.g. 21st century or 3rd millennium, but 2010s for decade)
					mayAddCalendar = true
					if addUnit then
						if precision <= 7 then
							yRound = math.ceil(yDiv)
							
							if precision == 6 then
								suffix = " millennium"
							else
								suffix = " century"
							end
							
							suffix = self:getOrdinalSuffix(yRound) .. suffix
						else
							yRound = math.floor(yDiv) * yFactor
							suffix = "s"
						end
					else
						yRound = math.floor(yDiv) * yFactor
					end
				else
					local yReFactor, yReDiv, yReRound
					
					-- round to nearest for tens of thousands of years or more
					yRound = math.floor(yDiv + 0.5)
					
					if yRound == 0 then
						if precision <= 2 and y ~= 0 then
							yReFactor = 1e6
							yReDiv = y / yReFactor
							yReRound = math.floor(yReDiv + 0.5)
							
							if yReDiv == yReRound then
								-- change precision to millions of years only if we have a whole number of them
								precision = 3
								yFactor = yReFactor
								yRound = yReRound
							end
						end
						
						if yRound == 0 then
							-- otherwise, take the unrounded (original) number of years
							precision = 5
							yFactor = 1
							yRound = y
							mayAddCalendar = true
						end
					end
					
					if precision >= 1 and y ~= 0 then
						yFull = yRound * yFactor
						
						yReFactor = 1e9
						yReDiv = yFull / yReFactor
						yReRound = math.floor(yReDiv + 0.5)
						
						if yReDiv == yReRound then
							-- change precision to billions of years if we're in that range
							precision = 0
							yFactor = yReFactor
							yRound = yReRound
						else
							yReFactor = 1e6
							yReDiv = yFull / yReFactor
							yReRound = math.floor(yReDiv + 0.5)
							
							if yReDiv == yReRound then
								-- change precision to millions of years if we're in that range
								precision = 3
								yFactor = yReFactor
								yRound = yReRound
							end
						end
					end
					
					if addUnit then
						if precision == 3 then
							suffix = " million years"
						elseif precision == 0 then
							suffix = " billion years"
						else
							yRound = yRound * yFactor
							if yRound == 1 then
								suffix = " year"
							else
								suffix = " years"
							end
						end
					else
						yRound = yRound * yFactor
					end
				end
			else
				yRound = y
				mayAddCalendar = true
			end
			
			if mayAddCalendar then
				calendarID = snak.datavalue.value['calendarmodel']
				
				if calendarID:match('^http[s]?://') then
					i, j = calendarID:find("Q")
					
					if i then
						calendarID = calendarID:sub(i)
						
						if calendarID == "Q1985786" then  -- 'Proleptic Julian calendar'
							if addUnit then
								if addLink then
									calendar = " ([[Julian calendar|Julian]])"
								else
									calendar = " (Julian)"
								end
							else
								calendar = "/Julian"
							end
						end
					end
				end
			end
			
			if addUnit then
				local ce = nil
				
				if sign < 0 then
					ce = "BCE"
				elseif precision <= 5 then
					ce = "CE"
				end
				
				if ce then
					if addLink then
						ce = "[[Common Era|" .. ce .. "]]"
					end
					suffix = suffix .. " " .. ce
				end
				
				value = tostring(yRound)
				
				if m then
					value = self.langObj:formatDate("F", "1-"..m.."-1") .. " " .. value
					
					if d then
						value = d .. " " .. value
					end
				end
				
				value = value .. suffix .. calendar
			else
				value = tostring(yRound * sign)
				
				if m then
					value = value .. "-" .. m
					
					if d then
						value = value .. "-" .. d
					end
				end
				
				value = value .. calendar
			end
			
			return value
		elseif snak.datavalue.type == 'wikibase-entityid' then
			local value = ""
			local target = nil
			local itemID = "Q" .. snak.datavalue.value['numeric-id']
			
			if self.shortName then
				value = self:getShortName(itemID)
			end
			
			if value == "" then
				value = mw.wikibase.label(itemID)
			end
			
			if addLink or value == nil then
				target = mw.wikibase.sitelink(itemID)
			end
			
			if addLink then
				if target then
					value = "[[" .. target .. "|" .. (value or target) .. "]]"
				elseif not value then
					value = "[[:d:" .. itemID .. "|" .. itemID .. "]]"
				end
			elseif not value then
				value = (target or itemID)
			end
			
			return value
		else
			return self:unknownDatatypeError(snak.datavalue.type)
		end
	elseif snak.snaktype == 'somevalue' then
		return "unknown"
	elseif snak.snaktype == 'novalue' then
		return "none"
	else
		return nil
	end
end

function State:getRawValue(snak)
	local temp, value
	if snak.snaktype == 'value' and snak.datavalue.type == 'wikibase-entityid' then
		return "Q" .. snak.datavalue.value['numeric-id']
	else
		return self:getValue(snak, false, false)
	end
end

function State:snakEqualsValue(snak, value)
	if snak.snaktype == 'value' then
		local snakValue = self:getRawValue(snak)
		
		if snakValue and snak.datavalue.type == 'wikibase-entityid' then value = value:upper() end
		
		return snakValue == value
	elseif snak.snaktype == 'somevalue' then
		if value == " " then  -- single space represents 'somevalue'
			return true
		else
			return false
		end
	elseif snak.snaktype == 'novalue' then
		if value == "" then  -- empty value represents 'novalue'
			return true
		else
			return false
		end
	else
		return false
	end
end

function State:setRankBoundaries(rank)
	local rankPos
	
	if (rank == "best") then
		self.bestRank = true
		self.foundRank = 3
		return
	else
		self.bestRank = false
	end
	
	if (rank == "all") then
		self.maxRank = 1
		self.minRank = 3
		return
	end
	
	if (rank:sub(1,9) == "preferred") then
		rankPos = 1
	elseif (rank:sub(1,6) == "normal") then
		rankPos = 2
	elseif (rank:sub(1,10) == "deprecated") then
		rankPos = 3
	end
	
	if (rank:sub(-1) == "+") then
		self.maxRank = 1
		self.minRank = rankPos
	elseif (rank:sub(-1) == "-") then
		self.maxRank = rankPos
		self.minRank = 3
	else
		self.maxRank = rankPos
		self.minRank = rankPos
	end
end

function State:convertRank(rank)
	if (rank == "preferred") then
		return 1
	elseif (rank == "normal") then
		return 2
	elseif (rank == "deprecated") then
		return 3
	else
		return 4  -- default (in its literal sense)
	end
end

function State:rankMatches(rankPos)
	if self.bestRank then
		if self.foundRank > rankPos then
			self.foundRank = rankPos
			
			-- found a better rank, reset worse rank outputs
			if self.foundRank == 1 then
				self.outNormal = {}
				self.outDeprecated = {}
			elseif self.foundRank == 2 then
				self.outDeprecated = {}
			end
		end
		
		return self.foundRank >= rankPos  -- == would also work here
	else
		return (self.maxRank <= rankPos and rankPos <= self.minRank)
	end
end

function State:appendOutput(value, rankPos)
	if rankPos == 1 then
		self.outPreferred[#self.outPreferred + 1] = value
	elseif rankPos == 2 then
		self.outNormal[#self.outNormal + 1] = value
	elseif rankPos == 3 then
		self.outDeprecated[#self.outDeprecated + 1] = value
	end
end

function State:out()
	local out = ""
	
	if self.outDeprecated[1] then
		if self.singleValue then
			out = self.outDeprecated[1]
		else
			out = table.concat(self.outDeprecated, ", ")
		end
	end
	
	if self.outNormal[1] then
		if self.singleValue then
			out = self.outNormal[1]
		else
			if out ~= "" then
				out = "; " .. out
			end
			
			out = table.concat(self.outNormal, ", ") .. out
		end
	end
	
	if self.outPreferred[1] then
		if self.singleValue then
			out = self.outPreferred[1]
		else
			if out ~= "" then
				out = "; " .. out
			end
			
			out = table.concat(self.outPreferred, ", ") .. out
		end
	end
	
	return out
end

function State:processFlag(flag)
	if flag == "linked" then
		self.linked = true
		return true
	elseif flag == "unit" then
		self.withUnit = true
		return true
	elseif flag == "short" then
		self.shortName = true
		return true
	elseif flag == "single" then
		self.singleValue = true
		return true
	elseif flag == "best" or flag == "all" or flag:match('^preferred[+-]?$') or flag:match('^normal[+-]?$') or flag:match('^deprecated[+-]?$') then
		self:setRankBoundaries(flag)
		return true
	else
		return false
	end
end

p.property = function(frame)
	local _ = State.new()
	
	local entity, propertyID, claims, rankPos, value
	local nextArg = mw.text.trim(frame.args[1] or "")
	local nextIndex = 2
	
	while _:processFlag(nextArg) do
		nextArg = mw.text.trim(frame.args[nextIndex] or "")
		nextIndex = nextIndex + 1
	end
	
	if nextArg:sub(1,1):upper() == "Q" then
		entity = mw.wikibase.getEntity(nextArg)
		propertyID = mw.text.trim(frame.args[nextIndex] or "")
	else
		entity = mw.wikibase.getEntity()
		propertyID = nextArg
	end
	
	if entity and entity.claims then claims = entity.claims[propertyID] end
	if claims then
		for k, v in pairs(claims) do
			rankPos = _:convertRank(v.rank)
			if _:rankMatches(rankPos) then
				value = _:getValue(v.mainsnak, _.withUnit, _.linked)
				if value then _:appendOutput(value, rankPos) end
			end
		end
		return _:out()
	else
		return ""
	end
end

p.qualifier = function(frame, _)
	_ = _ or State.new()
	
	local entity, propertyID, propertyValue, qualifierID, claims, qualifiers, rankPos, outValue, outInter, outQualifier
	local nextArg = mw.text.trim(frame.args[1] or "")
	local nextIndex = 2
	
	while _:processFlag(nextArg) do
		nextArg = mw.text.trim(frame.args[nextIndex] or "")
		nextIndex = nextIndex + 1
	end
	
	if nextArg:sub(1,1):upper() == "Q" then
		entity = mw.wikibase.getEntity(nextArg)
		propertyID = mw.text.trim(frame.args[nextIndex] or "")
		nextIndex = nextIndex + 1
	else
		entity = mw.wikibase.getEntity()
		propertyID = nextArg
	end
	
	nextArg = frame.args[nextIndex]
	nextIndex = nextIndex + 1
	
	qualifierID = nextArg
	
	nextArg = mw.text.trim(frame.args[nextIndex] or "")
	nextIndex = nextIndex + 1
	
	if nextArg == "" then
		propertyValue = nil
		qualifierID = mw.text.trim(qualifierID or "")
	else
		propertyValue = qualifierID  -- cannot be nil when reached; empty value represents 'novalue'
		if propertyValue ~= "" and mw.text.trim(propertyValue) == "" then
			propertyValue = " "  -- single space represents 'somevalue'
		else
			propertyValue = mw.text.trim(propertyValue)
		end
		qualifierID = nextArg
	end
	
	if entity and entity.claims then claims = entity.claims[propertyID] end
	if claims then
		for k, v in pairs(claims) do
			rankPos = _:convertRank(v.rank)
			if propertyValue == nil or _:snakEqualsValue(v.mainsnak, propertyValue) then
				if _:rankMatches(rankPos) then
					outValue = nil
					outInter = nil
					outQualifier = {}
					
					if _.propertyWithQualifier then
						outValue = _:getValue(v.mainsnak, _.withUnit, _.linked)
					end
					
					if v.qualifiers then qualifiers = v.qualifiers[qualifierID] end
					if (not _.propertyWithQualifier or outValue) and qualifiers then
						for k2, v2 in pairs(v.qualifiers[qualifierID]) do
							outInter = _:getValue(v2, _.withUnit, _.linked)
							if outInter then
								if not _.propertyWithQualifier then
									_:appendOutput(outInter, rankPos)
								else
									outQualifier[#outQualifier + 1] = outInter
								end
							end
						end
					end
					
					if outValue and _.propertyWithQualifier then
						outQualifier = table.concat(outQualifier, ", ")
						
						if outQualifier ~= "" then
							outQualifier = " <span style=\"font-size:smaller\">(" .. outQualifier .. ")</span>"
							outValue = outValue .. outQualifier
						end
						
						_:appendOutput(outValue, rankPos)
					end
				end
			end
		end
		return _:out()
	else
		return ""
	end
end

p.propertyWithQualifier = function(frame)
	local _ = State.new()
	_.propertyWithQualifier = true
	_.withUnit = true
	return p.qualifier(frame, _)
end

p.label = function(frame)
	local _ = State.new()
	
	local label = ""
	local target = ""
	local nextArg = mw.text.trim(frame.args[1] or "")
	local nextIndex = 2
	
	while _:processFlag(nextArg) do
		nextArg = mw.text.trim(frame.args[nextIndex] or "")
		nextIndex = nextIndex + 1
	end
	
	if nextArg then
		if nextArg:sub(1,1):upper() == "Q" then
			if _.shortName then
				label = _:getShortName(nextArg)
			end
			
			if label == "" then
				label = mw.wikibase.label(nextArg)
			end
			
			if _.linked or label == nil then
				target = mw.wikibase.sitelink(nextArg)
			end
			
			if _.linked and target then
				label = "[[" .. target .. "|" .. (label or target) .. "]]"
			end
		else
			label = mw.wikibase.label(nextArg)
		end
		
		return (label or target)
	else
		return mw.wikibase.label()
	end
end

return p