Aller au contenu

Module:Episode

De Stargate Wiki Sémantique
Documentation icon Documentation module[créer]
------------------------------------------------------------
-- 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