Модуль:FetchData2: відмінності між версіями
Admin (обговорення | внесок) Немає опису редагування |
Admin (обговорення | внесок) Немає опису редагування |
||
| Рядок 16: | Рядок 16: | ||
text = mw.ustring.gsub(text, "%[%[([^%]]+)%]%]", "%1") | text = mw.ustring.gsub(text, "%[%[([^%]]+)%]%]", "%1") | ||
return mw.text.trim(text) | return mw.text.trim(text) | ||
end | end | ||
| Рядок 87: | Рядок 38: | ||
-- Знаходимо таблицю | -- Знаходимо таблицю | ||
local | local table_start = mw.ustring.find(content, "{|") | ||
if not | local table_end = mw.ustring.find(content, "|}", table_start) | ||
if not table_start or not table_end then | |||
return error_output("Table Missing", page_title) | return error_output("Table Missing", page_title) | ||
end | end | ||
local table_content = mw.ustring.sub(content, table_start, table_end + 1) | |||
-- Екрануємо спеціальні символи в імені гравця | |||
local escaped = mw.ustring.gsub(player_name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1") | local escaped = mw.ustring.gsub(player_name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1") | ||
local player_pattern = "%[%[" .. escaped .. "%]%]" | local player_pattern = "%[%[" .. escaped .. "%]%]" | ||
-- | -- Шукаємо рядки, які починаються з |- | ||
for row in mw.ustring.gmatch(table_content, "|\n(.-)\n|-") do | local rows = {} | ||
row = | for row in mw.ustring.gmatch(table_content, "|-\n(.-)\n|-") do | ||
table.insert(rows, row) | |||
end | |||
-- Останній рядок (до |}) | |||
local last_row = mw.ustring.match(table_content, "|-\n(.-)%s*|}") | |||
if last_row then | |||
table.insert(rows, last_row) | |||
end | |||
-- Обробляємо кожен рядок | |||
for _, row in ipairs(rows) do | |||
if mw.ustring.find(row, player_pattern) then | if mw.ustring.find(row, player_pattern) then | ||
local cells = | -- Розбиваємо рядок на комірки | ||
local cells = {} | |||
-- Видаляємо початковий | | |||
row = mw.ustring.gsub(row, "^%s*|%s*", "") | |||
if column_index <= #cells | -- Розбиваємо по || | ||
for cell in mw.ustring.gmatch(row, "([^|]+)") do | |||
-- Пропускаємо порожні комірки від подвійних || | |||
local trimmed = mw.text.trim(cell) | |||
if trimmed ~= "" then | |||
table.insert(cells, trimmed) | |||
end | |||
end | |||
-- Повертаємо потрібну колонку | |||
if column_index <= #cells then | |||
return clean_wikilinks(cells[column_index]) | return clean_wikilinks(cells[column_index]) | ||
else | |||
return error_output("Column Out of Range", | |||
string.format("Requested col %d, found %d cells", column_index, #cells)) | |||
end | end | ||
end | end | ||
end | end | ||
return | return error_output("Player Not Found", player_name) | ||
end | end | ||
Версія за 01:10, 24 листопада 2025
Документацію для цього модуля можна створити у Модуль:FetchData2/документація
local p = {}
--- Функція для виведення помилки на сторінку
local function error_output(context, error_details)
local safe_details = error_details or "N/A"
return string.format("<span style='color:red; font-weight:bold;'>[FD2 Error: %s. Details: %s]</span>",
context or "Unknown Context", safe_details)
end
--- Видаляє вікі-посилання [[link|text]] і [[link]]
local function clean_wikilinks(text)
if not text then return nil end
-- [[link|text]] -> text
text = mw.ustring.gsub(text, "%[%[([^%]|]+)|([^%]]+)%]%]", "%2")
-- [[link]] -> link
text = mw.ustring.gsub(text, "%[%[([^%]]+)%]%]", "%1")
return mw.text.trim(text)
end
----------------------------------------------------------------------
-- ГОЛОВНИЙ ПАРСЕР ДЛЯ ГОРИЗОНТАЛЬНИХ ТАБЛИЦЬ
----------------------------------------------------------------------
local function fetch_from_table(page_title, player_name, column_index)
if not player_name or player_name == "" then
return error_output("No Player Name", page_title)
end
local title = mw.title.new(page_title)
if not title or not title.exists then
return error_output("Page Missing", page_title)
end
local content = title:getContent()
if not content then
return error_output("Content Read Fail", page_title)
end
-- Знаходимо таблицю
local table_start = mw.ustring.find(content, "{|")
local table_end = mw.ustring.find(content, "|}", table_start)
if not table_start or not table_end then
return error_output("Table Missing", page_title)
end
local table_content = mw.ustring.sub(content, table_start, table_end + 1)
-- Екрануємо спеціальні символи в імені гравця
local escaped = mw.ustring.gsub(player_name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
local player_pattern = "%[%[" .. escaped .. "%]%]"
-- Шукаємо рядки, які починаються з |-
local rows = {}
for row in mw.ustring.gmatch(table_content, "|-\n(.-)\n|-") do
table.insert(rows, row)
end
-- Останній рядок (до |})
local last_row = mw.ustring.match(table_content, "|-\n(.-)%s*|}")
if last_row then
table.insert(rows, last_row)
end
-- Обробляємо кожен рядок
for _, row in ipairs(rows) do
if mw.ustring.find(row, player_pattern) then
-- Розбиваємо рядок на комірки
local cells = {}
-- Видаляємо початковий |
row = mw.ustring.gsub(row, "^%s*|%s*", "")
-- Розбиваємо по ||
for cell in mw.ustring.gmatch(row, "([^|]+)") do
-- Пропускаємо порожні комірки від подвійних ||
local trimmed = mw.text.trim(cell)
if trimmed ~= "" then
table.insert(cells, trimmed)
end
end
-- Повертаємо потрібну колонку
if column_index <= #cells then
return clean_wikilinks(cells[column_index])
else
return error_output("Column Out of Range",
string.format("Requested col %d, found %d cells", column_index, #cells))
end
end
end
return error_output("Player Not Found", player_name)
end
----------------------------------------------------------------------
-- ФУНКЦІЇ, ЩО ВИКЛИКАЮТЬСЯ З ШАБЛОНУ
----------------------------------------------------------------------
function p.recruiter(frame)
local name = frame.args.player
local raw = fetch_from_table("Гравці", name, 4)
if type(raw) == "string" and mw.ustring.find(raw, "Error") then
return raw
end
if raw == "Відсутній" or raw == "-" or not raw then
return "Не вказано"
end
return raw
end
function p.date_added(frame)
local name = frame.args.player
local raw = fetch_from_table("Гравці", name, 3)
if type(raw) == "string" and mw.ustring.find(raw, "Error") then
return raw
end
return raw or "Невідомо"
end
function p.prize_pool(frame)
local name = frame.args.player
local raw = fetch_from_table("Призовий_фонд", name, 3)
if type(raw) == "string" and mw.ustring.find(raw, "Error") then
return raw
end
if raw then
raw = mw.ustring.gsub(raw, "[^%d%s]", "")
raw = mw.text.trim(raw)
end
return (raw or "0") .. " ₴"
end
return p