Модуль:FetchData2: відмінності між версіями
Admin (обговорення | внесок) Немає опису редагування |
Admin (обговорення | внесок) Немає опису редагування |
||
| Рядок 1: | Рядок 1: | ||
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) | ||
if not title then return nil end | |||
if not content then | local content = title:getContent() | ||
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 | if not table_content then return nil end | ||
local escaped_player = mw.ustring.gsub(player_name, "([%(%)%.% | -- Робимо імʼя безпечним у RegEx | ||
local escaped_player = mw.ustring.gsub(player_name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1") | |||
-- | -- Шукаємо у форматах: | ||
-- | -- [[Player]], [[Player|текст]], Player | ||
local player_pattern = | |||
"%[%[" .. escaped_player .. "%]%]" .. | |||
"|%[%[" .. escaped_player .. "|.-%]%]" .. | |||
"|" .. escaped_player | |||
-- | -- Знайдемо весь рядок, де є гравець | ||
local | local row = nil | ||
for line in mw.ustring.gmatch(table_content, "[^\n]+") do | |||
if mw.ustring.match(line, player_pattern) then | |||
row = line | |||
break | |||
for | |||
if | |||
end | end | ||
end | end | ||
if not row then return nil end | |||
if | |||
end | |||
--- | -- Забираємо все після "|-" якщо є | ||
row = mw.ustring.gsub(row, "^%s*|%-", "") | |||
-- Розбиваємо рядок на комірки | |||
local | local cells = {} | ||
for cell in mw.ustring.gmatch(row, "%s*|%s*([^|]+)") do | |||
table.insert(cells, mw.text.trim(cell)) | |||
end | end | ||
local | -- Вибираємо потрібну колонку | ||
local result = cells[column_index] | |||
if not | if not result then return nil end | ||
-- Прибираємо вікі-посилання | |||
result = mw.ustring.gsub(result, "%[%[([^%]|]+)|?([^%]]*)%]%]", "%2") | |||
result = mw.text.trim(result) | |||
if result == "" then | |||
if | result = mw.ustring.gsub(result, "%[%[([^%]]+)%]%]", "%1") | ||
end | end | ||
return result | |||
return | |||
end | end | ||
Версія за 00:13, 24 листопада 2025
Документацію для цього модуля можна створити у Модуль:FetchData2/документація
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
-- Робимо імʼя безпечним у RegEx
local escaped_player = mw.ustring.gsub(player_name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
-- Шукаємо у форматах:
-- [[Player]], [[Player|текст]], Player
local player_pattern =
"%[%[" .. escaped_player .. "%]%]" ..
"|%[%[" .. escaped_player .. "|.-%]%]" ..
"|" .. escaped_player
-- Знайдемо весь рядок, де є гравець
local row = nil
for line in mw.ustring.gmatch(table_content, "[^\n]+") do
if mw.ustring.match(line, player_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")
result = mw.text.trim(result)
if result == "" then
result = mw.ustring.gsub(result, "%[%[([^%]]+)%]%]", "%1")
end
return result
end