TraditionalImageGallery.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <?php
  2. use MediaWiki\MediaWikiServices;
  3. /**
  4. * Image gallery.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. * http://www.gnu.org/copyleft/gpl.html
  20. *
  21. * @file
  22. */
  23. class TraditionalImageGallery extends ImageGalleryBase {
  24. /**
  25. * Return a HTML representation of the image gallery
  26. *
  27. * For each image in the gallery, display
  28. * - a thumbnail
  29. * - the image name
  30. * - the additional text provided when adding the image
  31. * - the size of the image
  32. *
  33. * @return string
  34. */
  35. function toHTML() {
  36. if ( $this->mPerRow > 0 ) {
  37. $maxwidth = $this->mPerRow * ( $this->mWidths + $this->getAllPadding() );
  38. $oldStyle = $this->mAttribs['style'] ?? '';
  39. # _width is ignored by any sane browser. IE6 doesn't know max-width
  40. # so it uses _width instead
  41. $this->mAttribs['style'] = "max-width: {$maxwidth}px;_width: {$maxwidth}px;" .
  42. $oldStyle;
  43. }
  44. $attribs = Sanitizer::mergeAttributes(
  45. [ 'class' => 'gallery mw-gallery-' . $this->mMode ], $this->mAttribs );
  46. $modules = $this->getModules();
  47. if ( $this->mParser ) {
  48. $this->mParser->getOutput()->addModules( $modules );
  49. $this->mParser->getOutput()->addModuleStyles( 'mediawiki.page.gallery.styles' );
  50. } else {
  51. $this->getOutput()->addModules( $modules );
  52. $this->getOutput()->addModuleStyles( 'mediawiki.page.gallery.styles' );
  53. }
  54. $output = Xml::openElement( 'ul', $attribs );
  55. if ( $this->mCaption ) {
  56. $output .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
  57. }
  58. if ( $this->mShowFilename ) {
  59. // Preload LinkCache info for when generating links
  60. // of the filename below
  61. $lb = new LinkBatch();
  62. foreach ( $this->mImages as $img ) {
  63. $lb->addObj( $img[0] );
  64. }
  65. $lb->execute();
  66. }
  67. $lang = $this->getRenderLang();
  68. # Output each image...
  69. foreach ( $this->mImages as $pair ) {
  70. // "text" means "caption" here
  71. /** @var Title $nt */
  72. list( $nt, $text, $alt, $link ) = $pair;
  73. $descQuery = false;
  74. if ( $nt->getNamespace() === NS_FILE ) {
  75. # Get the file...
  76. if ( $this->mParser instanceof Parser ) {
  77. # Give extensions a chance to select the file revision for us
  78. $options = [];
  79. Hooks::run( 'BeforeParserFetchFileAndTitle',
  80. [ $this->mParser, $nt, &$options, &$descQuery ] );
  81. # Fetch and register the file (file title may be different via hooks)
  82. list( $img, $nt ) = $this->mParser->fetchFileAndTitle( $nt, $options );
  83. } else {
  84. $img = MediaWikiServices::getInstance()->getRepoGroup()->findFile( $nt );
  85. }
  86. } else {
  87. $img = false;
  88. }
  89. $params = $this->getThumbParams( $img );
  90. // $pair[4] is per image handler options
  91. $transformOptions = $params + $pair[4];
  92. $thumb = false;
  93. if ( !$img ) {
  94. # We're dealing with a non-image, spit out the name and be done with it.
  95. $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: '
  96. . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">'
  97. . htmlspecialchars( $nt->getText() ) . '</div>';
  98. if ( $this->mParser instanceof Parser ) {
  99. $this->mParser->addTrackingCategory( 'broken-file-category' );
  100. }
  101. } elseif ( $this->mHideBadImages && MediaWikiServices::getInstance()->getBadFileLookup()
  102. ->isBadFile( $nt->getDBkey(), $this->getContextTitle() )
  103. ) {
  104. # The image is blacklisted, just show it as a text link.
  105. $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: ' .
  106. ( $this->getThumbPadding() + $this->mHeights ) . 'px;">' .
  107. Linker::linkKnown(
  108. $nt,
  109. htmlspecialchars( $nt->getText() )
  110. ) .
  111. '</div>';
  112. } else {
  113. $thumb = $img->transform( $transformOptions );
  114. if ( !$thumb ) {
  115. # Error generating thumbnail.
  116. $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: '
  117. . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">'
  118. . htmlspecialchars( $img->getLastError() ) . '</div>';
  119. } else {
  120. /** @var MediaTransformOutput $thumb */
  121. $vpad = $this->getVPad( $this->mHeights, $thumb->getHeight() );
  122. $imageParameters = [
  123. 'desc-link' => true,
  124. 'desc-query' => $descQuery,
  125. 'alt' => $alt,
  126. 'custom-url-link' => $link
  127. ];
  128. // In the absence of both alt text and caption, fall back on
  129. // providing screen readers with the filename as alt text
  130. if ( $alt == '' && $text == '' ) {
  131. $imageParameters['alt'] = $nt->getText();
  132. }
  133. $this->adjustImageParameters( $thumb, $imageParameters );
  134. Linker::processResponsiveImages( $img, $thumb, $transformOptions );
  135. # Set both fixed width and min-height.
  136. $thumbhtml = "\n\t\t\t"
  137. . '<div class="thumb" style="width: '
  138. . $this->getThumbDivWidth( $thumb->getWidth() ) . 'px;">'
  139. # Auto-margin centering for block-level elements. Needed
  140. # now that we have video handlers since they may emit block-
  141. # level elements as opposed to simple <img> tags. ref
  142. # http://css-discuss.incutio.com/?page=CenteringBlockElement
  143. . '<div style="margin:' . $vpad . 'px auto;">'
  144. . $thumb->toHtml( $imageParameters ) . '</div></div>';
  145. // Call parser transform hook
  146. /** @var MediaHandler $handler */
  147. $handler = $img->getHandler();
  148. if ( $this->mParser && $handler ) {
  149. $handler->parserTransformHook( $this->mParser, $img );
  150. }
  151. }
  152. }
  153. // @todo Code is incomplete.
  154. // $linkTarget = Title::newFromText( MediaWikiServices::getInstance()->
  155. // getContentLanguage()->getNsText( MediaWikiServices::getInstance()->
  156. // getNamespaceInfo()->getUser() ) . ":{$ut}" );
  157. // $ul = Linker::link( $linkTarget, $ut );
  158. $meta = [];
  159. if ( $img ) {
  160. if ( $this->mShowDimensions ) {
  161. $meta[] = $img->getDimensionsString();
  162. }
  163. if ( $this->mShowBytes ) {
  164. $meta[] = htmlspecialchars( $lang->formatSize( $img->getSize() ) );
  165. }
  166. } elseif ( $this->mShowDimensions || $this->mShowBytes ) {
  167. $meta[] = $this->msg( 'filemissing' )->escaped();
  168. }
  169. $meta = $lang->semicolonList( $meta );
  170. if ( $meta ) {
  171. $meta .= "<br />\n";
  172. }
  173. $textlink = $this->mShowFilename ?
  174. $this->getCaptionHtml( $nt, $lang ) :
  175. '';
  176. $galleryText = $textlink . $text . $meta;
  177. $galleryText = $this->wrapGalleryText( $galleryText, $thumb );
  178. $gbWidth = $this->getGBWidth( $thumb ) . 'px';
  179. if ( $this->getGBWidthOverwrite( $thumb ) ) {
  180. $gbWidth = $this->getGBWidthOverwrite( $thumb );
  181. }
  182. # Weird double wrapping (the extra div inside the li) needed due to FF2 bug
  183. # Can be safely removed if FF2 falls completely out of existence
  184. $output .= "\n\t\t" . '<li class="gallerybox" style="width: '
  185. . $gbWidth . '">'
  186. . '<div style="width: ' . $gbWidth . '">'
  187. . $thumbhtml
  188. . $galleryText
  189. . "\n\t\t</div></li>";
  190. }
  191. $output .= "\n</ul>";
  192. return $output;
  193. }
  194. /**
  195. * @param Title $nt
  196. * @param Language $lang
  197. * @return string HTML
  198. */
  199. protected function getCaptionHtml( Title $nt, Language $lang ) {
  200. // Preloaded into LinkCache in toHTML
  201. return Linker::linkKnown(
  202. $nt,
  203. htmlspecialchars(
  204. is_int( $this->getCaptionLength() ) ?
  205. $lang->truncateForVisual( $nt->getText(), $this->getCaptionLength() ) :
  206. $nt->getText()
  207. ),
  208. [
  209. 'class' => 'galleryfilename' .
  210. ( $this->getCaptionLength() === true ? ' galleryfilename-truncate' : '' )
  211. ]
  212. ) . "\n";
  213. }
  214. /**
  215. * Add the wrapper html around the thumb's caption
  216. *
  217. * @param string $galleryText The caption
  218. * @param MediaTransformOutput|bool $thumb The thumb this caption is for
  219. * or false for bad image.
  220. * @return string
  221. */
  222. protected function wrapGalleryText( $galleryText, $thumb ) {
  223. # ATTENTION: The newline after <div class="gallerytext"> is needed to
  224. # accommodate htmltidy which in version 4.8.6 generated crackpot html in
  225. # its absence, see: https://phabricator.wikimedia.org/T3765
  226. # -Ævar
  227. return "\n\t\t\t" . '<div class="gallerytext">' . "\n"
  228. . $galleryText
  229. . "\n\t\t\t</div>";
  230. }
  231. /**
  232. * How much padding the thumb has between the image and the inner div
  233. * that contains the border. This is for both vertical and horizontal
  234. * padding. (However, it is cut in half in the vertical direction).
  235. * @return int
  236. */
  237. protected function getThumbPadding() {
  238. return 30;
  239. }
  240. /**
  241. * @note GB stands for gallerybox (as in the <li class="gallerybox"> element)
  242. *
  243. * @return int
  244. */
  245. protected function getGBPadding() {
  246. return 5;
  247. }
  248. /**
  249. * Get how much extra space the borders around the image takes up.
  250. *
  251. * For this mode, it is 2px borders on each side + 2px implied padding on
  252. * each side from the stylesheet, giving us 2*2+2*2 = 8.
  253. * @return int
  254. */
  255. protected function getGBBorders() {
  256. return 8;
  257. }
  258. /**
  259. * Length (in characters) to truncate filename to in caption when using "showfilename" (if int).
  260. * A value of 'true' will truncate the filename to one line using CSS, while
  261. * 'false' will disable truncating.
  262. *
  263. * @return int|bool
  264. */
  265. protected function getCaptionLength() {
  266. return $this->mCaptionLength;
  267. }
  268. /**
  269. * Get total padding.
  270. *
  271. * @return int Number of pixels of whitespace surrounding the thumbnail.
  272. */
  273. protected function getAllPadding() {
  274. return $this->getThumbPadding() + $this->getGBPadding() + $this->getGBBorders();
  275. }
  276. /**
  277. * Get vertical padding for a thumbnail
  278. *
  279. * Generally this is the total height minus how high the thumb is.
  280. *
  281. * @param int $boxHeight How high we want the box to be.
  282. * @param int $thumbHeight How high the thumbnail is.
  283. * @return int Vertical padding to add on each side.
  284. */
  285. protected function getVPad( $boxHeight, $thumbHeight ) {
  286. return ( $this->getThumbPadding() + $boxHeight - $thumbHeight ) / 2;
  287. }
  288. /**
  289. * Get the transform parameters for a thumbnail.
  290. *
  291. * @param File $img The file in question. May be false for invalid image
  292. * @return array
  293. */
  294. protected function getThumbParams( $img ) {
  295. return [
  296. 'width' => $this->mWidths,
  297. 'height' => $this->mHeights
  298. ];
  299. }
  300. /**
  301. * Get the width of the inner div that contains the thumbnail in
  302. * question. This is the div with the class of "thumb".
  303. *
  304. * @param int $thumbWidth The width of the thumbnail.
  305. * @return int Width of inner thumb div.
  306. */
  307. protected function getThumbDivWidth( $thumbWidth ) {
  308. return $this->mWidths + $this->getThumbPadding();
  309. }
  310. /**
  311. * Computed width of gallerybox <li>.
  312. *
  313. * Generally is the width of the image, plus padding on image
  314. * plus padding on gallerybox.
  315. *
  316. * @note Important: parameter will be false if no thumb used.
  317. * @param MediaTransformOutput|bool $thumb MediaTransformObject object or false.
  318. * @return int Width of gallerybox element
  319. */
  320. protected function getGBWidth( $thumb ) {
  321. return $this->mWidths + $this->getThumbPadding() + $this->getGBPadding();
  322. }
  323. /**
  324. * Allows overwriting the computed width of the gallerybox <li> with a string,
  325. * like '100%'.
  326. *
  327. * Generally is the width of the image, plus padding on image
  328. * plus padding on gallerybox.
  329. *
  330. * @note Important: parameter will be false if no thumb used.
  331. * @param MediaTransformOutput|bool $thumb MediaTransformObject object or false.
  332. * @return bool|string Ignored if false.
  333. */
  334. protected function getGBWidthOverwrite( $thumb ) {
  335. return false;
  336. }
  337. /**
  338. * Get a list of modules to include in the page.
  339. *
  340. * Primarily intended for subclasses.
  341. *
  342. * @return array Modules to include
  343. */
  344. protected function getModules() {
  345. return [];
  346. }
  347. /**
  348. * Adjust the image parameters for a thumbnail.
  349. *
  350. * Used by a subclass to insert extra high resolution images.
  351. * @param MediaTransformOutput $thumb The thumbnail
  352. * @param array &$imageParameters Array of options
  353. */
  354. protected function adjustImageParameters( $thumb, &$imageParameters ) {
  355. }
  356. }