74 $this->title = $this->parser->getTitle();
75 $this->titleCache = [ $this->title ? $this->title->getPrefixedDBkey() : false ];
76 $this->loopCheckHash = [];
78 $this->childExpansionCache = [];
96 if (
$args !==
false ) {
102 foreach (
$args as $arg ) {
106 if ( !$xpath || $xpath->document !== $arg->ownerDocument ) {
107 $xpath =
new DOMXPath( $arg->ownerDocument );
110 $nameNodes = $xpath->query(
'name', $arg );
111 $value = $xpath->query(
'value', $arg );
112 if ( $nameNodes->item( 0 )->hasAttributes() ) {
114 $index = $nameNodes->item( 0 )->attributes->getNamedItem(
'index' )->textContent;
115 $index = $index - $indexOffset;
116 if ( isset( $namedArgs[$index] ) || isset( $numberedArgs[$index] ) ) {
117 $this->parser->getOutput()->addWarning(
wfMessage(
'duplicate-args-warning',
121 $this->parser->addTrackingCategory(
'duplicate-args-category' );
123 $numberedArgs[$index] = $value->item( 0 );
124 unset( $namedArgs[$index] );
128 if ( isset( $namedArgs[$name] ) || isset( $numberedArgs[$name] ) ) {
129 $this->parser->getOutput()->addWarning(
wfMessage(
'duplicate-args-warning',
133 $this->parser->addTrackingCategory(
'duplicate-args-category' );
135 $namedArgs[$name] = $value->item( 0 );
136 unset( $numberedArgs[$name] );
152 return $this->
expand( $root, $flags );
161 public function expand( $root, $flags = 0 ) {
162 static $expansionDepth = 0;
163 if ( is_string( $root ) ) {
167 if ( ++$this->parser->mPPNodeCount > $this->parser->mOptions->getMaxPPNodeCount() ) {
168 $this->parser->limitationWarn(
'node-count-exceeded',
169 $this->parser->mPPNodeCount,
170 $this->parser->mOptions->getMaxPPNodeCount()
172 return '<span class="error">Node-count limit exceeded</span>';
175 if ( $expansionDepth > $this->parser->mOptions->getMaxPPExpandDepth() ) {
176 $this->parser->limitationWarn(
'expansion-depth-exceeded',
178 $this->parser->mOptions->getMaxPPExpandDepth()
180 return '<span class="error">Expansion depth limit exceeded</span>';
183 if ( $expansionDepth > $this->parser->mHighestExpansionDepth ) {
184 $this->parser->mHighestExpansionDepth = $expansionDepth;
190 if ( $root instanceof DOMDocument ) {
191 $root = $root->documentElement;
194 $outStack = [
'',
'' ];
195 $iteratorStack = [
false, $root ];
196 $indexStack = [ 0, 0 ];
198 while ( count( $iteratorStack ) > 1 ) {
199 $level = count( $outStack ) - 1;
200 $iteratorNode =& $iteratorStack[$level];
201 $out =& $outStack[$level];
202 $index =& $indexStack[$level];
205 $iteratorNode = $iteratorNode->node;
208 if ( is_array( $iteratorNode ) ) {
209 if ( $index >= count( $iteratorNode ) ) {
211 $iteratorStack[$level] =
false;
212 $contextNode =
false;
214 $contextNode = $iteratorNode[$index];
217 } elseif ( $iteratorNode instanceof DOMNodeList ) {
218 if ( $index >= $iteratorNode->length ) {
220 $iteratorStack[$level] =
false;
221 $contextNode =
false;
223 $contextNode = $iteratorNode->item( $index );
229 $contextNode = $iteratorStack[$level];
230 $iteratorStack[$level] =
false;
234 $contextNode = $contextNode->node;
237 $newIterator =
false;
239 if ( $contextNode ===
false ) {
241 } elseif ( is_string( $contextNode ) ) {
242 $out .= $contextNode;
243 } elseif ( is_array( $contextNode ) || $contextNode instanceof DOMNodeList ) {
244 $newIterator = $contextNode;
245 } elseif ( $contextNode instanceof DOMNode ) {
246 if ( $contextNode->nodeType == XML_TEXT_NODE ) {
247 $out .= $contextNode->nodeValue;
248 } elseif ( $contextNode->nodeName ==
'template' ) {
249 # Double-brace expansion
250 $xpath =
new DOMXPath( $contextNode->ownerDocument );
251 $titles = $xpath->query(
'title', $contextNode );
252 $title = $titles->item( 0 );
253 $parts = $xpath->query(
'part', $contextNode );
257 $lineStart = $contextNode->getAttribute(
'lineStart' );
261 'lineStart' => $lineStart ];
262 $ret = $this->parser->braceSubstitution( $params, $this );
263 if ( isset( $ret[
'object'] ) ) {
264 $newIterator = $ret[
'object'];
266 $out .= $ret[
'text'];
269 } elseif ( $contextNode->nodeName ==
'tplarg' ) {
270 # Triple-brace expansion
271 $xpath =
new DOMXPath( $contextNode->ownerDocument );
272 $titles = $xpath->query(
'title', $contextNode );
273 $title = $titles->item( 0 );
274 $parts = $xpath->query(
'part', $contextNode );
281 $ret = $this->parser->argSubstitution( $params, $this );
282 if ( isset( $ret[
'object'] ) ) {
283 $newIterator = $ret[
'object'];
285 $out .= $ret[
'text'];
288 } elseif ( $contextNode->nodeName ==
'comment' ) {
290 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
291 # Not in RECOVER_COMMENTS mode (msgnw) though.
292 if ( ( $this->parser->ot[
'html']
293 || ( $this->parser->ot[
'pre'] && $this->parser->mOptions->getRemoveComments() )
299 # Add a strip marker in PST mode so that pstPass2() can
300 # run some old-fashioned regexes on the result.
301 # Not in RECOVER_COMMENTS mode (extractSections) though.
302 $out .= $this->parser->insertStripItem( $contextNode->textContent );
304 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
305 $out .= $contextNode->textContent;
307 } elseif ( $contextNode->nodeName ==
'ignore' ) {
308 # Output suppression used by <includeonly> etc.
309 # OT_WIKI will only respect <ignore> in substed templates.
310 # The other output types respect it unless NO_IGNORE is set.
311 # extractSections() sets NO_IGNORE and so never respects it.
312 if ( ( !isset( $this->parent ) && $this->parser->ot[
'wiki'] )
315 $out .= $contextNode->textContent;
319 } elseif ( $contextNode->nodeName ==
'ext' ) {
321 $xpath =
new DOMXPath( $contextNode->ownerDocument );
322 $names = $xpath->query(
'name', $contextNode );
323 $attrs = $xpath->query(
'attr', $contextNode );
324 $inners = $xpath->query(
'inner', $contextNode );
325 $closes = $xpath->query(
'close', $contextNode );
327 $s =
'<' . $this->
expand( $names->item( 0 ), $flags );
328 if ( $attrs->length > 0 ) {
329 $s .= $this->
expand( $attrs->item( 0 ), $flags );
331 if ( $inners->length > 0 ) {
332 $s .=
'>' . $this->
expand( $inners->item( 0 ), $flags );
333 if ( $closes->length > 0 ) {
334 $s .= $this->
expand( $closes->item( 0 ), $flags );
342 'name' =>
new PPNode_DOM( $names->item( 0 ) ),
343 'attr' => $attrs->length > 0 ?
new PPNode_DOM( $attrs->item( 0 ) ) :
null,
344 'inner' => $inners->length > 0 ?
new PPNode_DOM( $inners->item( 0 ) ) :
null,
345 'close' => $closes->length > 0 ?
new PPNode_DOM( $closes->item( 0 ) ) :
null,
347 $out .= $this->parser->extensionSubstitution( $params, $this );
349 } elseif ( $contextNode->nodeName ==
'h' ) {
351 $s = $this->
expand( $contextNode->childNodes, $flags );
353 # Insert a heading marker only for <h> children of <root>
354 # This is to stop extractSections from going over multiple tree levels
355 if ( $contextNode->parentNode->nodeName ==
'root' && $this->parser->ot[
'html'] ) {
356 # Insert heading index marker
357 $headingIndex = $contextNode->getAttribute(
'i' );
358 $titleText = $this->title->getPrefixedDBkey();
359 $this->parser->mHeadings[] = [ $titleText, $headingIndex ];
360 $serial = count( $this->parser->mHeadings ) - 1;
361 $marker = Parser::MARKER_PREFIX .
"-h-$serial-" . Parser::MARKER_SUFFIX;
362 $count = $contextNode->getAttribute(
'level' );
363 $s = substr(
$s, 0, $count ) . $marker . substr(
$s, $count );
364 $this->parser->mStripState->addGeneral( $marker,
'' );
368 # Generic recursive expansion
369 $newIterator = $contextNode->childNodes;
372 throw new MWException( __METHOD__ .
': Invalid parameter type' );
375 if ( $newIterator !==
false ) {
377 $newIterator = $newIterator->node;
380 $iteratorStack[] = $newIterator;
382 } elseif ( $iteratorStack[$level] ===
false ) {
385 while ( $iteratorStack[$level] ===
false && $level > 0 ) {
386 $outStack[$level - 1] .= $out;
387 array_pop( $outStack );
388 array_pop( $iteratorStack );
389 array_pop( $indexStack );
407 foreach (
$args as $root ) {
411 if ( !is_array( $root ) && !( $root instanceof DOMNodeList ) ) {
414 foreach ( $root as $node ) {
420 $s .= $this->
expand( $node, $flags );
437 foreach (
$args as $root ) {
441 if ( !is_array( $root ) && !( $root instanceof DOMNodeList ) ) {
444 foreach ( $root as $node ) {
469 foreach (
$args as $root ) {
473 if ( !is_array( $root ) && !( $root instanceof DOMNodeList ) ) {
476 foreach ( $root as $node ) {
501 foreach (
$args as $root ) {
505 if ( !is_array( $root ) && !( $root instanceof DOMNodeList ) ) {
508 foreach ( $root as $node ) {
526 if ( $level ===
false ) {
527 return $this->title->getPrefixedDBkey();
529 return $this->titleCache[$level] ??
false;
605 $this->
volatile = $flag;
623 if (
$ttl !==
null && ( $this->ttl ===
null || $ttl < $this->ttl ) ) {