Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / parser / StripState.php
blobc0ffa38a7b3685d9ad3ef34fff2a9f6608bfbe09
1 <?php
2 /**
3 * Holder for stripped items when parsing wiki markup.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Parser
24 namespace MediaWiki\Parser;
26 use Closure;
27 use InvalidArgumentException;
29 /**
30 * @todo document, briefly.
31 * @newable
32 * @ingroup Parser
34 class StripState {
35 /** @var array[] */
36 protected $data;
37 /** @var string */
38 protected $regex;
40 protected ?Parser $parser;
42 /** @var array */
43 protected $circularRefGuard;
44 /** @var int */
45 protected $depth = 0;
46 /** @var int */
47 protected $highestDepth = 0;
48 /** @var int */
49 protected $expandSize = 0;
51 /** @var int */
52 protected $depthLimit = 20;
53 /** @var int */
54 protected $sizeLimit = 5_000_000;
56 /**
57 * @stable to call
59 * @param Parser|null $parser
60 * @param array $options
62 public function __construct( ?Parser $parser = null, $options = [] ) {
63 $this->data = [
64 'nowiki' => [],
65 'general' => []
67 $this->regex = '/' . Parser::MARKER_PREFIX . "([^\x7f<>&'\"]+)" . Parser::MARKER_SUFFIX . '/';
68 $this->circularRefGuard = [];
69 $this->parser = $parser;
71 if ( isset( $options['depthLimit'] ) ) {
72 $this->depthLimit = $options['depthLimit'];
74 if ( isset( $options['sizeLimit'] ) ) {
75 $this->sizeLimit = $options['sizeLimit'];
79 /**
80 * Add a nowiki strip item
81 * @param string $marker
82 * @param string|Closure $value
84 public function addNoWiki( $marker, $value ) {
85 $this->addItem( 'nowiki', $marker, $value );
88 /**
89 * @param string $marker
90 * @param string|Closure $value
92 public function addGeneral( $marker, $value ) {
93 $this->addItem( 'general', $marker, $value );
96 /**
97 * @param string $marker
98 * @param string|Closure $value
99 * @since 1.44
100 * @internal Parsoid use only.
102 public function addExtTag( $marker, $value ) {
103 $this->addItem( 'exttag', $marker, $value );
107 * @param string $type
108 * @param-taint $type none
109 * @param string $marker
110 * @param-taint $marker none
111 * @param string|Closure $value
112 * @param-taint $value exec_html
114 protected function addItem( $type, $marker, $value ) {
115 if ( !preg_match( $this->regex, $marker, $m ) ) {
116 throw new InvalidArgumentException( "Invalid marker: $marker" );
119 $this->data[$type][$m[1]] = $value;
123 * @param string $text
124 * @return mixed
126 public function unstripGeneral( $text ) {
127 return $this->unstripType( 'general', $text );
131 * @param string $text
132 * @return mixed
134 public function unstripNoWiki( $text ) {
135 return $this->unstripType( 'nowiki', $text );
139 * @param string $text
140 * @param callable $callback
141 * @return string
143 public function replaceNoWikis( string $text, callable $callback ): string {
144 // Shortcut
145 if ( !count( $this->data['nowiki'] ) ) {
146 return $text;
149 $callback = function ( $m ) use ( $callback ) {
150 $marker = $m[1];
151 if ( isset( $this->data['nowiki'][$marker] ) ) {
152 $value = $this->data['nowiki'][$marker];
153 if ( $value instanceof Closure ) {
154 $value = $value();
157 $this->expandSize += strlen( $value );
158 if ( $this->expandSize > $this->sizeLimit ) {
159 return $this->getLimitationWarning( 'unstrip-size', $this->sizeLimit );
162 return call_user_func( $callback, $value );
163 } else {
164 return $m[0];
168 return preg_replace_callback( $this->regex, $callback, $text );
172 * Split the given text by strip markers, returning an array that
173 * alternates between plain text and strip marker information. The
174 * strip marker information includes 'type', and 'content'. The
175 * resulting array will always be at least 1 element long and contain
176 * an odd number of elements.
177 * @return array<string|array{type:string,content:string}>
179 public function split( string $text ): array {
180 $pieces = preg_split( $this->regex, $text, -1, PREG_SPLIT_DELIM_CAPTURE );
181 for ( $i = 1; $i < count( $pieces ); $i += 2 ) {
182 $marker = $pieces[$i];
183 foreach ( $this->data as $type => $items ) {
184 if ( isset( $items[$marker] ) ) {
185 $pieces[$i] = [
186 'type' => $type,
187 'content' => $items[$marker],
189 continue 2;
192 $pieces[$i] = [
193 'marker' => $marker,
194 'type' => 'unknown',
195 'content' => null,
198 return $pieces;
202 * @param string $text
203 * @return mixed
205 public function unstripBoth( $text ) {
206 $text = $this->unstripType( 'general', $text );
207 $text = $this->unstripType( 'nowiki', $text );
208 return $text;
212 * @param string $type
213 * @param string $text
214 * @return mixed
216 protected function unstripType( $type, $text ) {
217 // Shortcut
218 if ( !count( $this->data[$type] ) ) {
219 return $text;
222 $callback = function ( $m ) use ( $type ) {
223 $marker = $m[1];
224 if ( isset( $this->data[$type][$marker] ) ) {
225 if ( isset( $this->circularRefGuard[$marker] ) ) {
226 return $this->getWarning( 'parser-unstrip-loop-warning' );
229 if ( $this->depth > $this->highestDepth ) {
230 $this->highestDepth = $this->depth;
232 if ( $this->depth >= $this->depthLimit ) {
233 return $this->getLimitationWarning( 'unstrip-depth', $this->depthLimit );
236 $value = $this->data[$type][$marker];
237 if ( $value instanceof Closure ) {
238 $value = $value();
241 $this->expandSize += strlen( $value );
242 if ( $this->expandSize > $this->sizeLimit ) {
243 return $this->getLimitationWarning( 'unstrip-size', $this->sizeLimit );
246 $this->circularRefGuard[$marker] = true;
247 $this->depth++;
248 $ret = $this->unstripType( $type, $value );
249 $this->depth--;
250 unset( $this->circularRefGuard[$marker] );
252 return $ret;
253 } else {
254 return $m[0];
258 $text = preg_replace_callback( $this->regex, $callback, $text );
259 return $text;
263 * Get warning HTML and register a limitation warning with the parser
265 * @param string $type
266 * @param int|string $max
267 * @return string
269 private function getLimitationWarning( $type, $max = '' ) {
270 if ( $this->parser ) {
271 $this->parser->limitationWarn( $type, $max );
273 return $this->getWarning( "$type-warning", $max );
277 * Get warning HTML
279 * @param string $message
280 * @param int|string $max
281 * @return string
283 private function getWarning( $message, $max = '' ) {
284 return '<span class="error">' .
285 wfMessage( $message )
286 ->numParams( $max )->inContentLanguage()->text() .
287 '</span>';
291 * Get an array of parameters to pass to ParserOutput::setLimitReportData()
293 * @internal Should only be called by Parser
294 * @return array
296 public function getLimitReport() {
297 return [
298 [ 'limitreport-unstrip-depth',
300 $this->highestDepth,
301 $this->depthLimit
304 [ 'limitreport-unstrip-size',
306 $this->expandSize,
307 $this->sizeLimit
314 * Remove any strip markers found in the given text.
316 * @param string $text
317 * @return string
319 public function killMarkers( $text ) {
320 return preg_replace( $this->regex, '', $text );
324 /** @deprecated class alias since 1.43 */
325 class_alias( StripState::class, 'StripState' );