Add list=allfileusages
[mediawiki.git] / includes / gallery / TraditionalImageGallery.php
blob1f60fa69b5062b843aecaa8ff64b9ab037ab341e
1 <?php
2 /**
3 * Image gallery.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
23 class TraditionalImageGallery extends ImageGalleryBase {
26 /**
27 * Return a HTML representation of the image gallery
29 * For each image in the gallery, display
30 * - a thumbnail
31 * - the image name
32 * - the additional text provided when adding the image
33 * - the size of the image
35 * @return string
37 function toHTML() {
38 if ( $this->mPerRow > 0 ) {
39 $maxwidth = $this->mPerRow * ( $this->mWidths + $this->getAllPadding() );
40 $oldStyle = isset( $this->mAttribs['style'] ) ? $this->mAttribs['style'] : '';
41 # _width is ignored by any sane browser. IE6 doesn't know max-width so it uses _width instead
42 $this->mAttribs['style'] = "max-width: {$maxwidth}px;_width: {$maxwidth}px;" . $oldStyle;
45 $attribs = Sanitizer::mergeAttributes(
46 array( 'class' => 'gallery mw-gallery-' . $this->mMode ), $this->mAttribs );
48 $modules = $this->getModules();
50 if ( $this->mParser ) {
51 $this->mParser->getOutput()->addModules( $modules );
52 } else {
53 $this->getOutput()->addModules( $modules );
55 $output = Xml::openElement( 'ul', $attribs );
56 if ( $this->mCaption ) {
57 $output .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
60 $lang = $this->getRenderLang();
61 # Output each image...
62 foreach ( $this->mImages as $pair ) {
63 $nt = $pair[0];
64 $text = $pair[1]; # "text" means "caption" here
65 $alt = $pair[2];
66 $link = $pair[3];
68 $descQuery = false;
69 if ( $nt->getNamespace() === NS_FILE ) {
70 # Get the file...
71 if ( $this->mParser instanceof Parser ) {
72 # Give extensions a chance to select the file revision for us
73 $options = array();
74 wfRunHooks( 'BeforeParserFetchFileAndTitle',
75 array( $this->mParser, $nt, &$options, &$descQuery ) );
76 # Fetch and register the file (file title may be different via hooks)
77 list( $img, $nt ) = $this->mParser->fetchFileAndTitle( $nt, $options );
78 } else {
79 $img = wfFindFile( $nt );
81 } else {
82 $img = false;
85 $params = $this->getThumbParams( $img );
86 // $pair[4] is per image handler options
87 $transformOptions = $params + $pair[4];
89 $thumb = false;
91 if ( !$img ) {
92 # We're dealing with a non-image, spit out the name and be done with it.
93 $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: ' . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">'
94 . htmlspecialchars( $nt->getText() ) . '</div>';
96 if ( $this->mParser instanceof Parser ) {
97 $this->mParser->addTrackingCategory( 'broken-file-category' );
99 } elseif ( $this->mHideBadImages && wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) ) {
100 # The image is blacklisted, just show it as a text link.
101 $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: ' . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">' .
102 Linker::link(
103 $nt,
104 htmlspecialchars( $nt->getText() ),
105 array(),
106 array(),
107 array( 'known', 'noclasses' )
109 '</div>';
110 } elseif ( !( $thumb = $img->transform( $transformOptions ) ) ) {
111 # Error generating thumbnail.
112 $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: ' . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">'
113 . htmlspecialchars( $img->getLastError() ) . '</div>';
114 } else {
115 $vpad = $this->getVPad( $this->mHeights, $thumb->getHeight() );
117 $imageParameters = array(
118 'desc-link' => true,
119 'desc-query' => $descQuery,
120 'alt' => $alt,
121 'custom-url-link' => $link
123 # In the absence of both alt text and caption, fall back on providing screen readers with the filename as alt text
124 if ( $alt == '' && $text == '' ) {
125 $imageParameters['alt'] = $nt->getText();
128 $this->adjustImageParameters( $thumb, $imageParameters );
130 # Set both fixed width and min-height.
131 $thumbhtml = "\n\t\t\t" .
132 '<div class="thumb" style="width: ' . $this->getThumbDivWidth( $thumb->getWidth() ) . 'px;">'
133 # Auto-margin centering for block-level elements. Needed now that we have video
134 # handlers since they may emit block-level elements as opposed to simple <img> tags.
135 # ref http://css-discuss.incutio.com/?page=CenteringBlockElement
136 . '<div style="margin:' . $vpad . 'px auto;">'
137 . $thumb->toHtml( $imageParameters ) . '</div></div>';
139 // Call parser transform hook
140 if ( $this->mParser && $img->getHandler() ) {
141 $img->getHandler()->parserTransformHook( $this->mParser, $img );
145 //TODO
146 // $linkTarget = Title::newFromText( $wgContLang->getNsText( MWNamespace::getUser() ) . ":{$ut}" );
147 // $ul = Linker::link( $linkTarget, $ut );
149 if ( $this->mShowBytes ) {
150 if ( $img ) {
151 $fileSize = htmlspecialchars( $lang->formatSize( $img->getSize() ) );
152 } else {
153 $fileSize = $this->msg( 'filemissing' )->escaped();
155 $fileSize = "$fileSize<br />\n";
156 } else {
157 $fileSize = '';
160 $textlink = $this->mShowFilename ?
161 Linker::link(
162 $nt,
163 htmlspecialchars( $lang->truncate( $nt->getText(), $this->mCaptionLength ) ),
164 array(),
165 array(),
166 array( 'known', 'noclasses' )
167 ) . "<br />\n" :
171 $galleryText = $textlink . $text . $fileSize;
172 $galleryText = $this->wrapGalleryText( $galleryText, $thumb );
174 # Weird double wrapping (the extra div inside the li) needed due to FF2 bug
175 # Can be safely removed if FF2 falls completely out of existence
176 $output .=
177 "\n\t\t" . '<li class="gallerybox" style="width: ' . $this->getGBWidth( $thumb ) . 'px">'
178 . '<div style="width: ' . $this->getGBWidth( $thumb ) . 'px">'
179 . $thumbhtml
180 . $galleryText
181 . "\n\t\t</div></li>";
183 $output .= "\n</ul>";
185 return $output;
190 * Add the wrapper html around the thumb's caption
192 * @param String $galleryText The caption
193 * @param MediaTransformOutput|boolean $thumb The thumb this caption is for or false for bad image.
195 protected function wrapGalleryText( $galleryText, $thumb ) {
196 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
197 # in version 4.8.6 generated crackpot html in its absence, see:
198 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
200 return "\n\t\t\t" . '<div class="gallerytext">' . "\n"
201 . $galleryText
202 . "\n\t\t\t</div>";
206 * How much padding such the thumb have between image and inner div that
207 * that contains the border. This is both for verical and horizontal
208 * padding. (However, it is cut in half in the vertical direction).
209 * @return int
211 protected function getThumbPadding() {
212 return 30;
217 * @note GB stands for gallerybox (as in the <li class="gallerybox"> element)
219 * @return int
221 protected function getGBPadding() {
222 return 5;
226 * Get how much extra space the borders around the image takes up.
228 * For this mode, it is 2px borders on each side + 2px implied padding on
229 * each side from the stylesheet, giving us 2*2+2*2 = 8.
230 * @return int
232 protected function getGBBorders() {
233 return 8;
237 * Get total padding.
239 * @return int How many pixels of whitespace surround the thumbnail.
241 protected function getAllPadding() {
242 return $this->getThumbPadding() + $this->getGBPadding() + $this->getGBBorders();
246 * Get vertical padding for a thumbnail
248 * Generally this is the total height minus how high the thumb is.
250 * @param int $boxHeight How high we want the box to be.
251 * @param int $thumbHeight How high the thumbnail is.
252 * @return int How many vertical padding to add on each side.
254 protected function getVPad( $boxHeight, $thumbHeight ) {
255 return ( $this->getThumbPadding() + $boxHeight - $thumbHeight ) / 2;
259 * Get the transform parameters for a thumbnail.
261 * @param File $img The file in question. May be false for invalid image
263 protected function getThumbParams( $img ) {
264 return array(
265 'width' => $this->mWidths,
266 'height' => $this->mHeights
271 * Get the width of the inner div that contains the thumbnail in
272 * question. This is the div with the class of "thumb".
274 * @param int $thumbWidth The width of the thumbnail.
275 * @return int Width of inner thumb div.
277 protected function getThumbDivWidth( $thumbWidth ) {
278 return $this->mWidths + $this->getThumbPadding();
282 * Width of gallerybox <li>.
284 * Generally is the width of the image, plus padding on image
285 * plus padding on gallerybox.
287 * @note Important: parameter will be false if no thumb used.
288 * @param Mixed $thumb MediaTransformObject object or false.
289 * @return int width of gallerybox element
291 protected function getGBWidth( $thumb ) {
292 return $this->mWidths + $this->getThumbPadding() + $this->getGBPadding();
296 * Get a list of modules to include in the page.
298 * Primarily intended for subclasses.
300 * @return Array modules to include
302 protected function getModules() {
303 return array();
307 * Adjust the image parameters for a thumbnail.
309 * Used by a subclass to insert extra high resolution images.
310 * @param MediaTransformOutput $thumb The thumbnail
311 * @param Array $imageParameters Array of options
313 protected function adjustImageParameters( $thumb, &$imageParameters ) { }
317 * Backwards compatibility. This always uses traditional mode
318 * if called the old way, for extensions that may expect traditional
319 * mode.
321 * @deprecated 1.22 Use ImageGalleryBase::factory instead.
323 class ImageGallery extends TraditionalImageGallery {
324 function __construct( $mode = 'traditional' ) {
325 wfDeprecated( __METHOD__, '1.22' );
326 parent::__construct( $mode );