Module:Sandbox/ProcrastinatingReader/Dbm

This is an old revision of this page, as edited by ProcrastinatingReader (talk | contribs) at 15:45, 20 September 2020 (0 init). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}
local getArgs = require('Module:Arguments').getArgs

-- Construct a table of CSDs and any arguments passed in per CSD
local function constructCsdTable(args)
	local tbl = {}
	local keys = {}
    local current = ''
    local size = 0
    for k,v in pairs(args) do
        if string.match(v, '[GgAaFfCcUuRrTtPp]%d%d?') then
            current, size = string.lower(v), 0
            table.insert(keys, current)
            tbl[current] = {['size'] = size}
        else
        	local arg = mw.text.split(v, '=')
        	tbl[current][string.lower(arg[1])] = arg[2]
        	size = size + 1
        	tbl[current]['size'] = size
        end
    end
    
    return {keys, tbl}
end

local function build(frame, args)
	local passThruArgs = {'rationale', 'xfd', 'user', 'blanked', 'url', 'url2', 'url3', 'source', 'article', '___location', 'filename', 'criterion'}
	local constructed = constructCsdTable(args)
	local csds = constructed[1]
	local csdList = constructed[2]

    local requestedReasons = {}
    local summary = ""
    
    -- Loop through CSDs (eg G1, G2)
    local n = 0
    for k,v in pairs(csdList) do
    	n = n + 1
    	local csdArgs = {
    		['raw'] = 'yes',
    		['nocat'] = 'true' -- for test purposes
        }
        
        summary = summary .. string.upper(k) .. (v['size'] > 0 and ' (' or '')
        
        -- add in any custom params we got, eg Rationale, URL
        for _,arg in ipairs(passThruArgs) do
        	if v[arg] then
        		csdArgs[arg] = v[arg]
        		summary = summary .. arg .. ': ' .. v[arg] .. (v['size'] > _ and ', ' or '')
        	end
        end
        summary = summary .. (v['size'] > 0 and ')' or '') .. (#csds > n and ', ' or '')
    	
    	table.insert(requestedReasons, frame:expandTemplate{ title = 'Db-'..k, args = csdArgs })
    end
    
    -- Construct Db-meta wrapper
    local dbTemplate = frame:expandTemplate{ title = 'Db-meta', args = {
    		[1] = '&#32;for the following reasons:</b><ul><li>'..table.concat(requestedReasons, '</li><li>') .. '</li></ul> <b>The page may be deleted under any criterion that is valid',
    		['criterion'] = 'NA',
    		['temp'] = 'Db-notice-multiple',
    		['temp2'] = '|'..table.concat(csds, '|'),
    		['summary'] = 'Multiple reasons: ' .. summary,
    		
    		-- forward given params to Db-meta
    		['bot'] = args['bot'] or '',
    		['blanked'] = args['blanked'] or (csdList['g10'] and 'yes') or '',
    		['divblank'] = args['divblank'] or (csdList['g10'] and 'yes') or '',
    		['help'] = args['help'] or '',
    }}
    return dbTemplate
end

-- Exports

function p.main(frame)
	local args = getArgs(frame)
	return build(frame, args)
end

function p.dump(frame)
	return mw.dumpObject(mw.text.unstripNoWiki(frame.args[1]))
end

return p