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.

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