Modul:Disambiguate

ZIM HilfeWiki - das Wiki
Documentation icon Module documentation
This module implements template {{Disambiguate}}.


Usage[Quelltext bearbeiten]

{{#invoke:Disambiguate|main}}

local getArgs = require('Module:Arguments').getArgs
local _ARTICLE = require('Module:Article')._main
local _SMWUTIL = require('Module:SmwUtil')
local _TT = require('Module:TableTools')

local category = 'Begriffserklärungsseiten für Artikel'

local p = {}

-- 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, 'Disambiguate'}})
			errorBoxes:newline()
		end
	end
	return errorBoxes
end

local function getMyArticles(debugService)
	local query = {
		select = { '[[Is disambiguated by::' .. (debugService or mw.title.getCurrentTitle().rootText) .. ']]' },
		fields = {
			'?Is related to os#',
			'?Is associated to service#',
			'?Is written for target audience#',
			'?Is of type#'
		}
	}
	local options = { sort = ',', order = 'ascending' }
	return _SMWUTIL.ask(query, options)
end

local function listMyArticles(data)
	
	local node = mw.html.create('')
	
	node:newline()
	node:wikitext('Folgende Artikel behandeln dieses Thema:')
	node:newline()
	for _k, article in pairs(data.articles) do
		node:wikitext('* [[' .. article .. ']]')
		node:newline()
	end

	return node
end

local function processMyArticleData(data)

	local myData = { articles = {}, os = {}, service = {}, targetgroup = {}, type = {}, pagetype = 'disambiguation page' }
	local fieldTranslation = {
	    ["Is associated to service"] = 'service',
	    ["Is of type"] = 'type',
	    ["Is related to os"] = 'os',
	    ["Is written for target audience"] = 'targetgroup',
	    ["PageName"] = 'articles',
	}
	local errors = {}
	
	if not data or data.lenth == 0 then
		table.insert(
			errors, 'There are no articles that use this page as disambiuation. Nothing to do here!'
		)
		return myData, errors
	end
	
	for _, row in pairs(data) do
		-- iterate over artice data (result from smw ask query)
		for propertyName, value in pairs(row) do
			local field = fieldTranslation[propertyName] or propertyName
			if type(value) == 'table' then
				for _, v in pairs(value) do
					if not _TT.inTable(myData[field], v) then
						table.insert(myData[field], v)
					end
				end
			else
				if not _TT.inTable(myData[field], value) then
					table.insert(myData[field], value)
				end
			end
		end
	end
	
	for k, v in pairs(myData) do
		if type(v) == 'table' then
			table.sort(myData[k])
		end
	end
	
	if #myData.type > 1 then
		table.insert(
			errors,
			'There is more than one type defined on the associated pages. '
			.. 'this error has to be corrected on the corresponding page(s)! '
			.. '(passed types are ' .. mw.text.listToText(myData.type, ', ', ' and ') .. ')'
		)
		myData.type = mw.text.listToText(myData.type, ', ', ' and ')
	elseif #myData.type == 1 then
		myData.type = table.remove(myData.type, 1)
	else
		myData.type = ''
	end

	if #myData.articles == 0 then
		table.insert(
			errors, 'There are no articles that use this page as disambiuation. Nothing to do here!'
		)
	end
	
	return myData, errors
end

function p._main(args)
	
	-- note: currently, this template/module uses no arguments of its own
	-- sanity, plausibiliy and defaults are done by Module:Article

	-- gather data for all articles, using this disambiguation page
	local result, query = getMyArticles()
	local myData, errors = processMyArticleData(result)
	
	local output = mw.html.create('')
	
	-- now call the artice module for sanity checks, smw stash and infobox
	output:wikitext(_ARTICLE(myData))
	
	if #errors > 0 then
		output:newline()
		output:node(createErrorNode(errors))
	else
		output:node(listMyArticles(myData))
	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()

	local result, query = getMyArticles('Eduroam einrichten')
	local data, errors = processMyArticleData(result)
	return data
end

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