PortableInfobox/services/Helpers/PortableInfoboxRenderServiceHelper.php

224 lines
5.7 KiB
PHP
Raw Normal View History

<?php
namespace Wikia\PortableInfobox\Helpers;
use \Wikia\Logger\WikiaLogger;
class PortableInfoboxRenderServiceHelper {
const LOGGER_LABEL = 'portable-infobox-render-not-supported-type';
2015-07-29 11:51:55 +00:00
//todo: https://wikia-inc.atlassian.net/browse/DAT-3075
//todo: figure out what to do when user changes default infobox width via custom theming
const DESKTOP_THUMBNAIL_WIDTH = 270;
const MOBILE_THUMBNAIL_WIDTH = 360;
const MINIMAL_HERO_IMG_WIDTH = 300;
2015-09-09 08:16:00 +00:00
const MAX_DESKTOP_THUMBNAIL_HEIGHT = 500;
2015-09-09 11:49:16 +00:00
function __construct() {}
/**
* creates special data structure for horizontal group from group data
*
* @param array $groupData
* @return array
*/
public function createHorizontalGroupData( $groupData ) {
2015-09-08 14:29:41 +00:00
$horizontalGroupData = [
'labels' => [],
'values' => [],
'renderLabels' => false
];
foreach ( $groupData as $item ) {
2015-09-09 08:01:24 +00:00
$data = $item[ 'data' ];
2015-09-09 08:01:24 +00:00
if ( $item[ 'type' ] === 'data' ) {
array_push( $horizontalGroupData[ 'labels' ], $data[ 'label' ] );
array_push( $horizontalGroupData[ 'values' ], $data[ 'value' ] );
2015-09-09 08:01:24 +00:00
if ( !empty( $data[ 'label' ] ) ) {
$horizontalGroupData[ 'renderLabels' ] = true;
2015-09-08 14:29:41 +00:00
}
2015-09-09 14:00:19 +00:00
} else if ( $item[ 'type' ] === 'header' ) {
2015-09-09 14:00:59 +00:00
$horizontalGroupData[ 'header' ] = $data[ 'value' ];
}
}
return $horizontalGroupData;
}
/**
2015-11-03 13:45:27 +00:00
* check if infobox item is a title, title inside the hero module or a label
* and if so, remove from it all HTML tags.
*
* @param string $type type of infobox item
* @param array $data infobox item data
* @return array infobox $data with sanitized title param if needed
*/
2015-11-03 13:45:27 +00:00
public function sanitizeTitlesAndLabels( $type, $data ) {
if ( $type === 'title' ) {
$data[ 'value' ] = $this->sanitizeElementData( $data[ 'value' ] );
} else if ( $type === 'data' ) {
2015-11-03 15:12:44 +00:00
$data[ 'label' ] = $this->sanitizeElementData( $data[ 'label' ], '<a>' );
2015-11-03 13:45:27 +00:00
} else if ( $type === 'hero-mobile' ) {
$data[ 'title' ][ 'value' ] = $this->sanitizeElementData( $data[ 'title' ][ 'value' ] );
}
return $data;
}
2015-11-03 13:45:27 +00:00
/**
* process single title or label
*
* @param $elementText
2015-11-03 15:58:25 +00:00
* @param string $allowedTags
2015-11-03 13:45:27 +00:00
* @return string
*/
private function sanitizeElementData( $elementText, $allowedTags = null ) {
if ( !empty( $elementText ) ) {
$elementTextAfterTrim = trim( strip_tags( $elementText, $allowedTags ) );
2015-11-03 15:58:25 +00:00
if ( $elementTextAfterTrim !== $elementText ) {
WikiaLogger::instance()->info( 'Striping HTML tags from infobox element' );
$elementText = $elementTextAfterTrim;
2015-11-03 15:58:25 +00:00
}
2015-11-03 13:45:27 +00:00
}
return $elementText;
2015-11-03 13:45:27 +00:00
}
/**
* extends image data
*
* @param array $data
*
* @return bool|array
*/
public function extendImageData( $data ) {
2015-09-09 08:01:24 +00:00
$thumbnail = $this->getThumbnail( $data[ 'name' ] );
2015-09-28 15:46:40 +00:00
$ref = null;
2015-09-08 14:29:41 +00:00
if ( !$thumbnail ) {
return false;
}
wfRunHooks( 'PortableInfoboxRenderServiceHelper::extendImageData', [ $data, &$ref ] );
2015-09-28 15:46:40 +00:00
$data[ 'ref' ] = $ref;
2015-09-09 08:01:24 +00:00
$data[ 'height' ] = $thumbnail->getHeight();
$data[ 'width' ] = $thumbnail->getWidth();
$data[ 'thumbnail' ] = $thumbnail->getUrl();
$data[ 'key' ] = urlencode( $data[ 'key' ] );
$data[ 'media-type' ] = $data[ 'isVideo' ] ? 'video' : 'image';
return $data;
}
/**
* checks if infobox data item is valid hero component data.
* If image is smaller than MINIMAL_HERO_IMG_WIDTH const, doesn't render the hero module.
*
* @param array $item - infobox data item
* @param array $heroData - hero component data
*
* @return bool
*/
public function isValidHeroDataItem( $item, $heroData ) {
2015-09-09 08:01:24 +00:00
$type = $item[ 'type' ];
if ( $type === 'title' && !array_key_exists( 'title', $heroData ) ) {
return true;
}
if ( $type === 'image' && !array_key_exists( 'image', $heroData ) && count( $item[ 'data' ] ) === 1 ) {
$imageWidth = $this->getFileWidth( $item[ 'data' ][ 0 ][ 'name' ] );
if ( $imageWidth >= self::MINIMAL_HERO_IMG_WIDTH ) {
return true;
}
}
return false;
}
/**
* required for testing mobile template rendering
* @return bool
*/
public function isWikiaMobile() {
return \F::app()->checkSkin( 'wikiamobile' );
}
/**
* check if item type is supported and logs unsupported types
*
* @param string $type - template type
* @param array $templates - array of supported templates
*
* @return bool
*/
2015-07-29 11:51:55 +00:00
public function isTypeSupportedInTemplates( $type, $templates ) {
$isValid = true;
2015-09-09 08:01:24 +00:00
if ( !isset( $templates[ $type ] ) ) {
WikiaLogger::instance()->info( self::LOGGER_LABEL, [
'type' => $type
] );
$isValid = false;
}
return $isValid;
}
/**
* return real width of the image.
* @param Title $title
* @return int number
*/
private function getFileWidth( $title ) {
$file = \WikiaFileHelper::getFileFromTitle( $title );
2015-09-09 08:01:24 +00:00
if ( $file ) {
return $file->getWidth();
}
}
/**
2015-09-08 14:29:41 +00:00
* @desc create a thumb of the image from file title.
* @param Title $title
* @return bool|MediaTransformOutput
*/
private function getThumbnail( $title ) {
$file = \WikiaFileHelper::getFileFromTitle( $title );
if ( $file ) {
2015-09-09 08:16:00 +00:00
$size = $this->getAdjustedImageSize( $file );
$thumb = $file->transform( $size );
2015-09-08 14:29:41 +00:00
if ( !is_null( $thumb ) && !$thumb->isError() ) {
return $thumb;
}
}
2015-09-09 08:01:24 +00:00
return false;
}
2015-09-09 08:16:00 +00:00
/**
* @desc get image size according to the width and height limitations:
* Height on desktop cannot be bigger than 500px
* Width have to be adjusted to const for mobile or desktop infobox
* @param $image
* @return array width and height
*/
2015-09-09 12:54:08 +00:00
public function getAdjustedImageSize( $image ) {
2015-09-09 08:16:00 +00:00
if ( $this->isWikiaMobile() ) {
$width = self::MOBILE_THUMBNAIL_WIDTH;
$height = null;
} else {
$height = min( self::MAX_DESKTOP_THUMBNAIL_HEIGHT, $image->getHeight() );
$width = self::DESKTOP_THUMBNAIL_WIDTH;
}
return [ 'height' => $height, 'width' => $width ];
}
2015-09-09 08:01:24 +00:00
}