2016-12-27 14:58:01 +00:00
|
|
|
<?php
|
|
|
|
|
2018-08-09 09:49:10 +00:00
|
|
|
namespace PortableInfobox\Helpers;
|
2016-12-27 14:58:01 +00:00
|
|
|
|
2018-07-24 13:56:43 +00:00
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
2016-12-27 14:58:01 +00:00
|
|
|
|
2018-08-08 09:31:33 +00:00
|
|
|
class PortableInfoboxTemplateEngine {
|
2018-08-15 14:31:42 +00:00
|
|
|
const CACHE_TTL = 86400;
|
2016-12-27 14:58:01 +00:00
|
|
|
const TYPE_NOT_SUPPORTED_MESSAGE = 'portable-infobox-render-not-supported-type';
|
|
|
|
|
2018-08-09 09:49:10 +00:00
|
|
|
private static $cache = [];
|
2018-08-15 14:31:42 +00:00
|
|
|
private static $memcache;
|
2018-08-09 09:49:10 +00:00
|
|
|
|
2016-12-27 14:58:01 +00:00
|
|
|
protected static $templates = [
|
2018-08-08 09:31:33 +00:00
|
|
|
'wrapper' => 'PortableInfoboxWrapper.hbs',
|
|
|
|
'title' => 'PortableInfoboxItemTitle.hbs',
|
|
|
|
'header' => 'PortableInfoboxItemHeader.hbs',
|
2018-08-12 09:45:29 +00:00
|
|
|
'media' => 'PortableInfoboxItemMedia.hbs',
|
2018-08-08 09:31:33 +00:00
|
|
|
'data' => 'PortableInfoboxItemData.hbs',
|
|
|
|
'group' => 'PortableInfoboxItemGroup.hbs',
|
|
|
|
'smart-group' => 'PortableInfoboxItemSmartGroup.hbs',
|
|
|
|
'horizontal-group-content' => 'PortableInfoboxHorizontalGroupContent.hbs',
|
|
|
|
'navigation' => 'PortableInfoboxItemNavigation.hbs',
|
2018-08-12 09:45:29 +00:00
|
|
|
'media-collection' => 'PortableInfoboxItemMediaCollection.hbs',
|
2018-08-09 09:49:10 +00:00
|
|
|
'xml-parse-error' => 'PortableInfoboxMarkupDebug.hbs'
|
2016-12-27 14:58:01 +00:00
|
|
|
];
|
2018-08-13 14:31:50 +00:00
|
|
|
|
2018-08-15 14:31:42 +00:00
|
|
|
public function __construct() {
|
2018-08-16 09:25:53 +00:00
|
|
|
if ( !isset( self::$memcache ) ) {
|
2018-08-15 14:31:42 +00:00
|
|
|
self::$memcache = \ObjectCache::getMainWANInstance();
|
|
|
|
}
|
|
|
|
}
|
2016-12-27 14:58:01 +00:00
|
|
|
|
|
|
|
public static function getTemplatesDir() {
|
2018-08-16 09:25:53 +00:00
|
|
|
return __DIR__ . '/../../../templates';
|
2016-12-27 14:58:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function getTemplates() {
|
|
|
|
return self::$templates;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function render( $type, array $data ) {
|
2018-09-22 13:31:32 +00:00
|
|
|
global $wgPortableInfoboxUseHeadings;
|
|
|
|
$data['useHeadings'] = $wgPortableInfoboxUseHeadings;
|
|
|
|
|
2018-08-09 09:49:10 +00:00
|
|
|
$renderer = $this->getRenderer( $type );
|
|
|
|
return $renderer( $data );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a template renderer
|
|
|
|
*
|
2018-08-16 09:25:53 +00:00
|
|
|
* @param string $type Template type
|
2018-08-09 09:49:10 +00:00
|
|
|
* @return Closure
|
|
|
|
*/
|
|
|
|
public function getRenderer( $type ) {
|
2018-08-16 09:25:53 +00:00
|
|
|
if ( !empty( self::$cache[$type] ) ) {
|
|
|
|
return self::$cache[$type];
|
2018-08-09 09:49:10 +00:00
|
|
|
}
|
2018-08-12 09:45:29 +00:00
|
|
|
|
2018-09-22 13:31:32 +00:00
|
|
|
$cachekey = self::$memcache->makeKey(
|
|
|
|
__CLASS__, \PortableInfoboxParserTagController::PARSER_TAG_VERSION, $type
|
|
|
|
);
|
2018-08-09 09:49:10 +00:00
|
|
|
|
|
|
|
// @see https://github.com/wikimedia/mediawiki-vendor/tree/master/zordius/lightncandy
|
|
|
|
$renderer = \LightnCandy::prepare(
|
2018-08-15 14:31:42 +00:00
|
|
|
self::$memcache->getWithSetCallback( $cachekey, self::CACHE_TTL, function () use ( $type ) {
|
2018-08-16 09:25:53 +00:00
|
|
|
$path = self::getTemplatesDir() . DIRECTORY_SEPARATOR . static::getTemplates()[$type];
|
2018-08-15 14:31:42 +00:00
|
|
|
|
|
|
|
return \LightnCandy::compile( file_get_contents( $path ), [
|
2018-09-22 13:31:32 +00:00
|
|
|
'flags' => \LightnCandy::FLAG_BESTPERFORMANCE | \LightnCandy::FLAG_PARENT
|
2018-08-15 14:31:42 +00:00
|
|
|
] );
|
|
|
|
} )
|
2018-08-09 09:49:10 +00:00
|
|
|
);
|
|
|
|
|
2018-08-16 09:25:53 +00:00
|
|
|
self::$cache[$type] = $renderer;
|
2018-08-09 09:49:10 +00:00
|
|
|
|
|
|
|
return $renderer;
|
2016-12-27 14:58:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check if item type is supported and logs unsupported types
|
|
|
|
*
|
|
|
|
* @param string $type - template type
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function isSupportedType( $type ) {
|
2018-08-16 09:25:53 +00:00
|
|
|
$result = isset( static::getTemplates()[$type] );
|
2016-12-27 14:58:01 +00:00
|
|
|
if ( !$result ) {
|
2018-09-22 13:31:32 +00:00
|
|
|
LoggerFactory::getInstance( 'PortableInfobox' )->info(
|
|
|
|
self::TYPE_NOT_SUPPORTED_MESSAGE, [ 'type' => $type ]
|
|
|
|
);
|
2016-12-27 14:58:01 +00:00
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|