r86859 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r86858‎ | r86859 | r86860 >
Date:13:51, 25 April 2011
Author:diebuche
Status:ok (Comments)
Tags:
Comment:
Add |alt= option for galleries ( Bug 18682 ). Recommit of r86749, with nested |alt= now fixed. Patch by Jan Paul Posma
Modified paths:
  • /trunk/phase3/includes/ImageGallery.php (modified) (history)
  • /trunk/phase3/includes/MagicWord.php (modified) (history)
  • /trunk/phase3/includes/parser/Parser.php (modified) (history)
  • /trunk/phase3/tests/parser/parserTests.txt (modified) (history)

Diff [purge]

Index: trunk/phase3/tests/parser/parserTests.txt
@@ -7312,6 +7312,7 @@
73137313 File:Nonexistant.jpg
73147314 image:foobar.jpg|some '''caption''' [[Main Page]]
73157315 image:foobar.jpg
 7316+image:foobar.jpg|Blabla|alt=This is a foo-bar.|blabla.
73167317 </gallery>
73177318 !! result
73187319 <ul class="gallery" style="max-width: 202px;_width: 202px;">
@@ -7340,11 +7341,45 @@
73417342 <div class="gallerytext">
73427343 </div>
73437344 </div></li>
 7345+ <li class="gallerybox" style="width: 95px"><div style="width: 95px">
 7346+ <div class="thumb" style="width: 90px;"><div style="margin:26px auto;"><a href="https://www.mediawiki.org/wiki/File:Foobar.jpg" class="image"><img alt="This is a foo-bar." src="http://example.com/images/3/3a/Foobar.jpg" width="60" height="7" /></a></div></div>
 7347+ <div class="gallerytext">
 7348+<p>Blabla|blabla.
 7349+</p>
 7350+ </div>
 7351+ </div></li>
73447352 </ul>
73457353
73467354 !! end
73477355
73487356 !! test
 7357+Gallery with wikitext inside caption
 7358+!! input
 7359+<gallery>
 7360+File:foobar.jpg|[[File:foobar.jpg|20px|desc|alt=inneralt]]|alt=galleryalt
 7361+File:foobar.jpg|{{Test|unamedParam|alt=param}}|alt=galleryalt
 7362+</gallery>
 7363+!! result
 7364+<ul class="gallery">
 7365+ <li class="gallerybox" style="width: 155px"><div style="width: 155px">
 7366+ <div class="thumb" style="width: 150px;"><div style="margin:66px auto;"><a href="https://www.mediawiki.org/wiki/File:Foobar.jpg" class="image"><img alt="galleryalt" src="http://example.com/images/3/3a/Foobar.jpg" width="120" height="14" /></a></div></div>
 7367+ <div class="gallerytext">
 7368+<p><a href="https://www.mediawiki.org/wiki/File:Foobar.jpg" class="image" title="desc"><img alt="inneralt" src="http://example.com/images/3/3a/Foobar.jpg" width="20" height="2" /></a>
 7369+</p>
 7370+ </div>
 7371+ </div></li>
 7372+ <li class="gallerybox" style="width: 155px"><div style="width: 155px">
 7373+ <div class="thumb" style="width: 150px;"><div style="margin:66px auto;"><a href="https://www.mediawiki.org/wiki/File:Foobar.jpg" class="image"><img alt="galleryalt" src="http://example.com/images/3/3a/Foobar.jpg" width="120" height="14" /></a></div></div>
 7374+ <div class="gallerytext">
 7375+<p>This is a test template
 7376+</p>
 7377+ </div>
 7378+ </div></li>
 7379+</ul>
 7380+
 7381+!! end
 7382+
 7383+!! test
73497384 gallery (with showfilename option)
73507385 !! input
73517386 <gallery showfilename>
@@ -7425,33 +7460,6 @@
74267461 !! end
74277462
74287463 !! test
7429 -Gallery with wikitext inside caption
7430 -!! input
7431 -<gallery>
7432 -File:Wiki.png|[[File:Wiki.png|20px|desc|alt=inneralt]]|alt=galleryalt
7433 -File:Wiki.png|{{tl|test|alt=param}}|alt=galleryalt
7434 -</gallery>
7435 -!! result
7436 -<ul class="gallery">
7437 - <li class="gallerybox" style="width: 155px"><div style="width: 155px">
7438 - <div style="height: 150px;">Wiki.png</div>
7439 - <div class="gallerytext">
7440 -<p><a href="https://www.mediawiki.org/index.php?title=Special:Upload&amp;wpDestFile=Wiki.png" class="new" title="File:Wiki.png">desc</a>|alt=galleryalt
7441 -</p>
7442 - </div>
7443 - </div></li>
7444 - <li class="gallerybox" style="width: 155px"><div style="width: 155px">
7445 - <div style="height: 150px;">Wiki.png</div>
7446 - <div class="gallerytext">
7447 -<p><a href="https://www.mediawiki.org/index.php?title=Template:Tl&amp;action=edit&amp;redlink=1" class="new" title="Template:Tl (page does not exist)">Template:Tl</a>|alt=galleryalt
7448 -</p>
7449 - </div>
7450 - </div></li>
7451 -</ul>
7452 -
7453 -!! end
7454 -
7455 -!! test
74567464 HTML Hex character encoding (spells the word "JavaScript")
74577465 !! input
74587466 &#x4A;&#x061;&#x0076;&#x00061;&#x000053;&#x0000063;&#114;&#x0000069;&#00000112;&#x0000000074;
Index: trunk/phase3/includes/parser/Parser.php
@@ -4733,21 +4733,38 @@
47344734 if ( strpos( $matches[0], '%' ) !== false ) {
47354735 $matches[1] = rawurldecode( $matches[1] );
47364736 }
4737 - $tp = Title::newFromText( $matches[1], NS_FILE );
4738 - $nt =& $tp;
4739 - if ( is_null( $nt ) ) {
 4737+ $title = Title::newFromText( $matches[1], NS_FILE );
 4738+ if ( is_null( $title ) ) {
47404739 # Bogus title. Ignore these so we don't bomb out later.
47414740 continue;
47424741 }
 4742+
 4743+ $label = '';
 4744+ $alt = '';
47434745 if ( isset( $matches[3] ) ) {
4744 - $label = $matches[3];
4745 - } else {
4746 - $label = '';
 4746+ // look for an |alt= definition while trying not to break existing
 4747+ // captions with multiple pipes (|) in it, until a more sensible grammar
 4748+ // is defined for images in galleries
 4749+
 4750+ $matches[3] = $this->recursiveTagParse( trim( $matches[3] ) );
 4751+ $altmatches = StringUtils::explode('|', $matches[3]);
 4752+ $magicWordAlt = MagicWord::get( 'img_alt' );
 4753+
 4754+ foreach ( $altmatches as $altmatch ) {
 4755+ $match = $magicWordAlt->matchVariableStartToEnd( $altmatch );
 4756+ if ( $match ) {
 4757+ $alt = $this->stripAltText( $match, false );
 4758+ }
 4759+ else {
 4760+ // concatenate all other pipes
 4761+ $label .= '|' . $altmatch;
 4762+ }
 4763+ }
 4764+ // remove the first pipe
 4765+ $label = substr( $label, 1 );
47474766 }
47484767
4749 - $html = $this->recursiveTagParse( trim( $label ) );
4750 -
4751 - $ig->add( $nt, $html );
 4768+ $ig->add( $title, $label, $alt );
47524769 }
47534770 return $ig->toHTML();
47544771 }
Index: trunk/phase3/includes/MagicWord.php
@@ -35,6 +35,7 @@
3636 var $mRegexStart = '';
3737 var $mBaseRegex = '';
3838 var $mVariableRegex = '';
 39+ var $mVariableStartToEndRegex = '';
3940 var $mModified = false;
4041 var $mFound = false;
4142
Index: trunk/phase3/includes/ImageGallery.php
@@ -136,14 +136,15 @@
137137 * Add an image to the gallery.
138138 *
139139 * @param $title Title object of the image that is added to the gallery
140 - * @param $html String: additional HTML text to be shown. The name and size of the image are always shown.
 140+ * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
 141+ * @param $alt String: Alt text for the image
141142 */
142 - function add( $title, $html='' ) {
 143+ function add( $title, $html = '', $alt = '' ) {
143144 if ( $title instanceof File ) {
144145 // Old calling convention
145146 $title = $title->getTitle();
146147 }
147 - $this->mImages[] = array( $title, $html );
 148+ $this->mImages[] = array( $title, $html, $alt );
148149 wfDebug( "ImageGallery::add " . $title->getText() . "\n" );
149150 }
150151
@@ -151,14 +152,15 @@
152153 * Add an image at the beginning of the gallery.
153154 *
154155 * @param $title Title object of the image that is added to the gallery
155 - * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
 156+ * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
 157+ * @param $alt String: Alt text for the image
156158 */
157 - function insert( $title, $html='' ) {
 159+ function insert( $title, $html='', $alt='' ) {
158160 if ( $title instanceof File ) {
159161 // Old calling convention
160162 $title = $title->getTitle();
161163 }
162 - array_unshift( $this->mImages, array( &$title, $html ) );
 164+ array_unshift( $this->mImages, array( &$title, $html, $alt ) );
163165 }
164166
165167
@@ -218,15 +220,16 @@
219221 if ( $this->mPerRow > 0 ) {
220222 $maxwidth = $this->mPerRow * ( $this->mWidths + self::THUMB_PADDING + self::GB_PADDING + self::GB_BORDERS );
221223 $oldStyle = isset( $this->mAttribs['style'] ) ? $this->mAttribs['style'] : "";
 224+ # _width is ignored by any sane browser. IE6 doesn't know max-width so it uses _width instead
222225 $this->mAttribs['style'] = "max-width: {$maxwidth}px;_width: {$maxwidth}px;" . $oldStyle;
223226 }
224227
225228 $attribs = Sanitizer::mergeAttributes(
226229 array( 'class' => 'gallery' ), $this->mAttribs );
227230
228 - $s = Xml::openElement( 'ul', $attribs );
 231+ $output = Xml::openElement( 'ul', $attribs );
229232 if ( $this->mCaption ) {
230 - $s .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
 233+ $output .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
231234 }
232235
233236 $params = array( 'width' => $this->mWidths, 'height' => $this->mHeights );
@@ -234,6 +237,7 @@
235238 foreach ( $this->mImages as $pair ) {
236239 $nt = $pair[0];
237240 $text = $pair[1]; # "text" means "caption" here
 241+ $alt = $pair[2];
238242
239243 $descQuery = false;
240244 if ( $nt->getNamespace() == NS_FILE ) {
@@ -272,18 +276,19 @@
273277 $thumbhtml = "\n\t\t\t".'<div style="height: '.(self::THUMB_PADDING + $this->mHeights).'px;">'
274278 . htmlspecialchars( $img->getLastError() ) . '</div>';
275279 } else {
276 - //We get layout problems with the margin, if the image is smaller
277 - //than the line-height, so we less margin in these cases.
 280+ # We get layout problems with the margin, if the image is smaller
 281+ # than the line-height (17), so we add less margin in these cases.
278282 $minThumbHeight = $thumb->height > 17 ? $thumb->height : 17;
279283 $vpad = floor(( self::THUMB_PADDING + $this->mHeights - $minThumbHeight ) /2);
280284
281285
282286 $imageParameters = array(
283287 'desc-link' => true,
284 - 'desc-query' => $descQuery
 288+ 'desc-query' => $descQuery,
 289+ 'alt' => $alt,
285290 );
286 - # In the absence of a caption, fall back on providing screen readers with the filename as alt text
287 - if ( $text == '' ) {
 291+ # In the absence of both alt text and caption, fall back on providing screen readers with the filename as alt text
 292+ if ( $alt == '' && $text == '' ) {
288293 $imageParameters['alt'] = $nt->getText();
289294 }
290295
@@ -308,14 +313,14 @@
309314
310315 if( $this->mShowBytes ) {
311316 if( $img ) {
312 - $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
 317+ $fileSize = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
313318 $wgLang->formatNum( $img->getSize() ) );
314319 } else {
315 - $nb = wfMsgHtml( 'filemissing' );
 320+ $fileSize = wfMsgHtml( 'filemissing' );
316321 }
317 - $nb = "$nb<br />\n";
 322+ $fileSize = "$fileSize<br />\n";
318323 } else {
319 - $nb = '';
 324+ $fileSize = '';
320325 }
321326
322327 $textlink = $this->mShowFilename ?
@@ -332,20 +337,20 @@
333338 # in version 4.8.6 generated crackpot html in its absence, see:
334339 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
335340
336 - # Weird double wrapping in div needed due to FF2 bug
 341+ # Weird double wrapping (the extra div inside the li) needed due to FF2 bug
337342 # Can be safely removed if FF2 falls completely out of existance
338 - $s .=
 343+ $output .=
339344 "\n\t\t" . '<li class="gallerybox" style="width: ' . ( $this->mWidths + self::THUMB_PADDING + self::GB_PADDING ) . 'px">'
340345 . '<div style="width: ' . ( $this->mWidths + self::THUMB_PADDING + self::GB_PADDING ) . 'px">'
341346 . $thumbhtml
342347 . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
343 - . $textlink . $text . $nb
 348+ . $textlink . $text . $fileSize
344349 . "\n\t\t\t</div>"
345350 . "\n\t\t</div></li>";
346351 }
347 - $s .= "\n</ul>";
 352+ $output .= "\n</ul>";
348353
349 - return $s;
 354+ return $output;
350355 }
351356
352357 /**

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r85109Moved constant initialisation to class definition instead of constructorialex09:35, 1 April 2011
r86749Add ability to use add alt texts for images in galleries ( Bug 18682 ). Patch...diebuche07:27, 23 April 2011

Comments

#Comment by Brion VIBBER (talk | contribs)   21:25, 14 June 2011

So far so good! Marking ok in my check.

Status & tagging log