Modul:Service portal

IMT HilfeWiki - das Wiki
Documentation icon Module documentation
This module implements template {{Service portal}}.

Usage[Quelltext bearbeiten]

{{#invoke:Service portal|main}}

local getArgs = require('Module:Arguments').getArgs
local _CFG = mw.loadData('Modul:Article/config')	-- it is intended to load article's config
local _ERROR = require('Modul:Error')._error
local _INFOBOX = require('Module:Infobox').infobox
local _TT = require('Module:TableTools')
local _SMWUTIL = require('Module:SmwUtil')

-- helper vars, standing in for missing config
local category = 'Dienstportalseiten'
local defaultTargetgroups = _TT.shallowClone(_CFG.defaultValues.targetgroup)
local statusImageSuffix = '.png'
local urlTostatusPortal = 'https://statusmeldungen.uni-paderborn.de/state/'

local p = {}

local function arraysEqual(ar1, ar2)
	local ar1 = _TT.shallowClone(ar1)
	local ar2 = _TT.shallowClone(ar2)
	if type(ar1) ~= 'table' or type(ar2) ~= 'table' then
		return false
	end
	table.sort(ar1)
	table.sort(ar2)
	return #ar1 and #ar2 and (table.concat(ar1, '-') == table.concat(ar2, '-'))	-- cheap hack, but should suffice
end

local function compileInfobox(processedArgs)
	
	local targetGroup = processedArgs.targetgroup
	if #processedArgs.targetgroup == 0 then
		targetGroup = _ERROR{message='Keine Zielgruppe angegeben'}
	elseif ( arraysEqual(processedArgs.targetgroup, defaultTargetgroups) ) then
		targetGroup = 'Endbenutzer'
	else
		targetGroup = mw.text.listToText(targetGroup, ', ', ' und ')
	end
	
	local ib_args = {
--		aboveclass = 'objtitle titletext',
		above = mw.title.getCurrentTitle().rootText,
		image = processedArgs.image,
		subheader = 'Ein Dienst des IMT',
		caption = mw.title.getCurrentTitle().rootText,
--		headerclass = 'headertext',
		labelstyle = 'width: 30%;',
		datastyle = 'width: 70%;',
		header1 = "Informationen",
		label2 = 'Verantwortliche/r',
		data2 = processedArgs.responsible,	-- not set atm
		label3 = 'Zielgruppe',
		data3 = targetGroup,
		data4 = processedArgs.description,
	}
	return _INFOBOX(ib_args)
end


-- creates an mw.html node, containing (an) errorbox(es), if errors are present.
--	in this case, Category:Erroneous will be set by template Template warning
local function createErrorNode(errors)
	local errorBoxes = mw.html.create('')
	local frame = mw.getCurrentFrame()
	if #errors > 0 then
		for _, errorText in pairs(errors) do
			errorBoxes:wikitext(frame:expandTemplate{title = 'Template warning', args={errorText, 'Service portal'}})
			errorBoxes:newline()
		end
	end
	return errorBoxes
end

-- Here we process the supplied args, sanitize then, check for plausibility and set defaults, if needed
local function processArgs(args)
	local errors = {}
	local processedArgs = {}

	-- we need to process the following arguments: description, image, targetgroup

	-- 1st parameter: targetgroup
	if args.targetgroup and #args.targetgroup then
		local valid = {}
		local suppliedTargetGroups
		if type(args.targetgroup) == 'table' then
			suppliedTargetGroups = args.targetgroup
		else
			suppliedTargetGroups = mw.text.gsplit(args.targetgroup, ',', true)
		end

		for fragment in suppliedTargetGroups do
			fragment = mw.text.trim(fragment)
			if fragment and mw.ustring.len(fragment) > 0 then
				if _TT.inTable(_CFG.validValues.targetgroup, fragment) then
					table.insert(valid, fragment)
				else
					table.insert(errors, "Parameter ''targetgroup'' hat einen ungültigen Wert: \"" .. fragment .. "\"!")
				end
			end
		end
		processedArgs.targetgroup = valid
	else
		processedArgs.targetgroup = defaultTargetgroups
	end

	-- 2nd parameter (OPTIONAL): description
	processedArgs.description = nil
	if args.description and mw.ustring.len(mw.text.trim(args.description)) then
		processedArgs.description = mw.text.trim(args.description)
	end

	-- 3rd parameter (OPTIONAL): image
	-- if empty, try a fallback name
	-- else if encased in [[]], then use directly
	-- else if contains . then encase it in [[]] and use
	-- else include image from status portal
	processedArgs.image = '[[Datei:Logo_' .. mw.title.getCurrentTitle().rootText .. '.png]]'
	if args.image and mw.ustring.len(mw.text.trim(args.image)) then
		local suppliedImage = mw.text.trim(args.image)
		if mw.ustring.match(suppliedImage, '^%[%[.+%]%]$') then
			processedArgs.image = suppliedImage
		elseif mw.ustring.find(suppliedImage, '.', 1, true) then
			if not mw.ustring.match(suppliedImage, ':') then
				suppliedImage = 'File:' .. suppliedImage
			end
			processedArgs.image = '[[' .. suppliedImage .. ']]'
		else
			processedArgs.image = urlTostatusPortal .. suppliedImage .. statusImageSuffix
		end
	end
	
	return processedArgs, errors
end

local function storeSementicData(uservalues)
	
	local smwData = {
		['is written for target audience'] = uservalues.targetgroup
	}

	if uservalues.description then
		smwData['has short description'] = uservalues.description
		smwData['Glossary-Term'] = mw.title.getCurrentTitle().rootText
		smwData['Glossary-Definition'] = uservalues.description
	end
	
	_SMWUTIL.set(smwData)
end

function p._main(args)
	
	-- sanity, plausibiliy and defaults:
	local processedArgs, errors = processArgs(args)
	
	-- store data, if there are no errors
	if #errors == 0 then
		storeSementicData(processedArgs)
	end

	-- add infobox
	local output = mw.html.create('')
	output:wikitext(compileInfobox(processedArgs))
	output:newline()

	-- display errors, if there are some
	if #errors > 0 then
		output:newline()
		output:node(createErrorNode(errors))
	end

	-- add category
	output:wikitext('[[Category:' .. category .. ']]')

	return tostring(output)
end

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

function p.dl(args)
	local processedArgs, errors = processArgs(args)
	return processedArgs
end


return p
Cookies helfen uns bei der Bereitstellung des IMT HilfeWikis. Bei der Nutzung vom IMT HilfeWiki werden die in der Datenschutzerklärung beschriebenen Cookies gespeichert.