Модуль:Песочница/Torop92/ucimtb
Перейти к навигации
Перейти к поиску
Документация
local p = {}
local wiki = string.match(mw.site.server, "%a+")
if wiki == "www" then
wiki = "fr"
end
--import data
local data = mw.loadData("Module:Песочница/Torop92/ucimtb/data")
--import translation
l10n = {
general_clas_table_translate={ "№", "Гонщик", "Очков"},
general_clas_table_women_translate={ "№", "Гонщица", "Очков"}
}
local contentLanguage = mw.getContentLanguage()
local wikilang = contentLanguage:getCode()
local wikibase = mw.wikibase
local html = mw.html
local backgroundColor="#FFDF80"
local backgroundColorLight="#FFF7DF"
local function nextStatement(state, i)
repeat
i = i + 1
local s = state[i]
if s and s.rank ~= 'deprecated' and s.mainsnak.snaktype == 'value' then
return i, s
end
until s == nil
end
local function statements(QID, PID)
return nextStatement, wikibase.getAllStatements(QID, PID), 0
end
local function isWomenrace(raceID) -- for translation
for _, p2094 in statements(raceID, 'P2094') do
if p2094.mainsnak.datavalue.value.id == "Q1451845" then
return true
end
end
return false
end
local function get_lf(frame)
local lf = frame
if string.match(frame:getParent():getTitle(), '%P+') == mw.site.namespaces.Template.name then
lf = frame:getParent()
end
return lf
end
local function get_and_checkID(frame)
local lf = get_lf(frame)
local entityID = mw.text.trim(lf.args[1])
entityID = string.gsub(entityID, "%c", "") -- probably redundant
if type(entityID) ~= 'string' then
error('parameter must be a string')
end
if not entityID:match('Q%d+') then
error('parameter must be a valid Wikidata item (ex: Q42)')
end
return entityID, lf
end
local function wdLink (id)
return "[[File:Wikidata-logo S.svg|12px|link=d:" .. id .. "]]"
end
local function flag(countryID, date)
local entry = data.flags[countryID]
local IOC
local file
local result = ""
if entry then
for i, v in ipairs(entry) do
if i == 1 then
IOC = v
else
if not date then
file = v[1]
break
else
local from = v[2]
local to = v[3]
if (not from or from <= date) and (not to or to > date) then
file = v[1]
break
end
end
end
end
end
local flagpxSize = '20px'
if countryID == 'Q39' then
flagpxSize = '16px'
end -- Small size for an square flag as Switzerland
result = '[[File:' .. file .. '|border|' .. flagpxSize .. '|' .. IOC .. ']]'
return result
end
local function revertfirstlast(name)
local nametable = mw.text.split(name, ",")
if nametable[2] then -- there is a coma
return nametable[2]:sub(2) .. " " .. nametable[1]
else
return nametable[1]
end
end
local function translate(func_name_short, index, w_race, title)
if index == 1000 then -- code for some custom function
return title
else
if func_name_short then
local func_name
if w_race then
func_name = func_name_short .. "_women_translate"
if l10n[func_name] and l10n[func_name][index] then
return l10n[func_name][index]
end
end
func_name = func_name_short .. "_translate"
if l10n[func_name][index] then
return l10n[func_name][index]
end
return "translation for " .. func_name .. " index " .. tostring(index) .. " not found"
else
error('func_name found')
end
end
end
function p.test2()
raceID = 'Q118368166'
--wRace = wikibase.getAllStatements(raceID, 'P2094')
--mw.logObject(wRace)
mw.logObject(isWomenrace(raceID))
end
function p.test()
local raceID = 'Q118368166'
local SeasonGeneralClass = wikibase.getBestStatements(raceID, 'P2321')
local SeasonEndTime = wikibase.getBestStatements(raceID, 'P582')[1].mainsnak.datavalue.value.time
local tableGeneral = {}
local labelMissing = false
local WRace = isWomenrace(raceID)
for _, v in pairs(SeasonGeneralClass) do
local row = {}
--get ranking rider
local key = v.qualifiers["P1352"][1].datavalue.value.amount:sub(2)
row["1"] = key
--ge triderID
local riderID = v.mainsnak.datavalue.value.id
row.riderID = riderID
--get points rider
local points = v.qualifiers["P1351"][1].datavalue.value.amount:sub(2)
row["3"] = points
--get name rider
local rider = wikibase.getEntityObject(riderID)
local label = rider:getLabel()
label = revertfirstlast(label)
local link = rider:getSitelink()
local name = ''
if label == null then
label = '\'\'' .. rider:getLabel('en') .. ' (en)\'\''
labelMissing = true
end
if link == null then
name = '[[' .. label .. ']]'
elseif label == link then
name = '[[' .. label .. ']]'
else
name = '[[' .. link .. '|' .. label .. ']]'
end
row["2"] = name
--get flag rider country
local countryID = wikibase.getBestStatements( riderID, 'P27')[1].mainsnak.datavalue.value.id
row.flag = flag(countryID)
-- add row to table
table.insert(tableGeneral, row)
end
local Header = { [1] = translate('general_clas_table', 1, WRace),
[2] = translate('general_clas_table', 2, WRace),
[3] = translate('general_clas_table', 3, WRace)
}
mw.logObject(tableGeneral)
column_num = 3
title = 'Общий зачёт'
--Create table
local t = mw.html.create('table'):addClass('sortable'):attr('cellpadding', '4'):attr('cellspacing', '0'):css('border', '1px solid rgb(200,200,200)'):css('padding', '3px')
--Create caption
local tableCaption = html.create('tr'):tag('th'):attr('colspan',tostring(#Header + 1)):css('padding','2px 2px'):css('text-align','center'):css('background-color', backgroundColor):wikitext(title)
--Create header
local tableHeader = html.create('tr'):css('text-align','center'):css('padding','2px 2px'):css('white-space','nowrap')
for _, j in pairs(Header) do
tableHeader:tag('th'):addClass('headerSort'):wikitext(j)
end
tableFull = t:node(tableCaption):node(tableHeader)
--Create body
for k, v in pairs(tableGeneral) do
local tableBody = html.create('tr')
for i = 1, column_num do
if i == 2 then
tableBody:tag('td'):css('text-align','left'):wikitext(v["flag"] .. " " .. v[tostring(i)] .. " " .. wdLink (v["riderID"]))
else
tableBody:tag('td'):css('text-align','left'):wikitext(v[tostring(i)])
end
end
tableFull:node(tableBody)
end
--mw.logObject(#GeneralClass)
mw.logObject(tableFull)
return tableFull
end
return p