107 if ( $args !==
false ) {
109 $args = $args->value;
110 } elseif ( !is_array( $args ) ) {
111 throw new InvalidArgumentException( __METHOD__ .
': $args must be array or PPNode_Hash_Array' );
113 foreach ( $args as $arg ) {
114 $bits = $arg->splitArg();
115 if ( $bits[
'index'] !==
'' ) {
117 $index = $bits[
'index'] - $indexOffset;
118 if ( isset( $namedArgs[$index] ) || isset( $numberedArgs[$index] ) ) {
119 $this->parser->getOutput()->addWarningMsg(
120 'duplicate-args-warning',
125 $this->parser->addTrackingCategory(
'duplicate-args-category' );
127 $numberedArgs[$index] = $bits[
'value'];
128 unset( $namedArgs[$index] );
132 if ( isset( $namedArgs[$name] ) || isset( $numberedArgs[$name] ) ) {
133 $this->parser->getOutput()->addWarningMsg(
134 'duplicate-args-warning',
140 $this->parser->addTrackingCategory(
'duplicate-args-category' );
142 $namedArgs[$name] = $bits[
'value'];
143 unset( $numberedArgs[$name] );
166 public function expand( $root, $flags = 0 ) {
167 static $expansionDepth = 0;
168 if ( is_string( $root ) ) {
172 if ( ++$this->parser->mPPNodeCount > $this->maxPPNodeCount ) {
173 $this->parser->limitationWarn(
'node-count-exceeded',
174 $this->parser->mPPNodeCount,
175 $this->maxPPNodeCount
177 return '<span class="error">Node-count limit exceeded</span>';
179 if ( $expansionDepth > $this->maxPPExpandDepth ) {
180 $this->parser->limitationWarn(
'expansion-depth-exceeded',
182 $this->maxPPExpandDepth
184 return '<span class="error">Expansion depth limit exceeded</span>';
187 if ( $expansionDepth > $this->parser->mHighestExpansionDepth ) {
188 $this->parser->mHighestExpansionDepth = $expansionDepth;
191 $outStack = [
'',
'' ];
192 $iteratorStack = [
false, $root ];
193 $indexStack = [ 0, 0 ];
195 while ( count( $iteratorStack ) > 1 ) {
196 $level = count( $outStack ) - 1;
197 $iteratorNode =& $iteratorStack[$level];
198 $out =& $outStack[$level];
199 $index =& $indexStack[$level];
201 if ( is_array( $iteratorNode ) ) {
202 if ( $index >= count( $iteratorNode ) ) {
204 $iteratorStack[$level] =
false;
205 $contextNode =
false;
207 $contextNode = $iteratorNode[$index];
211 if ( $index >= $iteratorNode->getLength() ) {
213 $iteratorStack[$level] =
false;
214 $contextNode =
false;
216 $contextNode = $iteratorNode->item( $index );
222 $contextNode = $iteratorStack[$level];
223 $iteratorStack[$level] =
false;
226 $newIterator =
false;
227 $contextName =
false;
228 $contextChildren =
false;
230 if ( $contextNode ===
false ) {
232 } elseif ( is_string( $contextNode ) ) {
233 $out .= $contextNode;
235 $newIterator = $contextNode;
239 $out .= $contextNode->value;
241 $contextName = $contextNode->name;
242 $contextChildren = $contextNode->getRawChildren();
243 } elseif ( is_array( $contextNode ) ) {
245 if ( count( $contextNode ) !== 2 ) {
246 throw new RuntimeException( __METHOD__ .
247 ': found an array where a node descriptor should be' );
249 [ $contextName, $contextChildren ] = $contextNode;
251 throw new RuntimeException( __METHOD__ .
': Invalid parameter type' );
255 if ( $contextName ===
false ) {
257 } elseif ( $contextName[0] ===
'@' ) {
259 } elseif ( $contextName ===
'template' ) {
260 # Double-brace expansion
269 $ret = $this->parser->braceSubstitution( $bits, $this );
270 if ( isset( $ret[
'object'] ) ) {
271 $newIterator = $ret[
'object'];
273 $out .= $ret[
'text'];
276 } elseif ( $contextName ===
'tplarg' ) {
277 # Triple-brace expansion
286 $ret = $this->parser->argSubstitution( $bits, $this );
287 if ( isset( $ret[
'object'] ) ) {
288 $newIterator = $ret[
'object'];
290 $out .= $ret[
'text'];
293 } elseif ( $contextName ===
'comment' ) {
295 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
296 # Not in RECOVER_COMMENTS mode (msgnw) though.
299 $this->parser->getOptions()->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->getOutputType() ===
Parser::OT_WIKI )
324 $out .= $contextChildren[0];
328 } elseif ( $contextName ===
'ext' ) {
331 [
'attr' =>
null,
'inner' =>
null,
'close' => null ];
333 $s =
'<' . $bits[
'name']->getFirstChild()->value;
334 if ( $bits[
'attr'] ) {
335 $s .= $bits[
'attr']->getFirstChild()->value;
337 if ( $bits[
'inner'] ) {
338 $s .=
'>' . $bits[
'inner']->getFirstChild()->value;
339 if ( $bits[
'close'] ) {
340 $s .= $bits[
'close']->getFirstChild()->value;
347 $out .= $this->parser->extensionSubstitution( $bits, $this );
349 } elseif ( $contextName ===
'h' ) {
352 # Expand immediately and insert heading index marker
353 $s = $this->
expand( $contextChildren, $flags );
355 $titleText = $this->title->getPrefixedDBkey();
356 $this->parser->mHeadings[] = [ $titleText, $bits[
'i'] ];
357 $serial = count( $this->parser->mHeadings ) - 1;
359 $s = substr( $s, 0, $bits[
'level'] ) . $marker . substr( $s, $bits[
'level'] );
360 $this->parser->getStripState()->addGeneral( $marker,
'' );
363 $this->parser->useParsoidFragments() &&
366 $s = $this->
expand( $contextChildren, $flags );
369 $pFragment =
new HeadingPFragment( $fragment, $this->title, $bits[
'i'] );
372 $this->parser->mHeadings[] = [];
373 $serial = count( $this->parser->mHeadings ) - 1;
376 $this->parser->getStripState()->addParsoidOpaque( $marker, $pFragment );
379 # Expand in virtual stack
380 $newIterator = $contextChildren;
383 # Generic recursive expansion
384 $newIterator = $contextChildren;
387 if ( $newIterator !==
false ) {
389 $iteratorStack[] = $newIterator;
391 } elseif ( $iteratorStack[$level] ===
false ) {
394 while ( $iteratorStack[$level] ===
false && $level > 0 ) {
395 $outStack[$level - 1] .= $out;
396 array_pop( $outStack );
397 array_pop( $iteratorStack );
398 array_pop( $indexStack );