Truncate gallery caption filenames with CSS
[lhc/web/wiklou.git] / includes / gallery / TraditionalImageGallery.php
index 181c7b8..f6527b8 100644 (file)
@@ -43,7 +43,7 @@ class TraditionalImageGallery extends ImageGalleryBase {
                }
 
                $attribs = Sanitizer::mergeAttributes(
-                       array( 'class' => 'gallery mw-gallery-' . $this->mMode ), $this->mAttribs );
+                       [ 'class' => 'gallery mw-gallery-' . $this->mMode ], $this->mAttribs );
 
                $modules = $this->getModules();
 
@@ -59,6 +59,16 @@ class TraditionalImageGallery extends ImageGalleryBase {
                        $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 ) {
@@ -73,9 +83,9 @@ class TraditionalImageGallery extends ImageGalleryBase {
                                # Get the file...
                                if ( $this->mParser instanceof Parser ) {
                                        # Give extensions a chance to select the file revision for us
-                                       $options = array();
+                                       $options = [];
                                        Hooks::run( 'BeforeParserFetchFileAndTitle',
-                                               array( $this->mParser, $nt, &$options, &$descQuery ) );
+                                               [ $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 {
@@ -111,48 +121,51 @@ class TraditionalImageGallery extends ImageGalleryBase {
                                                htmlspecialchars( $nt->getText() )
                                        ) .
                                        '</div>';
-                       } elseif ( !( $thumb = $img->transform( $transformOptions ) ) ) {
-                               # 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 = array(
-                                       '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 );
+                               $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 );
+                                       }
                                }
                        }
 
@@ -173,10 +186,19 @@ class TraditionalImageGallery extends ImageGalleryBase {
                        }
 
                        $textlink = $this->mShowFilename ?
+                               // Preloaded into LinkCache above
                                Linker::linkKnown(
                                        $nt,
-                                       htmlspecialchars( $lang->truncate( $nt->getText(), $this->mCaptionLength ) )
-                               ) . "<br />\n" :
+                                       htmlspecialchars(
+                                               $this->mCaptionLength !== true ?
+                                                       $lang->truncate( $nt->getText(), $this->mCaptionLength ) :
+                                                       $nt->getText()
+                                       ),
+                                       [
+                                               'class' => 'galleryfilename' .
+                                                       ( $this->mCaptionLength === true ? ' galleryfilename-truncate' : '' )
+                                       ]
+                               ) . "\n" :
                                '';
 
                        $galleryText = $textlink . $text . $fileSize;
@@ -274,10 +296,10 @@ class TraditionalImageGallery extends ImageGalleryBase {
         * @return array
         */
        protected function getThumbParams( $img ) {
-               return array(
+               return [
                        'width' => $this->mWidths,
                        'height' => $this->mHeights
-               );
+               ];
        }
 
        /**
@@ -313,7 +335,7 @@ class TraditionalImageGallery extends ImageGalleryBase {
         * @return array Modules to include
         */
        protected function getModules() {
-               return array();
+               return [];
        }
 
        /**