| 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 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
abstract class BaseTextDiffer implements TextDiffer { |
| 19 |
|
| 20 |
private $localizer; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public function setLocalizer( |
| 26 |
MessageLocalizer $localizer |
| 27 |
) { |
| 28 |
$this->localizer = $localizer; |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 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 |
|
| 59 |
|
| 60 |
|
| 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 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 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 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
protected function localizeLineNumbers( |
| 123 |
$text, $reducedLineNumbers |
| 124 |
) { |
| 125 |
|
| 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 |
|
| 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 |
} |