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-06-19 10:31:22 +00:00
|
|
|
$imageData = $this->getRawValueWithDefault( $this->xmlNode );
|
|
|
|
|
|
|
|
if( is_string($imageData) && PortableInfoboxDataBag::getInstance()->getGallery($imageData)) {
|
|
|
|
$imageData = PortableInfoboxDataBag::getInstance()->getGallery($imageData);
|
|
|
|
}
|
|
|
|
|
|
|
|
$title = $this->getImageAsTitleObject( $imageData );
|
2015-07-21 14:29:14 +00:00
|
|
|
$file = $this->getFilefromTitle( $title );
|
2015-08-31 11:53:06 +00:00
|
|
|
if ( $title instanceof \Title ) {
|
|
|
|
$this->getExternalParser()->addImage( $title->getDBkey() );
|
|
|
|
}
|
2015-06-11 09:34:28 +00:00
|
|
|
$ref = null;
|
|
|
|
$alt = $this->getValueWithDefault( $this->xmlNode->{self::ALT_TAG_NAME} );
|
|
|
|
$caption = $this->getValueWithDefault( $this->xmlNode->{self::CAPTION_TAG_NAME} );
|
|
|
|
|
|
|
|
wfRunHooks( 'PortableInfoboxNodeImage::getData', [ $title, &$ref, $alt ] );
|
|
|
|
|
|
|
|
$this->data = [
|
2015-07-21 14:29:14 +00:00
|
|
|
'url' => $this->resolveImageUrl( $file ),
|
2015-06-11 09:34:28 +00:00
|
|
|
'name' => ( $title ) ? $title->getText() : '',
|
|
|
|
'key' => ( $title ) ? $title->getDBKey() : '',
|
|
|
|
'alt' => $alt,
|
|
|
|
'caption' => $caption,
|
|
|
|
'ref' => $ref
|
|
|
|
];
|
2015-07-21 14:29:14 +00:00
|
|
|
|
|
|
|
if ( $this->isVideo( $file ) ) {
|
|
|
|
$this->data = $this->videoDataDecorator( $this->data, $file );
|
|
|
|
}
|
2015-06-11 09:34:28 +00:00
|
|
|
}
|
2015-06-10 09:28:33 +00:00
|
|
|
|
|
|
|
return $this->data;
|
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-06-11 09:48:32 +00:00
|
|
|
|
2015-06-12 13:43:37 +00:00
|
|
|
return empty( $data[ 'url' ] );
|
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
|
|
|
|
*/
|
|
|
|
private function videoDataDecorator( $data, $file ) {
|
|
|
|
$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
|
|
|
}
|
|
|
|
}
|