Modulo:Ciclismo

Da Wikipedia, l'enciclopedia libera.
Jump to navigation Jump to search

Modulo che implementa i template {{Tappe corsa ciclismo}} e {{Vincitori corsa ciclismo}}.

Ha una sottopagina di configurazione: Modulo:Ciclismo/Configuration.


--[[
* Module implementing "Template:Tappe corsa ciclismo" and "Template:Vincitori corsa ciclismo".
* Author: https://it.wikipedia.org/wiki/User:Rotpunkt
]]--

require('Module:No globals')
local getArgs = require('Module:Arguments').getArgs
local mWikidata = require('Module:Wikidata')
local cfg = mw.loadData('Module:Ciclismo/Configuration')
local i18n = cfg.i18n[mw.language.getContentLanguage():getCode()]

-------------------------------------------------------------------------------
--                           class StagesTable
-------------------------------------------------------------------------------

local StagesTable = {}

local function compStages(stage1, stage2)
	return tonumber(stage1.num) < tonumber(stage2.num)
end

function StagesTable:new(from, args)
	local self = {}
	
	setmetatable(self, { __index = StagesTable })
	self.stages = {}
	local lang = mw.language.getContentLanguage()
	local ids = mWikidata._getProperty( { 'P527', formatting = 'raw', from = from }, true ) or {}
	for _, id in ipairs(ids) do
		local timestamp = mWikidata._getProperty( { 'P585', n = 1, formatting = 'raw', from = id } )
		local year, month, day = timestamp:match('(%d+)%-(%d%d)%-(%d%d).+')
		local month = lang:formatDate('F', tonumber(year) .. '-' .. month .. '-' .. day)
		self.stages[#self.stages + 1] = {
			num = mWikidata._getProperty( { 'P1545', n = 1, from = id } ),
			icon = mWikidata._getProperty( { 'P31', n = 1, formatting = 'raw', showprop = 'P18', from = id } ),
			date = tonumber(day) .. ' ' .. month,
			origin = mWikidata._getProperty( { 'P1427', n = 1, from = id } ),
			destination = mWikidata._getProperty( { 'P1444', n = 1, from = id } ),
			length = mWikidata._getProperty( { 'P3157', n = 1, unit = 'km', formatnum = true , from = id } )
		}
	end
	table.sort(self.stages, compStages)

	return self
end

function StagesTable:getHTML()
	local tableStyle = {
		['font-size'] = '85%',
	}
	
	local tableNode = mw.html.create('table'):addClass('wikitable'):css(tableStyle)
	local tr = tableNode:tag('tr')
	tr:tag('th'):wikitext(i18n.thStage)
	tr:tag('th'):wikitext(i18n.thDate)
	tr:tag('th'):wikitext(i18n.thCourse)
	tr:tag('th'):wikitext(i18n.thDistance)
	tr:tag('th'):wikitext(i18n.thStageWinner)
	tr:tag('th'):wikitext(i18n.thLeader)

	for _, stage in ipairs(self.stages) do
		local tr = tableNode:tag('tr')
		tr:tag('td'):attr('align', 'center'):wikitext(stage.num)
		tr:tag('td'):wikitext(stage.date)
		tr:tag('td'):wikitext(string.format('%s – %s %s', stage.origin, stage.destination,
			stage.icon and string.format('[[File:%s|20x20|right]]', stage.icon) or ''))
		tr:tag('td'):attr('align', 'center'):wikitext(stage.length)
		tr:tag('td'):wikitext('')
		tr:tag('td'):wikitext('')
	end

	return tostring(tableNode)	
end

-------------------------------------------------------------------------------
--                           class WinnersTable
-------------------------------------------------------------------------------

local WinnersTable = {}

local function compEditions(edition1, edition2)
	return tonumber(edition1.year) < tonumber(edition2.year)
end

function WinnersTable:new(from, args)
	local self = {}
	
	setmetatable(self, { __index = WinnersTable })
	self.editions = {}
	local ids = mWikidata._getProperty( { 'P527', formatting = 'raw', from = from }, true ) or {}
	for _, id in ipairs(ids) do
		local timestamp = mWikidata._getProperty( { 'P585', n = 1, formatting = 'raw', from = id } )
		self.editions[#self.editions + 1] = {
			year = timestamp:match('(%d+)%-%d%d%-%d%d.+'),
			winner = mWikidata._getProperty( { 'P1346', qualifier = 'P642', qualifiervalue = 'Q20882667', from = id } ),
			second = mWikidata._getProperty( { 'P1346', qualifier = 'P642', qualifiervalue = 'Q20882668', from = id } ),
			third  = mWikidata._getProperty( { 'P1346', qualifier = 'P642', qualifiervalue = 'Q20882669', from = id } )
		}
	end
	table.sort(self.editions, compEditions)

	return self
end

function WinnersTable:getHTML()
	local tableStyle = {
		['font-size'] = '85%',
	}
	
	local tableNode = mw.html.create('table'):addClass('wikitable'):css(tableStyle)
	local tr = tableNode:tag('tr')
	tr:tag('th'):wikitext(i18n.thYear)
	tr:tag('th'):wikitext(i18n.thWinner)
	tr:tag('th'):wikitext(i18n.thSecond)
	tr:tag('th'):wikitext(i18n.thThird)

	for _, edition in ipairs(self.editions) do
		local tr = tableNode:tag('tr')
		tr:tag('td'):attr('align', 'center'):wikitext(edition.year)
		tr:tag('td'):wikitext(edition.winner)
		tr:tag('td'):wikitext(edition.second)
		tr:tag('td'):wikitext(edition.third)
	end

	return tostring(tableNode)	
end

-------------------------------------------------------------------------------
--                                    API
-------------------------------------------------------------------------------

local p = {}

-- Entry-point for {{Tappe corsa ciclismo}}
function p.stages(frame)
	local args = getArgs(frame, { parentOnly = true })
	return StagesTable:new(args[1], args):getHTML()
end

-- Entry-point for {{Vincitori corsa ciclismo}}
function p.winners(frame)
	local args = getArgs(frame, { parentOnly = true })
	return WinnersTable:new(args[1], args):getHTML()
end

return p