模組:Pagetype
![]() | 此模組被引用於約722,000個頁面。 為了避免造成大規模的影響,所有對此模組的編輯應先於沙盒或測試樣例上測試。 測試後無誤的版本可以一次性地加入此模組中,但是修改前請務必於討論頁發起討論。 模板引用數量會自動更新。 |
![]() | 此模組已被保護。此為高度可見模組,其已用於大量條目或被頻繁替換引用。由於破壞或失誤會影響諸多頁面,即便細小的改動也可能導致大量伺服器負載,因此已被保護,不可編輯。 |
本模組用於檢測指定頁面之類型,其能自動檢測重新導向頁面,且可高度自訂。本模組可獨立使用,亦可作為元模組用於建立其他頁面類型檢測模板。使用{{pagetype}}亦可訪問本模組。
語法
- 基本代碼
{{#invoke:pagetype|main}}
- 完整代碼
{{#invoke:pagetype|main | page = | defaultns = | main = | user = | wikipedia = | project = | wp = | file = | image = | mediawiki = | template = | help = | category = | portal = | book = | draft = | education program = | timedtext = | module = | topic = | special = | media = | talk = | redirect = | dab = | na = | other = }}
基本用法
如需對當前頁面類型作簡單檢測,使用預設設置:
{{#invoke:pagetype|main}}
如此即可根據頁面類型或命名空間生成下列結果:
頁面類型 / 命名空間 | 輸出 |
---|---|
重新導向(任意命名空間) | 重新導向 |
主命名空間 | 條目 |
檔案名字空間 | 檔案 |
模板命名空間 | 模板 |
分類命名空間 | 分類 |
模組命名空間 | 模組 |
圖書命名空間 | 圖書 |
其他命名空間 | 頁面 |
(註:部分命名空間尚未在中文維基百科啟用。)
若本模組用於討論頁,則會檢測對應主頁面類型。
- 範例
代碼 | 結果 |
---|---|
{{#invoke:pagetype|main}} |
模組 |
頁面參數
您可使用page
參數模擬模組呼叫該頁面。
{{#invoke:pagetype|main|page=page name}}
- 範例
代碼 | 結果 |
---|---|
{{#invoke:pagetype|main|page=英国}} |
條目 |
{{#invoke:pagetype|main|page=UK}} |
重新導向 |
{{#invoke:pagetype|main|page=Talk:英国}} |
條目 |
{{#invoke:pagetype|main|page=File:Example.svg}} |
檔案 |
{{#invoke:pagetype|main|page=Wikipedia:Namespace}} |
重新導向 |
{{#invoke:pagetype|main|page=TimedText:Imagine.ogg.en.srt}} |
頁面 |
增減命名空間
可為各命名空間指定與基本用法一節不同的行為。例如,您可能希望模組在用戶命名空間顯示「用戶頁」而非預設的「頁面」,為此您可以使用代碼|user=yes
。這樣便可為用戶命名空間頁面尋找典型名稱,即本例中的「用戶頁」,並在模組使用在用戶命名空間的頁面時使用該文字。用於其他命名空間時,請使用小寫字母的命名空間名作為參數。
{{#invoke:pagetype|main | user = yes | wikipedia = yes | project = yes | wikiproject = yes | wp = yes | mediawiki = yes | help = yes | portal = yes | draft = yes | education program = yes | timedtext = yes | topic = yes | special = yes | media = yes }}
您可使用下列代碼中的任一形式指定命名空間:"yes
"、"y
"、"true
"或"1
",任意大小寫字母組合形式均可使用(這些值由模組:Yesno定義)。
此類命名空間的預設文字如下所示:
頁面類型 / 命名空間 | 輸出 |
---|---|
User | 用戶頁 |
Wikipedia | 維基百科 |
MediaWiki | MediaWiki |
Help | 幫助 |
Portal | 主題 |
WikiProject | 維基專題 |
Draft | 草稿 |
Education | 教育課程 |
Timed Text | 字幕 |
Topic | 話題 |
Special | 特殊頁面 |
Media | 檔案 |
(註:部分命名空間尚未在中文維基百科啟用。)
您亦可使用相似的方式移除預設任意命名空間值。例如,若使用代碼|template=no
,則模組使用於模板頁時會顯示「頁面」,而非預設的「模板」。
{{#invoke:pagetype|main | main = no | file = no | template = no | category = no | module = no | book = no }}
下列值均可用於移除命名空間值:"no
"、"n
"、"false
"或"0
",任意大小寫字母組合形式均可使用。
- 範例
代碼 | 結果 |
---|---|
{{#invoke:pagetype|main|page=Wikipedia:Namespace}} |
重新導向 |
{{#invoke:pagetype|main|page=Wikipedia:Namespace|wikipedia=yes}} |
重新導向 |
{{#invoke:pagetype|main|page=Talk:英国}} |
條目 |
{{#invoke:pagetype|main|page=Talk:英国|talk=y}} |
討論頁 |
{{#invoke:pagetype|main|page=英国}} |
條目 |
{{#invoke:pagetype|main|page=英国|main=no}} |
頁面 |
{{#invoke:pagetype|main|page=TimedText:Imagine.ogg.en.srt}} |
頁面 |
{{#invoke:pagetype|main|page=TimedText:Imagine.ogg.en.srt|timedtext=Yes}} |
字幕 |
默認命名空間設置
除了添加或移除某個命名空間,亦可使用|defaultns=
指定不同的命名空間集。共有四組集可供使用:all
、extended
、none
和預設集。這些集允許您一次性指定多個命名空間,而無須一一使用所有命名空間參數。命名空間可按下表設置:
命名空間 | |defaultns= 值
|
輸出 | |||
---|---|---|---|---|---|
none
|
extended
|
all
|
其他值 | ||
主命名空間 | ![]() |
![]() |
![]() |
![]() |
條目 |
User | ![]() |
![]() |
![]() |
![]() |
用戶頁 |
Wikipedia | ![]() |
![]() |
![]() |
![]() |
維基百科 |
File | ![]() |
![]() |
![]() |
![]() |
檔案 |
MediaWiki | ![]() |
![]() |
![]() |
![]() |
MediaWiki |
Template | ![]() |
![]() |
![]() |
![]() |
模板 |
Help | ![]() |
![]() |
![]() |
![]() |
幫助 |
Category | ![]() |
![]() |
![]() |
![]() |
分類 |
Portal | ![]() |
![]() |
![]() |
![]() |
主題 |
WikiProject | ![]() |
![]() |
![]() |
![]() |
維基專題 |
Book | ![]() |
![]() |
![]() |
![]() |
圖書 |
Draft | ![]() |
![]() |
![]() |
![]() |
草稿 |
Education Program | ![]() |
![]() |
![]() |
![]() |
教育專案 |
TimedText | ![]() |
![]() |
![]() |
![]() |
字幕 |
模块 | ![]() |
![]() |
![]() |
![]() |
模組 |
Topic (Flow boards) | ![]() |
![]() |
![]() |
![]() |
話題 |
所有討論命名空間 | ![]() |
![]() |
![]() |
![]() |
討論頁 |
Special | ![]() |
![]() |
![]() |
![]() |
特殊頁面 |
Media | ![]() |
![]() |
![]() |
![]() |
檔案 |
(註:部分命名空間尚未在中文維基百科啟用。)
- 範例
代碼 | 結果 |
---|---|
{{#invoke:pagetype|main|page=英国}} |
條目 |
{{#invoke:pagetype|main|page=英国|defaultns=none}} |
頁面 |
{{#invoke:pagetype|main|page=Talk:英国}} |
條目 |
{{#invoke:pagetype|main|page=Talk:英国|defaultns=all}} |
討論頁 |
{{#invoke:pagetype|main|page=File:Example.svg}} |
檔案 |
{{#invoke:pagetype|main|page=File:Example.svg|defaultns=none}} |
頁面 |
{{#invoke:pagetype|main|page=Wikipedia:Namespace}} |
重新導向 |
{{#invoke:pagetype|main|page=Wikipedia:Namespace|defaultns=extended}} |
重新導向 |
{{#invoke:pagetype|main|page=TimedText:Imagine.ogg.en.srt|defaultns=extended}} |
頁面 |
{{#invoke:pagetype|main|page=TimedText:Imagine.ogg.en.srt|defaultns=all}} |
字幕 |
自訂命名空間值
可以為任意命名空間指定自訂值:
{{#invoke:pagetype|main | main = 文字 | user = 文字 | wikipedia = 文字 | project = 文字 | wp = 文字 | file = 文字 | image = 文字 | mediawiki = 文字 | template = 文字 | help = 文字 | category = 文字 | portal = 文字 | book = 文字 | draft = 文字 | education program = 文字 | timedtext = 文字 | module = 文字 | topic = 文字 | special = 文字 | media = 文字 }}
- 範例
代碼 | 結果 |
---|---|
{{#invoke:pagetype|main|page=Wikipedia:Namespace}} |
重新導向 |
{{#invoke:pagetype|main|page=Wikipedia:Namespace|wikipedia=文字}} |
重新導向 |
{{#invoke:pagetype|main|page=Talk:英国}} |
條目 |
{{#invoke:pagetype|main|page=Talk:英国|talk=文字}} |
文字 |
{{#invoke:pagetype|main|page=英国}} |
條目 |
{{#invoke:pagetype|main|page=英国|main=文字}} |
文字 |
{{#invoke:pagetype|main|page=TimedText:Imagine.ogg.en.srt}} |
頁面 |
{{#invoke:pagetype|main|page=TimedText:Imagine.ogg.en.srt|timedtext=文字}} |
文字 |
討論參數
一般而言,本模組會將討論頁視作對應主頁面。然而,如果設有talk
參數,模組則會把討論頁與主頁面區別對待。talk
參數會為所有討論頁設置值,即Talk:命名空間的頁面、User talk:命名空間的頁面、Wikipedia talk:命名空間的頁面等等。
{{#invoke:pagetype|main|talk=yes}}
{{#invoke:pagetype|main|talk=文字}}
"talk=yes
"的預設值為"討論頁"。
- 範例
代碼 | 結果 |
---|---|
{{#invoke:pagetype|main|page=Wikipedia talk:Namespace}} |
重新導向 |
{{#invoke:pagetype|main|page=Wikipedia talk:Namespace|wikipedia=yes}} |
重新導向 |
{{#invoke:pagetype|main|page=Wikipedia talk:Namespace|talk=yes}} |
討論頁 |
{{#invoke:pagetype|main|page=Wikipedia talk:Namespace|wikipedia=yes|talk=yes}} |
討論頁 |
{{#invoke:pagetype|main|page=Wikipedia talk:Namespace|talk=文字}} |
文字 |
{{#invoke:pagetype|main|page=Wikipedia talk:Namespace|wikipedia=yes|talk=文字}} |
文字 |
主命名空間評級
對於主命名空間的頁面,可在第一位置參數處添加一個評級值。
{{#invoke:pagetype|main|class}}
如果該評級值與下表的某一值相匹配,即可生成與預設"條目"不同的輸出。
評級 | 評級值 | 輸出 |
---|---|---|
消歧義級 | disambiguation 、disambig 、disamb 、dab |
頁面 |
不適用級 | na 、n/a |
頁面 |
任意大小寫字母組合形式均可使用。
自訂主命名空間評級
亦可為主命名空間評級指定自訂值。
{{#invoke:pagetype|main|class|dab=yes|na=yes}}
{{#invoke:pagetype|main|class|dab=文字|na=文字}}
與自訂命名空間頁面類型的方式相同,可能的參數有:
- dab:消歧義級頁面的自訂值,若設為"
yes
"、"y
"、"true
"或"1
",則生成"消歧義頁"。若設為"no
"、"n
"、"false
"或"0
",則不生成自訂值。若設為其他任何值,則生成該文字。 - na:不適用級頁面的自訂值,與
dab
參數的運作方式相同。預設值為"yes
",即"頁面"。
- 範例
代碼 | 結果 |
---|---|
{{#invoke:pagetype|main|page=英国}} |
條目 |
{{#invoke:pagetype|main|page=英国|dab}} |
頁面 |
{{#invoke:pagetype|main|page=英国|dab|dab=文字}} |
文字 |
{{#invoke:pagetype|main|page=英国|na}} |
頁面 |
{{#invoke:pagetype|main|page=英国|na|na=文字}} |
文字 |
重新導向
本模組預設檢測所有命名空間的重新導向。無法按命名空間指定重新導向行為,但您可以關閉重新導向檢測,或對重新導向頁面使用自訂值。
{{#invoke:pagetype|main|redirect=no}}
{{#invoke:pagetype|main|redirect=文字}}
- 範例
代碼 | 結果 |
---|---|
{{#invoke:pagetype|main|page=UK}} |
重新導向 |
{{#invoke:pagetype|main|page=UK|redirect=no}} |
條目 |
{{#invoke:pagetype|main|page=UK|redirect=文字}} |
文字 |
其他命名空間
若某命名空間未受指定,本模組預設生成"页面
",該值可透過|other=
參數變更。
{{#invoke:pagetype|main|other=文字}}
- 範例
代碼 | 結果 |
---|---|
{{#invoke:pagetype|main|page=Wikipedia:Namespace}} |
重新導向 |
{{#invoke:pagetype|main|page=Wikipedia:Namespace|other=文字}} |
重新導向 |
{{#invoke:pagetype|main|page=Wikipedia:Namespace|other=文字|wikipedia=维基文字}} |
重新導向 |
--------------------------------------------------------------------------------
-- --
-- PAGETYPE --
-- --
-- This is a meta-module intended to replace {{pagetype}} and similar --
-- templates. It automatically detects namespaces, and allows for a --
-- great deal of customisation. It can easily be ported to other --
-- wikis by changing the values in the [[Module:Pagetype/config]]. --
-- --
--------------------------------------------------------------------------------
-- Load config.
local cfg = mw.loadData('Module:Pagetype/config')
-- Load required modules.
local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local nsDetectModule = require('Module:Namespace detect')
local nsDetect = nsDetectModule._main
local getParamMappings = nsDetectModule.getParamMappings
local getPageObject = nsDetectModule.getPageObject
local p = {}
local function shallowCopy(t)
-- Makes a shallow copy of a table.
local ret = {}
for k, v in pairs(t) do
ret[k] = v
end
return ret
end
local function checkPagetypeInput(namespace, val)
-- Checks to see whether we need the default value for the given namespace,
-- and if so gets it from the pagetypes table.
-- The yesno function returns true/false for "yes", "no", etc., and returns
-- val for other input.
local ret = yesno(val, val)
if ret and type(ret) ~= 'string' then
ret = cfg.pagetypes[namespace]
end
return ret
end
local function getPagetypeFromClass(class, param, aliasTable, default)
-- Gets the pagetype from a class specified from the first positional
-- parameter.
param = yesno(param, param)
if param ~= false then -- No check if specifically disallowed.
for _, alias in ipairs(aliasTable) do
if class == alias then
if type(param) == 'string' then
return param
else
return default
end
end
end
end
end
local function getNsDetectValue(args)
-- Builds the arguments to pass to [[Module:Namespace detect]] and returns
-- the result.
-- Get the default values.
local ndArgs = {}
local defaultns = args[cfg.defaultns]
if defaultns == cfg.defaultnsAll then
ndArgs = shallowCopy(cfg.pagetypes)
else
local defaultnsArray
if defaultns == cfg.defaultnsExtended then
defaultnsArray = cfg.extendedNamespaces
elseif defaultns == cfg.defaultnsNone then
defaultnsArray = {}
else
defaultnsArray = cfg.defaultNamespaces
end
for _, namespace in ipairs(defaultnsArray) do
ndArgs[namespace] = cfg.pagetypes[namespace]
end
end
--[[
-- Add custom values passed in from the arguments. These overwrite the
-- defaults. The possible argument names are fetched from
-- Module:Namespace detect automatically in case new namespaces are
-- added. Although we accept namespace aliases as parameters, we only pass
-- the local namespace name as a parameter to Module:Namespace detect.
-- This means that the "image" parameter can overwrite defaults for the
-- File: namespace, which wouldn't work if we passed the parameters through
-- separately.
--]]
local mappings = getParamMappings()
for ns, paramAliases in pairs(mappings) do
-- Copy the aliases table, as # doesn't work with tables returned from
-- mw.loadData.
paramAliases = shallowCopy(paramAliases)
local paramName = paramAliases[1]
-- Iterate backwards along the array so that any values for the local
-- namespace names overwrite those for namespace aliases.
for i = #paramAliases, 1, -1 do
local paramAlias = paramAliases[i]
local ndArg = checkPagetypeInput(paramAlias, args[paramAlias])
if ndArg == false then
-- If any arguments are false, convert them to nil to protect
-- against breakage by future changes to
-- [[Module:Namespace detect]].
ndArgs[paramName] = nil
elseif ndArg then
ndArgs[paramName] = ndArg
end
end
end
-- Check for disambiguation-class and N/A-class pages in mainspace.
if ndArgs.main then
local class = args[1]
if type(class) == 'string' then
-- Put in lower case so e.g. "Dab" and "dab" will both match.
class = mw.ustring.lower(class)
end
local dab = getPagetypeFromClass(
class,
args[cfg.dab],
cfg.dabAliases,
cfg.dabDefault
)
if dab then
ndArgs.main = dab
else
local na = getPagetypeFromClass(
class,
args[cfg.na],
cfg.naAliases,
cfg.naDefault
)
if na then
ndArgs.main = na
end
end
end
-- If there is no talk value specified, use the corresponding subject
-- namespace for talk pages.
if not ndArgs.talk then
ndArgs.subjectns = true
end
-- Add the fallback value. This can also be customised, but it cannot be
-- disabled.
local other = args[cfg.other]
-- We will ignore true/false/nil results from yesno here, but using it
-- anyway for consistency.
other = yesno(other, other)
if type(other) == 'string' then
ndArgs.other = other
else
ndArgs.other = cfg.otherDefault
end
-- Allow custom page values.
ndArgs.page = args.page
return nsDetect(ndArgs)
end
local function detectRedirects(args)
local redirect = args[cfg.redirect]
-- The yesno function returns true/false for "yes", "no", etc., and returns
-- redirect for other input.
redirect = yesno(redirect, redirect)
if redirect == false then
-- Detect redirects unless they have been explicitly disallowed with
-- "redirect=no" or similar.
return
end
local pageObject = getPageObject(args.page)
-- If we are using subject namespaces elsewhere, do so here as well.
if pageObject
and not yesno(args.talk, true)
and args[cfg.defaultns] ~= cfg.defaultnsAll
then
pageObject = getPageObject(
pageObject.subjectNsText .. ':' .. pageObject.text
)
end
-- Allow custom values for redirects.
if pageObject and pageObject.isRedirect then
if type(redirect) == 'string' then
return redirect
else
return cfg.redirectDefault
end
end
end
function p._main(args)
local redirect = detectRedirects(args)
if redirect then
return redirect
else
return getNsDetectValue(args)
end
end
function p.main(frame)
local args = getArgs(frame)
return p._main(args)
end
return p