2015-04-27 14:05:31 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
class PortableInfoboxRenderService extends WikiaService {
|
|
|
|
|
const LOGGER_LABEL = 'portable-infobox-render-not-supported-type';
|
2015-05-07 15:16:07 +00:00
|
|
|
|
const DESKTOP_THUMBNAIL_WIDTH = 270;
|
|
|
|
|
const MOBILE_THUMBNAIL_WIDTH = 360;
|
2015-05-21 11:20:57 +00:00
|
|
|
|
// TODO: https://wikia-inc.atlassian.net/browse/MAIN-4601 - request for the missing vignette feature which will
|
|
|
|
|
// allow us to remove THUMBNAIL_HEIGHT from the code. Currently we need this value cause it it impossible to get
|
|
|
|
|
// vignette thumbnail without upsampling only specifying width. The height need to be big enough so each image width
|
|
|
|
|
// will reach our thumbnail width based on its aspect ratio
|
|
|
|
|
|
2015-05-21 09:58:33 +00:00
|
|
|
|
const THUMBNAIL_HEIGHT = 1000;
|
2015-05-12 11:32:42 +00:00
|
|
|
|
const MOBILE_TEMPLATE_POSTFIX = '-mobile';
|
2015-04-27 14:05:31 +00:00
|
|
|
|
|
|
|
|
|
private $templates = [
|
|
|
|
|
'wrapper' => 'PortableInfoboxWrapper.mustache',
|
|
|
|
|
'title' => 'PortableInfoboxItemTitle.mustache',
|
|
|
|
|
'header' => 'PortableInfoboxItemHeader.mustache',
|
|
|
|
|
'image' => 'PortableInfoboxItemImage.mustache',
|
2015-05-12 11:32:42 +00:00
|
|
|
|
'image-mobile' => 'PortableInfoboxItemImageMobile.mustache',
|
2015-05-04 10:48:57 +00:00
|
|
|
|
'data' => 'PortableInfoboxItemData.mustache',
|
2015-04-27 14:05:31 +00:00
|
|
|
|
'group' => 'PortableInfoboxItemGroup.mustache',
|
|
|
|
|
'comparison' => 'PortableInfoboxItemComparison.mustache',
|
|
|
|
|
'comparison-set' => 'PortableInfoboxItemComparisonSet.mustache',
|
|
|
|
|
'comparison-set-header' => 'PortableInfoboxItemComparisonSetHeader.mustache',
|
|
|
|
|
'comparison-set-item' => 'PortableInfoboxItemComparisonSetItem.mustache',
|
|
|
|
|
'footer' => 'PortableInfoboxItemFooter.mustache'
|
|
|
|
|
];
|
|
|
|
|
private $templateEngine;
|
|
|
|
|
|
|
|
|
|
function __construct() {
|
|
|
|
|
$this->templateEngine = ( new Wikia\Template\MustacheEngine )
|
|
|
|
|
->setPrefix( dirname( __FILE__ ) . '/../templates' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* renders infobox
|
|
|
|
|
*
|
|
|
|
|
* @param array $infoboxdata
|
|
|
|
|
* @return string - infobox HTML
|
|
|
|
|
*/
|
2015-05-19 15:20:15 +00:00
|
|
|
|
public function renderInfobox( array $infoboxdata, $theme ) {
|
2015-04-27 14:05:31 +00:00
|
|
|
|
wfProfileIn( __METHOD__ );
|
|
|
|
|
$infoboxHtmlContent = '';
|
|
|
|
|
|
|
|
|
|
foreach ( $infoboxdata as $item ) {
|
|
|
|
|
$data = $item[ 'data' ];
|
|
|
|
|
$type = $item[ 'type' ];
|
|
|
|
|
|
|
|
|
|
switch ( $type ) {
|
|
|
|
|
case 'comparison':
|
|
|
|
|
$infoboxHtmlContent .= $this->renderComparisonItem( $data['value'] );
|
|
|
|
|
break;
|
|
|
|
|
case 'group':
|
|
|
|
|
$infoboxHtmlContent .= $this->renderGroup( $data['value'] );
|
|
|
|
|
break;
|
|
|
|
|
case 'footer':
|
|
|
|
|
$infoboxHtmlContent .= $this->renderItem( 'footer', $data );
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
if ( $this->validateType( $type ) ) {
|
|
|
|
|
$infoboxHtmlContent .= $this->renderItem( $type, $data );
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!empty($infoboxHtmlContent)) {
|
2015-05-19 15:20:15 +00:00
|
|
|
|
$output = $this->renderItem( 'wrapper', [ 'content' => $infoboxHtmlContent, 'theme' => $theme ] );
|
2015-04-27 14:05:31 +00:00
|
|
|
|
} else {
|
|
|
|
|
$output = '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* renders comparison infobox component
|
|
|
|
|
*
|
|
|
|
|
* @param array $comparisonData
|
|
|
|
|
* @return string - comparison HTML
|
|
|
|
|
*/
|
2015-05-04 14:30:40 +00:00
|
|
|
|
private function renderComparisonItem( $comparisonData )
|
|
|
|
|
{
|
2015-04-27 14:05:31 +00:00
|
|
|
|
$comparisonHTMLContent = '';
|
|
|
|
|
|
2015-05-04 14:30:40 +00:00
|
|
|
|
foreach ($comparisonData as $set) {
|
2015-04-27 14:05:31 +00:00
|
|
|
|
$setHTMLContent = '';
|
|
|
|
|
|
2015-05-04 14:30:40 +00:00
|
|
|
|
foreach ($set['data']['value'] as $item) {
|
|
|
|
|
$type = $item['type'];
|
2015-04-27 14:05:31 +00:00
|
|
|
|
|
2015-05-04 14:30:40 +00:00
|
|
|
|
if ($type === 'header') {
|
2015-04-27 14:05:31 +00:00
|
|
|
|
$setHTMLContent .= $this->renderItem(
|
|
|
|
|
'comparison-set-header',
|
2015-05-04 14:30:40 +00:00
|
|
|
|
['content' => $this->renderItem($type, $item['data'])]
|
2015-04-27 14:05:31 +00:00
|
|
|
|
);
|
|
|
|
|
} else {
|
2015-05-04 14:30:40 +00:00
|
|
|
|
if ($this->validateType($type)) {
|
2015-04-27 14:05:31 +00:00
|
|
|
|
$setHTMLContent .= $this->renderItem(
|
|
|
|
|
'comparison-set-item',
|
2015-05-04 14:30:40 +00:00
|
|
|
|
['content' => $this->renderItem($type, $item['data'])]
|
2015-04-27 14:05:31 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$comparisonHTMLContent .= $this->renderItem( 'comparison-set', [ 'content' => $setHTMLContent ] );
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-04 14:30:40 +00:00
|
|
|
|
if ( !empty( $comparisonHTMLContent ) ) {
|
|
|
|
|
$output = $this->renderItem('comparison', [ 'content' => $comparisonHTMLContent ] );
|
|
|
|
|
} else {
|
|
|
|
|
$output = '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $output;
|
2015-04-27 14:05:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* renders group infobox component
|
|
|
|
|
*
|
|
|
|
|
* @param array $groupData
|
|
|
|
|
* @return string - group HTML markup
|
|
|
|
|
*/
|
|
|
|
|
private function renderGroup( $groupData ) {
|
|
|
|
|
$groupHTMLContent = '';
|
|
|
|
|
|
|
|
|
|
foreach ( $groupData as $item ) {
|
|
|
|
|
$type = $item['type'];
|
|
|
|
|
|
|
|
|
|
if ( $this->validateType( $type ) ) {
|
|
|
|
|
$groupHTMLContent .= $this->renderItem( $type, $item['data'] );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->renderItem( 'group', [ 'content' => $groupHTMLContent ] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* renders part of infobox
|
|
|
|
|
*
|
|
|
|
|
* @param string $type
|
|
|
|
|
* @param array $data
|
|
|
|
|
* @return string - HTML
|
|
|
|
|
*/
|
|
|
|
|
private function renderItem( $type, array $data ) {
|
2015-05-06 14:31:57 +00:00
|
|
|
|
//TODO: with validated the performance of render Service and in the next phase we want to refactor it (make
|
|
|
|
|
// it modular) While doing this we also need to move this logic to appropriate image render class
|
2015-05-06 14:02:44 +00:00
|
|
|
|
if ( $type === 'image' ) {
|
2015-05-22 15:27:20 +00:00
|
|
|
|
$data[ 'thumbnail' ] = $this->getThumbnailUrl( $data );
|
2015-05-20 11:56:56 +00:00
|
|
|
|
$data[ 'key' ] = urlencode( $data[ 'key' ] );
|
2015-05-12 11:32:42 +00:00
|
|
|
|
|
2015-05-20 11:56:56 +00:00
|
|
|
|
if ( $this->isWikiaMobile() ) {
|
2015-05-12 11:32:42 +00:00
|
|
|
|
$type = $type . self::MOBILE_TEMPLATE_POSTFIX;
|
|
|
|
|
}
|
2015-05-06 14:02:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-27 14:05:31 +00:00
|
|
|
|
return $this->templateEngine->clearData()
|
|
|
|
|
->setData( $data )
|
|
|
|
|
->render( $this->templates[ $type ] );
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-22 15:27:20 +00:00
|
|
|
|
/**
|
|
|
|
|
* @desc returns the thumbnail url from
|
|
|
|
|
* Vignette or from old service
|
|
|
|
|
* @param string $url
|
|
|
|
|
* @return string thumbnail url
|
|
|
|
|
*/
|
|
|
|
|
protected function getThumbnailUrl( $data ) {
|
|
|
|
|
$url = $data['url'];
|
|
|
|
|
// TODO: remove 'if' condition when unified thumb method
|
|
|
|
|
// will be implemented: https://wikiainc.atlassian.net/browse/PLATFORM1237
|
2015-05-22 15:46:54 +00:00
|
|
|
|
if ( VignetteRequest::isVignetteUrl( $url ) ) {
|
2015-05-22 16:25:21 +00:00
|
|
|
|
return $this->createVignetteThumbnail( $url );
|
2015-05-22 15:27:20 +00:00
|
|
|
|
} else {
|
2015-05-22 16:25:21 +00:00
|
|
|
|
return $this->createOldThumbnail( $data['name'] );
|
2015-05-22 15:27:20 +00:00
|
|
|
|
}
|
2015-05-22 16:25:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $url
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function createVignetteThumbnail( $url ) {
|
|
|
|
|
return VignetteRequest::fromUrl( $url )
|
|
|
|
|
->thumbnailDown()
|
2015-05-22 14:32:02 +00:00
|
|
|
|
->width( $this->isWikiaMobile() ?
|
2015-05-22 16:25:21 +00:00
|
|
|
|
self::MOBILE_THUMBNAIL_WIDTH :
|
|
|
|
|
self::DESKTOP_THUMBNAIL_WIDTH
|
|
|
|
|
)
|
|
|
|
|
->height( self::THUMBNAIL_HEIGHT )
|
|
|
|
|
->url();
|
2015-05-22 15:27:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @desc If the image is served from an old
|
|
|
|
|
* service we have to again obtain file to
|
|
|
|
|
* call the createThumb function
|
2015-05-25 11:02:35 +00:00
|
|
|
|
* @param $title
|
2015-05-22 15:27:20 +00:00
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2015-05-25 11:02:35 +00:00
|
|
|
|
private function createOldThumbnail( $title )
|
2015-05-22 15:27:20 +00:00
|
|
|
|
{
|
2015-05-25 11:02:35 +00:00
|
|
|
|
$file = \WikiaFileHelper::getFileFromTitle( $title );
|
2015-05-22 16:25:21 +00:00
|
|
|
|
if ( $file ) {
|
|
|
|
|
return $file->createThumb(
|
2015-05-25 11:02:35 +00:00
|
|
|
|
F::app()->checkSkin( 'wikiamobile' ) ?
|
2015-05-22 16:25:21 +00:00
|
|
|
|
self::MOBILE_THUMBNAIL_WIDTH :
|
|
|
|
|
self::DESKTOP_THUMBNAIL_WIDTH
|
2015-05-22 15:27:20 +00:00
|
|
|
|
);
|
2015-05-22 16:25:21 +00:00
|
|
|
|
}
|
|
|
|
|
return '';
|
2015-05-26 11:07:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-05-26 10:57:00 +00:00
|
|
|
|
/**
|
2015-05-20 11:56:56 +00:00
|
|
|
|
* required for testing mobile template rendering
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
protected function isWikiaMobile() {
|
|
|
|
|
return F::app()->checkSkin( 'wikiamobile' );
|
2015-05-07 12:36:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-27 14:05:31 +00:00
|
|
|
|
/**
|
|
|
|
|
* check if item type is supported and logs unsupported types
|
|
|
|
|
*
|
|
|
|
|
* @param string $type - template type
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
private function validateType( $type ) {
|
|
|
|
|
$isValid = true;
|
|
|
|
|
|
|
|
|
|
if ( !isset( $this->templates[ $type ] ) ) {
|
|
|
|
|
Wikia\Logger\WikiaLogger::instance()->info( self::LOGGER_LABEL, [
|
|
|
|
|
'type' => $type
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$isValid = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $isValid;
|
|
|
|
|
}
|
|
|
|
|
}
|