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 10:25, 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: true if title is Template:Category main article or subpage
local function isTemplateOrSubpage(title)
	if type(title) ~= 'string' then
		title = title.prefixedText
	end
	return title:match('^Template:Category main article')
end

function p.catMain(frame)
	mTableTools = require('Module:TableTools')
	mArguments = require('Module:Arguments')

	local args = mArguments.getArgs(frame)
	local pages = mTableTools.compressSparseArray(args)
	if #pages == 0 and args[1] then
		pages = { args[1] }
	end

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

	local thisTitle = mw.title.getCurrentTitle()
	local ns = thisTitle.namespace
	local frameObj = mw.getCurrentFrame()

	-- Add tracking categories
	if not isTemplateOrSubpage(thisTitle) and frameObj and frameObj.addCategories then
		-- Article namespace detection
		if ns == 0 then
			frameObj:addCategories('Articles using category hatnotes')
		end

		-- Red link detection
		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
					frameObj:addCategories('Categories with hatnote templates targeting a non-existent page')
					break
				end
			end
		end

		-- Title mismatch detection
		if #pages == 1 then
			local pageName = pages[1]
			if not pageName or pageName == '' then
				pageName = thisTitle.text
			end
			local compareStr = pageName:gsub('^[^:]*:', '')
			local catName = thisTitle.text:gsub('^[^:]*:', '')
			if compareStr ~= catName then
				frameObj:addCategories('Category main article does not match category title')
			end
		end
	end

	return p._catMain(options, unpack(pages))
end

function p._catMain(options, ...)
	options = options or {}
	local pages = {...}
	local thisTitle = mw.title.getCurrentTitle()
	local thisText = thisTitle.text

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

	-- Auto-fill if empty
	local usedAutoFill = false
	if not 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

	-- Determine page type
	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

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

	-- Pass it through to Module:Hatnote
	local hnOptions = { selfref = options.selfref }
	text = mHatnote._hatnote(text, hnOptions)

	return text
end

return p