Модуль:RandomPageFromCategory: различия между версиями

Материал из Поле цифровой дидактики
Нет описания правки
Нет описания правки
 
(не показано 5 промежуточных версий этого же участника)
Строка 1: Строка 1:
-- Module:RandomPageFromCategory
local p = {}
local p = {}


function p.fromCategory(frame)
math.randomseed(os.time())
  local cat = frame.args[1] or "Book"
 
  return "[[Special:RandomInCategory/" .. cat .. "|случайная страница из " .. cat .. "]]"
local function pickOne(t)
    if not t or #t == 0 then
        return nil
    end
    return t[math.random(#t)]
end
 
function p.getRandomTitle(category)
    if not mw.smw then
        return nil, "Ошибка: mw.smw недоступен"
    end
 
    if not category or category == "" then
        category = "Book"
    end
 
    local results = mw.smw.ask({
        string.format('[[Category:%s]]', category),
        '?#-=title',
        'limit=200'
    })
 
    if not results or #results == 0 then
        return nil, "В категории нет страниц: " .. category
    end
 
    local item = pickOne(results)
 
    if type(item) == "table" then
        return item.title or item[1] or nil, nil
    end
 
    return item, nil
end
 
function p.main(frame)
    local category = frame.args.category or frame.args[1] or "Book"
    local title, err = p.getRandomTitle(category)
 
    if not title or title == "" then
        return err or "Не удалось получить название страницы"
    end
 
    return title
end
end


return p
return p

Текущая версия от 07:48, 21 мая 2026


local p = {}

math.randomseed(os.time())

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

function p.getRandomTitle(category)
    if not mw.smw then
        return nil, "Ошибка: mw.smw недоступен"
    end

    if not category or category == "" then
        category = "Book"
    end

    local results = mw.smw.ask({
        string.format('[[Category:%s]]', category),
        '?#-=title',
        'limit=200'
    })

    if not results or #results == 0 then
        return nil, "В категории нет страниц: " .. category
    end

    local item = pickOne(results)

    if type(item) == "table" then
        return item.title or item[1] or nil, nil
    end

    return item, nil
end

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

    if not title or title == "" then
        return err or "Не удалось получить название страницы"
    end

    return title
end

return p