MaxSem has uploaded a new change for review.
https://gerrit.wikimedia.org/r/285443
Change subject: Refactoring
......................................................................
Refactoring
Change-Id: I5847f2bb89402d0537b9e99cccd24c547ce4e4e2
---
M autoload.php
M includes/diff/DairikiDiff.php
M includes/diff/TableDiffFormatter.php
3 files changed, 7 insertions(+), 236 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core
refs/changes/43/285443/1
diff --git a/autoload.php b/autoload.php
index f802ddd..b64244f 100644
--- a/autoload.php
+++ b/autoload.php
@@ -532,7 +532,6 @@
'HTMLTextFieldWithButton' => __DIR__ .
'/includes/htmlform/HTMLTextFieldWithButton.php',
'HTMLTitleTextField' => __DIR__ .
'/includes/htmlform/HTMLTitleTextField.php',
'HTMLUserTextField' => __DIR__ .
'/includes/htmlform/HTMLUserTextField.php',
- 'HWLDFWordAccumulator' => __DIR__ . '/includes/diff/DairikiDiff.php',
'HashBagOStuff' => __DIR__ .
'/includes/libs/objectcache/HashBagOStuff.php',
'HashConfig' => __DIR__ . '/includes/config/HashConfig.php',
'HashRing' => __DIR__ . '/includes/libs/HashRing.php',
@@ -762,7 +761,6 @@
'MalformedTitleException' => __DIR__ .
'/includes/title/MalformedTitleException.php',
'ManualLogEntry' => __DIR__ . '/includes/logging/LogEntry.php',
'MapCacheLRU' => __DIR__ . '/includes/libs/MapCacheLRU.php',
- 'MappedDiff' => __DIR__ . '/includes/diff/DairikiDiff.php',
'MappedIterator' => __DIR__ . '/includes/libs/MappedIterator.php',
'MarkpatrolledAction' => __DIR__ .
'/includes/actions/MarkpatrolledAction.php',
'McTest' => __DIR__ . '/maintenance/mctest.php',
@@ -777,6 +775,8 @@
'MediaWikiSite' => __DIR__ . '/includes/site/MediaWikiSite.php',
'MediaWikiTitleCodec' => __DIR__ .
'/includes/title/MediaWikiTitleCodec.php',
'MediaWikiVersionFetcher' => __DIR__ .
'/includes/MediaWikiVersionFetcher.php',
+ 'MediaWiki\\Diff\\WordAccumulator' => __DIR__ .
'/includes/diff/WordAccumulator.php',
+ 'MediaWiki\\Diff\\WordLevelDiff' => __DIR__ .
'/includes/diff/WordLevelDiff.php',
'MediaWiki\\Languages\\Data\\Names' => __DIR__ .
'/languages/data/Names.php',
'MediaWiki\\Languages\\Data\\ZhConversion' => __DIR__ .
'/languages/data/ZhConversion.php',
'MediaWiki\\Linker\\LinkTarget' => __DIR__ .
'/includes/linker/LinkTarget.php',
@@ -1448,7 +1448,6 @@
'WikitextContentHandler' => __DIR__ .
'/includes/content/WikitextContentHandler.php',
'WinCacheBagOStuff' => __DIR__ .
'/includes/libs/objectcache/WinCacheBagOStuff.php',
'WithoutInterwikiPage' => __DIR__ .
'/includes/specials/SpecialWithoutinterwiki.php',
- 'WordLevelDiff' => __DIR__ . '/includes/diff/DairikiDiff.php',
'WrapOldPasswords' => __DIR__ . '/maintenance/wrapOldPasswords.php',
'XCFHandler' => __DIR__ . '/includes/media/XCF.php',
'XCacheBagOStuff' => __DIR__ .
'/includes/libs/objectcache/XCacheBagOStuff.php',
diff --git a/includes/diff/DairikiDiff.php b/includes/diff/DairikiDiff.php
index e5e082f..165585b 100644
--- a/includes/diff/DairikiDiff.php
+++ b/includes/diff/DairikiDiff.php
@@ -448,11 +448,11 @@
*
* @param string[] $from_lines An array of strings.
* Typically these are lines from a file.
- * @param string[] $to_lines An array of strings.
+ * @param string[] $to_linesAfter An array of strings.
*/
- public function __construct( $from_lines, $to_lines ) {
+ public function __construct( $from_lines, $to_linesAfter ) {
$eng = new DiffEngine;
- $this->edits = $eng->diff( $from_lines, $to_lines );
+ $this->edits = $eng->diff( $from_lines, $to_linesAfter );
}
/**
@@ -559,236 +559,6 @@
}
/**
- * @todo document, bad name.
- * @private
- * @ingroup DifferenceEngine
- */
-class MappedDiff extends Diff {
- /**
- * Constructor.
- *
- * Computes diff between sequences of strings.
- *
- * This can be used to compute things like
- * case-insensitve diffs, or diffs which ignore
- * changes in white-space.
- *
- * @param string[] $from_lines An array of strings.
- * Typically these are lines from a file.
- * @param string[] $to_lines An array of strings.
- * @param string[] $mapped_from_lines This array should
- * have the same size number of elements as $from_lines.
- * The elements in $mapped_from_lines and
- * $mapped_to_lines are what is actually compared
- * when computing the diff.
- * @param string[] $mapped_to_lines This array should
- * have the same number of elements as $to_lines.
- */
- public function __construct( $from_lines, $to_lines,
- $mapped_from_lines, $mapped_to_lines ) {
-
- assert( count( $from_lines ) == count( $mapped_from_lines ) );
- assert( count( $to_lines ) == count( $mapped_to_lines ) );
-
- parent::__construct( $mapped_from_lines, $mapped_to_lines );
-
- $xi = $yi = 0;
- $editCount = count( $this->edits );
- for ( $i = 0; $i < $editCount; $i++ ) {
- $orig = &$this->edits[$i]->orig;
- if ( is_array( $orig ) ) {
- $orig = array_slice( $from_lines, $xi, count(
$orig ) );
- $xi += count( $orig );
- }
-
- $closing = &$this->edits[$i]->closing;
- if ( is_array( $closing ) ) {
- $closing = array_slice( $to_lines, $yi, count(
$closing ) );
- $yi += count( $closing );
- }
- }
- }
-}
-
-/**
* Additions by Axel Boldt follow, partly taken from diff.php, phpwiki-1.3.3
*/
-/**
- * @todo document
- * @private
- * @ingroup DifferenceEngine
- */
-class HWLDFWordAccumulator {
- public $insClass = ' class="diffchange diffchange-inline"';
- public $delClass = ' class="diffchange diffchange-inline"';
-
- private $lines = [];
- private $line = '';
- private $group = '';
- private $tag = '';
-
- /**
- * @param string $new_tag
- */
- private function flushGroup( $new_tag ) {
- if ( $this->group !== '' ) {
- if ( $this->tag == 'ins' ) {
- $this->line .= "<ins{$this->insClass}>" .
- htmlspecialchars( $this->group ) .
'</ins>';
- } elseif ( $this->tag == 'del' ) {
- $this->line .= "<del{$this->delClass}>" .
- htmlspecialchars( $this->group ) .
'</del>';
- } else {
- $this->line .= htmlspecialchars( $this->group );
- }
- }
- $this->group = '';
- $this->tag = $new_tag;
- }
-
- /**
- * @param string $new_tag
- */
- private function flushLine( $new_tag ) {
- $this->flushGroup( $new_tag );
- if ( $this->line != '' ) {
- array_push( $this->lines, $this->line );
- } else {
- # make empty lines visible by inserting an NBSP
- array_push( $this->lines, ' ' );
- }
- $this->line = '';
- }
-
- /**
- * @param string[] $words
- * @param string $tag
- */
- public function addWords( $words, $tag = '' ) {
- if ( $tag != $this->tag ) {
- $this->flushGroup( $tag );
- }
-
- foreach ( $words as $word ) {
- // new-line should only come as first char of word.
- if ( $word == '' ) {
- continue;
- }
- if ( $word[0] == "\n" ) {
- $this->flushLine( $tag );
- $word = substr( $word, 1 );
- }
- assert( !strstr( $word, "\n" ) );
- $this->group .= $word;
- }
- }
-
- /**
- * @return string[]
- */
- public function getLines() {
- $this->flushLine( '~done' );
-
- return $this->lines;
- }
-}
-
-/**
- * @todo document
- * @private
- * @ingroup DifferenceEngine
- */
-class WordLevelDiff extends MappedDiff {
- const MAX_LINE_LENGTH = 10000;
-
- /**
- * @param string[] $orig_lines
- * @param string[] $closing_lines
- */
- public function __construct( $orig_lines, $closing_lines ) {
-
- list( $orig_words, $orig_stripped ) = $this->split( $orig_lines
);
- list( $closing_words, $closing_stripped ) = $this->split(
$closing_lines );
-
- parent::__construct( $orig_words, $closing_words,
- $orig_stripped, $closing_stripped );
- }
-
- /**
- * @param string[] $lines
- *
- * @return array[]
- */
- private function split( $lines ) {
-
- $words = [];
- $stripped = [];
- $first = true;
- foreach ( $lines as $line ) {
- # If the line is too long, just pretend the entire line
is one big word
- # This prevents resource exhaustion problems
- if ( $first ) {
- $first = false;
- } else {
- $words[] = "\n";
- $stripped[] = "\n";
- }
- if ( strlen( $line ) > self::MAX_LINE_LENGTH ) {
- $words[] = $line;
- $stripped[] = $line;
- } else {
- $m = [];
- if ( preg_match_all( '/ ( [^\S\n]+ |
[0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
- $line, $m )
- ) {
- foreach ( $m[0] as $word ) {
- $words[] = $word;
- }
- foreach ( $m[1] as $stripped_word ) {
- $stripped[] = $stripped_word;
- }
- }
- }
- }
-
- return [ $words, $stripped ];
- }
-
- /**
- * @return string[]
- */
- public function orig() {
- $orig = new HWLDFWordAccumulator;
-
- foreach ( $this->edits as $edit ) {
- if ( $edit->type == 'copy' ) {
- $orig->addWords( $edit->orig );
- } elseif ( $edit->orig ) {
- $orig->addWords( $edit->orig, 'del' );
- }
- }
- $lines = $orig->getLines();
-
- return $lines;
- }
-
- /**
- * @return string[]
- */
- public function closing() {
- $closing = new HWLDFWordAccumulator;
-
- foreach ( $this->edits as $edit ) {
- if ( $edit->type == 'copy' ) {
- $closing->addWords( $edit->closing );
- } elseif ( $edit->closing ) {
- $closing->addWords( $edit->closing, 'ins' );
- }
- }
- $lines = $closing->getLines();
-
- return $lines;
- }
-
-}
diff --git a/includes/diff/TableDiffFormatter.php
b/includes/diff/TableDiffFormatter.php
index bcae746..463d67b 100644
--- a/includes/diff/TableDiffFormatter.php
+++ b/includes/diff/TableDiffFormatter.php
@@ -24,6 +24,8 @@
* @ingroup DifferenceEngine
*/
+use MediaWiki\Diff\WordLevelDiff;
+
/**
* MediaWiki default table style diff formatter
* @todo document
--
To view, visit https://gerrit.wikimedia.org/r/285443
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I5847f2bb89402d0537b9e99cccd24c547ce4e4e2
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: MaxSem <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits