Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
WordAccumulator
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 4
156
0.00% covered (danger)
0.00%
0 / 1
 flushGroup
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
 flushLine
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 addWords
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
30
 getLines
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Copyright © 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org>
4 * You may copy this code freely under the conditions of the GPL.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 * @ingroup DifferenceEngine
23 * @defgroup DifferenceEngine DifferenceEngine
24 */
25
26namespace Wikimedia\Diff;
27
28/**
29 * Stores, escapes and formats the results of word-level diff
30 */
31class WordAccumulator {
32    /** @var string */
33    public $insClass = ' class="diffchange diffchange-inline"';
34    /** @var string */
35    public $delClass = ' class="diffchange diffchange-inline"';
36
37    /** @var array */
38    private $lines = [];
39    /** @var string */
40    private $line = '';
41    /** @var string */
42    private $group = '';
43    /** @var string */
44    private $tag = '';
45
46    /**
47     * @param string $new_tag
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     * @param string $new_tag
66     */
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
78    /**
79     * @param string[] $words
80     * @param string $tag
81     */
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
103    /**
104     * @return string[]
105     */
106    public function getLines() {
107        $this->flushLine( '~done' );
108
109        return $this->lines;
110    }
111}
112
113/** @deprecated class alias since 1.41 */
114class_alias( WordAccumulator::class, 'MediaWiki\\Diff\\WordAccumulator' );