「モジュール:Namespace detect」を編集中

移動先: 案内検索

警告: ログインしていません。編集を行うと、あなたの IP アドレスが公開されます。ログインまたはアカウントを作成すれば、あなたの編集はその利用者名とともに表示されるほか、その他の利点もあります。

この編集を取り消せます。 下記の差分を確認して、本当に取り消していいか検証してください。よろしければ変更を保存して取り消しを完了してください。
最新版 編集中の文章
1行目: 1行目:
--[[
+
----------------------------------------------------------------------
--------------------------------------------------------------------------------
+
--                                                                 --
--                                                                           --
+
--                       NAMESPACE DETECT                         --
--                           NAMESPACE DETECT                               --
+
--                                                                 --
--                                                                           --
+
--     This module implements the {{namespace detect}} template    --
-- This module implements the {{namespace detect}} template in Lua, with a   --
+
--     in Lua, with a few improvements: all namespaces and all     --
-- few improvements: all namespaces and all namespace aliases are supported, --
+
--      namespace aliases are supported, and namespace names are    --
-- and namespace names are detected automatically for the local wiki. The    --
+
--     detected automatically for the local wiki. Function names  --
-- module can also use the corresponding subject namespace value if it is    --
+
--     can be configured for different wikis by altering the      --
-- used on a talk page. Parameter names can be configured for different wikis --
+
--     values in the "cfg" table.                                  --
-- by altering the values in the "cfg" table in                              --
+
--                                                                 --
-- Module:Namespace detect/config.                                            --
+
----------------------------------------------------------------------
--                                                                           --
+
 
--------------------------------------------------------------------------------
+
----------------------------------------------------------------------
--]]
+
--                      Configuration data                          --
+
--     Language-specific parameter names can be set here.          --
local data = mw.loadData('Module:Namespace detect/data')
+
----------------------------------------------------------------------
local argKeys = data.argKeys
+
 
local cfg = data.cfg
+
local cfg = {}
local mappings = data.mappings
+
 
+
-- The name for the parameter to display content for the main namespace:
local yesno = require('Module:Yesno')
+
cfg.main = 'main'
local mArguments -- Lazily initialise Module:Arguments
+
 
local mTableTools -- Lazily initilalise Module:TableTools
+
-- The name for the parameter to display content in talk namespaces:
local ustringLower = mw.ustring.lower
+
cfg.talk = 'talk'
+
 
 +
-- The name for the parameter to display content for "other" namespaces
 +
-- (namespaces for which parameters have not been specified, or for when
 +
-- cfg.demospace is set to cfg.other):
 +
cfg.other = 'other'
 +
 
 +
-- The name for the parameter to set a demonstration namespace:
 +
cfg.demospace = 'demospace'
 +
 
 +
-- The name for the parameter to set a specific page to compare:
 +
cfg.page = 'page'
 +
 
 +
-- The header for the namespace column in the wikitable containing the
 +
-- list of possible subject-space parameters.
 +
cfg.wikitableNamespaceHeader = 'Namespace'
 +
 
 +
-- The header for the wikitable containing the list of possible
 +
-- subject-space parameters.
 +
cfg.wikitableAliasesHeader = 'Aliases'
 +
 
 +
----------------------------------------------------------------------
 +
--                    End configuration data                       --
 +
----------------------------------------------------------------------
 +
 
 +
----------------------------------------------------------------------
 +
--                        Global functions                          --
 +
--      The following functions are global, because we want them    --
 +
--      to be accessible from other Lua modules called using        --
 +
--      require().                                                  --
 +
----------------------------------------------------------------------
 +
 
 +
-- Declare the table of functions to return.
 
local p = {}
 
local p = {}
+
 
local function fetchValue(t1, t2)
+
-- Get the page object. This will return the page object for the page
-- Fetches a value from the table t1 for the first key in array t2 where
+
-- specified, or nil if there are errors in the title or if the
-- a non-nil value of t1 exists.
+
-- expensive function count has been exceeded.
for i, key in ipairs(t2) do
+
function p.getPageObject( page )
local value = t1[key]
+
    -- Get the title object for args.page if it is specified. Otherwise
if value ~= nil then
+
    -- get the title object for the current page.
return value
+
    if page then
end
+
        -- Get the page object, passing the function through pcall
end
+
        -- in case we are over the expensive function count limit.
return nil
+
        local noError, pageObject = pcall(mw.title.new, page)
 +
        if not noError then
 +
            return nil
 +
        else
 +
            return pageObject
 +
        end
 +
    else
 +
        return mw.title.getCurrentTitle()
 +
    end   
 
end
 
end
+
 
local function equalsArrayValue(t, value)
+
--[[ Returns a table of how parameter names map to namespace names.
-- Returns true if value equals a value in the array t. Otherwise
+
The keys are the actual namespace names, in lower case, and the  
-- returns false.
+
values are the possible parameter names for that namespace, also
for i, arrayValue in ipairs(t) do
+
in lower case. The table entries are structured like this:
if value == arrayValue then
+
    [''] = {
return true
+
        {'main'},
end
+
    },
end
+
    ['wikipedia'] = {
return false
+
        {'wikipedia', 'project', 'wp' }
end
+
    }
+
]]
function p.getPageObject(page)
+
function p.getParamMappings()
-- Get the page object, passing the function through pcall in case of
+
    local mappings = {}
-- errors, e.g. being over the expensive function count limit.
+
    mappings[mw.ustring.lower( mw.site.namespaces[0].name )] = { cfg.main }
if page then
+
    mappings[cfg.talk] = { cfg.talk }
local success, pageObject = pcall(mw.title.new, page)
+
    for nsid, ns in pairs( mw.site.subjectNamespaces ) do
if success then
+
        if nsid ~= 0 then -- Exclude main namespace.
return pageObject
+
            local nsname = mw.ustring.lower( ns.name )
else
+
            local canonicalName = mw.ustring.lower( ns.canonicalName )
return nil
+
            mappings[nsname] = { nsname }
end
+
            if canonicalName ~= nsname then
else
+
                table.insert( mappings[nsname], canonicalName )
return mw.title.getCurrentTitle()
+
            end
end
+
            for _, alias in ipairs( ns.aliases ) do
 +
                table.insert( mappings[nsname], mw.ustring.lower( alias ) )
 +
            end
 +
        end
 +
    end
 +
    return mappings
 
end
 
end
+
 
-- Provided for backward compatibility with other modules
+
--[[ Create a wikitable of all subject namespace parameters, for documentation
function p.getParamMappings()
+
  purposes. Talk is excluded, as it should usually be treated separately in
return mappings
+
  the documentation.
 +
]]
 +
function p.table()
 +
    -- Get the parameter mappings.
 +
    local mappings = p.getParamMappings()
 +
   
 +
    -- Start the wikitable.
 +
    local ret = '{| class="wikitable"'
 +
        .. '\n|-'
 +
        .. '\n! ' .. cfg.wikitableNamespaceHeader
 +
        .. '\n! ' .. cfg.wikitableAliasesHeader
 +
   
 +
    -- Generate the row for the main namespace, as we want this
 +
    -- to be first in the list.
 +
    ret = ret .. '\n|-'
 +
        .. '\n| <code>' .. cfg.main .. '</code>'
 +
        .. '\n|'
 +
       
 +
    -- Enclose all parameter names in <code> tags.
 +
    for ns, params in pairs( mappings ) do
 +
        if ns ~= mw.site.namespaces[0].name then
 +
            for i, param in ipairs( params ) do
 +
                mappings[ns][i] = '<code>' .. param .. '</code>'
 +
            end
 +
        end
 +
    end
 +
   
 +
    -- Generate the other wikitable rows.
 +
    for ns, params in pairs( mappings ) do
 +
        if ns ~= mw.site.namespaces[0].name then -- Ignore the main namespace.
 +
            ret = ret .. '\n|-'
 +
                .. '\n| ' .. params[1]
 +
                .. '\n| ' .. table.concat( params, ', ', 2 )
 +
        end
 +
    end
 +
   
 +
    -- End the wikitable.
 +
    ret = ret .. '\n|-'
 +
        .. '\n|}'
 +
   
 +
    return ret
 
end
 
end
   
+
 
local function getNamespace(args)
+
----------------------------------------------------------------------
-- This function gets the namespace name from the page object.
+
--                        Local functions                          --
local page = fetchValue(args, argKeys.demopage)
+
--      The following are internal functions, which we do not want --
if page == '' then
+
--      to be accessible from other modules.                        --
page = nil
+
----------------------------------------------------------------------
end
+
 
local demospace = fetchValue(args, argKeys.demospace)
+
-- Gets the namespace name to compare to the arguments. The returned value
if demospace == '' then
+
-- is lower-case.
demospace = nil
+
local function getNamespace( page, demospace )
end
+
    local ret
local subjectns = fetchValue(args, argKeys.subjectns)
+
    if demospace then
local ret
+
        -- Handle "demospace = main" properly.
if demospace then
+
        if mw.ustring.lower( demospace ) == cfg.main then
-- Handle "demospace = main" properly.
+
            ret = mw.site.namespaces[0].name
if equalsArrayValue(argKeys.main, ustringLower(demospace)) then
+
        else
ret = mw.site.namespaces[0].name
+
            ret = demospace
else
+
        end
ret = demospace
+
    else
end
+
        local pageObject = p.getPageObject( page )
else
+
        if pageObject then
local pageObject = p.getPageObject(page)
+
            if pageObject.isTalkPage then
if pageObject then
+
                -- {{namespace detect}} uses the same value for all talk
if pageObject.isTalkPage then
+
                -- namespaces, so that's what the module should do too.
-- Get the subject namespace if the option is set,
+
                ret = cfg.talk
-- otherwise use "talk".
+
            else
if yesno(subjectns) then
+
                ret = pageObject.nsText
ret = mw.site.namespaces[pageObject.namespace].subject.name
+
            end
else
+
        else
ret = 'talk'
+
            return nil -- return nil if the page object doesn't exist.
end
+
        end
else
+
    end
ret = pageObject.nsText
+
    return mw.ustring.lower(ret)
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)
+
-- Compare the namespace found with the parameters that have been
-- Check the parameters stored in the mappings table for any matches.
+
-- specified, and return content of the appropriate parameter.
local namespace = getNamespace(args) or 'other' -- "other" avoids nil table keys
+
local function compare( args )
local params = mappings[namespace] or {}
+
    -- Get the namespace to compare the parameters to, and the parameter
local ret = fetchValue(args, params)
+
    -- mapping table.
--[[
+
    local namespace = getNamespace( args[cfg.page], args[cfg.demospace] )
-- If there were no matches, return parameters for other namespaces.
+
    local mappings = p.getParamMappings()
-- This happens if there was no text specified for the namespace that
+
   
-- was detected or if the demospace parameter is not a valid
+
    -- Check for any matches in the namespace arguments. The order we check
-- namespace. Note that the parameter for the detected namespace must be
+
    -- them doesn't matter, as there can only be one match.
-- completely absent for this to happen, not merely blank.
+
    for ns, params in pairs( mappings ) do
--]]
+
        if ns == namespace then
if ret == nil then
+
            -- Check all aliases for matches. The default local namespace is
ret = fetchValue(args, argKeys.other)
+
            -- checked first, as {{namespace detect}} checked these before
end
+
            -- alias names.
return ret
+
            for _, param in ipairs( params ) do
 +
                if args[param] then
 +
                    return args[param]
 +
                end
 +
            end
 +
        end
 +
    end
 +
   
 +
    -- 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 args[cfg.other] then
 +
        return args[cfg.other]
 +
    end
 
end
 
end
+
 
 +
----------------------------------------------------------------------
 +
--                            Main function                        --
 +
--      This is the function that will be most used. It processes  --
 +
--      the arguments and calls the compare() function. It is      --
 +
--      global, but is put down here as it depends on the other    --
 +
--      local in order for it to work.                              --
 +
----------------------------------------------------------------------
 +
 
 
function p.main(frame)
 
function p.main(frame)
mArguments = require('Module:Arguments')
+
    -- If called via #invoke, use the args passed into the invoking
local args = mArguments.getArgs(frame, {removeBlanks = false})
+
    -- template, or the args passed to #invoke if any exist. Otherwise
local ret = p._main(args)
+
    -- assume args are being passed directly in.
return ret or ''
+
    local origArgs
end
+
    if frame == mw.getCurrentFrame() then
+
        origArgs = frame:getParent().args
function p.table(frame)
+
        for k, v in pairs( frame.args ) do
--[[
+
            origArgs = frame.args
-- Create a wikitable of all subject namespace parameters, for
+
            break
-- documentation purposes. The talk parameter is optional, in case it
+
        end
-- needs to be excluded in the documentation.
+
    else
--]]
+
        origArgs = frame
+
    end
-- Load modules and initialise variables.
+
   
mTableTools = require('Module:TableTools')
+
    -- Trim whitespace and remove blank arguments for demospace and
local namespaces = mw.site.namespaces
+
    -- page parameters.
local cfg = data.cfg
+
    local args = {}
local useTalk = type(frame) == 'table'
+
    for k, v in pairs(origArgs) do
and type(frame.args) == 'table'
+
        v = mw.text.trim(v) -- Trim whitespace.
and yesno(frame.args.talk) -- Whether to use the talk parameter.
+
        if k == cfg.demospace or k == cfg.page then
+
            if v ~= '' then
-- Get the header names.
+
                args[k] = v
local function checkValue(value, default)
+
            end
if type(value) == 'string' then
+
        else
return value
+
            args[k] = v
else
+
        end
return default
+
    end
end
+
   
end
+
    return compare(args)
local nsHeader = checkValue(cfg.wikitableNamespaceHeader, 'Namespace')
 
local aliasesHeader = checkValue(cfg.wikitableAliasesHeader, 'Aliases')
 
 
-- Put the namespaces in order.
 
local mappingsOrdered = {}
 
for nsname, params in pairs(mappings) do
 
if useTalk or nsname ~= 'talk' then
 
local nsid = namespaces[nsname].id
 
-- Add 1, as the array must start with 1; nsid 0 would be lost otherwise.
 
nsid = nsid + 1
 
mappingsOrdered[nsid] = params
 
end
 
end
 
mappingsOrdered = mTableTools.compressSparseArray(mappingsOrdered)
 
 
-- Build the table.
 
local ret = '{| class="wikitable"'
 
.. '\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 p
 
return p

ひつじ小屋別館2代目への投稿はすべて、他の投稿者によって編集、変更、除去される場合があります。 自分が書いたものが他の人に容赦なく編集されるのを望まない場合は、ここに投稿しないでください。
また、投稿するのは、自分で書いたものか、パブリック ドメインまたはそれに類するフリーな資料からの複製であることを約束してください(詳細はひつじ小屋別館2代目:著作権を参照)。 著作権保護されている作品は、許諾なしに投稿しないでください!

このページを編集するには、下記の確認用の質問に回答してください (詳細):

取り消し | 編集の仕方 (新しいウィンドウで開きます)