Module:Bot task

local getArgs = require('Module:Arguments').getArgs

local p = {}

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

local function error(text)
	return '*' .. require('Module:Error').error{ 'Invalid task config: ' .. text, tag = 'p' }
end 

local function has_value(array, val)
    for index, value in ipairs(array) do
        if value == val then
            return true
        end
    end
    return false
end

local function lowerfirst(s) 
	return s:sub(1,1):lower()..s:sub(2)
end 

local function get_namespaces(namespaces_list)
	local list = {}
	for token in string.gmatch(namespaces_list, "%d+") do
		local ns_id = tonumber(token)
		table.insert(list, ns_id ~= 0 and lowerfirst(mw.site.namespaces[ns_id]['name']) or 'main')
	end
	return table.concat(list, ', ')
end 	


function p._main(args)
	
	-- check for invalid parameters
	for param, value in pairs(args) do
		if not has_value({'bot','task','min_edits','duration','title_regex','title','namespace','action','summary','summary_regex','notify','email'}, param) then
			return error('unknown parameter: ' .. param .. ' used')
		end
	end
	
	if not args.bot or not args.task then
		return error('required parameters "bot" and/or "task" missing')
	end
	
	-- set defaults if not given
	args.min_edits = args.min_edits or 1
	args.duration = args.duration or '3 days'
	args.action = args.action or 'edit'
	
	local str = string.format("* [[User:%s|%s]]: %s. '''Frequency''': ≥ %s %s%s in the last %s. ", 
		args.bot, args.bot,
		args.task, 
		args.min_edits, 
		args.action == 'edit' and 'edit' or args.action .. ' action',
		args.min_edits == 1 and '' or 's', 
		args.duration
	)
	
	if args.title_regex then
		str = str .. string.format("'''Pages''': matching <code>%s</code> %s", args.title_regex, 
			args.namespace and string.format("in namespace: %s.", get_namespaces(args.namespace)) or '')
		if args.title then
			return error('do not use both title_regex and title parameters')
		end 
	elseif args.title then
		str = str .. string.format("'''Page''': [[%s]] ", args.title)
	elseif args.namespace then
		str = str .. string.format("In namespace: %s. ", get_namespaces(args.namespace))
	end
	
	if args.summary_regex then
		str = str .. string.format("With summary matching <code>%s</code>. ", args.summary_regex)
		if args.summary then
			return error('do not use both summary_regex and summary parameters')
		end 
	elseif args.summary then
		str = str .. string.format("With summary <code>%s</code>. ", args.summary)
	end
	
	if args.notify then
		local alertpage = mw.title.new(args.notify, 3).fullText
		str = str .. string.format("<b>Will notify</b> [[%s|%s]] in case of issues. ", alertpage, args.notify)
	end
	
	if args.email then
		if string.find(args.email, '@') then
			str = str .. string.format("<b>Will notify</b> by email in case of issues. ", args.email)
		else 
			local userPage = mw.title.new(args.email, 2).fullText
			str = str .. string.format("<b>Will notify</b> [[%s|%s]] by email in case of issues. ", userPage, args.email)
		end
	end
	
	return str
end

return p

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.