Jump to content

Module:Uses TemplateStyles

From Wikimedia Commons, the free media repository

Documentation for this module may be created at Module:Uses TemplateStyles/doc

Code

-- This module implements the {{Uses TemplateStyles}} template.
local yesno = require('Module:Yesno')
local mList = require('Module:List')
local mTableTools = require('Module:TableTools')
local mMessageBox = require('Module:Message box')
local TNT = require('Module:TNT')
local lang = mw.getCurrentFrame():preprocess('{{int:lang}}')

local p = {}

local function format(msg, ...)
	return TNT.formatInLanguage(lang, 'I18n/Uses TemplateStyles', msg, ...)
end

local function getConfig()
	return mw.loadData('Module:Uses TemplateStyles/config')
end

function p.main(frame)
	local origArgs = frame:getParent().args
	local args = {}
	for k, v in pairs(origArgs) do
		v = v:match('^%s*(.-)%s*$')
		if v ~= '' then
			args[k] = v
		end
	end
	return p._main(args)
end

function p._main(args)
    -- Parse array for ipairs() loop, take positive integer keys only who have a value and put those values into a table whilst keeping the order of arguments. Discards negative keys, zero, non-integer keys, and nil or empty values.
    -- e.g. {1, nil, foo = 'bar', 3, 2} => {1, 2, 3}
	local tStyles = mTableTools.compressSparseArray(args)
	local box, titleObj = p.renderBox(tStyles)
	local trackingCategories = p.renderTrackingCategories(args, tStyles, titleObj)
	return box .. trackingCategories
end

function p.renderBox(tStyles)
	local boxArgs = {}
    local tsTitle, guess_tsTitle = nil, nil
	if #tStyles < 1 then -- if no TemplateStyles page links explicitly given as arguments.
        local cfg = getConfig()
        local stylesCss = cfg['styles_css_title'] or 'styles.css'
        local foundIt = false
        tsTitle = mw.title.getCurrentTitle() -- mw.title object of current page
        guess_tsTitle = mw.title.new(string.format('%s:%s/%s', tsTitle.nsText or 'Template', tsTitle.baseText or '',
            stylesCss)) -- mw.title object of guessed styles.css subpage of where most likely to be.
        if guess_tsTitle and guess_tsTitle.exists then
            foundIt = true
        elseif guess_tsTitle and not guess_tsTitle.exists and guess_tsTitle.namespace == 'Template' then
            foundIt = true
        else
            foundIt = false
        end
        
        if not foundIt then
            mw.addWarning(string.format('<strong class="error">{{int:error}} %s</strong>', format('error-emptylist')))
            boxArgs.text = string.format('<strong class="error" style="font-size: inherit;">%s</strong>',
                format('error-emptylist'))
        end
	end
    if #tStyles > 0 then -- It should continue, we could not insert something in the table as it's read-only.
		local cfg = getConfig()
		local tStylesLinks = {}
		for i, ts in ipairs(tStyles) do
			local link = string.format('[[:%s]]', ts)
			local sandboxLink = nil
			if tsTitle and cfg['sandbox_title'] then
				local tsSandboxTitle = mw.title.new(string.format(
					'%s:%s/%s/%s', tsTitle.nsText, tsTitle.baseText, cfg['sandbox_title'], tsTitle.subpageText))
				if tsSandboxTitle and tsSandboxTitle.exists then
					sandboxLink = format('sandboxlink', link, ':' .. tsSandboxTitle.prefixedText)
				end
			end
			tStylesLinks[i] = sandboxLink or link
		end
		local tStylesList = mList.makeList('bulleted', tStylesLinks)
        tsTitle = tsTitle or mw.title.getCurrentTitle()
		boxArgs.text = format(
			tsTitle:inNamespaces(828, 829) and 'header-module' or 'header-template') ..
			'\n' .. tStylesList
    elseif guess_tsTitle and guess_tsTitle.prefixedText then
        local tStylesList = ''
        tsTitle = guess_tsTitle
        --if guess_tsTitle.exists then
            tStylesList = string.format('<ul><li>[[:%s]]</li></ul>', guess_tsTitle.prefixedText)
        --end
        boxArgs.text = format(
			guess_tsTitle:inNamespaces(828, 829) and 'header-module' or 'header-template') ..
			'\n' .. tStylesList
    end
    if guess_tsTitle then
        boxArgs.style = 'border-color: var(--border-color-disabled, #c8ccd1);'
    end
	boxArgs.type = 'notice'
	boxArgs.small = true
	boxArgs.image = string.format('<div aria-hidden="true">[[File:CSS3_logo_small.svg|x30px|link=|class=skin-invert|alt=%s]]</div>', format('logo-alt'))
	return mMessageBox.main('mbox', boxArgs), tsTitle
end

function p.renderTrackingCategories(args, tStyles, titleObj)
	if yesno(args.nocat) then
		return ''
	end

	local cfg = getConfig()
	local cats = {}

	-- Error category
	if #tStyles < 1 and cfg['error_category'] then
		cats[#cats + 1] = cfg['error_category']
	end

	-- TemplateStyles category
	titleObj = titleObj or mw.title.getCurrentTitle()
	if (titleObj.namespace == 10 or titleObj.namespace == 828)
		and not cfg['subpage_blacklist'][titleObj.subpageText]
	then
		local category = args.category or cfg['default_category']
		if category then
			cats[#cats + 1] = category
		end
		if not yesno(args.noprotcat) and (cfg['protection_conflict_category'] or cfg['padlock_pattern']) then
			local currentProt = titleObj.protectionLevels["edit"] and titleObj.protectionLevels["edit"][1] or nil
			local addedLevelCat = false
			local addedPadlockCat = false
			for i, ts in ipairs(tStyles) do
				local tsTitleObj = mw.title.new(ts)
				if tsTitleObj then
					local tsProt = tsTitleObj.protectionLevels["edit"] and tsTitleObj.protectionLevels["edit"][1] or nil
				if cfg['protection_conflict_category'] and tsProt ~= currentProt and not addedLevelCat then
					cats[#cats + 1] = cfg['protection_conflict_category']
				end
				if cfg['padlock_pattern'] and tsProt and not addedPadlockCat then
					local content = tsTitleObj:getContent()
					if content and not content:find(cfg['padlock_pattern']) then
						cats[#cats + 1] = cfg['missing_padlock_category']
						addedPadlockCat = true
					end
				end
			end
			end
		end
	end
	for i, cat in ipairs(cats) do
		cats[i] = string.format('[[Category:%s]]', cat)
	end
	return table.concat(cats)
end

return p