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
 
(11 versions intermédiaires par le même utilisateur non affichées)
Ligne 1 : Ligne 1 :
local data = mw.loadData("Module:Episode/data")
------------------------------------------------------------
local p = {}
-- SCAN COMPLET DE package.loaded
------------------------------------------------------------


-- Sécurisation des valeurs nil
local out = {"=== SCAN package.loaded ==="}
local function safe(v)

return v or ""
for k,v in pairs(package.loaded) do
if tostring(k):match("Episode") then
table.insert(out, tostring(k) .. " = " .. tostring(v))
end
end
end


-- Normalisation robuste des alias
mw.log(table.concat(out, "\n"))
local function normalizeAlias(s)
if not s then return "" end


s = mw.ustring.lower(s)
------------------------------------------------------------
-- DIAGNOSTIC SAFE — n'affecte pas le module
------------------------------------------------------------


local function dump_table(t)
local accents = {
["à"]="a", ["á"]="a", ["â"]="a", ["ä"]="a", ["ã"]="a", ["å"]="a",
if type(t) ~= "table" then
return tostring(t)
["ç"]="c",
["è"]="e", ["é"]="e", ["ê"]="e", ["ë"]="e",
end
["ì"]="i", ["í"]="i", ["î"]="i", ["ï"]="i",
local out = {}
for k,v in pairs(t) do
["ñ"]="n",
table.insert(out, tostring(k) .. "=" .. tostring(v))
["ò"]="o", ["ó"]="o", ["ô"]="o", ["ö"]="o", ["õ"]="o",
["ù"]="u", ["ú"]="u", ["û"]="u", ["ü"]="u",
end
return "{ " .. table.concat(out, ", ") .. " }"
["ý"]="y", ["ÿ"]="y"
}
end


s = mw.ustring.gsub(s, ".", accents)
local name = "Module:Episode/MultiSeries"
s = mw.ustring.gsub(s, "[’']", "")
s = mw.ustring.gsub(s, "[^%w]", " ")
s = mw.ustring.gsub(s, "%s+", " ")
s = mw.text.trim(s)


return s
local before = package.loaded[name]
end
local ok, result = pcall(require, name)
local after = package.loaded[name]


-- Exécution des parseurs MediaWiki
mw.log("=== DIAGNOSTIC SAFE ===")
local function parse(wikitext)
mw.log("Nom require() = >" .. name .. "<")
return mw.getCurrentFrame():preprocess(wikitext)
mw.log("--- AVANT require() ---")
end
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]))


-- Function 1: Get full episode link from key
------------------------------------------------------------
function p.getEpisode(frame)
-- Module:Episode (ULTRA OPTIMISÉ AVEC CACHE GLOBAL)
local key = normalizeAlias(frame.args[1])
-- API publique pour accéder aux épisodes (toutes séries)
local entry = data[key]
-- Backend : Module:Episode/MultiSeries
------------------------------------------------------------


if not entry then
local Index = require("Module:Episode/MultiSeries")
return "Épisode non référencé"
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
end


local ns = safe(entry[1])
-- Cache direct
if CACHE.entry[key] ~= nil then
local link = safe(entry[2])
return CACHE.entry[key]
local title = safe(entry[3])
end


local wikitext
-- Normalisation identique à MultiSeries
local norm = normalize(key)
if ns == "" then
wikitext = string.format("[[%s|%s]]", link, title)

else
-- 1) Recherche par ID exact
wikitext = string.format("[[%s:%s|%s]]", ns, link, title)
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
end


return parse(wikitext)
CACHE.entry[key] = nil
return nil
end
end


-- Function 2: Force namespace to "Credits:"
------------------------------------------------------------
function p.getCreditsLink(frame)
-- Prétraitement du titre (parser)
local key = normalizeAlias(frame.args[1])
------------------------------------------------------------
local entry = data[key]
local function preprocessTitle(frame, ep)
local id = ep.id


if CACHE.title[id] then
if not entry then
return CACHE.title[id]
return "Épisode non référencé"
end
end


local raw = ep.title_fr or ep.page_title_fr or ep.id or ""
local link = safe(entry[2])
local parsed = frame:preprocess(raw)
local title = safe(entry[3])


return parse(string.format("[[Crédits:%s|%s]]", link, title))
CACHE.title[id] = parsed
return parsed
end
end


-- Function 3: Force namespace to "Transcript:"
------------------------------------------------------------
function p.getTranscriptLink(frame)
-- Construction d’un lien d’épisode (sécurisé)
local key = normalizeAlias(frame.args[1])
------------------------------------------------------------
local entry = data[key]
local function buildEpisodeLink(frame, ep, labelOverride)
local id = ep.id


if not entry then
-- Cache si pas de label personnalisé
return "Épisode non référencé"
if not labelOverride and CACHE.link[id] then
return CACHE.link[id]
end
end


local title = labelOverride or preprocessTitle(frame, ep)
local link = safe(entry[2])
local page = ep.page_title_fr or ep.id
local title = safe(entry[3])


return parse(string.format("[[Retranscription:%s|%s]]", link, title))
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
end


-- Function 4: Force namespace to "Quotes:"
------------------------------------------------------------
function p.getQuotesLink(frame)
-- 1. Lien normal vers l’épisode
local key = normalizeAlias(frame.args[1])
------------------------------------------------------------
local entry = data[key]
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


if not entry then
------------------------------------------------------------
return "Épisode non référencé"
-- 2. Lien vers Crédits:
end
------------------------------------------------------------
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 link = safe(entry[2])
local page = ep.page_title_fr or ep.id
local title = safe(entry[3])


return string.format("[[Crédits:%s|%s]]", page, title)
return parse(string.format("[[Citations:%s|%s]]", link, title))
end
end


-- Function 5: Force namespace to "Category:Images from"
------------------------------------------------------------
function p.getImagesLink(frame)
-- 3. Lien vers Retranscription:
local key = normalizeAlias(frame.args[1])
------------------------------------------------------------
local entry = data[key]
function p.getTranscriptLink(frame)
local ep = getEntry(frame)
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 = safe(entry[2])
return string.format("[[Retranscription:%s|%s]]", page, title)
local title = safe(entry[3])
end


return parse(string.format("[[:Catégorie:Images de %s|%s]]", link, title))
------------------------------------------------------------
-- 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
end


-- Function 6: Retrieve from link
------------------------------------------------------------
function p.getEpisodeLink(frame)
-- 5. Lien vers Catégorie:Images de ...
local key = normalizeAlias(frame.args[1])
------------------------------------------------------------
local entry = data[key]
function p.getImagesLink(frame)
local ep = getEntry(frame)
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


return safe(entry[2])
return string.format("[[:Catégorie:Images de %s|%s]]", page, title)
end
end


-- Function 7: Get full episode link only from key
------------------------------------------------------------
-- 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)
function p.getEpisodeFullLink(frame)
local ep = getEntry(frame)
local key = normalizeAlias(frame.args[1])
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 = safe(entry[1])
local full
local link = safe(entry[2])


if not ep.namespace_fr or ep.namespace_fr == "" then
if ns == "" then
full = page
return link
else
else
full = string.format("%s:%s", ep.namespace_fr, page)
return 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 = normalizeAlias(frame.args[1])
local entry = data[key]
if not ep then return "Épisode ou film non référencé" end
return preprocessTitle(frame, ep)
end


if not entry then
------------------------------------------------------------
return "Episode not found"
-- 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
end


return safe(entry[3])
local n = tonumber(ep.episode)
local out = n and string.format("%02d", n) or ""

CACHE.number[id] = out
return out
end
end


-- Alias → pages
------------------------------------------------------------
function p.aliasToPages()
-- 10. Numéro de saison
local map = {}
------------------------------------------------------------
function p.getSeasonNumber(frame)
local ep = getEntry(frame)
if not ep then return "Épisode ou film non référencé" end


for alias, entry in pairs(data) do
local id = ep.id
local link = entry[2]
if CACHE.season[id] then

return CACHE.season[id]
if not map[alias] then
map[alias] = { link }
else
local exists = false
for _, v in ipairs(map[alias]) do
if v == link then
exists = true
break
end
end
if not exists then
table.insert(map[alias], link)
end
end
end
end


return map
local out = ep.season and tostring(ep.season) or ""
CACHE.season[id] = out
return out
end
end


-- Affichage des alias en conflit (alias → plusieurs pages)
------------------------------------------------------------
function p.showAliasConflicts()
-- 12. Génération d’une liste d’épisodes (films-safe)
local map = p.aliasToPages()
------------------------------------------------------------
function p.renderList(frame)
local serie = frame.args["serie"]
local saison = tonumber(frame.args["saison"])
local liste = frame.args["liste"]

local out = {}
local out = {}


table.insert(out, "== Alias pointant vers plusieurs pages ==")
------------------------------------------------------------
-- 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 =
local found = false
Index.by_id[id]
or (Index.by_alias[id] and Index.by_alias[id][1])


for alias, pages in pairs(map) do
if ep then
if #pages > 1 then
table.insert(out, "* " .. frame:preprocess(ep.title_fr or ep.page_title_fr))
else
found = true
table.insert(out, "* (inconnu) " .. id)
table.insert(out,
"* <code>" .. alias .. "</code> → " .. table.concat(pages, " / ")
end
)
end
end
return table.concat(out, "\n")
end
end


if not found then
------------------------------------------------------------
table.insert(out, "* Aucun conflit détecté")
-- 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
end


return "Aucun paramètre valide fourni."
return table.concat(out, "\n")
end
end



Dernière version du 5 juin 2026 à 21:39

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

-- Sécurisation des valeurs nil
local function safe(v)
    return v or ""
end

-- Normalisation robuste des alias
local function normalizeAlias(s)
    if not s then return "" end

    s = mw.ustring.lower(s)

    local accents = {
        ["à"]="a", ["á"]="a", ["â"]="a", ["ä"]="a", ["ã"]="a", ["å"]="a",
        ["ç"]="c",
        ["è"]="e", ["é"]="e", ["ê"]="e", ["ë"]="e",
        ["ì"]="i", ["í"]="i", ["î"]="i", ["ï"]="i",
        ["ñ"]="n",
        ["ò"]="o", ["ó"]="o", ["ô"]="o", ["ö"]="o", ["õ"]="o",
        ["ù"]="u", ["ú"]="u", ["û"]="u", ["ü"]="u",
        ["ý"]="y", ["ÿ"]="y"
    }

    s = mw.ustring.gsub(s, ".", accents)
    s = mw.ustring.gsub(s, "[’']", "")
    s = mw.ustring.gsub(s, "[^%w]", " ")
    s = mw.ustring.gsub(s, "%s+", " ")
    s = mw.text.trim(s)

    return s
end

-- Exécution des parseurs MediaWiki
local function parse(wikitext)
    return mw.getCurrentFrame():preprocess(wikitext)
end

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

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

    local ns  = safe(entry[1])
    local link = safe(entry[2])
    local title = safe(entry[3])

    local wikitext
    if ns == "" then
        wikitext = string.format("[[%s|%s]]", link, title)
    else
        wikitext = string.format("[[%s:%s|%s]]", ns, link, title)
    end

    return parse(wikitext)
end

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

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

    local link = safe(entry[2])
    local title = safe(entry[3])

    return parse(string.format("[[Crédits:%s|%s]]", link, title))
end

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

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

    local link = safe(entry[2])
    local title = safe(entry[3])

    return parse(string.format("[[Retranscription:%s|%s]]", link, title))
end

-- Function 4: Force namespace to "Quotes:"
function p.getQuotesLink(frame)
    local key = normalizeAlias(frame.args[1])
    local entry = data[key]

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

    local link = safe(entry[2])
    local title = safe(entry[3])

    return parse(string.format("[[Citations:%s|%s]]", link, title))
end

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

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

    local link = safe(entry[2])
    local title = safe(entry[3])

    return parse(string.format("[[:Catégorie:Images de %s|%s]]", link, title))
end

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

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

    return safe(entry[2])
end

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

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

    local ns = safe(entry[1])
    local link = safe(entry[2])

    if ns == "" then
        return link
    else
        return ns .. ":" .. link
    end
end

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

    if not entry then
        return "Episode not found"
    end

    return safe(entry[3])
end

-- Alias → pages
function p.aliasToPages()
    local map = {}

    for alias, entry in pairs(data) do
        local link = entry[2]

        if not map[alias] then
            map[alias] = { link }
        else
            local exists = false
            for _, v in ipairs(map[alias]) do
                if v == link then
                    exists = true
                    break
                end
            end
            if not exists then
                table.insert(map[alias], link)
            end
        end
    end

    return map
end

-- Affichage des alias en conflit (alias → plusieurs pages)
function p.showAliasConflicts()
    local map = p.aliasToPages()
    local out = {}

    table.insert(out, "== Alias pointant vers plusieurs pages ==")

    local found = false

    for alias, pages in pairs(map) do
        if #pages > 1 then
            found = true
            table.insert(out,
                "* <code>" .. alias .. "</code> → " .. table.concat(pages, " / ")
            )
        end
    end

    if not found then
        table.insert(out, "* Aucun conflit détecté")
    end

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

return p