Aller au contenu

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

De Stargate Wiki Sémantique
Contenu supprimé Contenu ajouté
Aucun résumé des modifications
m LIMAFOX76 a déplacé la page Module:Episode REAL vers Module:Episode sans laisser de redirection
 
(20 versions intermédiaires par le même utilisateur non affichées)
Ligne 1 : Ligne 1 :
------------------------------------------------------------
mw.logObject(require("Module:Episode/MultiSeries"), "MULTI")
-- SCAN COMPLET DE package.loaded
------------------------------------------------------------

local out = {"=== SCAN package.loaded ==="}

for k,v in pairs(package.loaded) do
if tostring(k):match("Episode") then
table.insert(out, tostring(k) .. " = " .. tostring(v))
end
end

mw.log(table.concat(out, "\n"))

------------------------------------------------------------
-- DIAGNOSTIC SAFE — n'affecte pas le module
------------------------------------------------------------

local function dump_table(t)
if type(t) ~= "table" then
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

local name = "Module:Episode/MultiSeries"

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

mw.log("=== DIAGNOSTIC SAFE ===")
mw.log("Nom require() = >" .. name .. "<")
mw.log("--- AVANT require() ---")
mw.log(tostring(before))
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]))


------------------------------------------------------------
------------------------------------------------------------
-- Module:Episode
-- Module:Episode (ULTRA OPTIMISÉ AVEC CACHE GLOBAL)
-- API publique pour accéder aux épisodes Stargate
-- API publique pour accéder aux épisodes (toutes séries)
-- Utilise Module:Episode/MultiSeries (indexation complète)
-- Backend : Module:Episode/MultiSeries
------------------------------------------------------------
------------------------------------------------------------


local Index = require("Module:Episode/MultiSeries")
local normalize = Index.normalize
local p = {}
local p = {}
local Multi = require("Module:Episode/MultiSeries")


------------------------------------------------------------
------------------------------------------------------------
-- CACHE GLOBAL (résultats mémorisés par page)
-- Normalisation locale (même logique que MultiSeries)
------------------------------------------------------------
------------------------------------------------------------
local function normalize(str)
local CACHE = {
entry = {}, -- ID/alias → ep
if not str then return "" end
title = {}, -- id → titre prétraité
link = {}, -- id → lien [[...]]
full = {}, -- id → namespace:page
number = {}, -- id → numéro d’épisode
season = {}, -- id → numéro de saison
}


------------------------------------------------------------
str = mw.ustring.lower(str)
-- Récupère une entrée d’épisode depuis un argument
str = mw.ustring.toNFD(str)
------------------------------------------------------------
str = mw.ustring.gsub(str, "%pM", "")
local function getEntry(frame)
str = str:gsub("'%s*", "")
str = str:gsub("[{}]", " ")
local key = frame.args[1]
str = str:gsub("[%p]", " ")
if not key or key == "" then
return nil
str = str:gsub("[^%w%s]", " ")
end
str = str:gsub("%s+", " ")
str = mw.text.trim(str)


return str
-- Cache direct
if CACHE.entry[key] ~= nil then
return CACHE.entry[key]
end

-- Normalisation identique à MultiSeries
local norm = normalize(key)

-- 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)
-- Récupération d’un épisode par ID
------------------------------------------------------------
------------------------------------------------------------
function p.getEpisodeById(id)
local function preprocessTitle(frame, ep)
if not id then return nil end
local id = ep.id

return Multi.by_id[id]
if CACHE.title[id] then
return CACHE.title[id]
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


------------------------------------------------------------
------------------------------------------------------------
-- Récupération d’un épisode par alias
-- Construction d’un lien d’épisode (sécurisé)
------------------------------------------------------------
------------------------------------------------------------
local function buildEpisodeLink(frame, ep, labelOverride)
function p.getEpisodeByAlias(alias)
local id = ep.id
if not alias then return nil end
alias = normalize(alias)
local list = Multi.by_alias[alias]
if not list then return nil end


-- Cache si pas de label personnalisé
-- Si un seul épisode correspond → renvoyer directement
if #list == 1 then
if not labelOverride and CACHE.link[id] then
return list[1]
return CACHE.link[id]
end
end


local title = labelOverride or preprocessTitle(frame, ep)
-- Sinon renvoyer la liste complète (ambiguïté)
local page = ep.page_title_fr or ep.id
return list

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

if not labelOverride then
CACHE.link[id] = link
end

return link
end
end


------------------------------------------------------------
------------------------------------------------------------
-- 1. Lien normal vers l’épisode
-- Récupération d’un épisode par titre de page
------------------------------------------------------------
------------------------------------------------------------
function p.getEpisodeByPage(page)
function p.getEpisode(frame)
local ep = getEntry(frame)
if not page then return nil end
if not ep then return "Épisode ou film non référencé" end
return Multi.by_page[normalize(page)]
return buildEpisodeLink(frame, ep)
end
end


------------------------------------------------------------
------------------------------------------------------------
-- 2. Lien vers Crédits:
-- Récupération d’un épisode par série + saison + numéro
------------------------------------------------------------
------------------------------------------------------------
function p.getEpisode(series, season, episode)
function p.getCreditsLink(frame)
local ep = getEntry(frame)
if not (series and season and episode) then return nil end
if not ep then return "Épisode ou film non référencé" end


local key = series .. "-" .. season
local title = preprocessTitle(frame, ep)
local list = Multi.by_season[key]
local page = ep.page_title_fr or ep.id
if not list then return nil end


return string.format("[[Crédits:%s|%s]]", page, title)
for _, ep in ipairs(list) do
end
if ep.episode == tonumber(episode) then
return ep
end
end


------------------------------------------------------------
return nil
-- 3. Lien vers Retranscription:
------------------------------------------------------------
function p.getTranscriptLink(frame)
local ep = getEntry(frame)
if not ep then return "Épisode ou film non référencé" end

local title = preprocessTitle(frame, ep)
local page = ep.page_title_fr or ep.id

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


------------------------------------------------------------
------------------------------------------------------------
-- Génération d’un lien vers l’épisode
-- 4. Lien vers Citations:
------------------------------------------------------------
------------------------------------------------------------
function p.getEpisodeLink(ep)
function p.getQuotesLink(frame)
if not ep then return "" end
local ep = getEntry(frame)
if not ep then return "Épisode ou film non référencé" end
return string.format("[[%s:%s]]", ep.namespace_fr or "Épisode", ep.page_title_fr)

local title = preprocessTitle(frame, ep)
local page = ep.page_title_fr or ep.id

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


------------------------------------------------------------
------------------------------------------------------------
-- 5. Lien vers Catégorie:Images de ...
-- Génération d’un lien complet (FR + EN)
------------------------------------------------------------
------------------------------------------------------------
function p.getEpisodeFullLink(ep)
function p.getImagesLink(frame)
if not ep then return "" end
local ep = getEntry(frame)
if not ep then return "Épisode ou film non référencé" end


local fr = string.format("[[%s:%s|%s]]",
local title = preprocessTitle(frame, ep)
ep.namespace_fr or "Épisode",
local page = ep.page_title_fr or ep.id
ep.page_title_fr,
ep.title_fr or ep.page_title_fr
)


local en = string.format("[[%s:%s|%s]]",
return string.format("[[:Catégorie:Images de %s|%s]]", page, title)
end
ep.namespace_en or "Episode",
ep.page_title_en,
ep.title_en or ep.page_title_en
)


------------------------------------------------------------
return fr .. " / " .. en
-- 6. Récupère uniquement le titre de page (sans namespace)
------------------------------------------------------------
function p.getEpisodeLink(frame)
local ep = getEntry(frame)
if not ep then return "Épisode ou film non référencé" end
return ep.page_title_fr or ep.id
end
end


------------------------------------------------------------
------------------------------------------------------------
-- 7. Récupère le lien complet (namespace + titre)
-- Raccourcis API pour #invoke
------------------------------------------------------------
------------------------------------------------------------
function p.id(frame)
function p.getEpisodeFullLink(frame)
local id = frame.args[1] or frame.args.id
local ep = getEntry(frame)
if not ep then return "Épisode ou film non référencé" end
local ep = p.getEpisodeById(id)

if not ep then return "" end
local id = ep.id
return p.getEpisodeLink(ep)
if CACHE.full[id] then
return CACHE.full[id]
end

local page = ep.page_title_fr or ep.id
local full

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

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


------------------------------------------------------------
function p.alias(frame)
-- 8. Récupère uniquement le titre français (prétraité)
local alias = frame.args[1] or frame.args.alias
------------------------------------------------------------
local ep = p.getEpisodeByAlias(alias)
function p.getEpisodeTitle(frame)
if not ep then return "" end
local ep = getEntry(frame)
if not ep then return "Épisode ou film non référencé" end
return preprocessTitle(frame, ep)
end


------------------------------------------------------------
if type(ep) == "table" and ep.id then
-- 9. Numéro d’épisode (2 chiffres)
return p.getEpisodeLink(ep)
------------------------------------------------------------
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


local n = tonumber(ep.episode)
-- Ambiguïté : renvoyer la liste
local out = {}
local out = n and string.format("%02d", n) or ""

for _, e in ipairs(ep) do
CACHE.number[id] = out
table.insert(out, p.getEpisodeLink(e))
return out
end

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

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

return table.concat(out, " • ")
local out = ep.season and tostring(ep.season) or ""
CACHE.season[id] = out
return out
end
end


------------------------------------------------------------
function p.page(frame)
-- 12. Génération d’une liste d’épisodes (films-safe)
local page = frame.args[1] or frame.args.page
------------------------------------------------------------
local ep = p.getEpisodeByPage(page)
function p.renderList(frame)
if not ep then return "" end
local serie = frame.args["serie"]
return p.getEpisodeLink(ep)
local saison = tonumber(frame.args["saison"])
local liste = frame.args["liste"]

local out = {}

------------------------------------------------------------
-- Mode 1 : liste personnalisée
------------------------------------------------------------
if liste and liste ~= "" then
for id in mw.text.gsplit(liste, ",") do
id = mw.text.trim(id)

local ep =
Index.by_id[id]
or (Index.by_alias[id] and Index.by_alias[id][1])

if ep then
table.insert(out, "* " .. frame:preprocess(ep.title_fr or ep.page_title_fr))
else
table.insert(out, "* (inconnu) " .. id)
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

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

------------------------------------------------------------
-- 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."
end
end



Dernière version du 3 juin 2026 à 14:15

Documentation icon Documentation module[créer]
------------------------------------------------------------
-- SCAN COMPLET DE package.loaded
------------------------------------------------------------

local out = {"=== SCAN package.loaded ==="}

for k,v in pairs(package.loaded) do
    if tostring(k):match("Episode") then
        table.insert(out, tostring(k) .. " = " .. tostring(v))
    end
end

mw.log(table.concat(out, "\n"))

------------------------------------------------------------
-- DIAGNOSTIC SAFE — n'affecte pas le module
------------------------------------------------------------

local function dump_table(t)
    if type(t) ~= "table" then
        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

local name = "Module:Episode/MultiSeries"

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

mw.log("=== DIAGNOSTIC SAFE ===")
mw.log("Nom require() = >" .. name .. "<")
mw.log("--- AVANT require() ---")
mw.log(tostring(before))
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]))

------------------------------------------------------------
-- Module:Episode (ULTRA OPTIMISÉ AVEC CACHE GLOBAL)
-- API publique pour accéder aux épisodes (toutes séries)
-- Backend : Module:Episode/MultiSeries
------------------------------------------------------------

local Index = require("Module:Episode/MultiSeries")
local normalize = Index.normalize
local p = {}

------------------------------------------------------------
-- CACHE GLOBAL (résultats mémorisés par page)
------------------------------------------------------------
local CACHE = {
    entry  = {},   -- ID/alias → ep
    title  = {},   -- id → titre prétraité
    link   = {},   -- id → lien [[...]]
    full   = {},   -- id → namespace:page
    number = {},   -- id → numéro d’épisode
    season = {},   -- id → numéro de saison
}

------------------------------------------------------------
-- 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
    if CACHE.entry[key] ~= nil then
        return CACHE.entry[key]
    end

    -- Normalisation identique à MultiSeries
    local norm = normalize(key)

    -- 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

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

    if CACHE.title[id] then
        return CACHE.title[id]
    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

------------------------------------------------------------
-- Construction d’un lien d’épisode (sécurisé)
------------------------------------------------------------
local function buildEpisodeLink(frame, ep, labelOverride)
    local id = ep.id

    -- Cache si pas de label personnalisé
    if not labelOverride and CACHE.link[id] then
        return CACHE.link[id]
    end

    local title = labelOverride or preprocessTitle(frame, ep)
    local page  = ep.page_title_fr or ep.id

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

    if not labelOverride then
        CACHE.link[id] = link
    end

    return link
end

------------------------------------------------------------
-- 1. Lien normal vers l’épisode
------------------------------------------------------------
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

------------------------------------------------------------
-- 2. Lien vers Crédits:
------------------------------------------------------------
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 page  = ep.page_title_fr or ep.id

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

------------------------------------------------------------
-- 3. Lien vers Retranscription:
------------------------------------------------------------
function p.getTranscriptLink(frame)
    local ep = getEntry(frame)
    if not ep then return "Épisode ou film non référencé" end

    local title = preprocessTitle(frame, ep)
    local page  = ep.page_title_fr or ep.id

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

------------------------------------------------------------
-- 4. Lien vers Citations:
------------------------------------------------------------
function p.getQuotesLink(frame)
    local ep = getEntry(frame)
    if not ep then return "Épisode ou film non référencé" end

    local title = preprocessTitle(frame, ep)
    local page  = ep.page_title_fr or ep.id

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

------------------------------------------------------------
-- 5. Lien vers Catégorie:Images de ...
------------------------------------------------------------
function p.getImagesLink(frame)
    local ep = getEntry(frame)
    if not ep then return "Épisode ou film non référencé" end

    local title = preprocessTitle(frame, ep)
    local page  = ep.page_title_fr or ep.id

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

------------------------------------------------------------
-- 6. Récupère uniquement le titre de page (sans namespace)
------------------------------------------------------------
function p.getEpisodeLink(frame)
    local ep = getEntry(frame)
    if not ep then return "Épisode ou film non référencé" end
    return ep.page_title_fr or ep.id
end

------------------------------------------------------------
-- 7. Récupère le lien complet (namespace + titre)
------------------------------------------------------------
function p.getEpisodeFullLink(frame)
    local ep = getEntry(frame)
    if not ep then return "Épisode ou film non référencé" end

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

    local page = ep.page_title_fr or ep.id
    local full

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

    CACHE.full[id] = full
    return full
end

------------------------------------------------------------
-- 8. Récupère uniquement le titre français (prétraité)
------------------------------------------------------------
function p.getEpisodeTitle(frame)
    local ep = getEntry(frame)
    if not ep then return "Épisode ou film non référencé" end
    return preprocessTitle(frame, ep)
end

------------------------------------------------------------
-- 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

    local n = tonumber(ep.episode)
    local out = n and string.format("%02d", n) or ""

    CACHE.number[id] = out
    return out
end

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

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

    local out = ep.season and tostring(ep.season) or ""
    CACHE.season[id] = out
    return out
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"]

    local out = {}

    ------------------------------------------------------------
    -- Mode 1 : liste personnalisée
    ------------------------------------------------------------
    if liste and liste ~= "" then
        for id in mw.text.gsplit(liste, ",") do
            id = mw.text.trim(id)

            local ep =
                Index.by_id[id]
                or (Index.by_alias[id] and Index.by_alias[id][1])

            if ep then
                table.insert(out, "* " .. frame:preprocess(ep.title_fr or ep.page_title_fr))
            else
                table.insert(out, "* (inconnu) " .. id)
            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

        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

    ------------------------------------------------------------
    -- 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."
end

return p