Debian Codegraph @

includes/diff/TextDiffer/BaseTextDiffer.php

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