Модуль:RandomPageFromCategory
Материал из Поле цифровой дидактики
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
