66 private $volatile =
false;
77 private $maxPPNodeCount;
81 private $maxPPExpandDepth;
89 $this->title = $this->parser->getTitle();
90 $this->maxPPNodeCount = $this->parser->getOptions()->getMaxPPNodeCount();
91 $this->maxPPExpandDepth = $this->parser->getOptions()->getMaxPPExpandDepth();
92 $this->titleCache = [ $this->title ? $this->title->getPrefixedDBkey() : false ];
93 $this->loopCheckHash = [];
95 $this->childExpansionCache = [];
113 if ( $args !==
false ) {
115 $args = $args->value;
116 } elseif ( !is_array( $args ) ) {
117 throw new InvalidArgumentException( __METHOD__ .
': $args must be array or PPNode_Hash_Array' );
119 foreach ( $args as $arg ) {
120 $bits = $arg->splitArg();
121 if ( $bits[
'index'] !==
'' ) {
123 $index = $bits[
'index'] - $indexOffset;
124 if ( isset( $namedArgs[$index] ) || isset( $numberedArgs[$index] ) ) {
125 $this->parser->getOutput()->addWarningMsg(
126 'duplicate-args-warning',
131 $this->parser->addTrackingCategory(
'duplicate-args-category' );
133 $numberedArgs[$index] = $bits[
'value'];
134 unset( $namedArgs[$index] );
138 if ( isset( $namedArgs[$name] ) || isset( $numberedArgs[$name] ) ) {
139 $this->parser->getOutput()->addWarningMsg(
140 'duplicate-args-warning',
145 $this->parser->addTrackingCategory(
'duplicate-args-category' );
147 $namedArgs[$name] = $bits[
'value'];
148 unset( $numberedArgs[$name] );
163 return $this->
expand( $root, $flags );
171 public function expand( $root, $flags = 0 ) {
172 static $expansionDepth = 0;
173 if ( is_string( $root ) ) {
177 if ( ++$this->parser->mPPNodeCount > $this->maxPPNodeCount ) {
178 $this->parser->limitationWarn(
'node-count-exceeded',
179 $this->parser->mPPNodeCount,
180 $this->maxPPNodeCount
182 return '<span class="error">Node-count limit exceeded</span>';
184 if ( $expansionDepth > $this->maxPPExpandDepth ) {
185 $this->parser->limitationWarn(
'expansion-depth-exceeded',
187 $this->maxPPExpandDepth
189 return '<span class="error">Expansion depth limit exceeded</span>';
192 if ( $expansionDepth > $this->parser->mHighestExpansionDepth ) {
193 $this->parser->mHighestExpansionDepth = $expansionDepth;
196 $outStack = [
'',
'' ];
197 $iteratorStack = [
false, $root ];
198 $indexStack = [ 0, 0 ];
200 while ( count( $iteratorStack ) > 1 ) {
201 $level = count( $outStack ) - 1;
202 $iteratorNode =& $iteratorStack[$level];
203 $out =& $outStack[$level];
204 $index =& $indexStack[$level];
206 if ( is_array( $iteratorNode ) ) {
207 if ( $index >= count( $iteratorNode ) ) {
209 $iteratorStack[$level] =
false;
210 $contextNode =
false;
212 $contextNode = $iteratorNode[$index];
216 if ( $index >= $iteratorNode->getLength() ) {
218 $iteratorStack[$level] =
false;
219 $contextNode =
false;
221 $contextNode = $iteratorNode->item( $index );
227 $contextNode = $iteratorStack[$level];
228 $iteratorStack[$level] =
false;
231 $newIterator =
false;
232 $contextName =
false;
233 $contextChildren =
false;
235 if ( $contextNode ===
false ) {
237 } elseif ( is_string( $contextNode ) ) {
238 $out .= $contextNode;
240 $newIterator = $contextNode;
244 $out .= $contextNode->value;
246 $contextName = $contextNode->name;
247 $contextChildren = $contextNode->getRawChildren();
248 } elseif ( is_array( $contextNode ) ) {
250 if ( count( $contextNode ) !== 2 ) {
251 throw new RuntimeException( __METHOD__ .
252 ': found an array where a node descriptor should be' );
254 [ $contextName, $contextChildren ] = $contextNode;
256 throw new RuntimeException( __METHOD__ .
': Invalid parameter type' );
260 if ( $contextName ===
false ) {
262 } elseif ( $contextName[0] ===
'@' ) {
264 } elseif ( $contextName ===
'template' ) {
265 # Double-brace expansion
274 $ret = $this->parser->braceSubstitution( $bits, $this );
275 if ( isset( $ret[
'object'] ) ) {
276 $newIterator = $ret[
'object'];
278 $out .= $ret[
'text'];
281 } elseif ( $contextName ===
'tplarg' ) {
282 # Triple-brace expansion
291 $ret = $this->parser->argSubstitution( $bits, $this );
292 if ( isset( $ret[
'object'] ) ) {
293 $newIterator = $ret[
'object'];
295 $out .= $ret[
'text'];
298 } elseif ( $contextName ===
'comment' ) {
300 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
301 # Not in RECOVER_COMMENTS mode (msgnw) though.
302 if ( ( $this->parser->ot[
'html']
303 || ( $this->parser->ot[
'pre'] && $this->parser->mOptions->getRemoveComments() )
309 # Add a strip marker in PST mode so that pstPass2() can
310 # run some old-fashioned regexes on the result.
311 # Not in RECOVER_COMMENTS mode (extractSections) though.
312 $out .= $this->parser->insertStripItem( $contextChildren[0] );
314 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
315 $out .= $contextChildren[0];
317 } elseif ( $contextName ===
'ignore' ) {
318 # Output suppression used by <includeonly> etc.
319 # OT_WIKI will only respect <ignore> in substed templates.
320 # The other output types respect it unless NO_IGNORE is set.
321 # extractSections() sets NO_IGNORE and so never respects it.
322 if ( ( !isset( $this->parent ) && $this->parser->ot[
'wiki'] )
325 $out .= $contextChildren[0];
329 } elseif ( $contextName ===
'ext' ) {
332 [
'attr' =>
null,
'inner' =>
null,
'close' => null ];
334 $s =
'<' . $bits[
'name']->getFirstChild()->value;
336 if ( $bits[
'attr'] ) {
337 $s .= $bits[
'attr']->getFirstChild()->value;
340 if ( $bits[
'inner'] ) {
341 $s .=
'>' . $bits[
'inner']->getFirstChild()->value;
343 if ( $bits[
'close'] ) {
344 $s .= $bits[
'close']->getFirstChild()->value;
351 $out .= $this->parser->extensionSubstitution( $bits, $this,
354 } elseif ( $contextName ===
'h' ) {
356 if ( $this->parser->ot[
'html'] ) {
357 # Expand immediately and insert heading index marker
358 $s = $this->
expand( $contextChildren, $flags );
360 $titleText = $this->title->getPrefixedDBkey();
361 $this->parser->mHeadings[] = [ $titleText, $bits[
'i'] ];
362 $serial = count( $this->parser->mHeadings ) - 1;
364 $s = substr( $s, 0, $bits[
'level'] ) . $marker . substr( $s, $bits[
'level'] );
365 $this->parser->getStripState()->addGeneral( $marker,
'' );
368 # Expand in virtual stack
369 $newIterator = $contextChildren;
372 # Generic recursive expansion
373 $newIterator = $contextChildren;
376 if ( $newIterator !==
false ) {
378 $iteratorStack[] = $newIterator;
380 } elseif ( $iteratorStack[$level] ===
false ) {
383 while ( $iteratorStack[$level] ===
false && $level > 0 ) {
384 $outStack[$level - 1] .= $out;
385 array_pop( $outStack );
386 array_pop( $iteratorStack );
387 array_pop( $indexStack );
405 foreach ( $args as $root ) {
407 $root = $root->value;
409 if ( !is_array( $root ) ) {
412 foreach ( $root as $node ) {
418 $s .= $this->
expand( $node, $flags );
434 foreach ( $args as $root ) {
436 $root = $root->value;
438 if ( !is_array( $root ) ) {
441 foreach ( $root as $node ) {
447 $s .= $this->
expand( $node );
465 foreach ( $args as $root ) {
467 $root = $root->value;
469 if ( !is_array( $root ) ) {
472 foreach ( $root as $node ) {
497 foreach ( $args as $root ) {
499 $root = $root->value;
501 if ( !is_array( $root ) ) {
504 foreach ( $root as $node ) {
526 if ( $level ===
false ) {
527 return $this->title->getPrefixedDBkey();
529 return $this->titleCache[$level] ??
false;
606 $this->
volatile = $flag;
615 return $this->volatile;
622 if ( $ttl !==
null && ( $this->ttl ===
null || $ttl < $this->ttl ) ) {
static plaintextParam( $plaintext)
An expansion frame, used as a context to expand the result of preprocessToObj()
isEmpty()
Returns true if there are no arguments in this frame.
int $depth
Recursion depth of this frame, top = 0 Note that this is NOT the same as expansion depth in expand()
loopCheck( $title)
Returns true if the infinite loop check is OK, false if a loop is detected.
setVolatile( $flag=true)
Set the volatile flag.
string false[] $titleCache
implodeWithFlags( $sep, $flags,... $args)
getTitle()
Get a title of frame.
Preprocessor $preprocessor
implode( $sep,... $args)
Implode with no flags specified This previously called implodeWithFlags but has now been inlined to r...
true[] $loopCheckHash
Hashtable listing templates which are disallowed for expansion in this frame, having been encountered...
cachedExpand( $key, $root, $flags=0)
__construct( $preprocessor)
array $childExpansionCache
virtualImplode( $sep,... $args)
Makes an object that, when expand()ed, will be the same as one obtained with implode()
virtualBracketedImplode( $start, $sep, $end,... $args)
Virtual implode with brackets.
isVolatile()
Get the volatile flag.
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...
isTemplate()
Return true if the frame is a template frame.
static splitRawTemplate(array $children)
Like splitTemplate() but for a raw child array.
static splitRawHeading(array $children)
Like splitHeading() but for a raw child array.
static splitRawExt(array $children)
Like splitExt() but for a raw child array.
Expansion frame with template arguments.