Jump to content

Module:Category main article/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mclay1 (talk | contribs) at 08:43, 5 November 2025. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
-- This module implements {{Category main article}}.

local mHatnote = require('Module:Hatnote')
local mFormatLink = require('Module:Format link')
local yesno = require('Module:Yesno')
local mTableTools -- lazily initialise
local mArguments -- lazily initialise

local p = {}

-- helper: returns true if page is the template or a subpage
local function isTemplateOrSubpage(title)
	local t = mw.title.new(title)
	return t and t.prefixedText:match('^Template:Category main article')
end

function p.catMain(frame)
	local thisTitle = mw.title.getCurrentTitle()

	-- Skip all frame-dependent operations on template/subpages to avoid circular reference
	if isTemplateOrSubpage(thisTitle) then
		local text = p._catMain({}, thisTitle.text)
		return text
	end

	mTableTools = require('Module:TableTools')
	mArguments = require('Module:Arguments')

	local args = mArguments.getArgs(frame)
	local pages = mTableTools.compressSparseArray(args)

	-- Ensure first unnamed argument is used if compressSparseArray returns empty
	if #pages == 0 and args[1] then
		pages = { args[1] }
	end

	local options = {
		article = args.article,
		selfref = args.selfref,
		_rawPages = pages
	}

	-- Generate the hatnote text
	local text = p._catMain(options, unpack(pages))

	-- Only add tracking categories if frame supports it
	if frame and frame.addCategories then
		local ns = thisTitle.namespace

		-- wrong namespace tracking in article namespace
		if ns == 0 then
			frame:addCategories('Articles using category hatnotes')
		end

		-- redlink tracking
		for _, page in ipairs(pages) do
			local checkPage = page:gsub("|.*","")
			if checkPage ~= '' and not isTemplateOrSubpage(checkPage) then
				local t = mw.title.new(checkPage)
				if t and not t.exists then
					frame:addCategories('Categories with hatnote templates targeting a nonexistent page')
					break
				end
			end
		end

		-- mismatch tracking
		if #pages == 1 then
			local usedAutoFill = false
			if not pages[1] or pages[1] == '' then
				pages[1] = thisTitle.text
				usedAutoFill = true
			end

			local compareStr
			if usedAutoFill then
				compareStr = pages[1]:gsub('^[^:]*:', '')
			else
				compareStr = options._rawPages[1]:gsub('^[^:]*:', '')
			end

			local catName = thisTitle.text:gsub('^[^:]*:', '')
			if compareStr ~= catName then
				frame:addCategories('Category main article does not match category title')
			end
		end
	end

	return text
end

function p._catMain(options, ...)
	options = options or {}

	local pages = {...}
	local thisTitle = mw.title.getCurrentTitle()
	local thisText = thisTitle and thisTitle.text or pages[1] or ''

	-- Get the links table.
	local rawLinks = mFormatLink.formatPages({}, {...})
	local links = {}
	for i, link in ipairs(rawLinks) do
		links[i] = tostring(link)
	end

	local usedAutoFill = false
	if not links[1] or links[1] == '' then
		local title = mw.title.new(thisText)
		if title.isRedirect then
			title = title.redirectTarget
		end
		links[1] = tostring(mFormatLink._formatLink{link = title.text})
		usedAutoFill = true
		pages = { title.text }
	end

	-- bold links
	for i, link in ipairs(links) do
		links[i] = string.format("'''%s'''", link)
	end

	-- Get the pagetype.
	local pagetype
	if options.article ~= nil then
		pagetype = yesno(options.article) ~= false and 'article' or 'page'
	elseif pages and pages[1] then
		local page = pages[1]:gsub("|.*","")
		pagetype = mw.title.new(page).namespace == 0 and "article" or "page"
	else
		pagetype = "article"
	end

	-- Work out whether we need to be singular or plural.
	local stringToFormat
	if #links > 1 then
		stringToFormat = 'The main %ss for this [[Help:Categories|category]] are %s.'
	else
		stringToFormat = 'The main %s for this [[Help:Categories|category]] is %s.'
	end

	-- Get the text.
	local text = string.format(
		stringToFormat,
		pagetype,
		mw.text.listToText(links)
	)

	-- Pass it through to Module:Hatnote.
	if not isTemplateOrSubpage(thisTitle) then
		local hnOptions = {}
		hnOptions.selfref = options.selfref
		text = mHatnote._hatnote(text, hnOptions)
	end

	return text
end

return p