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
 
(32 versions intermédiaires par le même utilisateur non affichées)
Ligne 1 : Ligne 1 :
local data = mw.loadData("Module:Episode/data")
------------------------------------------------------------
-- Module:Episode
-- API publique pour accéder aux épisodes (toutes séries)
-- Backend : Module:Episode/MultiSeries
------------------------------------------------------------

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


-- Sécurisation des valeurs nil
------------------------------------------------------------
local function safe(v)
-- Récupère une entrée d’épisode depuis un argument
return v or ""
------------------------------------------------------------
end
local function getEntry(frame)
local key = frame.args[1]
if not key or key == "" then
return nil
end


-- Normalisation simple pour les alias
-- Normalisation robuste des alias
local norm = mw.ustring.lower(key)
local function normalizeAlias(s)
norm = norm:gsub("%s+", " ")
if not s then return "" end


s = mw.ustring.lower(s)
-- 1) Recherche par ID exact
if Index.by_id[key] then
return Index.by_id[key]
end


-- 2) Recherche par alias normalisé
-- 0) Supprimer les apostrophes HTML
s = mw.ustring.gsub(s, "'", "")
if Index.by_alias[norm] then
s = mw.ustring.gsub(s, "'", "")
return Index.by_alias[norm][1] -- premier match
s = mw.ustring.gsub(s, "’", "")
end
s = mw.ustring.gsub(s, "‘", "")

-- 1) Supprimer les apostrophes simples et typographiques
s = mw.ustring.gsub(s, "[’']", "")

-- 2) Supprimer les virgules sans laisser d'espace
s = mw.ustring.gsub(s, ",", "")

-- 3) Remplacement des accents
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)

-- 4) Remplacer les autres caractères non alphanumériques par un espace
s = mw.ustring.gsub(s, "[^%w]", " ")

-- 5) Compresser les espaces
s = mw.ustring.gsub(s, "%s+", " ")

-- 6) Trim
s = mw.text.trim(s)


return nil
return s
end
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 ""


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


-- Function 1: Get full episode link from key
------------------------------------------------------------
function p.getEpisode(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 title = labelOverride or preprocessTitle(frame, ep)
if not entry then
return "Épisode non référencé"
end


local ns = safe(entry[1])
-- Page cible (fallbacks sûrs)
local page =
local link = safe(entry[2])
local title = safe(entry[3])
ep.page_title_fr


local wikitext
if not ep.namespace or ep.namespace == "" then
-- Films / téléfilms
if ns == "" then
return string.format("[[%s|%s]]", page, title_fr)
wikitext = string.format("[[%s|%s]]", link, title)
else
else
wikitext = string.format("[[%s:%s|%s]]", ns, link, title)
-- Épisodes normaux
return string.format("[[%s:%s|%s]]", ep.namespace, page, title_fr)
end
end
end


return parse(wikitext)
------------------------------------------------------------
-- 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 = normalizeAlias(frame.args[1])
if not ep then
local entry = data[key]

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

local title = preprocessTitle(frame, ep)
local link = safe(entry[2])
return string.format("[[Crédits:%s|%s]]", ep.page_title_fr or ep.id, title_fr)
local title = safe(entry[3])

return parse(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 = normalizeAlias(frame.args[1])
if not ep then
local entry = data[key]
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


if not entry then
------------------------------------------------------------
return "Épisode non référencé"
-- 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
end
local title = preprocessTitle(frame, ep)
return string.format("[[Citations:%s|%s]]", ep.page_title_fr or ep.id, title_fr)
end


local link = safe(entry[2])
------------------------------------------------------------
local title = safe(entry[3])
-- 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


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


-- Function 4: Force namespace to "Quotes:"
------------------------------------------------------------
function p.getQuotesLink(frame)
-- 7. Récupère le lien complet (namespace + titre)
local key = normalizeAlias(frame.args[1])
------------------------------------------------------------
local entry = data[key]
function p.getEpisodeFullLink(frame)

local ep = getEntry(frame)
if not ep then
if not entry then
return "Épisode ou film non référencé"
return "Épisode non référencé"
end
end


local page =
local link = safe(entry[2])
local title = safe(entry[3])
ep.page_title_fr
or ep.page_title_en
or ep.property
or ep.id


return parse(string.format("[[Citations:%s|%s]]", link, title))
if not ep.namespace or ep.namespace == "" then
return page
else
return string.format("%s:%s", ep.namespace, page)
end
end
end


-- Function 5: Force namespace to "Category:Images from"
------------------------------------------------------------
function p.getImagesLink(frame)
-- 8. Récupère uniquement le titre français (prétraité)
local key = normalizeAlias(frame.args[1])
------------------------------------------------------------
local entry = data[key]
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


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


local link = safe(entry[2])
if not ep.episode or ep.episode == "" then
return ""
local title = safe(entry[3])
end


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


-- Function 6: Retrieve from link
------------------------------------------------------------
function p.getEpisodeLink(frame)
-- 10. Numéro de saison
local key = normalizeAlias(frame.args[1])
------------------------------------------------------------
local entry = data[key]
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
if not entry then
return ""
return "Épisode non référencé"
end
end


return tostring(ep.season)
return safe(entry[2])
end
end


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

local d = Index.duplicates
if not d then
if not entry then
return "Aucun rapport de doublons disponible"
return "Épisode non référencé"
end
end


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


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


-- Function 8: Get episode title only from key
table.insert(out, "\n== Alias dupliqués ==")
function p.getEpisodeTitle(frame)
if #d.duplicate_aliases == 0 then
local key = normalizeAlias(frame.args[1])
table.insert(out, "Aucun")
local entry = data[key]
else
for _, alias in ipairs(d.duplicate_aliases) do
table.insert(out, "* " .. alias)
end
end


if not entry then
table.insert(out, "\n== Alias contradictoires ==")
return "Episode not found"
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
end


return table.concat(out, "\n")
return safe(entry[3])
end
end


-- Alias → pages
------------------------------------------------------------
function p.aliasToPages()
-- 12. Génération d’une liste d’épisodes
local map = {}
------------------------------------------------------------
function p.renderList(frame)
local serie = frame.args["serie"]
local saison = tonumber(frame.args["saison"])
local liste = frame.args["liste"]


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


if not map[alias] then
------------------------------------------------------------
map[alias] = { link }
-- Mode 1 : liste personnalisée
else
------------------------------------------------------------
local exists = false
if liste and liste ~= "" then
for id in mw.text.gsplit(liste, ",") do
for _, v in ipairs(map[alias]) do
id = mw.text.trim(id)
if v == link then
local ep =
exists = true
Index.by_id[id]
break
or (Index.by_alias[id] and Index.by_alias[id][1])
end
end

if ep then
if not exists then
table.insert(out, "* " .. frame:preprocess(ep.title_fr))
table.insert(map[alias], link)
else
table.insert(out, "* (inconnu) " .. id)
end
end
end
end
return table.concat(out, "\n")
end
end


return map
------------------------------------------------------------
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)


function p.debugAllAliases()
local lines = {}
local out = {}
for _, item in ipairs(out) do
table.insert(out, "== Vérification complète des alias ==")
table.insert(lines, item.text)
end


for alias, entry in pairs(data) do
return table.concat(lines, "\n")
local norm = normalizeAlias(alias)
local ok = data[norm] and "✔" or "✘"

table.insert(out,
string.format(
"* %s <code>%s</code> → <code>%s</code>%s",
ok,
alias,
norm,
ok == "✘" and " (introuvable)" or ""
)
)
end
end


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


function p.testTitle(frame)
table.sort(out, function(a,b) return a.sort < b.sort end)
local input = frame.args[1] or ""
local norm = normalizeAlias(input)
local exists = data[norm] ~= nil


local lines = {}
local out = {}
table.insert(out, "== Test de normalisation ==")
for _, item in ipairs(out) do
table.insert(out, "* Entrée : <code>" .. input .. "</code>")
table.insert(lines, item.text)
table.insert(out, "* Normalisé : <code>" .. norm .. "</code>")
end
table.insert(out, "* Trouvé : " .. (exists and "✔ oui" or "✘ non"))


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

return "Aucun paramètre valide fourni."
end
end



Dernière version du 7 juin 2026 à 14:25

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)

    -- 0) Supprimer les apostrophes HTML
    s = mw.ustring.gsub(s, "&#39;", "")
    s = mw.ustring.gsub(s, "&apos;", "")
    s = mw.ustring.gsub(s, "&rsquo;", "")
    s = mw.ustring.gsub(s, "&lsquo;", "")

    -- 1) Supprimer les apostrophes simples et typographiques
    s = mw.ustring.gsub(s, "[’']", "")

    -- 2) Supprimer les virgules sans laisser d'espace
    s = mw.ustring.gsub(s, ",", "")

    -- 3) Remplacement des accents
    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)

    -- 4) Remplacer les autres caractères non alphanumériques par un espace
    s = mw.ustring.gsub(s, "[^%w]", " ")

    -- 5) Compresser les espaces
    s = mw.ustring.gsub(s, "%s+", " ")

    -- 6) Trim
    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


function p.debugAllAliases()
    local out = {}
    table.insert(out, "== Vérification complète des alias ==")

    for alias, entry in pairs(data) do
        local norm = normalizeAlias(alias)
        local ok = data[norm] and "✔" or "✘"

        table.insert(out,
            string.format(
                "* %s <code>%s</code> → <code>%s</code>%s",
                ok,
                alias,
                norm,
                ok == "✘" and " (introuvable)" or ""
            )
        )
    end

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

function p.testTitle(frame)
    local input = frame.args[1] or ""
    local norm = normalizeAlias(input)
    local exists = data[norm] ~= nil

    local out = {}
    table.insert(out, "== Test de normalisation ==")
    table.insert(out, "* Entrée : <code>" .. input .. "</code>")
    table.insert(out, "* Normalisé : <code>" .. norm .. "</code>")
    table.insert(out, "* Trouvé : " .. (exists and "✔ oui" or "✘ non"))

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

return p