PortableInfobox/services/Parser/Nodes/NodeImage.php

49 lines
1.3 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() {
$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-19 07:56:57 +00:00
$caption = $this->getValueWithDefault( $this->xmlNode->{self::CAPTION_TAG_NAME} );
2015-05-11 12:36:23 +00:00
wfRunHooks( 'PortableInfoboxNodeImage::getData', [ $title, &$ref, $alt ] );
2015-05-11 15:28:43 +00:00
return [
'url' => $this->resolveImageUrl( $title ),
2015-05-20 11:56:19 +00:00
'name' => ( $title ) ? $title->getText() : '',
'key' => ( $title ) ? $title->getDBKey() : '',
2015-05-11 12:36:23 +00:00
'alt' => $alt,
2015-05-19 07:56:57 +00:00
'caption' => $caption,
2015-05-11 12:36:23 +00:00
'ref' => $ref
];
}
public function isEmpty( $data ) {
return !( isset( $data[ 'url' ] ) ) || empty( $data[ 'url' ] );
}
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;
}
public function resolveImageUrl( $title ) {
if ( $title && $title->exists() ) {
return \WikiaFileHelper::getFileFromTitle( $title )->getUrlGenerator()->url();
} else {
return '';
}
}
}