Aller au contenu

Module:Episode

De Stargate Wiki Sémantique
Version datée du 1 juin 2026 à 20:48 par LIMAFOX76 (discussion | contributions)
(diff) ← Version précédente | Version actuelle (diff) | Version suivante → (diff)
Documentation icon Documentation module[créer]
-- Module:Episode
-- API publique pour accéder aux épisodes (toutes séries confondues)
-- Utilise Module:Episode/MultiSeries comme backend

local Index = require("Module:Episode/MultiSeries")

local p = {}

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

    -- Recherche par ID direct ou alias
    return Index.get(key) or Index.get_by_alias(key)
end

------------------------------------------------------------
-- Prétraitement du titre (parser)
------------------------------------------------------------
local function preprocessTitle(frame, ep)
    local raw = ep.title_fr or ep.page_title or ep.property or ep.id or ""
    return frame:preprocess(raw)
end

------------------------------------------------------------
-- Construction d’un lien d’épisode
------------------------------------------------------------
local function buildEpisodeLink(frame, ep, labelOverride)
    local title = labelOverride or preprocessTitle(frame, ep)

    if ep.namespace == "" or ep.namespace == nil then
        -- Films / téléfilms
        return string.format("[[%s|%s]]", ep.page_title, title)
    else
        -- Épisodes normaux
        return string.format("[[%s:%s|%s]]", ep.namespace, ep.page_title, title)
    end
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)
    return string.format("[[Crédits:%s|%s]]", ep.page_title, 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)
    return string.format("[[Retranscription:%s|%s]]", ep.page_title, 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)
    return string.format("[[Citations:%s|%s]]", ep.page_title, 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)
    return string.format("[[:Catégorie:Images de %s|%s]]", ep.page_title, 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
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

    if ep.namespace == "" or ep.namespace == nil then
        return ep.page_title
    else
        return string.format("%s:%s", ep.namespace, ep.page_title)
    end
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

    if not ep.episode or ep.episode == "" then
        return ""
    end

    return string.format("%02d", tonumber(ep.episode))
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

    if not ep.season or ep.season == "" then
        return ""
    end

    return tostring(ep.season)
end

------------------------------------------------------------
-- 11. Rapport lisible des doublons
------------------------------------------------------------
function p.debugDuplicates(frame)
    local d = Index.duplicates
    if not d then
        return "Aucun rapport de doublons disponible"
    end

    local out = {}

    table.insert(out, "== Doublons d’ID ==")
    if #d.duplicate_ids == 0 then
        table.insert(out, "Aucun")
    else
        for _, id in ipairs(d.duplicate_ids) do
            table.insert(out, "* " .. id)
        end
    end

    table.insert(out, "\n== Alias dupliqués ==")
    if #d.duplicate_aliases == 0 then
        table.insert(out, "Aucun")
    else
        for _, alias in ipairs(d.duplicate_aliases) do
            table.insert(out, "* " .. alias)
        end
    end

    table.insert(out, "\n== Alias contradictoires ==")
    if #d.conflicting_aliases == 0 then
        table.insert(out, "Aucun")
    else
        for _, c in ipairs(d.conflicting_aliases) do
            table.insert(out, "* " .. c.alias .. " → " .. c.id1 .. " / " .. c.id2)
        end
    end

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

------------------------------------------------------------
-- 12. Génération d’une liste d’épisodes
-- Paramètres :
--   serie  = SG1 / SGA / SGU / Films
--   saison = numéro de saison (optionnel)
--   liste  = liste d’IDs séparés par des virgules (optionnel)
------------------------------------------------------------
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 (liste = "SG1-S01E01, SG1-S01E02")
    ------------------------------------------------------------
    if liste and liste ~= "" then
        for id in mw.text.gsplit(liste, ",") do
            id = mw.text.trim(id)
            local ep = Index.get(id) or Index.get_by_alias(id)
            if ep then
                table.insert(out, "* " .. ep.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 id, ep in pairs(Index.episodes) do
            if ep.series == serie and ep.season == saison then
                table.insert(out, { sort = ep.episode, text = "* " .. ep.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 id, ep in pairs(Index.episodes) do
            if ep.series == serie then
                table.insert(out, { sort = ep.season * 100 + ep.episode, text = "* " .. ep.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