Introduce config var for moved-paragraph-detection threshold
authorTobias Gritschacher <tobias.gritschacher@wikimedia.de>
Thu, 8 Jun 2017 14:59:21 +0000 (16:59 +0200)
committerTobias Gritschacher <tobias.gritschacher@wikimedia.de>
Tue, 18 Jul 2017 09:07:04 +0000 (11:07 +0200)
This introduces a configuration variable for the bailout threshold for
the moved-paragraph-detection in wikidiff2.
This allows to rollout a new version of wikidiff2 that supports
detecting changes in moved paragraphs without changing behaviour of all
wikis in production as the default value of the setting (0) will leave the
new feature disabled.
Compatibility with older versions of wikidiff2 is retained by checking for
the version number of wikidiff2 and calling the method without the 4th
parameter if the version is < 0.3.0.

Bug: T166571
Change-Id: Ic01054354a4fbba410e58c9873edcbde797f883d

includes/DefaultSettings.php
includes/diff/DifferenceEngine.php
tests/phan/stubs/wikidiff.php

index 5b7ca3e..6a27709 100644 (file)
@@ -8281,6 +8281,20 @@ $wgUpdateRowsPerQuery = 100;
  */
 $wgExternalDiffEngine = false;
 
+/**
+ * wikidiff2 supports detection of changes in moved paragraphs.
+ * This setting controls the maximum number of paragraphs to compare before it bails out.
+ * Supported values:
+ * * 0: detection of moved paragraphs is disabled
+ * * int > 0: maximum number of paragraphs to compare
+ * Note: number of paragraph comparisons is in O(n^2).
+ * This setting is only effective if the wikidiff2 PHP/HHVM module is used as diffengine.
+ * See $wgExternalDiffEngine.
+ *
+ * @since 1.30
+ */
+$wgWikiDiff2MovedParagraphDetectionCutoff = 0;
+
 /**
  * Disable redirects to special pages and interwiki redirects, which use a 302
  * and have no "redirected from" link.
index b0ab244..525ea2d 100644 (file)
@@ -908,10 +908,35 @@ class DifferenceEngine extends ContextSource {
                        $wgExternalDiffEngine = false;
                }
 
+               // Better external diff engine, the 2 may some day be dropped
+               // This one does the escaping and segmenting itself
                if ( function_exists( 'wikidiff2_do_diff' ) && $wgExternalDiffEngine === false ) {
-                       # Better external diff engine, the 2 may some day be dropped
-                       # This one does the escaping and segmenting itself
-                       $text = wikidiff2_do_diff( $otext, $ntext, 2 );
+                       $wikidiff2Version = phpversion( 'wikidiff2' );
+                       if (
+                               $wikidiff2Version !== false &&
+                               version_compare( $wikidiff2Version, '0.3.0', '>=' )
+                       ) {
+                               $text = wikidiff2_do_diff(
+                                       $otext,
+                                       $ntext,
+                                       2,
+                                       $this->getConfig()->get( 'WikiDiff2MovedParagraphDetectionCutoff' )
+                               );
+                       } else {
+                               // Don't pass the 4th parameter for compatibility with older versions of wikidiff2
+                               $text = wikidiff2_do_diff(
+                                       $otext,
+                                       $ntext,
+                                       2
+                               );
+
+                               // Log a warning in case the configuration value is set to not silently ignore it
+                               if ( $this->getConfig()->get( 'WikiDiff2MovedParagraphDetectionCutoff' ) > 0 ) {
+                                       wfLogWarning( '$wgWikiDiff2MovedParagraphDetectionCutoff is set but has no
+                                               effect since the used version of WikiDiff2 does not support it.' );
+                               }
+                       }
+
                        $text .= $this->debug( 'wikidiff2' );
 
                        return $text;
index 9bd5d8d..1015b0b 100644 (file)
@@ -22,7 +22,8 @@
  * @param string $text1
  * @param string $text2
  * @param int $numContextLines
+ * @param int $movedParagraphDetectionCutoff
  * @return string
  */
-function wikidiff2_do_diff( $text1, $text2, $numContextLines ) {
+function wikidiff2_do_diff( $text1, $text2, $numContextLines, $movedParagraphDetectionCutoff = 0 ) {
 }