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()->addWarningMsg(
125 'duplicate-args-warning',
130 $this->parser->addTrackingCategory(
'duplicate-args-category' );
132 $numberedArgs[$index] = $bits[
'value'];
133 unset( $namedArgs[$index] );
137 if ( isset( $namedArgs[$name] ) || isset( $numberedArgs[$name] ) ) {
138 $this->parser->getOutput()->addWarningMsg(
139 'duplicate-args-warning',
144 $this->parser->addTrackingCategory(
'duplicate-args-category' );
146 $namedArgs[$name] = $bits[
'value'];
147 unset( $numberedArgs[$name] );
172 public function expand( $root, $flags = 0 ) {
173 static $expansionDepth = 0;
174 if ( is_string( $root ) ) {
178 if ( ++$this->parser->mPPNodeCount > $this->maxPPNodeCount ) {
179 $this->parser->limitationWarn(
'node-count-exceeded',
180 $this->parser->mPPNodeCount,
181 $this->maxPPNodeCount
183 return '<span class="error">Node-count limit exceeded</span>';
185 if ( $expansionDepth > $this->maxPPExpandDepth ) {
186 $this->parser->limitationWarn(
'expansion-depth-exceeded',
188 $this->maxPPExpandDepth
190 return '<span class="error">Expansion depth limit exceeded</span>';
193 if ( $expansionDepth > $this->parser->mHighestExpansionDepth ) {
194 $this->parser->mHighestExpansionDepth = $expansionDepth;
197 $outStack = [
'',
'' ];
198 $iteratorStack = [
false, $root ];
199 $indexStack = [ 0, 0 ];
201 while ( count( $iteratorStack ) > 1 ) {
202 $level = count( $outStack ) - 1;
203 $iteratorNode =& $iteratorStack[$level];
204 $out =& $outStack[$level];
205 $index =& $indexStack[$level];
207 if ( is_array( $iteratorNode ) ) {
208 if ( $index >= count( $iteratorNode ) ) {
210 $iteratorStack[$level] =
false;
211 $contextNode =
false;
213 $contextNode = $iteratorNode[$index];
217 if ( $index >= $iteratorNode->getLength() ) {
219 $iteratorStack[$level] =
false;
220 $contextNode =
false;
222 $contextNode = $iteratorNode->item( $index );
228 $contextNode = $iteratorStack[$level];
229 $iteratorStack[$level] =
false;
232 $newIterator =
false;
233 $contextName =
false;
234 $contextChildren =
false;
236 if ( $contextNode ===
false ) {
238 } elseif ( is_string( $contextNode ) ) {
239 $out .= $contextNode;
241 $newIterator = $contextNode;
245 $out .= $contextNode->value;
247 $contextName = $contextNode->name;
248 $contextChildren = $contextNode->getRawChildren();
249 } elseif ( is_array( $contextNode ) ) {
251 if ( count( $contextNode ) !== 2 ) {
253 ': found an array where a node descriptor should be' );
255 list( $contextName, $contextChildren ) = $contextNode;
257 throw new MWException( __METHOD__ .
': Invalid parameter type' );
261 if ( $contextName ===
false ) {
263 } elseif ( $contextName[0] ===
'@' ) {
265 } elseif ( $contextName ===
'template' ) {
266 # Double-brace expansion
275 $ret = $this->parser->braceSubstitution( $bits, $this );
276 if ( isset( $ret[
'object'] ) ) {
277 $newIterator = $ret[
'object'];
279 $out .= $ret[
'text'];
282 } elseif ( $contextName ===
'tplarg' ) {
283 # Triple-brace expansion
292 $ret = $this->parser->argSubstitution( $bits, $this );
293 if ( isset( $ret[
'object'] ) ) {
294 $newIterator = $ret[
'object'];
296 $out .= $ret[
'text'];
299 } elseif ( $contextName ===
'comment' ) {
301 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
302 # Not in RECOVER_COMMENTS mode (msgnw) though.
303 if ( ( $this->parser->ot[
'html']
304 || ( $this->parser->ot[
'pre'] && $this->parser->mOptions->getRemoveComments() )
310 # Add a strip marker in PST mode so that pstPass2() can
311 # run some old-fashioned regexes on the result.
312 # Not in RECOVER_COMMENTS mode (extractSections) though.
313 $out .= $this->parser->insertStripItem( $contextChildren[0] );
315 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
316 $out .= $contextChildren[0];
318 } elseif ( $contextName ===
'ignore' ) {
319 # Output suppression used by <includeonly> etc.
320 # OT_WIKI will only respect <ignore> in substed templates.
321 # The other output types respect it unless NO_IGNORE is set.
322 # extractSections() sets NO_IGNORE and so never respects it.
323 if ( ( !isset( $this->parent ) && $this->parser->ot[
'wiki'] )
326 $out .= $contextChildren[0];
330 } elseif ( $contextName ===
'ext' ) {
333 [
'attr' =>
null,
'inner' =>
null,
'close' => null ];
335 $s =
'<' . $bits[
'name']->getFirstChild()->value;
337 if ( $bits[
'attr'] ) {
338 $s .= $bits[
'attr']->getFirstChild()->value;
341 if ( $bits[
'inner'] ) {
342 $s .=
'>' . $bits[
'inner']->getFirstChild()->value;
344 if ( $bits[
'close'] ) {
345 $s .= $bits[
'close']->getFirstChild()->value;
352 $out .= $this->parser->extensionSubstitution( $bits, $this,
355 } elseif ( $contextName ===
'h' ) {
357 if ( $this->parser->ot[
'html'] ) {
358 # Expand immediately and insert heading index marker
359 $s = $this->
expand( $contextChildren, $flags );
361 $titleText = $this->title->getPrefixedDBkey();
362 $this->parser->mHeadings[] = [ $titleText, $bits[
'i'] ];
363 $serial = count( $this->parser->mHeadings ) - 1;
365 $s = substr(
$s, 0, $bits[
'level'] ) . $marker . substr(
$s, $bits[
'level'] );
366 $this->parser->getStripState()->addGeneral( $marker,
'' );
369 # Expand in virtual stack
370 $newIterator = $contextChildren;
373 # Generic recursive expansion
374 $newIterator = $contextChildren;
377 if ( $newIterator !==
false ) {
379 $iteratorStack[] = $newIterator;
381 } elseif ( $iteratorStack[$level] ===
false ) {
384 while ( $iteratorStack[$level] ===
false && $level > 0 ) {
385 $outStack[$level - 1] .= $out;
386 array_pop( $outStack );
387 array_pop( $iteratorStack );
388 array_pop( $indexStack );