Benutzer:Jobw/Spielwiese/Modul:AlexaRank

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen

local p = {} frame = mw.getCurrentFrame() local AlexaProperty = 'P1661' local PointInTimeQualifier = 'P585'

function getQualifierSnak(claim, qualifierId) -- a "snak" is Wikidata terminology for a typed key/value pair -- a claim consists of a main snak holding the main information of this claim, -- as well as a list of attribute snaks and a list of references snaks if qualifierId then -- search the attribute snak with the given qualifier as key if claim and claim.qualifiers then local qualifier = claim.qualifiers[qualifierId] if qualifier then return qualifier[1] end end return nil, printError("qualifier-not-found") else -- otherwise return the main snak return claim.mainsnak end end

local function datavalueTimeToDateObject(data) local sign, year, month, day, hour, minute, second = string.match(data.time, "(.)(%d+)%-(%d+)%-(%d+)T(%d+):(%d+):(%d+)Z") local result = { year = tonumber(year), month = tonumber(month), day = tonumber(day), hour = tonumber(hour), min = tonumber(minute), sec = tonumber(second), timezone = data.timezone, julian = data.calendarmodel and string.match(data.calendarmodel, "Q11184$") } if sign == "-" then result.year = -result.year end return result end


function julianDay(dateObject) local year = dateObject.year local month = dateObject.month or 0 local day = dateObject.day or 0

if month == 0 then month = 1 end if day == 0 then day = 1 end if month <= 2 then year = year - 1 month = month + 12 end

local time = ((((dateObject.sec or 0) / 60 + (dateObject.min or 0) + (dateObject.timezone or 0)) / 60) + (dateObject.hour or 0)) / 24

local b if dateObject.julian then b = 0 else local century = math.floor(year / 100) b = 2 - century + math.floor(century / 4) end

return math.floor(365.25 * (year + 4716)) + math.floor(30.6001 * (month + 1)) + day + time + b - 1524.5 end

function getQualifierSortValue(claim, qualifierId) local snak = getQualifierSnak(claim, qualifierId) if snak and snak.snaktype == "value" then if snak.datavalue.type == "time" then return julianDay(datavalueTimeToDateObject(snak.datavalue.value)) else return getSnakValue(snak) end end end


function p.printInfoboxEntry() -- get wikidata entity local entity = mw.wikibase.getEntity(frame.args["id"]) if not entity then if showerrors then return printError("entity-not-found") else return default end end -- fetch the first claim of satisfying the given property

   local claims

if entity.claims then claims = entity.claims[mw.wikibase.resolvePropertyId(AlexaProperty)] end if not claims or not claims[1] then if showerrors then return printError("property-not-found") else return default end end

-- log local claim for index,claim in ipairs(claims) do mw.logObject(claim) end

-- get initial sort indices local sortindices = {} for idx in pairs(claims) do sortindices[#sortindices + 1] = idx end

-- sort by time qualifier (descending) local comparator comparator = function(a, b) local timea = getQualifierSortValue(claims[a], PointInTimeQualifier) local timeb = getQualifierSortValue(claims[b], PointInTimeQualifier) if tonumber(timea) and tonumber(timeb) then timea = tonumber(timea) timeb = tonumber(timeb) else return false -- different types, neither numbers nor strings, no chance to compare => random result to avoid script error end return timea > timeb end table.sort(sortindices, comparator)

--get value local last = tonumber(claims[sortindices[1]].mainsnak.datavalue.value.amount)

-- compare last and secondlast mw.logObject(sortindices) local trend = "" if (#sortindices >= 2) then mw.log("compare:") -- mw.logObject(tonumber(claims[sortindices[1]].mainsnak.datavalue.value.amount)) local secondlast = tonumber(claims[sortindices[2]].mainsnak.datavalue.value.amount) if last == secondlast then trend = '' elseif last < secondlast then trend = ' ' elseif last > secondlast then trend = '' else printError("comparison failed") end mw.log(trend) else mw.log('Less then 2 claims, no comparison') end

local result result = trend .. " " .. last --getReferences(frame, claims[sortindices[1]]) if result then return result else return default end end

return p