Модуль:RandomPageFromCategory: различия между версиями
Материал из Поле цифровой дидактики
Patarakin (обсуждение | вклад) Нет описания правки |
Patarakin (обсуждение | вклад) Нет описания правки |
||
| Строка 1: | Строка 1: | ||
local p = {} | local p = {} | ||
local function pick( | local function pick(t) | ||
if not t or #t == 0 then | |||
return nil | |||
end | |||
math.randomseed(os.time()) | |||
return t[math.random(#t)] | |||
end | end | ||
function p. | 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 | end | ||
function p. | 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 | end | ||
return p | return p | ||
Текущая версия от 08:51, 19 мая 2026
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
