Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.48% covered (success)
90.48%
38 / 42
69.23% covered (warning)
69.23%
9 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
BaseTextDiffer
90.48% covered (success)
90.48%
38 / 42
69.23% covered (warning)
69.23%
9 / 13
23.46
0.00% covered (danger)
0.00%
0 / 1
 setLocalizer
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getLocalizer
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasFormat
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addRowWrapper
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
3.02
 validateFormats
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 render
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 renderBatch
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 doRenderBatch
n/a
0 / 0
n/a
0 / 0
0
 addModules
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCacheKeys
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 localize
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getTablePrefixes
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPreferredFormatBatch
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 localizeLineNumbers
94.12% covered (success)
94.12%
16 / 17
0.00% covered (danger)
0.00%
0 / 1
7.01
1<?php
2
3namespace MediaWiki\Diff\TextDiffer;
4
5use InvalidArgumentException;
6use MediaWiki\Html\Html;
7use MediaWiki\Language\MessageLocalizer;
8use MediaWiki\Output\OutputPage;
9
10/**
11 * The base class for specific implementations of TextDiffer, apart from
12 * ManifoldTextDiffer.
13 *
14 * A place for protected utility functions.
15 *
16 * @since 1.41
17 */
18abstract class BaseTextDiffer implements TextDiffer {
19    /** @var MessageLocalizer */
20    private $localizer;
21
22    public function setLocalizer(
23        MessageLocalizer $localizer
24    ) {
25        $this->localizer = $localizer;
26    }
27
28    /**
29     * Provide a MessageLocalizer, or throw if setLocalizer() has not been called.
30     */
31    protected function getLocalizer(): MessageLocalizer {
32        return $this->localizer;
33    }
34
35    public function hasFormat( string $format ): bool {
36        return in_array( $format, $this->getFormats(), true );
37    }
38
39    public function addRowWrapper( string $format, string $diffText ): string {
40        $context = $this->getFormatContext( $format );
41        if ( $context === self::CONTEXT_PLAIN ) {
42            return "<tr><td colspan=\"4\">$diffText</td></tr>";
43        } elseif ( $context === self::CONTEXT_PRE ) {
44            return '<tr><td colspan="4">' .
45                Html::element( 'pre', [], $diffText ) .
46                '</td></tr>';
47        } else {
48            return $diffText;
49        }
50    }
51
52    /**
53     * Throw an exception if any of the formats in the array is not supported.
54     *
55     * @param string[] $formats
56     */
57    protected function validateFormats( $formats ) {
58        $badFormats = array_diff( $formats, $this->getFormats() );
59        if ( $badFormats ) {
60            throw new InvalidArgumentException( 'The requested format is not supported by this engine' );
61        }
62    }
63
64    public function render( string $oldText, string $newText, string $format ): string {
65        $result = $this->renderBatch( $oldText, $newText, [ $format ] );
66        return reset( $result );
67    }
68
69    public function renderBatch( string $oldText, string $newText, array $formats ): array {
70        $this->validateFormats( $formats );
71        if ( !count( $formats ) ) {
72            return [];
73        }
74        return $this->doRenderBatch( $oldText, $newText, $formats );
75    }
76
77    /**
78     * Subclasses should override this to render diffs in the specified formats.
79     * The $formats array is guaranteed to not be empty and to contain only
80     * formats supported by the subclass.
81     *
82     * @param string $oldText
83     * @param string $newText
84     * @param array $formats
85     * @return array
86     */
87    abstract protected function doRenderBatch( string $oldText, string $newText, array $formats ): array;
88
89    public function addModules( OutputPage $out, string $format ): void {
90    }
91
92    public function getCacheKeys( array $formats ): array {
93        return [];
94    }
95
96    public function localize( string $format, string $diff, array $options = [] ): string {
97        return $diff;
98    }
99
100    public function getTablePrefixes( string $format ): array {
101        return [];
102    }
103
104    public function getPreferredFormatBatch( string $format ): array {
105        return [ $format ];
106    }
107
108    /**
109     * Replace a common convention for language-independent line numbers with
110     * the text in the language of the current localizer.
111     *
112     * @param string $text
113     * @param bool $reducedLineNumbers
114     *
115     * @return string
116     */
117    protected function localizeLineNumbers(
118        $text, $reducedLineNumbers
119    ) {
120        // Inline diffs.
121        $text = preg_replace_callback( '/<!-- LINES (\d+),(\d+) -->/',
122            function ( array $matches ) use ( $reducedLineNumbers ) {
123                if ( $matches[1] === '1' && $matches[2] === '1' && $reducedLineNumbers ) {
124                    return '';
125                }
126                $msg = $matches[1] === $matches[2]
127                    ? 'lineno'
128                    : 'lineno-inline';
129                return $this->getLocalizer()->msg( $msg )
130                    ->numParams( $matches[1], $matches[2] )
131                    ->escaped();
132            }, $text );
133
134        // Table diffs.
135        return preg_replace_callback( '/<!--LINE (\d+)-->/',
136            function ( array $matches ) use ( $reducedLineNumbers ) {
137                if ( $matches[1] === '1' && $reducedLineNumbers ) {
138                    return '';
139                }
140                return $this->getLocalizer()->msg( 'lineno' )->numParams( $matches[1] )->escaped();
141            }, $text );
142    }
143
144}