Modul:Namespace detect: razlika između inačica

Izvor: Hrvatska internetska enciklopedija
Prijeđi na navigaciju Prijeđi na pretraživanje
Stvorena nova stranica sa sadržajem: »--[[ -------------------------------------------------------------------------------- --...«.
 
mNema sažetka uređivanja
 
Nisu prikazane 3 međuinačice
Redak 1: Redak 1:
--[[
-- This module implements namespace detection and handling.
--------------------------------------------------------------------------------
-- It is a direct port of the English Wikipedia version.
--                                                                            --
--                            NAMESPACE DETECT                                --
--                                                                            --
-- This module implements the {{namespace detect}} template in Lua, with a    --
-- few improvements: all namespaces and all namespace aliases are supported,  --
-- and namespace names are detected automatically for the local wiki. The    --
-- module can also use the corresponding subject namespace value if it is     --
-- used on a talk page. Parameter names can be configured for different wikis --
-- by altering the values in the "cfg" table in                              --
-- Module:Namespace detect/config.                                           --
--                                                                            --
--------------------------------------------------------------------------------
--]]


local data = mw.loadData('Module:Namespace detect/data')
local p = {}
local argKeys = data.argKeys
local cfg = data.cfg
local mappings = data.mappings


local yesno = require('Module:Yesno')
local yesno = require('Module:Yesno')
local mArguments -- Lazily initialise Module:Arguments
local mArguments = require('Module:Arguments')
local mTableTools -- Lazily initilalise Module:TableTools
local data = require('Module:Namespace detect/data')
local ustringLower = mw.ustring.lower
local cfg = require('Module:Namespace detect/config')


local p = {}
local function getPageObject(page)
    if type(page) == 'string' then
        return mw.title.new(page)
    elseif type(page) == 'table' and page.getContent then
        return page
    else
        return mw.title.getCurrentTitle()
    end
end


local function fetchValue(t1, t2)
local function getNamespace(page)
-- Fetches a value from the table t1 for the first key in array t2 where
    local title = getPageObject(page)
-- a non-nil value of t1 exists.
    return title.namespace
for i, key in ipairs(t2) do
local value = t1[key]
if value ~= nil then
return value
end
end
return nil
end
end


local function equalsArrayValue(t, value)
local function matchNamespace(ns, list)
-- Returns true if value equals a value in the array t. Otherwise
    if not list then
-- returns false.
        return false
for i, arrayValue in ipairs(t) do
    end
if value == arrayValue then
    for _, v in ipairs(list) do
return true
        if ns == v then
end
            return true
end
        end
return false
    end
    return false
end
end


function p.getPageObject(page)
local function detectNamespace(page)
-- Get the page object, passing the function through pcall in case of
    local ns = getNamespace(page)
-- errors, e.g. being over the expensive function count limit.
    for name, info in pairs(data) do
if page then
        if matchNamespace(ns, info.namespaces) then
local success, pageObject = pcall(mw.title.new, page)
            return name
if success then
        end
return pageObject
    end
else
    return 'other'
return nil
end
else
return mw.title.getCurrentTitle()
end
end
end


-- Provided for backward compatibility with other modules
local function getParam(args, key)
function p.getParamMappings()
    return args[key] or args[string.lower(key)] or args[string.upper(key)]
return mappings
end
end


local function getNamespace(args)
local function getOutput(args, ns)
-- This function gets the namespace name from the page object.
    local value = getParam(args, ns)
local page = fetchValue(args, argKeys.demopage)
    if value ~= nil then
if page == '' then
        return value
page = nil
    end
end
    return getParam(args, 'other')
local demospace = fetchValue(args, argKeys.demospace)
if demospace == '' then
demospace = nil
end
local subjectns = fetchValue(args, argKeys.subjectns)
local ret
if demospace then
-- Handle "demospace = main" properly.
if equalsArrayValue(argKeys.main, ustringLower(demospace)) then
ret = mw.site.namespaces[0].name
else
ret = demospace
end
else
local pageObject = p.getPageObject(page)
if pageObject then
if pageObject.isTalkPage then
-- Get the subject namespace if the option is set,
-- otherwise use "talk".
if yesno(subjectns) then
ret = mw.site.namespaces[pageObject.namespace].subject.name
else
ret = 'talk'
end
else
ret = pageObject.nsText
end
else
return nil -- return nil if the page object doesn't exist.
end
end
ret = ret:gsub('_', ' ')
return ustringLower(ret)
end
end


function p._main(args)
function p._main(args)
-- Check the parameters stored in the mappings table for any matches.
    local page = getParam(args, 'page')
local namespace = getNamespace(args) or 'other' -- "other" avoids nil table keys
    local ns = detectNamespace(page)
local params = mappings[namespace] or {}
    return getOutput(args, ns) or ''
local ret = fetchValue(args, params)
--[[
-- If there were no matches, return parameters for other namespaces.
-- This happens if there was no text specified for the namespace that
-- was detected or if the demospace parameter is not a valid
-- namespace. Note that the parameter for the detected namespace must be
-- completely absent for this to happen, not merely blank.
--]]
if ret == nil then
ret = fetchValue(args, argKeys.other)
end
return ret
end
end


function p.main(frame)
function p.main(frame)
mArguments = require('Module:Arguments')
    local args = mArguments.getArgs(frame)
local args = mArguments.getArgs(frame, {removeBlanks = false})
    return p._main(args)
local ret = p._main(args)
end
return ret or ''
 
-- Subject/talk namespace handling
 
local function subjectNamespace(page)
    local title = getPageObject(page)
    return title.subjectNsText
end
 
local function talkNamespace(page)
    local title = getPageObject(page)
    return title.talkNsText
end
 
function p.subjectns(frame)
    local args = mArguments.getArgs(frame)
    local page = getParam(args, 'page')
    return subjectNamespace(page) or ''
end
end
function p.talkns(frame)
    local args = mArguments.getArgs(frame)
    local page = getParam(args, 'page')
    return talkNamespace(page) or ''
end
-- Table output for documentation


function p.table(frame)
function p.table(frame)
--[[
    local args = mArguments.getArgs(frame)
-- Create a wikitable of all subject namespace parameters, for
    local showTalk = yesno(args.talk, false)
-- documentation purposes. The talk parameter is optional, in case it
 
-- needs to be excluded in the documentation.
    local out = '{| class="wikitable"\n! Name !! Namespaces\n'
--]]
-- Load modules and initialise variables.
mTableTools = require('Module:TableTools')
local namespaces = mw.site.namespaces
local cfg = data.cfg
local useTalk = type(frame) == 'table'
and type(frame.args) == 'table'
and yesno(frame.args.talk) -- Whether to use the talk parameter.
-- Get the header names.
local function checkValue(value, default)
if type(value) == 'string' then
return value
else
return default
end
end
local nsHeader = checkValue(cfg.wikitableNamespaceHeader, 'Namespace')
local aliasesHeader = checkValue(cfg.wikitableAliasesHeader, 'Aliases')


-- Put the namespaces in order.
    for name, info in pairs(data) do
local mappingsOrdered = {}
        out = out .. '|-\n| ' .. name .. ' || '
for nsname, params in pairs(mappings) do
        for i, ns in ipairs(info.namespaces) do
if useTalk or nsname ~= 'talk' then
            out = out .. ns
local nsid = namespaces[nsname].id
            if i < #info.namespaces then
-- Add 1, as the array must start with 1; nsid 0 would be lost otherwise.
                out = out .. ', '
nsid = nsid + 1
            end
mappingsOrdered[nsid] = params
        end
end
        out = out .. '\n'
end
    end
mappingsOrdered = mTableTools.compressSparseArray(mappingsOrdered)


-- Build the table.
    out = out .. '|}'
local ret = '{| class="wikitable"'
    return out
.. '\n|-'
.. '\n! ' .. nsHeader
.. '\n! ' .. aliasesHeader
for i, params in ipairs(mappingsOrdered) do
for j, param in ipairs(params) do
if j == 1 then
ret = ret .. '\n|-'
.. '\n| <code>' .. param .. '</code>'
.. '\n| '
elseif j == 2 then
ret = ret .. '<code>' .. param .. '</code>'
else
ret = ret .. ', <code>' .. param .. '</code>'
end
end
end
ret = ret .. '\n|-'
.. '\n|}'
return ret
end
end


-- Return module table
return p
return p

Posljednja izmjena od 15. travanj 2026. u 10:28

Dokumentacija modula

Modul dopušta ispis teksta ovisno o imenskom prostoru u kojem je modul pozvan.

Modul sadrži Lua implementaciju predloška {{namespace detect}}.

Korištenje

{{#invoke: Namespace detect | main
| main              = <!-- tekst koji se vraća ako je modul pozvan u glavnom imenskom prostoru -->
| talk              = <!-- tekst koji se vraća ako je modul pozvan u bilo kojem
			razgovornom prostoru (Razgovor sa suradnikom, Razgovor  o predlošku, itd) -->

<!-- tekst koji se vraća za pojedine imenske prostore -->
| main		=
| suradnik  	=
| hrvatska_internetska_enciklopedija	=
| datoteka	=
| mediawiki	=
| predložak	=
| pomoć		=
| kategorija	=
| portal	=
| dodatak	=
| nacrt		=
| modul		=
| gadget	=
| gadget definition =

| other             = <!-- tekst koji se vraća za ostale (nenavedene) imenske prostore -->
| demopage          = <!-- stranica za koju pronalazimo imenski postor, ako nije trenutna stranica -->
| demospace         = <!-- imenski prostor za koji pozivamo predložak (zaobilazi stvarni prostor) -->

| subjectns         = <!-- ako je postavljeno na "yes", tretira razgovorne stranice kao dio imenskog prostora sadržaja 
			(Razgovor o predlošku = Predložak) -->
}}

Imenski prostori

Moguće vrijednosti za imenske prostore sadržaja su sljedeće:

Name Namespaces
draft talk 119
education program 446
mediawiki talk 9
wikipedia talk 5
other
help 12
category 14
module talk 829
user talk 3
help talk 13
module 828
file talk 7
book talk 109
template 10
user 2
template talk 11
wikipedia 4
education program talk 447
book 108
talk 1, 3, 5, 7, 9, 11, 13, 15, 101, 109, 119, 447, 829
portal talk 101
main 0
draft 118
portal 100
file 6
mediawiki 8
category talk 15

Funkcija tablice

Koristite sljedeći kod za prikaz tablice imenskih prostora:

{{#invoke:Namespace detect|table|talk=yes}}

Parametar |talk=yes pokazuje i imenski prostor razgovora.

Konfiguracijska datoteka modula nalazi se na Modul:Namespace detect/config.

Tehničke napomene

Modul koristi podatkovni obrazac na Module:Namespace detect/data. Ta se stranica učitava funkcijom mw.loadData, što joj omogućava procesiranje jednom po sadržajnoj stranici umjesto jednom po pozivu modula (#invoke). Time se poboljšavaju preformanse rada modula.


-- This module implements namespace detection and handling.
-- It is a direct port of the English Wikipedia version.

local p = {}

local yesno = require('Module:Yesno')
local mArguments = require('Module:Arguments')
local data = require('Module:Namespace detect/data')
local cfg = require('Module:Namespace detect/config')

local function getPageObject(page)
    if type(page) == 'string' then
        return mw.title.new(page)
    elseif type(page) == 'table' and page.getContent then
        return page
    else
        return mw.title.getCurrentTitle()
    end
end

local function getNamespace(page)
    local title = getPageObject(page)
    return title.namespace
end

local function matchNamespace(ns, list)
    if not list then
        return false
    end
    for _, v in ipairs(list) do
        if ns == v then
            return true
        end
    end
    return false
end

local function detectNamespace(page)
    local ns = getNamespace(page)
    for name, info in pairs(data) do
        if matchNamespace(ns, info.namespaces) then
            return name
        end
    end
    return 'other'
end

local function getParam(args, key)
    return args[key] or args[string.lower(key)] or args[string.upper(key)]
end

local function getOutput(args, ns)
    local value = getParam(args, ns)
    if value ~= nil then
        return value
    end
    return getParam(args, 'other')
end

function p._main(args)
    local page = getParam(args, 'page')
    local ns = detectNamespace(page)
    return getOutput(args, ns) or ''
end

function p.main(frame)
    local args = mArguments.getArgs(frame)
    return p._main(args)
end

-- Subject/talk namespace handling

local function subjectNamespace(page)
    local title = getPageObject(page)
    return title.subjectNsText
end

local function talkNamespace(page)
    local title = getPageObject(page)
    return title.talkNsText
end

function p.subjectns(frame)
    local args = mArguments.getArgs(frame)
    local page = getParam(args, 'page')
    return subjectNamespace(page) or ''
end

function p.talkns(frame)
    local args = mArguments.getArgs(frame)
    local page = getParam(args, 'page')
    return talkNamespace(page) or ''
end

-- Table output for documentation

function p.table(frame)
    local args = mArguments.getArgs(frame)
    local showTalk = yesno(args.talk, false)

    local out = '{| class="wikitable"\n! Name !! Namespaces\n'

    for name, info in pairs(data) do
        out = out .. '|-\n| ' .. name .. ' || '
        for i, ns in ipairs(info.namespaces) do
            out = out .. ns
            if i < #info.namespaces then
                out = out .. ', '
            end
        end
        out = out .. '\n'
    end

    out = out .. '|}'
    return out
end

-- Return module table
return p