Module:PageInfo
De Nefald
Autres actions
La documentation pour ce module peut être créée à Module:PageInfo/doc
local p = {}
-- 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)
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
-- 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
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 '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
local content = pageObj:getContent()
if not content then
return '0'
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
local pageObj = mw.title.new(pageName)
if not pageObj or not pageObj.exists then
return "''Page inexistante''"
end
return tostring(pageObj.id)
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
local revId = pageObj.latest
if not revId then
return "''Indisponible''"
end
return "Révision #" .. revId
end
return p