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 4 : Ligne 4 :
-- Backend : Module:Episode/MultiSeries
-- Backend : Module:Episode/MultiSeries
------------------------------------------------------------
------------------------------------------------------------
mw.logObject(Index, "INDEX")


local Index = require("Module:Episode/MultiSeries")
local Index = require("Module:Episode/MultiSeries")
Ligne 12 : Ligne 11 :
-- Récupère une entrée d’épisode depuis un argument
-- Récupère une entrée d’épisode depuis un argument
------------------------------------------------------------
------------------------------------------------------------

local function getEntry(frame)
local function getEntry(frame)
local key = frame.args[1]
local key = frame.args[1]
Ligne 19 : Ligne 17 :
end
end


-- Normalisation de la clé pour les alias
-- Normalisation simple pour les alias
local norm = mw.ustring.lower(key)
local norm = mw.ustring.lower(key)
norm = norm:gsub("%s+", " ")
norm = norm:gsub("%s+", " ")
Ligne 30 : Ligne 28 :
-- 2) Recherche par alias normalisé
-- 2) Recherche par alias normalisé
if Index.by_alias[norm] then
if Index.by_alias[norm] then
-- by_alias[norm] est une LISTE d'épisodes
return Index.by_alias[norm][1] -- premier match
-- mais dans 99 % des cas il n’y en a qu’un
return Index.by_alias[norm][1]
end
end


return nil
return nil
end
end



------------------------------------------------------------
------------------------------------------------------------
Ligne 45 : Ligne 40 :
local raw =
local raw =
ep.title_fr
ep.title_fr
or ep.page_title
or ep.page_title_fr
or ep.page_title_en
or ep.property
or ep.property
or ep.id
or ep.id
Ligne 54 : Ligne 50 :


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

-- Page cible (fallbacks sûrs)
local page =
ep.page_title_fr
or ep.page_title_en
or ep.property
or ep.id


if not ep.namespace or ep.namespace == "" then
if not ep.namespace or ep.namespace == "" then
-- Films / téléfilms
-- Films / téléfilms
return string.format("[[%s|%s]]", ep.page_title_fr, title_fr)
return string.format("[[%s|%s]]", page, title_fr)
else
else
-- Épisodes normaux
-- Épisodes normaux
return string.format("[[%s:%s|%s]]", ep.namespace, ep.page_title_fr, title_fr)
return string.format("[[%s:%s|%s]]", ep.namespace, page, title_fr)
end
end
end
end
Ligne 88 : Ligne 91 :
end
end
local title = preprocessTitle(frame, ep)
local title = preprocessTitle(frame, ep)
return string.format("[[Crédits:%s|%s]]", ep.page_title_fr, title_fr)
return string.format("[[Crédits:%s|%s]]", ep.page_title_fr or ep.id, title_fr)
end
end


Ligne 100 : Ligne 103 :
end
end
local title = preprocessTitle(frame, ep)
local title = preprocessTitle(frame, ep)
return string.format("[[Retranscription:%s|%s]]", ep.page_title_fr, title_fr)
return string.format("[[Retranscription:%s|%s]]", ep.page_title_fr or ep.id, title_fr)
end
end


Ligne 112 : Ligne 115 :
end
end
local title = preprocessTitle(frame, ep)
local title = preprocessTitle(frame, ep)
return string.format("[[Citations:%s|%s]]", ep.page_title_fr, title_fr)
return string.format("[[Citations:%s|%s]]", ep.page_title_fr or ep.id, title_fr)
end
end


Ligne 124 : Ligne 127 :
end
end
local title = preprocessTitle(frame, ep)
local title = preprocessTitle(frame, ep)
return string.format("[[:Catégorie:Images de %s|%s]]", ep.page_title_fr, title_fr)
return string.format("[[:Catégorie:Images de %s|%s]]", ep.page_title_fr or ep.id, title_fr)
end
end


Ligne 135 : Ligne 138 :
return "Épisode ou film non référencé"
return "Épisode ou film non référencé"
end
end
return ep.page_title_fr
return ep.page_title_fr or ep.page_title_en or ep.id
end
end


Ligne 146 : Ligne 149 :
return "Épisode ou film non référencé"
return "Épisode ou film non référencé"
end
end

local page =
ep.page_title_fr
or ep.page_title_en
or ep.property
or ep.id


if not ep.namespace or ep.namespace == "" then
if not ep.namespace or ep.namespace == "" then
return ep.page_title_fr
return page
else
else
return string.format("%s:%s", ep.namespace, ep.page_title_fr)
return string.format("%s:%s", ep.namespace, page)
end
end
end
end
Ligne 255 : Ligne 264 :
id = mw.text.trim(id)
id = mw.text.trim(id)
local ep =
local ep =
Index.by_id[id]
Index.by_id[id]
or (Index.by_alias[id] and Index.by_alias[id][1])
or (Index.by_alias[id] and Index.by_alias[id][1])


if ep then
if ep then

Version du 2 juin 2026 à 20:49

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

    -- Normalisation simple pour les alias
    local norm = mw.ustring.lower(key)
    norm = norm:gsub("%s+", " ")

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

    -- 2) Recherche par alias normalisé
    if Index.by_alias[norm] then
        return Index.by_alias[norm][1]   -- premier match
    end

    return nil
end

------------------------------------------------------------
-- Prétraitement du titre (parser)
------------------------------------------------------------
local function preprocessTitle(frame, ep)
    local raw =
        ep.title_fr
        or ep.page_title_fr
        or ep.page_title_en
        or ep.property
        or ep.id
        or ""

    return frame:preprocess(raw)
end

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

    -- Page cible (fallbacks sûrs)
    local page =
        ep.page_title_fr
        or ep.page_title_en
        or ep.property
        or ep.id

    if not ep.namespace or ep.namespace == "" then
        -- Films / téléfilms
        return string.format("[[%s|%s]]", page, title_fr)
    else
        -- Épisodes normaux
        return string.format("[[%s:%s|%s]]", ep.namespace, page, title_fr)
    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_fr or ep.id, title_fr)
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_fr or ep.id, title_fr)
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_fr or ep.id, title_fr)
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_fr or ep.id, title_fr)
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.page_title_en 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 page =
        ep.page_title_fr
        or ep.page_title_en
        or ep.property
        or ep.id

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