73 $this->title = $this->parser->getTitle();
74 $this->titleCache = [ $this->title ? $this->title->getPrefixedDBkey() : false ];
75 $this->loopCheckHash = [];
77 $this->childExpansionCache = [];
95 if (
$args !==
false ) {
101 foreach (
$args as $arg ) {
105 if ( !$xpath || $xpath->document !== $arg->ownerDocument ) {
106 $xpath =
new DOMXPath( $arg->ownerDocument );
109 $nameNodes = $xpath->query(
'name', $arg );
110 $value = $xpath->query(
'value', $arg );
111 if ( $nameNodes->item( 0 )->hasAttributes() ) {
113 $index = $nameNodes->item( 0 )->attributes->getNamedItem(
'index' )->textContent;
114 $index = $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] = $value->item( 0 );
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] = $value->item( 0 );
135 unset( $numberedArgs[$name] );
151 return $this->
expand( $root, $flags );
160 public function expand( $root, $flags = 0 ) {
161 static $expansionDepth = 0;
162 if ( is_string( $root ) ) {
166 if ( ++$this->parser->mPPNodeCount > $this->parser->mOptions->getMaxPPNodeCount() ) {
167 $this->parser->limitationWarn(
'node-count-exceeded',
168 $this->parser->mPPNodeCount,
169 $this->parser->mOptions->getMaxPPNodeCount()
171 return '<span class="error">Node-count limit exceeded</span>';
174 if ( $expansionDepth > $this->parser->mOptions->getMaxPPExpandDepth() ) {
175 $this->parser->limitationWarn(
'expansion-depth-exceeded',
177 $this->parser->mOptions->getMaxPPExpandDepth()
179 return '<span class="error">Expansion depth limit exceeded</span>';
182 if ( $expansionDepth > $this->parser->mHighestExpansionDepth ) {
183 $this->parser->mHighestExpansionDepth = $expansionDepth;
189 if ( $root instanceof DOMDocument ) {
190 $root = $root->documentElement;
193 $outStack = [
'',
'' ];
194 $iteratorStack = [
false, $root ];
195 $indexStack = [ 0, 0 ];
197 while ( count( $iteratorStack ) > 1 ) {
198 $level = count( $outStack ) - 1;
199 $iteratorNode =& $iteratorStack[$level];
200 $out =& $outStack[$level];
201 $index =& $indexStack[$level];
204 $iteratorNode = $iteratorNode->node;
207 if ( is_array( $iteratorNode ) ) {
208 if ( $index >= count( $iteratorNode ) ) {
210 $iteratorStack[$level] =
false;
211 $contextNode =
false;
213 $contextNode = $iteratorNode[$index];
216 } elseif ( $iteratorNode instanceof DOMNodeList ) {
217 if ( $index >= $iteratorNode->length ) {
219 $iteratorStack[$level] =
false;
220 $contextNode =
false;
222 $contextNode = $iteratorNode->item( $index );
228 $contextNode = $iteratorStack[$level];
229 $iteratorStack[$level] =
false;
233 $contextNode = $contextNode->node;
236 $newIterator =
false;
238 if ( $contextNode ===
false ) {
240 } elseif ( is_string( $contextNode ) ) {
241 $out .= $contextNode;
242 } elseif ( is_array( $contextNode ) || $contextNode instanceof DOMNodeList ) {
243 $newIterator = $contextNode;
244 } elseif ( $contextNode instanceof DOMNode ) {
245 if ( $contextNode->nodeType == XML_TEXT_NODE ) {
246 $out .= $contextNode->nodeValue;
247 } elseif ( $contextNode->nodeName ==
'template' ) {
248 # Double-brace expansion
249 $xpath =
new DOMXPath( $contextNode->ownerDocument );
250 $titles = $xpath->query(
'title', $contextNode );
251 $title = $titles->item( 0 );
252 $parts = $xpath->query(
'part', $contextNode );
256 $lineStart = $contextNode->getAttribute(
'lineStart' );
260 'lineStart' => $lineStart ];
261 $ret = $this->parser->braceSubstitution( $params, $this );
262 if ( isset( $ret[
'object'] ) ) {
263 $newIterator = $ret[
'object'];
265 $out .= $ret[
'text'];
268 } elseif ( $contextNode->nodeName ==
'tplarg' ) {
269 # Triple-brace expansion
270 $xpath =
new DOMXPath( $contextNode->ownerDocument );
271 $titles = $xpath->query(
'title', $contextNode );
272 $title = $titles->item( 0 );
273 $parts = $xpath->query(
'part', $contextNode );
280 $ret = $this->parser->argSubstitution( $params, $this );
281 if ( isset( $ret[
'object'] ) ) {
282 $newIterator = $ret[
'object'];
284 $out .= $ret[
'text'];
287 } elseif ( $contextNode->nodeName ==
'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( $contextNode->textContent );
303 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
304 $out .= $contextNode->textContent;
306 } elseif ( $contextNode->nodeName ==
'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 .= $contextNode->textContent;
318 } elseif ( $contextNode->nodeName ==
'ext' ) {
320 $xpath =
new DOMXPath( $contextNode->ownerDocument );
321 $names = $xpath->query(
'name', $contextNode );
322 $attrs = $xpath->query(
'attr', $contextNode );
323 $inners = $xpath->query(
'inner', $contextNode );
324 $closes = $xpath->query(
'close', $contextNode );
326 $s =
'<' . $this->
expand( $names->item( 0 ), $flags );
327 if ( $attrs->length > 0 ) {
328 $s .= $this->
expand( $attrs->item( 0 ), $flags );
330 if ( $inners->length > 0 ) {
331 $s .=
'>' . $this->
expand( $inners->item( 0 ), $flags );
332 if ( $closes->length > 0 ) {
333 $s .= $this->
expand( $closes->item( 0 ), $flags );
341 'name' =>
new PPNode_DOM( $names->item( 0 ) ),
342 'attr' => $attrs->length > 0 ?
new PPNode_DOM( $attrs->item( 0 ) ) :
null,
343 'inner' => $inners->length > 0 ?
new PPNode_DOM( $inners->item( 0 ) ) :
null,
344 'close' => $closes->length > 0 ?
new PPNode_DOM( $closes->item( 0 ) ) :
null,
346 $out .= $this->parser->extensionSubstitution( $params, $this );
348 } elseif ( $contextNode->nodeName ==
'h' ) {
350 $s = $this->
expand( $contextNode->childNodes, $flags );
352 # Insert a heading marker only for <h> children of <root>
353 # This is to stop extractSections from going over multiple tree levels
354 if ( $contextNode->parentNode->nodeName ==
'root' && $this->parser->ot[
'html'] ) {
355 # Insert heading index marker
356 $headingIndex = $contextNode->getAttribute(
'i' );
357 $titleText = $this->title->getPrefixedDBkey();
358 $this->parser->mHeadings[] = [ $titleText, $headingIndex ];
359 $serial = count( $this->parser->mHeadings ) - 1;
360 $marker = Parser::MARKER_PREFIX .
"-h-$serial-" . Parser::MARKER_SUFFIX;
361 $count = $contextNode->getAttribute(
'level' );
362 $s = substr(
$s, 0, $count ) . $marker . substr(
$s, $count );
363 $this->parser->mStripState->addGeneral( $marker,
'' );
367 # Generic recursive expansion
368 $newIterator = $contextNode->childNodes;
371 throw new MWException( __METHOD__ .
': Invalid parameter type' );
374 if ( $newIterator !==
false ) {
376 $newIterator = $newIterator->node;
379 $iteratorStack[] = $newIterator;
381 } elseif ( $iteratorStack[$level] ===
false ) {
384 while ( $iteratorStack[$level] ===
false && $level > 0 ) {
385 $outStack[$level - 1] .= $out;
386 array_pop( $outStack );
387 array_pop( $iteratorStack );
388 array_pop( $indexStack );
406 foreach (
$args as $root ) {
410 if ( !is_array( $root ) && !( $root instanceof DOMNodeList ) ) {
413 foreach ( $root as $node ) {
419 $s .= $this->
expand( $node, $flags );
436 foreach (
$args as $root ) {
440 if ( !is_array( $root ) && !( $root instanceof DOMNodeList ) ) {
443 foreach ( $root as $node ) {
468 foreach (
$args as $root ) {
472 if ( !is_array( $root ) && !( $root instanceof DOMNodeList ) ) {
475 foreach ( $root as $node ) {
500 foreach (
$args as $root ) {
504 if ( !is_array( $root ) && !( $root instanceof DOMNodeList ) ) {
507 foreach ( $root as $node ) {
525 if ( $level ===
false ) {
526 return $this->title->getPrefixedDBkey();
528 return $this->titleCache[$level] ??
false;
604 $this->
volatile = $flag;
622 if (
$ttl !==
null && ( $this->ttl ===
null || $ttl < $this->ttl ) ) {