Модуль:RandomPageFromCategory

Материал из Поле цифровой дидактики
Версия от 08:51, 19 мая 2026; Patarakin (обсуждение | вклад)
(разн.) ← Предыдущая версия | Текущая версия (разн.) | Следующая версия → (разн.)

local p = {}

local function pick(t)
    if not t or #t == 0 then
        return nil
    end
    math.randomseed(os.time())
    return t[math.random(#t)]
end

function p.getRandomTitle(category)
    if not mw.smw then
        return nil, "mw.smw недоступен"
    end

    local results = mw.smw.ask('[[Category:' .. category .. ']]')
    if not results or #results == 0 then
        return nil, "В категории нет страниц: " .. category
    end

    return pick(results), nil
end

function p.fromCategory(frame)
    local cat = frame.args[1] or "Book"
    local title, err = p.getRandomTitle(cat)

    if not title then
        return err
    end

    return "[[" .. title .. "]]"
end

return p