Aller au contenu

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

De Stargate Wiki Sémantique
Contenu supprimé Contenu ajouté
Aucun résumé des modifications
Aucun résumé des modifications
 
(21 versions intermédiaires par le même utilisateur non affichées)
Ligne 1 : Ligne 1 :
local data = mw.loadData("Module:Episode/data")
------------------------------------------------------------
local p = {}
-- DIAGNOSTIC SAFE — n'affecte pas le module
------------------------------------------------------------


-- Sécurisation des valeurs nil
local function dump_table(t)
local function safe(v)
if type(t) ~= "table" then
return v or ""
return tostring(t)
end
local out = {}
for k,v in pairs(t) do
table.insert(out, tostring(k) .. "=" .. tostring(v))
end
return "{ " .. table.concat(out, ", ") .. " }"
end
end


-- Normalisation robuste des alias
local name = "Module:Episode/MultiSeries"
local function normalizeAlias(s)
if not s then return "" end


s = mw.ustring.lower(s)
local before = package.loaded[name]
local ok, result = pcall(require, name)
local after = package.loaded[name]


-- 0) Supprimer les apostrophes HTML
mw.log("=== DIAGNOSTIC SAFE ===")
s = mw.ustring.gsub(s, "'", "")
mw.log("Nom require() = >" .. name .. "<")
s = mw.ustring.gsub(s, "&apos;", "")
mw.log("--- AVANT require() ---")
s = mw.ustring.gsub(s, "&rsquo;", "")
mw.log(tostring(before))
s = mw.ustring.gsub(s, "&lsquo;", "")
mw.log("--- RESULTAT require() ---")
mw.log("pcall ok = " .. tostring(ok))
mw.log("result = " .. tostring(result))
mw.log("type(result) = " .. type(result))
mw.log("--- APRES require() ---")
mw.log(tostring(after))
mw.log("--- package.loaded[name] ---")
mw.log(tostring(package.loaded[name]))


-- 1) Supprimer les apostrophes simples et typographiques
------------------------------------------------------------
s = mw.ustring.gsub(s, "[’']", "")
-- Module:Episode (ULTRA OPTIMISÉ AVEC CACHE GLOBAL)
-- API publique pour accéder aux épisodes (toutes séries)
-- Backend : Module:Episode/MultiSeries
------------------------------------------------------------


-- 2) Supprimer les virgules sans laisser d'espace
local Index = require("Module:Episode/MultiSeries")
s = mw.ustring.gsub(s, ",", "")
local normalize = Index.normalize
local p = {}


-- 3) Remplacement des accents
------------------------------------------------------------
local accents = {
-- CACHE GLOBAL (résultats mémorisés par page)
["à"]="a", ["á"]="a", ["â"]="a", ["ä"]="a", ["ã"]="a", ["å"]="a",
------------------------------------------------------------
["ç"]="c",
local CACHE = {
["è"]="e", ["é"]="e", ["ê"]="e", ["ë"]="e",
entry = {}, -- ID/alias → ep
["ì"]="i", ["í"]="i", ["î"]="i", ["ï"]="i",
title = {}, -- id → titre prétraité
link = {}, -- id → lien [[...]]
["ñ"]="n",
["ò"]="o", ["ó"]="o", ["ô"]="o", ["ö"]="o", ["õ"]="o",
full = {}, -- id → namespace:page
["ù"]="u", ["ú"]="u", ["û"]="u", ["ü"]="u",
number = {}, -- id → numéro d’épisode
["ý"]="y", ["ÿ"]="y"
season = {}, -- id → numéro de saison
}
}
s = mw.ustring.gsub(s, ".", accents)


-- 4) Remplacer les autres caractères non alphanumériques par un espace
------------------------------------------------------------
s = mw.ustring.gsub(s, "[^%w]", " ")
-- Récupère une entrée d’épisode depuis un argument
------------------------------------------------------------
local function getEntry(frame)
local key = frame.args[1]
if not key or key == "" then
return nil
end


-- Cache direct
-- 5) Compresser les espaces
if CACHE.entry[key] ~= nil then
s = mw.ustring.gsub(s, "%s+", " ")
return CACHE.entry[key]
end


-- 6) Trim
-- Normalisation identique à MultiSeries
local norm = normalize(key)
s = mw.text.trim(s)


return s
-- 1) Recherche par ID exact
if Index.by_id[key] then
CACHE.entry[key] = Index.by_id[key]
return CACHE.entry[key]
end

-- 2) Recherche par alias normalisé
local list = Index.by_alias[norm]
if list and list[1] then
CACHE.entry[key] = list[1]
return CACHE.entry[key]
end

CACHE.entry[key] = nil
return nil
end
end


------------------------------------------------------------
-- Prétraitement du titre (parser)
------------------------------------------------------------
local function preprocessTitle(frame, ep)
local id = ep.id


-- Exécution des parseurs MediaWiki
if CACHE.title[id] then
local function parse(wikitext)
return CACHE.title[id]
return mw.getCurrentFrame():preprocess(wikitext)
end

local raw = ep.title_fr or ep.page_title_fr or ep.id or ""
local parsed = frame:preprocess(raw)

CACHE.title[id] = parsed
return parsed
end
end


-- Function 1: Get full episode link from key
------------------------------------------------------------
function p.getEpisode(frame)
-- Construction d’un lien d’épisode (sécurisé)
local key = normalizeAlias(frame.args[1])
------------------------------------------------------------
local entry = data[key]
local function buildEpisodeLink(frame, ep, labelOverride)
local id = ep.id


if not entry then
-- Cache si pas de label personnalisé
return "Épisode non référencé"
if not labelOverride and CACHE.link[id] then
return CACHE.link[id]
end
end


local title = labelOverride or preprocessTitle(frame, ep)
local ns = safe(entry[1])
local page = ep.page_title_fr or ep.id
local link = safe(entry[2])
local title = safe(entry[3])


local link
local wikitext
if not ep.namespace_fr or ep.namespace_fr == "" then
if ns == "" then
wikitext = string.format("[[%s|%s]]", link, title)
-- Films
link = string.format("[[%s|%s]]", page, title)
else
else
wikitext = string.format("[[%s:%s|%s]]", ns, link, title)
-- Épisodes normaux
link = string.format("[[%s:%s|%s]]", ep.namespace_fr, page, title)
end
end


return parse(wikitext)
if not labelOverride then
CACHE.link[id] = link
end

return link
end
end


-- Function 2: Force namespace to "Credits:"
------------------------------------------------------------
function p.getCreditsLink(frame)
-- 1. Lien normal vers l’épisode
local key = normalizeAlias(frame.args[1])
------------------------------------------------------------
local entry = data[key]
function p.getEpisode(frame)
local ep = getEntry(frame)
if not ep then return "Épisode ou film non référencé" end
return buildEpisodeLink(frame, ep)
end


if not entry then
------------------------------------------------------------
return "Épisode non référencé"
-- 2. Lien vers Crédits:
end
------------------------------------------------------------
function p.getCreditsLink(frame)
local ep = getEntry(frame)
if not ep then return "Épisode ou film non référencé" end


local title = preprocessTitle(frame, ep)
local link = safe(entry[2])
local page = ep.page_title_fr or ep.id
local title = safe(entry[3])


return string.format("[[Crédits:%s|%s]]", page, title)
return parse(string.format("[[Crédits:%s|%s]]", link, title))
end
end


-- Function 3: Force namespace to "Transcript:"
------------------------------------------------------------
-- 3. Lien vers Retranscription:
------------------------------------------------------------
function p.getTranscriptLink(frame)
function p.getTranscriptLink(frame)
local ep = getEntry(frame)
local key = normalizeAlias(frame.args[1])
local entry = data[key]
if not ep then return "Épisode ou film non référencé" end


if not entry then
local title = preprocessTitle(frame, ep)
return "Épisode non référencé"
local page = ep.page_title_fr or ep.id
end


local link = safe(entry[2])
return string.format("[[Retranscription:%s|%s]]", page, title)
local title = safe(entry[3])

return parse(string.format("[[Retranscription:%s|%s]]", link, title))
end
end


-- Function 4: Force namespace to "Quotes:"
------------------------------------------------------------
-- 4. Lien vers Citations:
------------------------------------------------------------
function p.getQuotesLink(frame)
function p.getQuotesLink(frame)
local ep = getEntry(frame)
local key = normalizeAlias(frame.args[1])
local entry = data[key]
if not ep then return "Épisode ou film non référencé" end

if not entry then
return "Épisode non référencé"
end


local title = preprocessTitle(frame, ep)
local link = safe(entry[2])
local page = ep.page_title_fr or ep.id
local title = safe(entry[3])


return string.format("[[Citations:%s|%s]]", page, title)
return parse(string.format("[[Citations:%s|%s]]", link, title))
end
end


-- Function 5: Force namespace to "Category:Images from"
------------------------------------------------------------
-- 5. Lien vers Catégorie:Images de ...
------------------------------------------------------------
function p.getImagesLink(frame)
function p.getImagesLink(frame)
local ep = getEntry(frame)
local key = normalizeAlias(frame.args[1])
local entry = data[key]
if not ep then return "Épisode ou film non référencé" end


if not entry then
local title = preprocessTitle(frame, ep)
return "Épisode non référencé"
local page = ep.page_title_fr or ep.id
end


local link = safe(entry[2])
return string.format("[[:Catégorie:Images de %s|%s]]", page, title)
local title = safe(entry[3])

return parse(string.format("[[:Catégorie:Images de %s|%s]]", link, title))
end
end


-- Function 6: Retrieve from link
------------------------------------------------------------
-- 6. Récupère uniquement le titre de page (sans namespace)
------------------------------------------------------------
function p.getEpisodeLink(frame)
function p.getEpisodeLink(frame)
local ep = getEntry(frame)
local key = normalizeAlias(frame.args[1])
local entry = data[key]
if not ep then return "Épisode ou film non référencé" end

return ep.page_title_fr or ep.id
if not entry then
return "Épisode non référencé"
end

return safe(entry[2])
end
end


-- Function 7: Get full episode link only from key
------------------------------------------------------------
-- 7. Récupère le lien complet (namespace + titre)
------------------------------------------------------------
function p.getEpisodeFullLink(frame)
function p.getEpisodeFullLink(frame)
local ep = getEntry(frame)
local key = normalizeAlias(frame.args[1])
local entry = data[key]
if not ep then return "Épisode ou film non référencé" end


local id = ep.id
if not entry then
return "Épisode non référencé"
if CACHE.full[id] then
return CACHE.full[id]
end
end


local page = ep.page_title_fr or ep.id
local ns = safe(entry[1])
local full
local link = safe(entry[2])


if not ep.namespace_fr or ep.namespace_fr == "" then
if ns == "" then
full = page
return link
else
else
full = string.format("%s:%s", ep.namespace_fr, page)
return ns .. ":" .. link
end
end

CACHE.full[id] = full
return full
end
end


-- Function 8: Get episode title only from key
------------------------------------------------------------
-- 8. Récupère uniquement le titre français (prétraité)
------------------------------------------------------------
function p.getEpisodeTitle(frame)
function p.getEpisodeTitle(frame)
local ep = getEntry(frame)
local key = normalizeAlias(frame.args[1])
local entry = data[key]
if not ep then return "Épisode ou film non référencé" end
return preprocessTitle(frame, ep)
end


if not entry then
------------------------------------------------------------
return "Episode not found"
-- 9. Numéro d’épisode (2 chiffres)
------------------------------------------------------------
function p.getEpisodeNumber(frame)
local ep = getEntry(frame)
if not ep then return "Épisode ou film non référencé" end

local id = ep.id
if CACHE.number[id] then
return CACHE.number[id]
end
end


return safe(entry[3])
local n = tonumber(ep.episode)
local out = n and string.format("%02d", n) or ""

CACHE.number[id] = out
return out
end
end


-- Alias → pages
------------------------------------------------------------
function p.aliasToPages()
-- 10. Numéro de saison
local map = {}
------------------------------------------------------------
function p.getSeasonNumber(frame)
local ep = getEntry(frame)
if not ep then return "Épisode ou film non référencé" end


for alias, entry in pairs(data) do
local id = ep.id
local link = entry[2]
if CACHE.season[id] then

return CACHE.season[id]
if not map[alias] then
map[alias] = { link }
else
local exists = false
for _, v in ipairs(map[alias]) do
if v == link then
exists = true
break
end
end
if not exists then
table.insert(map[alias], link)
end
end
end
end


return map
local out = ep.season and tostring(ep.season) or ""
CACHE.season[id] = out
return out
end
end


------------------------------------------------------------
-- 12. Génération d’une liste d’épisodes (films-safe)
------------------------------------------------------------
function p.renderList(frame)
local serie = frame.args["serie"]
local saison = tonumber(frame.args["saison"])
local liste = frame.args["liste"]


function p.debugAllAliases()
local out = {}
local out = {}
table.insert(out, "== Vérification complète des alias ==")


for alias, entry in pairs(data) do
------------------------------------------------------------
local norm = normalizeAlias(alias)
-- Mode 1 : liste personnalisée
local ok = data[norm] and "✔" or "✘"
------------------------------------------------------------
if liste and liste ~= "" then
for id in mw.text.gsplit(liste, ",") do
id = mw.text.trim(id)


local ep =
table.insert(out,
Index.by_id[id]
string.format(
or (Index.by_alias[id] and Index.by_alias[id][1])
"* %s <code>%s</code> → <code>%s</code>%s",
ok,

if ep then
alias,
norm,
table.insert(out, "* " .. frame:preprocess(ep.title_fr or ep.page_title_fr))
else
ok == "✘" and " (introuvable)" or ""
table.insert(out, "* (inconnu) " .. id)
)
end
)
end
return table.concat(out, "\n")
end
end


return table.concat(out, "\n")
------------------------------------------------------------
end
-- Mode 2 : série + saison
------------------------------------------------------------
if serie and saison then
for _, ep in pairs(Index.episodes) do
if ep.series == serie and ep.season == saison then
table.insert(out, {
sort = tonumber(ep.episode) or 0,
text = "* " .. frame:preprocess(ep.title_fr or ep.page_title_fr)
})
end
end


function p.testTitle(frame)
table.sort(out, function(a,b) return a.sort < b.sort end)
local input = frame.args[1] or ""
local norm = normalizeAlias(input)
local exists = data[norm] ~= nil


local lines = {}
local out = {}
table.insert(out, "== Test de normalisation ==")
for _, item in ipairs(out) do
table.insert(out, "* Entrée : <code>" .. input .. "</code>")
table.insert(lines, item.text)
table.insert(out, "* Normalisé : <code>" .. norm .. "</code>")
end
table.insert(out, "* Trouvé : " .. (exists and "✔ oui" or "✘ non"))

return table.concat(lines, "\n")
end

------------------------------------------------------------
-- Mode 3 : série complète
------------------------------------------------------------
if serie then
for _, ep in pairs(Index.episodes) do
if ep.series == serie then
local s = (tonumber(ep.season) or 0) * 100 + (tonumber(ep.episode) or 0)
table.insert(out, {
sort = s,
text = "* " .. frame:preprocess(ep.title_fr or ep.page_title_fr)
})
end
end

table.sort(out, function(a,b) return a.sort < b.sort end)

local lines = {}
for _, item in ipairs(out) do
table.insert(lines, item.text)
end

return table.concat(lines, "\n")
end


return "Aucun paramètre valide fourni."
return table.concat(out, "\n")
end
end



Dernière version du 7 juin 2026 à 14:25

Documentation icon Documentation module[créer]
local data = mw.loadData("Module:Episode/data")
local p = {}

-- Sécurisation des valeurs nil
local function safe(v)
    return v or ""
end

-- Normalisation robuste des alias
local function normalizeAlias(s)
    if not s then return "" end

    s = mw.ustring.lower(s)

    -- 0) Supprimer les apostrophes HTML
    s = mw.ustring.gsub(s, "&#39;", "")
    s = mw.ustring.gsub(s, "&apos;", "")
    s = mw.ustring.gsub(s, "&rsquo;", "")
    s = mw.ustring.gsub(s, "&lsquo;", "")

    -- 1) Supprimer les apostrophes simples et typographiques
    s = mw.ustring.gsub(s, "[’']", "")

    -- 2) Supprimer les virgules sans laisser d'espace
    s = mw.ustring.gsub(s, ",", "")

    -- 3) Remplacement des accents
    local accents = {
        ["à"]="a", ["á"]="a", ["â"]="a", ["ä"]="a", ["ã"]="a", ["å"]="a",
        ["ç"]="c",
        ["è"]="e", ["é"]="e", ["ê"]="e", ["ë"]="e",
        ["ì"]="i", ["í"]="i", ["î"]="i", ["ï"]="i",
        ["ñ"]="n",
        ["ò"]="o", ["ó"]="o", ["ô"]="o", ["ö"]="o", ["õ"]="o",
        ["ù"]="u", ["ú"]="u", ["û"]="u", ["ü"]="u",
        ["ý"]="y", ["ÿ"]="y"
    }
    s = mw.ustring.gsub(s, ".", accents)

    -- 4) Remplacer les autres caractères non alphanumériques par un espace
    s = mw.ustring.gsub(s, "[^%w]", " ")

    -- 5) Compresser les espaces
    s = mw.ustring.gsub(s, "%s+", " ")

    -- 6) Trim
    s = mw.text.trim(s)

    return s
end


-- Exécution des parseurs MediaWiki
local function parse(wikitext)
    return mw.getCurrentFrame():preprocess(wikitext)
end

-- Function 1: Get full episode link from key
function p.getEpisode(frame)
    local key = normalizeAlias(frame.args[1])
    local entry = data[key]

    if not entry then
        return "Épisode non référencé"
    end

    local ns  = safe(entry[1])
    local link = safe(entry[2])
    local title = safe(entry[3])

    local wikitext
    if ns == "" then
        wikitext = string.format("[[%s|%s]]", link, title)
    else
        wikitext = string.format("[[%s:%s|%s]]", ns, link, title)
    end

    return parse(wikitext)
end

-- Function 2: Force namespace to "Credits:"
function p.getCreditsLink(frame)
    local key = normalizeAlias(frame.args[1])
    local entry = data[key]

    if not entry then
        return "Épisode non référencé"
    end

    local link = safe(entry[2])
    local title = safe(entry[3])

    return parse(string.format("[[Crédits:%s|%s]]", link, title))
end

-- Function 3: Force namespace to "Transcript:"
function p.getTranscriptLink(frame)
    local key = normalizeAlias(frame.args[1])
    local entry = data[key]

    if not entry then
        return "Épisode non référencé"
    end

    local link = safe(entry[2])
    local title = safe(entry[3])

    return parse(string.format("[[Retranscription:%s|%s]]", link, title))
end

-- Function 4: Force namespace to "Quotes:"
function p.getQuotesLink(frame)
    local key = normalizeAlias(frame.args[1])
    local entry = data[key]

    if not entry then
        return "Épisode non référencé"
    end

    local link = safe(entry[2])
    local title = safe(entry[3])

    return parse(string.format("[[Citations:%s|%s]]", link, title))
end

-- Function 5: Force namespace to "Category:Images from"
function p.getImagesLink(frame)
    local key = normalizeAlias(frame.args[1])
    local entry = data[key]

    if not entry then
        return "Épisode non référencé"
    end

    local link = safe(entry[2])
    local title = safe(entry[3])

    return parse(string.format("[[:Catégorie:Images de %s|%s]]", link, title))
end

-- Function 6: Retrieve from link
function p.getEpisodeLink(frame)
    local key = normalizeAlias(frame.args[1])
    local entry = data[key]

    if not entry then
        return "Épisode non référencé"
    end

    return safe(entry[2])
end

-- Function 7: Get full episode link only from key
function p.getEpisodeFullLink(frame)
    local key = normalizeAlias(frame.args[1])
    local entry = data[key]

    if not entry then
        return "Épisode non référencé"
    end

    local ns = safe(entry[1])
    local link = safe(entry[2])

    if ns == "" then
        return link
    else
        return ns .. ":" .. link
    end
end

-- Function 8: Get episode title only from key
function p.getEpisodeTitle(frame)
    local key = normalizeAlias(frame.args[1])
    local entry = data[key]

    if not entry then
        return "Episode not found"
    end

    return safe(entry[3])
end

-- Alias → pages
function p.aliasToPages()
    local map = {}

    for alias, entry in pairs(data) do
        local link = entry[2]

        if not map[alias] then
            map[alias] = { link }
        else
            local exists = false
            for _, v in ipairs(map[alias]) do
                if v == link then
                    exists = true
                    break
                end
            end
            if not exists then
                table.insert(map[alias], link)
            end
        end
    end

    return map
end


function p.debugAllAliases()
    local out = {}
    table.insert(out, "== Vérification complète des alias ==")

    for alias, entry in pairs(data) do
        local norm = normalizeAlias(alias)
        local ok = data[norm] and "✔" or "✘"

        table.insert(out,
            string.format(
                "* %s <code>%s</code> → <code>%s</code>%s",
                ok,
                alias,
                norm,
                ok == "✘" and " (introuvable)" or ""
            )
        )
    end

    return table.concat(out, "\n")
end

function p.testTitle(frame)
    local input = frame.args[1] or ""
    local norm = normalizeAlias(input)
    local exists = data[norm] ~= nil

    local out = {}
    table.insert(out, "== Test de normalisation ==")
    table.insert(out, "* Entrée : <code>" .. input .. "</code>")
    table.insert(out, "* Normalisé : <code>" .. norm .. "</code>")
    table.insert(out, "* Trouvé : " .. (exists and "✔ oui" or "✘ non"))

    return table.concat(out, "\n")
end

return p