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',
138 $this->parser->addTrackingCategory(
'duplicate-args-category' );
140 $namedArgs[$name] = $bits[
'value'];
141 unset( $numberedArgs[$name] );
164 public function expand( $root, $flags = 0 ) {
165 static $expansionDepth = 0;
166 if ( is_string( $root ) ) {
170 if ( ++$this->parser->mPPNodeCount > $this->maxPPNodeCount ) {
171 $this->parser->limitationWarn(
'node-count-exceeded',
172 $this->parser->mPPNodeCount,
173 $this->maxPPNodeCount
175 return '<span class="error">Node-count limit exceeded</span>';
177 if ( $expansionDepth > $this->maxPPExpandDepth ) {
178 $this->parser->limitationWarn(
'expansion-depth-exceeded',
180 $this->maxPPExpandDepth
182 return '<span class="error">Expansion depth limit exceeded</span>';
185 if ( $expansionDepth > $this->parser->mHighestExpansionDepth ) {
186 $this->parser->mHighestExpansionDepth = $expansionDepth;
189 $outStack = [
'',
'' ];
190 $iteratorStack = [
false, $root ];
191 $indexStack = [ 0, 0 ];
193 while ( count( $iteratorStack ) > 1 ) {
194 $level = count( $outStack ) - 1;
195 $iteratorNode =& $iteratorStack[$level];
196 $out =& $outStack[$level];
197 $index =& $indexStack[$level];
199 if ( is_array( $iteratorNode ) ) {
200 if ( $index >= count( $iteratorNode ) ) {
202 $iteratorStack[$level] =
false;
203 $contextNode =
false;
205 $contextNode = $iteratorNode[$index];
209 if ( $index >= $iteratorNode->getLength() ) {
211 $iteratorStack[$level] =
false;
212 $contextNode =
false;
214 $contextNode = $iteratorNode->item( $index );
220 $contextNode = $iteratorStack[$level];
221 $iteratorStack[$level] =
false;
224 $newIterator =
false;
225 $contextName =
false;
226 $contextChildren =
false;
228 if ( $contextNode ===
false ) {
230 } elseif ( is_string( $contextNode ) ) {
231 $out .= $contextNode;
233 $newIterator = $contextNode;
237 $out .= $contextNode->value;
239 $contextName = $contextNode->name;
240 $contextChildren = $contextNode->getRawChildren();
241 } elseif ( is_array( $contextNode ) ) {
243 if ( count( $contextNode ) !== 2 ) {
244 throw new RuntimeException( __METHOD__ .
245 ': found an array where a node descriptor should be' );
247 [ $contextName, $contextChildren ] = $contextNode;
249 throw new RuntimeException( __METHOD__ .
': Invalid parameter type' );
253 if ( $contextName ===
false ) {
255 } elseif ( $contextName[0] ===
'@' ) {
257 } elseif ( $contextName ===
'template' ) {
258 # Double-brace expansion
267 $ret = $this->parser->braceSubstitution( $bits, $this );
268 if ( isset( $ret[
'object'] ) ) {
269 $newIterator = $ret[
'object'];
271 $out .= $ret[
'text'];
274 } elseif ( $contextName ===
'tplarg' ) {
275 # Triple-brace expansion
284 $ret = $this->parser->argSubstitution( $bits, $this );
285 if ( isset( $ret[
'object'] ) ) {
286 $newIterator = $ret[
'object'];
288 $out .= $ret[
'text'];
291 } elseif ( $contextName ===
'comment' ) {
293 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
294 # Not in RECOVER_COMMENTS mode (msgnw) though.
297 $this->parser->getOptions()->getRemoveComments() )
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] );
311 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
312 $out .= $contextChildren[0];
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 )
322 $out .= $contextChildren[0];
326 } elseif ( $contextName ===
'ext' ) {
329 [
'attr' =>
null,
'inner' =>
null,
'close' => null ];
331 $s =
'<' . $bits[
'name']->getFirstChild()->value;
332 if ( $bits[
'attr'] ) {
333 $s .= $bits[
'attr']->getFirstChild()->value;
335 if ( $bits[
'inner'] ) {
336 $s .=
'>' . $bits[
'inner']->getFirstChild()->value;
337 if ( $bits[
'close'] ) {
338 $s .= $bits[
'close']->getFirstChild()->value;
345 $out .= $this->parser->extensionSubstitution( $bits, $this,
348 } elseif ( $contextName ===
'h' ) {
351 # Expand immediately and insert heading index marker
352 $s = $this->
expand( $contextChildren, $flags );
354 $titleText = $this->title->getPrefixedDBkey();
355 $this->parser->mHeadings[] = [ $titleText, $bits[
'i'] ];
356 $serial = count( $this->parser->mHeadings ) - 1;
358 $s = substr( $s, 0, $bits[
'level'] ) . $marker . substr( $s, $bits[
'level'] );
359 $this->parser->getStripState()->addGeneral( $marker,
'' );
362 # Expand in virtual stack
363 $newIterator = $contextChildren;
366 # Generic recursive expansion
367 $newIterator = $contextChildren;
370 if ( $newIterator !==
false ) {
372 $iteratorStack[] = $newIterator;
374 } elseif ( $iteratorStack[$level] ===
false ) {
377 while ( $iteratorStack[$level] ===
false && $level > 0 ) {
378 $outStack[$level - 1] .= $out;
379 array_pop( $outStack );
380 array_pop( $iteratorStack );
381 array_pop( $indexStack );