99 public function newChild( $args =
false,
$title =
false, $indexOffset = 0 ) {
105 if ( $args !==
false ) {
107 $args = $args->value;
108 } elseif ( !is_array( $args ) ) {
109 throw new InvalidArgumentException( __METHOD__ .
': $args must be array or PPNode_Hash_Array' );
111 foreach ( $args as $arg ) {
112 $bits = $arg->splitArg();
113 if ( $bits[
'index'] !==
'' ) {
115 $index = $bits[
'index'] - $indexOffset;
116 if ( isset( $namedArgs[$index] ) || isset( $numberedArgs[$index] ) ) {
117 $this->parser->getOutput()->addWarningMsg(
118 'duplicate-args-warning',
123 $this->parser->addTrackingCategory(
'duplicate-args-category' );
125 $numberedArgs[$index] = $bits[
'value'];
126 unset( $namedArgs[$index] );
130 if ( isset( $namedArgs[$name] ) || isset( $numberedArgs[$name] ) ) {
131 $this->parser->getOutput()->addWarningMsg(
132 'duplicate-args-warning',
137 $this->parser->addTrackingCategory(
'duplicate-args-category' );
139 $namedArgs[$name] = $bits[
'value'];
140 unset( $numberedArgs[$name] );
163 public function expand( $root, $flags = 0 ) {
164 static $expansionDepth = 0;
165 if ( is_string( $root ) ) {
169 if ( ++$this->parser->mPPNodeCount > $this->maxPPNodeCount ) {
170 $this->parser->limitationWarn(
'node-count-exceeded',
171 $this->parser->mPPNodeCount,
172 $this->maxPPNodeCount
174 return '<span class="error">Node-count limit exceeded</span>';
176 if ( $expansionDepth > $this->maxPPExpandDepth ) {
177 $this->parser->limitationWarn(
'expansion-depth-exceeded',
179 $this->maxPPExpandDepth
181 return '<span class="error">Expansion depth limit exceeded</span>';
184 if ( $expansionDepth > $this->parser->mHighestExpansionDepth ) {
185 $this->parser->mHighestExpansionDepth = $expansionDepth;
188 $outStack = [
'',
'' ];
189 $iteratorStack = [
false, $root ];
190 $indexStack = [ 0, 0 ];
192 while ( count( $iteratorStack ) > 1 ) {
193 $level = count( $outStack ) - 1;
194 $iteratorNode =& $iteratorStack[$level];
195 $out =& $outStack[$level];
196 $index =& $indexStack[$level];
198 if ( is_array( $iteratorNode ) ) {
199 if ( $index >= count( $iteratorNode ) ) {
201 $iteratorStack[$level] =
false;
202 $contextNode =
false;
204 $contextNode = $iteratorNode[$index];
208 if ( $index >= $iteratorNode->getLength() ) {
210 $iteratorStack[$level] =
false;
211 $contextNode =
false;
213 $contextNode = $iteratorNode->item( $index );
219 $contextNode = $iteratorStack[$level];
220 $iteratorStack[$level] =
false;
223 $newIterator =
false;
224 $contextName =
false;
225 $contextChildren =
false;
227 if ( $contextNode ===
false ) {
229 } elseif ( is_string( $contextNode ) ) {
230 $out .= $contextNode;
232 $newIterator = $contextNode;
236 $out .= $contextNode->value;
238 $contextName = $contextNode->name;
239 $contextChildren = $contextNode->getRawChildren();
240 } elseif ( is_array( $contextNode ) ) {
242 if ( count( $contextNode ) !== 2 ) {
243 throw new RuntimeException( __METHOD__ .
244 ': found an array where a node descriptor should be' );
246 [ $contextName, $contextChildren ] = $contextNode;
248 throw new RuntimeException( __METHOD__ .
': Invalid parameter type' );
252 if ( $contextName ===
false ) {
254 } elseif ( $contextName[0] ===
'@' ) {
256 } elseif ( $contextName ===
'template' ) {
257 # Double-brace expansion
266 $ret = $this->parser->braceSubstitution( $bits, $this );
267 if ( isset( $ret[
'object'] ) ) {
268 $newIterator = $ret[
'object'];
270 $out .= $ret[
'text'];
273 } elseif ( $contextName ===
'tplarg' ) {
274 # Triple-brace expansion
283 $ret = $this->parser->argSubstitution( $bits, $this );
284 if ( isset( $ret[
'object'] ) ) {
285 $newIterator = $ret[
'object'];
287 $out .= $ret[
'text'];
290 } elseif ( $contextName ===
'comment' ) {
292 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
293 # Not in RECOVER_COMMENTS mode (msgnw) though.
296 $this->parser->getOptions()->getRemoveComments() )
305 # Add a strip marker in PST mode so that pstPass2() can
306 # run some old-fashioned regexes on the result.
307 # Not in RECOVER_COMMENTS mode (extractSections) though.
308 $out .= $this->parser->insertStripItem( $contextChildren[0] );
310 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
311 $out .= $contextChildren[0];
313 } elseif ( $contextName ===
'ignore' ) {
314 # Output suppression used by <includeonly> etc.
315 # OT_WIKI will only respect <ignore> in substed templates.
316 # The other output types respect it unless NO_IGNORE is set.
317 # extractSections() sets NO_IGNORE and so never respects it.
318 if ( ( !isset( $this->parent ) && $this->parser->getOutputType() ===
Parser::OT_WIKI )
321 $out .= $contextChildren[0];
325 } elseif ( $contextName ===
'ext' ) {
328 [
'attr' =>
null,
'inner' =>
null,
'close' => null ];
330 $s =
'<' . $bits[
'name']->getFirstChild()->value;
331 if ( $bits[
'attr'] ) {
332 $s .= $bits[
'attr']->getFirstChild()->value;
334 if ( $bits[
'inner'] ) {
335 $s .=
'>' . $bits[
'inner']->getFirstChild()->value;
336 if ( $bits[
'close'] ) {
337 $s .= $bits[
'close']->getFirstChild()->value;
344 $out .= $this->parser->extensionSubstitution( $bits, $this,
347 } elseif ( $contextName ===
'h' ) {
350 # Expand immediately and insert heading index marker
351 $s = $this->
expand( $contextChildren, $flags );
353 $titleText = $this->title->getPrefixedDBkey();
354 $this->parser->mHeadings[] = [ $titleText, $bits[
'i'] ];
355 $serial = count( $this->parser->mHeadings ) - 1;
357 $s = substr( $s, 0, $bits[
'level'] ) . $marker . substr( $s, $bits[
'level'] );
358 $this->parser->getStripState()->addGeneral( $marker,
'' );
361 # Expand in virtual stack
362 $newIterator = $contextChildren;
365 # Generic recursive expansion
366 $newIterator = $contextChildren;
369 if ( $newIterator !==
false ) {
371 $iteratorStack[] = $newIterator;
373 } elseif ( $iteratorStack[$level] ===
false ) {
376 while ( $iteratorStack[$level] ===
false && $level > 0 ) {
377 $outStack[$level - 1] .= $out;
378 array_pop( $outStack );
379 array_pop( $iteratorStack );
380 array_pop( $indexStack );