Merge remote-tracking branch 'origin/dev' into DAT-3204

This commit is contained in:
idradm 2015-09-16 12:14:36 +02:00
commit 4539e4e683
9 changed files with 202 additions and 22 deletions

View file

@ -66,6 +66,7 @@ $wgHooks[ 'BeforePageDisplay' ][] = 'PortableInfoboxHooks::onBeforePageDisplay';
$wgHooks[ 'ParserAfterTidy' ][] = 'PortableInfoboxParserTagController::replaceInfoboxMarkers';
$wgHooks[ 'ImageServing::buildAndGetIndex' ][] = 'PortableInfoboxHooks::onImageServingCollectImages';
$wgHooks[ 'wgQueryPages' ][] = 'PortableInfoboxHooks::onWgQueryPages';
$wgHooks[ 'AllInfoboxesQueryRecached' ][] = 'PortableInfoboxHooks::onAllInfoboxesQueryRecached';
// special pages
$wgSpecialPages[ 'AllInfoboxes' ] = 'AllinfoboxesQueryPage';

View file

@ -49,4 +49,9 @@ class PortableInfoboxHooks {
return true;
}
static public function onAllInfoboxesQueryRecached() {
F::app()->wg->Memc->delete( wfMemcKey( ApiQueryAllinfoboxes::MCACHE_KEY ) );
return true;
}
}

View file

@ -74,6 +74,8 @@ class AllinfoboxesQueryPage extends PageQueryPage {
$dbw->commit();
}
wfRunHooks( 'AllInfoboxesQueryRecached' );
return $inserted;
}

View file

@ -96,7 +96,7 @@ class ImageFilenameSanitizer {
$unprefixedFilename = mb_ereg_replace( $filePrefixRegex, "", $trimmedFilename );
$filenameParts = explode( '|', $unprefixedFilename );
if ( !empty( $filenameParts[ 0 ] ) ) {
return $filenameParts[ 0 ];
return urldecode( $filenameParts[0] );
}
return null;

View file

@ -11,6 +11,7 @@ class PortableInfoboxRenderServiceHelper {
const DESKTOP_THUMBNAIL_WIDTH = 270;
const MOBILE_THUMBNAIL_WIDTH = 360;
const MINIMAL_HERO_IMG_WIDTH = 300;
const MAX_DESKTOP_THUMBNAIL_HEIGHT = 500;
function __construct() {}
@ -21,7 +22,7 @@ class PortableInfoboxRenderServiceHelper {
* @return array
*/
public function createHorizontalGroupData( $groupData ) {
$horizontalGroupData =[
$horizontalGroupData = [
'labels' => [],
'values' => [],
'renderLabels' => false
@ -56,10 +57,12 @@ class PortableInfoboxRenderServiceHelper {
public function sanitizeInfoboxTitle( $type, $data ) {
if ( $type === 'title' && !empty( $data[ 'value' ] ) ) {
$data[ 'value' ] = trim( strip_tags( $data[ 'value' ] ) );
return $data;
}
if ( $type === 'hero-mobile' && !empty( $data[ 'title' ][ 'value' ] ) ) {
$data[ 'title' ][ 'value' ] = trim( strip_tags( $data[ 'title' ][ 'value' ] ) );
return $data;
}
@ -76,14 +79,12 @@ class PortableInfoboxRenderServiceHelper {
public function extendImageData( $data ) {
$thumbnail = $this->getThumbnail( $data[ 'name' ] );
if (!$thumbnail) {
if ( !$thumbnail ) {
return false;
}
// TODO: the min() function will be redundant when https://wikia-inc.atlassian.net/browse/PLATFORM-1359
// will hit the production
$data[ 'height' ] = min( $thumbnail->getHeight(), $thumbnail->file->getHeight() );
$data[ 'width' ] = min( $thumbnail->getWidth(), $thumbnail->file->getWidth() );
$data[ 'height' ] = $thumbnail->getHeight();
$data[ 'width' ] = $thumbnail->getWidth();
$data[ 'thumbnail' ] = $thumbnail->getUrl();
$data[ 'key' ] = urlencode( $data[ 'key' ] );
$data[ 'media-type' ] = $data[ 'isVideo' ] ? 'video' : 'image';
@ -162,7 +163,7 @@ class PortableInfoboxRenderServiceHelper {
}
/**
* @desc create a thumb of the image from file title
* @desc create a thumb of the image from file title.
* @param Title $title
* @return bool|MediaTransformOutput
*/
@ -170,16 +171,33 @@ class PortableInfoboxRenderServiceHelper {
$file = \WikiaFileHelper::getFileFromTitle( $title );
if ( $file ) {
$width = $this->isWikiaMobile() ?
self::MOBILE_THUMBNAIL_WIDTH :
self::DESKTOP_THUMBNAIL_WIDTH;
$thumb = $file->transform( ['width' => $width] );
$size = $this->getAdjustedImageSize( $file );
$thumb = $file->transform( $size );
if (!is_null($thumb) && !$thumb->isError()) {
if ( !is_null( $thumb ) && !$thumb->isError() ) {
return $thumb;
}
}
return false;
}
}
/**
* @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
*/
public function getAdjustedImageSize( $image ) {
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 ];
}
}

View file

@ -110,8 +110,14 @@ class NodeImage extends Node {
* @return array
*/
private function videoDataDecorator( $data, $file ) {
$data['isVideo'] = true;
$data['duration'] = WikiaFileHelper::formatDuration( $file->getMetadataDuration());
$title = $file->getTitle();
if ( $title ) {
$data[ 'url' ] = $title->getFullURL();
}
$data[ 'isVideo' ] = true;
$data[ 'duration' ] = WikiaFileHelper::formatDuration( $file->getMetadataDuration());
return $data;
}

View file

@ -84,13 +84,13 @@ class ImageFilenameSanitizerTest extends WikiaBaseTest {
'[[File:image.jpg|300px|lorem ipsum]]',
'es',
'image.jpg',
'Link to filename with canonical namespace, width and caption on a non-EN wiki '
'Link to filename with canonical namespace, width and caption on a non-EN wiki'
],
[
'[[File:image.jpg|lorem ipsum]]',
'es',
'image.jpg',
'Link to filename with canonical namespace and caption on a non-EN wiki '
'Link to filename with canonical namespace and caption on a non-EN wiki'
],
[
'<gallery>' . PHP_EOL .
@ -182,6 +182,12 @@ class ImageFilenameSanitizerTest extends WikiaBaseTest {
'filename.jpg',
'Filename with alias to namespace'
],
[
'[[File:Su-47_-iDOLM%40STER_Miki-EX-.jpg|300px]]',
'en',
'Su-47_-iDOLM@STER_Miki-EX-.jpg',
'Link to filename with canonical namespace, width urlencoded character in the middle'
],
];
}
}

View file

@ -18,16 +18,22 @@ class PortableInfoboxRenderServiceHelperTest extends WikiaBaseTest {
*/
public function createWikiaFileHelperMock( $input ) {
$fileWidth = isset( $input[ 'fileWidth' ] ) ? $input[ 'fileWidth' ] : null;
$fileHeight = isset( $input[ 'fileHeight' ] ) ? $input[ 'fileHeight' ] : null;
$fileMock = $this->getMockBuilder('File')
->setConstructorArgs( [ 'TestFile' ] )
->setMethods( [ 'getWidth' ] )
->setMethods( [ 'getWidth', 'getHeight' ] )
->getMock();
$fileMock->expects($this->any())
->method( 'getWidth' )
->will( $this->returnValue( $fileWidth ) );
$fileMock->expects($this->any())
->method( 'getHeight' )
->will( $this->returnValue( $fileHeight ) );
$this->mockStaticMethod( 'WikiaFileHelper', 'getFileFromTitle', $fileMock );
return $fileMock;
}
/**
@ -267,7 +273,6 @@ class PortableInfoboxRenderServiceHelperTest extends WikiaBaseTest {
* @param string $type
* @param boolean $result
* @param string $description
* @param array $mockParams
* @dataProvider testIsTypeSupportedInTemplatesDataProvider
*/
public function testIsTypeSupportedInTemplates( $type, $result, $description ) {
@ -296,4 +301,131 @@ class PortableInfoboxRenderServiceHelperTest extends WikiaBaseTest {
]
];
}
/**
* @desc test getAdjustedImageSize function. It should return the sizes we pass to transform function,
* not the sizes we want image to have. transform adjusts the correct sizes,
* that is creates thumbnail with sizes not bigger than passed, keeping the original aspect ratio.
*
* @param $mockParams
* @param $isWikiaMobile
* @param $result
* @param $description
* @dataProvider testGetAdjustedImageSizeDataProvider
*/
public function testGetAdjustedImageSize( $mockParams, $isWikiaMobile, $result, $description ) {
$mock = $this->getMockBuilder( 'Wikia\PortableInfobox\Helpers\PortableInfoboxRenderServiceHelper' )
->setMethods( [ 'isWikiaMobile' ] )
->getMock();
$mock->expects( $this->any() )->method( 'isWikiaMobile' )->will( $this->returnValue( $isWikiaMobile ) );
$file = $this->createWikiaFileHelperMock( $mockParams );
$this->assertEquals(
$result,
$mock->getAdjustedImageSize( $file ),
$description
);
}
public function testGetAdjustedImageSizeDataProvider() {
return [
[
'mockParams' => [
'fileHeight' => 2000,
'fileWidth' => 3000
],
'isWikiaMobile' => false,
'result' => [
'height' => 500,
'width' => 270
],
'description' => 'Big image on desktop'
],
[
'mockParams' => [
'fileHeight' => 3000,
'fileWidth' => 250
],
'isWikiaMobile' => false,
'result' => [
'height' => 500,
'width' => 270
],
'description' => 'Tall image on desktop'
],
[
'mockParams' => [
'fileHeight' => 200,
'fileWidth' => 2000
],
'isWikiaMobile' => false,
'result' => [
'height' => 200,
'width' => 270
],
'description' => 'Wide image on desktop'
],
[
'mockParams' => [
'fileHeight' => 50,
'fileWidth' => 45
],
'isWikiaMobile' => false,
'result' => [
'height' => 50,
'width' => 270
],
'description' => 'Small image on desktop'
],
[
'mockParams' => [
'fileHeight' => 2000,
'fileWidth' => 3000
],
'isWikiaMobile' => true,
'result' => [
'height' => null,
'width' => 360
],
'description' => 'Big image on mobile'
],
[
'mockParams' => [
'fileHeight' => 3000,
'fileWidth' => 250
],
'isWikiaMobile' => true,
'result' => [
'height' => null,
'width' => 360
],
'description' => 'Tall image on mobile'
],
[
'mockParams' => [
'fileHeight' => 200,
'fileWidth' => 2000
],
'isWikiaMobile' => true,
'result' => [
'height' => null,
'width' => 360
],
'description' => 'Wide image on mobile'
],
[
'mockParams' => [
'fileHeight' => 50,
'fileWidth' => 45
],
'isWikiaMobile' => true,
'result' => [
'height' => null,
'width' => 360
],
'description' => 'Small image on mobile'
]
];
}
}

View file

@ -112,12 +112,12 @@ class NodeImageTest extends WikiaBaseTest {
'<image source="img" />',
[ 'img' => 'test.jpg' ],
[
'url' => '',
'url' => 'http://test.url',
'name' => 'Test.jpg',
'key' => 'Test.jpg',
'alt' => null,
'caption' => null,
'ref' => 0,
'ref' => null,
'isVideo' => true,
'duration' => '00:10'
]
@ -138,4 +138,14 @@ class FileMock {
public function getUrl() {
return '';
}
public function getTitle() {
return new TitleMock();
}
}
class TitleMock {
public function getFullURL() {
return 'http://test.url';
}
}