MediaWiki  1.33.0
TraditionalImageGallery.php
Go to the documentation of this file.
1 <?php
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 
45  $attribs = Sanitizer::mergeAttributes(
46  [ 'class' => 'gallery mw-gallery-' . $this->mMode ], $this->mAttribs );
47 
48  $modules = $this->getModules();
49 
50  if ( $this->mParser ) {
51  $this->mParser->getOutput()->addModules( $modules );
52  $this->mParser->getOutput()->addModuleStyles( 'mediawiki.page.gallery.styles' );
53  } else {
54  $this->getOutput()->addModules( $modules );
55  $this->getOutput()->addModuleStyles( 'mediawiki.page.gallery.styles' );
56  }
58  if ( $this->mCaption ) {
59  $output .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
60  }
61 
62  if ( $this->mShowFilename ) {
63  // Preload LinkCache info for when generating links
64  // of the filename below
65  $lb = new LinkBatch();
66  foreach ( $this->mImages as $img ) {
67  $lb->addObj( $img[0] );
68  }
69  $lb->execute();
70  }
71 
72  $lang = $this->getRenderLang();
73  # Output each image...
74  foreach ( $this->mImages as $pair ) {
75  // "text" means "caption" here
77  list( $nt, $text, $alt, $link ) = $pair;
78 
79  $descQuery = false;
80  if ( $nt->getNamespace() === NS_FILE ) {
81  # Get the file...
82  if ( $this->mParser instanceof Parser ) {
83  # Give extensions a chance to select the file revision for us
84  $options = [];
85  Hooks::run( 'BeforeParserFetchFileAndTitle',
86  [ $this->mParser, $nt, &$options, &$descQuery ] );
87  # Fetch and register the file (file title may be different via hooks)
88  list( $img, $nt ) = $this->mParser->fetchFileAndTitle( $nt, $options );
89  } else {
90  $img = wfFindFile( $nt );
91  }
92  } else {
93  $img = false;
94  }
95 
96  $params = $this->getThumbParams( $img );
97  // $pair[4] is per image handler options
98  $transformOptions = $params + $pair[4];
99 
100  $thumb = false;
101 
102  if ( !$img ) {
103  # We're dealing with a non-image, spit out the name and be done with it.
104  $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: '
105  . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">'
106  . htmlspecialchars( $nt->getText() ) . '</div>';
107 
108  if ( $this->mParser instanceof Parser ) {
109  $this->mParser->addTrackingCategory( 'broken-file-category' );
110  }
111  } elseif ( $this->mHideBadImages
112  && wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() )
113  ) {
114  # The image is blacklisted, just show it as a text link.
115  $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: ' .
116  ( $this->getThumbPadding() + $this->mHeights ) . 'px;">' .
118  $nt,
119  htmlspecialchars( $nt->getText() )
120  ) .
121  '</div>';
122  } else {
123  $thumb = $img->transform( $transformOptions );
124  if ( !$thumb ) {
125  # Error generating thumbnail.
126  $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: '
127  . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">'
128  . htmlspecialchars( $img->getLastError() ) . '</div>';
129  } else {
131  $vpad = $this->getVPad( $this->mHeights, $thumb->getHeight() );
132 
133  $imageParameters = [
134  'desc-link' => true,
135  'desc-query' => $descQuery,
136  'alt' => $alt,
137  'custom-url-link' => $link
138  ];
139 
140  // In the absence of both alt text and caption, fall back on
141  // providing screen readers with the filename as alt text
142  if ( $alt == '' && $text == '' ) {
143  $imageParameters['alt'] = $nt->getText();
144  }
145 
146  $this->adjustImageParameters( $thumb, $imageParameters );
147 
148  Linker::processResponsiveImages( $img, $thumb, $transformOptions );
149 
150  # Set both fixed width and min-height.
151  $thumbhtml = "\n\t\t\t"
152  . '<div class="thumb" style="width: '
153  . $this->getThumbDivWidth( $thumb->getWidth() ) . 'px;">'
154  # Auto-margin centering for block-level elements. Needed
155  # now that we have video handlers since they may emit block-
156  # level elements as opposed to simple <img> tags. ref
157  # http://css-discuss.incutio.com/?page=CenteringBlockElement
158  . '<div style="margin:' . $vpad . 'px auto;">'
159  . $thumb->toHtml( $imageParameters ) . '</div></div>';
160 
161  // Call parser transform hook
163  $handler = $img->getHandler();
164  if ( $this->mParser && $handler ) {
165  $handler->parserTransformHook( $this->mParser, $img );
166  }
167  }
168  }
169 
170  // @todo Code is incomplete.
171  // $linkTarget = Title::newFromText( MediaWikiServices::getInstance()->
172  // getContentLanguage()->getNsText( MWNamespace::getUser() ) . ":{$ut}" );
173  // $ul = Linker::link( $linkTarget, $ut );
174 
175  $meta = [];
176  if ( $img ) {
177  if ( $this->mShowDimensions ) {
178  $meta[] = $img->getDimensionsString();
179  }
180  if ( $this->mShowBytes ) {
181  $meta[] = htmlspecialchars( $lang->formatSize( $img->getSize() ) );
182  }
183  } elseif ( $this->mShowDimensions || $this->mShowBytes ) {
184  $meta[] = $this->msg( 'filemissing' )->escaped();
185  }
186  $meta = $lang->semicolonList( $meta );
187  if ( $meta ) {
188  $meta .= "<br />\n";
189  }
190 
191  $textlink = $this->mShowFilename ?
192  $this->getCaptionHtml( $nt, $lang ) :
193  '';
194 
195  $galleryText = $textlink . $text . $meta;
196  $galleryText = $this->wrapGalleryText( $galleryText, $thumb );
197 
198  $gbWidth = $this->getGBWidth( $thumb ) . 'px';
199  if ( $this->getGBWidthOverwrite( $thumb ) ) {
200  $gbWidth = $this->getGBWidthOverwrite( $thumb );
201  }
202  # Weird double wrapping (the extra div inside the li) needed due to FF2 bug
203  # Can be safely removed if FF2 falls completely out of existence
204  $output .= "\n\t\t" . '<li class="gallerybox" style="width: '
205  . $gbWidth . '">'
206  . '<div style="width: ' . $gbWidth . '">'
207  . $thumbhtml
208  . $galleryText
209  . "\n\t\t</div></li>";
210  }
211  $output .= "\n</ul>";
212 
213  return $output;
214  }
215 
221  protected function getCaptionHtml( Title $nt, Language $lang ) {
222  // Preloaded into LinkCache in toHTML
223  return Linker::linkKnown(
224  $nt,
225  htmlspecialchars(
226  is_int( $this->getCaptionLength() ) ?
227  $lang->truncateForVisual( $nt->getText(), $this->getCaptionLength() ) :
228  $nt->getText()
229  ),
230  [
231  'class' => 'galleryfilename' .
232  ( $this->getCaptionLength() === true ? ' galleryfilename-truncate' : '' )
233  ]
234  ) . "\n";
235  }
236 
245  protected function wrapGalleryText( $galleryText, $thumb ) {
246  # ATTENTION: The newline after <div class="gallerytext"> is needed to
247  # accommodate htmltidy which in version 4.8.6 generated crackpot html in
248  # its absence, see: https://phabricator.wikimedia.org/T3765
249  # -Ævar
250 
251  return "\n\t\t\t" . '<div class="gallerytext">' . "\n"
252  . $galleryText
253  . "\n\t\t\t</div>";
254  }
255 
262  protected function getThumbPadding() {
263  return 30;
264  }
265 
271  protected function getGBPadding() {
272  return 5;
273  }
274 
282  protected function getGBBorders() {
283  return 8;
284  }
285 
293  protected function getCaptionLength() {
294  return $this->mCaptionLength;
295  }
296 
302  protected function getAllPadding() {
303  return $this->getThumbPadding() + $this->getGBPadding() + $this->getGBBorders();
304  }
305 
315  protected function getVPad( $boxHeight, $thumbHeight ) {
316  return ( $this->getThumbPadding() + $boxHeight - $thumbHeight ) / 2;
317  }
318 
325  protected function getThumbParams( $img ) {
326  return [
327  'width' => $this->mWidths,
328  'height' => $this->mHeights
329  ];
330  }
331 
339  protected function getThumbDivWidth( $thumbWidth ) {
340  return $this->mWidths + $this->getThumbPadding();
341  }
342 
353  protected function getGBWidth( $thumb ) {
354  return $this->mWidths + $this->getThumbPadding() + $this->getGBPadding();
355  }
356 
368  protected function getGBWidthOverwrite( $thumb ) {
369  return false;
370  }
371 
379  protected function getModules() {
380  return [];
381  }
382 
390  protected function adjustImageParameters( $thumb, &$imageParameters ) {
391  }
392 }
LinkBatch
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition: LinkBatch.php:34
TraditionalImageGallery\getThumbParams
getThumbParams( $img)
Get the transform parameters for a thumbnail.
Definition: TraditionalImageGallery.php:325
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:33
ImageGalleryBase
Image gallery.
Definition: ImageGalleryBase.php:32
ContextSource\msg
msg( $key)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: ContextSource.php:168
TraditionalImageGallery\toHTML
toHTML()
Return a HTML representation of the image gallery.
Definition: TraditionalImageGallery.php:35
Language\truncateForVisual
truncateForVisual( $string, $length, $ellipsis='...', $adjustLength=true)
Truncate a string to a specified number of characters, appending an optional string (e....
Definition: Language.php:3544
ImageGalleryBase\getContextTitle
getContextTitle()
Get the contextual title, if applicable.
Definition: ImageGalleryBase.php:377
block
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging a wrapping ErrorException block
Definition: hooks.txt:2162
NS_FILE
const NS_FILE
Definition: Defines.php:70
$params
$params
Definition: styleTest.css.php:44
ImageGalleryBase\$mCaptionLength
bool int $mCaptionLength
Length to truncate filename to in caption when using "showfilename".
Definition: ImageGalleryBase.php:70
TraditionalImageGallery\getCaptionLength
getCaptionLength()
Length (in characters) to truncate filename to in caption when using "showfilename" (if int).
Definition: TraditionalImageGallery.php:293
Linker\linkKnown
static linkKnown( $target, $html=null, $customAttribs=[], $query=[], $options=[ 'known'])
Identical to link(), except $options defaults to 'known'.
Definition: Linker.php:146
Linker\processResponsiveImages
static processResponsiveImages( $file, $thumb, $hp)
Process responsive images: add 1.5x and 2x subimages to the thumbnail, where applicable.
Definition: Linker.php:644
http
Apache License January http
Definition: APACHE-LICENSE-2.0.txt:3
Xml\openElement
static openElement( $element, $attribs=null)
This opens an XML element.
Definition: Xml.php:108
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
TraditionalImageGallery\getGBWidthOverwrite
getGBWidthOverwrite( $thumb)
Allows overwriting the computed width of the gallerybox with a string, like '100'.
Definition: TraditionalImageGallery.php:368
TraditionalImageGallery\getGBBorders
getGBBorders()
Get how much extra space the borders around the image takes up.
Definition: TraditionalImageGallery.php:282
TraditionalImageGallery\getVPad
getVPad( $boxHeight, $thumbHeight)
Get vertical padding for a thumbnail.
Definition: TraditionalImageGallery.php:315
TraditionalImageGallery\getCaptionHtml
getCaptionHtml(Title $nt, Language $lang)
Definition: TraditionalImageGallery.php:221
they
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that they
Definition: COPYING.txt:28
TraditionalImageGallery
Definition: TraditionalImageGallery.php:23
$handler
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check set to true or false to override the $wgMaxImageArea check result gives extension the possibility to transform it themselves $handler
Definition: hooks.txt:780
ContextSource\getOutput
getOutput()
Definition: ContextSource.php:112
$attribs
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing & $attribs
Definition: hooks.txt:1985
$modules
$modules
Definition: HTMLFormElement.php:12
TraditionalImageGallery\wrapGalleryText
wrapGalleryText( $galleryText, $thumb)
Add the wrapper html around the thumb's caption.
Definition: TraditionalImageGallery.php:245
$output
$output
Definition: SyntaxHighlight.php:334
TraditionalImageGallery\getModules
getModules()
Get a list of modules to include in the page.
Definition: TraditionalImageGallery.php:379
TraditionalImageGallery\getGBWidth
getGBWidth( $thumb)
Computed width of gallerybox .
Definition: TraditionalImageGallery.php:353
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
wfIsBadImage
wfIsBadImage( $name, $contextTitle=false, $blacklist=null)
Determine if an image exists on the 'bad image list'.
Definition: GlobalFunctions.php:3036
TraditionalImageGallery\adjustImageParameters
adjustImageParameters( $thumb, &$imageParameters)
Adjust the image parameters for a thumbnail.
Definition: TraditionalImageGallery.php:390
TraditionalImageGallery\getGBPadding
getGBPadding()
Definition: TraditionalImageGallery.php:271
TraditionalImageGallery\getAllPadding
getAllPadding()
Get total padding.
Definition: TraditionalImageGallery.php:302
wfFindFile
wfFindFile( $title, $options=[])
Find a file.
Definition: GlobalFunctions.php:2677
ImageGalleryBase\getRenderLang
getRenderLang()
Determines the correct language to be used for this image gallery.
Definition: ImageGalleryBase.php:387
Title
Represents a title within MediaWiki.
Definition: Title.php:40
$options
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition: hooks.txt:1985
TraditionalImageGallery\getThumbPadding
getThumbPadding()
How much padding the thumb has between the image and the inner div that contains the border.
Definition: TraditionalImageGallery.php:262
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
$link
usually copyright or history_copyright This message must be in HTML not wikitext & $link
Definition: hooks.txt:3053
that
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global then executing the whole list after the page is displayed We don t do anything smart like collating updates to the same table or such because the list is almost always going to have just one item on if that
Definition: deferred.txt:11
TraditionalImageGallery\getThumbDivWidth
getThumbDivWidth( $thumbWidth)
Get the width of the inner div that contains the thumbnail in question.
Definition: TraditionalImageGallery.php:339
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200
Title\getText
getText()
Get the text form (spaces not underscores) of the main part.
Definition: Title.php:952
Language
Internationalisation code.
Definition: Language.php:36