Aller au contenu

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

De Stargate Wiki Sémantique
Contenu supprimé Contenu ajouté
m LIMAFOX76 a déplacé la page Module:Episode REAL vers Module:Episode sans laisser de redirection
Aucun résumé des modifications
Ligne 1 : Ligne 1 :
local data = require("Module:Episode/data")
------------------------------------------------------------
-- 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 = {}
local p = {}


-- Function 1: Get full episode link from key
------------------------------------------------------------
function p.getEpisode(frame)
-- CACHE GLOBAL (résultats mémorisés par page)
local key = string.lower(frame.args[1] or "")
------------------------------------------------------------
local CACHE = {
local entry = data[key]
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
}


if not entry then
------------------------------------------------------------
return "Épisode non référencé"
-- 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
end


local ns, link, _, title = unpack(entry)
-- Cache direct
if CACHE.entry[key] ~= nil then
return CACHE.entry[key]
end


-- If namespace is empty or nil, omit it
-- Normalisation identique à MultiSeries
if ns == "" or ns == nil then
local norm = normalize(key)
return string.format("[[%s|%s]]", link, title)

-- 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
else
return string.format("[[%s:%s|%s]]", ns, link, title)
-- Épisodes normaux
link = string.format("[[%s:%s|%s]]", ep.namespace_fr, page, title)
end
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
end


-- Function 2: Force namespace to "Credits:"
------------------------------------------------------------
-- 2. Lien vers Crédits:
------------------------------------------------------------
function p.getCreditsLink(frame)
function p.getCreditsLink(frame)
local ep = getEntry(frame)
local key = string.lower(frame.args[1] or "")
local entry = data[key]
if not ep then return "Épisode ou film non référencé" end


if not entry then
local title = preprocessTitle(frame, ep)
return "Épisode non référencé"
local page = ep.page_title_fr or ep.id
end


local _, link, _,title = unpack(entry) -- We ignore original namespace
return string.format("[[Crédits:%s|%s]]", page, title)
return string.format("[[Crédits:%s|%s]]", link, title)
end
end


-- Function 3: Force namespace to "Transcript:"
------------------------------------------------------------
-- 3. Lien vers Retranscription:
------------------------------------------------------------
function p.getTranscriptLink(frame)
function p.getTranscriptLink(frame)
local ep = getEntry(frame)
local key = string.lower(frame.args[1] or "")
local entry = data[key]
if not ep then return "Épisode ou film non référencé" end


if not entry then
local title = preprocessTitle(frame, ep)
return "Épisode non référencé"
local page = ep.page_title_fr or ep.id
end


local _, link, _, title = unpack(entry) -- We ignore original namespace
return string.format("[[Retranscription:%s|%s]]", page, title)
return string.format("[[Retranscription:%s|%s]]", link, title)
end
end
-- Function 4: Force namespace to "Quotes:"

------------------------------------------------------------
-- 4. Lien vers Citations:
------------------------------------------------------------
function p.getQuotesLink(frame)
function p.getQuotesLink(frame)
local ep = getEntry(frame)
local key = string.lower(frame.args[1] or "")
local entry = data[key]
if not ep then return "Épisode ou film non référencé" end


if not entry then
local title = preprocessTitle(frame, ep)
return "Épisode non référencé"
local page = ep.page_title_fr or ep.id
end


local _, link, title = unpack(entry) -- We ignore original namespace
return string.format("[[Citations:%s|%s]]", page, title)
return string.format("[[Citations:%s|%s]]", link, title)
end
end


-- Function 5: Force namespace to "Category:Images from"
------------------------------------------------------------
-- 5. Lien vers Catégorie:Images de ...
------------------------------------------------------------
function p.getImagesLink(frame)
function p.getImagesLink(frame)
local ep = getEntry(frame)
local key = string.lower(frame.args[1] or "")
local entry = data[key]
if not ep then return "Épisode ou film non référencé" end


if not entry then
local title = preprocessTitle(frame, ep)
return "Épisode non référencé"
local page = ep.page_title_fr or ep.id
end


local _, link, _, title = unpack(entry) -- We ignore original namespace
return string.format("[[:Catégorie:Images de %s|%s]]", page, title)
return string.format("[[:Catégorie:Images de %s|%s]]", link, title)
end
end


-- Function 6: Retrieve from link
------------------------------------------------------------
-- 6. Récupère uniquement le titre de page (sans namespace)
------------------------------------------------------------
function p.getEpisodeLink(frame)
function p.getEpisodeLink(frame)
local ep = getEntry(frame)
local key = string.lower(frame.args[1] or "")
local entry = data[key]
if not ep then return "Épisode ou film non référencé" end

return ep.page_title_fr or ep.id
if not entry then
return "Épisode non référencé"
end

local _, link, _, _ = unpack(entry) -- We ignore original namespace
return string.format("%s", link)
end
end


-- Function 7: Get full episode link only from key
------------------------------------------------------------
-- 7. Récupère le lien complet (namespace + titre)
------------------------------------------------------------
function p.getEpisodeFullLink(frame)
function p.getEpisodeFullLink(frame)
local ep = getEntry(frame)
local key = string.lower(frame.args[1] or "")
local entry = data[key]
if not ep then return "Épisode ou film non référencé" end


local id = ep.id
if not entry then
return "Épisode non référencé"
if CACHE.full[id] then
return CACHE.full[id]
end
end


local page = ep.page_title_fr or ep.id
local ns, link, _, _ = unpack(entry)
local full


-- If namespace is empty or nil, omit it
if not ep.namespace_fr or ep.namespace_fr == "" then
full = page
if ns == "" or ns == nil then
return string.format("%s", link)
else
else
full = string.format("%s:%s", ep.namespace_fr, page)
return string.format("%s:%s", ns, link)
end
end

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


-- Function 8: Get episode title only from key
------------------------------------------------------------
-- 8. Récupère uniquement le titre français (prétraité)
------------------------------------------------------------
function p.getEpisodeTitle(frame)
function p.getEpisodeTitle(frame)
local ep = getEntry(frame)
local key = string.lower(frame.args[1] or "")
local entry = data[key]
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


if not entry then
return table.concat(lines, "\n")
return "Episode not found"
end
end


local _, _, _, title = unpack(entry)
return "Aucun paramètre valide fourni."
return string.format("%s", title)
end
end



Version du 4 juin 2026 à 20:43

Documentation icon Documentation module[créer]
local data = require("Module:Episode/data")
local p = {}

-- Function 1: Get full episode link from key
function p.getEpisode(frame)
    local key = string.lower(frame.args[1] or "")
    local entry = data[key]

    if not entry then
        return "Épisode non référencé"
    end

    local ns, link, _, title = unpack(entry)

    -- If namespace is empty or nil, omit it
    if ns == "" or ns == nil then
        return string.format("[[%s|%s]]", link, title)
    else
        return string.format("[[%s:%s|%s]]", ns, link, title)
    end
end

-- Function 2: Force namespace to "Credits:"
function p.getCreditsLink(frame)
    local key = string.lower(frame.args[1] or "")
    local entry = data[key]

    if not entry then
        return "Épisode non référencé"
    end

    local _, link, _,title = unpack(entry)  -- We ignore original namespace
    return string.format("[[Crédits:%s|%s]]", link, title)
end

-- Function 3: Force namespace to "Transcript:"
function p.getTranscriptLink(frame)
    local key = string.lower(frame.args[1] or "")
    local entry = data[key]

    if not entry then
        return "Épisode non référencé"
    end

    local _, link, _, title = unpack(entry)  -- We ignore original namespace
    return string.format("[[Retranscription:%s|%s]]", link, title)
end
-- Function 4: Force namespace to "Quotes:"
function p.getQuotesLink(frame)
    local key = string.lower(frame.args[1] or "")
    local entry = data[key]

    if not entry then
        return "Épisode non référencé"
    end

    local _, link, title = unpack(entry)  -- We ignore original namespace
    return string.format("[[Citations:%s|%s]]", link, title)
end

-- Function 5: Force namespace to "Category:Images from"
function p.getImagesLink(frame)
    local key = string.lower(frame.args[1] or "")
    local entry = data[key]

    if not entry then
        return "Épisode non référencé"
    end

    local _, link, _, title = unpack(entry)  -- We ignore original namespace
    return string.format("[[:Catégorie:Images de %s|%s]]", link, title)
end

-- Function 6: Retrieve from link
function p.getEpisodeLink(frame)
    local key = string.lower(frame.args[1] or "")
    local entry = data[key]

    if not entry then
        return "Épisode non référencé"
    end

    local _, link, _, _ = unpack(entry)  -- We ignore original namespace
    return string.format("%s", link)
end

-- Function 7: Get full episode link only from key
function p.getEpisodeFullLink(frame)
    local key = string.lower(frame.args[1] or "")
    local entry = data[key]

    if not entry then
        return "Épisode non référencé"
    end

    local ns, link, _, _ = unpack(entry)

    -- If namespace is empty or nil, omit it
    if ns == "" or ns == nil then
        return string.format("%s", link)
    else
        return string.format("%s:%s", ns, link)
    end
end

-- Function 8: Get episode title only from key
function p.getEpisodeTitle(frame)
    local key = string.lower(frame.args[1] or "")
    local entry = data[key]

    if not entry then
        return "Episode not found"
    end

    local _, _, _, title = unpack(entry)
    return string.format("%s", title)
end

return p