Modul:Article

IMT HilfeWiki - das Wiki
Documentation icon Module documentation
This module implements template {{Article}}. Configuration is stored in Module:Article/config.

Usage[Quelltext bearbeiten]

{{#invoke:Article|main}}

local getArgs = require('Module:Arguments').getArgs
local _INFOBOX = require('Module:Infobox').infobox
local _CFG = mw.loadData('Modul:Article/config')
local _TT = require('Module:TableTools')
local _SMWUTIL = require('Module:Smwutil')

local article = {}
local errors = {}
local os2osfamily = {}

function getPagesInCategory(category)
	if category and #category > 0 then
		return _SMWUTIL.luaask({'[[Category:' .. category .. ']]'}, {'?'}, nil, 'arr')
	else
		return nil
	end
end

function getOs2osfamily()
	if #os2osfamily == 0 then
		local result = _SMWUTIL.luaask('[[Category:Betriebssysteme]]', 'is member of os family')
		if not result or _TT.size(result) == 0 then
			os2osfamily = _CFG.os2osfamily
		else
			for os, osdata in pairs(result) do
				os2osfamily[mw.ustring.lower(os)] = mw.ustring.lower(osdata['is member of os family'])
				os2osfamily[os] = mw.ustring.lower(osdata['is member of os family'])
			end
		end
	end
	return os2osfamily
end

function getArticleInfoboxType(uservalues_os)
	if #os2osfamily == 0 then
		if _CFG.control.useAutomatedOsFamilyDetection then
			os2osfamily = getOs2osfamily()
		else
			os2osfamily = _CFG.os2osfamily
		end
	end

	local families = {}
	local ibType
	for _, os in pairs(uservalues_os) do
		if os2osfamily[mw.ustring.lower(os)] then
			families[os2osfamily[mw.ustring.lower(os)]] = 1
			ibType = os2osfamily[mw.ustring.lower(os)]
		else
			families[_CFG.control.fallbackIBType] = 1
			ibType = _CFG.control.fallbackIBType
		end
	end
	if _TT.size(families) ~= 1 then
		return _CFG.control.fallbackIBType
	else
		return ibType
	end
end

function article.processArgs(args)
	--[[
		This method takes the user provided arguments and
		* checks all mandatory requirements
		* converts all multivalue parameters to tables
		* checks all provided valid entries
		* sets default values for non mandatory and not provided parameters
		* adds a preceeding 'Service:' to service if omitted
		f
		************** NOTE:
		The following parameters will optionally be checked for validity:
		* os
		* service
		see Modul:Article/config table control for more information
	--]]
	local uservalues = {}
	for _, arg in pairs(_CFG.allParams) do
		if _TT.inTable(_CFG.mandatory, arg) and (not args[arg] or #args[arg] == 0) then
			table.insert(errors, "Pflichtfeld ''" .. arg .. "'' fehlt!")
		elseif _TT.inTable(_CFG.multivalues, arg) then	-- process multivalue parameters (can assume that mandatory fields are set)
			if args[arg] and #args[arg] > 0 then	-- multivalue parameter is set, so proceed
				local valid = {}
				local invalid = {}
				if _CFG.validValues[arg] then
					for _, v in pairs(_SMWUTIL.split(args[arg])) do	-- for every entry, check if it is valid
						if _TT.inTable(_CFG.validValues[arg], v) then
							table.insert(valid, v)
						else
							table.insert(invalid, v)
						end
					end
				else	-- of if _CFG.validValues[arg] then
					valid = _SMWUTIL.split(args[arg])
				end	-- of if _CFG.validValues[arg] then .. else
				uservalues[arg] = valid
				if #invalid > 0 then
					table.insert(errors, "Parameter ''" .. arg .. "'' hat einen ungültigen Wert: " .. mw.text.listToText(invalid) .. "!")
				end
			else	-- multivalue parameter is not set, take default (which can be nil)
				uservalues[arg] = _TT.shallowClone(_CFG.defaultValues[arg])
			end
		else	-- process singlevalue parameters (can assume that mandatory fields are set)
			if not args[arg] or #args[arg] == 0 then	-- if not set, take default (which can be nil)
				uservalues[arg] = mw.clone(_CFG.defaultValues[arg])
			elseif _CFG.validValues[arg] and not _TT.inTable(_CFG.validValues[arg], args[arg]) then
				table.insert(errors, "Parameter ''" .. arg .. "'' hat einen ungültigen Wert: " .. args[arg] .. "!")
			else
				uservalues[arg] = args[arg]
			end	-- of if not args[arg] or #args[arg] == 0 then .. elseif .. else
		end	-- of if _TT.inTable(_CFG.mandatory, arg) and (not args[arg] or #args[arg] == 0) then .. elseif .. else
	end	-- of for _, arg in pairs(_CFG.allParams) do

	-- special processing of service field. see to it, that all entries have a preceeding 'Service:'
	for k, v in pairs(uservalues.service) do
		-- the following is for backwards compatibilty when we migrated services from ns meta to ns service
		if mw.ustring.find(v, 'Meta:', 1, true) then
			uservalues.service[k] = mw.ustring.gsub(v, 'Meta', 'Service', 1)
		elseif not mw.ustring.find(v, 'Service:', 1, true) then
			uservalues.service[k] = 'Service:' .. v
		end
	end
	
	-- if requested, do some plausibility checks
	if _CFG.control.validateParamOs then
		-- local valid_os = getPagesInCategory('Betriebssysteme')
		local valid_os = getOs2osfamily()
		local newvalues = {}
		local fallingBack2default = nil
		for _, os in pairs(uservalues.os) do
			if not valid_os[mw.ustring.lower(os)] then
				if _TT.inTable(_CFG.fallBack2default, os) then
					if not fallingBack2default then
						table.insert(newvalues, _CFG.defaultValues.os[1])
						fallingBack2default = true
					end
				else
					table.insert(errors, "Parameter ''os'' hat einen ungültigen Wert: '" .. os .. "'!")
				end
			else
				table.insert(newvalues, os)
			end	-- of if not valid_os[os] then
		end	-- of for _, os in pairs(uservalues.os) do
		uservalues.os = newvalues
	end	-- of if _CFG.control.validateParamOs then
	if _CFG.control.validateParamService then
		local valid_service = getPagesInCategory('Services')
		local inactive_service = getPagesInCategory('Inactive services')
		local all_services = valid_service
		for k,v in pairs(inactive_service) do
			all_services[k] = v
		end
		local newvalues = {}
		local fallingBack2default = nil
		for _, service in pairs(uservalues.service) do
			if not all_services[service] then
				if _TT.inTable(_CFG.fallBack2default, service) then
					if not fallingBack2default then
						table.insert(newvalues, _CFG.defaultValues.service[1])
						fallingBack2default = true
					end
				else
					table.insert(errors, "Parameter ''service'' hat einen ungültigen Wert: '" .. service .. "'!")
				end
			else
				table.insert(newvalues, service)
			end	-- of if not valid_service[service] then
		end	-- of for _, service in pairs(uservalues.service) do
		uservalues.service = newvalues
	end	-- end of if _CFG.control.validateParamService then
	
	return uservalues	
end

function article.storeSementicData(uservalues)
	
	local singlevalues = {}
	for arg, prop in pairs(_CFG.arg2prop) do
		if _TT.inTable(_CFG.multivalues, arg) then
			if uservalues[arg] then
				_SMWUTIL.set(prop, uservalues[arg])
			end
		else
			singlevalues[prop] = uservalues[arg]
		end
	end
	-- handling of special property special property "has sortkey"
	if uservalues.disambiguation and #uservalues.disambiguation > 0 then
		singlevalues['has sortkey'] = uservalues.disambiguation .. '~' .. mw.title.getCurrentTitle().text
	else
		singlevalues['has sortkey'] = mw.title.getCurrentTitle().text
	end
	if uservalues.pagetype and #uservalues.pagetype > 0 then
		singlevalues['is disambiguation page'] = 1
	else
		singlevalues['is disambiguation page'] = 0
	end
	
	_SMWUTIL.set(singlevalues)
end

function article.infobox(uservalues)
	local frame = mw.getCurrentFrame()
	
	-- "calculate" type of infobox
	local ib_type = getArticleInfoboxType(uservalues.os)

	-- "convert" the technical services in service portals
	local portals = {}
	local portals_tmp = {}
	
	for _, s in pairs(uservalues.service) do
		if not _TT.inTable(_CFG.defaultValues.service, s) then
			linkedPortals = frame:expandTemplate{ title = s }
			for portal in mw.text.gsplit(linkedPortals, ',', true) do
				portals_tmp[portal] = true
			end
		else
			portals_tmp[_CFG.control.displayDefaultAs] = true
		end
	end
	for k,_ in pairs(portals_tmp) do
		table.insert(portals, k)
	end
	table.sort(portals)
	
	if uservalues.os and #uservalues.os > 0 then
		for k, v in pairs(uservalues.os) do
			if _TT.inTable(_CFG.defaultValues.os, v) then
				uservalues.os[k] = _CFG.control.displayDefaultAs
			else
				uservalues.os[k] = '[[' .. v .. ']]'
			end
		end
	else
		uservalues.os = {'None'}
	end
	
	local ib_args = {
		bodyclass = '',
		aboveclass = 'objtitle titletext',
		title = uservalues.type,
		above = _CFG.ibCoreData[ib_type].image .. ' ' .. _CFG.ibCoreData[ib_type].label,
		headerclass = 'headertext',
		labelstyle = 'width: 30%;',
		datastyle = 'width: 70%;',
		header1 = "Informationen",
		label2 = 'Betriebssystem',
		data2 = mw.text.listToText(uservalues.os, ', ', ' und ') or '',--(uservalues.os and #uservalues.os > 0) and '[[' .. mw.text.listToText(uservalues.os, ']], [[', ']] und [[') .. ']]' or '',
		label3 = 'Service',
		--data3 = mw.text.listToText(services),	--'[[' .. mw.text.listToText(uservalues.service, ']], [[', ']] und [[') .. ']]',
		data3 = (#portals and mw.text.listToText(portals) or 'keine'),
		label4 = 'Interessant für',
		data4 = mw.text.listToText(uservalues.targetgroup, ', ', ' und '),
		header6 = _CFG.ibCoreData[ib_type].portal
	}
	return _INFOBOX(ib_args)
end

function article.addCategory(uservalues)
	if mw.title.getCurrentTitle().namespace ~= 0 then
		return ''
	end
	local cat = _CFG.type2category[uservalues.type] or _CFG.control.fallBackCategory
	local category = '[[Category:' .. cat .. ']]'
	return category
end

function article.addErrors()
	local errorBoxes = mw.html.create('')
	if #errors > 0 then
		local messageBox = require('Module:Message box')
		for _, errortext in pairs(errors) do
			errorBoxes:wikitext(messageBox.main( 'ambox', {
						type = 'delete',
    					text = errortext
    					-- More parameters...
				})
			)
		end
		if mw.title.getCurrentTitle().namespace == 0 then
			errorBoxes:wikitext('[[Category:' .. _CFG.control.errorCategory .. ']]')
		end
	end
	return errorBoxes
end


function article.addLinkToDisambiguation(uservalues)
	local disambigLink = mw.html.create('')
	
	if uservalues.disambiguation and #uservalues.disambiguation > 0 then
		local hatnote = require('Module:Hatnote')._hatnote
		disambigLink:wikitext(hatnote(_CFG.hatnote .. ' [[' .. uservalues.disambiguation .. ']]', {}))
	end
	
	return disambigLink
end


function article._main(args)
	local uservalues = article.processArgs(args)
	local output = mw.html.create('')
	if #errors == 0 then
		article.storeSementicData(uservalues)
	end

	output:wikitext(article.infobox(uservalues))
		:wikitext(article.addCategory(uservalues))
		:node(article.addLinkToDisambiguation(uservalues))
		:node(article.addErrors())

	-- when {{article}} is called by {{disambiguation}} you need to: {{#ifeq:{{#var:disambiguate_isdisambiguation|0}}|1||{{#set:Is disambiguation=0}}}}
	-- --> maybe its enough, to not have it set at all... {{#set:Is disambiguation=1}} is done by {{disambiguate}}
	return tostring(output)
end

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

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