Модуль:FetchData2: відмінності між версіями
Admin (обговорення | внесок) Немає опису редагування |
Admin (обговорення | внесок) Немає опису редагування |
||
| Рядок 4: | Рядок 4: | ||
local function error_output(context, error_details) | local function error_output(context, error_details) | ||
local safe_details = error_details or "N/A" | local safe_details = error_details or "N/A" | ||
return string.format("<span style='color:red; font-weight:bold;'>[FD2 Error: %s. | return string.format("<span style='color:red; font-weight:bold;'>[FD2 Error: %s. Details: %s]</span>", | ||
context or "Unknown Context", safe_details) | |||
end | end | ||
| Рядок 15: | Рядок 16: | ||
text = mw.ustring.gsub(text, "%[%[([^%]]+)%]%]", "%1") | text = mw.ustring.gsub(text, "%[%[([^%]]+)%]%]", "%1") | ||
return mw.text.trim(text) | return mw.text.trim(text) | ||
end | |||
--- Парсить комірки з рядка таблиці | |||
local function parse_table_cells(row_text) | |||
local cells = {} | |||
local current_cell = "" | |||
local in_link = false | |||
local i = 1 | |||
while i <= #row_text do | |||
local char = row_text:sub(i, i) | |||
local next_char = row_text:sub(i + 1, i + 1) | |||
-- Відслідковуємо вікі-посилання | |||
if char == "[" and next_char == "[" then | |||
in_link = true | |||
current_cell = current_cell .. "[[" | |||
i = i + 2 | |||
elseif char == "]" and next_char == "]" then | |||
in_link = false | |||
current_cell = current_cell .. "]]" | |||
i = i + 2 | |||
-- Якщо знайшли роздільник комірок | |||
elseif char == "|" and not in_link then | |||
if next_char == "|" then | |||
-- || - роздільник комірок | |||
table.insert(cells, mw.text.trim(current_cell)) | |||
current_cell = "" | |||
i = i + 2 | |||
else | |||
-- | на початку рядка - також роздільник | |||
if current_cell ~= "" then | |||
table.insert(cells, mw.text.trim(current_cell)) | |||
current_cell = "" | |||
end | |||
i = i + 1 | |||
end | |||
else | |||
current_cell = current_cell .. char | |||
i = i + 1 | |||
end | |||
end | |||
-- Додаємо останню комірку | |||
if current_cell ~= "" then | |||
table.insert(cells, mw.text.trim(current_cell)) | |||
end | |||
return cells | |||
end | end | ||
| Рядок 20: | Рядок 70: | ||
-- ГОЛОВНИЙ ПАРСЕР ДЛЯ ГОРИЗОНТАЛЬНИХ ТАБЛИЦЬ | -- ГОЛОВНИЙ ПАРСЕР ДЛЯ ГОРИЗОНТАЛЬНИХ ТАБЛИЦЬ | ||
---------------------------------------------------------------------- | ---------------------------------------------------------------------- | ||
local function fetch_from_table(page_title, player_name, column_index) | local function fetch_from_table(page_title, player_name, column_index) | ||
| Рядок 31: | Рядок 80: | ||
return error_output("Page Missing", page_title) | return error_output("Page Missing", page_title) | ||
end | end | ||
local content = title:getContent() | local content = title:getContent() | ||
if not content then return error_output("Content Read Fail", page_title) end | if not content then | ||
return error_output("Content Read Fail", page_title) | |||
local table_content = mw.ustring.match(content, "{|.-( | end | ||
-- Знаходимо таблицю | |||
local table_content = mw.ustring.match(content, "{|.-(.-|})") | |||
if not table_content then | if not table_content then | ||
return error_output("Table Missing", page_title) | return error_output("Table Missing", page_title) | ||
end | end | ||
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 | |||
row = row .. "\n" -- Додаємо перенос для коректної обробки | |||
for row in mw.ustring.gmatch(table_content, " | |||
if | -- Перевіряємо, чи є ім'я гравця в цьому рядку | ||
if mw.ustring.find(row, player_pattern) then | |||
local cells = parse_table_cells(row) | |||
local | |||
if column_index <= # | if column_index <= #cells and cells[column_index] then | ||
return clean_wikilinks(cells[column_index]) | |||
end | end | ||
end | end | ||
end | end | ||
return | return nil | ||
end | end | ||
| Рядок 81: | Рядок 115: | ||
-- ФУНКЦІЇ, ЩО ВИКЛИКАЮТЬСЯ З ШАБЛОНУ | -- ФУНКЦІЇ, ЩО ВИКЛИКАЮТЬСЯ З ШАБЛОНУ | ||
---------------------------------------------------------------------- | ---------------------------------------------------------------------- | ||
function p.recruiter(frame) | function p.recruiter(frame) | ||
local name = frame.args.player | local name = frame.args.player | ||
local raw = fetch_from_table("Гравці", name, 4) | local raw = fetch_from_table("Гравці", name, 4) | ||
if type(raw) == "string" and mw.ustring.find(raw, "Error") then return raw end | 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 | |||
return raw | |||
end | end | ||
| Рядок 96: | Рядок 134: | ||
local raw = fetch_from_table("Гравці", name, 3) | local raw = fetch_from_table("Гравці", name, 3) | ||
if type(raw) == "string" and mw.ustring.find(raw, "Error") then return raw end | if type(raw) == "string" and mw.ustring.find(raw, "Error") then | ||
return raw | |||
end | |||
return raw or "Невідомо" | return raw or "Невідомо" | ||
| Рядок 105: | Рядок 145: | ||
local raw = fetch_from_table("Призовий_фонд", name, 3) | local raw = fetch_from_table("Призовий_фонд", name, 3) | ||
if type(raw) == "string" and mw.ustring.find(raw, "Error") then return raw end | if type(raw) == "string" and mw.ustring.find(raw, "Error") then | ||
return raw | |||
end | |||
if raw then | if raw then | ||
| Рядок 111: | Рядок 153: | ||
raw = mw.text.trim(raw) | raw = mw.text.trim(raw) | ||
end | end | ||
return (raw or "0") .. " ₴" | return (raw or "0") .. " ₴" | ||
end | end | ||
return p | return p | ||
Версія за 01:07, 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 parse_table_cells(row_text)
local cells = {}
local current_cell = ""
local in_link = false
local i = 1
while i <= #row_text do
local char = row_text:sub(i, i)
local next_char = row_text:sub(i + 1, i + 1)
-- Відслідковуємо вікі-посилання
if char == "[" and next_char == "[" then
in_link = true
current_cell = current_cell .. "[["
i = i + 2
elseif char == "]" and next_char == "]" then
in_link = false
current_cell = current_cell .. "]]"
i = i + 2
-- Якщо знайшли роздільник комірок
elseif char == "|" and not in_link then
if next_char == "|" then
-- || - роздільник комірок
table.insert(cells, mw.text.trim(current_cell))
current_cell = ""
i = i + 2
else
-- | на початку рядка - також роздільник
if current_cell ~= "" then
table.insert(cells, mw.text.trim(current_cell))
current_cell = ""
end
i = i + 1
end
else
current_cell = current_cell .. char
i = i + 1
end
end
-- Додаємо останню комірку
if current_cell ~= "" then
table.insert(cells, mw.text.trim(current_cell))
end
return cells
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_content = mw.ustring.match(content, "{|.-(.-|})")
if not table_content then
return error_output("Table Missing", page_title)
end
local escaped = mw.ustring.gsub(player_name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
local player_pattern = "%[%[" .. escaped .. "%]%]"
-- Парсимо рядки таблиці
for row in mw.ustring.gmatch(table_content, "|\n(.-)\n|-") do
row = row .. "\n" -- Додаємо перенос для коректної обробки
-- Перевіряємо, чи є ім'я гравця в цьому рядку
if mw.ustring.find(row, player_pattern) then
local cells = parse_table_cells(row)
if column_index <= #cells and cells[column_index] then
return clean_wikilinks(cells[column_index])
end
end
end
return nil
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