Модуль:FetchData2: відмінності між версіями
Admin (обговорення | внесок) Немає опису редагування |
Admin (обговорення | внесок) Немає опису редагування |
||
| Рядок 1: | Рядок 1: | ||
local p = {} | |||
-------------------------------------------------------------------- | |||
-- UNIVERSAL TABLE PARSER | |||
-------------------------------------------------------------------- | |||
local function fetch_data_from_table(page_title, player_name, column_index) | local function fetch_data_from_table(page_title, player_name, column_index) | ||
local title = mw.title.new(page_title) | local title = mw.title.new(page_title) | ||
| Рядок 6: | Рядок 11: | ||
if not content then return nil end | if not content then return nil end | ||
-- | -- Беремо будь-яку wikitable | ||
local table_content = mw.ustring.match(content, "{|.-|}") | local table_content = mw.ustring.match(content, "{|.-|}") | ||
if not table_content then return nil end | if not table_content then return nil end | ||
-- | -- Escape special chars | ||
local | local escaped = mw.ustring.gsub(player_name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1") | ||
-- | -- Патерн для пошуку імені | ||
local name_pattern = | |||
local | "%[%[" .. escaped .. "%]%]" .. | ||
"%[%[" .. | "|%[%[" .. escaped .. "|.-%]%]" .. | ||
"|%[%[" .. | "|" .. escaped | ||
"|" .. | |||
-- | -- Знаходимо рядок | ||
local row = nil | local row = nil | ||
for line in mw.ustring.gmatch(table_content, "[^\n]+") do | for line in mw.ustring.gmatch(table_content, "[^\n]+") do | ||
if mw.ustring.match(line, | if mw.ustring.match(line, name_pattern) then | ||
row = line | row = line | ||
break | break | ||
end | end | ||
end | end | ||
if not row then return nil end | if not row then return nil end | ||
-- | -- Чистимо від |- | ||
row = mw.ustring.gsub(row, "^%s*|%-", "") | row = mw.ustring.gsub(row, "^%s*|%-", "") | ||
-- Розбиваємо | -- Розбиваємо комірки | ||
local cells = {} | local cells = {} | ||
for cell in mw.ustring.gmatch(row, "%s*|%s*([^|]+)") do | for cell in mw.ustring.gmatch(row, "%s*|%s*([^|]+)") do | ||
| Рядок 39: | Рядок 44: | ||
end | end | ||
local result = cells[column_index] | local result = cells[column_index] | ||
if not result then return nil end | if not result then return nil end | ||
-- | -- Забираємо вікі-лінки | ||
result = mw.ustring.gsub(result, "%[%[([^%]|]+)|?([^%]]*)%]%]", "%2") | result = mw.ustring.gsub(result, "%[%[([^%]|]+)|?([^%]]*)%]%]", "%2") | ||
if result == "" then | if result == "" then | ||
result = mw.ustring.gsub(result, "%[%[([^%]]+)%]%]", "%1") | result = mw.ustring.gsub(result, "%[%[([^%]]+)%]%]", "%1") | ||
end | end | ||
return result | return mw.text.trim(result) | ||
end | |||
-------------------------------------------------------------------- | |||
-- PUBLIC FUNCTIONS | |||
-------------------------------------------------------------------- | |||
-- 1) Дата приєднання | |||
function p.join_date(frame) | |||
local name = frame.args[1] | |||
return fetch_data_from_table("Гравці", name, 3) or "Невідомо" | |||
end | |||
-- 2) Провідник | |||
function p.leader(frame) | |||
local name = frame.args[1] | |||
return fetch_data_from_table("Гравці", name, 4) or "Відсутній" | |||
end | |||
-- 3) Фундація | |||
function p.fund(frame) | |||
local name = frame.args[1] | |||
return fetch_data_from_table("Фундація", name, 2) or "0" | |||
end | |||
-- 4) Призовий фонд | |||
function p.prize(frame) | |||
local name = frame.args[1] | |||
return fetch_data_from_table("Призовий_фонд", name, 2) or "0" | |||
end | |||
-- 5) Фіналіст | |||
function p.finalist(frame) | |||
local name = frame.args[1] | |||
return fetch_data_from_table("Фіналіст", name, 2) or "Невідомо" | |||
end | |||
-- 6) Титул | |||
function p.title(frame) | |||
local name = frame.args[1] | |||
return fetch_data_from_table("Гравці", name, 5) or "Відсутній" | |||
end | end | ||
-------------------------------------------------------------------- | |||
-- EXPORT MODULE | |||
-------------------------------------------------------------------- | |||
return p | |||
Версія за 00:14, 24 листопада 2025
Документацію для цього модуля можна створити у Модуль:FetchData2/документація
local p = {}
--------------------------------------------------------------------
-- UNIVERSAL TABLE PARSER
--------------------------------------------------------------------
local function fetch_data_from_table(page_title, player_name, column_index)
local title = mw.title.new(page_title)
if not title then return nil end
local content = title:getContent()
if not content then return nil end
-- Беремо будь-яку wikitable
local table_content = mw.ustring.match(content, "{|.-|}")
if not table_content then return nil end
-- Escape special chars
local escaped = mw.ustring.gsub(player_name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
-- Патерн для пошуку імені
local name_pattern =
"%[%[" .. escaped .. "%]%]" ..
"|%[%[" .. escaped .. "|.-%]%]" ..
"|" .. escaped
-- Знаходимо рядок
local row = nil
for line in mw.ustring.gmatch(table_content, "[^\n]+") do
if mw.ustring.match(line, name_pattern) then
row = line
break
end
end
if not row then return nil end
-- Чистимо від |-
row = mw.ustring.gsub(row, "^%s*|%-", "")
-- Розбиваємо комірки
local cells = {}
for cell in mw.ustring.gmatch(row, "%s*|%s*([^|]+)") do
table.insert(cells, mw.text.trim(cell))
end
local result = cells[column_index]
if not result then return nil end
-- Забираємо вікі-лінки
result = mw.ustring.gsub(result, "%[%[([^%]|]+)|?([^%]]*)%]%]", "%2")
if result == "" then
result = mw.ustring.gsub(result, "%[%[([^%]]+)%]%]", "%1")
end
return mw.text.trim(result)
end
--------------------------------------------------------------------
-- PUBLIC FUNCTIONS
--------------------------------------------------------------------
-- 1) Дата приєднання
function p.join_date(frame)
local name = frame.args[1]
return fetch_data_from_table("Гравці", name, 3) or "Невідомо"
end
-- 2) Провідник
function p.leader(frame)
local name = frame.args[1]
return fetch_data_from_table("Гравці", name, 4) or "Відсутній"
end
-- 3) Фундація
function p.fund(frame)
local name = frame.args[1]
return fetch_data_from_table("Фундація", name, 2) or "0"
end
-- 4) Призовий фонд
function p.prize(frame)
local name = frame.args[1]
return fetch_data_from_table("Призовий_фонд", name, 2) or "0"
end
-- 5) Фіналіст
function p.finalist(frame)
local name = frame.args[1]
return fetch_data_from_table("Фіналіст", name, 2) or "Невідомо"
end
-- 6) Титул
function p.title(frame)
local name = frame.args[1]
return fetch_data_from_table("Гравці", name, 5) or "Відсутній"
end
--------------------------------------------------------------------
-- EXPORT MODULE
--------------------------------------------------------------------
return p