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

Материал из Поле цифровой дидактики
Нет описания правки
Нет описания правки
 
Строка 1: Строка 1:
-- Module:RandomPageFromCategory
local p = {}
local p = {}


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


function p.fromCategory(frame)
function p.getRandomTitle(category)
  local cat = frame.args[1] or "Book"
    if not mw.smw then
  return "[[Special:RandomInCategory/" .. cat .. "|случайная страница из " .. cat .. "]]"
        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.fromList(frame)
function p.fromCategory(frame)
  local items = {}
    local cat = frame.args[1] or "Book"
  local i = 1
     local title, err = p.getRandomTitle(cat)
  while frame.args[i] do
     table.insert(items, frame.args[i])
    i = i + 1
  end


  local page = pick(items)
    if not title then
  if not page then
        return err
    return "Нет страниц в списке"
    end
  end


  return page
    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