(bug 24437) Add nofollow to image link
authorcsteipp <csteipp@wikimedia.org>
Mon, 6 Aug 2012 17:04:07 +0000 (10:04 -0700)
committercsteipp <csteipp@wikimedia.org>
Tue, 7 Aug 2012 16:14:28 +0000 (09:14 -0700)
Add Parser's extLinkAttribs attributes when an image has a link
parameter, when generating the html. Currently, these may include
rel="nofollow" and the target attribute.

This will correctly add rel="nofollow", respecting $wgNoFollowLinks,
$wgNoFollowDomainExceptions, and $wgNoFollowNsExceptions settings.

Updated parser tests for expected results, added new tests for
$wgNoFollowLinks and $wgNoFollowDomainExceptions exceptions.

Change-Id: Ib4677760ec78a3f0c4ba781d893e0484cc8db3ed

includes/Linker.php
includes/media/MediaTransformOutput.php
tests/parser/parserTests.txt

index db144f7..ef77e63 100644 (file)
@@ -659,7 +659,7 @@ class Linker {
                                'title' => $fp['title'],
                                'valign' => isset( $fp['valign'] ) ? $fp['valign'] : false ,
                                'img-class' => isset( $fp['border'] ) ? 'thumbborder' : false );
-                       $params = self::getImageLinkMTOParams( $fp, $query ) + $params;
+                       $params = self::getImageLinkMTOParams( $fp, $query, $parser ) + $params;
 
                        $s = $thumb->toHtml( $params );
                }
@@ -687,13 +687,20 @@ class Linker {
         * @param $query string An optional query string to add to description page links
         * @return array
         */
-       private static function getImageLinkMTOParams( $frameParams, $query = '' ) {
+       private static function getImageLinkMTOParams( $frameParams, $query = '', $parser = null ) {
                $mtoParams = array();
                if ( isset( $frameParams['link-url'] ) && $frameParams['link-url'] !== '' ) {
                        $mtoParams['custom-url-link'] = $frameParams['link-url'];
                        if ( isset( $frameParams['link-target'] ) ) {
                                $mtoParams['custom-target-link'] = $frameParams['link-target'];
                        }
+                       if ( $parser ) {
+                               $extLinkAttrs = $parser->getExternalLinkAttribs( $frameParams['link-url'] );
+                               foreach ( $extLinkAttrs as $name => $val ) {
+                                       // Currently could include 'rel' and 'target'
+                                       $mtoParams['parser-extlink-'.$name] = $val;
+                               }
+                       }
                } elseif ( isset( $frameParams['link-title'] ) && $frameParams['link-title'] !== '' ) {
                        $mtoParams['custom-title-link'] = self::normaliseSpecialPage( $frameParams['link-title'] );
                } elseif ( !empty( $frameParams['no-link'] ) ) {
index d0a7339..00d12bb 100644 (file)
@@ -246,6 +246,9 @@ class ThumbnailImage extends MediaTransformOutput {
         *     custom-url-link    Custom URL to link to
         *     custom-title-link  Custom Title object to link to
         *     custom target-link Value of the target attribute, for custom-target-link
+        *     parser-extlink-*   Attributes added by parser for external links:
+        *          parser-extlink-rel: add rel="nofollow"
+        *          parser-extlink-target: link target, but overridden by custom-target-link
         *
         * For images, desc-link and file-link are implemented as a click-through. For
         * sounds and videos, they may be displayed in other ways.
@@ -268,6 +271,11 @@ class ThumbnailImage extends MediaTransformOutput {
                        }
                        if ( !empty( $options['custom-target-link'] ) ) {
                                $linkAttribs['target'] = $options['custom-target-link'];
+                       } elseif ( !empty( $options['parser-extlink-target'] ) ) {
+                               $linkAttribs['target'] = $options['parser-extlink-target'];
+                       }
+                       if ( !empty( $options['parser-extlink-rel'] ) ) {
+                               $linkAttribs['rel'] = $options['parser-extlink-rel'];
                        }
                } elseif ( !empty( $options['custom-title-link'] ) ) {
                        $title = $options['custom-title-link'];
index 48e1d7c..2bdb1ae 100644 (file)
@@ -4680,7 +4680,7 @@ Image with link parameter, URL target
 !! input
 [[Image:foobar.jpg|link=http://example.com/]]
 !! result
-<p><a href="http://example.com/"><img alt="Foobar.jpg" src="http://example.com/images/3/3a/Foobar.jpg" width="1941" height="220" /></a>
+<p><a href="http://example.com/" rel="nofollow"><img alt="Foobar.jpg" src="http://example.com/images/3/3a/Foobar.jpg" width="1941" height="220" /></a>
 </p>
 !! end
 
@@ -4691,7 +4691,29 @@ Image with link parameter, wgExternalLinkTarget
 !! config
 wgExternalLinkTarget='foobar'
 !! result
-<p><a href="http://example.com/" target="foobar"><img alt="Foobar.jpg" src="http://example.com/images/3/3a/Foobar.jpg" width="1941" height="220" /></a>
+<p><a href="http://example.com/" target="foobar" rel="nofollow"><img alt="Foobar.jpg" src="http://example.com/images/3/3a/Foobar.jpg" width="1941" height="220" /></a>
+</p>
+!! end
+
+!! test
+Image with link parameter, wgNoFollowLinks set to false
+!! input
+[[Image:foobar.jpg|link=http://example.com/]]
+!! config
+wgNoFollowLinks=false
+!! result
+<p><a href="http://example.com/"><img alt="Foobar.jpg" src="http://example.com/images/3/3a/Foobar.jpg" width="1941" height="220" /></a>
+</p>
+!! end
+
+!! test
+Image with link parameter, wgNoFollowDomainExceptions
+!! input
+[[Image:foobar.jpg|link=http://example.com/]]
+!! config
+wgNoFollowDomainExceptions='example.com'
+!! result
+<p><a href="http://example.com/"><img alt="Foobar.jpg" src="http://example.com/images/3/3a/Foobar.jpg" width="1941" height="220" /></a>
 </p>
 !! end
 
@@ -4702,7 +4724,7 @@ Image with link parameter, wgExternalLinkTarget, unnamed parameter
 !! config
 wgExternalLinkTarget='foobar'
 !! result
-<p><a href="http://example.com/" title="Title" target="foobar"><img alt="Title" src="http://example.com/images/3/3a/Foobar.jpg" width="1941" height="220" /></a>
+<p><a href="http://example.com/" title="Title" target="foobar" rel="nofollow"><img alt="Title" src="http://example.com/images/3/3a/Foobar.jpg" width="1941" height="220" /></a>
 </p>
 !! end
 
@@ -4729,7 +4751,7 @@ Image with link parameter (URL target) and unnamed parameter
 !! input
 [[Image:foobar.jpg|link=http://example.com/|Title]]
 !! result
-<p><a href="http://example.com/" title="Title"><img alt="Title" src="http://example.com/images/3/3a/Foobar.jpg" width="1941" height="220" /></a>
+<p><a href="http://example.com/" title="Title" rel="nofollow"><img alt="Title" src="http://example.com/images/3/3a/Foobar.jpg" width="1941" height="220" /></a>
 </p>
 !! end