Basculer le menu
Changer de menu des préférences
Basculer le menu personnel
Non connecté(e)
Votre adresse IP sera visible au public si vous faites des modifications.

« Module:PageInfo » : différence entre les versions

De Nefald
Hiob (discussion | contributions)
mAucun résumé des modifications
Hiob (discussion | contributions)
mAucun résumé des modifications
Balise : Révoqué
Ligne 1 : Ligne 1 :
local p = {}
-- =================================================================
 
-- FONCTION CORRIGÉE ET AMÉLIORÉE pour extraire la description
-- Fonction principale qui retourne toutes les infos d'une page
-- =================================================================
function p.getInfo(frame)
    local pageName = frame.args[1]
    if not pageName or pageName == '' then
        return "'''Erreur :''' Aucun nom de page fourni"
    end
   
    local pageObj = mw.title.new(pageName)
    if not pageObj then
        return "'''Erreur :''' Titre invalide : " .. pageName
    end
   
    if not pageObj.exists then
        return "'''Erreur :''' La page '" .. pageName .. "' n'existe pas"
    end
   
    local result = {}
   
    -- Informations de base
    table.insert(result, "=== Informations de la page ===")
    table.insert(result, "* '''Titre complet :''' " .. pageObj.prefixedText)
    table.insert(result, "* '''Namespace :''' " .. pageObj.nsText)
    table.insert(result, "* '''Titre (sans namespace) :''' " .. pageObj.text)
    table.insert(result, "* '''URL :''' " .. pageObj:fullUrl())
    table.insert(result, "* '''ID de la page :''' " .. pageObj.id)
   
    -- Contenu
    local content = pageObj:getContent()
    if content then
        local contentLength = mw.ustring.len(content)
        table.insert(result, "* '''Taille du contenu :''' " .. contentLength .. " caractères")
       
        -- Compter les sections
        local _, sectionCount = content:gsub('\n==', '\n==')
        table.insert(result, "* '''Nombre de sections :''' " .. sectionCount)
    end
   
    -- Catégories
    local categories = pageObj.categories
    if categories and #categories > 0 then
        table.insert(result, "* '''Catégories :''' " .. #categories)
        local catList = {}
        for _, cat in ipairs(categories) do
            table.insert(catList, cat.text)
        end
        table.insert(result, "** " .. table.concat(catList, ", "))
    else
        table.insert(result, "* '''Catégories :''' Aucune")
    end
   
    -- Protection
    if pageObj.protectionLevels and next(pageObj.protectionLevels) then
        table.insert(result, "* '''Protection :''' Oui")
        for action, level in pairs(pageObj.protectionLevels) do
            table.insert(result, "** " .. action .. " : " .. table.concat(level, ", "))
        end
    else
        table.insert(result, "* '''Protection :''' Non")
    end
   
    -- Redirection
    if pageObj.isRedirect then
        table.insert(result, "* '''Type :''' Page de redirection")
    else
        table.insert(result, "* '''Type :''' Page normale")
    end
   
    return table.concat(result, "\n")
end
 
-- Fonction pour obtenir uniquement le titre
function p.getTitle(frame)
    local pageName = frame.args[1]
    if not pageName or pageName == '' then
        return ''
    end
   
    local pageObj = mw.title.new(pageName)
    if not pageObj or not pageObj.exists then
        return "''Page inexistante''"
    end
   
    return pageObj.text
end
 
-- Fonction pour obtenir le namespace
function p.getNamespace(frame)
    local pageName = frame.args[1]
    if not pageName or pageName == '' then
        return ''
    end
   
    local pageObj = mw.title.new(pageName)
    if not pageObj or not pageObj.exists then
        return "''Page inexistante''"
    end
   
    return pageObj.nsText ~= '' and pageObj.nsText or 'Principal'
end
 
-- Fonction pour obtenir l'URL complète
function p.getUrl(frame)
    local pageName = frame.args[1]
    if not pageName or pageName == '' then
        return ''
    end
   
    local pageObj = mw.title.new(pageName)
    if not pageObj or not pageObj.exists then
        return "''Page inexistante''"
    end
   
    return pageObj:fullUrl()
end
 
-- Fonction pour obtenir les catégories
function p.getCategories(frame)
    local pageName = frame.args[1]
    if not pageName or pageName == '' then
        return ''
    end
   
    local pageObj = mw.title.new(pageName)
    if not pageObj or not pageObj.exists then
        return "''Page inexistante''"
    end
   
    local categories = pageObj.categories
    if not categories or #categories == 0 then
        return "''Aucune catégorie''"
    end
   
    local catList = {}
    for _, cat in ipairs(categories) do
        table.insert(catList, cat.text)
    end
   
    return table.concat(catList, ', ')
end
 
-- Fonction pour extraire la description d'une page
function p.getDescription(frame)
function p.getDescription(frame)
    local pageName = frame.args[1]
     -- CORRECTION 1 : Accepter un paramètre nommé "page" OU le premier paramètre non nommé.
    if not pageName or pageName == '' then
     -- Ceci rend le module plus flexible.
        return ''
     local pageName = frame.args.page or frame.args[1]
    end
   
    local pageObj = mw.title.new(pageName)
    if not pageObj or not pageObj.exists then
        return "''Page inexistante''"
    end
   
    local content = pageObj:getContent()
    if not content then
        return ''
    end
   
     -- PRIORITÉ 1 : Chercher {{SHORTDESC:...}} (magic word de l'extension)
    local desc = content:match('{{SHORTDESC:([^}]+)}}')
    if desc then
        desc = mw.text.trim(desc)
        if desc ~= '' then
            return desc
        end
    end
   
    -- PRIORITÉ 1bis : Chercher aussi {{Short description|...}} (template standard)
    desc = content:match('{{[Ss]hort%s+description%s*|%s*([^}]+)}}')
    if desc then
        desc = mw.text.trim(desc)
        desc = desc:gsub('|.*$', '') -- Nettoyer paramètres supplémentaires
        if desc ~= '' then
            return desc
        end
    end
   
    -- PRIORITÉ 2 : Chercher dans un champ description d'infobox
    desc = content:match('|%s*description%s*=%s*([^\n|]+)')
    if desc then
        desc = mw.text.trim(desc)
        if desc ~= '' and #desc > 10 then
            return desc
        end
    end
   
     -- PRIORITÉ 3 : Extraire le premier paragraphe après l'infobox
    local afterInfobox = content:match('}}%s*\n(.+)')
      
    if afterInfobox then
        local beforeSection = afterInfobox:match('^(.-)%s*\n%s*==')
       
        if not beforeSection then
            beforeSection = afterInfobox
        end
       
        local cleaned = beforeSection
       
        -- Supprimer les magic words et templates
        cleaned = cleaned:gsub('{{SHORTDESC:[^}]+}}', '')
        cleaned = cleaned:gsub('{{[^}]+}}', '')
        cleaned = cleaned:gsub('%[%[[Cc]ategor[yi]e?:[^%]]+%]%]', '')
        cleaned = cleaned:gsub('<[^>]+>', '')
        cleaned = cleaned:gsub('\n\n+', '\n')
        cleaned = cleaned:gsub("'''", '')
        cleaned = cleaned:gsub("''", '')
       
        for line in cleaned:gmatch('[^\n]+') do
            line = mw.text.trim(line)
           
            if line ~= ''
              and not line:match('^[*#:;]')
              and not line:match('^%s*{|')
              and not line:match('^%s*|}')
              and not line:match('^%s*|[^%[]')
              and #line > 30 then
               
                if #line > 300 then
                    local cut = line:sub(1, 297)
                    local lastPeriod = cut:match('.*()%.%s')
                    if lastPeriod and lastPeriod > 200 then
                        line = cut:sub(1, lastPeriod)
                    else
                        line = cut .. '...'
                    end
                end
               
                return line
            end
        end
    end
      
      
    return "''Aucune description disponible''"
end
-- Fonction pour obtenir la taille de la page
function p.getSize(frame)
    local pageName = frame.args[1]
     if not pageName or pageName == '' then
     if not pageName or pageName == '' then
        -- Pas de page fournie, on ne renvoie rien pour ne pas polluer DPL.
         return ''
         return ''
     end
     end
Ligne 244 : Ligne 14 :
     local pageObj = mw.title.new(pageName)
     local pageObj = mw.title.new(pageName)
     if not pageObj or not pageObj.exists then
     if not pageObj or not pageObj.exists then
         return "''Page inexistante''"
         return "''Page '" .. pageName .. "' inexistante''"
    end
   
    local content = pageObj:getContent()
    if not content then
        return '0'
    end
   
    return tostring(mw.ustring.len(content))
end
 
-- Fonction pour compter les sections
function p.countSections(frame)
    local pageName = frame.args[1]
    if not pageName or pageName == '' then
        return '0'
    end
   
    local pageObj = mw.title.new(pageName)
    if not pageObj or not pageObj.exists then
        return "''Page inexistante''"
     end
     end
      
      
     local content = pageObj:getContent()
     local content = pageObj:getContent()
     if not content then
     if not content then
        return '0'
         return "''Page '" .. pageName .. "' vide''"
    end
   
    local _, count = content:gsub('\n==', '\n==')
    return tostring(count)
end
 
-- Fonction pour vérifier si une page est protégée
function p.isProtected(frame)
    local pageName = frame.args[1]
    if not pageName or pageName == '' then
        return 'Non'
    end
   
    local pageObj = mw.title.new(pageName)
    if not pageObj or not pageObj.exists then
         return "''Page inexistante''"
    end
   
    if pageObj.protectionLevels and next(pageObj.protectionLevels) then
        return 'Oui'
    else
        return 'Non'
    end
end
 
-- Fonction pour vérifier si une page est une redirection
function p.isRedirect(frame)
    local pageName = frame.args[1]
    if not pageName or pageName == '' then
        return 'Non'
    end
   
    local pageObj = mw.title.new(pageName)
    if not pageObj or not pageObj.exists then
        return "''Page inexistante''"
    end
   
    return pageObj.isRedirect and 'Oui' or 'Non'
end
 
-- Fonction pour obtenir l'ID de la page
function p.getId(frame)
    local pageName = frame.args[1]
    if not pageName or pageName == '' then
        return ''
     end
     end
      
      
     local pageObj = mw.title.new(pageName)
     -- CORRECTION 2 : Utilisation d'une regex beaucoup plus robuste pour trouver la description.
    if not pageObj or not pageObj.exists then
     -- Elle capture le contenu (même sur plusieurs lignes) jusqu'au prochain paramètre.
        return "''Page inexistante''"
     -- (.-) : capture non-gourmande de n'importe quel caractère, y compris les retours à la ligne.
    end
     -- \n%s*| : s'arrête lorsqu'on trouve un retour à la ligne, du blanc, et un nouveau pipe,
   
     --         ce qui marque le début du paramètre suivant de l'infobox.
     return tostring(pageObj.id)
     local desc = content:match('|%s*description%s*=%s*(.-)%s*\n%s*|')
end
 
-- Fonction pour obtenir le titre complet (avec namespace)
function p.getFullTitle(frame)
     local pageName = frame.args[1]
    if not pageName or pageName == '' then
        return ''
    end
   
    local pageObj = mw.title.new(pageName)
    if not pageObj or not pageObj.exists then
        return "''Page inexistante''"
    end
   
    return pageObj.prefixedText
end
 
-- Fonction pour extraire l'image principale de la page
function p.getMainImage(frame)
     local pageName = frame.args[1]
    if not pageName or pageName == '' then
        return ''
    end
   
    local pageObj = mw.title.new(pageName)
    if not pageObj or not pageObj.exists then
        return "''Page inexistante''"
    end
   
    local content = pageObj:getContent()
    if not content then
        return ''
    end
   
     -- Chercher dans l'infobox d'abord
     local image = content:match('|%s*image%s*=%s*([^\n|]+)')
    if image then
        image = mw.text.trim(image)
        -- Nettoyer les éventuels paramètres de taille
        image = image:gsub('%s*|.*$', '')
        if image ~= '' then
            return image
        end
    end
   
    -- Chercher la première image dans le contenu
    image = content:match('%[%[Fichier:([^%]|]+)')
    if not image then
        image = content:match('%[%[File:([^%]|]+)')
    end
    if not image then
        image = content:match('%[%[Image:([^%]|]+)')
    end
   
    if image then
        return mw.text.trim(image)
    end
   
    return "''Aucune image''"
end
 
-- Fonction pour obtenir la date de dernière modification
function p.getLastModified(frame)
    local pageName = frame.args[1]
    if not pageName or pageName == '' then
        return ''
    end
   
    local pageObj = mw.title.new(pageName)
    if not pageObj or not pageObj.exists then
        return "''Page inexistante''"
    end
      
      
     -- Utiliser l'API pour obtenir la date de dernière modification
     if desc and desc ~= '' then
    local revId = pageObj.latest
        -- On a trouvé une description, on la nettoie et on la retourne.
    if not revId then
         return mw.text.trim(desc)
         return "''Indisponible''"
     end
     end
      
      
     return "Révision #" .. revId
    -- Si on n'a rien trouvé, on retourne un message clair.
     return "''Description non trouvée''"
end
end
return p

Version du 7 octobre 2025 à 11:48

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

-- =================================================================
-- FONCTION CORRIGÉE ET AMÉLIORÉE pour extraire la description
-- =================================================================
function p.getDescription(frame)
    -- CORRECTION 1 : Accepter un paramètre nommé "page" OU le premier paramètre non nommé.
    -- Ceci rend le module plus flexible.
    local pageName = frame.args.page or frame.args[1]
    
    if not pageName or pageName == '' then
        -- Pas de page fournie, on ne renvoie rien pour ne pas polluer DPL.
        return ''
    end
    
    local pageObj = mw.title.new(pageName)
    if not pageObj or not pageObj.exists then
        return "''Page '" .. pageName .. "' inexistante''"
    end
    
    local content = pageObj:getContent()
    if not content then
        return "''Page '" .. pageName .. "' vide''"
    end
    
    -- CORRECTION 2 : Utilisation d'une regex beaucoup plus robuste pour trouver la description.
    -- Elle capture le contenu (même sur plusieurs lignes) jusqu'au prochain paramètre.
    -- (.-) : capture non-gourmande de n'importe quel caractère, y compris les retours à la ligne.
    -- \n%s*| : s'arrête lorsqu'on trouve un retour à la ligne, du blanc, et un nouveau pipe,
    --          ce qui marque le début du paramètre suivant de l'infobox.
    local desc = content:match('|%s*description%s*=%s*(.-)%s*\n%s*|')
    
    if desc and desc ~= '' then
        -- On a trouvé une description, on la nettoie et on la retourne.
        return mw.text.trim(desc)
    end
    
    -- Si on n'a rien trouvé, on retourne un message clair.
    return "''Description non trouvée''"
end