Module:Disambiguation

From MOASSpedia
Revision as of 08:01, 25 October 2021 by Wikipedia>MSGJ (add more forms to list)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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}}
  • {{#invoke:Disambiguation|isDisambiguationPage|New}}
  • {{#invoke:Disambiguation|isDisambiguationPage|Black swan (disambiguation)}} → yes

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

  • {{#invoke:Disambiguation|isDisambiguationPage|{{SUBJECTPAGENAME}}}} → yes

Usage within Lua modules

Import this module, e.g with

local mDisambiguation = require('Module:Disambiguation')

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:

local isDab = mDisambiguation.isDisambiguation(content) -- returns true or false
(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:

local isDab = mDisambiguation._isDisambiguationPage(pageName) -- returns true or false
(where pageName is a string, the name of page to check)

local p = {}
local mRedirect = require('Module:Redirect')

local disambigTemplates = {
	"[Dd][Aa][Bb]",
	"[Dd]big",
	"[Dd]is",
	"[Dd]isambiguation",
	"[%w_%s]-%f[%w][Dd]isam[%w]-",
	"[Gg]eodis",
	"[Hh][Nn][Dd][Ii][Ss]",
	"[Hh]ndisambig",
	"[Ll]etter%-[Nn]umber [Cc]ombination [Dd]isambiguation",
	"[Ll]etter%-NumberCombDisambig",
	"[Mm]il%-unit%-dis",
	"[Nn]umberdis",
	"[Ss]choold[ai][bs]",
	"[Mm]il-unit-disambig",
	"[Mm]ilitary unit disambiguation",
	"[Gg]eo-dis",
	"[Gg]eodisambig",
	"[Dd]isambig[Gg]eo",
	"[Dd]isambig[GN]",
	"[Dd]isambigNm",
	"[Dd]isambigName",
	"[Ss]urname",
	"[Ss]urnames",
	"[Ss]pecies Latin name disambiguation",
	"[Ss]peciesLatinNameDisambig",
	"[Ll]atinNameDisambig",
	"[Mm]athematical disambiguation",
	"[Mm]athematics disambiguation",
	"[Mm]athdab",
	"[Mm]ath dab",
	"[Rr]oad disambiguation",
	"[Rr]oaddis",
}

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
	for _i, v in ipairs(disambigTemplates) do
		if mw.ustring.find(content, "{{%s*".. v .. "%s*%f[|}]") ~= nil then
			return true
		end
	end
	
	-- check for magic word
	if mw.ustring.find(content, "__DISAMBIG__", 1, true) ~= nil then
		return true
	end
	
	return false
end

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