MediaWiki master
WordAccumulator.php
Go to the documentation of this file.
1<?php
26namespace Wikimedia\Diff;
27
33 public $insClass = ' class="diffchange diffchange-inline"';
35 public $delClass = ' class="diffchange diffchange-inline"';
36
38 private $lines = [];
40 private $line = '';
42 private $group = '';
44 private $tag = '';
45
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
67 private function flushLine( $new_tag ) {
68 $this->flushGroup( $new_tag );
69 if ( $this->line != '' ) {
70 $this->lines[] = $this->line;
71 } else {
72 # make empty lines visible by inserting an NBSP
73 $this->lines[] = "\u{00A0}";
74 }
75 $this->line = '';
76 }
77
82 public function addWords( $words, $tag = '' ) {
83 if ( $tag != $this->tag ) {
84 $this->flushGroup( $tag );
85 }
86
87 foreach ( $words as $word ) {
88 // new-line should only come as first char of word.
89 if ( $word == '' ) {
90 continue;
91 }
92 if ( $word[0] == "\n" ) {
93 $this->flushLine( $tag );
94 $word = substr( $word, 1 );
95 }
96 // FIXME: Don't use assert()
97 // phpcs:ignore MediaWiki.Usage.ForbiddenFunctions.assert
98 assert( !strstr( $word, "\n" ) );
99 $this->group .= $word;
100 }
101 }
102
106 public function getLines() {
107 $this->flushLine( '~done' );
108
109 return $this->lines;
110 }
111}
112
114class_alias( WordAccumulator::class, 'MediaWiki\\Diff\\WordAccumulator' );
Stores, escapes and formats the results of word-level diff.
if(!file_exists( $CREDITS)) $lines