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


-- Sécurisation des valeurs nil
-- Centralized Episode Index (multi-series, episodes + movies)
local function safe(v)
local Index = require("Module:Episode/index")
return v or ""

------------------------------------------------------------
-- Internal helper: retrieve episode entry from key
------------------------------------------------------------
local function getEntry(frame)
local key = frame.args[1]
if not key or key == "" then
return nil
end
return Index.get(key)
end
end


-- Normalisation robuste des alias
------------------------------------------------------------
local function normalizeAlias(s)
-- Internal helper: preprocess a title using the parser
if not s then return "" end
-- Falls back gracefully if title is missing.

------------------------------------------------------------
s = mw.ustring.lower(s)
local function preprocessTitle(frame, ep)

local raw = ep.title_fr or ep.page_title or ep.property or ep.id or ""
-- 0) Supprimer les apostrophes HTML
return frame:preprocess(raw)
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


------------------------------------------------------------
-- Internal helper: build a link using the episode namespace
-- If namespace is empty (movies), omit it.
-- Title is always preprocessed here.
------------------------------------------------------------
local function buildEpisodeLink(frame, ep, labelOverride)
local title = labelOverride or preprocessTitle(frame, ep)


-- Exécution des parseurs MediaWiki
if ep.namespace == "" or ep.namespace == nil then
local function parse(wikitext)
-- Movies and telefilms: no namespace
return mw.getCurrentFrame():preprocess(wikitext)
return string.format("[[%s|%s]]", ep.page_title, title)
else
-- Regular episodes: use namespace
return string.format("[[%s:%s|%s]]", ep.namespace, ep.page_title, title)
end
end
end


-- Function 1: Get full episode link from key
------------------------------------------------------------
-- Function 1: Get full episode link (default namespace)
-- Uses preprocessed French title as label.
------------------------------------------------------------
function p.getEpisode(frame)
function p.getEpisode(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

return buildEpisodeLink(frame, ep)
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
end


-- Function 2: Force namespace to "Credits:"
------------------------------------------------------------
-- Function 2: Force namespace to "Crédits:"
-- Uses preprocessed French title as label.
------------------------------------------------------------
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])
local title = safe(entry[3])
return string.format("[[Crédits:%s|%s]]", ep.page_title, title)

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


-- Function 3: Force namespace to "Transcript:"
------------------------------------------------------------
-- Function 3: Force namespace to "Retranscription:"
-- Uses preprocessed French title as label.
------------------------------------------------------------
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é"
if not entry then
return "Épisode non référencé"
end
end


local title = preprocessTitle(frame, ep)
local link = safe(entry[2])
local title = safe(entry[3])
return string.format("[[Retranscription:%s|%s]]", ep.page_title, title)

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


-- Function 4: Force namespace to "Quotes:"
------------------------------------------------------------
-- Function 4: Force namespace to "Citations:"
-- Uses preprocessed French title as label.
------------------------------------------------------------
function p.getQuotesLink(frame)
function p.getQuotesLink(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])
local title = safe(entry[3])
return string.format("[[Citations:%s|%s]]", ep.page_title, title)

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


-- Function 5: Force namespace to "Category:Images from"
------------------------------------------------------------
-- Function 5: Force namespace to "Catégorie:Images de ..."
-- Leading colon prevents categorization when used in articles.
-- Uses preprocessed French title as label.
------------------------------------------------------------
function p.getImagesLink(frame)
function p.getImagesLink(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])
local title = safe(entry[3])
return string.format("[[:Catégorie:Images de %s|%s]]", ep.page_title, title)

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


-- Function 6: Retrieve from link
------------------------------------------------------------
-- Function 6: Retrieve only the page title (no namespace)
-- No preprocess needed here.
------------------------------------------------------------
function p.getEpisodeLink(frame)
function p.getEpisodeLink(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

return ep.page_title
return safe(entry[2])
end
end


-- Function 7: Get full episode link only from key
------------------------------------------------------------
-- Function 7: Retrieve full link target (namespace + title)
-- Example: "Épisode:Enfants des dieux"
-- Movies return only "Enfants des dieux".
-- No preprocess needed here.
------------------------------------------------------------
function p.getEpisodeFullLink(frame)
function p.getEpisodeFullLink(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 ns = safe(entry[1])
if ep.namespace == "" or ep.namespace == nil then
local link = safe(entry[2])
return ep.page_title

if ns == "" then
return link
else
else
return string.format("%s:%s", ep.namespace, ep.page_title)
return ns .. ":" .. link
end
end
end
end


-- Function 8: Get episode title only from key
------------------------------------------------------------
-- Function 8: Retrieve only the French title (preprocessed)
------------------------------------------------------------
function p.getEpisodeTitle(frame)
function p.getEpisodeTitle(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 "Episode not found"
end
end

return preprocessTitle(frame, ep)
return safe(entry[3])
end
end


-- Alias → pages
------------------------------------------------------------
function p.aliasToPages()
-- Function 9: Get the episode number as a two‑digit string
local map = {}
-- Returns "" for movies (no episode number)
------------------------------------------------------------
function p.getEpisodeNumber(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
-- Movies and telefilms have no episode number
if not ep.episode or ep.episode == "" then
local link = entry[2]

return ""
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
-- Format as two digits (01, 02, ..., 22)
return string.format("%02d", tonumber(ep.episode))
end
end


------------------------------------------------------------
-- Function 10-FR: Get the season number as a numeric string
-- Returns "" for movies (no season number)
------------------------------------------------------------
function p.getSeasonNumberFR(frame)
local ep = getEntry(frame)
if not ep then
return "Épisode ou film non référencé"
end


function p.debugAllAliases()
-- Movies and telefilms have no season number
local out = {}
if not ep.season or ep.season == "" then
table.insert(out, "== Vérification complète des alias ==")
return ""

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, "\n")
-- Return numeric season (no formatting)
end
return tostring(ep.season)

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
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