Debian Codegraph @

includes/parser/StripState.php

raw symbols SCIP
1 <?php
2 /**
3 * Holder for stripped items when parsing wiki markup.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Parser
22 */
23
24 namespace MediaWiki\Parser;
25
26 use Closure;
27 use InvalidArgumentException;
28
29 /**
30 * @todo document, briefly.
31 * @newable
32 * @ingroup Parser
33 */
34 class StripState {
35 /** @var array[] */
36 protected $data;
37 /** @var string */
38 protected $regex;
39
40 protected ?Parser $parser;
41
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;
50
51 /** @var int */
52 protected $depthLimit = 20;
53 /** @var int */
54 protected $sizeLimit = 5_000_000;
55
56 /**
57 * @stable to call
58 *
59 * @param Parser|null $parser
60 * @param array $options
61 */
62 public function __construct( ?Parser $parser = null, $options = [] ) {
63 $this->data = [
64 'nowiki' => [],
65 'general' => []
66 ];
67 $this->regex = '/' . Parser::MARKER_PREFIX . "([^\x7f<>&'\"]+)" . Parser::MARKER_SUFFIX . '/';
68 $this->circularRefGuard = [];
69 $this->parser = $parser;
70
71 if ( isset( $options['depthLimit'] ) ) {
72 $this->depthLimit = $options['depthLimit'];
73 }
74 if ( isset( $options['sizeLimit'] ) ) {
75 $this->sizeLimit = $options['sizeLimit'];
76 }
77 }
78
79 /**
80 * Add a nowiki strip item
81 * @param string $marker
82 * @param string|Closure $value
83 */
84 public function addNoWiki( $marker, $value ) {
85 $this->addItem( 'nowiki', $marker, $value );
86 }
87
88 /**
89 * @param string $marker
90 * @param string|Closure $value
91 */
92 public function addGeneral( $marker, $value ) {
93 $this->addItem( 'general', $marker, $value );
94 }
95
96 /**
97 * @param string $type
98 * @param-taint $type none
99 * @param string $marker
100 * @param-taint $marker none
101 * @param string|Closure $value
102 * @param-taint $value exec_html
103 */
104 protected function addItem( $type, $marker, $value ) {
105 if ( !preg_match( $this->regex, $marker, $m ) ) {
106 throw new InvalidArgumentException( "Invalid marker: $marker" );
107 }
108
109 $this->data[$type][$m[1]] = $value;
110 }
111
112 /**
113 * @param string $text
114 * @return mixed
115 */
116 public function unstripGeneral( $text ) {
117 return $this->unstripType( 'general', $text );
118 }
119
120 /**
121 * @param string $text
122 * @return mixed
123 */
124 public function unstripNoWiki( $text ) {
125 return $this->unstripType( 'nowiki', $text );
126 }
127
128 /**
129 * @param string $text
130 * @param callable $callback
131 * @return string
132 */
133 public function replaceNoWikis( string $text, callable $callback ): string {
134 // Shortcut
135 if ( !count( $this->data['nowiki'] ) ) {
136 return $text;
137 }
138
139 $callback = function ( $m ) use ( $callback ) {
140 $marker = $m[1];
141 if ( isset( $this->data['nowiki'][$marker] ) ) {
142 $value = $this->data['nowiki'][$marker];
143 if ( $value instanceof Closure ) {
144 $value = $value();
145 }
146
147 $this->expandSize += strlen( $value );
148 if ( $this->expandSize > $this->sizeLimit ) {
149 return $this->getLimitationWarning( 'unstrip-size', $this->sizeLimit );
150 }
151
152 return call_user_func( $callback, $value );
153 } else {
154 return $m[0];
155 }
156 };
157
158 return preg_replace_callback( $this->regex, $callback, $text );
159 }
160
161 /**
162 * @param string $text
163 * @return mixed
164 */
165 public function unstripBoth( $text ) {
166 $text = $this->unstripType( 'general', $text );
167 $text = $this->unstripType( 'nowiki', $text );
168 return $text;
169 }
170
171 /**
172 * @param string $type
173 * @param string $text
174 * @return mixed
175 */
176 protected function unstripType( $type, $text ) {
177 // Shortcut
178 if ( !count( $this->data[$type] ) ) {
179 return $text;
180 }
181
182 $callback = function ( $m ) use ( $type ) {
183 $marker = $m[1];
184 if ( isset( $this->data[$type][$marker] ) ) {
185 if ( isset( $this->circularRefGuard[$marker] ) ) {
186 return $this->getWarning( 'parser-unstrip-loop-warning' );
187 }
188
189 if ( $this->depth > $this->highestDepth ) {
190 $this->highestDepth = $this->depth;
191 }
192 if ( $this->depth >= $this->depthLimit ) {
193 return $this->getLimitationWarning( 'unstrip-depth', $this->depthLimit );
194 }
195
196 $value = $this->data[$type][$marker];
197 if ( $value instanceof Closure ) {
198 $value = $value();
199 }
200
201 $this->expandSize += strlen( $value );
202 if ( $this->expandSize > $this->sizeLimit ) {
203 return $this->getLimitationWarning( 'unstrip-size', $this->sizeLimit );
204 }
205
206 $this->circularRefGuard[$marker] = true;
207 $this->depth++;
208 $ret = $this->unstripType( $type, $value );
209 $this->depth--;
210 unset( $this->circularRefGuard[$marker] );
211
212 return $ret;
213 } else {
214 return $m[0];
215 }
216 };
217
218 $text = preg_replace_callback( $this->regex, $callback, $text );
219 return $text;
220 }
221
222 /**
223 * Get warning HTML and register a limitation warning with the parser
224 *
225 * @param string $type
226 * @param int|string $max
227 * @return string
228 */
229 private function getLimitationWarning( $type, $max = '' ) {
230 if ( $this->parser ) {
231 $this->parser->limitationWarn( $type, $max );
232 }
233 return $this->getWarning( "$type-warning", $max );
234 }
235
236 /**
237 * Get warning HTML
238 *
239 * @param string $message
240 * @param int|string $max
241 * @return string
242 */
243 private function getWarning( $message, $max = '' ) {
244 return '<span class="error">' .
245 wfMessage( $message )
246 ->numParams( $max )->inContentLanguage()->text() .
247 '</span>';
248 }
249
250 /**
251 * Get an array of parameters to pass to ParserOutput::setLimitReportData()
252 *
253 * @internal Should only be called by Parser
254 * @return array
255 */
256 public function getLimitReport() {
257 return [
258 [ 'limitreport-unstrip-depth',
259 [
260 $this->highestDepth,
261 $this->depthLimit
262 ],
263 ],
264 [ 'limitreport-unstrip-size',
265 [
266 $this->expandSize,
267 $this->sizeLimit
268 ],
269 ]
270 ];
271 }
272
273 /**
274 * Remove any strip markers found in the given text.
275 *
276 * @param string $text
277 * @return string
278 */
279 public function killMarkers( $text ) {
280 return preg_replace( $this->regex, '', $text );
281 }
282 }
283
284 /** @deprecated class alias since 1.43 */
285 class_alias( StripState::class, 'StripState' );