MediaWiki REL1_37
WordAccumulator.php
Go to the documentation of this file.
1<?php
26namespace MediaWiki\Diff;
27
36 public $insClass = ' class="diffchange diffchange-inline"';
38 public $delClass = ' class="diffchange diffchange-inline"';
39
41 private $lines = [];
43 private $line = '';
45 private $group = '';
47 private $tag = '';
48
52 private function flushGroup( $new_tag ) {
53 if ( $this->group !== '' ) {
54 if ( $this->tag == 'ins' ) {
55 $this->line .= "<ins{$this->insClass}>" . htmlspecialchars( $this->group ) . '</ins>';
56 } elseif ( $this->tag == 'del' ) {
57 $this->line .= "<del{$this->delClass}>" . htmlspecialchars( $this->group ) . '</del>';
58 } else {
59 $this->line .= htmlspecialchars( $this->group );
60 }
61 }
62 $this->group = '';
63 $this->tag = $new_tag;
64 }
65
69 private function flushLine( $new_tag ) {
70 $this->flushGroup( $new_tag );
71 if ( $this->line != '' ) {
72 array_push( $this->lines, $this->line );
73 } else {
74 # make empty lines visible by inserting an NBSP
75 array_push( $this->lines, "\u{00A0}" );
76 }
77 $this->line = '';
78 }
79
84 public function addWords( $words, $tag = '' ) {
85 if ( $tag != $this->tag ) {
86 $this->flushGroup( $tag );
87 }
88
89 foreach ( $words as $word ) {
90 // new-line should only come as first char of word.
91 if ( $word == '' ) {
92 continue;
93 }
94 if ( $word[0] == "\n" ) {
95 $this->flushLine( $tag );
96 $word = substr( $word, 1 );
97 }
98 // FIXME: Don't use assert()
99 // phpcs:ignore MediaWiki.Usage.ForbiddenFunctions.assert
100 assert( !strstr( $word, "\n" ) );
101 $this->group .= $word;
102 }
103 }
104
108 public function getLines() {
109 $this->flushLine( '~done' );
110
111 return $this->lines;
112 }
113}
Stores, escapes and formats the results of word-level diff.