Modul:Comm-Link

Aus Star Citizen Wiki
Die druckbare Version wird nicht mehr unterstützt und kann Darstellungsfehler aufweisen. Bitte aktualisiere deine Browser-Lesezeichen und verwende stattdessen die Standard-Druckfunktion des Browsers.
Modul Dokumentation[view][edit][history][purge]
Diese Dokumentation wird von Modul:Comm-Link/doc aus übernommen. Änderungen können auf der Diskussionsseite vorgeschlagen werden.

Modulinfo

Dieses Modul setzt die Vorlage:Comm-Link um. Anweisungen zur Verwendung findest du auf der Vorlagenseite.

Die Daten werden automatisiert in der Star Citizen Wiki API abgerufen.


local CommLink = {}

local metatable = {}
local methodtable = {}

metatable.__index = methodtable

local infobox = require( 'Module:InfoboxNeue' )
local common = require( 'Module:Common' )
local TNT = require( 'Module:Translate' ):new()
local localized = require( 'Module:Localized' )
local data = mw.loadJsonData( 'Module:Comm-Link/data.json' )

--- Calls TNT with the given key
local function translate( lang, key, prefix )
    prefix = prefix or 'lbl_'
    return TNT.formatInLanguage( lang, 'Module:Comm-Link/i18n.json', prefix .. key )
end

--- Queries the SMW Store and API
function methodtable.loadData( self )
    local commLinkId = mw.title.getCurrentTitle().rootText
    commLinkId = mw.text.split( commLinkId, '/', true )[1]

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

    if self.apiResult == nil then
        local metadata = mw.ext.Apiunto.get_raw( 'v2/comm-links/' .. tonumber( commLinkId ) )
        local success, apiResult = pcall( mw.text.jsonDecode, metadata )
        if success then
        	self.apiResult = apiResult
		end
    end

    if self.apiResult == nil or 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 )
	local title = mw.title.getCurrentTitle()
    if self.apiResult == nil or self.apiResult.data == nil or title.isSubpage 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

	local locale = 'de_DE'
	local title = mw.title.getCurrentTitle()
	if title.isSubpage then
		if title.subpageText == 'en' then
			locale = 'en_EN'
		end
	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 = locale,
        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 self.lang == 'de' then
	    table.insert( self.categories, '[[Category:Comm-Link]]' )
    else
	    table.insert( self.categories, '[[Category:Comm-Link ' .. self.smwAskResult.id .. ']]' )
    end

    if result == nil and self.apiResult ~= nil then
        result = self.apiResult.data
    end
    
    if result == nil then
    	return ''
	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:new()

	local image = {
        frame:getParent().args[ 'image' ] or frame:getParent().args[ 'Bild' ] or nil,
        string.format( "Comm-Link %d Titelbild", result.id ),
    }

	for series, seriesImage in pairs( data.imageMap ) do
		if string.match( result.title, series ) ~= nil then
			image = seriesImage
			break
		end
	end

    -- Image
    box:renderImage( common.getImage( image ) )

    box:renderHeader( { title = string.format( '%s <span class="infobox__desc">(%s)</span>', result.title, result.id ) } )

    box:renderSection( {
       		col = 2,
       		content = {
       			box:renderItem( translate( self.lang, 'publication' ), mw.getContentLanguage():formatDate( 'd.m.Y', result.created_at ) ),
       			box:renderItem( translate( self.lang, 'channel' ), CommLink.processSmwQuery( frame, '[[Comm-Link-Channel::' .. result.channel .. ']]', result.channel ) ),
       			box:renderItem( translate( self.lang, 'category' ), CommLink.processSmwQuery( frame, '[[Comm-Link-Kategorie::' .. result.category .. ']]', result.category ) ),
       			box:renderItem( translate( self.lang, 'series' ), CommLink.processSmwQuery( frame, '[[Comm-Link-Serie::' .. result.series .. ']]', result.series ) ),
       			box:renderItem( 'RSI', '[' .. result.rsi_url .. ' ' .. result.title .. ']' ),
       			box:renderItem( 'API', '[https://api.star-citizen.wiki/rsi/comm-links/' .. result.id .. '#meta Metadaten]' )
       		}
       } )

    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 == nil or self.apiResult.meta == nil then
        return ''
    end

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

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

    local navHeader = require( 'Modul:Prevnext' ):new({
    	prev = textPrev,
    	title = '[[Comm-Link:Übersicht]]',
    	['next'] = 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 = {},
        lang = require( 'Module:Template translation' )._getLanguageSubpage( mw.title.getCurrentTitle() )
    }

    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.