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