diff options
author | Max Semenik <maxsem.wiki@gmail.com> | 2016-04-26 11:57:44 -0700 |
---|---|---|
committer | Max Semenik <maxsem.wiki@gmail.com> | 2016-05-17 18:17:05 -0700 |
commit | 1cac442f1f1c72d7e71e34c1799ec8c2516b82c8 (patch) | |
tree | cfe9b4c3f3cefd8c2c1fb7d20ee0f335bf90b162 /includes/diff/WordAccumulator.php | |
parent | c9219c4f817f277e61009d1596c7026f541dce86 (diff) | |
download | mediawikicore-1cac442f1f1c72d7e71e34c1799ec8c2516b82c8.tar.gz mediawikicore-1cac442f1f1c72d7e71e34c1799ec8c2516b82c8.zip |
Refactor diffs
* Merge MappedDiff into WordLevelDiff
* Rename <something insane>WordAccumulator into WordAccumulator and namespace
* Better variable names
Change-Id: I5847f2bb89402d0537b9e99cccd24c547ce4e4e2
Diffstat (limited to 'includes/diff/WordAccumulator.php')
-rw-r--r-- | includes/diff/WordAccumulator.php | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/includes/diff/WordAccumulator.php b/includes/diff/WordAccumulator.php new file mode 100644 index 000000000000..a26775ffa80c --- /dev/null +++ b/includes/diff/WordAccumulator.php @@ -0,0 +1,107 @@ +<?php +/** + * Copyright © 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org> + * You may copy this code freely under the conditions of the GPL. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * + * @file + * @ingroup DifferenceEngine + * @defgroup DifferenceEngine DifferenceEngine + */ + +namespace MediaWiki\Diff; + +/** + * Stores, escapes and formats the results of word-level diff + * + * @private + * @ingroup DifferenceEngine + */ +class WordAccumulator { + 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; + } +} |