2015-04-27 14:05:31 +00:00
|
|
|
<?php
|
|
|
|
namespace Wikia\PortableInfobox\Parser\Nodes;
|
|
|
|
|
2015-05-07 12:37:13 +00:00
|
|
|
use Wikia\PortableInfobox\Helpers\ImageFilenameSanitizer;
|
2015-06-19 12:16:07 +00:00
|
|
|
use Wikia\PortableInfobox\Helpers\PortableInfoboxDataBag;
|
2015-07-21 14:29:14 +00:00
|
|
|
use WikiaFileHelper;
|
2015-05-07 12:37:13 +00:00
|
|
|
|
2015-04-27 14:05:31 +00:00
|
|
|
class NodeImage extends Node {
|
|
|
|
const ALT_TAG_NAME = 'alt';
|
2015-05-19 07:56:57 +00:00
|
|
|
const CAPTION_TAG_NAME = 'caption';
|
2015-07-21 14:29:14 +00:00
|
|
|
const MEDIA_TYPE_VIDEO = 'VIDEO';
|
2015-04-27 14:05:31 +00:00
|
|
|
|
|
|
|
public function getData() {
|
2015-06-11 09:34:28 +00:00
|
|
|
if ( !isset( $this->data ) ) {
|
2015-10-16 01:15:52 +00:00
|
|
|
$this->data = array();
|
2015-06-19 10:31:22 +00:00
|
|
|
|
2015-10-16 01:15:52 +00:00
|
|
|
// value passed to source parameter (or default)
|
|
|
|
$value = $this->getRawValueWithDefault( $this->xmlNode );
|
2015-06-19 10:31:22 +00:00
|
|
|
|
2015-10-16 01:15:52 +00:00
|
|
|
if ( $this->containsTabberOrGallery( $value ) ) {
|
|
|
|
$this->data = $this->getImagesData( $value );
|
|
|
|
} else {
|
|
|
|
$this->data = array( $this->getImageData(
|
|
|
|
$value,
|
|
|
|
$this->getValueWithDefault( $this->xmlNode->{self::ALT_TAG_NAME} ),
|
|
|
|
$this->getValueWithDefault( $this->xmlNode->{self::CAPTION_TAG_NAME} )
|
|
|
|
) );
|
2015-08-31 11:53:06 +00:00
|
|
|
}
|
2015-10-16 01:15:52 +00:00
|
|
|
}
|
|
|
|
return $this->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @desc Checks if parser preprocessed string containg Tabber or Gallery extension
|
|
|
|
* @param string $str String to check
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
2015-10-20 01:19:49 +00:00
|
|
|
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'] );
|
2015-10-16 01:15:52 +00:00
|
|
|
}
|
2015-10-20 01:19:49 +00:00
|
|
|
return $data;
|
2015-10-19 23:56:20 +00:00
|
|
|
}
|
2015-10-16 01:15:52 +00:00
|
|
|
|
2015-10-20 01:19:49 +00:00
|
|
|
private function getGalleryItems( $value ) {
|
2015-10-19 23:56:20 +00:00
|
|
|
global $wgArticleAsJson;
|
2015-10-20 01:19:49 +00:00
|
|
|
$galleryItems = array();
|
|
|
|
$galleryMarkers = $this->getMarkers( $value, 'GALLERY' );
|
|
|
|
for ( $i = 0; $i < count ( $galleryMarkers ); $i++ ) {
|
|
|
|
$galleryHtml = $this->getExternalParser()->parseRecursive( $galleryMarkers[$i] );
|
2015-10-19 23:56:20 +00:00
|
|
|
if ( $wgArticleAsJson ) {
|
2015-10-20 01:19:49 +00:00
|
|
|
if ( preg_match( '/data-ref=\'([^\']+)\'/', $galleryHtml, $out ) ) {
|
|
|
|
$media = \ArticleAsJson::$media[$out[1]];
|
|
|
|
for( $j = 0; $j < count( $media ); $j++ ) {
|
|
|
|
$galleryItems[] = array( 'label' => strip_tags( $media[$j]['caption'] ), 'title' => $media[$j]['title'] );
|
|
|
|
}
|
2015-10-19 23:56:20 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-10-20 01:19:49 +00:00
|
|
|
if ( preg_match( '#\sdata-model="([^"]+)"#', $galleryHtml, $galleryOut ) ) {
|
|
|
|
$model = json_decode( htmlspecialchars_decode( $galleryOut[1] ), true );
|
|
|
|
$galleryItems = array_map( function( $modelItem ) {
|
|
|
|
return array( 'label' => strip_tags( $modelItem[ 'caption' ] ), 'title' => $modelItem['title'] );
|
|
|
|
}, $model );
|
|
|
|
}
|
|
|
|
if ( preg_match_all('#data-image-key="([^"]+)".*?\s<h2>(.*?)<\/h2>#is', $html, $galleryOut ) ) {
|
|
|
|
for( $j = 0; $j < count( $galleryOut[0] ); $j++ ) {
|
|
|
|
$galleryItems[] = array( 'label' => $galleryOut[2][$j], 'title' => $galleryOut[1][$j] );
|
|
|
|
}
|
2015-10-19 23:56:20 +00:00
|
|
|
}
|
2015-07-21 14:29:14 +00:00
|
|
|
}
|
2015-06-11 09:34:28 +00:00
|
|
|
}
|
2015-10-20 01:19:49 +00:00
|
|
|
return $galleryItems;
|
2015-10-16 01:15:52 +00:00
|
|
|
}
|
2015-06-10 09:28:33 +00:00
|
|
|
|
2015-10-20 01:19:49 +00:00
|
|
|
private function getTabberItems( $value ) {
|
|
|
|
global $wgArticleAsJson;
|
2015-10-19 23:56:20 +00:00
|
|
|
$tabberItems = array();
|
2015-10-20 01:19:49 +00:00
|
|
|
$tabberMarkers = $this->getMarkers( $value, 'TABBER' );
|
2015-10-19 23:56:20 +00:00
|
|
|
for ( $i = 0; $i < count ( $tabberMarkers ); $i++ ) {
|
2015-10-20 01:19:49 +00:00
|
|
|
$tabberHtml = $this->getExternalParser()->parseRecursive( $tabberMarkers[$i] );
|
|
|
|
$tabberItems = array_merge( $tabberItems, $this->getTabberData( $tabberHtml ) );
|
2015-10-19 23:56:20 +00:00
|
|
|
}
|
2015-10-20 01:19:49 +00:00
|
|
|
return $tabberItems;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getMarkers( $value, $ext ) {
|
|
|
|
if ( preg_match_all('/\x7fUNIQ[A-Z0-9]*-' . $ext . '-[0-9]{8}-QINU\x7f/is', $value, $out ) ) {
|
|
|
|
return $out[0];
|
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getTabberData( $html ) {
|
2015-10-19 23:56:20 +00:00
|
|
|
$data = array();
|
2015-10-20 01:19:49 +00:00
|
|
|
$doc = new \DOMDocument();
|
|
|
|
$doc->loadHTML( $html );
|
|
|
|
$sxml = simplexml_import_dom( $doc );
|
|
|
|
$divs = $sxml->xpath( '//div[@class=\'tabbertab\']' );
|
|
|
|
foreach ( $divs as $div ) {
|
|
|
|
if ( $wgArticleAsJson ) {
|
|
|
|
if ( preg_match( '/data-ref="([^"]+)"/', $div->p->asXML(), $out ) ) {
|
|
|
|
$data[] = array( 'label' => (string) $div['title'], 'title' => \ArticleAsJson::$media[$out[1]]['title'] );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ( preg_match( '/data-image-key="([^"]+)"/', $div->p->asXML(), $out ) ) {
|
|
|
|
$data[] = array( 'label' => (string) $div['title'], 'title' => $out[1] );
|
|
|
|
}
|
|
|
|
}
|
2015-10-19 23:56:20 +00:00
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2015-10-16 01:15:52 +00:00
|
|
|
private function getImageData( $title, $alt, $caption ) {
|
|
|
|
$titleObj = $this->getImageAsTitleObject( $title );
|
|
|
|
$fileObj = $this->getFilefromTitle( $titleObj );
|
|
|
|
|
|
|
|
if ( $titleObj instanceof \Title ) {
|
|
|
|
$this->getExternalParser()->addImage( $titleObj->getDBkey() );
|
|
|
|
}
|
|
|
|
|
|
|
|
$image = [
|
|
|
|
'url' => $this->resolveImageUrl( $fileObj ),
|
|
|
|
'name' => $titleObj ? $titleObj->getText() : '',
|
|
|
|
'key' => $titleObj ? $titleObj->getDBKey() : '',
|
|
|
|
'alt' => $alt,
|
|
|
|
'caption' => $caption
|
|
|
|
];
|
|
|
|
|
|
|
|
if ( $this->isVideo( $fileObj ) ) {
|
|
|
|
$image = $this->videoDataDecorator( $image, $fileObj );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $image;
|
2015-04-27 14:05:31 +00:00
|
|
|
}
|
|
|
|
|
2015-06-10 09:28:33 +00:00
|
|
|
public function isEmpty() {
|
2015-06-11 09:34:28 +00:00
|
|
|
$data = $this->getData();
|
2015-10-16 01:15:52 +00:00
|
|
|
foreach ( $data as $dataItem ) {
|
|
|
|
if ( !empty( $dataItem[ 'url' ] ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2015-05-06 14:11:04 +00:00
|
|
|
}
|
|
|
|
|
2015-06-11 09:48:32 +00:00
|
|
|
public function getSource() {
|
|
|
|
$sources = $this->extractSourceFromNode( $this->xmlNode );
|
|
|
|
if ( $this->xmlNode->{self::ALT_TAG_NAME} ) {
|
|
|
|
$sources = array_merge( $sources,
|
|
|
|
$this->extractSourceFromNode( $this->xmlNode->{self::ALT_TAG_NAME} ) );
|
|
|
|
}
|
|
|
|
if ( $this->xmlNode->{self::CAPTION_TAG_NAME} ) {
|
|
|
|
$sources = array_merge( $sources,
|
|
|
|
$this->extractSourceFromNode( $this->xmlNode->{self::CAPTION_TAG_NAME} ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_unique( $sources );
|
|
|
|
}
|
|
|
|
|
2015-05-11 12:36:23 +00:00
|
|
|
private function getImageAsTitleObject( $imageName ) {
|
2015-05-04 15:43:09 +00:00
|
|
|
global $wgContLang;
|
2015-08-31 11:53:06 +00:00
|
|
|
$title = \Title::makeTitleSafe(
|
|
|
|
NS_FILE,
|
|
|
|
ImageFilenameSanitizer::getInstance()->sanitizeImageFileName( $imageName, $wgContLang )
|
2015-05-07 12:37:13 +00:00
|
|
|
);
|
2015-06-03 14:53:31 +00:00
|
|
|
|
2015-05-11 12:36:23 +00:00
|
|
|
return $title;
|
|
|
|
}
|
|
|
|
|
2015-07-21 14:29:14 +00:00
|
|
|
/**
|
|
|
|
* @desc get file object from title object
|
|
|
|
* @param Title|null $title
|
|
|
|
* @return File|null
|
|
|
|
*/
|
|
|
|
private function getFilefromTitle( $title ) {
|
|
|
|
return $title ? WikiaFileHelper::getFileFromTitle( $title ) : null;
|
|
|
|
}
|
|
|
|
|
2015-05-22 15:27:20 +00:00
|
|
|
/**
|
|
|
|
* @desc returns image url for given image title
|
2015-07-21 14:29:14 +00:00
|
|
|
* @param File|null $file
|
2015-05-22 15:27:20 +00:00
|
|
|
* @return string url or '' if image doesn't exist
|
|
|
|
*/
|
2015-07-21 14:29:14 +00:00
|
|
|
public function resolveImageUrl( $file ) {
|
|
|
|
return $file ? $file->getUrl() : '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @desc checks if file media type is VIDEO
|
|
|
|
* @param File|null $file
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function isVideo( $file ) {
|
|
|
|
return $file ? $file->getMediaType() === self::MEDIA_TYPE_VIDEO : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @desc add addtional data required for video media type
|
|
|
|
* @param array $data
|
|
|
|
* @param File $file
|
|
|
|
* @return array
|
|
|
|
*/
|
2015-09-09 11:23:34 +00:00
|
|
|
private function videoDataDecorator( $data, $file ) {
|
|
|
|
$title = $file->getTitle();
|
|
|
|
|
|
|
|
if ( $title ) {
|
|
|
|
$data[ 'url' ] = $title->getFullURL();
|
|
|
|
}
|
|
|
|
|
2015-09-07 15:38:17 +00:00
|
|
|
$data[ 'isVideo' ] = true;
|
|
|
|
$data[ 'duration' ] = WikiaFileHelper::formatDuration( $file->getMetadataDuration());
|
2015-06-03 14:53:31 +00:00
|
|
|
|
2015-07-21 14:29:14 +00:00
|
|
|
return $data;
|
2015-04-27 14:05:31 +00:00
|
|
|
}
|
|
|
|
}
|