Star Citizen Wiki Modul:Comm-Link
Wir laden dich herzlich auf den Star Citizen Wiki Discord Server ein! Du kannst uns auch auf unserem neuen YouTube-Chanel finden!

Modul:Comm-Link

Aus Star Citizen Wiki

Dokumentation unvollständig.


local CommLink = {}

local metatable = {}
local methodtable = {}

metatable.__index = methodtable

local infobox = require( 'Module:Infobox' )
local common = require( 'Module:Common' )

--- Queries the SMW Store and API
function methodtable.loadData( self )
    local commLinkId = mw.title.getCurrentTitle().text

    if self.smwAskResult == nil then
        self.smwAskResult = CommLink.loadSmwData( commLinkId )
    end

    if self.apiResult == nil then
        local metadata = mw.ext.Apiunto.get_comm_link_metadata( tonumber( commLinkId ) )
        self.apiResult = mw.text.jsonDecode( metadata )
    end

    if self.apiResult.meta == nil or self.apiResult.data == nil then
        self.apiResult = nil
        table.insert( self.categories, '[[Category:Comm-Link mit fehlerhaften API-Daten]]' )
        -- error( 'Api Daten nicht valide' )
    end
end

--- Set api result as smw data
function methodtable.setSmwData( self )
    if self.apiResult == nil or self.apiResult.data == nil then
        return
    end

    local result = self.apiResult.data

    local setData = {
        [ 'Comm-Link-Channel' ] = result.channel,
        [ 'Comm-Link-Kategorie' ] = result.category,
        [ 'Comm-Link-Serie' ] = result.series,
        [ 'Comm-Link-ID' ] = tonumber( result.id ),
        [ 'Comm-Link-Titel' ] = result.title,
        [ 'Erstelldatum' ] = result.created_at,
        [ 'Quelle' ] = result.rsi_url,
    }

    local smwResult = mw.smw.set( setData )

    common.checkSmwResult( smwResult )
end

--- Entrypoint for SEO
function methodtable.setSeoData( self )
    if self.apiResult == nil then
        -- Faulty SMW data, don't call #seo
        return
    end

    -- Call to {{#seo:}}
    mw.ext.seo.set{
        author = mw.getCurrentFrame():preprocess( '{{SERVER}}/Benutzer:{{urlencode:{{REVISIONUSER}}|WIKI}}' ),
        section = 'Comm-Link',
        url = tostring( mw.title.getCurrentTitle():fullUrl( '', 'https' ) ),
        title = table.concat( {
            self.apiResult.data.title,
            -- string.format( 'Comm-Link:%s', apiResult.id ),
            mw.getCurrentFrame():preprocess( '{{SITENAME}}' )
        }, ' - '),
        title_mode = 'replace',
        keywords = table.concat( {
            'Comm-Link',
            'Comm Link',
            'Star Citizen',
            'Squadron 42',
            'Cloud Imperium Games',
            'Star Citizen News',
            'Star Citizen Neuigkeiten',
            'Squadron 42 News',
            'Squadron 42 Neuigkeiten',
            self.apiResult.data.channel,
            self.apiResult.data.category,
            self.apiResult.data.series,
        }, ', '),
        -- description = desc,
        locale = 'de_DE',
        type = 'article',
        published_time = self.apiResult.data.created_at,
    }
end

--- Outputs the infobox
function methodtable.infobox( self, frame )
    local result

    self:loadData()

    if self.apiResult == nil and self.smwAskResult == nil then
        table.insert( self.categories, '[[Category:Comm-Link mit fehlenden Daten]]' )
        return ''
    end

    if self.smwAskResult ~= nil and self.smwAskResult.id ~= nil then
        result = self.smwAskResult
    end

    if result == nil and self.apiResult ~= nil then
        result = self.apiResult.data
    end

    -- Use API Title if SMW attribute is missing
    if result.title == nil and self.apiResult.data.title ~= nil then
        result.title = self.apiResult.data.title
    end
    
    if type( result.rsi_url ) == 'table' then
    	result.rsi_url = result.rsi_url[ 1 ] -- thats maybe wrong
    end

    local box = infobox.create( {
        bodyClass = 'floatright',
        allowReplace = true,
        displayPlaceholder = false,
    } )

    -- Image
    box:addImage( common.getImage( {
        frame:getParent().args[ 'image' ] or frame:getParent().args[ 'Bild' ] or nil,
        string.format( "Comm-Link %d Titelbild", result.id ),
    } ), {
        [ 'alternativtext' ] = result.title,
        'rahmenlos',
        '600px'
    } )

    box:addTitle( result.title )
       :addRow( 'ID', result.id, nil, 'col2' )
       :addRow( 'Veröffentlichung', mw.getContentLanguage():formatDate( 'd.m.Y', result.created_at ), nil, 'col2' )
       :addRow( 'Channel', CommLink.processSmwQuery( frame, '[[Comm-Link-Channel::' .. result.channel .. ']]', result.channel ), nil, 'col2' )
       :addRow( 'Kategorie', CommLink.processSmwQuery( frame, '[[Comm-Link-Kategorie::' .. result.category .. ']]', result.category ), nil, 'col2' )
       :addRow( 'Serie', CommLink.processSmwQuery( frame, '[[Comm-Link-Serie::' .. result.series .. ']]', result.series ), nil, 'col2' )
       :addRow( 'RSI', '[' .. result.rsi_url .. ' ' .. result.title .. ']', nil, 'col2' )
       :addRow( 'API', '[https://api.star-citizen.wiki/comm-links/' .. result.id .. '#meta Metadaten]', nil, 'col2' )
       :addRowsFromArgs( require( 'Module:Arguments' ).getArgs( mw.getCurrentFrame() ), '!' )

    self:setSmwData()

    common.setDisplayTitle( frame, 'Comm-Link:' .. result.id .. ' - ' .. result.title )

    return tostring( box )
end

--- Outputs the pagination header
function methodtable.header( self )
    local textPrev = nil
    local textNext = nil

    self:loadData()

    if self.apiResult.meta == nil then
        return ''
    end

    if ( self.apiResult.meta[ 'prev_id' ] ~= -1 ) then
        textPrev = '[[Comm-Link:' .. self.apiResult.meta[ 'prev_id' ] .. '|« Comm-Link:' .. self.apiResult.meta[ 'prev_id' ] .. ']]'
    end

    if ( self.apiResult.meta[ 'next_id' ] ~= -1 ) then
        textNext = '[[Comm-Link:' .. self.apiResult.meta[ 'next_id' ] .. '|Comm-Link:' .. self.apiResult.meta[ 'next_id' ] .. ' »]]'
    end

    local navHeader = require( 'Modul:NavHeader' ):new(
            textPrev,
            '[[Comm-Link:Übersicht|Comm-Link Übersicht]]',
            textNext
    )

    return navHeader:make()
end

--- Static method, queries the smw store for a given page name
---
--- @param page string
--- @return table|nil
function CommLink.loadSmwData( page )
    local result = mw.smw.ask{
        '[[Comm-Link:' .. page .. ']]',
        '?Comm-Link-Channel=channel',
        '?Comm-Link-Kategorie=category',
        '?Comm-Link-Serie=series',
        '?Comm-Link-Titel=title',
        '?Comm-Link-ID=id',
        '?Erstelldatum#ISO=created_at',
        '?Quelle#-=rsi_url'
    }

    if result ~= nil and result[ 1 ] ~= nil then
        return result[ 1 ]
    end

    return nil
end

--- Outputs a link for further results
---
--- @param frame table - The current frame
--- @param type string - SMW Query
--- @param name string - Search Label
function CommLink.processSmwQuery( frame, type, name )
    return frame:callParserFunction{
        name = '#ask:' .. type,
        args = {
            searchlabel = name,
            limit = '0',
            format = 'ul',
            intro = 'Comm-Links mit Attribut "' .. name .. '"'
        }
    }
end

--- Main entry for template calls
--- Outputs header and infobox
function CommLink.output( frame )
    local instance = CommLink:new()

    local output = instance:header() .. instance:infobox( frame ) .. tostring( table.concat( instance.categories ) )
    instance:setSeoData( frame )

    return output
end

--- New Instance
function CommLink.new( self )
    local instance = {
        apiResult = nil,
        smwAskResult = nil,
        categories = {},
    }

    setmetatable( instance, metatable )

    return instance
end

return CommLink
Cookies helfen uns bei der Bereitstellung dieses Wikis. Durch die Nutzung des Star Citizen Wiki erklärst du dich damit einverstanden, dass wir Cookies speichern.