From b233e9d1887de9e131b824ed5b659e3ab954ba6e Mon Sep 17 00:00:00 2001 From: Umherirrender Date: Sat, 30 Mar 2019 12:16:51 +0100 Subject: [PATCH] Move MediaTransformOutput classes to own files Change-Id: I1c676f18101250ffc28ea412213d568c5a4f527d --- .phpcs.xml | 1 - autoload.php | 8 +- includes/media/MediaTransformError.php | 68 +++++ includes/media/MediaTransformOutput.php | 274 ------------------ includes/media/ThumbnailImage.php | 206 +++++++++++++ includes/media/TransformParameterError.php | 41 +++ .../media/TransformTooBigImageAreaError.php | 47 +++ 7 files changed, 366 insertions(+), 279 deletions(-) create mode 100644 includes/media/MediaTransformError.php create mode 100644 includes/media/ThumbnailImage.php create mode 100644 includes/media/TransformParameterError.php create mode 100644 includes/media/TransformTooBigImageAreaError.php diff --git a/.phpcs.xml b/.phpcs.xml index 80b3e411a2..9939c5411e 100644 --- a/.phpcs.xml +++ b/.phpcs.xml @@ -221,7 +221,6 @@ */includes/libs/filebackend/SwiftFileBackend\.php */includes/logging/LogEntry\.php */includes/logging/LogFormatter\.php - */includes/media/MediaTransformOutput\.php */includes/media/SVGMetadataExtractor\.php */includes/parser/Preprocessor_DOM\.php */includes/parser/Preprocessor_Hash\.php diff --git a/autoload.php b/autoload.php index 6f25ebe0f5..b38c438de7 100644 --- a/autoload.php +++ b/autoload.php @@ -863,7 +863,7 @@ $wgAutoloadLocalClasses = [ 'MediaHandler' => __DIR__ . '/includes/media/MediaHandler.php', 'MediaHandlerFactory' => __DIR__ . '/includes/media/MediaHandlerFactory.php', 'MediaStatisticsPage' => __DIR__ . '/includes/specials/SpecialMediaStatistics.php', - 'MediaTransformError' => __DIR__ . '/includes/media/MediaTransformOutput.php', + 'MediaTransformError' => __DIR__ . '/includes/media/MediaTransformError.php', 'MediaTransformInvalidParametersException' => __DIR__ . '/includes/media/MediaTransformInvalidParametersException.php', 'MediaTransformOutput' => __DIR__ . '/includes/media/MediaTransformOutput.php', 'MediaWiki' => __DIR__ . '/includes/MediaWiki.php', @@ -1480,7 +1480,7 @@ $wgAutoloadLocalClasses = [ 'TextStatsOutput' => __DIR__ . '/maintenance/language/StatOutputs.php', 'TgConverter' => __DIR__ . '/languages/classes/LanguageTg.php', 'ThrottledError' => __DIR__ . '/includes/exception/ThrottledError.php', - 'ThumbnailImage' => __DIR__ . '/includes/media/MediaTransformOutput.php', + 'ThumbnailImage' => __DIR__ . '/includes/media/ThumbnailImage.php', 'ThumbnailRenderJob' => __DIR__ . '/includes/jobqueue/jobs/ThumbnailRenderJob.php', 'TidyUpT39714' => __DIR__ . '/maintenance/tidyUpT39714.php', 'TiffHandler' => __DIR__ . '/includes/media/TiffHandler.php', @@ -1497,8 +1497,8 @@ $wgAutoloadLocalClasses = [ 'TrackingCategories' => __DIR__ . '/includes/TrackingCategories.php', 'TraditionalImageGallery' => __DIR__ . '/includes/gallery/TraditionalImageGallery.php', 'TransactionRoundDefiningUpdate' => __DIR__ . '/includes/deferred/TransactionRoundDefiningUpdate.php', - 'TransformParameterError' => __DIR__ . '/includes/media/MediaTransformOutput.php', - 'TransformTooBigImageAreaError' => __DIR__ . '/includes/media/MediaTransformOutput.php', + 'TransformParameterError' => __DIR__ . '/includes/media/TransformParameterError.php', + 'TransformTooBigImageAreaError' => __DIR__ . '/includes/media/TransformTooBigImageAreaError.php', 'TransformationalImageHandler' => __DIR__ . '/includes/media/TransformationalImageHandler.php', 'UDPRCFeedEngine' => __DIR__ . '/includes/rcfeed/UDPRCFeedEngine.php', 'UDPTransport' => __DIR__ . '/includes/libs/UDPTransport.php', diff --git a/includes/media/MediaTransformError.php b/includes/media/MediaTransformError.php new file mode 100644 index 0000000000..f3b5d8f809 --- /dev/null +++ b/includes/media/MediaTransformError.php @@ -0,0 +1,68 @@ +msg = wfMessage( $msg )->params( $args ); + $this->width = intval( $width ); + $this->height = intval( $height ); + $this->url = false; + $this->path = false; + } + + function toHtml( $options = [] ) { + return "
width}px; height: {$this->height}px; display:inline-block;\">" . + $this->getHtmlMsg() . + "
"; + } + + function toText() { + return $this->msg->text(); + } + + function getHtmlMsg() { + return $this->msg->escaped(); + } + + function getMsg() { + return $this->msg; + } + + function isError() { + return true; + } + + function getHttpStatusCode() { + return 500; + } +} diff --git a/includes/media/MediaTransformOutput.php b/includes/media/MediaTransformOutput.php index 48ea4a510f..9c06ef421c 100644 --- a/includes/media/MediaTransformOutput.php +++ b/includes/media/MediaTransformOutput.php @@ -268,277 +268,3 @@ abstract class MediaTransformOutput { return $attribs; } } - -/** - * Media transform output for images - * - * @ingroup Media - */ -class ThumbnailImage extends MediaTransformOutput { - private static $firstNonIconImageRendered = false; - - /** - * Get a thumbnail object from a file and parameters. - * If $path is set to null, the output file is treated as a source copy. - * If $path is set to false, no output file will be created. - * $parameters should include, as a minimum, (file) 'width' and 'height'. - * It may also include a 'page' parameter for multipage files. - * - * @param File $file - * @param string $url URL path to the thumb - * @param string|bool $path Filesystem path to the thumb - * @param array $parameters Associative array of parameters - */ - function __construct( $file, $url, $path = false, $parameters = [] ) { - # Previous parameters: - # $file, $url, $width, $height, $path = false, $page = false - - $defaults = [ - 'page' => false, - 'lang' => false - ]; - - if ( is_array( $parameters ) ) { - $actualParams = $parameters + $defaults; - } else { - # Using old format, should convert. Later a warning could be added here. - $numArgs = func_num_args(); - $actualParams = [ - 'width' => $path, - 'height' => $parameters, - 'page' => ( $numArgs > 5 ) ? func_get_arg( 5 ) : false - ] + $defaults; - $path = ( $numArgs > 4 ) ? func_get_arg( 4 ) : false; - } - - $this->file = $file; - $this->url = $url; - $this->path = $path; - - # These should be integers when they get here. - # If not, there's a bug somewhere. But let's at - # least produce valid HTML code regardless. - $this->width = round( $actualParams['width'] ); - $this->height = round( $actualParams['height'] ); - - $this->page = $actualParams['page']; - $this->lang = $actualParams['lang']; - } - - /** - * Return HTML tag for the thumbnail, will include - * width and height attributes and a blank alt text (as required). - * - * @param array $options Associative array of options. Boolean options - * should be indicated with a value of true for true, and false or - * absent for false. - * - * alt HTML alt attribute - * title HTML title attribute - * desc-link Boolean, show a description link - * file-link Boolean, show a file download link - * valign vertical-align property, if the output is an inline element - * img-class Class applied to the \ tag, if there is such a tag - * desc-query String, description link query params - * override-width Override width attribute. Should generally not set - * override-height Override height attribute. Should generally not set - * no-dimensions Boolean, skip width and height attributes (useful if - * set in CSS) - * custom-url-link Custom URL to link to - * custom-title-link Custom Title object to link to - * custom target-link Value of the target attribute, for custom-target-link - * parser-extlink-* Attributes added by parser for external links: - * parser-extlink-rel: add rel="nofollow" - * parser-extlink-target: link target, but overridden by custom-target-link - * - * For images, desc-link and file-link are implemented as a click-through. For - * sounds and videos, they may be displayed in other ways. - * - * @throws MWException - * @return string - */ - function toHtml( $options = [] ) { - global $wgPriorityHints, $wgElementTiming; - - if ( count( func_get_args() ) == 2 ) { - throw new MWException( __METHOD__ . ' called in the old style' ); - } - - $alt = $options['alt'] ?? ''; - - $query = $options['desc-query'] ?? ''; - - $attribs = [ - 'alt' => $alt, - 'src' => $this->url, - 'decoding' => 'async', - ]; - - $elementTimingName = 'thumbnail'; - - if ( $wgPriorityHints - && !self::$firstNonIconImageRendered - && $this->width * $this->height > 100 * 100 ) { - self::$firstNonIconImageRendered = true; - - $attribs['importance'] = 'high'; - $elementTimingName = 'thumbnail-high'; - } - - if ( $wgElementTiming ) { - $attribs['elementtiming'] = $elementTimingName; - } - - if ( !empty( $options['custom-url-link'] ) ) { - $linkAttribs = [ 'href' => $options['custom-url-link'] ]; - if ( !empty( $options['title'] ) ) { - $linkAttribs['title'] = $options['title']; - } - if ( !empty( $options['custom-target-link'] ) ) { - $linkAttribs['target'] = $options['custom-target-link']; - } elseif ( !empty( $options['parser-extlink-target'] ) ) { - $linkAttribs['target'] = $options['parser-extlink-target']; - } - if ( !empty( $options['parser-extlink-rel'] ) ) { - $linkAttribs['rel'] = $options['parser-extlink-rel']; - } - } elseif ( !empty( $options['custom-title-link'] ) ) { - /** @var Title $title */ - $title = $options['custom-title-link']; - $linkAttribs = [ - 'href' => $title->getLinkURL(), - 'title' => empty( $options['title'] ) ? $title->getFullText() : $options['title'] - ]; - } elseif ( !empty( $options['desc-link'] ) ) { - $linkAttribs = $this->getDescLinkAttribs( - empty( $options['title'] ) ? null : $options['title'], - $query - ); - } elseif ( !empty( $options['file-link'] ) ) { - $linkAttribs = [ 'href' => $this->file->getUrl() ]; - } else { - $linkAttribs = false; - if ( !empty( $options['title'] ) ) { - $attribs['title'] = $options['title']; - } - } - - if ( empty( $options['no-dimensions'] ) ) { - $attribs['width'] = $this->width; - $attribs['height'] = $this->height; - } - if ( !empty( $options['valign'] ) ) { - $attribs['style'] = "vertical-align: {$options['valign']}"; - } - if ( !empty( $options['img-class'] ) ) { - $attribs['class'] = $options['img-class']; - } - if ( isset( $options['override-height'] ) ) { - $attribs['height'] = $options['override-height']; - } - if ( isset( $options['override-width'] ) ) { - $attribs['width'] = $options['override-width']; - } - - // Additional densities for responsive images, if specified. - // If any of these urls is the same as src url, it'll be excluded. - $responsiveUrls = array_diff( $this->responsiveUrls, [ $this->url ] ); - if ( !empty( $responsiveUrls ) ) { - $attribs['srcset'] = Html::srcSet( $responsiveUrls ); - } - - Hooks::run( 'ThumbnailBeforeProduceHTML', [ $this, &$attribs, &$linkAttribs ] ); - - return $this->linkWrap( $linkAttribs, Xml::element( 'img', $attribs ) ); - } -} - -/** - * Basic media transform error class - * - * @ingroup Media - */ -class MediaTransformError extends MediaTransformOutput { - /** @var Message */ - private $msg; - - function __construct( $msg, $width, $height /*, ... */ ) { - $args = array_slice( func_get_args(), 3 ); - $this->msg = wfMessage( $msg )->params( $args ); - $this->width = intval( $width ); - $this->height = intval( $height ); - $this->url = false; - $this->path = false; - } - - function toHtml( $options = [] ) { - return "
width}px; height: {$this->height}px; display:inline-block;\">" . - $this->getHtmlMsg() . - "
"; - } - - function toText() { - return $this->msg->text(); - } - - function getHtmlMsg() { - return $this->msg->escaped(); - } - - function getMsg() { - return $this->msg; - } - - function isError() { - return true; - } - - function getHttpStatusCode() { - return 500; - } -} - -/** - * Shortcut class for parameter validation errors - * - * @ingroup Media - */ -class TransformParameterError extends MediaTransformError { - function __construct( $params ) { - parent::__construct( 'thumbnail_error', - max( $params['width'] ?? 0, 120 ), - max( $params['height'] ?? 0, 120 ), - wfMessage( 'thumbnail_invalid_params' ) - ); - } - - function getHttpStatusCode() { - return 400; - } -} - -/** - * Shortcut class for parameter file size errors - * - * @ingroup Media - * @since 1.25 - */ -class TransformTooBigImageAreaError extends MediaTransformError { - function __construct( $params, $maxImageArea ) { - $msg = wfMessage( 'thumbnail_toobigimagearea' ); - $msg->params( - $msg->getLanguage()->formatComputingNumbers( $maxImageArea, 1000, "size-$1pixel" ) - ); - - parent::__construct( 'thumbnail_error', - max( $params['width'] ?? 0, 120 ), - max( $params['height'] ?? 0, 120 ), - $msg - ); - } - - function getHttpStatusCode() { - return 400; - } -} diff --git a/includes/media/ThumbnailImage.php b/includes/media/ThumbnailImage.php new file mode 100644 index 0000000000..0deb89fcc4 --- /dev/null +++ b/includes/media/ThumbnailImage.php @@ -0,0 +1,206 @@ + false, + 'lang' => false + ]; + + if ( is_array( $parameters ) ) { + $actualParams = $parameters + $defaults; + } else { + # Using old format, should convert. Later a warning could be added here. + $numArgs = func_num_args(); + $actualParams = [ + 'width' => $path, + 'height' => $parameters, + 'page' => ( $numArgs > 5 ) ? func_get_arg( 5 ) : false + ] + $defaults; + $path = ( $numArgs > 4 ) ? func_get_arg( 4 ) : false; + } + + $this->file = $file; + $this->url = $url; + $this->path = $path; + + # These should be integers when they get here. + # If not, there's a bug somewhere. But let's at + # least produce valid HTML code regardless. + $this->width = round( $actualParams['width'] ); + $this->height = round( $actualParams['height'] ); + + $this->page = $actualParams['page']; + $this->lang = $actualParams['lang']; + } + + /** + * Return HTML tag for the thumbnail, will include + * width and height attributes and a blank alt text (as required). + * + * @param array $options Associative array of options. Boolean options + * should be indicated with a value of true for true, and false or + * absent for false. + * + * alt HTML alt attribute + * title HTML title attribute + * desc-link Boolean, show a description link + * file-link Boolean, show a file download link + * valign vertical-align property, if the output is an inline element + * img-class Class applied to the \ tag, if there is such a tag + * desc-query String, description link query params + * override-width Override width attribute. Should generally not set + * override-height Override height attribute. Should generally not set + * no-dimensions Boolean, skip width and height attributes (useful if + * set in CSS) + * custom-url-link Custom URL to link to + * custom-title-link Custom Title object to link to + * custom target-link Value of the target attribute, for custom-target-link + * parser-extlink-* Attributes added by parser for external links: + * parser-extlink-rel: add rel="nofollow" + * parser-extlink-target: link target, but overridden by custom-target-link + * + * For images, desc-link and file-link are implemented as a click-through. For + * sounds and videos, they may be displayed in other ways. + * + * @throws MWException + * @return string + */ + function toHtml( $options = [] ) { + global $wgPriorityHints, $wgElementTiming; + + if ( count( func_get_args() ) == 2 ) { + throw new MWException( __METHOD__ . ' called in the old style' ); + } + + $alt = $options['alt'] ?? ''; + + $query = $options['desc-query'] ?? ''; + + $attribs = [ + 'alt' => $alt, + 'src' => $this->url, + 'decoding' => 'async', + ]; + + $elementTimingName = 'thumbnail'; + + if ( $wgPriorityHints + && !self::$firstNonIconImageRendered + && $this->width * $this->height > 100 * 100 ) { + self::$firstNonIconImageRendered = true; + + $attribs['importance'] = 'high'; + $elementTimingName = 'thumbnail-high'; + } + + if ( $wgElementTiming ) { + $attribs['elementtiming'] = $elementTimingName; + } + + if ( !empty( $options['custom-url-link'] ) ) { + $linkAttribs = [ 'href' => $options['custom-url-link'] ]; + if ( !empty( $options['title'] ) ) { + $linkAttribs['title'] = $options['title']; + } + if ( !empty( $options['custom-target-link'] ) ) { + $linkAttribs['target'] = $options['custom-target-link']; + } elseif ( !empty( $options['parser-extlink-target'] ) ) { + $linkAttribs['target'] = $options['parser-extlink-target']; + } + if ( !empty( $options['parser-extlink-rel'] ) ) { + $linkAttribs['rel'] = $options['parser-extlink-rel']; + } + } elseif ( !empty( $options['custom-title-link'] ) ) { + /** @var Title $title */ + $title = $options['custom-title-link']; + $linkAttribs = [ + 'href' => $title->getLinkURL(), + 'title' => empty( $options['title'] ) ? $title->getFullText() : $options['title'] + ]; + } elseif ( !empty( $options['desc-link'] ) ) { + $linkAttribs = $this->getDescLinkAttribs( + empty( $options['title'] ) ? null : $options['title'], + $query + ); + } elseif ( !empty( $options['file-link'] ) ) { + $linkAttribs = [ 'href' => $this->file->getUrl() ]; + } else { + $linkAttribs = false; + if ( !empty( $options['title'] ) ) { + $attribs['title'] = $options['title']; + } + } + + if ( empty( $options['no-dimensions'] ) ) { + $attribs['width'] = $this->width; + $attribs['height'] = $this->height; + } + if ( !empty( $options['valign'] ) ) { + $attribs['style'] = "vertical-align: {$options['valign']}"; + } + if ( !empty( $options['img-class'] ) ) { + $attribs['class'] = $options['img-class']; + } + if ( isset( $options['override-height'] ) ) { + $attribs['height'] = $options['override-height']; + } + if ( isset( $options['override-width'] ) ) { + $attribs['width'] = $options['override-width']; + } + + // Additional densities for responsive images, if specified. + // If any of these urls is the same as src url, it'll be excluded. + $responsiveUrls = array_diff( $this->responsiveUrls, [ $this->url ] ); + if ( !empty( $responsiveUrls ) ) { + $attribs['srcset'] = Html::srcSet( $responsiveUrls ); + } + + Hooks::run( 'ThumbnailBeforeProduceHTML', [ $this, &$attribs, &$linkAttribs ] ); + + return $this->linkWrap( $linkAttribs, Xml::element( 'img', $attribs ) ); + } +} diff --git a/includes/media/TransformParameterError.php b/includes/media/TransformParameterError.php new file mode 100644 index 0000000000..0f26239db4 --- /dev/null +++ b/includes/media/TransformParameterError.php @@ -0,0 +1,41 @@ +params( + $msg->getLanguage()->formatComputingNumbers( $maxImageArea, 1000, "size-$1pixel" ) + ); + + parent::__construct( 'thumbnail_error', + max( $params['width'] ?? 0, 120 ), + max( $params['height'] ?? 0, 120 ), + $msg + ); + } + + function getHttpStatusCode() { + return 400; + } +} -- 2.20.1