r12987 - Code Review

From MediaWiki.org

Jump to: navigation, search
Repository:MediaWiki
Revision:r12986 | r12987 (on ViewVC) | r12988 >
Date:13:04, 9 February 2006
Author:timstarling
Status:new
Tags:
Comment:Added support for wikidiff2 and similar external diff engines.
Modified paths:

Diff [purge]

Index: trunk/phase3/includes/DifferenceEngine.php
===================================================================
--- trunk/phase3/includes/DifferenceEngine.php	(revision 12986)
+++ trunk/phase3/includes/DifferenceEngine.php	(revision 12987)
@@ -44,7 +44,7 @@
 		$this->mTitle = $titleObj;
 		wfDebug("DifferenceEngine old '$old' new '$new' rcid '$rcid'\n");
 
-		if ( 'prev' == $new ) {
+		if ( 'prev' === $new ) {
 			# Show diff between revision $old and the previous one.
 			# Get previous one from DB.
 			#
@@ -52,7 +52,7 @@
 
 			$this->mOldid = $this->mTitle->getPreviousRevisionID( $this->mNewid );
 
-		} elseif ( 'next' == $new ) {
+		} elseif ( 'next' === $new ) {
 			# Show diff between revision $old and the previous one.
 			# Get previous one from DB.
 			#
@@ -316,8 +316,10 @@
 	 * Returns false on error
 	 */
 	function getDiffBody() {
-		global $wgUseExternalDiffEngine, $wgContLang, $wgMemc, $wgDBname;
-
+		global $wgExternalDiffEngine, $wgContLang, $wgMemc, $wgDBname;
+		$fname = 'DifferenceEngine::getDiffBody';
+		wfProfileIn( $fname );
+		
 		// Cacheable?
 		$key = false;
 		if ( $this->mOldid && $this->mNewid ) {
@@ -328,17 +330,19 @@
 				wfIncrStats( 'diff_cache_hit' );
 				$difftext = $this->localiseLineNumbers( $difftext );
 				$difftext .= "\n<!-- diff cache key $key -->\n"; 
+				wfProfileOut( $fname );
 				return $difftext;
 			}
 		}
 
 		if ( !$this->loadText() ) {
+			wfProfileOut( $fname );
 			return false;
 		}
 
 		$otext = $wgContLang->segmentForDiff($this->mOldtext);
 		$ntext = $wgContLang->segmentForDiff($this->mNewtext);
-		if ( $wgUseExternalDiffEngine ) {
+		if ( $wgExternalDiffEngine == 'wikidiff' ) {
 			# For historical reasons, external diff engine expects
 			# input text to be HTML-escaped already
 			$otext = str_replace( "\r\n", "\n", htmlspecialchars ( $otext ) );
@@ -347,6 +351,43 @@
 				dl('php_wikidiff.so');
 			}
 			$difftext = wikidiff_do_diff( $otext, $ntext, 2 );
+		} elseif ( $wgExternalDiffEngine == 'wikidiff2' ) {
+			# Better external diff engine, the 2 may some day be dropped
+			# This one does the escaping itself
+			$otext = str_replace( "\r\n", "\n", $otext );
+			$ntext = str_replace( "\r\n", "\n", $ntext );			
+			if ( !function_exists( 'wikidiff2_do_diff' ) ) {
+				dl('php_wikidiff2.so');
+			}
+			$difftext = wikidiff2_do_diff( $otext, $ntext, 2 );
+		} elseif ( $wgExternalDiffEngine !== false ) {
+			# Diff via the shell
+			global $wgTmpDirectory;
+			$otext = str_replace( "\r\n", "\n", $otext );
+			$ntext = str_replace( "\r\n", "\n", $ntext );
+			$tempName1 = tempnam( $wgTmpDirectory, 'diff_' );
+			$tempName2 = tempnam( $wgTmpDirectory, 'diff_' );
+
+			$tempFile1 = fopen( $tempName1, "w" );
+			if ( !$tempFile1 ) {
+				wfProfileOut( $fname );
+				return false;
+			}
+			$tempFile2 = fopen( $tempName2, "w" );
+			if ( !$tempFile2 ) {
+				wfProfileOut( $fname );
+				return false;
+			}
+			fwrite( $tempFile1, $otext );
+			fwrite( $tempFile2, $ntext );
+			fclose( $tempFile1 );
+			fclose( $tempFile2 );
+			$cmd = wfEscapeShellArg( $wgExternalDiffEngine, $tempName1, $tempName2 );
+			wfProfileIn( "$fname-shellexec" );
+			$difftext = wfShellExec( $cmd );
+			wfProfileOut( "$fname-shellexec" );
+			unlink( $tempName1 );
+			unlink( $tempName2 );
 		} else {
 			$ota = explode( "\n", str_replace( "\r\n", "\n", $otext ) );
 			$nta = explode( "\n", str_replace( "\r\n", "\n", $ntext ) );
@@ -365,6 +406,7 @@
 		}
 		// Replace line numbers with the text in the user's language
 		$difftext = $this->localiseLineNumbers( $difftext );
+		wfProfileOut( $fname );
 		return $difftext;
 	}
 
Index: trunk/phase3/includes/DefaultSettings.php
===================================================================
--- trunk/phase3/includes/DefaultSettings.php	(revision 12986)
+++ trunk/phase3/includes/DefaultSettings.php	(revision 12987)
@@ -1474,8 +1474,8 @@
 /** Maximum indent level of toc. */
 $wgMaxTocLevel = 999;
 
-/** Use external C++ diff engine (module wikidiff from the extensions package) */
-$wgUseExternalDiffEngine = false;
+/** Name of the external diff engine to use */
+$wgExternalDiffEngine = false;
 
 /** Use RC Patrolling to check for vandalism */
 $wgUseRCPatrol = true;
Index: trunk/phase3/RELEASE-NOTES
===================================================================
--- trunk/phase3/RELEASE-NOTES	(revision 12986)
+++ trunk/phase3/RELEASE-NOTES	(revision 12987)
@@ -232,6 +232,7 @@
 * (bug 4267) Switch dv sd ug ks arc languages to RTL
 * Default main page content improved per bug 4690
 * (bug 4615) Update for Portuguese language (pt)
+* Separated MessagesSl.php as the other languages.
 
 Parser:
 * (bug 2522) {{CURRENTDAY2}} now shows the current day number with two digits
@@ -606,7 +607,7 @@
 * (bug 4147) Added cleanupWatchlist.php to clear out bogus watchlist entries
 * (partial bug 3456) Disable auto redirect to Main Page after account creation
 * (bug 4824) Separate out IE7 CSS compat hacks, fix for RTL pages
-* Separated MessagesSl.php as the other languages.
+* Added support for wikidiff2 and similar external diff engines.
 
 
 === Caveats ===
Views
Toolbox