You can edit almost every page by Creating an account and confirming your email.

Module:Sandbox/Habst/Athletics results

From EverybodyWiki Bios & Wiki

Documentation for this module may be created at Module:Sandbox/Habst/Athletics results/doc

p = {}

local P_POINTINTIME = 'P585'
local P_PARTICIPANTOF = 'P1344'
local P_SPORTSDISCIPLINE = 'P2416'

local P_RACETIME = 'P2781'
local P_POINTS = 'P1358'
local P_DISTANCE = 'P2043'
local marks = { P_RACETIME, P_POINTS, P_DISTANCE }

p.athleticsResults = function(frame)
	local qid = frame.args[1]
	local ent = mw.wikibase.getEntity(qid)
	local participantOfClaims = ent['claims'][P_PARTICIPANTOF]
	local years = {}
	local output = ''
	output = output .. '{| class="infobox"\n'
	output = output .. '! Athletics Results\n'
	for i, pc in ipairs(participantOfClaims) do
		mw.logObject(mw.wikibase.renderSnak(pc['mainsnak']))
		if pc['qualifiers'] then
			local fulldate = pc['qualifiers'][P_POINTINTIME][1]['datavalue']['value']['time']
			local year = fulldate:sub(2, 5)
			local serialized = fulldate -- e.g. "+2017-06-28T00:00:00Z" 21 characters long
			if pc['qualifiers'][P_SPORTSDISCIPLINE] then
				serialized = serialized .. '\'\'\'' .. mw.wikibase.renderSnak(pc['qualifiers'][P_SPORTSDISCIPLINE][1]) .. ':\'\'\' '
			end
			local markProperty = nil
			for j, pm in ipairs(marks) do
				if pc['qualifiers'][pm] then
					markProperty = pm
				end
			end
			if markProperty ~= nil then
				-- mw.logObject(pc['qualifiers'][markProperty])
				serialized = serialized .. mw.wikibase.renderSnak(pc['qualifiers'][markProperty][1]):match('([^±]*)')
			end
			meetname = mw.wikibase.renderSnak(pc['mainsnak'])
			if meetname ~= 'no value' then
				serialized = serialized .. ' at [[' .. meetname .. ']]'
			end
			if not years[year] then
				years[year] = {}
			end
			table.insert(years[year], serialized)
		end
	end
	yearsort = {}
    for n in pairs(years) do table.insert(yearsort, n) end
    table.sort(yearsort)
	for i, year in ipairs(yearsort) do
		results = years[year]
		ressort = {}
		for k, v in pairs(results) do table.insert(ressort, v) end
		table.sort(ressort)
		output = output .. '|-\n|\n'
		output = output .. '  {| class="mw-collapsible mw-collapsed"\n'
		output = output .. '  ! ' .. year .. '\n'
		for i, res in ipairs(ressort) do
			output = output .. '  |-\n'
			output = output .. '  | ' .. res:sub(22) .. '\n'
		end
		output = output .. '  |}\n'
	end
	output = output .. '|}'
	return output
end
return p