2015-07-28 11:49:17 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Wikia\PortableInfobox\Helpers;
|
|
|
|
|
2016-03-17 09:37:18 +00:00
|
|
|
use Wikia\Logger\WikiaLogger;
|
2015-07-28 11:49:17 +00:00
|
|
|
|
|
|
|
class PortableInfoboxRenderServiceHelper {
|
|
|
|
const LOGGER_LABEL = 'portable-infobox-render-not-supported-type';
|
2016-03-17 09:37:18 +00:00
|
|
|
const DEFAULT_DESKTOP_THUMBNAIL_WIDTH = 270;
|
|
|
|
const EUROPA_THUMBNAIL_WIDTH = 300;
|
2015-07-28 11:49:17 +00:00
|
|
|
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;
|
2016-03-17 09:37:18 +00:00
|
|
|
const EUROPA_THEME_NAME = 'pi-theme-europa';
|
2015-07-28 11:49:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 = [
|
2016-03-17 09:37:18 +00:00
|
|
|
'labels' => [ ],
|
|
|
|
'values' => [ ],
|
2015-09-07 11:17:03 +00:00
|
|
|
'renderLabels' => false
|
2015-07-28 11:49:17 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
foreach ( $groupData as $item ) {
|
2015-09-09 08:01:24 +00:00
|
|
|
$data = $item[ 'data' ];
|
2015-07-28 11:49:17 +00:00
|
|
|
|
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-07 11:17:03 +00:00
|
|
|
|
2015-09-09 08:01:24 +00:00
|
|
|
if ( !empty( $data[ 'label' ] ) ) {
|
|
|
|
$horizontalGroupData[ 'renderLabels' ] = true;
|
2015-09-08 14:29:41 +00:00
|
|
|
}
|
2016-03-17 09:37:18 +00:00
|
|
|
} elseif ( $item[ 'type' ] === 'header' ) {
|
2015-09-09 14:00:59 +00:00
|
|
|
$horizontalGroupData[ 'header' ] = $data[ 'value' ];
|
2015-07-28 11:49:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $horizontalGroupData;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* extends image data
|
|
|
|
*
|
2016-03-17 09:37:18 +00:00
|
|
|
* @param array $data image data
|
|
|
|
* @param int $width preferred thumbnail width
|
|
|
|
* @return array|bool false on failure
|
2015-07-28 11:49:17 +00:00
|
|
|
*/
|
2016-03-17 09:37:18 +00:00
|
|
|
public function extendImageData( $data, $width ) {
|
|
|
|
global $wgPortableInfoboxCustomImageWidth;
|
2015-07-28 11:49:17 +00:00
|
|
|
|
2016-03-17 09:37:18 +00:00
|
|
|
// title param is provided through reference in WikiaFileHelper::getFileFromTitle
|
|
|
|
$title = $data[ 'name' ];
|
|
|
|
$file = \WikiaFileHelper::getFileFromTitle( $title );
|
|
|
|
if ( !$file || !$file->exists() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// get dimensions
|
2016-03-17 11:12:36 +00:00
|
|
|
$originalWidth = $file->getWidth();
|
2016-03-17 09:37:18 +00:00
|
|
|
$dimensions = $this->getThumbnailSizes(
|
2016-03-17 11:12:36 +00:00
|
|
|
$width, self::MAX_DESKTOP_THUMBNAIL_HEIGHT, $originalWidth, $file->getHeight() );
|
|
|
|
// if custom and big enough, scale thumbnail size
|
|
|
|
$ratio = !empty( $wgPortableInfoboxCustomImageWidth ) && $originalWidth > $wgPortableInfoboxCustomImageWidth ?
|
2016-03-17 09:37:18 +00:00
|
|
|
$wgPortableInfoboxCustomImageWidth / $dimensions[ 'width' ] : 1;
|
|
|
|
// get thumbnail
|
|
|
|
$thumbnail = $file->transform( [
|
|
|
|
'width' => round( $dimensions[ 'width' ] * $ratio ),
|
|
|
|
'height' => round( $dimensions[ 'height' ] * $ratio )
|
|
|
|
] );
|
|
|
|
if ( !$thumbnail || $thumbnail->isError() ) {
|
2015-07-28 11:49:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-03-17 09:37:18 +00:00
|
|
|
$ref = null;
|
2015-07-28 11:49:17 +00:00
|
|
|
|
2015-09-30 08:10:31 +00:00
|
|
|
wfRunHooks( 'PortableInfoboxRenderServiceHelper::extendImageData', [ $data, &$ref ] );
|
2015-07-28 11:49:17 +00:00
|
|
|
|
2016-03-17 09:37:18 +00:00
|
|
|
return array_merge( $data, [
|
|
|
|
'ref' => $ref,
|
|
|
|
'height' => $dimensions[ 'height' ],
|
|
|
|
'width' => $dimensions[ 'width' ],
|
|
|
|
'thumbnail' => $thumbnail->getUrl(),
|
|
|
|
'key' => urlencode( $data[ 'key' ] ),
|
2016-03-31 16:08:57 +00:00
|
|
|
'media-type' => $data[ 'isVideo' ] ? 'video' : 'image',
|
2016-04-01 10:06:10 +00:00
|
|
|
'mercury-component-attrs' => json_encode( [
|
|
|
|
'itemContext' => 'portable-infobox',
|
|
|
|
'ref' => $ref
|
|
|
|
] )
|
2016-03-17 09:37:18 +00:00
|
|
|
] );
|
2015-07-28 11:49:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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' ];
|
2015-07-28 11:49:17 +00:00
|
|
|
|
|
|
|
if ( $type === 'title' && !array_key_exists( 'title', $heroData ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-16 01:15:52 +00:00
|
|
|
if ( $type === 'image' && !array_key_exists( 'image', $heroData ) && count( $item[ 'data' ] ) === 1 ) {
|
|
|
|
$imageWidth = $this->getFileWidth( $item[ 'data' ][ 0 ][ 'name' ] );
|
2015-07-28 11:49:17 +00:00
|
|
|
|
2015-07-28 14:13:36 +00:00
|
|
|
if ( $imageWidth >= self::MINIMAL_HERO_IMG_WIDTH ) {
|
2015-07-28 11:49:17 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* required for testing mobile template rendering
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-04-04 11:13:52 +00:00
|
|
|
public function isMobile() {
|
2015-07-28 11:49:17 +00:00
|
|
|
return \F::app()->checkSkin( 'wikiamobile' );
|
|
|
|
}
|
|
|
|
|
2016-04-04 11:13:52 +00:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isMercury() {
|
2016-04-04 15:09:08 +00:00
|
|
|
global $wgArticleAsJson;
|
|
|
|
|
2016-04-04 11:13:52 +00:00
|
|
|
return !empty( $wgArticleAsJson );
|
|
|
|
}
|
|
|
|
|
2016-04-04 13:50:36 +00:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isMercuryExperimentalMarkupEnabled() {
|
|
|
|
global $wgEnableSeoFriendlyImagesForMobile;
|
|
|
|
|
|
|
|
return !empty( $wgEnableSeoFriendlyImagesForMobile );
|
|
|
|
}
|
|
|
|
|
2015-07-28 11:49:17 +00:00
|
|
|
/**
|
|
|
|
* 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 ) {
|
2015-07-28 11:49:17 +00:00
|
|
|
$isValid = true;
|
|
|
|
|
2015-09-09 08:01:24 +00:00
|
|
|
if ( !isset( $templates[ $type ] ) ) {
|
2015-07-28 11:49:17 +00:00
|
|
|
WikiaLogger::instance()->info( self::LOGGER_LABEL, [
|
|
|
|
'type' => $type
|
|
|
|
] );
|
|
|
|
|
|
|
|
$isValid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $isValid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-17 09:37:18 +00:00
|
|
|
* Checks if europa theme is enabled and used
|
|
|
|
* @return bool
|
2015-07-28 11:49:17 +00:00
|
|
|
*/
|
2016-03-29 10:31:03 +00:00
|
|
|
public function isEuropaTheme() {
|
2016-03-17 09:37:18 +00:00
|
|
|
global $wgEnablePortableInfoboxEuropaTheme;
|
2015-09-09 08:01:24 +00:00
|
|
|
|
2016-03-29 10:31:03 +00:00
|
|
|
return !empty( $wgEnablePortableInfoboxEuropaTheme );
|
2015-07-28 11:49:17 +00:00
|
|
|
}
|
2015-09-09 08:16:00 +00:00
|
|
|
|
2015-12-11 00:41:07 +00:00
|
|
|
/**
|
2016-03-17 09:37:18 +00:00
|
|
|
* Calculates image dimensions based on preferred width and max acceptable height
|
2015-12-11 10:56:41 +00:00
|
|
|
*
|
2016-03-17 09:37:18 +00:00
|
|
|
* @param int $preferredWidth
|
|
|
|
* @param int $maxHeight
|
|
|
|
* @param int $originalWidth
|
|
|
|
* @param int $originalHeight
|
|
|
|
* @return array [ 'width' => int, 'height' => int ]
|
2015-09-09 08:16:00 +00:00
|
|
|
*/
|
2016-03-17 09:37:18 +00:00
|
|
|
public function getThumbnailSizes( $preferredWidth, $maxHeight, $originalWidth, $originalHeight ) {
|
|
|
|
if ( ( $originalHeight / $originalWidth ) > ( $maxHeight / $preferredWidth ) ) {
|
|
|
|
$height = min( $maxHeight, $originalHeight );
|
|
|
|
$width = min( $preferredWidth, $height * $originalWidth / $originalHeight );
|
2015-12-11 10:56:41 +00:00
|
|
|
} else {
|
2016-03-17 09:37:18 +00:00
|
|
|
$width = min( $preferredWidth, $originalWidth );
|
|
|
|
$height = min( $maxHeight, $width * $originalHeight / $originalWidth );
|
2015-12-11 10:56:41 +00:00
|
|
|
}
|
|
|
|
|
2016-03-17 09:37:18 +00:00
|
|
|
return [ 'height' => round( $height ), 'width' => round( $width ) ];
|
2015-12-11 10:56:41 +00:00
|
|
|
}
|
|
|
|
|
2015-12-11 11:06:44 +00:00
|
|
|
/**
|
2016-03-17 09:37:18 +00:00
|
|
|
* return real width of the image.
|
|
|
|
* @param \Title $title
|
|
|
|
* @return int number
|
2015-12-11 11:06:44 +00:00
|
|
|
*/
|
2016-03-17 09:37:18 +00:00
|
|
|
private function getFileWidth( $title ) {
|
|
|
|
$file = \WikiaFileHelper::getFileFromTitle( $title );
|
2015-12-11 10:56:41 +00:00
|
|
|
|
2016-03-17 09:37:18 +00:00
|
|
|
if ( $file ) {
|
|
|
|
return $file->getWidth();
|
2015-09-09 08:16:00 +00:00
|
|
|
}
|
2015-12-12 00:05:10 +00:00
|
|
|
}
|
2015-09-09 08:01:24 +00:00
|
|
|
}
|