Module:Task

Revision as of 21:23, 4 November 2021 by Admin (talk | contribs) (1 revision imported)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

-- <nowiki>
local p = {}

local vars = mw.ext.VariablesLua
local yesno = require('Module:Yesno')
local paramtest = require('Module:Paramtest')
local dpl = require('Module:DPLlua').ask

local compstates = {
	[true] = {
		img = '[[File:Yes check.svg|20px|link=]]',
		txt = '<span class="task-status">(completed)</span>',
		smw = 'Complete'
	},
	[false] = {
		img = '[[File:X mark.svg|20px|link=]]',
		txt = '<span class="task-status">(open)</span>',
		smw = 'Open'
	}
}
local nacell = mw.html.create('td'):addClass('table-na'):css('text-align', 'center'):wikitext('N/A')

function p.main(frame)
	local args = frame:getParent().args
	local title = mw.title.getCurrentTitle()
	local istable = yesno( vars.var( 'taskIsTable', 'false' ) )
	
	args = getArgs(args, title)
	args = parseArgs(args, title)
	
	if istable then
		return genTable(args)
	end
	return genNormal(args)
end

function p.list(frame)
	local args = frame:getParent().args
	local limit = 500
	if paramtest.has_content( args.limit ) and tonumber(args.limit, 10) then
		limit = tonumber(args.limit, 10)
	end
	
	local filt_res = false
	local group,coordinator,completed
	if paramtest.has_content( args.group ) then
		filt_res = true
		group = string.lower(args.group)
	end
	if paramtest.has_content( args.coordinator ) then
		filt_res = true
		coordinator = string.lower(args.coordinator)
	end
	if paramtest.has_content( args.complete ) then
		filt_res = true
		complete = yesno( args.complete )
		if complete ~= true and complete ~= false then
			complete = false
		end
	end
	
	local ret = mw.html.create('table')
	ret
		:addClass('wikitable sortable align-center-1 autosort=1,a')
		:tag('tr')
			:tag('th'):wikitext('Completed'):done()
			:tag('th'):wikitext('Task'):done()
			:tag('th'):wikitext('Skills'):done()
			:tag('th'):wikitext('Coordinator'):done()
			:tag('th'):wikitext('Project/Group'):done()
		:done()
	
	local dplres = dpl({
		['count'] = limit,
		['notcategory'] = 'Personal tasks',
		['notnamespace'] = 'Template|Module',
		['uses'] = 'Template:Task',
		['include'] = '{Task}:1:task:complete:coordinator:group:skills:personal'
	})
	-- mw.logObject( dplres )
	
	local has_res = false
	local icount = 0
	if dplres and #dplres > 0 then
		for i,v in ipairs( dplres ) do
			local iargs = v['include']['Task']
			local ititle = mw.title.new( v['title'] )
			iargs = getArgs(iargs, ititle)
			
			if filt_res then
				local ishow = false
				local ihide = false
				if group then
					if group == string.lower(iargs.group) then
						ishow = true
					else
						ihide = true
					end
				end
				if coordinator then
					if coordinator == string.lower(iargs.coordinator) then
						ishow = true
					else
						ihide = true
					end
				end
				if complete ~= nil then
					if complete == iargs.complete then
						ishow = true
					else
						ihide = true
					end
				end
				if paramtest.has_content( iargs.personal ) then
					ihide = true
				end
				
				if ishow and not ihide then
					iargs = parseArgs(iargs, ititle)
					has_res = true
					ret:node( genTable(iargs) )
					icount = icount + 1
				end
			elseif not paramtest.has_content( iargs.personal ) then
				iargs = parseArgs(iargs, ititle)
				has_res = true
				ret:node( genTable(iargs) )
				icount = icount + 1
			end
			if icount > limit then
				break
			end
		end
	end
	
	if not has_res then
		ret:tag('tr')
			:tag('td')
				:attr({ colspan = 5 })
				:css( 'text-align', 'center')
				:wikitext('No applicable tasks found')
			:done()
	end
	
	return ret
end

function getArgs(args, title)
	if paramtest.is_empty( args.complete ) then
		args.complete = false
	else
		args.complete = yesno( args.complete )
	end
	if args.complete ~= true and args.complete ~= false then
		args.complete = false
	end
	args.compstate = compstates[ args.complete ]
	
	if paramtest.is_empty( args.group ) then
		local group = vars.var( 'taskGroup', '' )
		if paramtest.has_content( group ) then
			args.group = group
		elseif title.namespace == 2 or title.namespace == 3 then
			-- User sub pages and talk pages
			args.group = title.subpageText
		elseif title.namespace == 4 or title.namespace == 5 then
			-- Project (RuneScape:) pages and talk pages
			args.group = title.baseText
		elseif title.namespace == 1 then
			-- Talk, use page name without namespace
			args.group = title.text
		else
			-- Fall back to page title with namespace
			args.group = string.gsub( title.fullText, ' talk', '' )
		end
	end
	
	if paramtest.is_empty( args.coordinator ) then
		local tblcoord = vars.var( 'taskCoord', '' )
		if paramtest.has_content( tblcoord ) then
			args.coordinator = tblcoord
		elseif title.namespace == 2 then
			-- User namespace
			args.coordinator = title.rootText
		end
	end
	
	if paramtest.is_empty( args.skills ) then
		local tblskills = vars.var( 'taskSkills', '' )
		if paramtest.has_content( tblskills ) then
			args.skills = tblskills
		end
	end
	
	if paramtest.is_empty( args.task ) then
		if paramtest.has_content( args[1] ) then
			args.task = args[1]
		end
	end
	
	args.savesmw = true
	if paramtest.has_content( args.personal ) then
		args.savesmw = false
	elseif paramtest.has_content( args.nosmw ) then
		args.savesmw = false
	elseif  paramtest.has_content( vars.var('taskPersonal','') ) then
		args.savesmw = false
	end
	
	return args
end

function parseArgs(args, title)
	args.pgTitle = title.prefixedText
	args.editUrl = tostring( mw.uri.fullUrl( title.prefixedText, 'action=edit' ) )
	
	if paramtest.has_content( args.group ) then
		args.group = string.format( '[[%s|%s]]', args.pgTitle, args.group )
	else
		args.group = string.format( '[[%s|%s]]', args.pgTitle, args.pgTitle )
	end
	
	if paramtest.has_content( args.coordinator ) then
		args.coordinator = string.format( '[[User talk:%s|%s]]', args.coordinator, args.coordinator )
	end
	
	if paramtest.is_empty( args.task ) then
		args.task = string.format( '[%s edit]', args.editUrl )
	end
	
	return args
end

function genTable(args)
	local ret = mw.html.create('tr')
	
	ret
		:tag('td'):wikitext( args.compstate.img .. '<br><small>[' .. args.editUrl .. ' completed?]</small>' ):done()
		:tag('td'):wikitext( args.task ):done()
	
	if paramtest.has_content( args.skills ) then
		ret:tag('td'):wikitext( args.skills ):done()
	else
		ret:node( nacell )
	end
	if paramtest.has_content( args.coordinator ) then
		ret:tag('td'):wikitext( args.coordinator ):done()
	else
		ret:node( nacell )
	end
	if paramtest.has_content( args.group ) then
		ret:tag('td'):wikitext( args.group ):done()
	else
		ret:node( nacell )
	end
	
	return ret
end

function genNormal(args)
	local ret = mw.html.create('div')
	ret:addClass('messagebox2 task')
		:tag('div')
			:addClass('messagebox-header')
			:tag('div')
				:addClass('messagebox-image')
				:wikitext('[[File:Wikian book.png|24px|link=]]')
			:done()
			:tag('div')
				:addClass('messagebox-title')
				:tag('span'):wikitext( "'''Wiki Task:  '''" ):done()
				:wikitext( args.compstate.txt .. '  <small>[' .. args.editUrl ..' completed?]</small>' )
			:done()
		:done()
	local more = ret:tag('div')
			:addClass('messagebox-text')
			:tag('div')
				:addClass('messagebox-desc')
				:wikitext( args.task )
			:done()
	
	if paramtest.has_content( args.skills ) or paramtest.has_content( args.coordinator ) or paramtest.has_content( args.group ) then
		local info = {}
		if paramtest.has_content( args.coordinator ) then
			table.insert( info, "<span class=\"task-info\">'''Coordinator:'''  " .. args.coordinator .. '</span>' )
		end
		if paramtest.has_content( args.group ) then
			table.insert( info, "<span class=\"task-info\">'''Group:'''  " .. args.group .. '</span>' )
		end
		if paramtest.has_content( args.skills ) then
			table.insert( info, "<span class=\"task-info\">'''Skills:'''  " .. args.skills .. '</span>' )
		end
		more:tag('div')
			:addClass('messagebox-reason')
			:wikitext( table.concat(info, '') )
	end
	
	return ret
end

--[=[
Debug console code:
mw.log( p.main( { getParent = function() return { args = {
  [1] = "As ''you'' can see, [[Help:Editing/Cheat sheet|wikitext]] '''can''' be used in task descriptions.",
  coordinator = 'Elessar2',
  group = 'Test group',
  skills = 'My skills'
} } end } ) )
--]=]

return p