104 if (
$args !==
false ) {
107 } elseif ( !is_array(
$args ) ) {
108 throw new MWException( __METHOD__ .
': $args must be array or PPNode_Hash_Array' );
110 foreach (
$args as $arg ) {
111 $bits = $arg->splitArg();
112 if ( $bits[
'index'] !==
'' ) {
114 $index = $bits[
'index'] - $indexOffset;
115 if ( isset( $namedArgs[$index] ) || isset( $numberedArgs[$index] ) ) {
116 $this->parser->getOutput()->addWarning(
wfMessage(
'duplicate-args-warning',
120 $this->parser->addTrackingCategory(
'duplicate-args-category' );
122 $numberedArgs[$index] = $bits[
'value'];
123 unset( $namedArgs[$index] );
127 if ( isset( $namedArgs[$name] ) || isset( $numberedArgs[$name] ) ) {
128 $this->parser->getOutput()->addWarning(
wfMessage(
'duplicate-args-warning',
132 $this->parser->addTrackingCategory(
'duplicate-args-category' );
134 $namedArgs[$name] = $bits[
'value'];
135 unset( $numberedArgs[$name] );
160 public function expand( $root, $flags = 0 ) {
161 static $expansionDepth = 0;
162 if ( is_string( $root ) ) {
166 if ( ++$this->parser->mPPNodeCount > $this->maxPPNodeCount ) {
167 $this->parser->limitationWarn(
'node-count-exceeded',
168 $this->parser->mPPNodeCount,
169 $this->maxPPNodeCount
171 return '<span class="error">Node-count limit exceeded</span>';
173 if ( $expansionDepth > $this->maxPPExpandDepth ) {
174 $this->parser->limitationWarn(
'expansion-depth-exceeded',
176 $this->maxPPExpandDepth
178 return '<span class="error">Expansion depth limit exceeded</span>';
181 if ( $expansionDepth > $this->parser->mHighestExpansionDepth ) {
182 $this->parser->mHighestExpansionDepth = $expansionDepth;
185 $outStack = [
'',
'' ];
186 $iteratorStack = [
false, $root ];
187 $indexStack = [ 0, 0 ];
189 while ( count( $iteratorStack ) > 1 ) {
190 $level = count( $outStack ) - 1;
191 $iteratorNode =& $iteratorStack[$level];
192 $out =& $outStack[$level];
193 $index =& $indexStack[$level];
195 if ( is_array( $iteratorNode ) ) {
196 if ( $index >= count( $iteratorNode ) ) {
198 $iteratorStack[$level] =
false;
199 $contextNode =
false;
201 $contextNode = $iteratorNode[$index];
205 if ( $index >= $iteratorNode->getLength() ) {
207 $iteratorStack[$level] =
false;
208 $contextNode =
false;
210 $contextNode = $iteratorNode->item( $index );
216 $contextNode = $iteratorStack[$level];
217 $iteratorStack[$level] =
false;
220 $newIterator =
false;
221 $contextName =
false;
222 $contextChildren =
false;
224 if ( $contextNode ===
false ) {
226 } elseif ( is_string( $contextNode ) ) {
227 $out .= $contextNode;
229 $newIterator = $contextNode;
233 $out .= $contextNode->value;
235 $contextName = $contextNode->name;
236 $contextChildren = $contextNode->getRawChildren();
237 } elseif ( is_array( $contextNode ) ) {
239 if ( count( $contextNode ) !== 2 ) {
241 ': found an array where a node descriptor should be' );
243 list( $contextName, $contextChildren ) = $contextNode;
245 throw new MWException( __METHOD__ .
': Invalid parameter type' );
249 if ( $contextName ===
false ) {
251 } elseif ( $contextName[0] ===
'@' ) {
253 } elseif ( $contextName ===
'template' ) {
254 # Double-brace expansion
263 $ret = $this->parser->braceSubstitution( $bits, $this );
264 if ( isset( $ret[
'object'] ) ) {
265 $newIterator = $ret[
'object'];
267 $out .= $ret[
'text'];
270 } elseif ( $contextName ===
'tplarg' ) {
271 # Triple-brace expansion
280 $ret = $this->parser->argSubstitution( $bits, $this );
281 if ( isset( $ret[
'object'] ) ) {
282 $newIterator = $ret[
'object'];
284 $out .= $ret[
'text'];
287 } elseif ( $contextName ===
'comment' ) {
289 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
290 # Not in RECOVER_COMMENTS mode (msgnw) though.
291 if ( ( $this->parser->ot[
'html']
292 || ( $this->parser->ot[
'pre'] && $this->parser->mOptions->getRemoveComments() )
298 # Add a strip marker in PST mode so that pstPass2() can
299 # run some old-fashioned regexes on the result.
300 # Not in RECOVER_COMMENTS mode (extractSections) though.
301 $out .= $this->parser->insertStripItem( $contextChildren[0] );
303 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
304 $out .= $contextChildren[0];
306 } elseif ( $contextName ===
'ignore' ) {
307 # Output suppression used by <includeonly> etc.
308 # OT_WIKI will only respect <ignore> in substed templates.
309 # The other output types respect it unless NO_IGNORE is set.
310 # extractSections() sets NO_IGNORE and so never respects it.
311 if ( ( !isset( $this->parent ) && $this->parser->ot[
'wiki'] )
314 $out .= $contextChildren[0];
318 } elseif ( $contextName ===
'ext' ) {
321 [
'attr' =>
null,
'inner' =>
null,
'close' => null ];
323 $s =
'<' . $bits[
'name']->getFirstChild()->value;
325 if ( $bits[
'attr'] ) {
326 $s .= $bits[
'attr']->getFirstChild()->value;
329 if ( $bits[
'inner'] ) {
330 $s .=
'>' . $bits[
'inner']->getFirstChild()->value;
332 if ( $bits[
'close'] ) {
333 $s .= $bits[
'close']->getFirstChild()->value;
340 $out .= $this->parser->extensionSubstitution( $bits, $this );
342 } elseif ( $contextName ===
'h' ) {
344 if ( $this->parser->ot[
'html'] ) {
345 # Expand immediately and insert heading index marker
346 $s = $this->
expand( $contextChildren, $flags );
348 $titleText = $this->title->getPrefixedDBkey();
349 $this->parser->mHeadings[] = [ $titleText, $bits[
'i'] ];
350 $serial = count( $this->parser->mHeadings ) - 1;
351 $marker = Parser::MARKER_PREFIX .
"-h-$serial-" . Parser::MARKER_SUFFIX;
352 $s = substr(
$s, 0, $bits[
'level'] ) . $marker . substr(
$s, $bits[
'level'] );
353 $this->parser->mStripState->addGeneral( $marker,
'' );
356 # Expand in virtual stack
357 $newIterator = $contextChildren;
360 # Generic recursive expansion
361 $newIterator = $contextChildren;
364 if ( $newIterator !==
false ) {
366 $iteratorStack[] = $newIterator;
368 } elseif ( $iteratorStack[$level] ===
false ) {
371 while ( $iteratorStack[$level] ===
false && $level > 0 ) {
372 $outStack[$level - 1] .= $out;
373 array_pop( $outStack );
374 array_pop( $iteratorStack );
375 array_pop( $indexStack );