Aller au contenu

Module:TradRef

Une page de Wikipédia, l'encyclopédie libre.
Ceci est une version archivée de cette page, en date du 26 février 2017 à 07:12 et modifiée en dernier par Od1n (discuter | contributions) (catégorisation bien entendu si URL invalide, et aussi si aucune URL ou trop d'URL ; voir Catégorie:Page utilisant un modèle avec une syntaxe erronée). Elle peut contenir des erreurs, des inexactitudes ou des contenus vandalisés non présents dans la version actuelle.

 Documentation[voir] [modifier] [historique] [purger]

Ce module Lua est utilisé par le modèle {{TradRef}}.

-- À faire :
-- * Vérifier que le modèle est seulement utilisé dans l'espace de noms principal
-- ** S'inspirer de [[Modèle:Traduction/Référence]]

local moduleLangue = require 'Module:Langue'
local p = {}

function p.getNomProjet(prefixe, projet)

    local prefixes = {
        commons     = 'Wikimedia Commons',
        meta        = 'Meta-Wiki',
        species     = 'Wikispecies',
    }

    local projets = {
        wikibooks   = 'Wikilivres',
        wikidata    = 'Wikidata',
        wikinews    = 'Wikinews',
        wikipedia   = 'Wikipédia',
        wikisource  = 'Wikisource',
        wikiversity = 'Wikiversité',
        wikivoyage  = 'Wikivoyage',
        wiktionary  = 'Wiktionnaire',
    }

    return prefixes[prefixe] or projets[projet] or '(projet inconnu)'
end


function p.parseUrl(url)

    -- Deux formats d'URL :
    -- * https://de.wikipedia.org    /w/index.php?title=    Amandus_(Bagaudenf%C3%BChrer)&oldid=148366994
    -- * https://de.wikipedia.org    /wiki/                 Amandus_(Bagaudenf%C3%BChrer)?oldid=148366994

    local uri = mw.uri.new(url)
    local host, path, query = uri.host, uri.path, uri.query

    if not (host and path and query) then
        return
    end

    local prefixe, projet = host:match('^(%a+)%.(%a+)%.org$')

    local title
    if path == '/w/index.php' and type(query.title) == 'string' and query.title ~= '' then
        title = query.title
    elseif path:sub(1, 6) == '/wiki/' and path:sub(7) ~= '' then
        title = path:sub(7)
    end

    local oldid
    if type(query.oldid) == 'string' and query.oldid:match('^%d+$') then
        oldid = query.oldid
    end

    if prefixe and projet and title and oldid then
        return prefixe, projet, title, oldid
    end
end


function p.getIndicateurAndFragment(url)

    local prefixe, projet, title, oldid = p.parseUrl(url)

    if not prefixe then
        return
    end

    local htmlIndicateur, nomProjet
    if prefixe == 'commons' or prefixe == 'meta' or prefixe == 'species' or prefixe == 'www' then
        -- Pour les wikis multilingues :
        -- * Commons      :  https://commons.wikimedia.org/w/index.php?title=Accueil&oldid=169324172
        -- * Meta-Wiki    :  https://meta   .wikimedia.org/w/index.php?title=Accueil&oldid=15738990
        -- * Wikispecies  :  https://species.wikimedia.org/w/index.php?title=Accueil&oldid=2717007
        -- * Wikidata     :  https://    www.wikidata.org/w/index.php?title=Wikidata:Main_Page&oldid=400496920
        -- Retourne {{mul}} comme indicateur de langue et le fragment de phrase « de la page de Wikimedia Commons en ... (voir liste des auteurs) »
        -- (Les projets sans langue précisée sont, en théorie, multilingues.)
        htmlIndicateur = moduleLangue.indicationMultilingue({})
        nomProjet = p.getNomProjet(prefixe, projet)
    else
        -- Pour les wikis unilingues (WIKT.ES, WP.EN, WS.DE...)
        -- Retourne l'indicateur de langue et le fragment de phrase « de la page de Wiki... en ... (voir liste des auteurs) »
        htmlIndicateur = moduleLangue.indicationDeLangue({'', prefixe})
        nomProjet = p.getNomProjet(prefixe, projet) .. ' en ' .. moduleLangue.nomLangue(prefixe)
    end

    -- Remplacer %C3%B par un caractère Unicode, et _ par une espace
    local titleDecoded = mw.uri.decode(title, 'WIKI')

    local linkOldid   = '<span class="plainlinks">[//' .. prefixe .. '.' .. projet .. '.org/w/index.php?title=' .. title .. '&oldid=' .. oldid .. ' ' .. titleDecoded .. ']</span>'
    local linkHistory = '<span class="plainlinks">[//' .. prefixe .. '.' .. projet .. '.org/w/index.php?title=' .. title .. '&action=history voir la liste des auteurs]</span>'

    local htmlFragment = 'de la page de ' .. nomProjet .. ' intitulée « ' .. linkOldid .. ' » <small>(' .. linkHistory .. ')</small>'

    return htmlIndicateur, htmlFragment
end


function p.enumeration(valeurs, separateur, dernierSeparateur)
    if not dernierSeparateur then
        dernierSeparateur = separateur
    end
    if #valeurs == 0 then
        return ''
    elseif #valeurs == 1 then
        return valeurs[1]
    else
        return table.concat(valeurs, separateur, 1, #valeurs - 1) .. dernierSeparateur .. valeurs[#valeurs]
    end
end


function p.ConstruireTradRef(frame)
    local args = frame:getParent().args
    local hasErrors = false

    -- Selon les « préfixes » d'URL (en, de, www, commons...), noter des informations
    local indicateurs = {}
    local fragments = {}
    for i = 1, 5 do
        local nomParam = 'url'..i
        if i == 1 and not args.url1 and args.url then
            nomParam = 'url'
        end
        local url = args[nomParam]
        if not url then
            break
        end
        indicateurs[i], fragments[i] = p.getIndicateurAndFragment(url)
        if not indicateurs[i] then
            indicateurs[i], fragments[i] = '-', 'paramètre <code>'..nomParam..'</code> invalide'
            hasErrors = true
        end
    end

    -- Composer le message à l'intention des lecteurs
    local html
    if not args.url1 and not args.url then
        html = 'Chaque paramètre url1, url2, url3... doit être suivi d\'une adresse Web.'
        hasErrors = true
    elseif args.url6 then
        html = 'Le modèle n\'accepte pas plus que 5 adresses Web.'
        hasErrors = true
    else
        html = '<p><span style="padding-left: 1.2em;">'
            .. p.enumeration(indicateurs, '/')
            .. ' Cet article est partiellement ou en totalité issu '
            .. p.enumeration(fragments, ', ', ' et ')
            .. '.</span></p>'
    end

    if hasErrors then
        html = html .. '[[Catégorie:Page utilisant un modèle avec une syntaxe erronée|TradRef]]'
    end

    return html
end


-- Fonction servant uniquement à tester du code.
function p.test()
local arguments = {}
arguments[1] = 'https://de.wikipedia.org/w/index.php?title=DEUTSCH&oldid=148366994'
arguments[2] = 'https://commons.wikimedia.org/w/index.php?title=FILE&oldid=232035766'
arguments[3] = 'https://pt.wikipedia.org/w/index.php?title=PORTUGAIS&oldid=148366994'
arguments[4] = 'https://meta.wikimedia.org/w/index.php?title=META&oldid=1111'
arguments[5] = 'https://ru.wikipedia.org/w/index.php?title=RUSSE&oldid=101010101'
arguments[6] = 'https://species.wikimedia.org/w/index.php?title=SPECIES&oldid=222222'

-- local url = 'https://en.wikipedia.org/wiki/Adrienne_Mayor?oldid=716665156'
-- local url = 'https://species.wikimedia.org/w/index.php?title=SPECIES&oldid=222222'



-- mw.log(prefixe .. '\n' .. projet .. '\n' .. title .. '\n' .. oldid)

end

return p