PortableInfobox/includes/services/Parser/Nodes/NodeMedia.php

253 lines
6.7 KiB
PHP
Raw Normal View History

<?php
namespace PortableInfobox\Parser\Nodes;
use PortableInfobox\Helpers\FileNamespaceSanitizeHelper;
use PortableInfobox\Helpers\HtmlHelper;
use PortableInfobox\Helpers\PortableInfoboxDataBag;
use PortableInfobox\Sanitizers\SanitizerBuilder;
2018-08-12 09:45:29 +00:00
class NodeMedia extends Node {
const GALLERY = 'GALLERY';
const TABBER = 'TABBER';
2018-08-12 09:45:29 +00:00
const ALLOWIMAGE_ATTR_NAME = 'image';
const ALLOWVIDEO_ATTR_NAME = 'video';
const ALLOWAUDIO_ATTR_NAME = 'audio';
const ALT_TAG_NAME = 'alt';
2015-05-19 07:56:57 +00:00
const CAPTION_TAG_NAME = 'caption';
public static function getMarkers( $value, $ext ) {
2018-07-28 13:59:24 +00:00
if ( preg_match_all('/' . \Parser::MARKER_PREFIX . '-' . $ext . '-[A-F0-9]{8}' . \Parser::MARKER_SUFFIX . '/is', $value, $out ) ) {
return $out[0];
} else {
return [];
}
}
public static function getGalleryData( $marker ) {
$gallery = PortableInfoboxDataBag::getInstance()->getGallery( $marker );
return isset( $gallery ) ? array_map( function ( $image ) {
return [
'label' => $image[1] ?: $image[0]->getText(),
'title' => $image[0]
];
}, $gallery->getimages() ) : [];
}
public static function getTabberData( $html ) {
2018-08-04 17:12:35 +00:00
$data = [];
2016-01-12 09:50:24 +00:00
$doc = HtmlHelper::createDOMDocumentFromText( $html );
$sxml = simplexml_import_dom( $doc );
$divs = $sxml->xpath( '//div[@class=\'tabbertab\']' );
foreach ( $divs as $div ) {
if ( preg_match( '/ src="(?:[^"]*\/)?([^"]*?)"/', $div->asXML(), $out ) ) {
2018-08-04 17:12:35 +00:00
$data[] = [
'label' => (string) $div['title'],
'title' => $out[1]
];
}
}
return $data;
}
public function getData() {
2015-06-11 09:34:28 +00:00
if ( !isset( $this->data ) ) {
2018-08-04 17:12:35 +00:00
$this->data = [];
// value passed to source parameter (or default)
$value = $this->getRawValueWithDefault( $this->xmlNode );
if ( $this->containsTabberOrGallery( $value ) ) {
$this->data = $this->getImagesData( $value );
} else {
2018-08-04 17:12:35 +00:00
$this->data = [ $this->getImageData(
$value,
$this->getValueWithDefault( $this->xmlNode->{self::ALT_TAG_NAME} ),
$this->getValueWithDefault( $this->xmlNode->{self::CAPTION_TAG_NAME} )
2018-08-04 17:12:35 +00:00
) ];
}
}
return $this->data;
}
/**
* @desc Checks if parser preprocessed string containg Tabber or Gallery extension
* @param string $str String to check
* @return bool
*/
private function containsTabberOrGallery( $str ) {
return !empty( self::getMarkers( $str, self::TABBER ) ) || !empty( self::getMarkers( $str, self::GALLERY ) );
}
private function getImagesData( $value ) {
$data = [];
$items = array_merge( $this->getGalleryItems( $value ), $this->getTabberItems( $value ) );
foreach( $items as $item ) {
$data[] = $this->getImageData( $item['title'], $item['label'], $item['label'] );
}
return $data;
}
private function getGalleryItems( $value ) {
$galleryItems = [];
$galleryMarkers = self::getMarkers( $value, self::GALLERY );
foreach ( $galleryMarkers as $marker ) {
$galleryItems = array_merge( $galleryItems, self::getGalleryData( $marker ) );
2015-06-11 09:34:28 +00:00
}
return $galleryItems;
}
private function getTabberItems( $value ) {
$tabberItems = [];
$tabberMarkers = self::getMarkers( $value, self::TABBER );
foreach ( $tabberMarkers as $marker ) {
$tabberHtml = $this->getExternalParser()->parseRecursive( $marker );
$tabberItems = array_merge( $tabberItems, self::getTabberData( $tabberHtml ) );
}
return $tabberItems;
}
/**
* @desc prepare infobox image node data.
*
* @param $title
* @param $alt
* @param $caption
* @return array
*/
private function getImageData( $title, $alt, $caption ) {
$titleObj = $title instanceof \Title ? $title : $this->getImageAsTitleObject( $title );
$fileObj = $this->getFileFromTitle( $titleObj );
2018-08-12 09:45:29 +00:00
if( !isset( $fileObj ) || !$this->isTypeAllowed( $fileObj->getMediaType() ) ) {
return [];
}
if ( $titleObj instanceof \Title ) {
$this->getExternalParser()->addImage( $titleObj->getDBkey() );
}
$image = [
'url' => $this->resolveImageUrl( $fileObj ),
'name' => $titleObj ? $titleObj->getText() : '',
2018-08-08 09:42:22 +00:00
'alt' => $alt ?? ( $titleObj ? $titleObj->getText() : null ),
'caption' => SanitizerBuilder::createFromType( 'image' )
2018-08-12 09:45:29 +00:00
->sanitize( [ 'caption' => $caption ] )['caption'] ?: null
];
return $image;
}
public function isEmpty() {
2015-06-11 09:34:28 +00:00
$data = $this->getData();
foreach ( $data as $dataItem ) {
if ( !empty( $dataItem[ 'url' ] ) ) {
return false;
}
}
return true;
}
public function getSources() {
$sources = $this->extractSourcesFromNode( $this->xmlNode );
if ( $this->xmlNode->{self::ALT_TAG_NAME} ) {
$sources = array_merge( $sources,
$this->extractSourcesFromNode( $this->xmlNode->{self::ALT_TAG_NAME} ) );
}
if ( $this->xmlNode->{self::CAPTION_TAG_NAME} ) {
$sources = array_merge( $sources,
$this->extractSourcesFromNode( $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::makeTitleSafe(
NS_FILE,
FileNamespaceSanitizeHelper::getInstance()->sanitizeImageFileName( $imageName, $wgContLang )
);
2015-05-11 12:36:23 +00:00
return $title;
}
2015-07-21 14:29:14 +00:00
/**
2018-08-08 09:42:22 +00:00
* NOTE: Protected to override in unit tests
*
2015-07-21 14:29:14 +00:00
* @desc get file object from title object
* @param Title|null $title
* @return File|null
*/
protected function getFileFromTitle( $title ) {
if( is_string( $title ) ) {
$title = \Title::newFromText( $title, NS_FILE );
}
if( $title instanceof \Title ) {
$file = wfFindFile( $title );
if( $file instanceof \File && $file->exists() ) {
return $file;
}
}
return null;
2015-07-21 14:29:14 +00:00
}
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() : '';
}
/**
2018-08-12 09:45:29 +00:00
* @desc checks if file media type is allowed
* @param string $type
* @return bool
*/
private function isTypeAllowed( $type ) {
switch( $type ) {
case MEDIATYPE_BITMAP:
case MEDIATYPE_DRAWING:
return $this->allowImage();
case MEDIATYPE_VIDEO:
return $this->allowVideo();
case MEDIATYPE_AUDIO:
return $this->allowAudio();
default:
return false;
}
}
/**
2015-07-21 14:29:14 +00:00
* @return bool
*/
2018-08-12 09:45:29 +00:00
protected function allowImage() {
$attr = $this->getXmlAttribute( $this->xmlNode, self::ALLOWIMAGE_ATTR_NAME );
return !( isset( $attr ) && strtolower( $attr ) === 'false' );
}
/**
* @return bool
*/
protected function allowVideo() {
$attr = $this->getXmlAttribute( $this->xmlNode, self::ALLOWVIDEO_ATTR_NAME );
return !( isset( $attr ) && strtolower( $attr ) === 'false' );
}
/*
* @return bool
*/
protected function allowAudio() {
$attr = $this->getXmlAttribute( $this->xmlNode, self::ALLOWAUDIO_ATTR_NAME );
return !( isset( $attr ) && strtolower( $attr ) === 'false' );
2015-07-21 14:29:14 +00:00
}
}