Jump to content

Module:Disambiguation: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
m Changed protection settings for "Module:Disambiguation": Upping to full protection as now used by Module:Pagetype ([Edit=Require administrator access] (indefinite) [Move=Require administrator access] (indefinite))
deploy new version, code by User:Mr. Stradivarius
Line 1: Line 1:
local p = {}
local p = {}
local mRedirect = require('Module:Redirect')
local mRedirect = require('Module:Redirect')
local disambiguationTemplates = mw.loadData('Module:Disambiguation/templates')


local function capitalize(s)
local disambigTemplates = {
-- This function only works on ASCII strings. If your wiki has
"[Dd][Aa][Bb]",
-- disambiguation templates that use Unicode strings, use the commented-out
"[Dd]big",
-- line instead. Enwiki uses ASCII string manipulation only here to improve
"[Dd]is",
-- performance.
"[Dd]isambiguation",
return s:sub(1, 1):upper() .. s:sub(2, -1)
"[%w_%s]-%f[%w][Dd]isam[%w]-",
-- return mw.ustring.upper(mw.ustring.sub(1, 1)) .. mw.ustring.sub(2, -1)
"[Gg]eodis",
end
"[Hh][Nn][Dd][Ii][Ss]",

"[Hh]ndisambig",
local function isDisambiguationTemplate(template)
"[Ll]etter%-[Nn]umber [Cc]ombination [Dd]isambiguation",
return disambiguationTemplates[capitalize(template)] or false
"[Ll]etter%-NumberCombDisambig",
end
"[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]urnames?",
"[Ss]pecies Latin name disambiguation",
"[Ss]peciesLatinNameDisambig",
"[Ll]atinNameDisambig",
"[Mm]athematical disambiguation",
"[Mm]athematics disambiguation",
"[Mm]ath ?dab",
"[Rr]oad disambiguation",
"[Rr]oaddis",
}


p.isDisambiguation = function(content)
p.isDisambiguation = function(content)
-- false if there is no content
-- false if there is no content
if content == nil then return false end
if content == nil then
return false
end

-- redirects are not disambiguation pages
-- redirects are not disambiguation pages
if mRedirect.getTargetFromText(content) ~= nil then return false end
if mRedirect.getTargetFromText(content) ~= nil then
return false
end

-- check for disambiguation templates in the content
-- check for disambiguation templates in the content
local templateNames = {}
local templateNames = {}
for name in string.gmatch(content, "{{%s*([^|}]-)%s*[|}]") do
for template in string.gmatch(content, "{{%s*([^|}]-)%s*[|}]") do
if isDisambiguationTemplate(template) then
templateNames[name] = true
return true
end
for _i, v in ipairs(disambigTemplates) do
for template, _ in pairs(templateNames) do
if string.match(template, "^"..v.."$") then
return true
end
end
end
end
end

-- check for magic word
-- check for magic word
if string.find(content, "__DISAMBIG__", 1, true) ~= nil then
if string.find(content, "__DISAMBIG__", 1, true) ~= nil then
return true
return true
end
end

return false
return false
end
end

Revision as of 18:27, 19 September 2023

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 = {}
	for template in string.gmatch(content, "{{%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