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

# Conflicts:
#	extensions/wikia/PortableInfobox/PortableInfoboxHooks.class.php
This commit is contained in:
idradm 2015-06-23 13:31:56 +02:00
commit 29580c1c9d
6 changed files with 197 additions and 13 deletions

View file

@ -48,7 +48,7 @@ foreach ( $wgInfoboxParserNodes as $parserNode ) {
$wgAutoloadClasses[ 'Wikia\PortableInfobox\Helpers\ImageFilenameSanitizer' ] = $dir . 'services/Helpers/ImageFilenameSanitizer.php';
$wgAutoloadClasses[ 'Wikia\PortableInfobox\Helpers\SimpleXmlUtil' ] = $dir . 'services/Helpers/SimpleXmlUtil.php';
$wgAutoloadClasses[ 'Wikia\PortableInfobox\Helpers\InfoboParamsValidator' ] = $dir . 'services/Helpers/InfoboParamsValidator.php';
$wgAutoloadClasses[ 'Wikia\PortableInfobox\Helpers\PortableInfoboxDataBag' ] = $dir . 'services/Helpers/PortableInfoboxDataBag.php';
// controller classes
$wgAutoloadClasses[ 'PortableInfoboxParserTagController' ] = $dir . 'controllers/PortableInfoboxParserTagController.class.php';
@ -57,7 +57,8 @@ $wgAutoloadClasses[ 'ApiQueryPortableInfobox' ] = $dir . 'controllers/ApiQueryPo
$wgAutoloadClasses[ 'PortableInfoboxHooks' ] = $dir . 'PortableInfoboxHooks.class.php';
// hooks
$wgHooks[ 'ParserFirstCallInit' ][] = 'PortableInfoboxParserTagController::parserTagInit';
$wgHooks['ParserFirstCallInit'][] = 'PortableInfoboxParserTagController::parserTagInit';
$wgHooks['ParserTagHooksBeforeInvoke'][] = 'PortableInfoboxHooks::onParserTagHooksBeforeInvoke';
$wgHooks['BeforePageDisplay'][] = 'PortableInfoboxHooks::onBeforePageDisplay';
$wgHooks['ParserAfterTidy'][] = 'PortableInfoboxParserTagController::replaceInfoboxMarkers';
$wgHooks['ImageServing::buildAndGetIndex'][] = 'PortableInfoboxHooks::onImageServingCollectImages';

View file

@ -1,6 +1,8 @@
<?php
class PortableInfoboxHooks {
const PARSER_TAG_GALLERY = 'gallery';
static public function onBeforePageDisplay( OutputPage $out, Skin $skin ) {
if ( F::app()->checkSkin( 'monobook', $skin ) ) {
Wikia::addAssetsToOutput( 'portable_infobox_monobook_scss' );
@ -21,4 +23,22 @@ class PortableInfoboxHooks {
return true;
}
/**
* 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
*/
static public function onParserTagHooksBeforeInvoke( $name, $marker, $content, $attributes, $parser, $frame ) {
if ( $name === self::PARSER_TAG_GALLERY ) {
\Wikia\PortableInfobox\Helpers\PortableInfoboxDataBag::getInstance()->setGallery( $marker, $content );
}
return true;
}
}

View file

@ -2,6 +2,10 @@
namespace Wikia\PortableInfobox\Helpers;
/**
* Class ImageFilenameSanitizer
* @package Wikia\PortableInfobox\Helpers
*/
class ImageFilenameSanitizer {
private static $instance = null;
private $filePrefixRegex = [ ];
@ -44,16 +48,45 @@ class ImageFilenameSanitizer {
* @return mixed
*/
public function sanitizeImageFileName( $filename, $contLang ) {
// replace the MW square brackets and surrounding whitespace
$trimmedFilename = trim( $filename, "\t\n\r[]" );
$plainText = $this->convertToPlainText( $filename );
$filePrefixRegex = $this->getFilePrefixRegex( $contLang );
$unprefixedFilename = mb_ereg_replace( $filePrefixRegex, "", $trimmedFilename );
// strip
$filenameParts = explode( '|', $unprefixedFilename );
if ( !empty( $filenameParts[0] ) ) {
$filename = $filenameParts[0];
$textLines = explode( PHP_EOL, $plainText );
foreach ( $textLines as $potentialFilename ) {
$filename = $this->extractFilename( $potentialFilename, $filePrefixRegex );
if ($filename) {
return $filename;
}
}
return $plainText;
}
/**
* @param $filename
* @return string
*/
private function convertToPlainText( $filename ) {
// strip HTML tags
$filename = strip_tags( $filename );
// replace the surrounding whitespace
$filename = trim( $filename );
return $filename;
}
/**
* @param $potentialFilename
* @param $filePrefixRegex
* @return string|null
*/
private function extractFilename( $potentialFilename, $filePrefixRegex ) {
$trimmedFilename = trim( $potentialFilename, "[]" );
$unprefixedFilename = mb_ereg_replace( $filePrefixRegex, "", $trimmedFilename );
$filenameParts = explode( '|', $unprefixedFilename );
if ( !empty( $filenameParts[0] ) ) {
return $filenameParts[0];
}
return null;
}
}

View file

@ -0,0 +1,40 @@
<?php
namespace Wikia\PortableInfobox\Helpers;
/**
* Class PortableInfoboxDataBag
* @package Wikia\PortableInfobox\Helpers
*/
class PortableInfoboxDataBag {
private static $instance = null;
private $galleries = [ ];
private function __construct() {
}
/**
* @return null|PortableInfoboxDataBag
*/
public static function getInstance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self;
}
return self::$instance;
}
public function setGallery( $marker, $content ) {
$this->galleries[$marker] = $content;
}
/**
* Retrieve source content of a gallery identified by Parser marker id
*/
public function getGallery( $marker ) {
if ( isset( $this->galleries[$marker] ) ) {
return $this->galleries[$marker];
}
return null;
}
}

View file

@ -2,6 +2,7 @@
namespace Wikia\PortableInfobox\Parser\Nodes;
use Wikia\PortableInfobox\Helpers\ImageFilenameSanitizer;
use Wikia\PortableInfobox\Helpers\PortableInfoboxDataBag;
class NodeImage extends Node {
const ALT_TAG_NAME = 'alt';
@ -9,9 +10,14 @@ class NodeImage extends Node {
public function getData() {
if ( !isset( $this->data ) ) {
$imageName = $this->getRawValueWithDefault( $this->xmlNode );
$title = $this->getImageAsTitleObject( $imageName );
$this->getExternalParser()->addImage( $title ? $title->getDBkey() : $imageName );
$imageData = $this->getRawValueWithDefault( $this->xmlNode );
if( is_string($imageData) && PortableInfoboxDataBag::getInstance()->getGallery($imageData)) {
$imageData = PortableInfoboxDataBag::getInstance()->getGallery($imageData);
}
$title = $this->getImageAsTitleObject( $imageData );
$this->getExternalParser()->addImage( $title ? $title->getDBkey() : $imageData );
$ref = null;
$alt = $this->getValueWithDefault( $this->xmlNode->{self::ALT_TAG_NAME} );
$caption = $this->getValueWithDefault( $this->xmlNode->{self::CAPTION_TAG_NAME} );

View file

@ -91,6 +91,90 @@ class ImageFilenameSanitizerTest extends WikiaBaseTest {
'es',
'image.jpg',
'Link to filename with canonical namespace and caption on a non-EN wiki '
],
[
'<gallery>' . PHP_EOL .
'</gallery>' . PHP_EOL,
'en',
'',
'Empty gallery'
],
[
'<gallery></gallery>',
'en',
'',
'Empty gallery'
],
[
'<gallery />',
'en',
'',
'Empty gallery'
],
[
'<gallery>' . PHP_EOL .
'image.jpg' . PHP_EOL .
'</gallery>' . PHP_EOL,
'en',
'image.jpg',
'Gallery with one image'
],
[
'<gallery>' . PHP_EOL .
'File:image.jpg' . PHP_EOL .
'</gallery>' . PHP_EOL,
'en',
'image.jpg',
'Gallery with one image with canonical namespace',
],
[
'<gallery>' . PHP_EOL .
'文件名óśłżźćńę?.jpg' . PHP_EOL .
'Image010.jpg' . PHP_EOL .
'Image009.jpg' . PHP_EOL .
'</gallery>' . PHP_EOL,
'en',
'文件名óśłżźćńę?.jpg',
'Gallery with diacritics and UTF characters'
],
[
PHP_EOL .
PHP_EOL,
'en',
'',
'Content of empty gallery with newlines'
],
[
'',
'en',
'',
'Content of empty gallery'
],
[
PHP_EOL .
'image.jpg' . PHP_EOL .
PHP_EOL,
'en',
'image.jpg',
'Content of gallery with one image'
],
[
PHP_EOL .
'File:image.jpg' . PHP_EOL .
PHP_EOL,
'en',
'image.jpg',
'Content of gallery with one image with canonical namespace',
],
[
PHP_EOL .
'文件名óśłżźćńę?.jpg' . PHP_EOL .
'Image010.jpg' . PHP_EOL .
'Image009.jpg' . PHP_EOL .
PHP_EOL,
'en',
'文件名óśłżźćńę?.jpg',
'Content of gallery with diacritics and UTF characters'
]
];
}