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
Ligne 1 : Ligne 1 :
mw.logObject(require("Module:Episode/MultiSeries"), "MULTI")

------------------------------------------------------------
------------------------------------------------------------
-- Module:Episode
-- Module:Episode
-- 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 p = {}
local p = {}
local Multi = require("Module:Episode/MultiSeries")


------------------------------------------------------------
------------------------------------------------------------
-- Récupère une entrée d’épisode depuis un argument
-- Normalisation locale (même logique que MultiSeries)
------------------------------------------------------------
------------------------------------------------------------
local function normalize(str)
local function getEntry(frame)
local key = frame.args[1]
if not str then return "" end
if not key or key == "" then
return nil
end


-- Recherche par ID direct ou alias
str = mw.ustring.lower(str)
return Index.get(key) or Index.get_by_alias(key)
str = mw.ustring.toNFD(str)
end
str = mw.ustring.gsub(str, "%pM", "")

str = str:gsub("'%s*", "")
------------------------------------------------------------
str = str:gsub("[{}]", " ")
-- Prétraitement du titre (parser)
str = str:gsub("[%p]", " ")
------------------------------------------------------------
str = str:gsub("[^%w%s]", " ")
local function preprocessTitle(frame, ep)
str = str:gsub("%s+", " ")
local raw =
str = mw.text.trim(str)
ep.title_fr
or ep.page_title
or ep.property
or ep.id
or ""


return str
return frame:preprocess(raw)
end
end


------------------------------------------------------------
------------------------------------------------------------
-- Récupération d’un épisode par ID
-- Construction d’un lien d’épisode
------------------------------------------------------------
------------------------------------------------------------
local function buildEpisodeLink(frame, ep, labelOverride)
function p.getEpisodeById(id)
local title = labelOverride or preprocessTitle(frame, ep)
if not id then return nil end

return Multi.by_id[id]
if not ep.namespace or ep.namespace == "" 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
end


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


------------------------------------------------------------
-- Si un seul épisode correspond → renvoyer directement
-- 2. Lien vers Crédits:
if #list == 1 then
------------------------------------------------------------
return list[1]
function p.getCreditsLink(frame)
local ep = getEntry(frame)
if not ep then
return "Épisode ou film non référencé"
end
end
local title = preprocessTitle(frame, ep)
return string.format("[[Crédits:%s|%s]]", ep.page_title, title)
end


------------------------------------------------------------
-- Sinon renvoyer la liste complète (ambiguïté)
-- 3. Lien vers Retranscription:
return list
------------------------------------------------------------
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
end


------------------------------------------------------------
------------------------------------------------------------
-- 4. Lien vers Citations:
-- Récupération d’un épisode par titre de page
------------------------------------------------------------
------------------------------------------------------------
function p.getEpisodeByPage(page)
function p.getQuotesLink(frame)
local ep = getEntry(frame)
if not page then return nil end
if not ep then
return Multi.by_page[normalize(page)]
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
end


------------------------------------------------------------
------------------------------------------------------------
-- 5. Lien vers Catégorie:Images de ...
-- Récupération d’un épisode par série + saison + numéro
------------------------------------------------------------
------------------------------------------------------------
function p.getEpisode(series, season, episode)
function p.getImagesLink(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 title = preprocessTitle(frame, ep)
return string.format("[[:Catégorie:Images de %s|%s]]", ep.page_title, title)
end


------------------------------------------------------------
local key = series .. "-" .. season
-- 6. Récupère uniquement le titre de page (sans namespace)
local list = Multi.by_season[key]
------------------------------------------------------------
if not list then return nil end
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


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


if not ep.namespace or ep.namespace == "" then
return nil
return ep.page_title
else
return string.format("%s:%s", ep.namespace, ep.page_title)
end
end
end


------------------------------------------------------------
------------------------------------------------------------
-- 8. Récupère uniquement le titre français (prétraité)
-- Génération d’un lien vers l’épisode
------------------------------------------------------------
------------------------------------------------------------
function p.getEpisodeLink(ep)
function p.getEpisodeTitle(frame)
if not ep then return "" end
local ep = getEntry(frame)
if not ep then
return string.format("[[%s:%s]]", ep.namespace_fr or "Épisode", ep.page_title_fr)
return "Épisode ou film non référencé"
end
return preprocessTitle(frame, ep)
end
end


------------------------------------------------------------
------------------------------------------------------------
-- 9. Numéro d’épisode (2 chiffres)
-- Génération d’un lien complet (FR + EN)
------------------------------------------------------------
------------------------------------------------------------
function p.getEpisodeFullLink(ep)
function p.getEpisodeNumber(frame)
if not ep then return "" end
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
local fr = string.format("[[%s:%s|%s]]",
ep.namespace_fr or "Épisode",
return ""
end
ep.page_title_fr,
ep.title_fr or ep.page_title_fr
)


local en = string.format("[[%s:%s|%s]]",
return string.format("%02d", tonumber(ep.episode))
ep.namespace_en or "Episode",
ep.page_title_en,
ep.title_en or ep.page_title_en
)

return fr .. " / " .. en
end
end


------------------------------------------------------------
------------------------------------------------------------
-- 10. Numéro de saison
-- Raccourcis API pour #invoke
------------------------------------------------------------
------------------------------------------------------------
function p.id(frame)
function p.getSeasonNumber(frame)
local id = frame.args[1] or frame.args.id
local ep = getEntry(frame)
if not ep then
local ep = p.getEpisodeById(id)
if not ep then return "" end
return "Épisode ou film non référencé"
end
return p.getEpisodeLink(ep)
end


if not ep.season or ep.season == "" then
function p.alias(frame)
return ""
local alias = frame.args[1] or frame.args.alias
end
local ep = p.getEpisodeByAlias(alias)
if not ep then return "" end


return tostring(ep.season)
if type(ep) == "table" and ep.id then
end
return p.getEpisodeLink(ep)

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


-- Ambiguïté : renvoyer la liste
local out = {}
local out = {}

for _, e in ipairs(ep) do
table.insert(out, p.getEpisodeLink(e))
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
end

return table.concat(out, " • ")
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
end


------------------------------------------------------------
function p.page(frame)
-- 12. Génération d’une liste d’épisodes
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.get(id) or Index.get_by_alias(id)
if ep then
table.insert(out, "* " .. frame:preprocess(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 _, ep in pairs(Index.episodes) do
if ep.series == serie and ep.season == saison then
table.insert(out, {
sort = ep.episode,
text = "* " .. frame:preprocess(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 _, ep in pairs(Index.episodes) do
if ep.series == serie then
table.insert(out, {
sort = ep.season * 100 + ep.episode,
text = "* " .. frame:preprocess(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
end



Version du 2 juin 2026 à 20:22

Documentation icon Documentation module[créer]
------------------------------------------------------------
-- Module:Episode
-- API publique pour accéder aux épisodes (toutes séries)
-- Backend : Module:Episode/MultiSeries
------------------------------------------------------------

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 not ep.namespace or ep.namespace == "" 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 not ep.namespace or ep.namespace == "" 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
------------------------------------------------------------
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.get(id) or Index.get_by_alias(id)
            if ep then
                table.insert(out, "* " .. frame:preprocess(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 _, ep in pairs(Index.episodes) do
            if ep.series == serie and ep.season == saison then
                table.insert(out, {
                    sort = ep.episode,
                    text = "* " .. frame:preprocess(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 _, ep in pairs(Index.episodes) do
            if ep.series == serie then
                table.insert(out, {
                    sort = ep.season * 100 + ep.episode,
                    text = "* " .. frame:preprocess(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