Das Star Citizen Wiki wird von Leuten wie dir gemacht! Bitte überleg doch bei uns mitzuwirken, wir können viel Hilfe gebrauchen!

Modul:Comm-Link: Unterschied zwischen den Versionen

Aus Star Citizen Wiki
K (change api link)
K (table style)
Zeile 111: Zeile 111:
 
         title = result.title,
 
         title = result.title,
 
         bodyClass = 'wikitable',
 
         bodyClass = 'wikitable',
        bodyStyle = 'width: auto; max-width: 300px',
 
 
     } )
 
     } )
 
         :addRow( 'ID', result.id )
 
         :addRow( 'ID', result.id )

Version vom 14. Oktober 2020, 21:30 Uhr

Dokumentation unvollständig.


local p = {}

local capiunto = require( 'capiunto' )
local common = require( 'Module:Common' )

local apiResult
local smwAskResult
local categories = {}

--
local function 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

--
local function loadData()
    local commLinkId = mw.title.getCurrentTitle().text

    if smwAskResult == nil then
        smwAskResult = loadSmwData( commLinkId )
    end

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

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

--
local function setSmwData()
    if apiResult == nil or apiResult.data == nil then
        return
    end

    local result = 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

--
local function processSmwQuery( frame, type, name )
    return frame:callParserFunction{
        name = '#ask:' .. type,
        args = {
            searchlabel = name,
            limit = '0',
            format = 'ul',
            intro = 'Comm-Links mit Attribut "' .. name .. '"'
        }
    }
end

-- Only Infobox
function p.infobox( frame )
    local result

    loadData()

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

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

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

    -- Use API Title if SMW attribute is missing
    if result.title == nil and apiResult.data.title ~= nil then
        result.title = apiResult.data.title
    end

    local infoBox = capiunto.create( {
        title = result.title,
        bodyClass = 'wikitable',
    } )
        :addRow( 'ID', result.id )
        :addRow( 'Veröffentlichung', mw.getContentLanguage():formatDate( 'd.m.Y', result.created_at ) )
        :addRow( 'Channel', processSmwQuery( frame, '[[Comm-Link-Channel::' .. result.channel .. ']]', result.channel ) )
        :addRow( 'Kategorie', processSmwQuery( frame, '[[Comm-Link-Kategorie::' .. result.category .. ']]', result.category ) )
        :addRow( 'Serie', processSmwQuery( frame, '[[Comm-Link-Serie::' .. result.series .. ']]', result.series ) )
        :addRow( 'RSI', '[' .. result.rsi_url .. ' ' .. result.title .. ']' )
        :addRow( 'API', '[https://api.star-citizen.wiki/comm-links/' .. result.id .. '#meta Metadaten]' )

    setSmwData()

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

    return tostring( infoBox )
end

-- Only Pagination Header
function p.header()
    local textPrev = ''
    local textNext = ''

    loadData()

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

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

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

    local div = mw.html.create( 'div' )
    div
        :attr( 'class', 'comm-link-header _wikitable' )
        :tag( 'span' )
            :wikitext( textPrev )
            :done()
        :tag( 'span' )
            :wikitext( '[[Comm-Link:Übersicht|Comm-Link Übersicht]]' )
            :done()
        :tag( 'span' )
            :wikitext( textNext )
            :done()

    return tostring( div )
end

-- Return Comm-Link Header and Infobox
function p.output( frame )
    return p.header( frame ) .. p.infobox( frame ) .. tostring( table.concat( categories ) )
end

return p