Jump to content

Module:Disambiguation: Difference between revisions

From Humanipedia
hp>Wbm1058
only populate Category:Unsynchronized disambiguation talk pages with pages in mainspace
 
hp>R'n'B
ignore templates appearing in comments or nowiki elements
Line 1: Line 1:
<includeonly>{{#if:{{{nocat|}}}||{{Disambiguation page short description}}}}</includeonly>
local p = {}
{{Dmbox
local mRedirect = require('Module:Redirect')
| type = disambig
local disambiguationTemplates = mw.loadData('Module:Disambiguation/templates')
| text = {{#switch: {{{nsoverride|<includeonly>{{NAMESPACE}}</includeonly>}}}|<!--detects namespace between Wikipedia, MOS, Portal, and everything else-->
 
| #default = This [[Help:Disambiguation|disambiguation]] page lists {{{type|{{#if:{{{ignore_parentheses|}}}||{{Title disambig text|1={{Str rep|{{{page-title|{{FULLPAGENAME}}}}}|(disambiguation)|}}}}}}}}} {{namespace detect|main=articles|category=categories|wikipedia=project pages<includeonly>|template=templates</includeonly>|other=pages}} associated with the title '''{{#if:{{{ignore_parentheses|}}}|{{{page-title|{{FULLPAGENAME}}}}}|{{PAGENAMEBASE|{{{page-title|{{FULLPAGENAME}}}}}}}}}'''.<!--for normal disambiguation pages-->
local function capitalize(s)
| Wikipedia  = This page is a list of [[Wikipedia:Project namespace|project pages]] associated with the same title or shortcut.[[Category:Wikipedia disambiguation pages]]<!--for Wikipedia disambiguation pages-->
-- This function only works on ASCII strings. If your wiki has
| MOS = This page is a list of [[Wikipedia:Manual of Style|Wikipedia Manual of Style pages or sections]] associated with the same title or shortcut.[[Category:Manual of Style disambiguation pages]]<!--for Manual of Style disambiguation pages-->
-- disambiguation templates that use Unicode strings, use the commented-out
| Portal = This page is a list of [[Wikipedia:Contents/Portals|portals]] associated with the same title.[[Category:Portal disambiguation pages]]<!--for Portal disambiguation pages-->
-- line instead. Enwiki uses ASCII string manipulation only here to improve
}}<br /><small>{{main other<!--detects if in mainspace or not-->
-- performance.
| demospace = {{{demospace|}}}<!--for testing, see docs at [[Template:Main other]]-->
return s:sub(1, 1):upper() .. s:sub(2, -1)
| If an [{{fullurl:Special:WhatLinksHere/{{{page-title|{{FULLPAGENAME}}}}}|namespace=0}} internal link] led you here, you may wish to change the link to point directly to the intended article.<!--If in mainspace, the "internal link" only links to incoming links in mainspace, and we use the term "intended article"-->
-- return mw.ustring.upper(mw.ustring.sub(1, 1)) .. mw.ustring.sub(2, -1)
| If an [{{fullurl:Special:WhatLinksHere/{{{page-title|{{FULLPAGENAME}}}}}}} internal link] led you here, you may wish to change the link to point directly to the intended page.<!--If not in mainspace, the "internal link" links to incoming links in all namespaces, and we use the term "intended page"-->
end
}}</small>
 
| nocat = {{{nocat|}}}  <!--So "nocat=true" works-->
local function isDisambiguationTemplate(template)
}}{{main other
return disambiguationTemplates[capitalize(template)] or false
| demospace = {{{demospace|}}}
end
| [[Category:Disambiguation pages]]{{disambiguation/cat|{{{1|}}}}}{{disambiguation/cat|{{{2|}}}}}{{disambiguation/cat|{{{3|}}}}}{{disambiguation/cat|{{{4|}}}}}{{disambiguation/cat|{{{5|}}}}}{{disambiguation/cat|{{{6|}}}}}{{disambiguation/cat|{{{7|}}}}}{{disambiguation/cat|{{{8|}}}}}{{disambiguation/cat|{{{9|}}}}}{{disambiguation/cat|{{{10|}}}}}{{#if: {{{hn|}}} | [[Category:Human name disambiguation pages|{{{hn}}}]] }}{{#if:{{Title disambig text|1={{Str rep|{{FULLPAGENAME}}|(disambiguation)|}}}}|[[Category:Disambiguation pages with (qualified) titles]]|}}<!-- The preceding test should include articles like "Socialist Party (Sweden) (disambiguation)". -->
 
| <!-- Don't categorise when not in main (article) space. However, note that {{Dmbox}} categorises pages into Category:All disambiguation pages and Category:All article disambiguation pages-->
p.isDisambiguation = function(content)
}}<includeonly>{{main other|{{#if:{{{nocat|}}}||{{#ifeq:{{#invoke:redirect|isRedirect|{{TALKPAGENAME}}}}|yes|[[Category:Unsynchronized disambiguation talk pages]]}}}}}}</includeonly><noinclude>
-- false if there is no content
{{Documentation}}
if content == nil then
<!-- Add categories to the /doc subpage and interwikis to Wikidata, not here! -->
return false
</noinclude>
end
 
-- redirects are not disambiguation pages
if mRedirect.getTargetFromText(content) ~= nil then
return false
end
 
-- check for disambiguation templates in the content
local templateNames = {}
local activecontent = string.gsub(content, "<!%-%-.-%-%->", "")
activecontent = string.gsub(activecontent, "<nowiki>.-</nowiki>", "")
for template in string.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
end
 
return false
end
 
p._isDisambiguationPage = function(page)
-- Look "(disambiguation)" in the title
if string.find(page, "(disambiguation)",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

Revision as of 14:30, 18 January 2025

Lua error in package.lua at line 80: module 'Module:Yesno' not found. Script error: No such module "High-use". Lua error in package.lua at line 80: module 'Module:Yesno' not found. This module detects if a given page is a disambiguation page.

Usage

{{#invoke:Disambiguation|isDisambiguationPage|Page title}}
returns yes if the page is a disambiguation page, or nothing if the page is not a disambiguation page

Examples:

  • {{#invoke:Disambiguation|isDisambiguationPage|Paris}}Script error: No such module "Disambiguation".
  • {{#invoke:Disambiguation|isDisambiguationPage|New}}Script error: No such module "Disambiguation".
  • {{#invoke:Disambiguation|isDisambiguationPage|Black swan (disambiguation)}}Script error: No such module "Disambiguation".

You can also use magic words like {{SUBJECTPAGENAME}}:

  • {{#invoke:Disambiguation|isDisambiguationPage|{{SUBJECTPAGENAME}}}}Script error: No such module "Disambiguation".

Usage within Lua modules

Import this module, e.g with <syntaxhighlight lang="lua"> local mDisambiguation = require('Module:Disambiguation') </syntaxhighlight> Then you can use the functions isDisambiguation and _isDisambiguationPage.

If you have already have a Title object for the page to check, get the content using the title object's getContent() method, and pass that into isDisambiguation: <syntaxhighlight lang="lua"> local isDab = mDisambiguation.isDisambiguation(content) -- returns true or false </syntaxhighlight>

(where content is a string, the wikitext content of page to check)

If you don't otherwise need the title, you can pass in the page name to _isDisambiguationPage: <syntaxhighlight lang="lua"> local isDab = mDisambiguation._isDisambiguationPage(pageName) -- returns true or false </syntaxhighlight>

(where pageName is a string, the name of page to check)

Internal operations

  • Although set index articles are treated by some templates as disambiguation pages, they are actually considered a special type of list and are not treated as disambiguation pages by this module
  • As this module relies on detecting templates with names like "disambiguation" in the article text, it is subject to false positives by templates such as {{italic disambiguation}}. These templates should be added to the falsePositives list in the code to exclude them.
  • The list of disambiguation templates is maintained at Module:Disambiguation/templates.

local p = {}
local mRedirect = require('Module:Redirect')
local disambiguationTemplates = mw.loadData('Module:Disambiguation/templates')

local function capitalize(s)
	-- This function only works on ASCII strings. If your wiki has
	-- disambiguation templates that use Unicode strings, use the commented-out
	-- line instead. Enwiki uses ASCII string manipulation only here to improve
	-- performance.
	return s:sub(1, 1):upper() .. s:sub(2, -1)
	-- return mw.ustring.upper(mw.ustring.sub(1, 1)) .. mw.ustring.sub(2, -1)
end

local function isDisambiguationTemplate(template)
	return disambiguationTemplates[capitalize(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) ~= nil then
		return false
	end

	-- check for disambiguation templates in the content
	local templateNames = {}
	local activecontent = string.gsub(content, "<!%-%-.-%-%->", "")
	activecontent = string.gsub(activecontent, "<nowiki>.-</nowiki>", "")
	for template in string.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
	end

	return false
end

p._isDisambiguationPage = function(page)
	-- Look "(disambiguation)" in the title
	if string.find(page, "(disambiguation)",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