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
 
(40 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 Stargate
-- Utilise Module:Episode/MultiSeries (indexation complète)
------------------------------------------------------------

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


-- Sécurisation des valeurs nil
------------------------------------------------------------
local function safe(v)
-- Normalisation locale (même logique que MultiSeries)
return v or ""
------------------------------------------------------------
end
local function normalize(str)
if not str then return "" end


-- Normalisation robuste des alias
str = mw.ustring.lower(str)
local function normalizeAlias(s)
str = mw.ustring.toNFD(str)
if not s then return "" end
str = mw.ustring.gsub(str, "%pM", "")
str = str:gsub("'%s*", "")
str = str:gsub("[{}]", " ")
str = str:gsub("[%p]", " ")
str = str:gsub("[^%w%s]", " ")
str = str:gsub("%s+", " ")
str = mw.text.trim(str)


s = mw.ustring.lower(s)
return str

-- 0) Supprimer les apostrophes HTML
s = mw.ustring.gsub(s, "'", "")
s = mw.ustring.gsub(s, "'", "")
s = mw.ustring.gsub(s, "’", "")
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 s
end
end



------------------------------------------------------------
-- Exécution des parseurs MediaWiki
-- Récupération d’un épisode par ID
local function parse(wikitext)
------------------------------------------------------------
return mw.getCurrentFrame():preprocess(wikitext)
function p.getEpisodeById(id)
if not id then return nil end
return Multi.by_id[id]
end
end


-- Function 1: Get full episode link from key
------------------------------------------------------------
function p.getEpisode(frame)
-- Récupération d’un épisode par alias
local key = normalizeAlias(frame.args[1])
------------------------------------------------------------
local entry = data[key]
function p.getEpisodeByAlias(alias)
if not alias then return nil end
alias = normalize(alias)
local list = Multi.by_alias[alias]
if not list then return nil end


if not entry then
-- Si un seul épisode correspond → renvoyer directement
return "Épisode non référencé"
if #list == 1 then
return list[1]
end
end


local ns = safe(entry[1])
-- Sinon renvoyer la liste complète (ambiguïté)
local link = safe(entry[2])
return list
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
end


-- Function 2: Force namespace to "Credits:"
------------------------------------------------------------
function p.getCreditsLink(frame)
-- Récupération d’un épisode par titre de page
local key = normalizeAlias(frame.args[1])
------------------------------------------------------------
local entry = data[key]
function p.getEpisodeByPage(page)

if not page then return nil end
if not entry then
return Multi.by_page[normalize(page)]
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
end


-- Function 3: Force namespace to "Transcript:"
------------------------------------------------------------
function p.getTranscriptLink(frame)
-- Récupération d’un épisode par série + saison + numéro
local key = normalizeAlias(frame.args[1])
------------------------------------------------------------
local entry = data[key]
function p.getEpisode(series, season, episode)
if not (series and season and episode) then return nil end


if not entry then
local key = series .. "-" .. season
return "Épisode non référencé"
local list = Multi.by_season[key]
end
if not list then return nil end


for _, ep in ipairs(list) do
local link = safe(entry[2])
local title = safe(entry[3])
if ep.episode == tonumber(episode) then

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


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

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


-- Function 5: Force namespace to "Category:Images from"
------------------------------------------------------------
function p.getImagesLink(frame)
-- Génération d’un lien vers l’épisode
local key = normalizeAlias(frame.args[1])
------------------------------------------------------------
local entry = data[key]
function p.getEpisodeLink(ep)

if not ep then return "" end
if not entry then
return string.format("[[%s:%s]]", ep.namespace_fr or "Épisode", ep.page_title_fr)
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
end


-- Function 6: Retrieve from link
------------------------------------------------------------
function p.getEpisodeLink(frame)
-- Génération d’un lien complet (FR + EN)
local key = normalizeAlias(frame.args[1])
------------------------------------------------------------
local entry = data[key]
function p.getEpisodeFullLink(ep)
if not ep then return "" end


if not entry then
local fr = string.format("[[%s:%s|%s]]",
ep.namespace_fr or "Épisode",
return "Épisode non référencé"
end
ep.page_title_fr,
ep.title_fr or ep.page_title_fr
)


return safe(entry[2])
local en = string.format("[[%s:%s|%s]]",
end
ep.namespace_en or "Episode",
ep.page_title_en,
ep.title_en or ep.page_title_en
)


-- Function 7: Get full episode link only from key
return fr .. " / " .. en
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
end


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

local id = frame.args[1] or frame.args.id
if not entry then
local ep = p.getEpisodeById(id)
if not ep then return "" end
return "Episode not found"
end
return p.getEpisodeLink(ep)

return safe(entry[3])
end
end


-- Alias → pages
function p.alias(frame)
function p.aliasToPages()
local alias = frame.args[1] or frame.args.alias
local ep = p.getEpisodeByAlias(alias)
local map = {}
if not ep then return "" end


for alias, entry in pairs(data) do
if type(ep) == "table" and ep.id then
return p.getEpisodeLink(ep)
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
end


return map
-- Ambiguïté : renvoyer la liste
end


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

table.insert(out, p.getEpisodeLink(e))
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
end

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


function p.page(frame)
function p.testTitle(frame)
local page = frame.args[1] or frame.args.page
local input = frame.args[1] or ""
local ep = p.getEpisodeByPage(page)
local norm = normalizeAlias(input)
if not ep then return "" end
local exists = data[norm] ~= nil

return p.getEpisodeLink(ep)
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
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