MediaWiki master
StripState.php
Go to the documentation of this file.
1<?php
30 protected $data;
31 protected $regex;
32
33 protected $parser;
34
36 protected $depth = 0;
37 protected $highestDepth = 0;
38 protected $expandSize = 0;
39
40 protected $depthLimit = 20;
41 protected $sizeLimit = 5000000;
42
49 public function __construct( Parser $parser = null, $options = [] ) {
50 $this->data = [
51 'nowiki' => [],
52 'general' => []
53 ];
54 $this->regex = '/' . Parser::MARKER_PREFIX . "([^\x7f<>&'\"]+)" . Parser::MARKER_SUFFIX . '/';
55 $this->circularRefGuard = [];
56 $this->parser = $parser;
57
58 if ( isset( $options['depthLimit'] ) ) {
59 $this->depthLimit = $options['depthLimit'];
60 }
61 if ( isset( $options['sizeLimit'] ) ) {
62 $this->sizeLimit = $options['sizeLimit'];
63 }
64 }
65
71 public function addNoWiki( $marker, $value ) {
72 $this->addItem( 'nowiki', $marker, $value );
73 }
74
79 public function addGeneral( $marker, $value ) {
80 $this->addItem( 'general', $marker, $value );
81 }
82
91 protected function addItem( $type, $marker, $value ) {
92 if ( !preg_match( $this->regex, $marker, $m ) ) {
93 throw new InvalidArgumentException( "Invalid marker: $marker" );
94 }
95
96 $this->data[$type][$m[1]] = $value;
97 }
98
103 public function unstripGeneral( $text ) {
104 return $this->unstripType( 'general', $text );
105 }
106
111 public function unstripNoWiki( $text ) {
112 return $this->unstripType( 'nowiki', $text );
113 }
114
120 public function replaceNoWikis( string $text, callable $callback ): string {
121 // Shortcut
122 if ( !count( $this->data['nowiki'] ) ) {
123 return $text;
124 }
125
126 $callback = function ( $m ) use ( $callback ) {
127 $marker = $m[1];
128 if ( isset( $this->data['nowiki'][$marker] ) ) {
129 $value = $this->data['nowiki'][$marker];
130 if ( $value instanceof Closure ) {
131 $value = $value();
132 }
133
134 $this->expandSize += strlen( $value );
135 if ( $this->expandSize > $this->sizeLimit ) {
136 return $this->getLimitationWarning( 'unstrip-size', $this->sizeLimit );
137 }
138
139 return call_user_func( $callback, $value );
140 } else {
141 return $m[0];
142 }
143 };
144
145 return preg_replace_callback( $this->regex, $callback, $text );
146 }
147
152 public function unstripBoth( $text ) {
153 $text = $this->unstripType( 'general', $text );
154 $text = $this->unstripType( 'nowiki', $text );
155 return $text;
156 }
157
163 protected function unstripType( $type, $text ) {
164 // Shortcut
165 if ( !count( $this->data[$type] ) ) {
166 return $text;
167 }
168
169 $callback = function ( $m ) use ( $type ) {
170 $marker = $m[1];
171 if ( isset( $this->data[$type][$marker] ) ) {
172 if ( isset( $this->circularRefGuard[$marker] ) ) {
173 return $this->getWarning( 'parser-unstrip-loop-warning' );
174 }
175
176 if ( $this->depth > $this->highestDepth ) {
177 $this->highestDepth = $this->depth;
178 }
179 if ( $this->depth >= $this->depthLimit ) {
180 return $this->getLimitationWarning( 'unstrip-depth', $this->depthLimit );
181 }
182
183 $value = $this->data[$type][$marker];
184 if ( $value instanceof Closure ) {
185 $value = $value();
186 }
187
188 $this->expandSize += strlen( $value );
189 if ( $this->expandSize > $this->sizeLimit ) {
190 return $this->getLimitationWarning( 'unstrip-size', $this->sizeLimit );
191 }
192
193 $this->circularRefGuard[$marker] = true;
194 $this->depth++;
195 $ret = $this->unstripType( $type, $value );
196 $this->depth--;
197 unset( $this->circularRefGuard[$marker] );
198
199 return $ret;
200 } else {
201 return $m[0];
202 }
203 };
204
205 $text = preg_replace_callback( $this->regex, $callback, $text );
206 return $text;
207 }
208
216 private function getLimitationWarning( $type, $max = '' ) {
217 if ( $this->parser ) {
218 $this->parser->limitationWarn( $type, $max );
219 }
220 return $this->getWarning( "$type-warning", $max );
221 }
222
230 private function getWarning( $message, $max = '' ) {
231 return '<span class="error">' .
232 wfMessage( $message )
233 ->numParams( $max )->inContentLanguage()->text() .
234 '</span>';
235 }
236
243 public function getLimitReport() {
244 return [
245 [ 'limitreport-unstrip-depth',
246 [
247 $this->highestDepth,
248 $this->depthLimit
249 ],
250 ],
251 [ 'limitreport-unstrip-size',
252 [
253 $this->expandSize,
254 $this->sizeLimit
255 ],
256 ]
257 ];
258 }
259
266 public function killMarkers( $text ) {
267 return preg_replace( $this->regex, '', $text );
268 }
269}
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:88
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:118
unstripBoth( $text)
unstripGeneral( $text)
addNoWiki( $marker, $value)
Add a nowiki strip item.
getLimitReport()
Get an array of parameters to pass to ParserOutput::setLimitReportData()
unstripType( $type, $text)
replaceNoWikis(string $text, callable $callback)
addGeneral( $marker, $value)
killMarkers( $text)
Remove any strip markers found in the given text.
addItem( $type, $marker, $value)
__construct(Parser $parser=null, $options=[])
unstripNoWiki( $text)