local CommLink = {}
local metatable = {}
local methodtable = {}
metatable.__index = methodtable
local infobox = require( 'Module:Infobox' )
local common = require( 'Module:Common' )
local TNT = require( 'Module:TNT' )
local localized = require( 'Module:Localized' )
local data = mw.loadData( 'Module:Comm-Link/Data' )
--- Calls TNT with the given key
local function translate( lang, key, prefix )
prefix = prefix or 'lbl_'
return TNT.formatInLanguage( lang, 'I18n/Module:Comm-Link', 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 ) )
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 )
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
-- 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,
tableAttributes = {
vocab = 'https://schema.org/',
typeof = 'schema:Blog',
},
} )
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:addImage( common.getImage( image ), {
[ 'alternativtext' ] = result.title,
'rahmenlos',
'600px'
} )
box:addTitle( result.title, nil, nil, { data = { property = 'schema:name' } } )
:addRow( 'ID', result.id, nil, 'col2', nil, nil, { data = { property = 'schema:identifier' } } )
:addRow( translate( self.lang, 'publication' ), mw.getContentLanguage():formatDate( 'd.m.Y', result.created_at ), nil, 'col2', nil, nil, { data = { property = 'schema:datePublished' } } )
:addRow( translate( self.lang, 'channel' ), CommLink.processSmwQuery( frame, '[[Comm-Link-Channel::' .. result.channel .. ']]', result.channel ), nil, 'col2', nil, nil, { data = { property = 'schema:genre' } } )
:addRow( translate( self.lang, 'category' ), CommLink.processSmwQuery( frame, '[[Comm-Link-Kategorie::' .. result.category .. ']]', result.category ), nil, 'col2' )
:addRow( translate( self.lang, 'series' ), CommLink.processSmwQuery( frame, '[[Comm-Link-Serie::' .. result.series .. ']]', result.series ), nil, 'col2', nil, nil, { data = { property = 'schema:isPartOf' } } )
:addRow( 'RSI', '[' .. result.rsi_url .. ' ' .. result.title .. ']', nil, 'col2', nil, nil, { data = { property = 'schema:author' } } )
:addRow( 'API', '[https://api.star-citizen.wiki/rsi/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 = '[[Special:MyLanguage/Comm-Link:' .. self.apiResult.meta[ 'prev_id' ] .. '|« Comm-Link:' .. self.apiResult.meta[ 'prev_id' ] .. ']]'
end
if ( self.apiResult.meta[ 'next_id' ] ~= -1 ) then
textNext = '[[Special:MyLanguage/Comm-Link:' .. self.apiResult.meta[ 'next_id' ] .. '|Comm-Link:' .. self.apiResult.meta[ 'next_id' ] .. ' »]]'
end
local navHeader = require( 'Modul:NavHeader' ):new(
textPrev,
'[[Special:MyLanguage/Comm-Link:Übersicht|' .. translate( self.lang, 'comm_link_main', 'txt_' ) .. ']]',
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