Merge branch 'dev' into XW-2591

This commit is contained in:
Mateusz Rybarski 2017-01-13 15:57:38 +01:00 committed by GitHub
commit ffdf4f3f62
5 changed files with 124 additions and 74 deletions

View file

@ -74,7 +74,7 @@ $wgAutoloadClasses[ 'AllinfoboxesQueryPage' ] = $dir . 'querypage/AllinfoboxesQu
// hooks
$wgHooks[ 'ParserFirstCallInit' ][] = 'PortableInfoboxParserTagController::parserTagInit';
$wgHooks[ 'ParserTagHooksBeforeInvoke' ][] = 'PortableInfoboxHooks::onParserTagHooksBeforeInvoke';
$wgHooks[ 'AfterParserParseImageGallery' ][] = 'PortableInfoboxHooks::onAfterParserParseImageGallery';
$wgHooks[ 'BeforePageDisplay' ][] = 'PortableInfoboxHooks::onBeforePageDisplay';
$wgHooks[ 'ParserAfterTidy' ][] = 'PortableInfoboxParserTagController::replaceInfoboxMarkers';
$wgHooks[ 'ImageServing::buildAndGetIndex' ][] = 'PortableInfoboxHooks::onImageServingCollectImages';

View file

@ -1,7 +1,6 @@
<?php
class PortableInfoboxHooks {
const PARSER_TAG_GALLERY = 'gallery';
public static function onBeforePageDisplay( OutputPage $out, Skin $skin ) {
global $wgEnablePortableInfoboxEuropaTheme;
@ -32,22 +31,16 @@ class PortableInfoboxHooks {
}
/**
* Store information about raw content of all galleries in article to handle images in infoboxes
*
* @param $name Parser tag name
* @param $marker substitution marker
* @param $content raw tag contents
* @param $attributes
* @param $parser
* @param $frame
* Store data of all galleries in article to handle images in infoboxes
*
* @param $marker
* @param WikiaPhotoGallery $gallery
* @return bool
*/
public static function onParserTagHooksBeforeInvoke( $name, $marker, $content, $attributes, $parser, $frame ) {
if ( $name === self::PARSER_TAG_GALLERY ) {
\Wikia\PortableInfobox\Helpers\PortableInfoboxDataBag::getInstance()->setGallery( $marker, $content );
public static function onAfterParserParseImageGallery( $marker, $gallery ) {
if ( $gallery instanceof WikiaPhotoGallery ) {
\Wikia\PortableInfobox\Helpers\PortableInfoboxDataBag::getInstance()->setGallery($marker, $gallery->getData());
}
return true;
}

View file

@ -2,9 +2,13 @@
namespace Wikia\PortableInfobox\Parser\Nodes;
use HtmlHelper;
use Wikia\PortableInfobox\Helpers\PortableInfoboxDataBag;
use WikiaFileHelper;
class NodeImage extends Node {
const GALLERY = 'GALLERY';
const TABBER = 'TABBER';
const ALT_TAG_NAME = 'alt';
const CAPTION_TAG_NAME = 'caption';
const MEDIA_TYPE_VIDEO = 'VIDEO';
@ -17,35 +21,14 @@ class NodeImage extends Node {
}
}
public static function getGalleryData( $html ) {
global $wgArticleAsJson;
$data = array();
if ( $wgArticleAsJson ) {
if ( preg_match( '/data-ref=\'([^\']+)\'/', $html, $out ) ) {
$media = \ArticleAsJson::$media[$out[1]];
for( $i = 0; $i < count( $media ); $i++ ) {
$data[] = array( 'label' => strip_tags( $media[$i]['caption'] ), 'title' => $media[$i]['title'] );
}
}
} else {
if ( preg_match( '#\sdata-model="([^"]+)"#', $html, $galleryOut ) ) {
$model = json_decode( htmlspecialchars_decode( $galleryOut[1] ), true );
for( $i = 0; $i < count( $model ); $i++ ) {
$data[] = array( 'label' => strip_tags( $model[$i][ 'caption' ] ), 'title' => $model[$i][ 'title' ] );
}
}
if ( preg_match_all('#data-(video|image)-key="([^"]+)".*?\s<h2>(.*?)<\/h2>#is', $html, $galleryOut ) ) {
for( $i = 0; $i < count( $galleryOut[0] ); $i++ ) {
$data[] = array( 'label' => $galleryOut[3][$i], 'title' => $galleryOut[2][$i] );
}
}
if ( preg_match_all('#data-(video|image)-key="([^"]+)".*?<div class="lightbox-caption"[^>]*>(.*?)<\/div>#is', $html, $galleryOut ) ) {
for( $i = 0; $i < count( $galleryOut[0] ); $i++ ) {
$data[] = array( 'label' => $galleryOut[3][$i], 'title' => $galleryOut[2][$i] );
}
}
}
return $data;
public static function getGalleryData( $marker ) {
$galleryData = PortableInfoboxDataBag::getInstance()->getGallery( $marker );
return array_map( function ( $image ) {
return [
'label' => $image[ 'caption' ],
'title' => $image[ 'name' ]
];
}, $galleryData[ 'images' ] );
}
public static function getTabberData( $html ) {
@ -94,38 +77,33 @@ class NodeImage extends Node {
* @return bool
*/
private function containsTabberOrGallery( $str ) {
// TODO: Consider more robust approach (UNIQ...QINU)
$strLower = strtolower( $str );
if ( strpos( $strLower, '-tabber-' ) !== false || strpos( $strLower, '-gallery-' ) !== false ) {
return true;
}
return false;
return !empty( self::getMarkers( $str, self::TABBER ) ) || !empty( self::getMarkers( $str, self::GALLERY ) );
}
private function getImagesData( $value ) {
$data = array();
$items = array_merge( $this->getGalleryItems( $value ), $this->getTabberItems( $value ) );
for( $i = 0; $i < count( $items ); $i++ ) {
$data[] = $this->getImageData( $items[$i]['title'], $items[$i]['label'], $items[$i]['label'] );
foreach( $items as $item ) {
$data[] = $this->getImageData( $item['title'], $item['label'], $item['label'] );
}
return $data;
}
private function getGalleryItems( $value ) {
$galleryItems = array();
$galleryMarkers = self::getMarkers( $value, 'GALLERY' );
for ( $i = 0; $i < count ( $galleryMarkers ); $i++ ) {
$galleryHtml = $this->getExternalParser()->parseRecursive( $galleryMarkers[$i] );
$galleryItems = array_merge( $galleryItems, self::getGalleryData( $galleryHtml ) );
$galleryItems = [];
$galleryMarkers = self::getMarkers( $value, self::GALLERY );
foreach ( $galleryMarkers as $marker ) {
$galleryItems = array_merge( $galleryItems, self::getGalleryData( $marker ) );
}
return $galleryItems;
}
private function getTabberItems( $value ) {
$tabberItems = array();
$tabberMarkers = self::getMarkers( $value, 'TABBER' );
for ( $i = 0; $i < count ( $tabberMarkers ); $i++ ) {
$tabberHtml = $this->getExternalParser()->parseRecursive( $tabberMarkers[$i] );
$tabberMarkers = self::getMarkers( $value, self::TABBER );
foreach ( $tabberMarkers as $marker ) {
$tabberHtml = $this->getExternalParser()->parseRecursive( $marker );
$tabberItems = array_merge( $tabberItems, self::getTabberData( $tabberHtml ) );
}
return $tabberItems;

View file

@ -153,14 +153,17 @@ class PortableInfoboxRenderService extends WikiaService {
*/
protected function renderImage( $data ) {
$helper = $this->getImageHelper();
$data = $this->filterImageData( $data );
$images = [ ];
for ( $i = 0; $i < count( $data ); $i++ ) {
$data[$i]['context'] = null;
$data[$i] = $helper->extendImageData( $data[$i], $this->imagesWidth, $this->infoboxWidth );
foreach ( $data as $dataItem ) {
$extendedItem = $dataItem;
$extendedItem['context'] = null;
$extendedItem = $helper->extendImageData( $extendedItem, $this->imagesWidth, $this->infoboxWidth );
if ( !!$data[$i] ) {
$images[] = $data[$i];
if ( !!$extendedItem ) {
$images[] = $extendedItem;
}
}
@ -204,6 +207,22 @@ class PortableInfoboxRenderService extends WikiaService {
return $result;
}
private function filterImageData( $data ) {
$dataWithCaption = array_filter($data, function( $item ) {
return !empty( $item['caption'] );
});
$result = [];
if ( !empty( $dataWithCaption ) ) {
$result = $dataWithCaption;
} elseif ( !empty( $data ) ) {
$result = [ $data[0] ];
}
return $result;
}
private function getInlineStyles( $accentColor, $accentColorText ) {
$backgroundColor = empty( $accentColor ) ? '' : "background-color:{$accentColor};";
$color = empty( $accentColorText ) ? '' : "color:{$accentColorText};";

View file

@ -1,5 +1,8 @@
<?php
use Wikia\PortableInfobox\Helpers\PortableInfoboxDataBag;
use Wikia\PortableInfobox\Parser\Nodes\NodeImage;
class NodeImageTest extends WikiaBaseTest {
protected function setUp() {
$this->setupFile = dirname( __FILE__ ) . '/../../PortableInfobox.setup.php';
@ -8,16 +11,68 @@ class NodeImageTest extends WikiaBaseTest {
/**
* @covers \Wikia\PortableInfobox\Parser\Nodes\NodeImage::getGalleryData
* @dataProvider galleryDataProvider
* @param $marker
* @param $expected
*/
public function testGalleryData() {
$input = '<div data-model="[{&quot;caption&quot;:&quot;_caption_&quot;,&quot;title&quot;:&quot;_title_&quot;}]"></div>';
$expected = [
[
'label' => '_caption_',
'title' => '_title_',
]
public function testGalleryData( $marker, $expected ) {
$this->assertEquals( $expected, NodeImage::getGalleryData( $marker ) );
}
public function galleryDataProvider() {
$markers = [
"'\"`UNIQabcd-gAlLeRy-1-QINU`\"'",
"'\"`UNIQabcd-gAlLeRy-2-QINU`\"'",
"'\"`UNIQabcd-gAlLeRy-3-QINU`\"'"
];
PortableInfoboxDataBag::getInstance()->setGallery( $markers[0],
['images' => [
[
'name' => 'image0_name.jpg',
'caption' => 'image0_caption'
],
[
'name' => 'image01_name.jpg',
'caption' => 'image01_caption'
],
]]);
PortableInfoboxDataBag::getInstance()->setGallery( $markers[1],
['images' => [
[
'name' => 'image1_name.jpg',
'caption' => 'image1_caption'
]
]]);
PortableInfoboxDataBag::getInstance()->setGallery( $markers[2], [ 'images' => [] ]);
return [
[
'marker' => $markers[0],
'expected' => [
[
'label' => 'image0_caption',
'title' => 'image0_name.jpg',
],
[
'label' => 'image01_caption',
'title' => 'image01_name.jpg',
],
]
],
[
'marker' => $markers[1],
'expected' => [
[
'label' => 'image1_caption',
'title' => 'image1_name.jpg',
]
]
],
[
'marker' => $markers[2],
'expected' => []
],
];
$this->assertEquals( $expected, Wikia\PortableInfobox\Parser\Nodes\NodeImage::getGalleryData( $input ) );
}
/**
@ -31,7 +86,7 @@ class NodeImageTest extends WikiaBaseTest {
'title' => '_data-image-key_',
]
];
$this->assertEquals( $expected, Wikia\PortableInfobox\Parser\Nodes\NodeImage::getTabberData( $input ) );
$this->assertEquals( $expected, NodeImage::getTabberData( $input ) );
}
/**
@ -56,6 +111,11 @@ class NodeImageTest extends WikiaBaseTest {
'GALLERY',
"\x7f'\"`UNIQ123456789-tAbBeR-12345678-QINU`\"'\x7f<center>\x7f'\"`UNIQabcd-gAlLeRy-12345678-QINU`\"'\x7f</center>\x7f'\"`UNIQabcd-gAlLeRy-87654321-QINU`\"'\x7f",
[ "\x7f'\"`UNIQabcd-gAlLeRy-12345678-QINU`\"'\x7f", "\x7f'\"`UNIQabcd-gAlLeRy-87654321-QINU`\"'\x7f" ]
],
[
'GALLERY',
"\x7f'\"`UNIQ123456789-somethingelse-12345678-QINU`\"'\x7f",
[ ]
]
];
}
@ -199,7 +259,7 @@ class NodeImageTest extends WikiaBaseTest {
$xmlObj = Wikia\PortableInfobox\Parser\XmlParser::parseXmlString( $markup );
$this->mockStaticMethod( 'WikiaFileHelper', 'getFileFromTitle', $fileMock );
$nodeImage = new Wikia\PortableInfobox\Parser\Nodes\NodeImage( $xmlObj, $params );
$nodeImage = new NodeImage( $xmlObj, $params );
$this->assertEquals( $expected, $nodeImage->getData() );
}