| 1 |
<?php |
| 2 |
|
| 3 |
> |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
namespace Wikimedia\Diff; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
class WordAccumulator { |
| 32 |
|
| 33 |
public $insClass = ' class="diffchange diffchange-inline"'; |
| 34 |
|
| 35 |
public $delClass = ' class="diffchange diffchange-inline"'; |
| 36 |
|
| 37 |
|
| 38 |
private $lines = []; |
| 39 |
|
| 40 |
private $line = ''; |
| 41 |
|
| 42 |
private $group = ''; |
| 43 |
|
| 44 |
private $tag = ''; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
private function flushGroup( $new_tag ) { |
| 50 |
if ( $this->group !== '' ) { |
| 51 |
$encGroup = htmlspecialchars( $this->group, ENT_NOQUOTES ); |
| 52 |
if ( $this->tag == 'ins' ) { |
| 53 |
$this->line .= "<ins{$this->insClass}>$encGroup</ins>"; |
| 54 |
} elseif ( $this->tag == 'del' ) { |
| 55 |
$this->line .= "<del{$this->delClass}>$encGroup</del>"; |
| 56 |
} else { |
| 57 |
$this->line .= $encGroup; |
| 58 |
} |
| 59 |
} |
| 60 |
$this->group = ''; |
| 61 |
$this->tag = $new_tag; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
private function flushLine( $new_tag ) { |
| 68 |
$this->flushGroup( $new_tag ); |
| 69 |
if ( $this->line != '' ) { |
| 70 |
$this->lines[] = $this->line; |
| 71 |
} else { |
| 72 |
|
| 73 |
$this->lines[] = "\u{00A0}"; |
| 74 |
} |
| 75 |
$this->line = ''; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public function addWords( $words, $tag = '' ) { |
| 83 |
if ( $tag != $this->tag ) { |
| 84 |
$this->flushGroup( $tag ); |
| 85 |
} |
| 86 |
|
| 87 |
foreach ( $words as $word ) { |
| 88 |
|
| 89 |
if ( $word == '' ) { |
| 90 |
continue; |
| 91 |
} |
| 92 |
if ( $word[0] == "\n" ) { |
| 93 |
$this->flushLine( $tag ); |
| 94 |
$word = substr( $word, 1 ); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
assert( !strstr( $word, "\n" ) ); |
| 99 |
$this->group .= $word; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public function getLines() { |
| 107 |
$this->flushLine( '~done' ); |
| 108 |
|
| 109 |
return $this->lines; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
class_alias( WordAccumulator::class, 'MediaWiki\\Diff\\WordAccumulator' ); |