3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
23 * An expansion frame, used as a context to expand the result of preprocessToObj()
26 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
27 class PPFrame_DOM
implements PPFrame
{
46 * Hashtable listing templates which are disallowed for expansion in this frame,
47 * having been encountered previously in parent frames.
49 public $loopCheckHash;
52 * Recursion depth of this frame, top = 0
53 * Note that this is NOT the same as expansion depth in expand()
57 private $volatile = false;
63 protected $childExpansionCache;
66 * Construct a new preprocessor frame.
67 * @param Preprocessor $preprocessor The parent preprocessor
69 public function __construct( $preprocessor ) {
70 $this->preprocessor
= $preprocessor;
71 $this->parser
= $preprocessor->parser
;
72 $this->title
= $this->parser
->mTitle
;
73 $this->titleCache
= [ $this->title ?
$this->title
->getPrefixedDBkey() : false ];
74 $this->loopCheckHash
= [];
76 $this->childExpansionCache
= [];
80 * Create a new child frame
81 * $args is optionally a multi-root PPNode or array containing the template arguments
83 * @param bool|array $args
84 * @param Title|bool $title
85 * @param int $indexOffset
86 * @return PPTemplateFrame_DOM
88 public function newChild( $args = false, $title = false, $indexOffset = 0 ) {
91 if ( $title === false ) {
92 $title = $this->title
;
94 if ( $args !== false ) {
96 if ( $args instanceof PPNode
) {
99 foreach ( $args as $arg ) {
100 if ( $arg instanceof PPNode
) {
103 if ( !$xpath ||
$xpath->document
!== $arg->ownerDocument
) {
104 $xpath = new DOMXPath( $arg->ownerDocument
);
107 $nameNodes = $xpath->query( 'name', $arg );
108 $value = $xpath->query( 'value', $arg );
109 if ( $nameNodes->item( 0 )->hasAttributes() ) {
110 // Numbered parameter
111 $index = $nameNodes->item( 0 )->attributes
->getNamedItem( 'index' )->textContent
;
112 $index = $index - $indexOffset;
113 if ( isset( $namedArgs[$index] ) ||
isset( $numberedArgs[$index] ) ) {
114 $this->parser
->getOutput()->addWarning( wfMessage( 'duplicate-args-warning',
115 wfEscapeWikiText( $this->title
),
116 wfEscapeWikiText( $title ),
117 wfEscapeWikiText( $index ) )->text() );
118 $this->parser
->addTrackingCategory( 'duplicate-args-category' );
120 $numberedArgs[$index] = $value->item( 0 );
121 unset( $namedArgs[$index] );
124 $name = trim( $this->expand( $nameNodes->item( 0 ), PPFrame
::STRIP_COMMENTS
) );
125 if ( isset( $namedArgs[$name] ) ||
isset( $numberedArgs[$name] ) ) {
126 $this->parser
->getOutput()->addWarning( wfMessage( 'duplicate-args-warning',
127 wfEscapeWikiText( $this->title
),
128 wfEscapeWikiText( $title ),
129 wfEscapeWikiText( $name ) )->text() );
130 $this->parser
->addTrackingCategory( 'duplicate-args-category' );
132 $namedArgs[$name] = $value->item( 0 );
133 unset( $numberedArgs[$name] );
137 return new PPTemplateFrame_DOM( $this->preprocessor
, $this, $numberedArgs, $namedArgs, $title );
141 * @throws MWException
142 * @param string|int $key
143 * @param string|PPNode_DOM|DOMDocument $root
147 public function cachedExpand( $key, $root, $flags = 0 ) {
148 // we don't have a parent, so we don't have a cache
149 return $this->expand( $root, $flags );
153 * @throws MWException
154 * @param string|PPNode_DOM|DOMDocument $root
158 public function expand( $root, $flags = 0 ) {
159 static $expansionDepth = 0;
160 if ( is_string( $root ) ) {
164 if ( ++
$this->parser
->mPPNodeCount
> $this->parser
->mOptions
->getMaxPPNodeCount() ) {
165 $this->parser
->limitationWarn( 'node-count-exceeded',
166 $this->parser
->mPPNodeCount
,
167 $this->parser
->mOptions
->getMaxPPNodeCount()
169 return '<span class="error">Node-count limit exceeded</span>';
172 if ( $expansionDepth > $this->parser
->mOptions
->getMaxPPExpandDepth() ) {
173 $this->parser
->limitationWarn( 'expansion-depth-exceeded',
175 $this->parser
->mOptions
->getMaxPPExpandDepth()
177 return '<span class="error">Expansion depth limit exceeded</span>';
180 if ( $expansionDepth > $this->parser
->mHighestExpansionDepth
) {
181 $this->parser
->mHighestExpansionDepth
= $expansionDepth;
184 if ( $root instanceof PPNode_DOM
) {
187 if ( $root instanceof DOMDocument
) {
188 $root = $root->documentElement
;
191 $outStack = [ '', '' ];
192 $iteratorStack = [ false, $root ];
193 $indexStack = [ 0, 0 ];
195 while ( count( $iteratorStack ) > 1 ) {
196 $level = count( $outStack ) - 1;
197 $iteratorNode =& $iteratorStack[$level];
198 $out =& $outStack[$level];
199 $index =& $indexStack[$level];
201 if ( $iteratorNode instanceof PPNode_DOM
) {
202 $iteratorNode = $iteratorNode->node
;
205 if ( is_array( $iteratorNode ) ) {
206 if ( $index >= count( $iteratorNode ) ) {
207 // All done with this iterator
208 $iteratorStack[$level] = false;
209 $contextNode = false;
211 $contextNode = $iteratorNode[$index];
214 } elseif ( $iteratorNode instanceof DOMNodeList
) {
215 if ( $index >= $iteratorNode->length
) {
216 // All done with this iterator
217 $iteratorStack[$level] = false;
218 $contextNode = false;
220 $contextNode = $iteratorNode->item( $index );
224 // Copy to $contextNode and then delete from iterator stack,
225 // because this is not an iterator but we do have to execute it once
226 $contextNode = $iteratorStack[$level];
227 $iteratorStack[$level] = false;
230 if ( $contextNode instanceof PPNode_DOM
) {
231 $contextNode = $contextNode->node
;
234 $newIterator = false;
236 if ( $contextNode === false ) {
238 } elseif ( is_string( $contextNode ) ) {
239 $out .= $contextNode;
240 } elseif ( is_array( $contextNode ) ||
$contextNode instanceof DOMNodeList
) {
241 $newIterator = $contextNode;
242 } elseif ( $contextNode instanceof DOMNode
) {
243 if ( $contextNode->nodeType
== XML_TEXT_NODE
) {
244 $out .= $contextNode->nodeValue
;
245 } elseif ( $contextNode->nodeName
== 'template' ) {
246 # Double-brace expansion
247 $xpath = new DOMXPath( $contextNode->ownerDocument
);
248 $titles = $xpath->query( 'title', $contextNode );
249 $title = $titles->item( 0 );
250 $parts = $xpath->query( 'part', $contextNode );
251 if ( $flags & PPFrame
::NO_TEMPLATES
) {
252 $newIterator = $this->virtualBracketedImplode( '{{', '|', '}}', $title, $parts );
254 $lineStart = $contextNode->getAttribute( 'lineStart' );
256 'title' => new PPNode_DOM( $title ),
257 'parts' => new PPNode_DOM( $parts ),
258 'lineStart' => $lineStart ];
259 $ret = $this->parser
->braceSubstitution( $params, $this );
260 if ( isset( $ret['object'] ) ) {
261 $newIterator = $ret['object'];
263 $out .= $ret['text'];
266 } elseif ( $contextNode->nodeName
== 'tplarg' ) {
267 # Triple-brace expansion
268 $xpath = new DOMXPath( $contextNode->ownerDocument
);
269 $titles = $xpath->query( 'title', $contextNode );
270 $title = $titles->item( 0 );
271 $parts = $xpath->query( 'part', $contextNode );
272 if ( $flags & PPFrame
::NO_ARGS
) {
273 $newIterator = $this->virtualBracketedImplode( '{{{', '|', '}}}', $title, $parts );
276 'title' => new PPNode_DOM( $title ),
277 'parts' => new PPNode_DOM( $parts ) ];
278 $ret = $this->parser
->argSubstitution( $params, $this );
279 if ( isset( $ret['object'] ) ) {
280 $newIterator = $ret['object'];
282 $out .= $ret['text'];
285 } elseif ( $contextNode->nodeName
== 'comment' ) {
287 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
288 # Not in RECOVER_COMMENTS mode (msgnw) though.
289 if ( ( $this->parser
->ot
['html']
290 ||
( $this->parser
->ot
['pre'] && $this->parser
->mOptions
->getRemoveComments() )
291 ||
( $flags & PPFrame
::STRIP_COMMENTS
)
292 ) && !( $flags & PPFrame
::RECOVER_COMMENTS
)
295 } elseif ( $this->parser
->ot
['wiki'] && !( $flags & PPFrame
::RECOVER_COMMENTS
) ) {
296 # Add a strip marker in PST mode so that pstPass2() can
297 # run some old-fashioned regexes on the result.
298 # Not in RECOVER_COMMENTS mode (extractSections) though.
299 $out .= $this->parser
->insertStripItem( $contextNode->textContent
);
301 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
302 $out .= $contextNode->textContent
;
304 } elseif ( $contextNode->nodeName
== 'ignore' ) {
305 # Output suppression used by <includeonly> etc.
306 # OT_WIKI will only respect <ignore> in substed templates.
307 # The other output types respect it unless NO_IGNORE is set.
308 # extractSections() sets NO_IGNORE and so never respects it.
309 if ( ( !isset( $this->parent
) && $this->parser
->ot
['wiki'] )
310 ||
( $flags & PPFrame
::NO_IGNORE
)
312 $out .= $contextNode->textContent
;
316 } elseif ( $contextNode->nodeName
== 'ext' ) {
318 $xpath = new DOMXPath( $contextNode->ownerDocument
);
319 $names = $xpath->query( 'name', $contextNode );
320 $attrs = $xpath->query( 'attr', $contextNode );
321 $inners = $xpath->query( 'inner', $contextNode );
322 $closes = $xpath->query( 'close', $contextNode );
323 if ( $flags & PPFrame
::NO_TAGS
) {
324 $s = '<' . $this->expand( $names->item( 0 ), $flags );
325 if ( $attrs->length
> 0 ) {
326 $s .= $this->expand( $attrs->item( 0 ), $flags );
328 if ( $inners->length
> 0 ) {
329 $s .= '>' . $this->expand( $inners->item( 0 ), $flags );
330 if ( $closes->length
> 0 ) {
331 $s .= $this->expand( $closes->item( 0 ), $flags );
339 'name' => new PPNode_DOM( $names->item( 0 ) ),
340 'attr' => $attrs->length
> 0 ?
new PPNode_DOM( $attrs->item( 0 ) ) : null,
341 'inner' => $inners->length
> 0 ?
new PPNode_DOM( $inners->item( 0 ) ) : null,
342 'close' => $closes->length
> 0 ?
new PPNode_DOM( $closes->item( 0 ) ) : null,
344 $out .= $this->parser
->extensionSubstitution( $params, $this );
346 } elseif ( $contextNode->nodeName
== 'h' ) {
348 $s = $this->expand( $contextNode->childNodes
, $flags );
350 # Insert a heading marker only for <h> children of <root>
351 # This is to stop extractSections from going over multiple tree levels
352 if ( $contextNode->parentNode
->nodeName
== 'root' && $this->parser
->ot
['html'] ) {
353 # Insert heading index marker
354 $headingIndex = $contextNode->getAttribute( 'i' );
355 $titleText = $this->title
->getPrefixedDBkey();
356 $this->parser
->mHeadings
[] = [ $titleText, $headingIndex ];
357 $serial = count( $this->parser
->mHeadings
) - 1;
358 $marker = Parser
::MARKER_PREFIX
. "-h-$serial-" . Parser
::MARKER_SUFFIX
;
359 $count = $contextNode->getAttribute( 'level' );
360 $s = substr( $s, 0, $count ) . $marker . substr( $s, $count );
361 $this->parser
->mStripState
->addGeneral( $marker, '' );
365 # Generic recursive expansion
366 $newIterator = $contextNode->childNodes
;
369 throw new MWException( __METHOD__
. ': Invalid parameter type' );
372 if ( $newIterator !== false ) {
373 if ( $newIterator instanceof PPNode_DOM
) {
374 $newIterator = $newIterator->node
;
377 $iteratorStack[] = $newIterator;
379 } elseif ( $iteratorStack[$level] === false ) {
380 // Return accumulated value to parent
381 // With tail recursion
382 while ( $iteratorStack[$level] === false && $level > 0 ) {
383 $outStack[$level - 1] .= $out;
384 array_pop( $outStack );
385 array_pop( $iteratorStack );
386 array_pop( $indexStack );
398 * @param string|PPNode_DOM|DOMDocument ...$args
401 public function implodeWithFlags( $sep, $flags, ...$args ) {
404 foreach ( $args as $root ) {
405 if ( $root instanceof PPNode_DOM
) {
408 if ( !is_array( $root ) && !( $root instanceof DOMNodeList
) ) {
411 foreach ( $root as $node ) {
417 $s .= $this->expand( $node, $flags );
424 * Implode with no flags specified
425 * This previously called implodeWithFlags but has now been inlined to reduce stack depth
428 * @param string|PPNode_DOM|DOMDocument ...$args
431 public function implode( $sep, ...$args ) {
434 foreach ( $args as $root ) {
435 if ( $root instanceof PPNode_DOM
) {
438 if ( !is_array( $root ) && !( $root instanceof DOMNodeList
) ) {
441 foreach ( $root as $node ) {
447 $s .= $this->expand( $node );
454 * Makes an object that, when expand()ed, will be the same as one obtained
458 * @param string|PPNode_DOM|DOMDocument ...$args
461 public function virtualImplode( $sep, ...$args ) {
465 foreach ( $args as $root ) {
466 if ( $root instanceof PPNode_DOM
) {
469 if ( !is_array( $root ) && !( $root instanceof DOMNodeList
) ) {
472 foreach ( $root as $node ) {
485 * Virtual implode with brackets
486 * @param string $start
489 * @param string|PPNode_DOM|DOMDocument ...$args
492 public function virtualBracketedImplode( $start, $sep, $end, ...$args ) {
496 foreach ( $args as $root ) {
497 if ( $root instanceof PPNode_DOM
) {
500 if ( !is_array( $root ) && !( $root instanceof DOMNodeList
) ) {
503 foreach ( $root as $node ) {
516 public function __toString() {
520 public function getPDBK( $level = false ) {
521 if ( $level === false ) {
522 return $this->title
->getPrefixedDBkey();
524 return $this->titleCache
[$level] ??
false;
531 public function getArguments() {
538 public function getNumberedArguments() {
545 public function getNamedArguments() {
550 * Returns true if there are no arguments in this frame
554 public function isEmpty() {
559 * @param int|string $name
560 * @return bool Always false in this implementation.
562 public function getArgument( $name ) {
567 * Returns true if the infinite loop check is OK, false if a loop is detected
569 * @param Title $title
572 public function loopCheck( $title ) {
573 return !isset( $this->loopCheckHash
[$title->getPrefixedDBkey()] );
577 * Return true if the frame is a template frame
581 public function isTemplate() {
586 * Get a title of frame
590 public function getTitle() {
595 * Set the volatile flag
599 public function setVolatile( $flag = true ) {
600 $this->volatile
= $flag;
604 * Get the volatile flag
608 public function isVolatile() {
609 return $this->volatile
;
617 public function setTTL( $ttl ) {
618 if ( $ttl !== null && ( $this->ttl
=== null ||
$ttl < $this->ttl
) ) {
628 public function getTTL() {