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

Материал из Поле цифровой дидактики
Нет описания правки
Нет описания правки
 
Строка 19: Строка 19:
     end
     end


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


     if not results or #results == 0 then
     if not results or #results == 0 then
Строка 29: Строка 32:


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


Строка 39: Строка 42:
     local title, err = p.getRandomTitle(category)
     local title, err = p.getRandomTitle(category)


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



Текущая версия от 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