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()
24 * @deprecated since 1.34, use PPFrame_Hash
26 * @phan-file-suppress PhanUndeclaredMethod
28 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
29 class PPFrame_DOM
implements PPFrame
{
48 * Hashtable listing templates which are disallowed for expansion in this frame,
49 * having been encountered previously in parent frames.
51 public $loopCheckHash;
54 * Recursion depth of this frame, top = 0
55 * Note that this is NOT the same as expansion depth in expand()
59 private $volatile = false;
65 protected $childExpansionCache;
68 * Construct a new preprocessor frame.
69 * @param Preprocessor $preprocessor The parent preprocessor
71 public function __construct( $preprocessor ) {
72 $this->preprocessor
= $preprocessor;
73 $this->parser
= $preprocessor->parser
;
74 $this->title
= $this->parser
->getTitle();
75 $this->titleCache
= [ $this->title ?
$this->title
->getPrefixedDBkey() : false ];
76 $this->loopCheckHash
= [];
78 $this->childExpansionCache
= [];
82 * Create a new child frame
83 * $args is optionally a multi-root PPNode or array containing the template arguments
85 * @param bool|array $args
86 * @param Title|bool $title
87 * @param int $indexOffset
88 * @return PPTemplateFrame_DOM
90 public function newChild( $args = false, $title = false, $indexOffset = 0 ) {
93 if ( $title === false ) {
94 $title = $this->title
;
96 if ( $args !== false ) {
98 if ( $args instanceof PPNode
) {
101 foreach ( $args as $arg ) {
102 if ( $arg instanceof PPNode
) {
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() ) {
112 // Numbered parameter
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',
117 wfEscapeWikiText( $this->title
),
118 wfEscapeWikiText( $title ),
119 wfEscapeWikiText( $index ) )->text() );
120 $this->parser
->addTrackingCategory( 'duplicate-args-category' );
122 $numberedArgs[$index] = $value->item( 0 );
123 unset( $namedArgs[$index] );
126 $name = trim( $this->expand( $nameNodes->item( 0 ), PPFrame
::STRIP_COMMENTS
) );
127 if ( isset( $namedArgs[$name] ) ||
isset( $numberedArgs[$name] ) ) {
128 $this->parser
->getOutput()->addWarning( wfMessage( 'duplicate-args-warning',
129 wfEscapeWikiText( $this->title
),
130 wfEscapeWikiText( $title ),
131 wfEscapeWikiText( $name ) )->text() );
132 $this->parser
->addTrackingCategory( 'duplicate-args-category' );
134 $namedArgs[$name] = $value->item( 0 );
135 unset( $numberedArgs[$name] );
139 return new PPTemplateFrame_DOM( $this->preprocessor
, $this, $numberedArgs, $namedArgs, $title );
143 * @throws MWException
144 * @param string|int $key
145 * @param string|PPNode_DOM|DOMNode|DOMNodeList $root
149 public function cachedExpand( $key, $root, $flags = 0 ) {
150 // we don't have a parent, so we don't have a cache
151 return $this->expand( $root, $flags );
155 * @throws MWException
156 * @param string|PPNode_DOM|DOMNode $root
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;
186 if ( $root instanceof PPNode_DOM
) {
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];
203 if ( $iteratorNode instanceof PPNode_DOM
) {
204 $iteratorNode = $iteratorNode->node
;
207 if ( is_array( $iteratorNode ) ) {
208 if ( $index >= count( $iteratorNode ) ) {
209 // All done with this iterator
210 $iteratorStack[$level] = false;
211 $contextNode = false;
213 $contextNode = $iteratorNode[$index];
216 } elseif ( $iteratorNode instanceof DOMNodeList
) {
217 if ( $index >= $iteratorNode->length
) {
218 // All done with this iterator
219 $iteratorStack[$level] = false;
220 $contextNode = false;
222 $contextNode = $iteratorNode->item( $index );
226 // Copy to $contextNode and then delete from iterator stack,
227 // because this is not an iterator but we do have to execute it once
228 $contextNode = $iteratorStack[$level];
229 $iteratorStack[$level] = false;
232 if ( $contextNode instanceof PPNode_DOM
) {
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 );
253 if ( $flags & PPFrame
::NO_TEMPLATES
) {
254 $newIterator = $this->virtualBracketedImplode( '{{', '|', '}}', $title, $parts );
256 $lineStart = $contextNode->getAttribute( 'lineStart' );
258 'title' => new PPNode_DOM( $title ),
259 'parts' => new PPNode_DOM( $parts ),
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 );
274 if ( $flags & PPFrame
::NO_ARGS
) {
275 $newIterator = $this->virtualBracketedImplode( '{{{', '|', '}}}', $title, $parts );
278 'title' => new PPNode_DOM( $title ),
279 'parts' => new PPNode_DOM( $parts ) ];
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() )
293 ||
( $flags & PPFrame
::STRIP_COMMENTS
)
294 ) && !( $flags & PPFrame
::RECOVER_COMMENTS
)
297 } elseif ( $this->parser
->ot
['wiki'] && !( $flags & PPFrame
::RECOVER_COMMENTS
) ) {
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'] )
312 ||
( $flags & PPFrame
::NO_IGNORE
)
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 );
325 if ( $flags & PPFrame
::NO_TAGS
) {
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 ) {
375 if ( $newIterator instanceof PPNode_DOM
) {
376 $newIterator = $newIterator->node
;
379 $iteratorStack[] = $newIterator;
381 } elseif ( $iteratorStack[$level] === false ) {
382 // Return accumulated value to parent
383 // With tail recursion
384 while ( $iteratorStack[$level] === false && $level > 0 ) {
385 $outStack[$level - 1] .= $out;
386 array_pop( $outStack );
387 array_pop( $iteratorStack );
388 array_pop( $indexStack );
400 * @param string|PPNode_DOM|DOMNode ...$args
403 public function implodeWithFlags( $sep, $flags, ...$args ) {
406 foreach ( $args as $root ) {
407 if ( $root instanceof PPNode_DOM
) {
410 if ( !is_array( $root ) && !( $root instanceof DOMNodeList
) ) {
413 foreach ( $root as $node ) {
419 $s .= $this->expand( $node, $flags );
426 * Implode with no flags specified
427 * This previously called implodeWithFlags but has now been inlined to reduce stack depth
430 * @param string|PPNode_DOM|DOMNode ...$args
433 public function implode( $sep, ...$args ) {
436 foreach ( $args as $root ) {
437 if ( $root instanceof PPNode_DOM
) {
440 if ( !is_array( $root ) && !( $root instanceof DOMNodeList
) ) {
443 foreach ( $root as $node ) {
449 $s .= $this->expand( $node );
456 * Makes an object that, when expand()ed, will be the same as one obtained
460 * @param string|PPNode_DOM|DOMNode ...$args
462 * @suppress PhanParamSignatureMismatch
464 public function virtualImplode( $sep, ...$args ) {
468 foreach ( $args as $root ) {
469 if ( $root instanceof PPNode_DOM
) {
472 if ( !is_array( $root ) && !( $root instanceof DOMNodeList
) ) {
475 foreach ( $root as $node ) {
488 * Virtual implode with brackets
489 * @param string $start
492 * @param string|PPNode_DOM|DOMNode ...$args
494 * @suppress PhanParamSignatureMismatch
496 public function virtualBracketedImplode( $start, $sep, $end, ...$args ) {
500 foreach ( $args as $root ) {
501 if ( $root instanceof PPNode_DOM
) {
504 if ( !is_array( $root ) && !( $root instanceof DOMNodeList
) ) {
507 foreach ( $root as $node ) {
520 public function __toString() {
524 public function getPDBK( $level = false ) {
525 if ( $level === false ) {
526 return $this->title
->getPrefixedDBkey();
528 return $this->titleCache
[$level] ??
false;
535 public function getArguments() {
542 public function getNumberedArguments() {
549 public function getNamedArguments() {
554 * Returns true if there are no arguments in this frame
558 public function isEmpty() {
563 * @param int|string $name
564 * @return bool Always false in this implementation.
566 public function getArgument( $name ) {
571 * Returns true if the infinite loop check is OK, false if a loop is detected
573 * @param Title $title
576 public function loopCheck( $title ) {
577 return !isset( $this->loopCheckHash
[$title->getPrefixedDBkey()] );
581 * Return true if the frame is a template frame
585 public function isTemplate() {
590 * Get a title of frame
594 public function getTitle() {
599 * Set the volatile flag
603 public function setVolatile( $flag = true ) {
604 $this->volatile
= $flag;
608 * Get the volatile flag
612 public function isVolatile() {
613 return $this->volatile
;
621 public function setTTL( $ttl ) {
622 if ( $ttl !== null && ( $this->ttl
=== null ||
$ttl < $this->ttl
) ) {
632 public function getTTL() {