Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 161 |
|
0.00% |
0 / 15 |
CRAP | |
0.00% |
0 / 1 |
| TraditionalImageGallery | |
0.00% |
0 / 160 |
|
0.00% |
0 / 15 |
3080 | |
0.00% |
0 / 1 |
| toHTML | |
0.00% |
0 / 134 |
|
0.00% |
0 / 1 |
1560 | |||
| getCaptionHtml | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
| wrapGalleryText | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getThumbPadding | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getGBPadding | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getGBBorders | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getCaptionLength | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getAllPadding | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getVPad | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getThumbParams | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| getThumbDivWidth | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getGBWidth | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getGBWidthOverwrite | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getModules | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| adjustImageParameters | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @license GPL-2.0-or-later |
| 5 | * @file |
| 6 | */ |
| 7 | |
| 8 | namespace MediaWiki\Gallery; |
| 9 | |
| 10 | use MediaHandler; |
| 11 | use MediaTransformError; |
| 12 | use MediaTransformOutput; |
| 13 | use MediaWiki\FileRepo\File\File; |
| 14 | use MediaWiki\HookContainer\HookRunner; |
| 15 | use MediaWiki\Html\Html; |
| 16 | use MediaWiki\Language\Language; |
| 17 | use MediaWiki\Linker\Linker; |
| 18 | use MediaWiki\Linker\LinkRenderer; |
| 19 | use MediaWiki\MediaWikiServices; |
| 20 | use MediaWiki\Parser\Parser; |
| 21 | use MediaWiki\Title\Title; |
| 22 | use Wikimedia\Assert\Assert; |
| 23 | |
| 24 | /** |
| 25 | * Image gallery. |
| 26 | */ |
| 27 | class TraditionalImageGallery extends ImageGalleryBase { |
| 28 | /** |
| 29 | * Return a HTML representation of the image gallery |
| 30 | * |
| 31 | * For each image in the gallery, display |
| 32 | * - a thumbnail |
| 33 | * - the image name |
| 34 | * - the additional text provided when adding the image |
| 35 | * - the size of the image |
| 36 | * |
| 37 | * @return string |
| 38 | */ |
| 39 | public function toHTML() { |
| 40 | $resolveFilesViaParser = $this->mParser instanceof Parser; |
| 41 | if ( $resolveFilesViaParser ) { |
| 42 | $parserOutput = $this->mParser->getOutput(); |
| 43 | $repoGroup = null; |
| 44 | $linkRenderer = $this->mParser->getLinkRenderer(); |
| 45 | $badFileLookup = $this->mParser->getBadFileLookup(); |
| 46 | } else { |
| 47 | $parserOutput = $this->getOutput(); |
| 48 | $services = MediaWikiServices::getInstance(); |
| 49 | $repoGroup = $services->getRepoGroup(); |
| 50 | $linkRenderer = $services->getLinkRenderer(); |
| 51 | $badFileLookup = $services->getBadFileLookup(); |
| 52 | } |
| 53 | |
| 54 | Html::addClass( $this->mAttribs['class'], 'gallery' ); |
| 55 | Html::addClass( $this->mAttribs['class'], 'mw-gallery-' . $this->mMode ); |
| 56 | |
| 57 | if ( $this->mPerRow > 0 ) { |
| 58 | $maxwidth = $this->mPerRow * ( $this->mWidths + $this->getAllPadding() ); |
| 59 | $oldStyle = $this->mAttribs['style'] ?? ''; |
| 60 | $this->mAttribs['style'] = "max-width: {$maxwidth}px;" . $oldStyle; |
| 61 | } |
| 62 | |
| 63 | $parserOutput->addModules( $this->getModules() ); |
| 64 | $parserOutput->addModuleStyles( [ 'mediawiki.page.gallery.styles' ] ); |
| 65 | $output = Html::openElement( 'ul', $this->mAttribs ); |
| 66 | if ( $this->mCaption ) { |
| 67 | $output .= "\n\t" . Html::rawElement( 'li', [ 'class' => 'gallerycaption' ], $this->mCaption ); |
| 68 | } |
| 69 | |
| 70 | if ( $this->mShowFilename ) { |
| 71 | // Preload LinkCache info for when generating links |
| 72 | // of the filename below |
| 73 | $linkBatchFactory = MediaWikiServices::getInstance()->getLinkBatchFactory(); |
| 74 | $lb = $linkBatchFactory->newLinkBatch()->setCaller( __METHOD__ ); |
| 75 | foreach ( $this->mImages as [ $title, /* see below */ ] ) { |
| 76 | $lb->addObj( $title ); |
| 77 | } |
| 78 | $lb->execute(); |
| 79 | } |
| 80 | |
| 81 | $lang = $this->getRenderLang(); |
| 82 | $hookRunner = new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ); |
| 83 | |
| 84 | # Output each image... |
| 85 | foreach ( $this->mImages as [ $nt, $text, $alt, $link, $handlerOpts, $loading, $imageOptions ] ) { |
| 86 | // "text" means "caption" here |
| 87 | /** @var Title $nt */ |
| 88 | |
| 89 | $descQuery = false; |
| 90 | if ( $nt->inNamespace( NS_FILE ) && !$nt->isExternal() ) { |
| 91 | # Get the file... |
| 92 | if ( $resolveFilesViaParser ) { |
| 93 | # Give extensions a chance to select the file revision for us |
| 94 | $options = []; |
| 95 | $hookRunner->onBeforeParserFetchFileAndTitle( |
| 96 | // @phan-suppress-next-line PhanTypeMismatchArgument Type mismatch on pass-by-ref args |
| 97 | $this->mParser, $nt, $options, $descQuery ); |
| 98 | # Fetch and register the file (file title may be different via hooks) |
| 99 | [ $img, $nt ] = $this->mParser->fetchFileAndTitle( $nt, $options ); |
| 100 | } else { |
| 101 | $img = $repoGroup->findFile( $nt ); |
| 102 | } |
| 103 | } else { |
| 104 | $img = false; |
| 105 | } |
| 106 | |
| 107 | $transformOptions = $this->getThumbParams( $img ) + $handlerOpts; |
| 108 | $thumb = $img ? $img->transform( $transformOptions ) : false; |
| 109 | |
| 110 | $rdfaType = 'mw:File'; |
| 111 | |
| 112 | $isBadFile = $img && $thumb && $this->mHideBadImages && |
| 113 | $badFileLookup->isBadFile( $nt->getDBkey(), $this->getContextTitle() ); |
| 114 | |
| 115 | if ( !$img || !$thumb || $thumb->isError() || $isBadFile ) { |
| 116 | $rdfaType = 'mw:Error ' . $rdfaType; |
| 117 | |
| 118 | $currentExists = $img && $img->exists(); |
| 119 | if ( $currentExists && !$thumb ) { |
| 120 | $label = wfMessage( 'thumbnail_error', '' )->text(); |
| 121 | } elseif ( $thumb && $thumb->isError() ) { |
| 122 | Assert::invariant( |
| 123 | $thumb instanceof MediaTransformError, |
| 124 | 'Unknown MediaTransformOutput: ' . get_class( $thumb ) |
| 125 | ); |
| 126 | $label = $thumb->toText(); |
| 127 | } else { |
| 128 | $label = $alt ?? ''; |
| 129 | } |
| 130 | $thumbhtml = Linker::makeBrokenImageLinkObj( |
| 131 | $nt, $label, '', '', '', false, $transformOptions, $currentExists |
| 132 | ); |
| 133 | $thumbhtml = Html::rawElement( 'span', [ 'typeof' => $rdfaType ], $thumbhtml ); |
| 134 | |
| 135 | $thumbhtml = "\n\t\t\t" . Html::rawElement( |
| 136 | 'div', |
| 137 | [ |
| 138 | 'class' => 'thumb', |
| 139 | 'style' => 'height: ' . ( $this->getThumbPadding() + $this->mHeights ) . 'px;' |
| 140 | ], |
| 141 | $thumbhtml |
| 142 | ); |
| 143 | |
| 144 | if ( !$img && $resolveFilesViaParser ) { |
| 145 | $this->mParser->addTrackingCategory( 'broken-file-category' ); |
| 146 | } |
| 147 | } else { |
| 148 | /** @var MediaTransformOutput $thumb */ |
| 149 | $vpad = $this->getVPad( $this->mHeights, $thumb->getHeight() ); |
| 150 | |
| 151 | // Backwards compat before the $imageOptions existed |
| 152 | if ( $imageOptions === null ) { |
| 153 | $imageParameters = [ |
| 154 | 'desc-link' => true, |
| 155 | 'desc-query' => $descQuery, |
| 156 | 'alt' => $alt ?? '', |
| 157 | 'custom-url-link' => $link |
| 158 | ]; |
| 159 | } else { |
| 160 | $params = []; |
| 161 | // An empty alt indicates an image is not a key part of the |
| 162 | // content and that non-visual browsers may omit it from |
| 163 | // rendering. Only set the parameter if it's explicitly |
| 164 | // requested. |
| 165 | if ( $alt !== null ) { |
| 166 | $params['alt'] = $alt; |
| 167 | } |
| 168 | $params['title'] = $imageOptions['title']; |
| 169 | $params['img-class'] = 'mw-file-element'; |
| 170 | $imageParameters = Linker::getImageLinkMTOParams( |
| 171 | $imageOptions, $descQuery, $this->mParser |
| 172 | ) + $params; |
| 173 | } |
| 174 | |
| 175 | if ( $loading === ImageGalleryBase::LOADING_LAZY ) { |
| 176 | $imageParameters['loading'] = 'lazy'; |
| 177 | } |
| 178 | |
| 179 | $this->adjustImageParameters( $thumb, $imageParameters ); |
| 180 | |
| 181 | Linker::processResponsiveImages( $img, $thumb, $transformOptions ); |
| 182 | |
| 183 | $thumbhtml = $thumb->toHtml( $imageParameters ); |
| 184 | $thumbhtml = Html::rawElement( |
| 185 | 'span', [ 'typeof' => $rdfaType ], $thumbhtml |
| 186 | ); |
| 187 | |
| 188 | # Set both fixed width and min-height. |
| 189 | $width = $this->getThumbDivWidth( $thumb->getWidth() ); |
| 190 | $height = $this->getThumbPadding() + $this->mHeights; |
| 191 | $thumbhtml = "\n\t\t\t" . Html::rawElement( 'div', [ |
| 192 | 'class' => 'thumb', |
| 193 | 'style' => "width: {$width}px;" . |
| 194 | ( $this->mMode === 'traditional' ? " height: {$height}px;" : '' ), |
| 195 | ], $thumbhtml ); |
| 196 | |
| 197 | // Call parser transform hook |
| 198 | if ( $resolveFilesViaParser ) { |
| 199 | /** @var MediaHandler $handler */ |
| 200 | $handler = $img->getHandler(); |
| 201 | if ( $handler ) { |
| 202 | $handler->parserTransformHook( $this->mParser, $img ); |
| 203 | } |
| 204 | $this->mParser->modifyImageHtml( |
| 205 | $img, [ 'handler' => $imageParameters ], $thumbhtml ); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | $meta = []; |
| 210 | if ( $img ) { |
| 211 | if ( $this->mShowDimensions ) { |
| 212 | $meta[] = htmlspecialchars( $img->getDimensionsString() ); |
| 213 | } |
| 214 | if ( $this->mShowBytes ) { |
| 215 | $meta[] = htmlspecialchars( $lang->formatSize( $img->getSize() ) ); |
| 216 | } |
| 217 | } elseif ( $this->mShowDimensions || $this->mShowBytes ) { |
| 218 | $meta[] = $this->msg( 'filemissing' )->escaped(); |
| 219 | } |
| 220 | $meta = $lang->semicolonList( $meta ); |
| 221 | if ( $meta ) { |
| 222 | $meta .= Html::rawElement( 'br', [] ) . "\n"; |
| 223 | } |
| 224 | |
| 225 | $textlink = $this->mShowFilename ? |
| 226 | $this->getCaptionHtml( $nt, $lang, $linkRenderer ) : |
| 227 | ''; |
| 228 | |
| 229 | $galleryText = $this->wrapGalleryText( $textlink . $text . $meta, $thumb ); |
| 230 | |
| 231 | $gbWidth = $this->getGBWidthOverwrite( $thumb ) ?: $this->getGBWidth( $thumb ) . 'px'; |
| 232 | # Weird double wrapping (the extra div inside the li) needed due to FF2 bug |
| 233 | # Can be safely removed if FF2 falls completely out of existence |
| 234 | $output .= "\n\t\t" . |
| 235 | Html::rawElement( |
| 236 | 'li', |
| 237 | [ 'class' => 'gallerybox', 'style' => 'width: ' . $gbWidth ], |
| 238 | $thumbhtml |
| 239 | . $galleryText |
| 240 | . "\n\t\t" |
| 241 | ); |
| 242 | } |
| 243 | $output .= "\n" . Html::closeElement( 'ul' ); |
| 244 | |
| 245 | return $output; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * @param Title $nt |
| 250 | * @param Language $lang |
| 251 | * @param LinkRenderer $linkRenderer |
| 252 | * @return string HTML |
| 253 | */ |
| 254 | protected function getCaptionHtml( Title $nt, Language $lang, LinkRenderer $linkRenderer ) { |
| 255 | // Preloaded into LinkCache in toHTML |
| 256 | return $linkRenderer->makeKnownLink( |
| 257 | $nt, |
| 258 | is_int( $this->getCaptionLength() ) ? |
| 259 | $lang->truncateForVisual( $nt->getText(), $this->getCaptionLength() ) : |
| 260 | $nt->getText(), |
| 261 | [ |
| 262 | 'class' => 'galleryfilename' . |
| 263 | ( $this->getCaptionLength() === true ? ' galleryfilename-truncate' : '' ) |
| 264 | ] |
| 265 | ) . "\n"; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Add the wrapper html around the thumb's caption |
| 270 | * |
| 271 | * @param string $galleryText The caption |
| 272 | * @param MediaTransformOutput|false $thumb The thumb this caption is for |
| 273 | * or false for bad image. |
| 274 | * @return string |
| 275 | */ |
| 276 | protected function wrapGalleryText( $galleryText, $thumb ) { |
| 277 | return "\n\t\t\t" . Html::rawElement( 'div', [ 'class' => "gallerytext" ], $galleryText ); |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * How much padding the thumb has between the image and the inner div |
| 282 | * that contains the border. This is for both vertical and horizontal |
| 283 | * padding. (However, it is cut in half in the vertical direction). |
| 284 | * @return int |
| 285 | */ |
| 286 | protected function getThumbPadding() { |
| 287 | return 30; |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * @note GB stands for gallerybox (as in the <li class="gallerybox"> element) |
| 292 | * |
| 293 | * @return int |
| 294 | */ |
| 295 | protected function getGBPadding() { |
| 296 | return 5; |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Get how much extra space the borders around the image takes up. |
| 301 | * |
| 302 | * For this mode, it is 2px borders on each side + 2px implied padding on |
| 303 | * each side from the stylesheet, giving us 2*2+2*2 = 8. |
| 304 | * @return int |
| 305 | */ |
| 306 | protected function getGBBorders() { |
| 307 | return 8; |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Length (in characters) to truncate filename to in caption when using "showfilename" (if int). |
| 312 | * A value of 'true' will truncate the filename to one line using CSS, while |
| 313 | * 'false' will disable truncating. |
| 314 | * |
| 315 | * @return int|bool |
| 316 | */ |
| 317 | protected function getCaptionLength() { |
| 318 | return $this->mCaptionLength; |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Get total padding. |
| 323 | * |
| 324 | * @return int Number of pixels of whitespace surrounding the thumbnail. |
| 325 | */ |
| 326 | protected function getAllPadding() { |
| 327 | return $this->getThumbPadding() + $this->getGBPadding() + $this->getGBBorders(); |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Get vertical padding for a thumbnail |
| 332 | * |
| 333 | * Generally this is the total height minus how high the thumb is. |
| 334 | * |
| 335 | * @param int $boxHeight How high we want the box to be. |
| 336 | * @param int $thumbHeight How high the thumbnail is. |
| 337 | * @return float Vertical padding to add on each side. |
| 338 | */ |
| 339 | protected function getVPad( $boxHeight, $thumbHeight ) { |
| 340 | return ( $this->getThumbPadding() + $boxHeight - $thumbHeight ) / 2; |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * Get the transform parameters for a thumbnail. |
| 345 | * |
| 346 | * @param File|false $img The file in question. May be false for invalid image |
| 347 | * @return array |
| 348 | */ |
| 349 | protected function getThumbParams( $img ) { |
| 350 | return [ |
| 351 | 'width' => $this->mWidths, |
| 352 | 'height' => $this->mHeights |
| 353 | ]; |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Get the width of the inner div that contains the thumbnail in |
| 358 | * question. This is the div with the class of "thumb". |
| 359 | * |
| 360 | * @param int $thumbWidth The width of the thumbnail. |
| 361 | * @return float Width of inner thumb div. |
| 362 | */ |
| 363 | protected function getThumbDivWidth( $thumbWidth ) { |
| 364 | return $this->mWidths + $this->getThumbPadding(); |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Computed width of gallerybox <li>. |
| 369 | * |
| 370 | * Generally is the width of the image, plus padding on image |
| 371 | * plus padding on gallerybox. |
| 372 | * |
| 373 | * @note Important: parameter will be false if no thumb used. |
| 374 | * @param MediaTransformOutput|false $thumb |
| 375 | * @return float Width of gallerybox element |
| 376 | */ |
| 377 | protected function getGBWidth( $thumb ) { |
| 378 | return $this->mWidths + $this->getThumbPadding() + $this->getGBPadding(); |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * Allows overwriting the computed width of the gallerybox <li> with a string, |
| 383 | * like '100%'. |
| 384 | * |
| 385 | * Generally is the width of the image, plus padding on image |
| 386 | * plus padding on gallerybox. |
| 387 | * |
| 388 | * @note Important: parameter will be false if no thumb used. |
| 389 | * @param MediaTransformOutput|false $thumb |
| 390 | * @return string|false Ignored if false. |
| 391 | */ |
| 392 | protected function getGBWidthOverwrite( $thumb ) { |
| 393 | return false; |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Get a list of modules to include in the page. |
| 398 | * |
| 399 | * Primarily intended for subclasses. |
| 400 | * |
| 401 | * @return array Modules to include |
| 402 | */ |
| 403 | protected function getModules() { |
| 404 | return []; |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Adjust the image parameters for a thumbnail. |
| 409 | * |
| 410 | * Used by a subclass to insert extra high resolution images. |
| 411 | * @param MediaTransformOutput $thumb The thumbnail |
| 412 | * @param array &$imageParameters Array of options |
| 413 | */ |
| 414 | protected function adjustImageParameters( $thumb, &$imageParameters ) { |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | /** @deprecated class alias since 1.46 */ |
| 419 | class_alias( TraditionalImageGallery::class, 'TraditionalImageGallery' ); |