Modul:Information: Unterschied zwischen den Versionen

Aus Steel Beasts Wiki
imported>FoXFTW
KKeine Bearbeitungszusammenfassung
 
K (1 Version importiert)
 

Aktuelle Version vom 3. September 2023, 08:02 Uhr

Die Dokumentation für dieses Modul kann unter Modul:Information/Doku erstellt werden

local Information = {}

local metatable = {}
local methodtable = {}

metatable.__index = methodtable

--- Settings
local minDescriptionLength = 10
local authorRsi = 'Roberts Space Industries International Ltd.'
local warnings = {
    dateM = {
        text = 'Das Erstelldatum dieser Datei fehlt',
        type = 'info',
    },
    author = {
        text = 'Für diese Datei wurde kein Autor angegeben. Falls du den Autor der Quelle kennst, füge diesen der Seite gerne hinzu!',
        type = 'info',
    },
    source = {
        text = 'Dieser Datei fehlt die Quellenangabe. Falls du den Link der Quelle kennst, füge diesen der Seite gerne hinzu!',
        type = 'info',
    },
    sourceTooFew = {
        text = 'Dieser Datei scheint der Direktlink, oder der Link zur Quellseite, die die Datei verwendet, zu fehlen.',
        type = 'info',
    },
    description = {
        text = 'Diese Datei besitzt keine Beschreibung. Füge gerne eine kurze Beschriebung der Seite hinzu!',
        type = 'info',
    },
    descriptionTooShort = {
        text = 'Die Beschreibung der Datei ist zu kurz. Sie sollte mindestens 10 Zeichen enthalten.',
        type = 'info',
    },
}


--- Helper function checking if a string contains 'robertsspaceindustries'
--- Used to detect whether a source url is from an official site
---
--- @param source string - URL
---
--- @return boolean - True if string matches
local function checkSourceRsi( source )
    source = source or ''

    return string.match( source, 'robertsspaceindustries' )
end


--- Helper function checking if a string is handled as an official author
---
--- @param author string - Author
---
--- @return boolean - True if string matches
local function checkAuthorRsi( author )
    author = author or ''

    return string.match( author, authorRsi ) or author == authorRsi or string.match( author, 'RSI' )
end


--- Initializes the instance
---
--- @param frame table MW Frame
function methodtable.init( self, frame )
    self.frame = frame
    self.args = require('Module:Arguments').getArgs( frame )

    if type(self.args.source) ~= 'string' then
        self.args.source = ''
    end
end


--- Processes the date argument if present
--- Adds a maintenance category otherwise
function methodtable.processDate( self )
    if type( self.args.date ) == 'string' then
        self.smwData['Erstelldatum'] = self.args.date
    else
        self.showWarnings.date = true
        table.insert( self.categories, '[[Kategorie:Datei mit fehlendem Datum]]' )
    end
end


--- Processes the source argument if present
--- Adds a maintenance category otherwise
function methodtable.processSource( self )
    if type( self.args.source ) ~= 'string' then
        self.showWarnings.source = true
        table.insert( self.categories, '[[Kategorie:Datei mit fehlender Quelle]]' )
        return
    end

    local sources = mw.text.split( mw.text.trim( self.args.source ), ',')
    if type( sources ) == 'string' then
    	sources = mw.text.split( mw.text.trim( self.args.source ), ' ' )
    end

    local linkSources = {}

    for _, source in pairs( sources ) do
        source = self.frame:preprocess( source )

        -- If Source is RSI, hardcode Author
        -- But only add hardcoded author once
        if checkSourceRsi( source ) and self.authorAddedThroughSource == false then
            table.insert( self.smwData['Autor'], authorRsi )
            self.authorAddedThroughSource = true
        end

        -- Only add Links to SMW
        if string.match( source, 'http' ) then
            table.insert( linkSources, source )
        end
    end

    -- Files from RSI should have at least two sources
    -- 1. The direct link to the file
    -- 2. One ore more links to pages where this file is used
    if #linkSources < 2 and ( self.authorAddedThroughSource or self.args.sources == 'RSI' ) then
        self.showWarnings.sourceTooFew = true
        table.insert( self.categories, '[[Kategorie:Datei mit fehlender Quelle]]' )
    end

    self.smwData['Quelle'] = linkSources
end


--- Processes the author argument if present
--- Adds a maintenance category otherwise
function methodtable.processAuthor( self )
    if type( self.args.author ) ~= 'string' then
        self.showWarnings.author = true
        table.insert( self.categories, '[[Kategorie:Datei mit fehlendem Autor]]' )
        return
    end

    local authors = mw.text.split( self.args.author, ',' )

    for _, author in pairs( authors ) do
        if checkAuthorRsi( author ) then
            -- Check if Source is a link and not an own upload
            if string.match( self.args.source, 'http' ) == nil and not string.match( self.args.source, 'own' ) == nil then
                self.showWarnings.source = true
                table.insert( self.categories, '[[Kategorie:RSI Datei mit fehlender Quelle]]' )
            end

            if self.authorAddedThroughSource == false then
                -- Use Hardcoded RSI Author String
                table.insert( self.smwData['Autor'], authorRsi )
            end
        else
            table.insert( self.smwData['Autor'], self.frame:preprocess( author ) )
        end
    end
end


--- Processes the description argument if present
--- Adds a maintenance category otherwise
--- Also adds a maintenance category if description is too short
function methodtable.processDescription( self )
    if type( self.args.description ) ~= 'string' then
        self.showWarnings.description = true
        table.insert( self.categories, '[[Kategorie:Datei mit fehlender Beschreibung]]' )
        return
    end

    self.args.description = self.frame:preprocess( self.args.description )
    self.smwData['Beschreibung'] = self.args.description .. '@de'

    if mw.ustring.len( self.args.description ) < minDescriptionLength then
        self.showWarnings.descriptionTooShort = true
        table.insert( self.categories, '[[Kategorie:Datei mit zu kurzer Beschreibung]]' )
    end
end


--- Calls all processing methods and adds data to SMW
function methodtable.process( self )
    self:processDate()
    self:processSource()
    self:processAuthor()
    self:processDescription()

    mw.smw.set( self.smwData )
end


--- Creates a smw info tooltip if a warning should be displayed to the user
---
--- @param warningType string
---
--- @return string
function methodtable.getWarning( self, warningType )
    if self.showWarnings[ warningType ] == nil or self.showWarnings[ warningType ] == false then
        return ''
    end

    return mw.smw.info( warnings[ warningType ].text, warnings[ warningType ].type )
end


--- Outputs the info table
function methodtable.getOutput( self )
    local source = ''

    if #self.smwData['Quelle'] == 0 then
        source = self.args.source
    else
        source = table.concat( self.smwData['Quelle'], '<br>' )
    end

    local infoTable = mw.html.create('table')

    infoTable
            :addClass('wikitable')
            :addClass('fileinfotpl-type-information')
            :css('width', 'auto')
            -- Date
            :tag('tr')
                :tag('th')
                    :attr('id', 'fileinfotpl_date')
                    :wikitext( 'Erstelldatum:' )
                :done()
                :tag('td')
                    :wikitext( mw.getContentLanguage():formatDate( 'd.m.Y', self.smwData['Erstelldatum'] ) .. self:getWarning( 'date' ) )
                :done()
            :done()
            -- Source(s)
            :tag('tr')
                :tag('th')
                    :attr('id', 'fileinfotpl_src')
                    :wikitext( 'Quelle:' )
                :done()
                :tag('td')
                    :wikitext( ( source or 'FEHLEND' ) .. self:getWarning( 'source' ) .. self:getWarning( 'sourceTooFew' ) )
                :done()
            :done()
            -- Author(s)
            :tag('tr')
                :tag('th')
                    :attr('id', 'fileinfotpl_aut')
                    :wikitext('Autor:')
                :done()
                :tag('td')
                    :wikitext( table.concat( self.smwData['Autor'], '<br>' ) .. self:getWarning( 'author' ) )
                    :done()
            :done()
            -- Description
            :tag('tr')
                :tag('th')
                    :attr('id', 'fileinfotpl_desc')
                    :wikitext( 'Beschreibung:' )
                :done()
                :tag('td')
                    :wikitext( self.smwData['Beschreibung'] .. self:getWarning( 'description' ) .. self:getWarning( 'descriptionTooShort' ) )
                :done()
            :done()

    return tostring( infoTable:allDone() ) .. table.concat( self.categories, '' )
end


--- New Instance
function Information.new()
    local instance = {
        frame = {},
        args = {},
        smwData = {
            ['Erstelldatum'] = '',
            ['Quelle'] = {},
            ['Autor'] = {},
            ['Beschreibung'] = '',
        },
        showWarnings = {
            date = false,
            author = false,
            source = false,
            sourceTooFew = false,
            description = false,
            descriptionTooShort = false,
        },
        categories = {},
        authorAddedThroughSource = false
    }

    setmetatable( instance, metatable )

    return instance
end


--- Parser Call
function Information.output( frame )
    local instance = Information:new()
    instance:init( frame )
    instance:process()

    return instance:getOutput()
end

return Information