119 if ( $args !==
false ) {
121 $args = $args->value;
122 } elseif ( !is_array( $args ) ) {
123 throw new InvalidArgumentException( __METHOD__ .
': $args must be array or PPNode_Hash_Array' );
125 foreach ( $args as $arg ) {
126 $bits = $arg->splitArg();
127 if ( $bits[
'index'] !==
'' ) {
129 $index = $bits[
'index'] - $indexOffset;
130 if ( isset( $namedArgs[$index] ) || isset( $numberedArgs[$index] ) ) {
131 $this->parser->getOutput()->addWarningMsg(
132 'duplicate-args-warning',
137 $this->parser->addTrackingCategory(
'duplicate-args-category' );
139 $numberedArgs[$index] = $bits[
'value'];
140 unset( $namedArgs[$index] );
144 if ( isset( $namedArgs[$name] ) || isset( $numberedArgs[$name] ) ) {
145 $this->parser->getOutput()->addWarningMsg(
146 'duplicate-args-warning',
151 $this->parser->addTrackingCategory(
'duplicate-args-category' );
153 $namedArgs[$name] = $bits[
'value'];
154 unset( $numberedArgs[$name] );
177 public function expand( $root, $flags = 0 ) {
178 static $expansionDepth = 0;
179 if ( is_string( $root ) ) {
183 if ( ++$this->parser->mPPNodeCount > $this->maxPPNodeCount ) {
184 $this->parser->limitationWarn(
'node-count-exceeded',
185 $this->parser->mPPNodeCount,
186 $this->maxPPNodeCount
188 return '<span class="error">Node-count limit exceeded</span>';
190 if ( $expansionDepth > $this->maxPPExpandDepth ) {
191 $this->parser->limitationWarn(
'expansion-depth-exceeded',
193 $this->maxPPExpandDepth
195 return '<span class="error">Expansion depth limit exceeded</span>';
198 if ( $expansionDepth > $this->parser->mHighestExpansionDepth ) {
199 $this->parser->mHighestExpansionDepth = $expansionDepth;
202 $outStack = [
'',
'' ];
203 $iteratorStack = [
false, $root ];
204 $indexStack = [ 0, 0 ];
206 while ( count( $iteratorStack ) > 1 ) {
207 $level = count( $outStack ) - 1;
208 $iteratorNode =& $iteratorStack[$level];
209 $out =& $outStack[$level];
210 $index =& $indexStack[$level];
212 if ( is_array( $iteratorNode ) ) {
213 if ( $index >= count( $iteratorNode ) ) {
215 $iteratorStack[$level] =
false;
216 $contextNode =
false;
218 $contextNode = $iteratorNode[$index];
222 if ( $index >= $iteratorNode->getLength() ) {
224 $iteratorStack[$level] =
false;
225 $contextNode =
false;
227 $contextNode = $iteratorNode->item( $index );
233 $contextNode = $iteratorStack[$level];
234 $iteratorStack[$level] =
false;
237 $newIterator =
false;
238 $contextName =
false;
239 $contextChildren =
false;
241 if ( $contextNode ===
false ) {
243 } elseif ( is_string( $contextNode ) ) {
244 $out .= $contextNode;
246 $newIterator = $contextNode;
250 $out .= $contextNode->value;
252 $contextName = $contextNode->name;
253 $contextChildren = $contextNode->getRawChildren();
254 } elseif ( is_array( $contextNode ) ) {
256 if ( count( $contextNode ) !== 2 ) {
257 throw new RuntimeException( __METHOD__ .
258 ': found an array where a node descriptor should be' );
260 [ $contextName, $contextChildren ] = $contextNode;
262 throw new RuntimeException( __METHOD__ .
': Invalid parameter type' );
266 if ( $contextName ===
false ) {
268 } elseif ( $contextName[0] ===
'@' ) {
270 } elseif ( $contextName ===
'template' ) {
271 # Double-brace expansion
280 $ret = $this->parser->braceSubstitution( $bits, $this );
281 if ( isset( $ret[
'object'] ) ) {
282 $newIterator = $ret[
'object'];
284 $out .= $ret[
'text'];
287 } elseif ( $contextName ===
'tplarg' ) {
288 # Triple-brace expansion
297 $ret = $this->parser->argSubstitution( $bits, $this );
298 if ( isset( $ret[
'object'] ) ) {
299 $newIterator = $ret[
'object'];
301 $out .= $ret[
'text'];
304 } elseif ( $contextName ===
'comment' ) {
306 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
307 # Not in RECOVER_COMMENTS mode (msgnw) though.
310 $this->parser->getOptions()->getRemoveComments() )
319 # Add a strip marker in PST mode so that pstPass2() can
320 # run some old-fashioned regexes on the result.
321 # Not in RECOVER_COMMENTS mode (extractSections) though.
322 $out .= $this->parser->insertStripItem( $contextChildren[0] );
324 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
325 $out .= $contextChildren[0];
327 } elseif ( $contextName ===
'ignore' ) {
328 # Output suppression used by <includeonly> etc.
329 # OT_WIKI will only respect <ignore> in substed templates.
330 # The other output types respect it unless NO_IGNORE is set.
331 # extractSections() sets NO_IGNORE and so never respects it.
332 if ( ( !isset( $this->parent ) && $this->parser->getOutputType() ===
Parser::OT_WIKI )
335 $out .= $contextChildren[0];
339 } elseif ( $contextName ===
'ext' ) {
342 [
'attr' =>
null,
'inner' =>
null,
'close' => null ];
344 $s =
'<' . $bits[
'name']->getFirstChild()->value;
346 if ( $bits[
'attr'] ) {
347 $s .= $bits[
'attr']->getFirstChild()->value;
350 if ( $bits[
'inner'] ) {
351 $s .=
'>' . $bits[
'inner']->getFirstChild()->value;
353 if ( $bits[
'close'] ) {
354 $s .= $bits[
'close']->getFirstChild()->value;
361 $out .= $this->parser->extensionSubstitution( $bits, $this,
364 } elseif ( $contextName ===
'h' ) {
367 # Expand immediately and insert heading index marker
368 $s = $this->
expand( $contextChildren, $flags );
370 $titleText = $this->title->getPrefixedDBkey();
371 $this->parser->mHeadings[] = [ $titleText, $bits[
'i'] ];
372 $serial = count( $this->parser->mHeadings ) - 1;
374 $s = substr( $s, 0, $bits[
'level'] ) . $marker . substr( $s, $bits[
'level'] );
375 $this->parser->getStripState()->addGeneral( $marker,
'' );
378 # Expand in virtual stack
379 $newIterator = $contextChildren;
382 # Generic recursive expansion
383 $newIterator = $contextChildren;
386 if ( $newIterator !==
false ) {
388 $iteratorStack[] = $newIterator;
390 } elseif ( $iteratorStack[$level] ===
false ) {
393 while ( $iteratorStack[$level] ===
false && $level > 0 ) {
394 $outStack[$level - 1] .= $out;
395 array_pop( $outStack );
396 array_pop( $iteratorStack );
397 array_pop( $indexStack );