http://www.mediawiki.org/wiki/Special:Code/MediaWiki/95072

Revision: 95072
Author:   catrope
Date:     2011-08-20 10:18:09 +0000 (Sat, 20 Aug 2011)
Log Message:
-----------
Per r90849, factor out most of the code that's duplicated between 
Parser::getExternalLinkAttribs() and Skin::addToSidebarPlain() into 
wfMatchesDomainList(). Change a loose comparison to a strict one, and add a 
FIXME comment about how whitelisting nl.wikipedia.org also whitelists 
nds-nl.wikipedia.org due to the function's simplistic substring approach.

Modified Paths:
--------------
    trunk/phase3/includes/GlobalFunctions.php
    trunk/phase3/includes/Skin.php
    trunk/phase3/includes/parser/Parser.php

Modified: trunk/phase3/includes/GlobalFunctions.php
===================================================================
--- trunk/phase3/includes/GlobalFunctions.php   2011-08-20 09:59:00 UTC (rev 
95071)
+++ trunk/phase3/includes/GlobalFunctions.php   2011-08-20 10:18:09 UTC (rev 
95072)
@@ -646,6 +646,26 @@
 }
 
 /**
+ * Check whether a given URL has a domain that occurs in a given set of domains
+ * @param $url string URL
+ * @param $domains array Array of domains (strings)
+ * @return bool True if the host part of $url ends in one of the strings in 
$domains
+ */
+function wfMatchesDomainList( $url, $domains ) {
+       $bits = wfParseUrl( $url );
+       if ( is_array( $bits ) && isset( $bits['host'] ) ) {
+               foreach ( (array)$domains as $domain ) {
+                       // FIXME: This gives false positives. 
http://nds-nl.wikipedia.org will match nl.wikipedia.org
+                       // We should use something that interprets dots instead
+                       if ( substr( $bits['host'], -strlen( $domain ) ) === 
$domain ) {
+                               return true;
+                       }
+               }
+       }
+       return false;
+}
+
+/**
  * Sends a line to the debug log if enabled or, optionally, to a comment in 
output.
  * In normal operation this is a NOP.
  *

Modified: trunk/phase3/includes/Skin.php
===================================================================
--- trunk/phase3/includes/Skin.php      2011-08-20 09:59:00 UTC (rev 95071)
+++ trunk/phase3/includes/Skin.php      2011-08-20 10:18:09 UTC (rev 95072)
@@ -1153,24 +1153,13 @@
 
                                        if ( preg_match( '/^(?:' . 
wfUrlProtocols() . ')/', $link ) ) {
                                                $href = $link;
-                                               
//Parser::getExternalLinkAttribs won't work here because of the Namespace things
-                                               global $wgNoFollowLinks;
-                                               if ( $wgNoFollowLinks ) {
+                                               
+                                               // 
Parser::getExternalLinkAttribs won't work here because of the Namespace things
+                                               global $wgNoFollowLinks, 
$wgNoFollowDomainExceptions;
+                                               if ( $wgNoFollowLinks && 
!wfMatchesDomainList( $url, $wgNoFollowDomainExceptions ) ) {
                                                        $extraAttribs['rel'] = 
'nofollow';
-
-                                                       global 
$wgNoFollowDomainExceptions;
-                                                       if ( 
$wgNoFollowDomainExceptions ) {
-                                                               $bits = 
wfParseUrl( $url );
-                                                               if ( is_array( 
$bits ) && isset( $bits['host'] ) ) {
-                                                                       foreach 
( $wgNoFollowDomainExceptions as $domain ) {
-                                                                               
if ( substr( $bits['host'], -strlen( $domain ) ) == $domain ) {
-                                                                               
        unset( $extraAttribs['rel'] );
-                                                                               
        break;
-                                                                               
}
-                                                                       }
-                                                               }
-                                                       }
                                                }
+                                               
                                                global $wgExternalLinkTarget;
                                                if ( $wgExternalLinkTarget) {
                                                        $extraAttribs['target'] 
= $wgExternalLinkTarget;

Modified: trunk/phase3/includes/parser/Parser.php
===================================================================
--- trunk/phase3/includes/parser/Parser.php     2011-08-20 09:59:00 UTC (rev 
95071)
+++ trunk/phase3/includes/parser/Parser.php     2011-08-20 10:18:09 UTC (rev 
95072)
@@ -1648,23 +1648,12 @@
         */
        function getExternalLinkAttribs( $url = false ) {
                $attribs = array();
-               global $wgNoFollowLinks, $wgNoFollowNsExceptions;
+               global $wgNoFollowLinks, $wgNoFollowNsExceptions, 
$wgNoFollowDomainExceptions;
                $ns = $this->mTitle->getNamespace();
-               if ( $wgNoFollowLinks && !in_array( $ns, 
$wgNoFollowNsExceptions ) ) {
+               if ( $wgNoFollowLinks && !in_array( $ns, 
$wgNoFollowNsExceptions ) &&
+                               !wfMatchesDomainList( $url, 
$wgNoFollowDomainExceptions ) )
+               {
                        $attribs['rel'] = 'nofollow';
-
-                       global $wgNoFollowDomainExceptions;
-                       if ( $wgNoFollowDomainExceptions ) {
-                               $bits = wfParseUrl( $url );
-                               if ( is_array( $bits ) && isset( $bits['host'] 
) ) {
-                                       foreach ( $wgNoFollowDomainExceptions 
as $domain ) {
-                                               if ( substr( $bits['host'], 
-strlen( $domain ) ) == $domain ) {
-                                                       unset( $attribs['rel'] 
);
-                                                       break;
-                                               }
-                                       }
-                               }
-                       }
                }
                if ( $this->mOptions->getExternalLinkTarget() ) {
                        $attribs['target'] = 
$this->mOptions->getExternalLinkTarget();


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to