115 if ( $args !==
false ) {
117 $args = $args->value;
118 } elseif ( !is_array( $args ) ) {
119 throw new InvalidArgumentException( __METHOD__ .
': $args must be array or PPNode_Hash_Array' );
121 foreach ( $args as $arg ) {
122 $bits = $arg->splitArg();
123 if ( $bits[
'index'] !==
'' ) {
125 $index = $bits[
'index'] - $indexOffset;
126 if ( isset( $namedArgs[$index] ) || isset( $numberedArgs[$index] ) ) {
127 $this->parser->getOutput()->addWarningMsg(
128 'duplicate-args-warning',
129 Message::plaintextParam( (
string)$this->title ),
130 Message::plaintextParam( (
string)
$title ),
131 Message::numParam( $index )
133 $this->parser->addTrackingCategory(
'duplicate-args-category' );
135 $numberedArgs[$index] = $bits[
'value'];
136 unset( $namedArgs[$index] );
140 if ( isset( $namedArgs[$name] ) || isset( $numberedArgs[$name] ) ) {
141 $this->parser->getOutput()->addWarningMsg(
142 'duplicate-args-warning',
143 Message::plaintextParam( (
string)$this->title ),
144 Message::plaintextParam( (
string)
$title ),
145 Message::plaintextParam( $name )
147 $this->parser->addTrackingCategory(
'duplicate-args-category' );
149 $namedArgs[$name] = $bits[
'value'];
150 unset( $numberedArgs[$name] );
173 public function expand( $root, $flags = 0 ) {
174 static $expansionDepth = 0;
175 if ( is_string( $root ) ) {
179 if ( ++$this->parser->mPPNodeCount > $this->maxPPNodeCount ) {
180 $this->parser->limitationWarn(
'node-count-exceeded',
181 $this->parser->mPPNodeCount,
182 $this->maxPPNodeCount
184 return '<span class="error">Node-count limit exceeded</span>';
186 if ( $expansionDepth > $this->maxPPExpandDepth ) {
187 $this->parser->limitationWarn(
'expansion-depth-exceeded',
189 $this->maxPPExpandDepth
191 return '<span class="error">Expansion depth limit exceeded</span>';
194 if ( $expansionDepth > $this->parser->mHighestExpansionDepth ) {
195 $this->parser->mHighestExpansionDepth = $expansionDepth;
198 $outStack = [
'',
'' ];
199 $iteratorStack = [
false, $root ];
200 $indexStack = [ 0, 0 ];
202 while ( count( $iteratorStack ) > 1 ) {
203 $level = count( $outStack ) - 1;
204 $iteratorNode =& $iteratorStack[$level];
205 $out =& $outStack[$level];
206 $index =& $indexStack[$level];
208 if ( is_array( $iteratorNode ) ) {
209 if ( $index >= count( $iteratorNode ) ) {
211 $iteratorStack[$level] =
false;
212 $contextNode =
false;
214 $contextNode = $iteratorNode[$index];
218 if ( $index >= $iteratorNode->getLength() ) {
220 $iteratorStack[$level] =
false;
221 $contextNode =
false;
223 $contextNode = $iteratorNode->item( $index );
229 $contextNode = $iteratorStack[$level];
230 $iteratorStack[$level] =
false;
233 $newIterator =
false;
234 $contextName =
false;
235 $contextChildren =
false;
237 if ( $contextNode ===
false ) {
239 } elseif ( is_string( $contextNode ) ) {
240 $out .= $contextNode;
242 $newIterator = $contextNode;
246 $out .= $contextNode->value;
248 $contextName = $contextNode->name;
249 $contextChildren = $contextNode->getRawChildren();
250 } elseif ( is_array( $contextNode ) ) {
252 if ( count( $contextNode ) !== 2 ) {
253 throw new RuntimeException( __METHOD__ .
254 ': found an array where a node descriptor should be' );
256 [ $contextName, $contextChildren ] = $contextNode;
258 throw new RuntimeException( __METHOD__ .
': Invalid parameter type' );
262 if ( $contextName ===
false ) {
264 } elseif ( $contextName[0] ===
'@' ) {
266 } elseif ( $contextName ===
'template' ) {
267 # Double-brace expansion
276 $ret = $this->parser->braceSubstitution( $bits, $this );
277 if ( isset( $ret[
'object'] ) ) {
278 $newIterator = $ret[
'object'];
280 $out .= $ret[
'text'];
283 } elseif ( $contextName ===
'tplarg' ) {
284 # Triple-brace expansion
293 $ret = $this->parser->argSubstitution( $bits, $this );
294 if ( isset( $ret[
'object'] ) ) {
295 $newIterator = $ret[
'object'];
297 $out .= $ret[
'text'];
300 } elseif ( $contextName ===
'comment' ) {
302 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
303 # Not in RECOVER_COMMENTS mode (msgnw) though.
304 if ( ( $this->parser->getOutputType() === Parser::OT_HTML
305 || ( $this->parser->getOutputType() === Parser::OT_PREPROCESS &&
306 $this->parser->getOptions()->getRemoveComments() )
312 $this->parser->getOutputType() === Parser::OT_WIKI &&
315 # Add a strip marker in PST mode so that pstPass2() can
316 # run some old-fashioned regexes on the result.
317 # Not in RECOVER_COMMENTS mode (extractSections) though.
318 $out .= $this->parser->insertStripItem( $contextChildren[0] );
320 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
321 $out .= $contextChildren[0];
323 } elseif ( $contextName ===
'ignore' ) {
324 # Output suppression used by <includeonly> etc.
325 # OT_WIKI will only respect <ignore> in substed templates.
326 # The other output types respect it unless NO_IGNORE is set.
327 # extractSections() sets NO_IGNORE and so never respects it.
328 if ( ( !isset( $this->parent ) && $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 );