jenkins-bot has submitted this change and it was merged.

Change subject: Accept diffs formatted by UnifiedDiffFormatter
......................................................................


Accept diffs formatted by UnifiedDiffFormatter

GNU diff and mediawiki's internal UnifiedDiffFormatter do not have
the same default formats.  Here we adjust the output of the internal
diff to match gnu diff as is expected by DiscussionParser.
Bug: 41689

Change-Id: Ib83cacab41adfbdfa8e122c0494b266d4caefc83
---
M includes/DiscussionParser.php
M tests/DiscussionParserTest.php
2 files changed, 76 insertions(+), 0 deletions(-)

Approvals:
  Kaldari: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/DiscussionParser.php b/includes/DiscussionParser.php
index 7141b16..9061058 100644
--- a/includes/DiscussionParser.php
+++ b/includes/DiscussionParser.php
@@ -540,6 +540,49 @@
        }
 
        /**
+        * 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.
         *
@@ -560,6 +603,9 @@
                $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 );
diff --git a/tests/DiscussionParserTest.php b/tests/DiscussionParserTest.php
index ad676a4..c6228f8 100644
--- a/tests/DiscussionParserTest.php
+++ b/tests/DiscussionParserTest.php
@@ -8,6 +8,36 @@
        // - 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();

-- 
To view, visit https://gerrit.wikimedia.org/r/61514
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ib83cacab41adfbdfa8e122c0494b266d4caefc83
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/Echo
Gerrit-Branch: master
Gerrit-Owner: EBernhardson (WMF) <[email protected]>
Gerrit-Reviewer: Alex Monk <[email protected]>
Gerrit-Reviewer: Bsitu <[email protected]>
Gerrit-Reviewer: Kaldari <[email protected]>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to