From ba0a923fd9d0e046636506669d6186307df1c860 Mon Sep 17 00:00:00 2001 From: Rafal Leszczynski Date: Tue, 21 Jul 2015 16:29:14 +0200 Subject: [PATCH] add video functionality to image tag --- services/Parser/Nodes/NodeImage.php | 53 ++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/services/Parser/Nodes/NodeImage.php b/services/Parser/Nodes/NodeImage.php index b7ee7e9..c0616ec 100644 --- a/services/Parser/Nodes/NodeImage.php +++ b/services/Parser/Nodes/NodeImage.php @@ -3,10 +3,12 @@ namespace Wikia\PortableInfobox\Parser\Nodes; use Wikia\PortableInfobox\Helpers\ImageFilenameSanitizer; use Wikia\PortableInfobox\Helpers\PortableInfoboxDataBag; +use WikiaFileHelper; class NodeImage extends Node { const ALT_TAG_NAME = 'alt'; const CAPTION_TAG_NAME = 'caption'; + const MEDIA_TYPE_VIDEO = 'VIDEO'; public function getData() { if ( !isset( $this->data ) ) { @@ -17,6 +19,7 @@ class NodeImage extends Node { } $title = $this->getImageAsTitleObject( $imageData ); + $file = $this->getFilefromTitle( $title ); $this->getExternalParser()->addImage( $title ? $title->getDBkey() : $imageData ); $ref = null; $alt = $this->getValueWithDefault( $this->xmlNode->{self::ALT_TAG_NAME} ); @@ -25,13 +28,17 @@ class NodeImage extends Node { wfRunHooks( 'PortableInfoboxNodeImage::getData', [ $title, &$ref, $alt ] ); $this->data = [ - 'url' => $this->resolveImageUrl( $title ), + 'url' => $this->resolveImageUrl( $file ), 'name' => ( $title ) ? $title->getText() : '', 'key' => ( $title ) ? $title->getDBKey() : '', 'alt' => $alt, 'caption' => $caption, 'ref' => $ref ]; + + if ( $this->isVideo( $file ) ) { + $this->data = $this->videoDataDecorator( $this->data, $file ); + } } return $this->data; @@ -67,21 +74,43 @@ class NodeImage extends Node { return $title; } + /** + * @desc get file object from title object + * @param Title|null $title + * @return File|null + */ + private function getFilefromTitle( $title ) { + return $title ? WikiaFileHelper::getFileFromTitle( $title ) : null; + } + /** * @desc returns image url for given image title - * - * @param string $title - * + * @param File|null $file * @return string url or '' if image doesn't exist */ - public function resolveImageUrl( $title ) { - if ( $title ) { - $file = \WikiaFileHelper::getFileFromTitle( $title ); - if ( $file ) { - return $file->getUrl(); - } - } + public function resolveImageUrl( $file ) { + return $file ? $file->getUrl() : ''; + } - return ''; + /** + * @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()); + + return $data; } }