MediaWiki master
WordAccumulator.php
Go to the documentation of this file.
1<?php
12namespace Wikimedia\Diff;
13
19 public $insClass = ' class="diffchange diffchange-inline"';
21 public $delClass = ' class="diffchange diffchange-inline"';
22
24 private $lines = [];
26 private $line = '';
28 private $group = '';
30 private $tag = '';
31
35 private function flushGroup( $new_tag ) {
36 if ( $this->group !== '' ) {
37 $encGroup = htmlspecialchars( $this->group, ENT_NOQUOTES );
38 if ( $this->tag == 'ins' ) {
39 $this->line .= "<ins{$this->insClass}>$encGroup</ins>";
40 } elseif ( $this->tag == 'del' ) {
41 $this->line .= "<del{$this->delClass}>$encGroup</del>";
42 } else {
43 $this->line .= $encGroup;
44 }
45 }
46 $this->group = '';
47 $this->tag = $new_tag;
48 }
49
53 private function flushLine( $new_tag ) {
54 $this->flushGroup( $new_tag );
55 if ( $this->line != '' ) {
56 $this->lines[] = $this->line;
57 } else {
58 # make empty lines visible by inserting an NBSP
59 $this->lines[] = "\u{00A0}";
60 }
61 $this->line = '';
62 }
63
68 public function addWords( $words, $tag = '' ) {
69 if ( $tag != $this->tag ) {
70 $this->flushGroup( $tag );
71 }
72
73 foreach ( $words as $word ) {
74 // new-line should only come as first char of word.
75 if ( $word == '' ) {
76 continue;
77 }
78 if ( $word[0] == "\n" ) {
79 $this->flushLine( $tag );
80 $word = substr( $word, 1 );
81 }
82 // FIXME: Don't use assert()
83 // phpcs:ignore MediaWiki.Usage.ForbiddenFunctions.assert
84 assert( !str_contains( $word, "\n" ) );
85 $this->group .= $word;
86 }
87 }
88
92 public function getLines() {
93 $this->flushLine( '~done' );
94
95 return $this->lines;
96 }
97}
98
100class_alias( WordAccumulator::class, 'MediaWiki\\Diff\\WordAccumulator' );
Stores, escapes and formats the results of word-level diff.