112 if (
$args !==
false ) {
115 } elseif ( !is_array(
$args ) ) {
116 throw new MWException( __METHOD__ .
': $args must be array or PPNode_Hash_Array' );
118 foreach (
$args as $arg ) {
119 $bits = $arg->splitArg();
120 if ( $bits[
'index'] !==
'' ) {
122 $index = $bits[
'index'] - $indexOffset;
123 if ( isset( $namedArgs[$index] ) || isset( $numberedArgs[$index] ) ) {
124 $this->parser->getOutput()->addWarning(
wfMessage(
'duplicate-args-warning',
128 $this->parser->addTrackingCategory(
'duplicate-args-category' );
130 $numberedArgs[$index] = $bits[
'value'];
131 unset( $namedArgs[$index] );
135 if ( isset( $namedArgs[$name] ) || isset( $numberedArgs[$name] ) ) {
136 $this->parser->getOutput()->addWarning(
wfMessage(
'duplicate-args-warning',
141 $this->parser->addTrackingCategory(
'duplicate-args-category' );
143 $namedArgs[$name] = $bits[
'value'];
144 unset( $numberedArgs[$name] );
170 public function expand( $root, $flags = 0 ) {
171 static $expansionDepth = 0;
172 if ( is_string( $root ) ) {
176 if ( ++$this->parser->mPPNodeCount > $this->maxPPNodeCount ) {
177 $this->parser->limitationWarn(
'node-count-exceeded',
178 $this->parser->mPPNodeCount,
179 $this->maxPPNodeCount
181 return '<span class="error">Node-count limit exceeded</span>';
183 if ( $expansionDepth > $this->maxPPExpandDepth ) {
184 $this->parser->limitationWarn(
'expansion-depth-exceeded',
186 $this->maxPPExpandDepth
188 return '<span class="error">Expansion depth limit exceeded</span>';
191 if ( $expansionDepth > $this->parser->mHighestExpansionDepth ) {
192 $this->parser->mHighestExpansionDepth = $expansionDepth;
195 $outStack = [
'',
'' ];
196 $iteratorStack = [
false, $root ];
197 $indexStack = [ 0, 0 ];
199 while ( count( $iteratorStack ) > 1 ) {
200 $level = count( $outStack ) - 1;
201 $iteratorNode =& $iteratorStack[$level];
202 $out =& $outStack[$level];
203 $index =& $indexStack[$level];
205 if ( is_array( $iteratorNode ) ) {
206 if ( $index >= count( $iteratorNode ) ) {
208 $iteratorStack[$level] =
false;
209 $contextNode =
false;
211 $contextNode = $iteratorNode[$index];
215 if ( $index >= $iteratorNode->getLength() ) {
217 $iteratorStack[$level] =
false;
218 $contextNode =
false;
220 $contextNode = $iteratorNode->item( $index );
226 $contextNode = $iteratorStack[$level];
227 $iteratorStack[$level] =
false;
230 $newIterator =
false;
231 $contextName =
false;
232 $contextChildren =
false;
234 if ( $contextNode ===
false ) {
236 } elseif ( is_string( $contextNode ) ) {
237 $out .= $contextNode;
239 $newIterator = $contextNode;
243 $out .= $contextNode->value;
245 $contextName = $contextNode->name;
246 $contextChildren = $contextNode->getRawChildren();
247 } elseif ( is_array( $contextNode ) ) {
249 if ( count( $contextNode ) !== 2 ) {
251 ': found an array where a node descriptor should be' );
253 list( $contextName, $contextChildren ) = $contextNode;
255 throw new MWException( __METHOD__ .
': Invalid parameter type' );
259 if ( $contextName ===
false ) {
261 } elseif ( $contextName[0] ===
'@' ) {
263 } elseif ( $contextName ===
'template' ) {
264 # Double-brace expansion
273 $ret = $this->parser->braceSubstitution( $bits, $this );
274 if ( isset( $ret[
'object'] ) ) {
275 $newIterator = $ret[
'object'];
277 $out .= $ret[
'text'];
280 } elseif ( $contextName ===
'tplarg' ) {
281 # Triple-brace expansion
290 $ret = $this->parser->argSubstitution( $bits, $this );
291 if ( isset( $ret[
'object'] ) ) {
292 $newIterator = $ret[
'object'];
294 $out .= $ret[
'text'];
297 } elseif ( $contextName ===
'comment' ) {
299 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
300 # Not in RECOVER_COMMENTS mode (msgnw) though.
301 if ( ( $this->parser->ot[
'html']
302 || ( $this->parser->ot[
'pre'] && $this->parser->mOptions->getRemoveComments() )
308 # Add a strip marker in PST mode so that pstPass2() can
309 # run some old-fashioned regexes on the result.
310 # Not in RECOVER_COMMENTS mode (extractSections) though.
311 $out .= $this->parser->insertStripItem( $contextChildren[0] );
313 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
314 $out .= $contextChildren[0];
316 } elseif ( $contextName ===
'ignore' ) {
317 # Output suppression used by <includeonly> etc.
318 # OT_WIKI will only respect <ignore> in substed templates.
319 # The other output types respect it unless NO_IGNORE is set.
320 # extractSections() sets NO_IGNORE and so never respects it.
321 if ( ( !isset( $this->parent ) && $this->parser->ot[
'wiki'] )
324 $out .= $contextChildren[0];
328 } elseif ( $contextName ===
'ext' ) {
331 [
'attr' =>
null,
'inner' =>
null,
'close' => null ];
333 $s =
'<' . $bits[
'name']->getFirstChild()->value;
335 if ( $bits[
'attr'] ) {
336 $s .= $bits[
'attr']->getFirstChild()->value;
339 if ( $bits[
'inner'] ) {
340 $s .=
'>' . $bits[
'inner']->getFirstChild()->value;
342 if ( $bits[
'close'] ) {
343 $s .= $bits[
'close']->getFirstChild()->value;
350 $out .= $this->parser->extensionSubstitution( $bits, $this );
352 } elseif ( $contextName ===
'h' ) {
354 if ( $this->parser->ot[
'html'] ) {
355 # Expand immediately and insert heading index marker
356 $s = $this->
expand( $contextChildren, $flags );
358 $titleText = $this->title->getPrefixedDBkey();
359 $this->parser->mHeadings[] = [ $titleText, $bits[
'i'] ];
360 $serial = count( $this->parser->mHeadings ) - 1;
362 $s = substr(
$s, 0, $bits[
'level'] ) . $marker . substr(
$s, $bits[
'level'] );
363 $this->parser->mStripState->addGeneral( $marker,
'' );
366 # Expand in virtual stack
367 $newIterator = $contextChildren;
370 # Generic recursive expansion
371 $newIterator = $contextChildren;
374 if ( $newIterator !==
false ) {
376 $iteratorStack[] = $newIterator;
378 } elseif ( $iteratorStack[$level] ===
false ) {
381 while ( $iteratorStack[$level] ===
false && $level > 0 ) {
382 $outStack[$level - 1] .= $out;
383 array_pop( $outStack );
384 array_pop( $iteratorStack );
385 array_pop( $indexStack );