PortableInfobox/services/Parser/Nodes/NodeImage.php

82 lines
2.1 KiB
PHP
Raw Normal View History

<?php
namespace Wikia\PortableInfobox\Parser\Nodes;
use Wikia\PortableInfobox\Helpers\ImageFilenameSanitizer;
class NodeImage extends Node {
const ALT_TAG_NAME = 'alt';
2015-05-19 07:56:57 +00:00
const CAPTION_TAG_NAME = 'caption';
public function getData() {
2015-06-11 09:34:28 +00:00
if ( !isset( $this->data ) ) {
$imageName = $this->getRawValueWithDefault( $this->xmlNode );
$title = $this->getImageAsTitleObject( $imageName );
$this->getExternalParser()->addImage( $title ? $title->getDBkey() : $imageName );
$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 = [
'url' => $this->resolveImageUrl( $title ),
'name' => ( $title ) ? $title->getText() : '',
'key' => ( $title ) ? $title->getDBKey() : '',
'alt' => $alt,
'caption' => $caption,
'ref' => $ref
];
}
return $this->data;
}
public function isEmpty() {
2015-06-11 09:34:28 +00:00
$data = $this->getData();
2015-06-11 09:34:28 +00:00
return !( isset( $data[ 'url' ] ) ) || empty( $data[ 'url' ] );
}
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 ) {
global $wgContLang;
$title = \Title::newFromText(
2015-05-11 12:36:23 +00:00
ImageFilenameSanitizer::getInstance()->sanitizeImageFileName( $imageName, $wgContLang ),
NS_FILE
);
2015-05-11 12:36:23 +00:00
return $title;
}
2015-05-22 15:27:20 +00:00
/**
* @desc returns image url for given image title
*
2015-05-22 15:27:20 +00:00
* @param string $title
*
2015-05-22 15:27:20 +00:00
* @return string url or '' if image doesn't exist
*/
public function resolveImageUrl( $title ) {
2015-05-22 16:25:21 +00:00
if ( $title ) {
2015-05-25 11:02:35 +00:00
$file = \WikiaFileHelper::getFileFromTitle( $title );
if ( $file ) {
2015-05-22 16:25:21 +00:00
return $file->getUrl();
}
}
2015-05-22 15:27:20 +00:00
return '';
}
}