From: Roan Kattouw Date: Sat, 20 Aug 2011 10:18:09 +0000 (+0000) Subject: Per r90849, factor out most of the code that's duplicated between Parser::getExternal... X-Git-Tag: 1.31.0-rc.0~28150 X-Git-Url: http://git.cyclocoop.org/url?a=commitdiff_plain;h=b4ae56dea1a7f3f46c4f872151b0d5f121785271;p=lhc%2Fweb%2Fwiklou.git 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. --- diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index e51cea6764..2b16d17f05 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -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. diff --git a/includes/Skin.php b/includes/Skin.php index 5be598623e..e1b24fae93 100644 --- a/includes/Skin.php +++ b/includes/Skin.php @@ -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; diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php index dc039fd40e..1c96fc4007 100644 --- a/includes/parser/Parser.php +++ b/includes/parser/Parser.php @@ -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();