From: Kim Eik Date: Tue, 10 Apr 2012 10:30:17 +0000 (+0200) Subject: (bug 34852) new optional 'link' parameter to X-Git-Tag: 1.31.0-rc.0~23903^2 X-Git-Url: http://git.cyclocoop.org/%24image?a=commitdiff_plain;h=0a5278266790a6fb4a197aa1e0fbf415075b2297;p=lhc%2Fweb%2Fwiklou.git (bug 34852) new optional 'link' parameter to The patch adds an optional parameter |link= to the 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: File:20120106_001.jpg|link=Main_Page Link as absolute URI: File:20120106_001.jpg|my caption|alt=my alt text|link=http://bugzilla.wikimedia.org 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 --- diff --git a/includes/ImageGallery.php b/includes/ImageGallery.php index 5a8fb8e4f8..f947f09e1c 100644 --- a/includes/ImageGallery.php +++ b/includes/ImageGallery.php @@ -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 { . '
' . $thumbhtml . "\n\t\t\t" . '
' . "\n" - . $textlink . $text . $fileSize + . $textlink . $text . $fileSize . "\n\t\t\t
" - . "\n\t\t
"; + . "\n\t\t"; } $output .= "\n"; @@ -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 diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php index 89540316f5..20c8b9520f 100644 --- a/includes/parser/Parser.php +++ b/includes/parser/Parser.php @@ -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(); } diff --git a/tests/parser/parserTests.txt b/tests/parser/parserTests.txt index 7a67a8d3f4..be4f910d2e 100644 --- a/tests/parser/parserTests.txt +++ b/tests/parser/parserTests.txt @@ -9254,6 +9254,63 @@ Ignore pipe between table row attributes !! end +!!test +Gallery override link with WikiLink (bug 34852) +!! input + +File:foobar.jpg|caption|alt=galleryalt|link=InterWikiLink + +!! result + + +!! end + +!!test +Gallery override link with absolute external link (bug 34852) +!! input + +File:foobar.jpg|caption|alt=galleryalt|link=http://www.example.org + +!! result + + +!! end + +!!test +Gallery override link with malicious javascript (bug 34852) +!! input + +File:foobar.jpg|caption|alt=galleryalt|link=" onclick="alert('malicious javascript code!'); + +!! result + + +!! end + TODO: more images more tables