Per r90849, factor out most of the code that's duplicated between Parser::getExternal...
authorRoan Kattouw <catrope@users.mediawiki.org>
Sat, 20 Aug 2011 10:18:09 +0000 (10:18 +0000)
committerRoan Kattouw <catrope@users.mediawiki.org>
Sat, 20 Aug 2011 10:18:09 +0000 (10:18 +0000)
includes/GlobalFunctions.php
includes/Skin.php
includes/parser/Parser.php

index e51cea6..2b16d17 100644 (file)
@@ -645,6 +645,26 @@ function wfMakeUrlIndex( $url ) {
        return $index;
 }
 
+/**
+ * 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.
index 5be5986..e1b24fa 100644 (file)
@@ -1153,24 +1153,13 @@ abstract class Skin extends ContextSource {
 
                                        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;
index dc039fd..1c96fc4 100644 (file)
@@ -1648,23 +1648,12 @@ class Parser {
         */
        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();