r95072 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r95071‎ | r95072 | r95073 >
Date:10:18, 20 August 2011
Author:catrope
Status:ok (Comments)
Tags:
Comment:
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 (modified) (history)
  • /trunk/phase3/includes/Skin.php (modified) (history)
  • /trunk/phase3/includes/parser/Parser.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/GlobalFunctions.php
@@ -646,6 +646,26 @@
647647 }
648648
649649 /**
 650+ * Check whether a given URL has a domain that occurs in a given set of domains
 651+ * @param $url string URL
 652+ * @param $domains array Array of domains (strings)
 653+ * @return bool True if the host part of $url ends in one of the strings in $domains
 654+ */
 655+function wfMatchesDomainList( $url, $domains ) {
 656+ $bits = wfParseUrl( $url );
 657+ if ( is_array( $bits ) && isset( $bits['host'] ) ) {
 658+ foreach ( (array)$domains as $domain ) {
 659+ // FIXME: This gives false positives. http://nds-nl.wikipedia.org will match nl.wikipedia.org
 660+ // We should use something that interprets dots instead
 661+ if ( substr( $bits['host'], -strlen( $domain ) ) === $domain ) {
 662+ return true;
 663+ }
 664+ }
 665+ }
 666+ return false;
 667+}
 668+
 669+/**
650670 * Sends a line to the debug log if enabled or, optionally, to a comment in output.
651671 * In normal operation this is a NOP.
652672 *
Index: trunk/phase3/includes/parser/Parser.php
@@ -1648,23 +1648,12 @@
16491649 */
16501650 function getExternalLinkAttribs( $url = false ) {
16511651 $attribs = array();
1652 - global $wgNoFollowLinks, $wgNoFollowNsExceptions;
 1652+ global $wgNoFollowLinks, $wgNoFollowNsExceptions, $wgNoFollowDomainExceptions;
16531653 $ns = $this->mTitle->getNamespace();
1654 - if ( $wgNoFollowLinks && !in_array( $ns, $wgNoFollowNsExceptions ) ) {
 1654+ if ( $wgNoFollowLinks && !in_array( $ns, $wgNoFollowNsExceptions ) &&
 1655+ !wfMatchesDomainList( $url, $wgNoFollowDomainExceptions ) )
 1656+ {
16551657 $attribs['rel'] = 'nofollow';
1656 -
1657 - global $wgNoFollowDomainExceptions;
1658 - if ( $wgNoFollowDomainExceptions ) {
1659 - $bits = wfParseUrl( $url );
1660 - if ( is_array( $bits ) && isset( $bits['host'] ) ) {
1661 - foreach ( $wgNoFollowDomainExceptions as $domain ) {
1662 - if ( substr( $bits['host'], -strlen( $domain ) ) == $domain ) {
1663 - unset( $attribs['rel'] );
1664 - break;
1665 - }
1666 - }
1667 - }
1668 - }
16691658 }
16701659 if ( $this->mOptions->getExternalLinkTarget() ) {
16711660 $attribs['target'] = $this->mOptions->getExternalLinkTarget();
Index: trunk/phase3/includes/Skin.php
@@ -1153,24 +1153,13 @@
11541154
11551155 if ( preg_match( '/^(?:' . wfUrlProtocols() . ')/', $link ) ) {
11561156 $href = $link;
1157 - //Parser::getExternalLinkAttribs won't work here because of the Namespace things
1158 - global $wgNoFollowLinks;
1159 - if ( $wgNoFollowLinks ) {
 1157+
 1158+ // Parser::getExternalLinkAttribs won't work here because of the Namespace things
 1159+ global $wgNoFollowLinks, $wgNoFollowDomainExceptions;
 1160+ if ( $wgNoFollowLinks && !wfMatchesDomainList( $url, $wgNoFollowDomainExceptions ) ) {
11601161 $extraAttribs['rel'] = 'nofollow';
1161 -
1162 - global $wgNoFollowDomainExceptions;
1163 - if ( $wgNoFollowDomainExceptions ) {
1164 - $bits = wfParseUrl( $url );
1165 - if ( is_array( $bits ) && isset( $bits['host'] ) ) {
1166 - foreach ( $wgNoFollowDomainExceptions as $domain ) {
1167 - if ( substr( $bits['host'], -strlen( $domain ) ) == $domain ) {
1168 - unset( $extraAttribs['rel'] );
1169 - break;
1170 - }
1171 - }
1172 - }
1173 - }
11741162 }
 1163+
11751164 global $wgExternalLinkTarget;
11761165 if ( $wgExternalLinkTarget) {
11771166 $extraAttribs['target'] = $wgExternalLinkTarget;

Follow-up revisions

RevisionCommit summaryAuthorDate
r95074Followup r95072: add tests for wfMatchesDomainListcatrope10:41, 20 August 2011
r95632MFT to REL1_18...hashar17:57, 28 August 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r90849Apply rgcjones' patch for Bug#29533:...mah19:52, 26 June 2011

Comments

#Comment by Catrope (talk | contribs)   10:26, 20 August 2011

Tagging for 1.18 because r95073 depends on this.

Status & tagging log