Version vom 27. Juni 2023, 08:16 Uhr von Oetterer (Diskussion | Beiträge)
This module is rated as ready for general use. It has reached a mature form and is thought to be bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other Wikipedia resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing. |
This module depends on the following other modules: |
Relies on the following templates to display the article list
- {{Os portal listing/table row}}
- {{Os portal listing/table header}}
- {{Os portal listing/table footer}}
Usage[Quelltext bearbeiten]
{{#invoke:Os|main}}
local getArgs = require('Module:Arguments').getArgs
local _INFOBOX = require('Module:Infobox').infobox
local _NAVBOX = require('Module:Navbox os')._main
local _ERROR = require('Module:Error')._error
local _SMWUTIL = require('Module:SmwUtil')
local _TT = require('Module:TableTools')
-- helper vars, standing in for missing config
local category = 'Betriebssysteme'
local osFamilyCategory = 'Betriebssystemfamilien'
local p = {}
-- prename some function
local iosMatcher
-- create the infoxbox for this os
local function compileInfobox(processedArgs)
local osName = mw.title.getCurrentTitle().rootText
osName = iosMatcher(osName)
local release = nil
if processedArgs.year then
local age = tonumber(os.date('%Y')) - processedArgs.year
release = processedArgs.year .. ' (' .. ((age == 1) and 'vor einem Jahr' or 'vor ' .. age .. ' Jahren') .. ')'
end
local count = _SMWUTIL.ask(
{select={'[[is related to os::' .. osName .. ']]', '[[Is disambiguation page::falsch]]'}, fields={}},
{format='count'}
)
local ib_args = {
-- aboveclass = 'objtitle titletext',
above = osName,
image = processedArgs.image,
subheader = 'Betriebssystemfamilie: ' .. processedArgs.family,
caption = osName,
-- headerclass = 'headertext',
labelstyle = 'width: 30%;',
datastyle = 'width: 70%;',
header1 = "Informationen",
label2 = 'Entwickler',
data2 = processedArgs.developer,
label3 = 'Erscheinungsjahr',
data3 = release,
label4 = 'Vorgänger',
data4 = processedArgs.predecessor and '[[' .. processedArgs.predecessor .. ']]',
label5 = 'Nachfolger',
data5 = processedArgs.successor and '[[' .. processedArgs.successor .. ']]',
label6 = 'Anzahl Artikel',
data6 = tostring(count),
}
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
-- executes an smw query
local function executeLegacyQuery(uservalues)
local osName = uservalues.os or mw.title.getCurrentTitle().rootText
local query = {
select = { '[[Is related to os::' .. osName .. ']] ',
'[[Is disambiguation page::falsch]]',
},
fields = {
'?#',
'?Is associated to service#',
'?Is written for target audience#',
'?Is of type#'
}
}
local attributes = {
format='template',
mainlabel='-',
sort='has sortkey',
order='asc',
template = 'Os portal listing/table row',
introtemplate = 'Os portal listing/table header',
outrotemplate = 'Os portal listing/table footer',
default = 'keine vorhanden... :('
}
local rawaskResult = _SMWUTIL.rawask(query, attributes)
return rawaskResult
end
-- helper function to gather all pages in a given category
function getPagesInCategory(category)
if category and #category > 0 then
local result = _SMWUTIL.ask({select = '[[Category:' .. category .. ']]', fields = {}}, { mainlabel = 'pageName' } )
local ret = {}
for _, v in pairs(result) do
if v.pageName and #v.pageName > 0 then
ret[v.pageName] = v.pageName
end
end
return ret
else
return nil
end
end
iosMatcher = function(str)
if str:match('^IOS.*') then
str = 'i' .. str:sub(2)
end
return str
end
-- Here we process the supplied args, sanitize then, check for plausibility and set defaults, if needed
local function processArgs(args)
local args = args or {}
local errors = {}
local processedArgs = {}
-- we need to process the following arguments: family, image, developer, year, predecessor, successor
-- 1st parameter: family
if not args.family then
local message = "Verpflichtender Parameter ''family'' fehlt!"
table.insert(errors, message)
processedArgs.family = _ERROR{message=message}
else
local validOsFamilies = getPagesInCategory(osFamilyCategory)
validOsFamilies['iOS'] = 'iOS' -- pages in this wiki have ucfirst, so this is a hotfix for that
if not validOsFamilies[args.family] then
local message = "Parameter ''family'' hat einen ungültigen Wert: \"" .. args.family .. "\"!"
table.insert(errors, message)
processedArgs.family = _ERROR{message=message}
else
processedArgs.family = '[[' .. args.family .. ']]'
end
end
-- 2nd 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
processedArgs.image = '[[File: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
processedArgs.image = '[[File:' .. suppliedImage .. ']]'
else
table.insert(errors, "Parameter ''image'' hat einen ungültigen Wert: \"" .. image .. "\"!")
processedArgs.image = '[[File:Logo_' .. mw.title.getCurrentTitle().rootText .. '.png]]'
end
end
-- 3rd parameter (OPTIONAL): developer
processedArgs.developer = 'nicht angegeben'
if args.developer and mw.ustring.len(mw.text.trim(args.developer)) then
processedArgs.developer = mw.text.trim(args.developer)
end
-- 4th parameter (OPTIONAL): year
processedArgs.year = nil
if args.year and mw.ustring.len(mw.text.trim(args.year)) then
processedArgs.year = tonumber(mw.text.trim(args.year))
end
-- 5th parameter (OPTIONAL): predecessor
processedArgs.predecessor = nil
if args.predecessor and mw.ustring.len(mw.text.trim(args.predecessor)) then
processedArgs.predecessor = mw.text.trim(args.predecessor)
end
-- 6th parameter (OPTIONAL): successor
processedArgs.successor = nil
if args.successor and mw.ustring.len(mw.text.trim(args.successor)) then
processedArgs.successor = mw.text.trim(args.successor)
end
-- 7th parameter (OPTIONAL|HIDDEN): os
processedArgs.os = mw.title.getCurrentTitle().rootText
if args.os and mw.ustring.len(mw.text.trim(args.os)) then
processedArgs.os = mw.text.trim(args.os)
end
return processedArgs, errors
end
local function storeSementicData(processedArgs)
local smwData = {
['Is member of os family'] = processedArgs.family
}
if processedArgs.year then
smwData['Was released in'] = processedArgs.year
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()
:node(createErrorNode(errors))
end
-- list all articles for this os
output:wikitext(
"Aufgelistet finden Sie hier alle Artikel für das Betriebssystem '''"
.. mw.title.getCurrentTitle().rootText .. "'''.")
:newline()
:newline()
:wikitext(executeLegacyQuery(processedArgs))
-- finally, add the navbox:
:wikitext(_NAVBOX())
-- 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(arg)
return processArgs(arg)
end
return p