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)
Page créée avec « local p = {} -- Fonction pour extraire la description function p.getDescription(frame) local pageName = frame.args[1] if not pageName or pageName == '' then return "''Erreur : nom de page manquant''" end local pageObj = mw.title.new(pageName) if not pageObj or not pageObj.exists then return "''Page inexistante''" end local texte = pageObj:getContent() if not texte then return "''Auc... »
 
Hiob (discussion | contributions)
Aucun résumé des modifications
Ligne 1 : Ligne 1 :
local p = {}
local p = {}


-- Fonction pour extraire la description
-- Fonction pour extraire la description d'une page
function p.getDescription(frame)
function p.getDescription(frame)
     local pageName = frame.args[1]
     local pageName = frame.args[1]
   
     if not pageName or pageName == '' then
     if not pageName or pageName == '' then
         return "''Erreur : nom de page manquant''"
         return ''
     end
     end
      
      
     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 inexistante''"
     end
     end
      
      
     local texte = pageObj:getContent()
    -- Extraire le contenu de la page
     local content = pageObj:getContent()
    if not content then
        return ''
    end
   
    -- Chercher une description dans différents formats
      
      
     if not texte then
    -- 1. Chercher dans un modèle {{Description|...}}
         return "''Aucune description''"
    local desc = content:match('{{[Dd]escription%s*|%s*([^}]+)}}')
     if desc then
         return mw.text.trim(desc)
     end
     end
      
      
     -- 1. Chercher {{Short description|...}}
     -- 2. Chercher dans un champ description d'infobox
     local shortDesc = texte:match('{{[Ss]hort%s+description|([^}]+)}}')
     desc = content:match('|%s*[Dd]escription%s*=%s*([^\n|]+)')
     if shortDesc then
     if desc then
         return mw.text.trim(shortDesc)
         return mw.text.trim(desc)
     end
     end
      
      
     -- 2. Chercher dans les infobox (paramètre description)
     -- 3. Chercher dans un modèle {{Info|description=...}}
     local infoboxDesc = texte:match('|%s*description%s*=%s*([^\n|]+)')
     desc = content:match('{{[Ii]nfo[^}]*|%s*[Dd]escription%s*=%s*([^|}\n]+)')
     if infoboxDesc then
     if desc then
         infoboxDesc = mw.text.trim(infoboxDesc)
         return mw.text.trim(desc)
        if #infoboxDesc > 10 then
            return infoboxDesc
        end
     end
     end
      
      
     -- 3. Utiliser TextExtract (premier paragraphe nettoyé)
     -- 4. Extraire le premier paragraphe (hors modèles)
     local extract = getTextExtract(texte)
    -- Nettoyer les modèles et les balises
     if extract and #extract > 10 then
     local cleaned = content:gsub('{{[^}]+}}', '')  -- Enlever les modèles
         return extract
    cleaned = cleaned:gsub('%[%[Category:[^%]]+%]%]', '')  -- Enlever les catégories
    cleaned = cleaned:gsub('%[%[Catégorie:[^%]]+%]%]', '')
    cleaned = cleaned:gsub('<[^>]+>', '')  -- Enlever les balises HTML
    cleaned = cleaned:gsub('^%s*==+[^=]+==[^\n]*\n', '') -- Enlever les titres
   
    -- Extraire le premier paragraphe significatif
     for line in cleaned:gmatch('[^\n]+') do
        line = mw.text.trim(line)
        if line ~= '' and not line:match('^[*#:]') and not line:match('^%s*{|') then
            -- Limiter la longueur
            if #line > 150 then
                line = line:sub(1, 147) .. '...'
            end
            return line
         end
     end
     end
      
      
     return "''Aucune description''"
     return "''Aucune description disponible''"
end
end


Ligne 48 : Ligne 65 :
function p.getDocumentation(frame)
function p.getDocumentation(frame)
     local pageName = frame.args[1]
     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 ''
    end
   
    local content = pageObj:getContent()
    if not content then
        return ''
    end
   
    -- Chercher différents formats de liens de documentation
   
    -- 1. Lien externe direct [http://... Documentation]
    local docLink = content:match('%[%s*(https?://[^%s%]]+)%s+[Dd]ocumentation%s*%]')
    if docLink then
        return '[' .. docLink .. ' Documentation]'
    end
   
    -- 2. Lien externe avec texte alternatif
    docLink = content:match('%[%s*(https?://[^%s%]]+)%s+[^%]]*[Dd]oc[^%]]*%]')
    if docLink then
        return '[' .. docLink .. ' Documentation]'
    end
   
    -- 3. Modèle {{Documentation|url=...}}
    docLink = content:match('{{[Dd]ocumentation%s*|%s*[Uu][Rr][Ll]%s*=%s*([^}|]+)')
    if docLink then
        docLink = mw.text.trim(docLink)
        return '[' .. docLink .. ' Documentation]'
    end
      
      
    -- 4. Champ documentation dans une infobox
    docLink = content:match('|%s*[Dd]ocumentation%s*=%s*%[%s*(https?://[^%s%]]+)')
    if docLink then
        return '[' .. docLink .. ' Documentation]'
    end
   
    -- 5. Chercher n'importe quel lien externe (dernier recours)
    docLink = content:match('%[%s*(https?://[^%s%]]+)')
    if docLink then
        return '[' .. docLink .. ' Lien externe]'
    end
   
    -- 6. Vérifier si une sous-page /Documentation existe
    local docPage = pageName .. '/Documentation'
    local docPageObj = mw.title.new(docPage)
    if docPageObj and docPageObj.exists then
        return '[[' .. docPage .. '|Documentation]]'
    end
   
    return "''Aucune documentation''"
end
-- Fonction pour obtenir toutes les infos d'une page (debug)
function p.getInfo(frame)
    local pageName = frame.args[1]
     if not pageName or pageName == '' then
     if not pageName or pageName == '' then
         return ''
         return 'Erreur : aucun nom de page fourni'
     end
     end
      
      
     local pageObj = mw.title.new(pageName)
     local pageObj = mw.title.new(pageName)
    if not pageObj then
        return 'Erreur : titre invalide'
    end
   
    if not pageObj.exists then
        return 'Erreur : la page n\'existe pas'
    end
      
      
    local result = {}
    table.insert(result, "'''Informations sur la page : " .. pageName .. "'''")
    table.insert(result, "\n* '''Existe :''' Oui")
    table.insert(result, "\n* '''Namespace :''' " .. pageObj.namespace)
    table.insert(result, "\n* '''Titre complet :''' " .. pageObj.fullText)
   
    local content = pageObj:getContent()
    if content then
        table.insert(result, "\n* '''Longueur du contenu :''' " .. #content .. " caractères")
        table.insert(result, "\n* '''Premiers 200 caractères :'''\n<pre>" .. content:sub(1, 200) .. "...</pre>")
    end
   
    table.insert(result, "\n* '''Description extraite :''' " .. p.getDescription(frame))
    table.insert(result, "\n* '''Documentation extraite :''' " .. p.getDocumentation(frame))
   
    return table.concat(result)
end
-- Fonction pour obtenir des statistiques sur une page
function p.getStats(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
     if not pageObj or not pageObj.exists then
         return ''
         return ''
    end
   
    local content = pageObj:getContent()
    if not content then
        return ''
    end
   
    local stats = {}
    stats.longueur = #content
    stats.lignes = select(2, content:gsub('\n', '\n')) + 1
    stats.mots = select(2, content:gsub('%S+', ''))
   
    return string.format('%d caractères, %d lignes, ~%d mots',
        stats.longueur, stats.lignes, stats.mots)
end
 
-- Fonction pour extraire l'auteur/créateur
function p.getAuthor(frame)
    local pageName = frame.args[1]
    if not pageName or pageName == '' then
        return ''
     end
     end
      
      
     local texte = pageObj:getContent()
     local pageObj = mw.title.new(pageName)
    if not pageObj or not pageObj.exists then
        return ''
    end
      
      
     if not texte then
    local content = pageObj:getContent()
         return ''
     if not content then
         return ''
     end
     end
      
      
     -- Chercher le paramètre documentation dans n'importe quelle infobox
     -- Chercher un champ auteur
     local docParam = texte:match('|%s*documentation%s*=%s*([^\n|]+)')
     local author = content:match('|%s*[Aa]uteur%s*=%s*([^\n|]+)')
    if author then
        return mw.text.trim(author)
    end
      
      
     if docParam then
     author = content:match('|%s*[Aa]uthor%s*=%s*([^\n|]+)')
        docParam = mw.text.trim(docParam)
    if author then
       
        return mw.text.trim(author)
        -- Si c'est déjà un lien wiki formaté [url texte]
        if docParam:match('^%[http') then
            return docParam
        end
       
        -- Si c'est une URL brute
        if docParam:match('^https?://') then
            return '[' .. docParam .. ' 📖 Documentation]'
        end
       
        -- Si c'est un lien wiki interne [[Page]]
        if docParam:match('^%[%[') then
            return docParam
        end
       
        -- Si c'est juste du texte
        if #docParam > 0 then
            return docParam
        end
     end
     end
      
      
     -- Chercher aussi |lien=, |url=, |site=
     author = content:match('{{[Aa]uteur%s*|%s*([^}]+)}}')
    for _, param in ipairs({'lien', 'url', 'site', 'website'}) do
    if author then
        local link = texte:match('|%s*' .. param .. '%s*=%s*([^\n|]+)')
        return mw.text.trim(author)
        if link then
            link = mw.text.trim(link)
            if link:match('^https?://') then
                return '[' .. link .. ' 🔗 Lien]'
            end
        end
     end
     end
      
      
     return ''
     return ''
end
end


-- Fonction interne pour extraire le premier paragraphe (façon TextExtract)
-- Fonction pour extraire la version
function getTextExtract(texte)
function p.getVersion(frame)
     -- Supprimer les infobox et templates au début
     local pageName = frame.args[1]
     texte = texte:gsub('{{.-}}', '')
     if not pageName or pageName == '' then
        return ''
    end
      
      
     -- Supprimer les balises HTML
     local pageObj = mw.title.new(pageName)
     texte = texte:gsub('<[^>]+>', '')
     if not pageObj or not pageObj.exists then
        return ''
    end
      
      
     -- Supprimer les références
    local content = pageObj:getContent()
     texte = texte:gsub('<ref[^>]*>.-</ref>', '')
    if not content then
    texte = texte:gsub('<ref[^/>]*/', '')
        return ''
    end
   
     -- Chercher un champ version
     local version = content:match('|%s*[Vv]ersion%s*=%s*([^\n|]+)')
    if version then
        return mw.text.trim(version)
    end
      
      
     -- Supprimer les liens internes mais garder le texte affiché
     version = content:match('{{[Vv]ersion%s*|%s*([^}]+)}}')
    texte = texte:gsub('%[%[([^%]|]+)|([^%]]+)%]%]', '%2')
     if version then
     texte = texte:gsub('%[%[([^%]]+)%]%]', '%1')
        return mw.text.trim(version)
    end
      
      
     -- Supprimer les liens externes
     -- Chercher un pattern v1.2.3 ou version 1.2.3
     texte = texte:gsub('%[http[^%]]+%]', '')
     version = content:match('[Vv]ersion%s*:?%s*(%d+%.%d+[%.%d]*)')
    if version then
        return version
    end
      
      
     -- Supprimer les balises wiki (gras, italique)
     version = content:match('v(%d+%.%d+[%.%d]*)')
    texte = texte:gsub("'''", '')
     if version then
     texte = texte:gsub("''", '')
        return version
    end
      
      
     -- Supprimer les fichiers/images
     return ''
    texte = texte:gsub('%[%[[Ff]ichier:.-]]', '')
end
     texte = texte:gsub('%[%[[Ii]mage:.-]]', '')
 
     texte = texte:gsub('%[%[[Ff]ile:.-]]', '')
-- Fonction pour extraire la date de mise à jour
function p.getUpdateDate(frame)
     local pageName = frame.args[1]
     if not pageName or pageName == '' then
        return ''
    end
      
      
     -- Trouver le premier paragraphe significatif
    local pageObj = mw.title.new(pageName)
     for ligne in texte:gmatch('[^\n]+') do
    if not pageObj or not pageObj.exists then
         ligne = mw.text.trim(ligne)
        return ''
       
    end
        -- Ignorer les lignes vides, titres, listes
   
        if #ligne > 30 and not ligne:match('^[=*#:;]') and not ligne:match('^{|') then
    local content = pageObj:getContent()
            -- Limiter à 200 caractères
    if not content then
            if #ligne > 200 then
        return ''
                ligne = ligne:sub(1, 197) .. '...'
    end
            end
   
            return ligne
     -- Chercher une date de mise à jour
        end
     local date = content:match('|%s*[Mm]ise%s*à%s*jour%s*=%s*([^\n|]+)')
    if date then
         return mw.text.trim(date)
    end
   
    date = content:match('|%s*[Uu]pdate[Dd]?%s*=%s*([^\n|]+)')
    if date then
        return mw.text.trim(date)
    end
   
    date = content:match('|%s*[Dd]ate%s*=%s*([^\n|]+)')
    if date then
        return mw.text.trim(date)
     end
     end
      
      
     return nil
     return ''
end
end


return p
return p

Version du 4 octobre 2025 à 08:27

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

local p = {}

-- Fonction pour extraire la description d'une page
function p.getDescription(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
    
    -- Extraire le contenu de la page
    local content = pageObj:getContent()
    if not content then
        return ''
    end
    
    -- Chercher une description dans différents formats
    
    -- 1. Chercher dans un modèle {{Description|...}}
    local desc = content:match('{{[Dd]escription%s*|%s*([^}]+)}}')
    if desc then
        return mw.text.trim(desc)
    end
    
    -- 2. Chercher dans un champ description d'infobox
    desc = content:match('|%s*[Dd]escription%s*=%s*([^\n|]+)')
    if desc then
        return mw.text.trim(desc)
    end
    
    -- 3. Chercher dans un modèle {{Info|description=...}}
    desc = content:match('{{[Ii]nfo[^}]*|%s*[Dd]escription%s*=%s*([^|}\n]+)')
    if desc then
        return mw.text.trim(desc)
    end
    
    -- 4. Extraire le premier paragraphe (hors modèles)
    -- Nettoyer les modèles et les balises
    local cleaned = content:gsub('{{[^}]+}}', '')  -- Enlever les modèles
    cleaned = cleaned:gsub('%[%[Category:[^%]]+%]%]', '')  -- Enlever les catégories
    cleaned = cleaned:gsub('%[%[Catégorie:[^%]]+%]%]', '')
    cleaned = cleaned:gsub('<[^>]+>', '')  -- Enlever les balises HTML
    cleaned = cleaned:gsub('^%s*==+[^=]+==[^\n]*\n', '')  -- Enlever les titres
    
    -- Extraire le premier paragraphe significatif
    for line in cleaned:gmatch('[^\n]+') do
        line = mw.text.trim(line)
        if line ~= '' and not line:match('^[*#:]') and not line:match('^%s*{|') then
            -- Limiter la longueur
            if #line > 150 then
                line = line:sub(1, 147) .. '...'
            end
            return line
        end
    end
    
    return "''Aucune description disponible''"
end

-- Fonction pour extraire le lien de documentation
function p.getDocumentation(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 ''
    end
    
    local content = pageObj:getContent()
    if not content then
        return ''
    end
    
    -- Chercher différents formats de liens de documentation
    
    -- 1. Lien externe direct [http://... Documentation]
    local docLink = content:match('%[%s*(https?://[^%s%]]+)%s+[Dd]ocumentation%s*%]')
    if docLink then
        return '[' .. docLink .. ' Documentation]'
    end
    
    -- 2. Lien externe avec texte alternatif
    docLink = content:match('%[%s*(https?://[^%s%]]+)%s+[^%]]*[Dd]oc[^%]]*%]')
    if docLink then
        return '[' .. docLink .. ' Documentation]'
    end
    
    -- 3. Modèle {{Documentation|url=...}}
    docLink = content:match('{{[Dd]ocumentation%s*|%s*[Uu][Rr][Ll]%s*=%s*([^}|]+)')
    if docLink then
        docLink = mw.text.trim(docLink)
        return '[' .. docLink .. ' Documentation]'
    end
    
    -- 4. Champ documentation dans une infobox
    docLink = content:match('|%s*[Dd]ocumentation%s*=%s*%[%s*(https?://[^%s%]]+)')
    if docLink then
        return '[' .. docLink .. ' Documentation]'
    end
    
    -- 5. Chercher n'importe quel lien externe (dernier recours)
    docLink = content:match('%[%s*(https?://[^%s%]]+)')
    if docLink then
        return '[' .. docLink .. ' Lien externe]'
    end
    
    -- 6. Vérifier si une sous-page /Documentation existe
    local docPage = pageName .. '/Documentation'
    local docPageObj = mw.title.new(docPage)
    if docPageObj and docPageObj.exists then
        return '[[' .. docPage .. '|Documentation]]'
    end
    
    return "''Aucune documentation''"
end

-- Fonction pour obtenir toutes les infos d'une page (debug)
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'
    end
    
    if not pageObj.exists then
        return 'Erreur : la page n\'existe pas'
    end
    
    local result = {}
    table.insert(result, "'''Informations sur la page : " .. pageName .. "'''")
    table.insert(result, "\n* '''Existe :''' Oui")
    table.insert(result, "\n* '''Namespace :''' " .. pageObj.namespace)
    table.insert(result, "\n* '''Titre complet :''' " .. pageObj.fullText)
    
    local content = pageObj:getContent()
    if content then
        table.insert(result, "\n* '''Longueur du contenu :''' " .. #content .. " caractères")
        table.insert(result, "\n* '''Premiers 200 caractères :'''\n<pre>" .. content:sub(1, 200) .. "...</pre>")
    end
    
    table.insert(result, "\n* '''Description extraite :''' " .. p.getDescription(frame))
    table.insert(result, "\n* '''Documentation extraite :''' " .. p.getDocumentation(frame))
    
    return table.concat(result)
end

-- Fonction pour obtenir des statistiques sur une page
function p.getStats(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 ''
    end
    
    local content = pageObj:getContent()
    if not content then
        return ''
    end
    
    local stats = {}
    stats.longueur = #content
    stats.lignes = select(2, content:gsub('\n', '\n')) + 1
    stats.mots = select(2, content:gsub('%S+', ''))
    
    return string.format('%d caractères, %d lignes, ~%d mots', 
        stats.longueur, stats.lignes, stats.mots)
end

-- Fonction pour extraire l'auteur/créateur
function p.getAuthor(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 ''
    end
    
    local content = pageObj:getContent()
    if not content then
        return ''
    end
    
    -- Chercher un champ auteur
    local author = content:match('|%s*[Aa]uteur%s*=%s*([^\n|]+)')
    if author then
        return mw.text.trim(author)
    end
    
    author = content:match('|%s*[Aa]uthor%s*=%s*([^\n|]+)')
    if author then
        return mw.text.trim(author)
    end
    
    author = content:match('{{[Aa]uteur%s*|%s*([^}]+)}}')
    if author then
        return mw.text.trim(author)
    end
    
    return ''
end

-- Fonction pour extraire la version
function p.getVersion(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 ''
    end
    
    local content = pageObj:getContent()
    if not content then
        return ''
    end
    
    -- Chercher un champ version
    local version = content:match('|%s*[Vv]ersion%s*=%s*([^\n|]+)')
    if version then
        return mw.text.trim(version)
    end
    
    version = content:match('{{[Vv]ersion%s*|%s*([^}]+)}}')
    if version then
        return mw.text.trim(version)
    end
    
    -- Chercher un pattern v1.2.3 ou version 1.2.3
    version = content:match('[Vv]ersion%s*:?%s*(%d+%.%d+[%.%d]*)')
    if version then
        return version
    end
    
    version = content:match('v(%d+%.%d+[%.%d]*)')
    if version then
        return version
    end
    
    return ''
end

-- Fonction pour extraire la date de mise à jour
function p.getUpdateDate(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 ''
    end
    
    local content = pageObj:getContent()
    if not content then
        return ''
    end
    
    -- Chercher une date de mise à jour
    local date = content:match('|%s*[Mm]ise%s*à%s*jour%s*=%s*([^\n|]+)')
    if date then
        return mw.text.trim(date)
    end
    
    date = content:match('|%s*[Uu]pdate[Dd]?%s*=%s*([^\n|]+)')
    if date then
        return mw.text.trim(date)
    end
    
    date = content:match('|%s*[Dd]ate%s*=%s*([^\n|]+)')
    if date then
        return mw.text.trim(date)
    end
    
    return ''
end

return p