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-04-27 14:05:31 +00:00
|
|
|
class NodeImage extends Node {
|
|
|
|
const ALT_TAG_NAME = 'alt';
|
|
|
|
|
|
|
|
public function getData() {
|
2015-05-11 15:24:19 +00:00
|
|
|
$title = $this->getImageAsTitleObject( $this->getRawValueWithDefault( $this->xmlNode ) );
|
2015-05-11 12:36:23 +00:00
|
|
|
$ref = null;
|
|
|
|
$alt = $this->getValueWithDefault( $this->xmlNode->{self::ALT_TAG_NAME} );
|
|
|
|
|
2015-05-11 15:24:19 +00:00
|
|
|
wfRunHooks( 'PortableInfoboxNodeImage::getData', [ $title, &$ref, $alt ] );
|
2015-05-11 15:28:43 +00:00
|
|
|
|
2015-05-07 12:37:13 +00:00
|
|
|
return [
|
2015-05-11 15:07:07 +00:00
|
|
|
'url' => $this->resolveImageUrl( $title ),
|
|
|
|
'name' => ( $title ) ? $title->getDBkey() : '',
|
2015-05-11 12:36:23 +00:00
|
|
|
'alt' => $alt,
|
|
|
|
'ref' => $ref
|
2015-05-07 12:37:13 +00:00
|
|
|
];
|
2015-04-27 14:05:31 +00:00
|
|
|
}
|
|
|
|
|
2015-05-06 14:11:04 +00:00
|
|
|
public function isEmpty( $data ) {
|
|
|
|
return !( isset( $data[ 'url' ] ) ) || empty( $data[ 'url' ] );
|
|
|
|
}
|
|
|
|
|
2015-05-11 12:36:23 +00:00
|
|
|
private function getImageAsTitleObject( $imageName ) {
|
2015-05-04 15:43:09 +00:00
|
|
|
global $wgContLang;
|
2015-05-07 12:37:13 +00:00
|
|
|
$title = \Title::newFromText(
|
2015-05-11 12:36:23 +00:00
|
|
|
ImageFilenameSanitizer::getInstance()->sanitizeImageFileName( $imageName, $wgContLang ),
|
2015-05-07 12:37:13 +00:00
|
|
|
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
|
|
|
|
* @param string $title
|
|
|
|
* @return string url or '' if image doesn't exist
|
|
|
|
*/
|
2015-05-11 11:56:18 +00:00
|
|
|
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-04-27 14:05:31 +00:00
|
|
|
}
|
2015-05-22 15:27:20 +00:00
|
|
|
return '';
|
2015-04-27 14:05:31 +00:00
|
|
|
}
|
|
|
|
}
|