Modul:Comm-Link

Aus Star Citizen Wiki

Die Dokumentation für dieses Modul kann unter Modul:Comm-Link/Doku erstellt werden

local p = {}

local capiunto = require( 'capiunto' )

local apiResult

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

    local smwResult = mw.smw.set( setData )

    if smwResult ~= true and smwResult.error ~= nil then
        error( 'Semantic Mediawiki Fehler ' .. smwResult.error )
    end
end

--
local function makeTitle( frame, result )
	return frame:callParserFunction( 'DISPLAYTITLE', 'Comm-Link:' .. result.id .. ' - ' .. result.title )
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

    if ( apiResult == nil ) then
        local smwResult = mw.ext.Apiunto.get_comm_link_metadata( tonumber( mw.title.getCurrentTitle().text ) )
        apiResult = mw.text.jsonDecode( smwResult )
    end

	if apiResult.data == nil then
		error( 'Api Daten nicht valide' )
	else
		result = apiResult.data
	end

    local infoBox = capiunto.create( {
        title = result.title,
        bodyClass = 'wikitable',
        bodyStyle = 'width: auto; max-width: 300px',
    } )
    :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/rsi/comm-links/' .. result.id .. '#meta Metadaten]' )

    setSmwData( result )

    return tostring( infoBox ) .. makeTitle( frame, result )

end

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

    if ( apiResult == nil ) then
        local smwResult = mw.ext.Apiunto.get_comm_link_metadata( tonumber( mw.title.getCurrentTitle().text ) )
        apiResult = mw.text.jsonDecode( smwResult )
    end

	if apiResult.meta == nil then
		error( 'Api Daten nicht valide' )
	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 )
end

return p