MediaWiki master
PPFrame_Hash.php
Go to the documentation of this file.
1<?php
8namespace MediaWiki\Parser;
9
10use InvalidArgumentException;
13use RuntimeException;
14use Stringable;
15
20// phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
21class PPFrame_Hash implements Stringable, PPFrame {
22
26 public $parser;
27
32
36 public $title;
37
42
49
55 public $depth;
56
58 private $volatile = false;
60 private $ttl = null;
61
69 private $maxPPNodeCount;
73 private $maxPPExpandDepth;
74
78 public function __construct( $preprocessor ) {
79 $this->preprocessor = $preprocessor;
80 $this->parser = $preprocessor->parser;
81 $this->title = $this->parser->getTitle();
82 $this->maxPPNodeCount = $this->parser->getOptions()->getMaxPPNodeCount();
83 $this->maxPPExpandDepth = $this->parser->getOptions()->getMaxPPExpandDepth();
84 $this->titleCache = [ $this->title ? $this->title->getPrefixedDBkey() : false ];
85 $this->loopCheckHash = [];
86 $this->depth = 0;
87 $this->childExpansionCache = [];
88 }
89
99 public function newChild( $args = false, $title = false, $indexOffset = 0 ) {
100 $namedArgs = [];
101 $numberedArgs = [];
102 if ( $title === false ) {
104 }
105 if ( $args !== false ) {
106 if ( $args instanceof PPNode_Hash_Array ) {
107 $args = $args->value;
108 } elseif ( !is_array( $args ) ) {
109 throw new InvalidArgumentException( __METHOD__ . ': $args must be array or PPNode_Hash_Array' );
110 }
111 foreach ( $args as $arg ) {
112 $bits = $arg->splitArg();
113 if ( $bits['index'] !== '' ) {
114 // Numbered parameter
115 $index = $bits['index'] - $indexOffset;
116 if ( isset( $namedArgs[$index] ) || isset( $numberedArgs[$index] ) ) {
117 $this->parser->getOutput()->addWarningMsg(
118 'duplicate-args-warning',
119 wfEscapeWikiText( (string)$this->title ),
120 wfEscapeWikiText( (string)$title ),
121 Message::numParam( $index )
122 );
123 $this->parser->addTrackingCategory( 'duplicate-args-category' );
124 }
125 $numberedArgs[$index] = $bits['value'];
126 unset( $namedArgs[$index] );
127 } else {
128 // Named parameter
129 $name = trim( $this->expand( $bits['name'], PPFrame::STRIP_COMMENTS ) );
130 if ( isset( $namedArgs[$name] ) || isset( $numberedArgs[$name] ) ) {
131 $this->parser->getOutput()->addWarningMsg(
132 'duplicate-args-warning',
133 wfEscapeWikiText( (string)$this->title ),
134 wfEscapeWikiText( (string)$title ),
135 // @phan-suppress-next-line SecurityCheck-DoubleEscaped
136 wfEscapeWikiText( $name )
137 );
138 $this->parser->addTrackingCategory( 'duplicate-args-category' );
139 }
140 $namedArgs[$name] = $bits['value'];
141 unset( $numberedArgs[$name] );
142 }
143 }
144 }
145 return new PPTemplateFrame_Hash( $this->preprocessor, $this, $numberedArgs, $namedArgs, $title );
146 }
147
154 public function cachedExpand( $key, $root, $flags = 0 ) {
155 // we don't have a parent, so we don't have a cache
156 return $this->expand( $root, $flags );
157 }
158
164 public function expand( $root, $flags = 0 ) {
165 static $expansionDepth = 0;
166 if ( is_string( $root ) ) {
167 return $root;
168 }
169
170 if ( ++$this->parser->mPPNodeCount > $this->maxPPNodeCount ) {
171 $this->parser->limitationWarn( 'node-count-exceeded',
172 $this->parser->mPPNodeCount,
173 $this->maxPPNodeCount
174 );
175 return '<span class="error">Node-count limit exceeded</span>';
176 }
177 if ( $expansionDepth > $this->maxPPExpandDepth ) {
178 $this->parser->limitationWarn( 'expansion-depth-exceeded',
179 $expansionDepth,
180 $this->maxPPExpandDepth
181 );
182 return '<span class="error">Expansion depth limit exceeded</span>';
183 }
184 ++$expansionDepth;
185 if ( $expansionDepth > $this->parser->mHighestExpansionDepth ) {
186 $this->parser->mHighestExpansionDepth = $expansionDepth;
187 }
188
189 $outStack = [ '', '' ];
190 $iteratorStack = [ false, $root ];
191 $indexStack = [ 0, 0 ];
192
193 while ( count( $iteratorStack ) > 1 ) {
194 $level = count( $outStack ) - 1;
195 $iteratorNode =& $iteratorStack[$level];
196 $out =& $outStack[$level];
197 $index =& $indexStack[$level];
198
199 if ( is_array( $iteratorNode ) ) {
200 if ( $index >= count( $iteratorNode ) ) {
201 // All done with this iterator
202 $iteratorStack[$level] = false;
203 $contextNode = false;
204 } else {
205 $contextNode = $iteratorNode[$index];
206 $index++;
207 }
208 } elseif ( $iteratorNode instanceof PPNode_Hash_Array ) {
209 if ( $index >= $iteratorNode->getLength() ) {
210 // All done with this iterator
211 $iteratorStack[$level] = false;
212 $contextNode = false;
213 } else {
214 $contextNode = $iteratorNode->item( $index );
215 $index++;
216 }
217 } else {
218 // Copy to $contextNode and then delete from iterator stack,
219 // because this is not an iterator but we do have to execute it once
220 $contextNode = $iteratorStack[$level];
221 $iteratorStack[$level] = false;
222 }
223
224 $newIterator = false;
225 $contextName = false;
226 $contextChildren = false;
227
228 if ( $contextNode === false ) {
229 // nothing to do
230 } elseif ( is_string( $contextNode ) ) {
231 $out .= $contextNode;
232 } elseif ( $contextNode instanceof PPNode_Hash_Array ) {
233 $newIterator = $contextNode;
234 } elseif ( $contextNode instanceof PPNode_Hash_Attr ) {
235 // No output
236 } elseif ( $contextNode instanceof PPNode_Hash_Text ) {
237 $out .= $contextNode->value;
238 } elseif ( $contextNode instanceof PPNode_Hash_Tree ) {
239 $contextName = $contextNode->name;
240 $contextChildren = $contextNode->getRawChildren();
241 } elseif ( is_array( $contextNode ) ) {
242 // Node descriptor array
243 if ( count( $contextNode ) !== 2 ) {
244 throw new RuntimeException( __METHOD__ .
245 ': found an array where a node descriptor should be' );
246 }
247 [ $contextName, $contextChildren ] = $contextNode;
248 } else {
249 throw new RuntimeException( __METHOD__ . ': Invalid parameter type' );
250 }
251
252 // Handle node descriptor array or tree object
253 if ( $contextName === false ) {
254 // Not a node, already handled above
255 } elseif ( $contextName[0] === '@' ) {
256 // Attribute: no output
257 } elseif ( $contextName === 'template' ) {
258 # Double-brace expansion
259 $bits = PPNode_Hash_Tree::splitRawTemplate( $contextChildren );
260 if ( $flags & PPFrame::NO_TEMPLATES ) {
261 $newIterator = $this->virtualBracketedImplode(
262 '{{', '|', '}}',
263 $bits['title'],
264 $bits['parts']
265 );
266 } else {
267 $ret = $this->parser->braceSubstitution( $bits, $this );
268 if ( isset( $ret['object'] ) ) {
269 $newIterator = $ret['object'];
270 } else {
271 $out .= $ret['text'];
272 }
273 }
274 } elseif ( $contextName === 'tplarg' ) {
275 # Triple-brace expansion
276 $bits = PPNode_Hash_Tree::splitRawTemplate( $contextChildren );
277 if ( $flags & PPFrame::NO_ARGS ) {
278 $newIterator = $this->virtualBracketedImplode(
279 '{{{', '|', '}}}',
280 $bits['title'],
281 $bits['parts']
282 );
283 } else {
284 $ret = $this->parser->argSubstitution( $bits, $this );
285 if ( isset( $ret['object'] ) ) {
286 $newIterator = $ret['object'];
287 } else {
288 $out .= $ret['text'];
289 }
290 }
291 } elseif ( $contextName === 'comment' ) {
292 # HTML-style comment
293 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
294 # Not in RECOVER_COMMENTS mode (msgnw) though.
295 if ( ( $this->parser->getOutputType() === Parser::OT_HTML
296 || ( $this->parser->getOutputType() === Parser::OT_PREPROCESS &&
297 $this->parser->getOptions()->getRemoveComments() )
298 || ( $flags & PPFrame::STRIP_COMMENTS )
299 ) && !( $flags & PPFrame::RECOVER_COMMENTS )
300 ) {
301 $out .= '';
302 } elseif (
303 $this->parser->getOutputType() === Parser::OT_WIKI &&
304 !( $flags & PPFrame::RECOVER_COMMENTS )
305 ) {
306 # Add a strip marker in PST mode so that pstPass2() can
307 # run some old-fashioned regexes on the result.
308 # Not in RECOVER_COMMENTS mode (extractSections) though.
309 $out .= $this->parser->insertStripItem( $contextChildren[0] );
310 } else {
311 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
312 $out .= $contextChildren[0];
313 }
314 } elseif ( $contextName === 'ignore' ) {
315 # Output suppression used by <includeonly> etc.
316 # OT_WIKI will only respect <ignore> in substed templates.
317 # The other output types respect it unless NO_IGNORE is set.
318 # extractSections() sets NO_IGNORE and so never respects it.
319 if ( ( !isset( $this->parent ) && $this->parser->getOutputType() === Parser::OT_WIKI )
320 || ( $flags & PPFrame::NO_IGNORE )
321 ) {
322 $out .= $contextChildren[0];
323 } else {
324 // $out .= '';
325 }
326 } elseif ( $contextName === 'ext' ) {
327 # Extension tag
328 $bits = PPNode_Hash_Tree::splitRawExt( $contextChildren ) +
329 [ 'attr' => null, 'inner' => null, 'close' => null ];
330 if ( $flags & PPFrame::NO_TAGS ) {
331 $s = '<' . $bits['name']->getFirstChild()->value;
332 if ( $bits['attr'] ) {
333 $s .= $bits['attr']->getFirstChild()->value;
334 }
335 if ( $bits['inner'] ) {
336 $s .= '>' . $bits['inner']->getFirstChild()->value;
337 if ( $bits['close'] ) {
338 $s .= $bits['close']->getFirstChild()->value;
339 }
340 } else {
341 $s .= '/>';
342 }
343 $out .= $s;
344 } else {
345 $out .= $this->parser->extensionSubstitution( $bits, $this,
346 (bool)( $flags & PPFrame::PROCESS_NOWIKI ) );
347 }
348 } elseif ( $contextName === 'h' ) {
349 # Heading
350 if ( $this->parser->getOutputType() === Parser::OT_HTML ) {
351 # Expand immediately and insert heading index marker
352 $s = $this->expand( $contextChildren, $flags );
353 $bits = PPNode_Hash_Tree::splitRawHeading( $contextChildren );
354 $titleText = $this->title->getPrefixedDBkey();
355 $this->parser->mHeadings[] = [ $titleText, $bits['i'] ];
356 $serial = count( $this->parser->mHeadings ) - 1;
357 $marker = Parser::MARKER_PREFIX . "-h-$serial-" . Parser::MARKER_SUFFIX;
358 $s = substr( $s, 0, $bits['level'] ) . $marker . substr( $s, $bits['level'] );
359 $this->parser->getStripState()->addGeneral( $marker, '' );
360 $out .= $s;
361 } else {
362 # Expand in virtual stack
363 $newIterator = $contextChildren;
364 }
365 } else {
366 # Generic recursive expansion
367 $newIterator = $contextChildren;
368 }
369
370 if ( $newIterator !== false ) {
371 $outStack[] = '';
372 $iteratorStack[] = $newIterator;
373 $indexStack[] = 0;
374 } elseif ( $iteratorStack[$level] === false ) {
375 // Return accumulated value to parent
376 // With tail recursion
377 while ( $iteratorStack[$level] === false && $level > 0 ) {
378 $outStack[$level - 1] .= $out;
379 array_pop( $outStack );
380 array_pop( $iteratorStack );
381 array_pop( $indexStack );
382 $level--;
383 }
384 }
385 }
386 --$expansionDepth;
387 return $outStack[0];
388 }
389
396 public function implodeWithFlags( $sep, $flags, ...$args ) {
397 $first = true;
398 $s = '';
399 foreach ( $args as $root ) {
400 if ( $root instanceof PPNode_Hash_Array ) {
401 $root = $root->value;
402 }
403 if ( !is_array( $root ) ) {
404 $root = [ $root ];
405 }
406 foreach ( $root as $node ) {
407 if ( $first ) {
408 $first = false;
409 } else {
410 $s .= $sep;
411 }
412 $s .= $this->expand( $node, $flags );
413 }
414 }
415 return $s;
416 }
417
425 public function implode( $sep, ...$args ) {
426 $first = true;
427 $s = '';
428 foreach ( $args as $root ) {
429 if ( $root instanceof PPNode_Hash_Array ) {
430 $root = $root->value;
431 }
432 if ( !is_array( $root ) ) {
433 $root = [ $root ];
434 }
435 foreach ( $root as $node ) {
436 if ( $first ) {
437 $first = false;
438 } else {
439 $s .= $sep;
440 }
441 $s .= $this->expand( $node );
442 }
443 }
444 return $s;
445 }
446
455 public function virtualImplode( $sep, ...$args ) {
456 $out = [];
457 $first = true;
458
459 foreach ( $args as $root ) {
460 if ( $root instanceof PPNode_Hash_Array ) {
461 $root = $root->value;
462 }
463 if ( !is_array( $root ) ) {
464 $root = [ $root ];
465 }
466 foreach ( $root as $node ) {
467 if ( $first ) {
468 $first = false;
469 } else {
470 $out[] = $sep;
471 }
472 $out[] = $node;
473 }
474 }
475 return new PPNode_Hash_Array( $out );
476 }
477
487 public function virtualBracketedImplode( $start, $sep, $end, ...$args ) {
488 $out = [ $start ];
489 $first = true;
490
491 foreach ( $args as $root ) {
492 if ( $root instanceof PPNode_Hash_Array ) {
493 $root = $root->value;
494 }
495 if ( !is_array( $root ) ) {
496 $root = [ $root ];
497 }
498 foreach ( $root as $node ) {
499 if ( $first ) {
500 $first = false;
501 } else {
502 $out[] = $sep;
503 }
504 $out[] = $node;
505 }
506 }
507 $out[] = $end;
508 return new PPNode_Hash_Array( $out );
509 }
510
511 public function __toString() {
512 return 'frame{}';
513 }
514
519 public function getPDBK( $level = false ) {
520 if ( $level === false ) {
521 return $this->title->getPrefixedDBkey();
522 } else {
523 return $this->titleCache[$level] ?? false;
524 }
525 }
526
530 public function getArguments() {
531 return [];
532 }
533
537 public function getNumberedArguments() {
538 return [];
539 }
540
544 public function getNamedArguments() {
545 return [];
546 }
547
553 public function isEmpty() {
554 return true;
555 }
556
561 public function getArgument( $name ) {
562 return false;
563 }
564
572 public function loopCheck( $title ) {
573 return !isset( $this->loopCheckHash[$title->getPrefixedDBkey()] );
574 }
575
581 public function isTemplate() {
582 return false;
583 }
584
590 public function getTitle() {
591 return $this->title;
592 }
593
599 public function setVolatile( $flag = true ) {
600 $this->volatile = $flag;
601 }
602
608 public function isVolatile() {
609 return $this->volatile;
610 }
611
616 public function setTTL( $ttl ) {
617 wfDeprecated( __METHOD__, '1.44' );
618 if ( $ttl !== null && ( $this->ttl === null || $ttl < $this->ttl ) ) {
619 $this->ttl = $ttl;
620 }
621 }
622
627 public function getTTL() {
628 wfDeprecated( __METHOD__, '1.46' );
629 return $this->ttl;
630 }
631}
632
634class_alias( PPFrame_Hash::class, 'PPFrame_Hash' );
wfEscapeWikiText( $input)
Escapes the given text so that it may be output using addWikiText() without any linking,...
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:144
An expansion frame, used as a context to expand the result of preprocessToObj()
implodeWithFlags( $sep, $flags,... $args)
setVolatile( $flag=true)
Set the volatile flag.
loopCheck( $title)
Returns true if the infinite loop check is OK, false if a loop is detected.
int $depth
Recursion depth of this frame, top = 0 Note that this is NOT the same as expansion depth in expand()
true[] $loopCheckHash
Hashtable listing templates which are disallowed for expansion in this frame, having been encountered...
isEmpty()
Returns true if there are no arguments in this frame.
cachedExpand( $key, $root, $flags=0)
virtualImplode( $sep,... $args)
Makes an object that, when expand()ed, will be the same as one obtained with implode()
implode( $sep,... $args)
Implode with no flags specified This previously called implodeWithFlags but has now been inlined to r...
getTitle()
Get a title of frame.
isTemplate()
Return true if the frame is a template frame.
isVolatile()
Get the volatile flag.
virtualBracketedImplode( $start, $sep, $end,... $args)
Virtual implode with brackets.
newChild( $args=false, $title=false, $indexOffset=0)
Create a new child frame $args is optionally a multi-root PPNode or array containing the template arg...
static splitRawHeading(array $children)
Like splitHeading() but for a raw child array.
static splitRawExt(array $children)
Like splitExt() but for a raw child array.
static splitRawTemplate(array $children)
Like splitTemplate() but for a raw child array.
Expansion frame with template arguments.
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:134
const OT_PREPROCESS
Output type: like Parser::preprocess()
Definition Parser.php:176
const OT_WIKI
Output type: like Parser::preSaveTransform()
Definition Parser.php:174
const OT_HTML
Output type: like Parser::parse()
Definition Parser.php:172
Represents a title within MediaWiki.
Definition Title.php:69
getPrefixedDBkey()
Get the prefixed database key form.
Definition Title.php:1845