(bug 34852) new optional 'link' parameter to <gallery>
authorKim Eik <kim@heldig.org>
Tue, 10 Apr 2012 10:30:17 +0000 (12:30 +0200)
committerKim Eik <kim@heldig.org>
Mon, 16 Apr 2012 08:09:31 +0000 (10:09 +0200)
The patch adds an optional parameter |link= to the <gallery>
tag. This will allow for images to link to other pages and
externals urls instead of being hardlinked to the image file
that is displayed in the gallery.

Here are a couple of examples.

Link as WikiLink:
<gallery>
File:20120106_001.jpg|link=Main_Page
</gallery>

Link as absolute URI:
<gallery>
File:20120106_001.jpg|my caption|alt=my alt
text|link=http://bugzilla.wikimedia.org
</gallery>

this would cause the link on the thumbnails rendered by the gallery tag to link
to a custom page/url instead of the actual media/image.

a link should be an internal wiki link or an absolute uri as shown in the examples.

Change-Id: I21b276ad5c7a8df13b3a716957d23fd53c37d29e

includes/ImageGallery.php
includes/parser/Parser.php
tests/parser/parserTests.txt

index 5a8fb8e..f947f09 100644 (file)
@@ -141,23 +141,24 @@ class ImageGallery {
         * @param $title Title object of the image that is added to the gallery
         * @param $html  String: Additional HTML text to be shown. The name and size of the image are always shown.
         * @param $alt   String: Alt text for the image
+        * @param $link  String: Override image link (optional)
         */
-       function add( $title, $html = '', $alt = '' ) {
+       function add( $title, $html = '', $alt = '', $link = '') {
                if ( $title instanceof File ) {
                        // Old calling convention
                        $title = $title->getTitle();
                }
-               $this->mImages[] = array( $title, $html, $alt );
+               $this->mImages[] = array( $title, $html, $alt, $link );
                wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" );
        }
 
        /**
-       * Add an image at the beginning of the gallery.
-       *
-       * @param $title Title object of the image that is added to the gallery
-       * @param $html  String: Additional HTML text to be shown. The name and size of the image are always shown.
-       * @param $alt   String: Alt text for the image
-       */
+        * Add an image at the beginning of the gallery.
+        *
+        * @param $title Title object of the image that is added to the gallery
+        * @param $html  String: Additional HTML text to be shown. The name and size of the image are always shown.
+        * @param $alt   String: Alt text for the image
+        */
        function insert( $title, $html = '', $alt = '' ) {
                if ( $title instanceof File ) {
                        // Old calling convention
@@ -245,6 +246,7 @@ class ImageGallery {
                        $nt = $pair[0];
                        $text = $pair[1]; # "text" means "caption" here
                        $alt = $pair[2];
+                       $link = $pair[3];
 
                        $descQuery = false;
                        if ( $nt->getNamespace() == NS_FILE ) {
@@ -289,6 +291,7 @@ class ImageGallery {
                                        'desc-link' => true,
                                        'desc-query' => $descQuery,
                                        'alt' => $alt,
+                                       'custom-url-link' => $link
                                );
                                # In the absence of both alt text and caption, fall back on providing screen readers with the filename as alt text
                                if ( $alt == '' && $text == '' ) {
@@ -346,9 +349,9 @@ class ImageGallery {
                                        . '<div style="width: ' . ( $this->mWidths + self::THUMB_PADDING + self::GB_PADDING ) . 'px">'
                                        . $thumbhtml
                                        . "\n\t\t\t" . '<div class="gallerytext">' . "\n"
-                                               . $textlink . $text . $fileSize
+                                       . $textlink . $text . $fileSize
                                        . "\n\t\t\t</div>"
-                               . "\n\t\t</div></li>";
+                                       . "\n\t\t</div></li>";
                }
                $output .= "\n</ul>";
 
@@ -378,8 +381,8 @@ class ImageGallery {
         */
        public function getContextTitle() {
                return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
-                               ? $this->contextTitle
-                               : false;
+                       ? $this->contextTitle
+                       : false;
        }
 
 } //class
index 8954031..20c8b95 100644 (file)
@@ -4854,30 +4854,41 @@ class Parser {
 
                        $label = '';
                        $alt = '';
+                       $link = '';
                        if ( isset( $matches[3] ) ) {
                                // look for an |alt= definition while trying not to break existing
                                // captions with multiple pipes (|) in it, until a more sensible grammar
                                // is defined for images in galleries
 
                                $matches[3] = $this->recursiveTagParse( trim( $matches[3] ) );
-                               $altmatches = StringUtils::explode('|', $matches[3]);
+                               $parameterMatches = StringUtils::explode('|', $matches[3]);
                                $magicWordAlt = MagicWord::get( 'img_alt' );
+                               $magicWordLink = MagicWord::get( 'img_link' );
 
-                               foreach ( $altmatches as $altmatch ) {
-                                       $match = $magicWordAlt->matchVariableStartToEnd( $altmatch );
-                                       if ( $match ) {
+                               foreach ( $parameterMatches as $parameterMatch ) {
+                                       if ( $match = $magicWordAlt->matchVariableStartToEnd( $parameterMatch ) ) {
                                                $alt = $this->stripAltText( $match, false );
                                        }
+                                       elseif( $match = $magicWordLink->matchVariableStartToEnd( $parameterMatch ) ){
+                                               $link = strip_tags($this->replaceLinkHoldersText($match));
+                                               $chars = self::EXT_LINK_URL_CLASS;
+                                               $prots = $this->mUrlProtocols;
+                                               //check to see if link matches an absolute url, if not then it must be a wiki link.
+                                               if(!preg_match( "/^($prots)$chars+$/u", $link)){
+                                                       $localLinkTitle = Title::newFromText($link);
+                                                       $link = $localLinkTitle->getLocalURL();
+                                               }
+                                       }
                                        else {
                                                // concatenate all other pipes
-                                               $label .= '|' . $altmatch;
+                                               $label .= '|' . $parameterMatch;
                                        }
                                }
                                // remove the first pipe
                                $label = substr( $label, 1 );
                        }
 
-                       $ig->add( $title, $label, $alt );
+                       $ig->add( $title, $label, $alt ,$link);
                }
                return $ig->toHTML();
        }
index 7a67a8d..be4f910 100644 (file)
@@ -9254,6 +9254,63 @@ Ignore pipe between table row attributes
 
 !! end
 
+!!test
+Gallery override link with WikiLink (bug 34852)
+!! input
+<gallery>
+File:foobar.jpg|caption|alt=galleryalt|link=InterWikiLink
+</gallery>
+!! result
+<ul class="gallery">
+               <li class="gallerybox" style="width: 155px"><div style="width: 155px">
+                       <div class="thumb" style="width: 150px;"><div style="margin:68px auto;"><a href="/wiki/InterWikiLink"><img alt="galleryalt" src="http://example.com/images/3/3a/Foobar.jpg" width="120" height="14" /></a></div></div>
+                       <div class="gallerytext">
+<p>caption
+</p>
+                       </div>
+               </div></li>
+</ul>
+
+!! end
+
+!!test
+Gallery override link with absolute external link (bug 34852)
+!! input
+<gallery>
+File:foobar.jpg|caption|alt=galleryalt|link=http://www.example.org
+</gallery>
+!! result
+<ul class="gallery">
+               <li class="gallerybox" style="width: 155px"><div style="width: 155px">
+                       <div class="thumb" style="width: 150px;"><div style="margin:68px auto;"><a href="http://www.example.org"><img alt="galleryalt" src="http://example.com/images/3/3a/Foobar.jpg" width="120" height="14" /></a></div></div>
+                       <div class="gallerytext">
+<p>caption
+</p>
+                       </div>
+               </div></li>
+</ul>
+
+!! end
+
+!!test
+Gallery override link with malicious javascript (bug 34852)
+!! input
+<gallery>
+File:foobar.jpg|caption|alt=galleryalt|link=" onclick="alert('malicious javascript code!');
+</gallery>
+!! result
+<ul class="gallery">
+               <li class="gallerybox" style="width: 155px"><div style="width: 155px">
+                       <div class="thumb" style="width: 150px;"><div style="margin:68px auto;"><a href="/wiki/%22_onclick%3D%22alert(%27malicious_javascript_code!%27);"><img alt="galleryalt" src="http://example.com/images/3/3a/Foobar.jpg" width="120" height="14" /></a></div></div>
+                       <div class="gallerytext">
+<p>caption
+</p>
+                       </div>
+               </div></li>
+</ul>
+
+!! end
+
 TODO:
 more images
 more tables