From d6d3169754dc379085ad854391b498a108bbc979 Mon Sep 17 00:00:00 2001 From: Daimona Eaytoy Date: Sun, 8 Apr 2018 17:38:39 +0200 Subject: [PATCH] Use empty arrays instead of empty strings for diffs Otherwise, a blank page will be considered as having a newline inside, which won't be marked as added (or removed) in the diff. This requires introducing a new method and leaving the old one for backward compatibility, and may cause regressions. Bug: T74329 Change-Id: I9a2397fd849544b499cad97a383e5331471e9d73 --- includes/AFComputedVariable.php | 17 +++++++++++++++++ includes/AbuseFilter.php | 4 ++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/includes/AFComputedVariable.php b/includes/AFComputedVariable.php index 01f469926..e94abf848 100644 --- a/includes/AFComputedVariable.php +++ b/includes/AFComputedVariable.php @@ -154,6 +154,8 @@ class AFComputedVariable { switch ( $this->mMethod ) { case 'diff': + // Currently unused. Kept for backwards compatibility since it remains + // as mMethod for old variables. A fallthrough would instead change old results. $text1Var = $parameters['oldtext-var']; $text2Var = $parameters['newtext-var']; $text1 = $vars->getVar( $text1Var )->toString(); @@ -162,6 +164,21 @@ class AFComputedVariable { $format = new UnifiedDiffFormatter(); $result = $format->format( $diffs ); break; + case 'diff-array': + // Introduced with T74329 to uniform the diff to MW's standard one. + // The difference with 'diff' method is noticeable when one of the + // $text is empty: it'll be treated as **really** empty, instead of + // an empty string. + $text1Var = $parameters['oldtext-var']; + $text2Var = $parameters['newtext-var']; + $text1 = $vars->getVar( $text1Var )->toString(); + $text2 = $vars->getVar( $text2Var )->toString(); + $text1 = $text1 === '' ? [] : explode( "\n", $text1 ); + $text2 = $text2 === '' ? [] : explode( "\n", $text2 ); + $diffs = new Diff( $text1, $text2 ); + $format = new UnifiedDiffFormatter(); + $result = $format->format( $diffs ); + break; case 'diff-split': $diff = $vars->getVar( $parameters['diff-var'] )->toString(); $line_prefix = $parameters['line-prefix']; diff --git a/includes/AbuseFilter.php b/includes/AbuseFilter.php index 285220227..bfc86b00e 100644 --- a/includes/AbuseFilter.php +++ b/includes/AbuseFilter.php @@ -2362,9 +2362,9 @@ class AbuseFilter { $page = WikiPage::factory( $title ); } - $vars->setLazyLoadVar( 'edit_diff', 'diff', + $vars->setLazyLoadVar( 'edit_diff', 'diff-array', [ 'oldtext-var' => 'old_wikitext', 'newtext-var' => 'new_wikitext' ] ); - $vars->setLazyLoadVar( 'edit_diff_pst', 'diff', + $vars->setLazyLoadVar( 'edit_diff_pst', 'diff-array', [ 'oldtext-var' => 'old_wikitext', 'newtext-var' => 'new_pst' ] ); $vars->setLazyLoadVar( 'new_size', 'length', [ 'length-var' => 'new_wikitext' ] ); $vars->setLazyLoadVar( 'old_size', 'length', [ 'length-var' => 'old_wikitext' ] );