Merge "Accept diffs formatted by UnifiedDiffFormatter"

This commit is contained in:
jenkins-bot 2013-05-20 17:43:20 +00:00 committed by Gerrit Code Review
commit e6769b3391
2 changed files with 76 additions and 0 deletions

View file

@ -571,6 +571,49 @@ abstract class EchoDiscussionParser {
return $tsMatches[0][0];
}
/**
* Duplicates the check from the global wfDiff function to determine
* if we are using internal or external diff utilities
*/
static protected function usingInternalDiff() {
global $wgDiff;
wfSuppressWarnings();
$haveDiff = $wgDiff && file_exists( $wgDiff );
wfRestoreWarnings();
return !$haveDiff;
}
/**
* Strips a single space from the 2nd character of internal diff output
* For more info see bug 41689.
* @param $diff string
*/
static protected function fixInternalDiff( $diff ) {
$result = array();
$seenFirstLine = false;
foreach ( explode( "\n", $diff ) as $line ) {
if ( !$seenFirstLine ) {
$result[] = $line;
$seenFirstLine = true;
continue;
}
$len = strlen( $line );
if ( $len === 0 ) {
$result[] = '';
} elseif ( $len <= 2 ) {
$result[] = $line[0];
} elseif( $line[1] !== ' ' ) {
throw new MWException( "Internal diff did not match expected broken format. Line: `$line`" );
} else {
$result[] = $line[0] . substr( $line, 2 );
}
}
return implode( "\n", $result );
}
/**
* Finds differences between $oldText and $newText
* and returns the result in a machine-readable format.
@ -592,6 +635,9 @@ abstract class EchoDiscussionParser {
$oldText = trim( $oldText ) . "\n";
$newText = trim( $newText ) . "\n";
$diff = wfDiff( $oldText, $newText, '-u -w' );
if ( self::usingInternalDiff() ) {
$diff = self::fixInternalDiff( $diff );
}
$old_lines = explode( "\n", $oldText );
$new_lines = explode( "\n", $newText );

View file

@ -8,6 +8,36 @@ class EchoDiscussionParserTest extends MediaWikiTestCase {
// - stripSignature
// - getNotifiedUsersForComment
public function testDiscussionParserAcceptsInternalDiff() {
global $wgDiff;
$origWgDiff = $wgDiff;
$wgDiff = '/does/not/exist/or/at/least/we/hope/not';
try {
$res = EchoDiscussionParser::getMachineReadableDiff(
<<<TEXT
line 1
line 2
line 3
line 4
TEXT
,
<<<TEXT
line 1
line c
line 4
TEXT
);
} catch ( MWException $e ) {
$wgDiff = $origWgDiff;
throw $e;
}
$wgDiff = $origWgDiff;
// Test failure occurs when MWException is thrown due to parsing failure
$this->assertTrue( true );
}
public function testTimestampRegex() {
$exemplarTimestamp = self::getExemplarTimestamp();
$timestampRegex = EchoDiscussionParser::getTimestampRegex();