r41333 - Code Review

From MediaWiki.org

Jump to: navigation, search
Repository:MediaWiki
Revision:r41332 | r41333 (on ViewVC) | r41334 >
Date:02:35, 28 September 2008
Author:tstarling
Status:ok (Comments)
Tags:reverted 
Comment:* Added the ability to set the target attribute on external links with $wgExternalLinkTarget
* Removed the namespace parameter from Linker::makeExternalLink(), added a generic associative array of attributes instead. Let the Parser decide whether to use rel=nofollow.
Modified paths:

Diff [purge]

Index: trunk/phase3/includes/parser/Parser_OldPP.php
===================================================================
--- trunk/phase3/includes/parser/Parser_OldPP.php	(revision 41332)
+++ trunk/phase3/includes/parser/Parser_OldPP.php	(revision 41333)
@@ -1353,7 +1353,7 @@
 			# This means that users can paste URLs directly into the text
 			# Funny characters like ö aren't valid in URLs anyway
 			# This was changed in August 2004
-			$s .= $sk->makeExternalLink( $url, $text, false, $linktype, $this->mTitle->getNamespace() ) . $dtrail . $trail;
+			$s .= $sk->makeExternalLink( $url, $text, false, $linktype, $this->getExternalLinkAttribs() ) . $dtrail . $trail;
 
 			# Register link in the output object.
 			# Replace unnecessary URL escape codes with the referenced character
@@ -1366,6 +1366,19 @@
 		return $s;
 	}
 
+	function getExternalLinkAttribs() {
+		$attribs = array();
+		global $wgNoFollowLinks, $wgNoFollowNsExceptions;
+		$ns = $this->mTitle->getNamespace();
+		if( $wgNoFollowLinks && !in_array($ns, $wgNoFollowNsExceptions) ) {
+			$attribs['rel'] = 'nofollow';
+		}
+		if ( $this->mOptions->getExternalLinkTarget() ) {
+			$attribs['target'] = $this->mOptions->getExternalLinkTarget();
+		}
+		return $attribs;
+	}
+
 	/**
 	 * Replace anything that looks like a URL with a link
 	 * @private
@@ -1432,7 +1445,8 @@
 				$text = $this->maybeMakeExternalImage( $url );
 				if ( $text === false ) {
 					# Not an image, make a link
-					$text = $sk->makeExternalLink( $url, $wgContLang->markNoConversion($url), true, 'free', $this->mTitle->getNamespace() );
+					$text = $sk->makeExternalLink( $url, $wgContLang->markNoConversion($url), true, 'free',
+						$this->getExternalLinkAttribs() );
 					# Register it in the output object...
 					# Replace unnecessary URL escape codes with their equivalent characters
 					$pasteurized = self::replaceUnusualEscapes( $url );
Index: trunk/phase3/includes/parser/Parser.php
===================================================================
--- trunk/phase3/includes/parser/Parser.php	(revision 41332)
+++ trunk/phase3/includes/parser/Parser.php	(revision 41333)
@@ -1118,7 +1118,8 @@
 		$text = $this->maybeMakeExternalImage( $url );
 		if ( $text === false ) {
 			# Not an image, make a link
-			$text = $sk->makeExternalLink( $url, $wgContLang->markNoConversion($url), true, 'free', $this->mTitle->getNamespace() );
+			$text = $sk->makeExternalLink( $url, $wgContLang->markNoConversion($url), true, 'free', 
+				$this->getExternalLinkAttribs() );
 			# Register it in the output object...
 			# Replace unnecessary URL escape codes with their equivalent characters
 			$pasteurized = self::replaceUnusualEscapes( $url );
@@ -1393,11 +1394,18 @@
 
 			$url = Sanitizer::cleanUrl( $url );
 
+			if ( $this->mOptions->mExternalLinkTarget ) {
+				$attribs = array( 'target' => $this->mOptions->mExternalLinkTarget );
+			} else {
+				$attribs = array();
+			}
+
 			# Use the encoded URL
 			# This means that users can paste URLs directly into the text
 			# Funny characters like ö aren't valid in URLs anyway
 			# This was changed in August 2004
-			$s .= $sk->makeExternalLink( $url, $text, false, $linktype, $this->mTitle->getNamespace() ) . $dtrail . $trail;
+			$s .= $sk->makeExternalLink( $url, $text, false, $linktype, $this->getExternalLinkAttribs() ) 
+				. $dtrail . $trail;
 
 			# Register link in the output object.
 			# Replace unnecessary URL escape codes with the referenced character
@@ -1410,6 +1418,20 @@
 		return $s;
 	}
 
+	function getExternalLinkAttribs() {
+		$attribs = array();
+		global $wgNoFollowLinks, $wgNoFollowNsExceptions;
+		$ns = $this->mTitle->getNamespace();
+		if( $wgNoFollowLinks && !in_array($ns, $wgNoFollowNsExceptions) ) {
+			$attribs['rel'] = 'nofollow';
+		}
+		if ( $this->mOptions->getExternalLinkTarget() ) {
+			$attribs['target'] = $this->mOptions->getExternalLinkTarget();
+		}
+		return $attribs;
+	}
+
+
 	/**
 	 * Replace unusual URL escape codes with their equivalent characters
 	 * @param string
Index: trunk/phase3/includes/parser/ParserOptions.php
===================================================================
--- trunk/phase3/includes/parser/ParserOptions.php	(revision 41332)
+++ trunk/phase3/includes/parser/ParserOptions.php	(revision 41333)
@@ -30,6 +30,7 @@
 	var $mTemplateCallback;          # Callback for template fetching
 	var $mEnableLimitReport;         # Enable limit report in an HTML comment on output
 	var $mTimestamp;                 # Timestamp used for {{CURRENTDAY}} etc.
+	var $mExternalLinkTarget;        # Target attribute for external links
 
 	var $mUser;                      # Stored user object, just used to initialise the skin
 
@@ -52,6 +53,7 @@
 	function getTemplateCallback()              { return $this->mTemplateCallback; }
 	function getEnableLimitReport()             { return $this->mEnableLimitReport; }
 	function getCleanSignatures()               { return $this->mCleanSignatures; }
+	function getExternalLinkTarget()            { return $this->mExternalLinkTarget; }
 
 	function getSkin() {
 		if ( !isset( $this->mSkin ) ) {
@@ -96,6 +98,7 @@
 	function enableLimitReport( $x = true )     { return wfSetVar( $this->mEnableLimitReport, $x ); }
 	function setTimestamp( $x )                 { return wfSetVar( $this->mTimestamp, $x ); }
 	function setCleanSignatures( $x )           { return wfSetVar( $this->mCleanSignatures, $x ); }
+	function setExternalLinkTarget( $x )        { return wfSetVar( $this->mExternalLinkTarget, $x ); }
 
 	function __construct( $user = null ) {
 		$this->initialiseFromUser( $user );
@@ -114,6 +117,7 @@
 		global $wgUseTeX, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
 		global $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion, $wgMaxArticleSize;
 		global $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth, $wgCleanSignatures;
+		global $wgExternalLinkTarget;
 		$fname = 'ParserOptions::initialiseFromUser';
 		wfProfileIn( $fname );
 		if ( !$userInput ) {
@@ -151,6 +155,7 @@
 		$this->mTemplateCallback = array( 'Parser', 'statelessFetchTemplate' );
 		$this->mEnableLimitReport = false;
 		$this->mCleanSignatures = $wgCleanSignatures;
+		$this->mExternalLinkTarget = $wgExternalLinkTarget;
 		wfProfileOut( $fname );
 	}
 }
Index: trunk/phase3/includes/Linker.php
===================================================================
--- trunk/phase3/includes/Linker.php	(revision 41332)
+++ trunk/phase3/includes/Linker.php	(revision 41333)
@@ -990,11 +990,10 @@
 	}
 
 	/** @todo document */
-	function makeExternalLink( $url, $text, $escape = true, $linktype = '', $ns = null ) {
-		$style = $this->getExternalLinkAttributes( $url, $text, 'external ' . $linktype );
-		global $wgNoFollowLinks, $wgNoFollowNsExceptions;
-		if( $wgNoFollowLinks && !(isset($ns) && in_array($ns, $wgNoFollowNsExceptions)) ) {
-			$style .= ' rel="nofollow"';
+	function makeExternalLink( $url, $text, $escape = true, $linktype = '', $attribs = array() ) {
+		$attribsText = $this->getExternalLinkAttributes( $url, $text, 'external ' . $linktype );
+		if ( $attribs ) {
+			$attribsText .= ' ' . Xml::expandAttributes( $attribs );
 		}
 		$url = htmlspecialchars( $url );
 		if( $escape ) {
@@ -1006,7 +1005,7 @@
 			wfDebug("Hook LinkerMakeExternalLink changed the output of link with url {$url} and text {$text} to {$link}", true);
 			return $link;
 		}
-		return '<a href="'.$url.'"'.$style.'>'.$text.'</a>';
+		return '<a href="'.$url.'"'.$attribsText.'>'.$text.'</a>';
 	}
 
 	/**
Index: trunk/phase3/includes/DefaultSettings.php
===================================================================
--- trunk/phase3/includes/DefaultSettings.php	(revision 41332)
+++ trunk/phase3/includes/DefaultSettings.php	(revision 41333)
@@ -2883,6 +2883,11 @@
 $wgSearchForwardUrl = null;
 
 /**
+ * Set a default target for external links, e.g. _blank to pop up a new window
+ */
+$wgExternalLinkTarget = false;
+
+/**
  * If true, external URL links in wiki text will be given the
  * rel="nofollow" attribute as a hint to search engines that
  * they should not be followed for ranking purposes as they
Index: trunk/phase3/RELEASE-NOTES
===================================================================
--- trunk/phase3/RELEASE-NOTES	(revision 41332)
+++ trunk/phase3/RELEASE-NOTES	(revision 41333)
@@ -142,6 +142,8 @@
 * Improved upload file type detection for OpenDocument formats
 * (bug 15739) Add $wgArticlePathForCurid to make links with only curid=# as the
   query string use the article path, rather than the script path
+* Added the ability to set the target attribute on external links with 
+  $wgExternalLinkTarget
 
 
 === Bug fixes in 1.14 ===

Follow-up revisions

RevisionCommit summaryAuthorDate
r41406Back out r41333 -- causes lots of parser test regressions due to funny spacing. ...brion00:14, 30 September 2008
r41409Revert revert r41406 of r41333, and removed one space between attributes.tstarling01:00, 30 September 2008

Comments

#Comment by Brion VIBBER (Talk | contribs)   20:38, 30 September 2008

Backed out in r41406:

Back out r41333 -- causes lots of parser test regressions due to funny spacing. Probably an easy fix but it wasn't tested apparently.  :)

#Comment by Tim Starling (Talk | contribs)   01:04, 1 October 2008

Fixed in r41409

Views
Toolbox