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_Hash
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
->getTitle();
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 array|bool|PPNode_Hash_Array $args
84 * @param Title|bool $title
85 * @param int $indexOffset
87 * @return PPTemplateFrame_Hash
89 public function newChild( $args = false, $title = false, $indexOffset = 0 ) {
92 if ( $title === false ) {
93 $title = $this->title
;
95 if ( $args !== false ) {
96 if ( $args instanceof PPNode_Hash_Array
) {
98 } elseif ( !is_array( $args ) ) {
99 throw new MWException( __METHOD__
. ': $args must be array or PPNode_Hash_Array' );
101 foreach ( $args as $arg ) {
102 $bits = $arg->splitArg();
103 if ( $bits['index'] !== '' ) {
104 // Numbered parameter
105 $index = $bits['index'] - $indexOffset;
106 if ( isset( $namedArgs[$index] ) ||
isset( $numberedArgs[$index] ) ) {
107 $this->parser
->getOutput()->addWarning( wfMessage( 'duplicate-args-warning',
108 wfEscapeWikiText( $this->title
),
109 wfEscapeWikiText( $title ),
110 wfEscapeWikiText( $index ) )->text() );
111 $this->parser
->addTrackingCategory( 'duplicate-args-category' );
113 $numberedArgs[$index] = $bits['value'];
114 unset( $namedArgs[$index] );
117 $name = trim( $this->expand( $bits['name'], PPFrame
::STRIP_COMMENTS
) );
118 if ( isset( $namedArgs[$name] ) ||
isset( $numberedArgs[$name] ) ) {
119 $this->parser
->getOutput()->addWarning( wfMessage( 'duplicate-args-warning',
120 wfEscapeWikiText( $this->title
),
121 wfEscapeWikiText( $title ),
122 wfEscapeWikiText( $name ) )->text() );
123 $this->parser
->addTrackingCategory( 'duplicate-args-category' );
125 $namedArgs[$name] = $bits['value'];
126 unset( $numberedArgs[$name] );
130 return new PPTemplateFrame_Hash( $this->preprocessor
, $this, $numberedArgs, $namedArgs, $title );
134 * @throws MWException
135 * @param string|int $key
136 * @param string|PPNode $root
140 public function cachedExpand( $key, $root, $flags = 0 ) {
141 // we don't have a parent, so we don't have a cache
142 return $this->expand( $root, $flags );
146 * @throws MWException
147 * @param string|PPNode $root
151 public function expand( $root, $flags = 0 ) {
152 static $expansionDepth = 0;
153 if ( is_string( $root ) ) {
157 if ( ++
$this->parser
->mPPNodeCount
> $this->parser
->mOptions
->getMaxPPNodeCount() ) {
158 $this->parser
->limitationWarn( 'node-count-exceeded',
159 $this->parser
->mPPNodeCount
,
160 $this->parser
->mOptions
->getMaxPPNodeCount()
162 return '<span class="error">Node-count limit exceeded</span>';
164 if ( $expansionDepth > $this->parser
->mOptions
->getMaxPPExpandDepth() ) {
165 $this->parser
->limitationWarn( 'expansion-depth-exceeded',
167 $this->parser
->mOptions
->getMaxPPExpandDepth()
169 return '<span class="error">Expansion depth limit exceeded</span>';
172 if ( $expansionDepth > $this->parser
->mHighestExpansionDepth
) {
173 $this->parser
->mHighestExpansionDepth
= $expansionDepth;
176 $outStack = [ '', '' ];
177 $iteratorStack = [ false, $root ];
178 $indexStack = [ 0, 0 ];
180 while ( count( $iteratorStack ) > 1 ) {
181 $level = count( $outStack ) - 1;
182 $iteratorNode =& $iteratorStack[$level];
183 $out =& $outStack[$level];
184 $index =& $indexStack[$level];
186 if ( is_array( $iteratorNode ) ) {
187 if ( $index >= count( $iteratorNode ) ) {
188 // All done with this iterator
189 $iteratorStack[$level] = false;
190 $contextNode = false;
192 $contextNode = $iteratorNode[$index];
195 } elseif ( $iteratorNode instanceof PPNode_Hash_Array
) {
196 if ( $index >= $iteratorNode->getLength() ) {
197 // All done with this iterator
198 $iteratorStack[$level] = false;
199 $contextNode = false;
201 $contextNode = $iteratorNode->item( $index );
205 // Copy to $contextNode and then delete from iterator stack,
206 // because this is not an iterator but we do have to execute it once
207 $contextNode = $iteratorStack[$level];
208 $iteratorStack[$level] = false;
211 $newIterator = false;
212 $contextName = false;
213 $contextChildren = false;
215 if ( $contextNode === false ) {
217 } elseif ( is_string( $contextNode ) ) {
218 $out .= $contextNode;
219 } elseif ( $contextNode instanceof PPNode_Hash_Array
) {
220 $newIterator = $contextNode;
221 } elseif ( $contextNode instanceof PPNode_Hash_Attr
) {
223 } elseif ( $contextNode instanceof PPNode_Hash_Text
) {
224 $out .= $contextNode->value
;
225 } elseif ( $contextNode instanceof PPNode_Hash_Tree
) {
226 $contextName = $contextNode->name
;
227 $contextChildren = $contextNode->getRawChildren();
228 } elseif ( is_array( $contextNode ) ) {
229 // Node descriptor array
230 if ( count( $contextNode ) !== 2 ) {
231 throw new MWException( __METHOD__
.
232 ': found an array where a node descriptor should be' );
234 list( $contextName, $contextChildren ) = $contextNode;
236 throw new MWException( __METHOD__
. ': Invalid parameter type' );
239 // Handle node descriptor array or tree object
240 if ( $contextName === false ) {
241 // Not a node, already handled above
242 } elseif ( $contextName[0] === '@' ) {
243 // Attribute: no output
244 } elseif ( $contextName === 'template' ) {
245 # Double-brace expansion
246 $bits = PPNode_Hash_Tree
::splitRawTemplate( $contextChildren );
247 if ( $flags & PPFrame
::NO_TEMPLATES
) {
248 $newIterator = $this->virtualBracketedImplode(
254 $ret = $this->parser
->braceSubstitution( $bits, $this );
255 if ( isset( $ret['object'] ) ) {
256 $newIterator = $ret['object'];
258 $out .= $ret['text'];
261 } elseif ( $contextName === 'tplarg' ) {
262 # Triple-brace expansion
263 $bits = PPNode_Hash_Tree
::splitRawTemplate( $contextChildren );
264 if ( $flags & PPFrame
::NO_ARGS
) {
265 $newIterator = $this->virtualBracketedImplode(
271 $ret = $this->parser
->argSubstitution( $bits, $this );
272 if ( isset( $ret['object'] ) ) {
273 $newIterator = $ret['object'];
275 $out .= $ret['text'];
278 } elseif ( $contextName === 'comment' ) {
280 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
281 # Not in RECOVER_COMMENTS mode (msgnw) though.
282 if ( ( $this->parser
->ot
['html']
283 ||
( $this->parser
->ot
['pre'] && $this->parser
->mOptions
->getRemoveComments() )
284 ||
( $flags & PPFrame
::STRIP_COMMENTS
)
285 ) && !( $flags & PPFrame
::RECOVER_COMMENTS
)
288 } elseif ( $this->parser
->ot
['wiki'] && !( $flags & PPFrame
::RECOVER_COMMENTS
) ) {
289 # Add a strip marker in PST mode so that pstPass2() can
290 # run some old-fashioned regexes on the result.
291 # Not in RECOVER_COMMENTS mode (extractSections) though.
292 $out .= $this->parser
->insertStripItem( $contextChildren[0] );
294 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
295 $out .= $contextChildren[0];
297 } elseif ( $contextName === 'ignore' ) {
298 # Output suppression used by <includeonly> etc.
299 # OT_WIKI will only respect <ignore> in substed templates.
300 # The other output types respect it unless NO_IGNORE is set.
301 # extractSections() sets NO_IGNORE and so never respects it.
302 if ( ( !isset( $this->parent
) && $this->parser
->ot
['wiki'] )
303 ||
( $flags & PPFrame
::NO_IGNORE
)
305 $out .= $contextChildren[0];
309 } elseif ( $contextName === 'ext' ) {
311 $bits = PPNode_Hash_Tree
::splitRawExt( $contextChildren ) +
312 [ 'attr' => null, 'inner' => null, 'close' => null ];
313 if ( $flags & PPFrame
::NO_TAGS
) {
314 $s = '<' . $bits['name']->getFirstChild()->value
;
315 if ( $bits['attr'] ) {
316 $s .= $bits['attr']->getFirstChild()->value
;
318 if ( $bits['inner'] ) {
319 $s .= '>' . $bits['inner']->getFirstChild()->value
;
320 if ( $bits['close'] ) {
321 $s .= $bits['close']->getFirstChild()->value
;
328 $out .= $this->parser
->extensionSubstitution( $bits, $this );
330 } elseif ( $contextName === 'h' ) {
332 if ( $this->parser
->ot
['html'] ) {
333 # Expand immediately and insert heading index marker
334 $s = $this->expand( $contextChildren, $flags );
335 $bits = PPNode_Hash_Tree
::splitRawHeading( $contextChildren );
336 $titleText = $this->title
->getPrefixedDBkey();
337 $this->parser
->mHeadings
[] = [ $titleText, $bits['i'] ];
338 $serial = count( $this->parser
->mHeadings
) - 1;
339 $marker = Parser
::MARKER_PREFIX
. "-h-$serial-" . Parser
::MARKER_SUFFIX
;
340 $s = substr( $s, 0, $bits['level'] ) . $marker . substr( $s, $bits['level'] );
341 $this->parser
->mStripState
->addGeneral( $marker, '' );
344 # Expand in virtual stack
345 $newIterator = $contextChildren;
348 # Generic recursive expansion
349 $newIterator = $contextChildren;
352 if ( $newIterator !== false ) {
354 $iteratorStack[] = $newIterator;
356 } elseif ( $iteratorStack[$level] === false ) {
357 // Return accumulated value to parent
358 // With tail recursion
359 while ( $iteratorStack[$level] === false && $level > 0 ) {
360 $outStack[$level - 1] .= $out;
361 array_pop( $outStack );
362 array_pop( $iteratorStack );
363 array_pop( $indexStack );
375 * @param string|PPNode ...$args
378 public function implodeWithFlags( $sep, $flags, ...$args ) {
381 foreach ( $args as $root ) {
382 if ( $root instanceof PPNode_Hash_Array
) {
383 $root = $root->value
;
385 if ( !is_array( $root ) ) {
388 foreach ( $root as $node ) {
394 $s .= $this->expand( $node, $flags );
401 * Implode with no flags specified
402 * This previously called implodeWithFlags but has now been inlined to reduce stack depth
404 * @param string|PPNode ...$args
407 public function implode( $sep, ...$args ) {
410 foreach ( $args as $root ) {
411 if ( $root instanceof PPNode_Hash_Array
) {
412 $root = $root->value
;
414 if ( !is_array( $root ) ) {
417 foreach ( $root as $node ) {
423 $s .= $this->expand( $node );
430 * Makes an object that, when expand()ed, will be the same as one obtained
434 * @param string|PPNode ...$args
435 * @return PPNode_Hash_Array
437 public function virtualImplode( $sep, ...$args ) {
441 foreach ( $args as $root ) {
442 if ( $root instanceof PPNode_Hash_Array
) {
443 $root = $root->value
;
445 if ( !is_array( $root ) ) {
448 foreach ( $root as $node ) {
457 return new PPNode_Hash_Array( $out );
461 * Virtual implode with brackets
463 * @param string $start
466 * @param string|PPNode ...$args
467 * @return PPNode_Hash_Array
469 public function virtualBracketedImplode( $start, $sep, $end, ...$args ) {
473 foreach ( $args as $root ) {
474 if ( $root instanceof PPNode_Hash_Array
) {
475 $root = $root->value
;
477 if ( !is_array( $root ) ) {
480 foreach ( $root as $node ) {
490 return new PPNode_Hash_Array( $out );
493 public function __toString() {
499 * @return array|bool|string
501 public function getPDBK( $level = false ) {
502 if ( $level === false ) {
503 return $this->title
->getPrefixedDBkey();
505 return $this->titleCache
[$level] ??
false;
512 public function getArguments() {
519 public function getNumberedArguments() {
526 public function getNamedArguments() {
531 * Returns true if there are no arguments in this frame
535 public function isEmpty() {
540 * @param int|string $name
541 * @return bool Always false in this implementation.
543 public function getArgument( $name ) {
548 * Returns true if the infinite loop check is OK, false if a loop is detected
550 * @param Title $title
554 public function loopCheck( $title ) {
555 return !isset( $this->loopCheckHash
[$title->getPrefixedDBkey()] );
559 * Return true if the frame is a template frame
563 public function isTemplate() {
568 * Get a title of frame
572 public function getTitle() {
577 * Set the volatile flag
581 public function setVolatile( $flag = true ) {
582 $this->volatile
= $flag;
586 * Get the volatile flag
590 public function isVolatile() {
591 return $this->volatile
;
599 public function setTTL( $ttl ) {
600 if ( $ttl !== null && ( $this->ttl
=== null ||
$ttl < $this->ttl
) ) {
610 public function getTTL() {