114 if ( $args !==
false ) {
116 $args = $args->value;
117 } elseif ( !is_array( $args ) ) {
118 throw new InvalidArgumentException( __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',
128 Message::plaintextParam( (
string)$this->title ),
129 Message::plaintextParam( (
string)
$title ),
130 Message::numParam( $index )
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',
142 Message::plaintextParam( (
string)$this->title ),
143 Message::plaintextParam( (
string)
$title ),
144 Message::plaintextParam( $name )
146 $this->parser->addTrackingCategory(
'duplicate-args-category' );
148 $namedArgs[$name] = $bits[
'value'];
149 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 ) {
252 throw new RuntimeException( __METHOD__ .
253 ': found an array where a node descriptor should be' );
255 [ $contextName, $contextChildren ] = $contextNode;
257 throw new RuntimeException( __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->getOutputType() === Parser::OT_HTML
304 || ( $this->parser->getOutputType() === Parser::OT_PREPROCESS &&
305 $this->parser->getOptions()->getRemoveComments() )
311 $this->parser->getOutputType() === Parser::OT_WIKI &&
314 # Add a strip marker in PST mode so that pstPass2() can
315 # run some old-fashioned regexes on the result.
316 # Not in RECOVER_COMMENTS mode (extractSections) though.
317 $out .= $this->parser->insertStripItem( $contextChildren[0] );
319 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
320 $out .= $contextChildren[0];
322 } elseif ( $contextName ===
'ignore' ) {
323 # Output suppression used by <includeonly> etc.
324 # OT_WIKI will only respect <ignore> in substed templates.
325 # The other output types respect it unless NO_IGNORE is set.
326 # extractSections() sets NO_IGNORE and so never respects it.
327 if ( ( !isset( $this->parent ) &&
328 $this->parser->getOutputType() === Parser::OT_WIKI )
331 $out .= $contextChildren[0];
335 } elseif ( $contextName ===
'ext' ) {
338 [
'attr' =>
null,
'inner' =>
null,
'close' => null ];
340 $s =
'<' . $bits[
'name']->getFirstChild()->value;
342 if ( $bits[
'attr'] ) {
343 $s .= $bits[
'attr']->getFirstChild()->value;
346 if ( $bits[
'inner'] ) {
347 $s .=
'>' . $bits[
'inner']->getFirstChild()->value;
349 if ( $bits[
'close'] ) {
350 $s .= $bits[
'close']->getFirstChild()->value;
357 $out .= $this->parser->extensionSubstitution( $bits, $this,
360 } elseif ( $contextName ===
'h' ) {
362 if ( $this->parser->getOutputType() === Parser::OT_HTML ) {
363 # Expand immediately and insert heading index marker
364 $s = $this->
expand( $contextChildren, $flags );
366 $titleText = $this->title->getPrefixedDBkey();
367 $this->parser->mHeadings[] = [ $titleText, $bits[
'i'] ];
368 $serial = count( $this->parser->mHeadings ) - 1;
369 $marker = Parser::MARKER_PREFIX .
"-h-$serial-" . Parser::MARKER_SUFFIX;
370 $s = substr( $s, 0, $bits[
'level'] ) . $marker . substr( $s, $bits[
'level'] );
371 $this->parser->getStripState()->addGeneral( $marker,
'' );
374 # Expand in virtual stack
375 $newIterator = $contextChildren;
378 # Generic recursive expansion
379 $newIterator = $contextChildren;
382 if ( $newIterator !==
false ) {
384 $iteratorStack[] = $newIterator;
386 } elseif ( $iteratorStack[$level] ===
false ) {
389 while ( $iteratorStack[$level] ===
false && $level > 0 ) {
390 $outStack[$level - 1] .= $out;
391 array_pop( $outStack );
392 array_pop( $iteratorStack );
393 array_pop( $indexStack );