Modul:FeaturedArticle

Aus Steel Beasts Wiki

Die Dokumentation für dieses Modul kann unter Modul:FeaturedArticle/Doku erstellt werden

local FeaturedArticle = {}

local metatable = {}
local methodtable = {}

metatable.__index = methodtable

local rubrikContainer = require( 'Module:RubrikContainer' )

local fallbackImage = 'Datei:Platzhalter.webp'

--- Queries the page image and display title from SMW for the given options
--- @return table|nil
function methodtable.getSmwData( self, restrictions )
	if type( restrictions ) ~= 'table' then
		restrictions = { restrictions }
	end

	local query = {
		'?#-=page',
		'?Page Image#-=image',
		'?Anzeigetitel#-=title',
		'limit=1',
	}

	for _, v in pairs( restrictions )  do
		table.insert( query, 1, v )
	end

	query = mw.smw.ask( query )
	
	if type( query ) ~= 'table' or #query == 0 then
		return nil
	end
	
	return query[1]
end


--- Queries SMW for the newest Comm-Link
--- @return table|nil
function methodtable.loadNewestCommLink( self )
	local query = {
		'[[Kategorie:Comm-Link|+depth=0]]',
		'[[Comm-Link-Kategorie::+]]',
		'Comm-Link-ID',
		'sort=Erstelldatum,Comm-Link-ID',
		'order=desc,desc'
	}

	return self:getSmwData( query )
end


--- Creates a card from the given data
--- @return string
function methodtable.getOutput( self )
	local smwData = nil
	local content = 'Der neueste Comm-Link'
	
	local newStructure = self.frameArgs[ 'New' ] ~= nil

	if self.frameArgs[ 'Titel' ] ~= nil then
		smwData = self:getSmwData( '[[' .. self.frameArgs[ 'Titel' ] .. ']]' )
		content = 'Vorgestellter Artikel'
	else
		smwData = self:loadNewestCommLink()
	end

	if smwData == nil then
        return '<div class="mw-message-box mw-message-box-warning">Konnte keine Daten aus dem Semantic Wiki laden.</div>'
	end

	if smwData[ 'image' ] == nil then
		smwData[ 'image' ] = fallbackImage
	end

	local title = smwData[ 'page' ]
	if smwData[ 'title' ] ~= nil then
		title = smwData[ 'title' ]
	end

	if newStructure then
		local container = mw.html.create('div')

		container
			:attr('id','home-featured')
			:addClass( 'home-card' )
			:addClass( 'home-card--button' )
			:addClass( 'home-card--col2' )
			:addClass( 'home-card--row3' )
			:tag( 'div' )
				:addClass( 'home-card__background' )
				:wikitext( self.currentFrame:preprocess( string.format( '[[%s|x200px|link={{Startseite/Einstellungen|featured}}]]', smwData['image'] ) ) )
				:done()
			:tag( 'div' )
				:addClass( 'home-card__foreground' )
				:tag( 'div' )
				:addClass( 'home-card__label' )
				:wikitext( content )
				:done()
			:tag( 'div' )
				:addClass( 'home-card__header' )
				:wikitext( title )
				:done()
			:allDone()
		
		return tostring( container )
	else
		local container = rubrikContainer:new()
		container:setArgs({
			containerClass = 'category-container--featured-article',
			containerSize = self.frameArgs[ 'Größe' ] or '9',
	
			title = string.format( '[[%s|%s]]', smwData[ 'page' ], title ),
	        title_subtext = content,
			titlePosition = 'category-container--title-prominent',
	
			image = self.frameArgs[ 'Bild' ] or smwData[ 'image' ],
	        imageLink = smwData[ 'page' ],
		})
	
	    return container:getContainer()
	end
end


--- Set the frame and load args
--- @param frame table
function methodtable.setFrame( self, frame )
    self.currentFrame = frame
    self.frameArgs = require( 'Module:Arguments' ).getArgs( frame )
end


--- Template entry
function FeaturedArticle.main( frame )
    local instance = FeaturedArticle:new()
    instance:setFrame( frame )

    return tostring( instance:getOutput() )
end


--- New Instance
function FeaturedArticle.new( self )
    local instance = {}

    setmetatable( instance, metatable )

    return instance
end


return FeaturedArticle