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

280 lines
7.6 KiB
PHP
Raw Normal View History

<?php
2021-06-02 16:52:59 +00:00
namespace PortableInfobox\Parser\Nodes;
2022-03-11 20:35:51 +00:00
use DOMDocument;
use DOMXpath;
use File;
2021-06-02 16:52:59 +00:00
use MediaWiki\MediaWikiServices;
2022-03-11 20:35:51 +00:00
use Parser;
use PortableInfobox\Helpers\FileNamespaceSanitizeHelper;
use PortableInfobox\Helpers\PortableInfoboxDataBag;
2018-08-22 14:20:49 +00:00
use PortableInfobox\Helpers\PortableInfoboxImagesHelper;
2022-03-11 20:35:51 +00:00
use PortableInfoboxRenderService;
use Title;
2018-08-12 09:45:29 +00:00
class NodeMedia extends Node {
2021-09-10 02:52:19 +00:00
private const GALLERY = 'GALLERY';
private const TABBER = 'TABBER';
2021-09-10 02:52:19 +00:00
private const ALLOWIMAGE_ATTR_NAME = 'image';
private const ALLOWVIDEO_ATTR_NAME = 'video';
private const ALLOWAUDIO_ATTR_NAME = 'audio';
2018-08-12 09:45:29 +00:00
2021-09-10 02:52:19 +00:00
private const ALT_TAG_NAME = 'alt';
private const CAPTION_TAG_NAME = 'caption';
2018-08-22 14:20:49 +00:00
private $helper;
public static function getMarkers( $value, $ext ) {
2022-03-11 20:35:51 +00:00
$regex = '/' . Parser::MARKER_PREFIX . "-$ext-[A-F0-9]{8}" . Parser::MARKER_SUFFIX . '/i';
if ( preg_match_all( $regex, $value ?? '', $out ) ) {
return $out[0];
} else {
return [];
}
}
public static function getGalleryData( $marker ) {
$gallery = PortableInfoboxDataBag::getInstance()->getGallery( $marker );
2021-09-10 02:52:19 +00:00
return isset( $gallery ) ? array_map( static function ( $image ) {
return [
2018-08-22 14:20:49 +00:00
'label' => $image[1],
'title' => $image[0]
];
}, $gallery->getimages() ) : [];
}
public static function getTabberData( $html ) {
2018-08-04 17:12:35 +00:00
$data = [];
2018-10-07 14:44:55 +00:00
2022-03-11 20:35:51 +00:00
$doc = new DOMDocument();
2018-10-07 14:44:55 +00:00
$libXmlErrorSetting = libxml_use_internal_errors( true );
// encode for correct load
$doc->loadHTML( mb_convert_encoding( $html, 'HTML-ENTITIES', 'UTF-8' ) );
libxml_clear_errors();
libxml_use_internal_errors( $libXmlErrorSetting );
2022-03-11 20:35:51 +00:00
$xpath = new DOMXpath( $doc );
2018-10-07 14:44:55 +00:00
$divs = $xpath->query( '//div[@class=\'tabbertab\']' );
foreach ( $divs as $div ) {
if ( preg_match( '# src="(?:[^"]*/)?(?:\d+px-)?([^"]*?)"#', $doc->saveXml( $div ), $out ) ) {
2018-08-04 17:12:35 +00:00
$data[] = [
2018-10-07 14:44:55 +00:00
'label' => $div->getAttribute( 'title' ),
2018-08-04 17:12:35 +00:00
'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 );
2018-08-22 14:20:49 +00:00
$helper = $this->getImageHelper();
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;
}
/**
2018-10-02 07:41:19 +00:00
* Checks if parser preprocessed string containg Tabber or Gallery extension
* @param string $str String to check
* @return bool
*/
private function containsTabberOrGallery( $str ) {
2018-10-02 07:41:19 +00:00
return !empty( self::getMarkers( $str, self::TABBER ) ) ||
!empty( self::getMarkers( $str, self::GALLERY ) );
}
private function getImagesData( $value ) {
2018-08-22 14:20:49 +00:00
$helper = $this->getImageHelper();
$data = [];
$items = array_merge( $this->getGalleryItems( $value ), $this->getTabberItems( $value ) );
2018-08-16 09:25:53 +00:00
foreach ( $items as $item ) {
2018-08-22 14:20:49 +00:00
$mediaItem = $this->getImageData( $item['title'], $item['label'], $item['label'] );
2021-09-10 02:52:19 +00:00
if ( (bool)$mediaItem ) {
2018-08-22 14:20:49 +00:00
$data[] = $mediaItem;
}
}
2018-08-22 14:20:49 +00:00
return count( $data ) > 1 ? $helper->extendImageCollectionData( $data ) : $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;
}
/**
2018-10-02 07:41:19 +00:00
* Prepare infobox image node data.
*
* @param $title
* @param $alt
* @param $caption
* @return array
*/
private function getImageData( $title, $alt, $caption ) {
2018-08-22 14:20:49 +00:00
$helper = $this->getImageHelper();
2022-03-11 20:35:51 +00:00
$titleObj = $title instanceof Title ? $title : $this->getImageAsTitleObject( $title );
2018-08-22 14:20:49 +00:00
$fileObj = $helper->getFile( $titleObj );
2018-08-16 09:25:53 +00:00
if ( !isset( $fileObj ) || !$this->isTypeAllowed( $fileObj->getMediaType() ) ) {
2018-08-12 09:45:29 +00:00
return [];
}
2018-08-22 14:20:49 +00:00
$mediatype = $fileObj->getMediaType();
$image = [
'url' => $this->resolveImageUrl( $fileObj ),
'name' => $titleObj ? $titleObj->getText() : '',
2018-08-08 09:42:22 +00:00
'alt' => $alt ?? ( $titleObj ? $titleObj->getText() : null ),
2018-09-03 15:59:15 +00:00
'caption' => $caption ?: null,
2018-08-22 14:20:49 +00:00
'isImage' => in_array( $mediatype, [ MEDIATYPE_BITMAP, MEDIATYPE_DRAWING ] ),
'isVideo' => $mediatype === MEDIATYPE_VIDEO,
'isAudio' => $mediatype === MEDIATYPE_AUDIO,
2019-02-02 22:34:48 +00:00
'source' => $this->getPrimarySource(),
'item-name' => $this->getItemName(),
'htmlAfter' => null,
];
if ( $titleObj instanceof Title ) {
// This call may produce extra HTML from, for example, the PageImages integration
$image['htmlAfter'] = $this->getExternalParser()->addImage( $titleObj );
}
2018-08-22 14:20:49 +00:00
if ( $image['isImage'] ) {
$image = array_merge( $image, $helper->extendImageData(
$fileObj,
2022-03-11 20:35:51 +00:00
PortableInfoboxRenderService::DEFAULT_DESKTOP_THUMBNAIL_WIDTH,
PortableInfoboxRenderService::DEFAULT_DESKTOP_INFOBOX_WIDTH
2018-08-22 14:20:49 +00:00
) );
}
return $image;
}
public function isEmpty() {
2015-06-11 09:34:28 +00:00
$data = $this->getData();
foreach ( $data as $dataItem ) {
2018-08-16 09:25:53 +00:00
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 ) {
2021-06-02 16:52:59 +00:00
$contLang = MediaWikiServices::getInstance()->getContentLanguage();
2022-03-11 20:35:51 +00:00
$title = Title::makeTitleSafe(
NS_FILE,
2021-06-02 16:52:59 +00:00
FileNamespaceSanitizeHelper::getInstance()->sanitizeImageFileName( $imageName, $contLang )
);
2015-05-11 12:36:23 +00:00
return $title;
}
2018-08-22 14:20:49 +00:00
protected function getImageHelper() {
if ( !isset( $this->helper ) ) {
$this->helper = new PortableInfoboxImagesHelper();
}
2018-08-22 14:20:49 +00:00
return $this->helper;
2015-07-21 14:29:14 +00:00
}
2015-05-22 15:27:20 +00:00
/**
2018-10-02 07:41:19 +00:00
* Returns image url for given image title
2022-03-11 20:35:51 +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-10-02 07:41:19 +00:00
* Checks if file media type is allowed
2018-08-12 09:45:29 +00:00
* @param string $type
* @return bool
*/
private function isTypeAllowed( $type ) {
2018-08-16 09:25:53 +00:00
switch ( $type ) {
2018-08-12 09:45:29 +00:00
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
}
}