summaryrefslogtreecommitdiff
path: root/www/wiki/includes/gallery
diff options
context:
space:
mode:
authorYaco <franco@reevo.org>2020-06-04 11:01:00 -0300
committerYaco <franco@reevo.org>2020-06-04 11:01:00 -0300
commitfc7369835258467bf97eb64f184b93691f9a9fd5 (patch)
treedaabd60089d2dd76d9f5fb416b005fbe159c799d /www/wiki/includes/gallery
first commit
Diffstat (limited to 'www/wiki/includes/gallery')
-rw-r--r--www/wiki/includes/gallery/ImageGalleryBase.php391
-rw-r--r--www/wiki/includes/gallery/NolinesImageGallery.php37
-rw-r--r--www/wiki/includes/gallery/PackedImageGallery.php112
-rw-r--r--www/wiki/includes/gallery/PackedOverlayImageGallery.php64
-rw-r--r--www/wiki/includes/gallery/SlideshowImageGallery.php41
-rw-r--r--www/wiki/includes/gallery/TraditionalImageGallery.php385
6 files changed, 1030 insertions, 0 deletions
diff --git a/www/wiki/includes/gallery/ImageGalleryBase.php b/www/wiki/includes/gallery/ImageGalleryBase.php
new file mode 100644
index 00000000..09e40a28
--- /dev/null
+++ b/www/wiki/includes/gallery/ImageGalleryBase.php
@@ -0,0 +1,391 @@
+<?php
+/**
+ * Image gallery.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
+/**
+ * Image gallery
+ *
+ * Add images to the gallery using add(), then render that list to HTML using toHTML().
+ *
+ * @ingroup Media
+ */
+abstract class ImageGalleryBase extends ContextSource {
+ /**
+ * @var array Gallery images
+ */
+ protected $mImages;
+
+ /**
+ * @var bool Whether to show the filesize in bytes in categories
+ */
+ protected $mShowBytes;
+
+ /**
+ * @var bool Whether to show the dimensions in categories
+ */
+ protected $mShowDimensions;
+
+ /**
+ * @var bool Whether to show the filename. Default: true
+ */
+ protected $mShowFilename;
+
+ /**
+ * @var string Gallery mode. Default: traditional
+ */
+ protected $mMode;
+
+ /**
+ * @var bool|string Gallery caption. Default: false
+ */
+ protected $mCaption = false;
+
+ /**
+ * Length to truncate filename to in caption when using "showfilename".
+ * A value of 'true' will truncate the filename to one line using CSS
+ * and will be the behaviour after deprecation.
+ *
+ * @var bool|int
+ */
+ protected $mCaptionLength = true;
+
+ /**
+ * @var bool Hide blacklisted images?
+ */
+ protected $mHideBadImages;
+
+ /**
+ * @var Parser Registered parser object for output callbacks
+ */
+ public $mParser;
+
+ /**
+ * @var Title Contextual title, used when images are being screened against
+ * the bad image list
+ */
+ protected $contextTitle = false;
+
+ /** @var array */
+ protected $mAttribs = [];
+
+ /** @var bool */
+ static private $modeMapping = false;
+
+ /**
+ * Get a new image gallery. This is the method other callers
+ * should use to get a gallery.
+ *
+ * @param string|bool $mode Mode to use. False to use the default
+ * @param IContextSource|null $context
+ * @return ImageGalleryBase
+ * @throws MWException
+ */
+ static function factory( $mode = false, IContextSource $context = null ) {
+ global $wgContLang;
+ self::loadModes();
+ if ( !$context ) {
+ $context = RequestContext::getMainAndWarn( __METHOD__ );
+ }
+ if ( !$mode ) {
+ $galleryOptions = $context->getConfig()->get( 'GalleryOptions' );
+ $mode = $galleryOptions['mode'];
+ }
+
+ $mode = $wgContLang->lc( $mode );
+
+ if ( isset( self::$modeMapping[$mode] ) ) {
+ $class = self::$modeMapping[$mode];
+ return new $class( $mode, $context );
+ } else {
+ throw new MWException( "No gallery class registered for mode $mode" );
+ }
+ }
+
+ private static function loadModes() {
+ if ( self::$modeMapping === false ) {
+ self::$modeMapping = [
+ 'traditional' => TraditionalImageGallery::class,
+ 'nolines' => NolinesImageGallery::class,
+ 'packed' => PackedImageGallery::class,
+ 'packed-hover' => PackedHoverImageGallery::class,
+ 'packed-overlay' => PackedOverlayImageGallery::class,
+ 'slideshow' => SlideshowImageGallery::class,
+ ];
+ // Allow extensions to make a new gallery format.
+ Hooks::run( 'GalleryGetModes', [ &self::$modeMapping ] );
+ }
+ }
+
+ /**
+ * Create a new image gallery object.
+ *
+ * You should not call this directly, but instead use
+ * ImageGalleryBase::factory().
+ * @param string $mode
+ * @param IContextSource|null $context
+ */
+ function __construct( $mode = 'traditional', IContextSource $context = null ) {
+ if ( $context ) {
+ $this->setContext( $context );
+ }
+
+ $galleryOptions = $this->getConfig()->get( 'GalleryOptions' );
+ $this->mImages = [];
+ $this->mShowBytes = $galleryOptions['showBytes'];
+ $this->mShowDimensions = $galleryOptions['showDimensions'];
+ $this->mShowFilename = true;
+ $this->mParser = false;
+ $this->mHideBadImages = false;
+ $this->mPerRow = $galleryOptions['imagesPerRow'];
+ $this->mWidths = $galleryOptions['imageWidth'];
+ $this->mHeights = $galleryOptions['imageHeight'];
+ $this->mCaptionLength = $galleryOptions['captionLength'];
+ $this->mMode = $mode;
+ }
+
+ /**
+ * Register a parser object. If you do not set this
+ * and the output of this gallery ends up in parser
+ * cache, the javascript will break!
+ *
+ * @note This also triggers using the page's target
+ * language instead of the user language.
+ *
+ * @param Parser $parser
+ */
+ function setParser( $parser ) {
+ $this->mParser = $parser;
+ }
+
+ /**
+ * Set bad image flag
+ * @param bool $flag
+ */
+ function setHideBadImages( $flag = true ) {
+ $this->mHideBadImages = $flag;
+ }
+
+ /**
+ * Set the caption (as plain text)
+ *
+ * @param string $caption
+ */
+ function setCaption( $caption ) {
+ $this->mCaption = htmlspecialchars( $caption );
+ }
+
+ /**
+ * Set the caption (as HTML)
+ *
+ * @param string $caption
+ */
+ public function setCaptionHtml( $caption ) {
+ $this->mCaption = $caption;
+ }
+
+ /**
+ * Set how many images will be displayed per row.
+ *
+ * @param int $num Integer >= 0; If perrow=0 the gallery layout will adapt
+ * to screensize invalid numbers will be rejected
+ */
+ public function setPerRow( $num ) {
+ if ( $num >= 0 ) {
+ $this->mPerRow = (int)$num;
+ }
+ }
+
+ /**
+ * Set how wide each image will be, in pixels.
+ *
+ * @param string $num Number. Unit other than 'px is invalid. Invalid numbers
+ * and those below 0 are ignored.
+ */
+ public function setWidths( $num ) {
+ $parsed = Parser::parseWidthParam( $num, false );
+ if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
+ $this->mWidths = $parsed['width'];
+ }
+ }
+
+ /**
+ * Set how high each image will be, in pixels.
+ *
+ * @param string $num Number. Unit other than 'px is invalid. Invalid numbers
+ * and those below 0 are ignored.
+ */
+ public function setHeights( $num ) {
+ $parsed = Parser::parseWidthParam( $num, false );
+ if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
+ $this->mHeights = $parsed['width'];
+ }
+ }
+
+ /**
+ * Allow setting additional options. This is meant
+ * to allow extensions to add additional parameters to
+ * <gallery> parser tag.
+ *
+ * @param array $options Attributes of gallery tag
+ */
+ public function setAdditionalOptions( $options ) {
+ }
+
+ /**
+ * Add an image to the gallery.
+ *
+ * @param Title $title Title object of the image that is added to the gallery
+ * @param string $html Additional HTML text to be shown. The name and size
+ * of the image are always shown.
+ * @param string $alt Alt text for the image
+ * @param string $link Override image link (optional)
+ * @param array $handlerOpts Array of options for image handler (aka page number)
+ */
+ function add( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
+ if ( $title instanceof File ) {
+ // Old calling convention
+ $title = $title->getTitle();
+ }
+ $this->mImages[] = [ $title, $html, $alt, $link, $handlerOpts ];
+ wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" );
+ }
+
+ /**
+ * Add an image at the beginning of the gallery.
+ *
+ * @param Title $title Title object of the image that is added to the gallery
+ * @param string $html Additional HTML text to be shown. The name and size
+ * of the image are always shown.
+ * @param string $alt Alt text for the image
+ * @param string $link Override image link (optional)
+ * @param array $handlerOpts Array of options for image handler (aka page number)
+ */
+ function insert( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
+ if ( $title instanceof File ) {
+ // Old calling convention
+ $title = $title->getTitle();
+ }
+ array_unshift( $this->mImages, [ &$title, $html, $alt, $link, $handlerOpts ] );
+ }
+
+ /**
+ * Returns the list of images this gallery contains
+ * @return array
+ */
+ public function getImages() {
+ return $this->mImages;
+ }
+
+ /**
+ * isEmpty() returns true if the gallery contains no images
+ * @return bool
+ */
+ function isEmpty() {
+ return empty( $this->mImages );
+ }
+
+ /**
+ * Enable/Disable showing of the dimensions of an image in the gallery.
+ * Enabled by default.
+ *
+ * @param bool $f Set to false to disable
+ */
+ function setShowDimensions( $f ) {
+ $this->mShowDimensions = (bool)$f;
+ }
+
+ /**
+ * Enable/Disable showing of the file size of an image in the gallery.
+ * Enabled by default.
+ *
+ * @param bool $f Set to false to disable
+ */
+ function setShowBytes( $f ) {
+ $this->mShowBytes = (bool)$f;
+ }
+
+ /**
+ * Enable/Disable showing of the filename of an image in the gallery.
+ * Enabled by default.
+ *
+ * @param bool $f Set to false to disable
+ */
+ function setShowFilename( $f ) {
+ $this->mShowFilename = (bool)$f;
+ }
+
+ /**
+ * Set arbitrary attributes to go on the HTML gallery output element.
+ * Should be suitable for a <ul> element.
+ *
+ * Note -- if taking from user input, you should probably run through
+ * Sanitizer::validateAttributes() first.
+ *
+ * @param array $attribs Array of HTML attribute pairs
+ */
+ function setAttributes( $attribs ) {
+ $this->mAttribs = $attribs;
+ }
+
+ /**
+ * Display an html representation of the gallery
+ *
+ * @return string The html
+ */
+ abstract public function toHTML();
+
+ /**
+ * @return int Number of images in the gallery
+ */
+ public function count() {
+ return count( $this->mImages );
+ }
+
+ /**
+ * Set the contextual title
+ *
+ * @param Title $title Contextual title
+ */
+ public function setContextTitle( $title ) {
+ $this->contextTitle = $title;
+ }
+
+ /**
+ * Get the contextual title, if applicable
+ *
+ * @return Title|bool Title or false
+ */
+ public function getContextTitle() {
+ return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
+ ? $this->contextTitle
+ : false;
+ }
+
+ /**
+ * Determines the correct language to be used for this image gallery
+ * @return Language
+ */
+ protected function getRenderLang() {
+ return $this->mParser
+ ? $this->mParser->getTargetLanguage()
+ : $this->getLanguage();
+ }
+}
diff --git a/www/wiki/includes/gallery/NolinesImageGallery.php b/www/wiki/includes/gallery/NolinesImageGallery.php
new file mode 100644
index 00000000..70f5bd93
--- /dev/null
+++ b/www/wiki/includes/gallery/NolinesImageGallery.php
@@ -0,0 +1,37 @@
+<?php
+/**
+ * Nolines image gallery. Like "traditional" but without borders and
+ * less padding.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
+class NolinesImageGallery extends TraditionalImageGallery {
+ protected function getThumbPadding() {
+ return 0;
+ }
+
+ protected function getGBBorders() {
+ // This accounts for extra space between <li> elements.
+ return 4;
+ }
+
+ protected function getVPad( $boxHeight, $thumbHeight ) {
+ return 0;
+ }
+}
diff --git a/www/wiki/includes/gallery/PackedImageGallery.php b/www/wiki/includes/gallery/PackedImageGallery.php
new file mode 100644
index 00000000..2e4836a5
--- /dev/null
+++ b/www/wiki/includes/gallery/PackedImageGallery.php
@@ -0,0 +1,112 @@
+<?php
+/**
+ * Packed image gallery. All images adjusted to be same height.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
+class PackedImageGallery extends TraditionalImageGallery {
+ function __construct( $mode = 'traditional', IContextSource $context = null ) {
+ parent::__construct( $mode, $context );
+ // Does not support per row option.
+ $this->mPerRow = 0;
+ }
+
+ /**
+ * We artificially have 1.5 the resolution neccessary so that
+ * we can scale it up by that much on the client side, without
+ * worrying about requesting a new image.
+ */
+ const SCALE_FACTOR = 1.5;
+
+ protected function getVPad( $boxHeight, $thumbHeight ) {
+ return ( $this->getThumbPadding() + $boxHeight - $thumbHeight / self::SCALE_FACTOR ) / 2;
+ }
+
+ protected function getThumbPadding() {
+ return 0;
+ }
+
+ protected function getGBPadding() {
+ return 2;
+ }
+
+ /**
+ * @param File $img The file being transformed. May be false
+ * @return array
+ */
+ protected function getThumbParams( $img ) {
+ if ( $img && $img->getMediaType() === MEDIATYPE_AUDIO ) {
+ $width = $this->mWidths;
+ } else {
+ // We want the width not to be the constraining
+ // factor, so use random big number.
+ $width = $this->mHeights * 10 + 100;
+ }
+
+ // self::SCALE_FACTOR so the js has some room to manipulate sizes.
+ return [
+ 'width' => $width * self::SCALE_FACTOR,
+ 'height' => $this->mHeights * self::SCALE_FACTOR,
+ ];
+ }
+
+ protected function getThumbDivWidth( $thumbWidth ) {
+ // Require at least 60px wide, so caption is wide enough to work.
+ if ( $thumbWidth < 60 * self::SCALE_FACTOR ) {
+ $thumbWidth = 60 * self::SCALE_FACTOR;
+ }
+
+ return $thumbWidth / self::SCALE_FACTOR + $this->getThumbPadding();
+ }
+
+ /**
+ * @param MediaTransformOutput|bool $thumb The thumbnail, or false if no
+ * thumb (which can happen)
+ * @return float
+ */
+ protected function getGBWidth( $thumb ) {
+ $thumbWidth = $thumb ? $thumb->getWidth() : $this->mWidths * self::SCALE_FACTOR;
+
+ return $this->getThumbDivWidth( $thumbWidth ) + $this->getGBPadding();
+ }
+
+ protected function adjustImageParameters( $thumb, &$imageParameters ) {
+ // Re-adjust back to normal size.
+ $imageParameters['override-width'] = ceil( $thumb->getWidth() / self::SCALE_FACTOR );
+ $imageParameters['override-height'] = ceil( $thumb->getHeight() / self::SCALE_FACTOR );
+ }
+
+ /**
+ * Add javascript which auto-justifies the rows by manipulating the image sizes.
+ * Also ensures that the hover version of this degrades gracefully.
+ * @return array
+ */
+ protected function getModules() {
+ return [ 'mediawiki.page.gallery' ];
+ }
+
+ /**
+ * Do not support per-row on packed. It really doesn't work
+ * since the images have varying widths.
+ * @param int $num
+ */
+ public function setPerRow( $num ) {
+ return;
+ }
+}
diff --git a/www/wiki/includes/gallery/PackedOverlayImageGallery.php b/www/wiki/includes/gallery/PackedOverlayImageGallery.php
new file mode 100644
index 00000000..0a5a457f
--- /dev/null
+++ b/www/wiki/includes/gallery/PackedOverlayImageGallery.php
@@ -0,0 +1,64 @@
+<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
+/**
+ * Packed overlay image gallery. All images adjusted to be same height and
+ * image caption being placed over top of image.
+ */
+class PackedOverlayImageGallery extends PackedImageGallery {
+ /**
+ * Add the wrapper html around the thumb's caption
+ *
+ * @param string $galleryText The caption
+ * @param MediaTransformOutput|bool $thumb The thumb this caption is for
+ * or false for bad image.
+ * @return string
+ */
+ protected function wrapGalleryText( $galleryText, $thumb ) {
+ // If we have no text, do not output anything to avoid
+ // ugly white overlay.
+ if ( trim( $galleryText ) === '' ) {
+ return '';
+ }
+
+ # ATTENTION: The newline after <div class="gallerytext"> is needed to
+ # accommodate htmltidy which in version 4.8.6 generated crackpot HTML
+ # in its absence, see: https://phabricator.wikimedia.org/T3765
+ # -Ævar
+
+ $thumbWidth = $this->getGBWidth( $thumb ) - $this->getThumbPadding() - $this->getGBPadding();
+ $captionWidth = ceil( $thumbWidth - 20 );
+
+ $outerWrapper = '<div class="gallerytextwrapper" style="width: ' . $captionWidth . 'px">';
+
+ return "\n\t\t\t" . $outerWrapper . '<div class="gallerytext">' . "\n"
+ . $galleryText
+ . "\n\t\t\t</div></div>";
+ }
+}
+
+/**
+ * Same as Packed except different CSS is applied to make the
+ * caption only show up on hover. If a touch screen is detected,
+ * falls back to PackedHoverGallery. Degrades gracefully for
+ * screen readers.
+ */
+class PackedHoverImageGallery extends PackedOverlayImageGallery {
+}
diff --git a/www/wiki/includes/gallery/SlideshowImageGallery.php b/www/wiki/includes/gallery/SlideshowImageGallery.php
new file mode 100644
index 00000000..f29c565f
--- /dev/null
+++ b/www/wiki/includes/gallery/SlideshowImageGallery.php
@@ -0,0 +1,41 @@
+<?php
+/**
+ * A slideshow gallery shows one image at a time with controls to move around.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
+class SlideshowImageGallery extends TraditionalImageGallery {
+ function __construct( $mode = 'traditional', IContextSource $context = null ) {
+ parent::__construct( $mode, $context );
+ // Does not support per row option.
+ $this->mPerRow = 0;
+ }
+
+ /**
+ * Add javascript adds interface elements
+ * @return array
+ */
+ protected function getModules() {
+ return [ 'mediawiki.page.gallery.slideshow' ];
+ }
+
+ public function setAdditionalOptions( $params ) {
+ $this->mAttribs['data-showthumbnails'] = isset( $params['showthumbnails'] );
+ }
+}
diff --git a/www/wiki/includes/gallery/TraditionalImageGallery.php b/www/wiki/includes/gallery/TraditionalImageGallery.php
new file mode 100644
index 00000000..1cb7e6d2
--- /dev/null
+++ b/www/wiki/includes/gallery/TraditionalImageGallery.php
@@ -0,0 +1,385 @@
+<?php
+/**
+ * Image gallery.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
+class TraditionalImageGallery extends ImageGalleryBase {
+ /**
+ * Return a HTML representation of the image gallery
+ *
+ * For each image in the gallery, display
+ * - a thumbnail
+ * - the image name
+ * - the additional text provided when adding the image
+ * - the size of the image
+ *
+ * @return string
+ */
+ function toHTML() {
+ if ( $this->mPerRow > 0 ) {
+ $maxwidth = $this->mPerRow * ( $this->mWidths + $this->getAllPadding() );
+ $oldStyle = isset( $this->mAttribs['style'] ) ? $this->mAttribs['style'] : '';
+ # _width is ignored by any sane browser. IE6 doesn't know max-width
+ # so it uses _width instead
+ $this->mAttribs['style'] = "max-width: {$maxwidth}px;_width: {$maxwidth}px;" .
+ $oldStyle;
+ }
+
+ $attribs = Sanitizer::mergeAttributes(
+ [ 'class' => 'gallery mw-gallery-' . $this->mMode ], $this->mAttribs );
+
+ $modules = $this->getModules();
+
+ if ( $this->mParser ) {
+ $this->mParser->getOutput()->addModules( $modules );
+ $this->mParser->getOutput()->addModuleStyles( 'mediawiki.page.gallery.styles' );
+ } else {
+ $this->getOutput()->addModules( $modules );
+ $this->getOutput()->addModuleStyles( 'mediawiki.page.gallery.styles' );
+ }
+ $output = Xml::openElement( 'ul', $attribs );
+ if ( $this->mCaption ) {
+ $output .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
+ }
+
+ if ( $this->mShowFilename ) {
+ // Preload LinkCache info for when generating links
+ // of the filename below
+ $lb = new LinkBatch();
+ foreach ( $this->mImages as $img ) {
+ $lb->addObj( $img[0] );
+ }
+ $lb->execute();
+ }
+
+ $lang = $this->getRenderLang();
+ # Output each image...
+ foreach ( $this->mImages as $pair ) {
+ /** @var Title $nt */
+ $nt = $pair[0];
+ $text = $pair[1]; # "text" means "caption" here
+ $alt = $pair[2];
+ $link = $pair[3];
+
+ $descQuery = false;
+ if ( $nt->getNamespace() === NS_FILE ) {
+ # Get the file...
+ if ( $this->mParser instanceof Parser ) {
+ # Give extensions a chance to select the file revision for us
+ $options = [];
+ Hooks::run( 'BeforeParserFetchFileAndTitle',
+ [ $this->mParser, $nt, &$options, &$descQuery ] );
+ # Fetch and register the file (file title may be different via hooks)
+ list( $img, $nt ) = $this->mParser->fetchFileAndTitle( $nt, $options );
+ } else {
+ $img = wfFindFile( $nt );
+ }
+ } else {
+ $img = false;
+ }
+
+ $params = $this->getThumbParams( $img );
+ // $pair[4] is per image handler options
+ $transformOptions = $params + $pair[4];
+
+ $thumb = false;
+
+ if ( !$img ) {
+ # We're dealing with a non-image, spit out the name and be done with it.
+ $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: '
+ . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">'
+ . htmlspecialchars( $nt->getText() ) . '</div>';
+
+ if ( $this->mParser instanceof Parser ) {
+ $this->mParser->addTrackingCategory( 'broken-file-category' );
+ }
+ } elseif ( $this->mHideBadImages
+ && wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() )
+ ) {
+ # The image is blacklisted, just show it as a text link.
+ $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: ' .
+ ( $this->getThumbPadding() + $this->mHeights ) . 'px;">' .
+ Linker::linkKnown(
+ $nt,
+ htmlspecialchars( $nt->getText() )
+ ) .
+ '</div>';
+ } else {
+ $thumb = $img->transform( $transformOptions );
+ if ( !$thumb ) {
+ # Error generating thumbnail.
+ $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: '
+ . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">'
+ . htmlspecialchars( $img->getLastError() ) . '</div>';
+ } else {
+ /** @var MediaTransformOutput $thumb */
+ $vpad = $this->getVPad( $this->mHeights, $thumb->getHeight() );
+
+ $imageParameters = [
+ 'desc-link' => true,
+ 'desc-query' => $descQuery,
+ 'alt' => $alt,
+ 'custom-url-link' => $link
+ ];
+
+ // In the absence of both alt text and caption, fall back on
+ // providing screen readers with the filename as alt text
+ if ( $alt == '' && $text == '' ) {
+ $imageParameters['alt'] = $nt->getText();
+ }
+
+ $this->adjustImageParameters( $thumb, $imageParameters );
+
+ Linker::processResponsiveImages( $img, $thumb, $transformOptions );
+
+ # Set both fixed width and min-height.
+ $thumbhtml = "\n\t\t\t"
+ . '<div class="thumb" style="width: '
+ . $this->getThumbDivWidth( $thumb->getWidth() ) . 'px;">'
+ # Auto-margin centering for block-level elements. Needed
+ # now that we have video handlers since they may emit block-
+ # level elements as opposed to simple <img> tags. ref
+ # http://css-discuss.incutio.com/?page=CenteringBlockElement
+ . '<div style="margin:' . $vpad . 'px auto;">'
+ . $thumb->toHtml( $imageParameters ) . '</div></div>';
+
+ // Call parser transform hook
+ /** @var MediaHandler $handler */
+ $handler = $img->getHandler();
+ if ( $this->mParser && $handler ) {
+ $handler->parserTransformHook( $this->mParser, $img );
+ }
+ }
+ }
+
+ // @todo Code is incomplete.
+ // $linkTarget = Title::newFromText( $wgContLang->getNsText( MWNamespace::getUser() ) .
+ // ":{$ut}" );
+ // $ul = Linker::link( $linkTarget, $ut );
+
+ $meta = [];
+ if ( $img ) {
+ if ( $this->mShowDimensions ) {
+ $meta[] = $img->getDimensionsString();
+ }
+ if ( $this->mShowBytes ) {
+ $meta[] = htmlspecialchars( $lang->formatSize( $img->getSize() ) );
+ }
+ } elseif ( $this->mShowDimensions || $this->mShowBytes ) {
+ $meta[] = $this->msg( 'filemissing' )->escaped();
+ }
+ $meta = $lang->semicolonList( $meta );
+ if ( $meta ) {
+ $meta .= "<br />\n";
+ }
+
+ $textlink = $this->mShowFilename ?
+ // Preloaded into LinkCache above
+ Linker::linkKnown(
+ $nt,
+ htmlspecialchars(
+ is_int( $this->getCaptionLength() ) ?
+ $lang->truncate( $nt->getText(), $this->getCaptionLength() ) :
+ $nt->getText()
+ ),
+ [
+ 'class' => 'galleryfilename' .
+ ( $this->getCaptionLength() === true ? ' galleryfilename-truncate' : '' )
+ ]
+ ) . "\n" :
+ '';
+
+ $galleryText = $textlink . $text . $meta;
+ $galleryText = $this->wrapGalleryText( $galleryText, $thumb );
+
+ $gbWidth = $this->getGBWidth( $thumb ) . 'px';
+ if ( $this->getGBWidthOverwrite( $thumb ) ) {
+ $gbWidth = $this->getGBWidthOverwrite( $thumb );
+ }
+ # Weird double wrapping (the extra div inside the li) needed due to FF2 bug
+ # Can be safely removed if FF2 falls completely out of existence
+ $output .= "\n\t\t" . '<li class="gallerybox" style="width: '
+ . $gbWidth . '">'
+ . '<div style="width: ' . $gbWidth . '">'
+ . $thumbhtml
+ . $galleryText
+ . "\n\t\t</div></li>";
+ }
+ $output .= "\n</ul>";
+
+ return $output;
+ }
+
+ /**
+ * Add the wrapper html around the thumb's caption
+ *
+ * @param string $galleryText The caption
+ * @param MediaTransformOutput|bool $thumb The thumb this caption is for
+ * or false for bad image.
+ * @return string
+ */
+ protected function wrapGalleryText( $galleryText, $thumb ) {
+ # ATTENTION: The newline after <div class="gallerytext"> is needed to
+ # accommodate htmltidy which in version 4.8.6 generated crackpot html in
+ # its absence, see: https://phabricator.wikimedia.org/T3765
+ # -Ævar
+
+ return "\n\t\t\t" . '<div class="gallerytext">' . "\n"
+ . $galleryText
+ . "\n\t\t\t</div>";
+ }
+
+ /**
+ * How much padding the thumb has between the image and the inner div
+ * that contains the border. This is for both vertical and horizontal
+ * padding. (However, it is cut in half in the vertical direction).
+ * @return int
+ */
+ protected function getThumbPadding() {
+ return 30;
+ }
+
+ /**
+ * @note GB stands for gallerybox (as in the <li class="gallerybox"> element)
+ *
+ * @return int
+ */
+ protected function getGBPadding() {
+ return 5;
+ }
+
+ /**
+ * Get how much extra space the borders around the image takes up.
+ *
+ * For this mode, it is 2px borders on each side + 2px implied padding on
+ * each side from the stylesheet, giving us 2*2+2*2 = 8.
+ * @return int
+ */
+ protected function getGBBorders() {
+ return 8;
+ }
+
+ /**
+ * Length to truncate filename to in caption when using "showfilename" (if int).
+ * A value of 'true' will truncate the filename to one line using CSS, while
+ * 'false' will disable truncating.
+ *
+ * @return int|bool
+ */
+ protected function getCaptionLength() {
+ return $this->mCaptionLength;
+ }
+
+ /**
+ * Get total padding.
+ *
+ * @return int Number of pixels of whitespace surrounding the thumbnail.
+ */
+ protected function getAllPadding() {
+ return $this->getThumbPadding() + $this->getGBPadding() + $this->getGBBorders();
+ }
+
+ /**
+ * Get vertical padding for a thumbnail
+ *
+ * Generally this is the total height minus how high the thumb is.
+ *
+ * @param int $boxHeight How high we want the box to be.
+ * @param int $thumbHeight How high the thumbnail is.
+ * @return int Vertical padding to add on each side.
+ */
+ protected function getVPad( $boxHeight, $thumbHeight ) {
+ return ( $this->getThumbPadding() + $boxHeight - $thumbHeight ) / 2;
+ }
+
+ /**
+ * Get the transform parameters for a thumbnail.
+ *
+ * @param File $img The file in question. May be false for invalid image
+ * @return array
+ */
+ protected function getThumbParams( $img ) {
+ return [
+ 'width' => $this->mWidths,
+ 'height' => $this->mHeights
+ ];
+ }
+
+ /**
+ * Get the width of the inner div that contains the thumbnail in
+ * question. This is the div with the class of "thumb".
+ *
+ * @param int $thumbWidth The width of the thumbnail.
+ * @return int Width of inner thumb div.
+ */
+ protected function getThumbDivWidth( $thumbWidth ) {
+ return $this->mWidths + $this->getThumbPadding();
+ }
+
+ /**
+ * Computed width of gallerybox <li>.
+ *
+ * Generally is the width of the image, plus padding on image
+ * plus padding on gallerybox.
+ *
+ * @note Important: parameter will be false if no thumb used.
+ * @param MediaTransformOutput|bool $thumb MediaTransformObject object or false.
+ * @return int Width of gallerybox element
+ */
+ protected function getGBWidth( $thumb ) {
+ return $this->mWidths + $this->getThumbPadding() + $this->getGBPadding();
+ }
+
+ /**
+ * Allows overwriting the computed width of the gallerybox <li> with a string,
+ * like '100%'.
+ *
+ * Generally is the width of the image, plus padding on image
+ * plus padding on gallerybox.
+ *
+ * @note Important: parameter will be false if no thumb used.
+ * @param MediaTransformOutput|bool $thumb MediaTransformObject object or false.
+ * @return bool|string Ignored if false.
+ */
+ protected function getGBWidthOverwrite( $thumb ) {
+ return false;
+ }
+
+ /**
+ * Get a list of modules to include in the page.
+ *
+ * Primarily intended for subclasses.
+ *
+ * @return array Modules to include
+ */
+ protected function getModules() {
+ return [];
+ }
+
+ /**
+ * Adjust the image parameters for a thumbnail.
+ *
+ * Used by a subclass to insert extra high resolution images.
+ * @param MediaTransformOutput $thumb The thumbnail
+ * @param array &$imageParameters Array of options
+ */
+ protected function adjustImageParameters( $thumb, &$imageParameters ) {
+ }
+}