Aller au contenu

Module:ImageDuJour

De Wikiquestia

La documentation pour ce module peut être créée à Module:ImageDuJour/doc

local p = {}

-- Fonction utilitaire : sélection aléatoire dans une table
local function aleatoire(tab)
    math.randomseed(os.time())
    return tab[math.random(#tab)]
end

-- Fonction : récupère les fichiers d'une catégorie
function p.depuisCategorie(frame)
    local categorie = frame.args[1] or "Tableau"
    local titreCat = mw.title.new("Catégorie:" .. categorie)

    if not titreCat or not titreCat.exists then
        return "<span style='color:red;'>Catégorie inexistante.</span>"
    end

    -- Récupère les membres de la catégorie
    local membres = titreCat:getMembers()
    local fichiers = {}

    for _, membre in ipairs(membres) do
        if membre.nsText == "Fichier" then
            table.insert(fichiers, membre.prefixedText)
        end
    end

    if #fichiers == 0 then
        return "<span style='color:red;'>Aucun fichier trouvé dans la catégorie.</span>"
    end

    return aleatoire(fichiers)
end

return p