114 if ( $args !==
false ) {
116 $args = $args->value;
117 } elseif ( !is_array( $args ) ) {
118 throw new MWException( __METHOD__ .
': $args must be array or PPNode_Hash_Array' );
120 foreach ( $args as $arg ) {
121 $bits = $arg->splitArg();
122 if ( $bits[
'index'] !==
'' ) {
124 $index = $bits[
'index'] - $indexOffset;
125 if ( isset( $namedArgs[$index] ) || isset( $numberedArgs[$index] ) ) {
126 $this->parser->getOutput()->addWarningMsg(
127 'duplicate-args-warning',
132 $this->parser->addTrackingCategory(
'duplicate-args-category' );
134 $numberedArgs[$index] = $bits[
'value'];
135 unset( $namedArgs[$index] );
139 if ( isset( $namedArgs[$name] ) || isset( $numberedArgs[$name] ) ) {
140 $this->parser->getOutput()->addWarningMsg(
141 'duplicate-args-warning',
146 $this->parser->addTrackingCategory(
'duplicate-args-category' );
148 $namedArgs[$name] = $bits[
'value'];
149 unset( $numberedArgs[$name] );
174 public function expand( $root, $flags = 0 ) {
175 static $expansionDepth = 0;
176 if ( is_string( $root ) ) {
180 if ( ++$this->parser->mPPNodeCount > $this->maxPPNodeCount ) {
181 $this->parser->limitationWarn(
'node-count-exceeded',
182 $this->parser->mPPNodeCount,
183 $this->maxPPNodeCount
185 return '<span class="error">Node-count limit exceeded</span>';
187 if ( $expansionDepth > $this->maxPPExpandDepth ) {
188 $this->parser->limitationWarn(
'expansion-depth-exceeded',
190 $this->maxPPExpandDepth
192 return '<span class="error">Expansion depth limit exceeded</span>';
195 if ( $expansionDepth > $this->parser->mHighestExpansionDepth ) {
196 $this->parser->mHighestExpansionDepth = $expansionDepth;
199 $outStack = [
'',
'' ];
200 $iteratorStack = [
false, $root ];
201 $indexStack = [ 0, 0 ];
203 while ( count( $iteratorStack ) > 1 ) {
204 $level = count( $outStack ) - 1;
205 $iteratorNode =& $iteratorStack[$level];
206 $out =& $outStack[$level];
207 $index =& $indexStack[$level];
209 if ( is_array( $iteratorNode ) ) {
210 if ( $index >= count( $iteratorNode ) ) {
212 $iteratorStack[$level] =
false;
213 $contextNode =
false;
215 $contextNode = $iteratorNode[$index];
219 if ( $index >= $iteratorNode->getLength() ) {
221 $iteratorStack[$level] =
false;
222 $contextNode =
false;
224 $contextNode = $iteratorNode->item( $index );
230 $contextNode = $iteratorStack[$level];
231 $iteratorStack[$level] =
false;
234 $newIterator =
false;
235 $contextName =
false;
236 $contextChildren =
false;
238 if ( $contextNode ===
false ) {
240 } elseif ( is_string( $contextNode ) ) {
241 $out .= $contextNode;
243 $newIterator = $contextNode;
247 $out .= $contextNode->value;
249 $contextName = $contextNode->name;
250 $contextChildren = $contextNode->getRawChildren();
251 } elseif ( is_array( $contextNode ) ) {
253 if ( count( $contextNode ) !== 2 ) {
255 ': found an array where a node descriptor should be' );
257 [ $contextName, $contextChildren ] = $contextNode;
259 throw new MWException( __METHOD__ .
': Invalid parameter type' );
263 if ( $contextName ===
false ) {
265 } elseif ( $contextName[0] ===
'@' ) {
267 } elseif ( $contextName ===
'template' ) {
268 # Double-brace expansion
277 $ret = $this->parser->braceSubstitution( $bits, $this );
278 if ( isset( $ret[
'object'] ) ) {
279 $newIterator = $ret[
'object'];
281 $out .= $ret[
'text'];
284 } elseif ( $contextName ===
'tplarg' ) {
285 # Triple-brace expansion
294 $ret = $this->parser->argSubstitution( $bits, $this );
295 if ( isset( $ret[
'object'] ) ) {
296 $newIterator = $ret[
'object'];
298 $out .= $ret[
'text'];
301 } elseif ( $contextName ===
'comment' ) {
303 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
304 # Not in RECOVER_COMMENTS mode (msgnw) though.
305 if ( ( $this->parser->ot[
'html']
306 || ( $this->parser->ot[
'pre'] && $this->parser->mOptions->getRemoveComments() )
312 # Add a strip marker in PST mode so that pstPass2() can
313 # run some old-fashioned regexes on the result.
314 # Not in RECOVER_COMMENTS mode (extractSections) though.
315 $out .= $this->parser->insertStripItem( $contextChildren[0] );
317 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
318 $out .= $contextChildren[0];
320 } elseif ( $contextName ===
'ignore' ) {
321 # Output suppression used by <includeonly> etc.
322 # OT_WIKI will only respect <ignore> in substed templates.
323 # The other output types respect it unless NO_IGNORE is set.
324 # extractSections() sets NO_IGNORE and so never respects it.
325 if ( ( !isset( $this->parent ) && $this->parser->ot[
'wiki'] )
328 $out .= $contextChildren[0];
332 } elseif ( $contextName ===
'ext' ) {
335 [
'attr' =>
null,
'inner' =>
null,
'close' => null ];
337 $s =
'<' . $bits[
'name']->getFirstChild()->value;
339 if ( $bits[
'attr'] ) {
340 $s .= $bits[
'attr']->getFirstChild()->value;
343 if ( $bits[
'inner'] ) {
344 $s .=
'>' . $bits[
'inner']->getFirstChild()->value;
346 if ( $bits[
'close'] ) {
347 $s .= $bits[
'close']->getFirstChild()->value;
354 $out .= $this->parser->extensionSubstitution( $bits, $this,
357 } elseif ( $contextName ===
'h' ) {
359 if ( $this->parser->ot[
'html'] ) {
360 # Expand immediately and insert heading index marker
361 $s = $this->
expand( $contextChildren, $flags );
363 $titleText = $this->title->getPrefixedDBkey();
364 $this->parser->mHeadings[] = [ $titleText, $bits[
'i'] ];
365 $serial = count( $this->parser->mHeadings ) - 1;
367 $s = substr( $s, 0, $bits[
'level'] ) . $marker . substr( $s, $bits[
'level'] );
368 $this->parser->getStripState()->addGeneral( $marker,
'' );
371 # Expand in virtual stack
372 $newIterator = $contextChildren;
375 # Generic recursive expansion
376 $newIterator = $contextChildren;
379 if ( $newIterator !==
false ) {
381 $iteratorStack[] = $newIterator;
383 } elseif ( $iteratorStack[$level] ===
false ) {
386 while ( $iteratorStack[$level] ===
false && $level > 0 ) {
387 $outStack[$level - 1] .= $out;
388 array_pop( $outStack );
389 array_pop( $iteratorStack );
390 array_pop( $indexStack );