Content deleted Content added
BrokenSegue (talk | contribs) factor out logic into newestMatching |
Tom.Reding (talk | contribs) m Improve readability of long, compound if-statements |
||
(38 intermediate revisions by 4 users not shown) | |||
Line 37:
if i then
-- year
parts[index] = tonumber(mw.ustring.gsub(dateStr:sub(ptr, i-1), "^
if parts[index] == -0 then
Line 87:
if aY < bY then
return true
elseif aY > bY then
return false
elseif aM < bM then
return true
elseif aM > bM then
return false
elseif aD < bD then
return true
end
return false
end
Line 143 ⟶ 105:
if #pointsInTime ~= 1 then
-- be conservative in what we accept
error("Encountered a statement with zero or multiple point in time (P85) qualifiers. Please add or remove point in time information so each statement has exactly one")
end
local pointInTime = pointsInTime[1]
if pointInTime and
pointInTime['datavalue'] and pointInTime['datavalue']['value'] and pointInTime['datavalue']['value']['time'] then return parseDate(pointInTime['datavalue']['value']['time'])
end
end
return nil
end
-- for a given
function
local newestStatement = nil
local newestStatementYr = nil
Line 162 ⟶ 126:
local newestStatementDay = nil
for k, v in pairs(statements) do
if v['rank'] ~= "deprecated" and v['qualifiers'] and v['qualifiers'][qual] then
local quals = v['qualifiers'][qual]
-- should only have one instance of the qualifier on a statement
Line 173 ⟶ 137:
if targetYr then
local older = datePrecedesDate(targetYr, targetMo, targetDay, newestStatementYr, newestStatementMo, newestStatementDay)
if older == nil or not older then
newestStatementYr, newestStatementMo, newestStatementDay = targetYr, targetMo, targetDay
newestStatement = v
Line 186 ⟶ 146:
end
end
end
return newestStatement
end
-- for a given property and qualifier pair returns the newest statement that matches
function newestMatching(e, prop, qual, targetQualValue)
-- first check the best statements
local statements = e:getBestStatements(prop)
local newestStatement = newestMatchingStatement(statements, qual, targetQualValue)
if newestStatement then
return newestStatement
end
-- try again with all statements if nothing so far
statements = e:getAllStatements(prop)
newestStatement = newestMatchingStatement(statements, qual, targetQualValue)
if newestStatement then
return newestStatement
Line 202 ⟶ 176:
end
if not qid then
local e = nil
return e
end
local e = mw.wikibase.getEntity(qid)
assert(e, "No such item found: " .. qid)
return e
end
Line 216 ⟶ 189:
if #chanIds == 1 then
local chan = chanIds[1]
if chan and
chan["mainsnak"] and chan["mainsnak"]["datavalue"] and chan["mainsnak"]["datavalue"]["value"] then return chan["mainsnak"]["datavalue"]["value"]
end
Line 223 ⟶ 200:
end
function returnError(frame, eMessage)
return frame:expandTemplate{ title = 'error', args = { eMessage } } .. "[[Category:Pages with YouTubeSubscribers module errors]]"
end
-- the date of the current YT subscriber count
function p.date( frame )
local e = getEntity(frame)
assert(e, "No qid found for page. Please make a Wikidata item for this article")
local chanId = getBestYtChanId(e)
assert(chanId, "Could not find a single best YouTube channel ID for this item. Add a YouTube channel ID or set the rank of one channel ID to be preferred")
local
if s then
local yt_year, yt_month, yt_day = getClaimDate(s)
if not yt_year then
return nil
end
-- construct YYYY|mm|dd date string
dateString = dateString .. yt_month .. "|"
-- truncate the day of month
--if yt_day and yt_day ~= 0 then
--end
end
return frame:expandTemplate{title="Format date", args = {yt_year, yt_month, yd_day}}
end
error("Could not find a date for YouTube subscriber information. Is there a social media followers statement (P8687) qualified with good values for P585 and P2397?")
end
function p.dateNice( frame )
local status, obj = pcall(p.date, frame)
if status then
return obj
else
return returnError(frame, obj)
end
end
-- the most up to date number of subscribers
function p.subCount( frame )
local subCount = nil
local e = getEntity(frame)
if not e then
subCount = -424
return tonumber(subCount)
end
local chanId = getBestYtChanId(e)
if chanId then
local
if s and
s["mainsnak"] and
s['mainsnak']["datavalue"] and
s['mainsnak']["datavalue"]['value']['amount']
then
else
subCount = -404
end
if subCount then
return tonumber(subCount)
else
subCount = -412
return
end
end
function p.subCountNice( frame )
local status, obj = pcall(p.subCount, frame)
if status then
if obj >= 0 then
return frame:expandTemplate{title="Format price", args = {obj}}
else
return obj
end
else
return returnError(frame, obj)
end
end
|