模組:Disambiguation
外观
| 此模块已评为通行版,稳定可靠,可各处使用无误。已可在帮助页面和其他维基百科资源中提及,以帮助新用户学习。为降低服务器负载和错误输出,改善本模块前应进行沙盒测试,而不是重复的试错性编辑。 |
| 此模块使用Lua语言: |
本模块用于检测指定的页面是否为消歧义页面。
用法
[编辑]{{#invoke:Disambiguation|isDisambiguationPage|页面标题}}
- 如果目标页面为消歧义页面将返回
yes,否则将不返回任何内容。
示例:
{{#invoke:Disambiguation|isDisambiguationPage|北京市}}→{{#invoke:Disambiguation|isDisambiguationPage|张伟}}→ yes{{#invoke:Disambiguation|isDisambiguationPage|歌手 (消歧義)}}→ yes
请注意,标题繁简必须与原始标题一致。
您也可以使用魔术字,例如{{SUBJECTPAGENAME}}:
{{#invoke:Disambiguation|isDisambiguationPage|{{SUBJECTPAGENAME}}}}→ yes
在Lua模块中使用
[编辑]导入本模块,例如使用
local mDisambiguation = require('Module:Disambiguation')
然后,您就可以使用函数isDisambiguation和_isDisambiguationPage。
如果您已经有要检查的页面的标题物件,请使用标题物件的getContent()方法获取内容,并将其传递给isDisambiguation:
local isDab = mDisambiguation.isDisambiguation(content) -- returns true or false
- (其中
content是一个字符串,即要检查的页面的Wikitext内容)
如果您不需要标题,可以将页面名称传递给_isDisambiguationPage:
local isDab = mDisambiguation._isDisambiguationPage(pageName) -- returns true or false
- (其中
pageName是一个字符串,即要检查的页面名称)
内部运作
[编辑]- 尽管同类索引条目被某些模板视为消歧义页面,但它们实际上是一种特殊类型的列表,并且不被本模块视为消歧义页面。
- 由于本模块依赖于检测条目文本中具有“消歧义”等名称的模板,所以会受到{{需要消歧义}}等模板的误报,这些模板应当添加到代码中的falsePositives列表中以便排除它们。
- 消歧义模板列表在Module:Disambiguation/templates中维护。
local p = {}
local mRedirect = require('Module:Redirect')
local ZhConversion = require('Module:ZhConversion')
local disambiguationTemplates = mw.loadData('Module:Disambiguation/templates')
local PrepareText = require('Module:Wikitext Parsing').PrepareText
local function capitalize(s)
return mw.ustring.upper(mw.ustring.sub(s, 1, 1)) .. mw.ustring.sub(s, 2, -1)
end
-- 中文版追加:二次处理模板标题繁简
local function zh_title(str)
local input_str = capitalize(str)
local output_str = ZhConversion.zh_title('Template:' .. input_str)
return mw.ustring.sub(output_str, 10, -1)
end
local function isDisambiguationTemplate(template)
return disambiguationTemplates[zh_title(template)] or false
end
p.isDisambiguation = function(content)
-- false if there is no content
if content == nil then
return false
end
-- redirects are not disambiguation pages
if mRedirect.getTargetFromText(content, "wikitext") ~= nil then
return false
end
-- check for disambiguation templates in the content
local templateNames = {}
-- remove nowiki content and html comments for this check
local activecontent = PrepareText(content)
for template in mw.ustring.gmatch(activecontent, "{{%s*([^|}]-)%s*[|}]") do
if isDisambiguationTemplate(template) then
return true
end
end
-- check for magic word
if string.find(content, "__DISAMBIG__", 1, true) ~= nil then
return true
elseif mw.ustring.find(content, "__消除歧义__", 1, true) ~= nil then
return true
end
return false
end
p._isDisambiguationPage = function(page)
-- Look "(disambiguation)" in the title
if mw.ustring.find(page, '(消歧义)',0,true) ~= nil then
return true;
elseif mw.ustring.find(page, '(消歧義)',0,true) ~= nil then
return true;
end
-- Look for disamiguation template in page content
local title = mw.title.new(page)
if not title then return false end
local content = title:getContent()
return p.isDisambiguation(content)
end
-- Entry points for templates
p.isDisambiguationPage = function(frame)
local title = frame.args[1]
return p._isDisambiguationPage(title) and "yes" or ""
end
return p