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
24 namespace MediaWiki\Parser
;
27 use InvalidArgumentException
;
30 * @todo document, briefly.
40 protected ?Parser
$parser;
43 protected $circularRefGuard;
47 protected $highestDepth = 0;
49 protected $expandSize = 0;
52 protected $depthLimit = 20;
54 protected $sizeLimit = 5_000_000
;
59 * @param Parser|null $parser
60 * @param array $options
62 public function __construct( ?Parser
$parser = null, $options = [] ) {
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'];
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 );
89 * @param string $marker
90 * @param string|Closure $value
92 public function addGeneral( $marker, $value ) {
93 $this->addItem( 'general', $marker, $value );
97 * @param string $marker
98 * @param string|Closure $value
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
126 public function unstripGeneral( $text ) {
127 return $this->unstripType( 'general', $text );
131 * @param string $text
134 public function unstripNoWiki( $text ) {
135 return $this->unstripType( 'nowiki', $text );
139 * @param string $text
140 * @param callable $callback
143 public function replaceNoWikis( string $text, callable
$callback ): string {
145 if ( !count( $this->data
['nowiki'] ) ) {
149 $callback = function ( $m ) use ( $callback ) {
151 if ( isset( $this->data
['nowiki'][$marker] ) ) {
152 $value = $this->data
['nowiki'][$marker];
153 if ( $value instanceof Closure
) {
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 );
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] ) ) {
187 'content' => $items[$marker],
202 * @param string $text
205 public function unstripBoth( $text ) {
206 $text = $this->unstripType( 'general', $text );
207 $text = $this->unstripType( 'nowiki', $text );
212 * @param string $type
213 * @param string $text
216 protected function unstripType( $type, $text ) {
218 if ( !count( $this->data
[$type] ) ) {
222 $callback = function ( $m ) use ( $type ) {
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
) {
241 $this->expandSize +
= strlen( $value );
242 if ( $this->expandSize
> $this->sizeLimit
) {
243 return $this->getLimitationWarning( 'unstrip-size', $this->sizeLimit
);
246 $this->circularRefGuard
[$marker] = true;
248 $ret = $this->unstripType( $type, $value );
250 unset( $this->circularRefGuard
[$marker] );
258 $text = preg_replace_callback( $this->regex
, $callback, $text );
263 * Get warning HTML and register a limitation warning with the parser
265 * @param string $type
266 * @param int|string $max
269 private function getLimitationWarning( $type, $max = '' ) {
270 if ( $this->parser
) {
271 $this->parser
->limitationWarn( $type, $max );
273 return $this->getWarning( "$type-warning", $max );
279 * @param string $message
280 * @param int|string $max
283 private function getWarning( $message, $max = '' ) {
284 return '<span class="error">' .
285 wfMessage( $message )
286 ->numParams( $max )->inContentLanguage()->text() .
291 * Get an array of parameters to pass to ParserOutput::setLimitReportData()
293 * @internal Should only be called by Parser
296 public function getLimitReport() {
298 [ 'limitreport-unstrip-depth',
304 [ 'limitreport-unstrip-size',
314 * Remove any strip markers found in the given text.
316 * @param string $text
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' );