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
22 use MediaWiki\Parser\Parser
;
23 use MediaWiki\Title\Title
;
26 * An expansion frame, used as a context to expand the result of preprocessToObj()
29 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
30 class PPFrame_Hash
implements PPFrame
{
48 * @var (string|false)[]
53 * Hashtable listing templates which are disallowed for expansion in this frame,
54 * having been encountered previously in parent frames.
57 public $loopCheckHash;
60 * Recursion depth of this frame, top = 0
61 * Note that this is NOT the same as expansion depth in expand()
67 private $volatile = false;
74 protected $childExpansionCache;
78 private $maxPPNodeCount;
82 private $maxPPExpandDepth;
85 * @param Preprocessor $preprocessor The parent preprocessor
87 public function __construct( $preprocessor ) {
88 $this->preprocessor
= $preprocessor;
89 $this->parser
= $preprocessor->parser
;
90 $this->title
= $this->parser
->getTitle();
91 $this->maxPPNodeCount
= $this->parser
->getOptions()->getMaxPPNodeCount();
92 $this->maxPPExpandDepth
= $this->parser
->getOptions()->getMaxPPExpandDepth();
93 $this->titleCache
= [ $this->title ?
$this->title
->getPrefixedDBkey() : false ];
94 $this->loopCheckHash
= [];
96 $this->childExpansionCache
= [];
100 * Create a new child frame
101 * $args is optionally a multi-root PPNode or array containing the template arguments
103 * @param PPNode[]|false|PPNode_Hash_Array $args
104 * @param Title|false $title
105 * @param int $indexOffset
106 * @return PPTemplateFrame_Hash
108 public function newChild( $args = false, $title = false, $indexOffset = 0 ) {
111 if ( $title === false ) {
112 $title = $this->title
;
114 if ( $args !== false ) {
115 if ( $args instanceof PPNode_Hash_Array
) {
116 $args = $args->value
;
117 } elseif ( !is_array( $args ) ) {
118 throw new InvalidArgumentException( __METHOD__
. ': $args must be array or PPNode_Hash_Array' );
120 foreach ( $args as $arg ) {
121 $bits = $arg->splitArg();
122 if ( $bits['index'] !== '' ) {
123 // Numbered parameter
124 $index = $bits['index'] - $indexOffset;
125 if ( isset( $namedArgs[$index] ) ||
isset( $numberedArgs[$index] ) ) {
126 $this->parser
->getOutput()->addWarningMsg(
127 'duplicate-args-warning',
128 Message
::plaintextParam( (string)$this->title
),
129 Message
::plaintextParam( (string)$title ),
130 Message
::numParam( $index )
132 $this->parser
->addTrackingCategory( 'duplicate-args-category' );
134 $numberedArgs[$index] = $bits['value'];
135 unset( $namedArgs[$index] );
138 $name = trim( $this->expand( $bits['name'], PPFrame
::STRIP_COMMENTS
) );
139 if ( isset( $namedArgs[$name] ) ||
isset( $numberedArgs[$name] ) ) {
140 $this->parser
->getOutput()->addWarningMsg(
141 'duplicate-args-warning',
142 Message
::plaintextParam( (string)$this->title
),
143 Message
::plaintextParam( (string)$title ),
144 Message
::plaintextParam( $name )
146 $this->parser
->addTrackingCategory( 'duplicate-args-category' );
148 $namedArgs[$name] = $bits['value'];
149 unset( $numberedArgs[$name] );
153 return new PPTemplateFrame_Hash( $this->preprocessor
, $this, $numberedArgs, $namedArgs, $title );
157 * @param string|int $key
158 * @param string|PPNode $root
162 public function cachedExpand( $key, $root, $flags = 0 ) {
163 // we don't have a parent, so we don't have a cache
164 return $this->expand( $root, $flags );
168 * @param string|PPNode $root
172 public function expand( $root, $flags = 0 ) {
173 static $expansionDepth = 0;
174 if ( is_string( $root ) ) {
178 if ( ++
$this->parser
->mPPNodeCount
> $this->maxPPNodeCount
) {
179 $this->parser
->limitationWarn( 'node-count-exceeded',
180 $this->parser
->mPPNodeCount
,
181 $this->maxPPNodeCount
183 return '<span class="error">Node-count limit exceeded</span>';
185 if ( $expansionDepth > $this->maxPPExpandDepth
) {
186 $this->parser
->limitationWarn( 'expansion-depth-exceeded',
188 $this->maxPPExpandDepth
190 return '<span class="error">Expansion depth limit exceeded</span>';
193 if ( $expansionDepth > $this->parser
->mHighestExpansionDepth
) {
194 $this->parser
->mHighestExpansionDepth
= $expansionDepth;
197 $outStack = [ '', '' ];
198 $iteratorStack = [ false, $root ];
199 $indexStack = [ 0, 0 ];
201 while ( count( $iteratorStack ) > 1 ) {
202 $level = count( $outStack ) - 1;
203 $iteratorNode =& $iteratorStack[$level];
204 $out =& $outStack[$level];
205 $index =& $indexStack[$level];
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 PPNode_Hash_Array
) {
217 if ( $index >= $iteratorNode->getLength() ) {
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 $newIterator = false;
233 $contextName = false;
234 $contextChildren = false;
236 if ( $contextNode === false ) {
238 } elseif ( is_string( $contextNode ) ) {
239 $out .= $contextNode;
240 } elseif ( $contextNode instanceof PPNode_Hash_Array
) {
241 $newIterator = $contextNode;
242 } elseif ( $contextNode instanceof PPNode_Hash_Attr
) {
244 } elseif ( $contextNode instanceof PPNode_Hash_Text
) {
245 $out .= $contextNode->value
;
246 } elseif ( $contextNode instanceof PPNode_Hash_Tree
) {
247 $contextName = $contextNode->name
;
248 $contextChildren = $contextNode->getRawChildren();
249 } elseif ( is_array( $contextNode ) ) {
250 // Node descriptor array
251 if ( count( $contextNode ) !== 2 ) {
252 throw new RuntimeException( __METHOD__
.
253 ': found an array where a node descriptor should be' );
255 [ $contextName, $contextChildren ] = $contextNode;
257 throw new RuntimeException( __METHOD__
. ': Invalid parameter type' );
260 // Handle node descriptor array or tree object
261 if ( $contextName === false ) {
262 // Not a node, already handled above
263 } elseif ( $contextName[0] === '@' ) {
264 // Attribute: no output
265 } elseif ( $contextName === 'template' ) {
266 # Double-brace expansion
267 $bits = PPNode_Hash_Tree
::splitRawTemplate( $contextChildren );
268 if ( $flags & PPFrame
::NO_TEMPLATES
) {
269 $newIterator = $this->virtualBracketedImplode(
275 $ret = $this->parser
->braceSubstitution( $bits, $this );
276 if ( isset( $ret['object'] ) ) {
277 $newIterator = $ret['object'];
279 $out .= $ret['text'];
282 } elseif ( $contextName === 'tplarg' ) {
283 # Triple-brace expansion
284 $bits = PPNode_Hash_Tree
::splitRawTemplate( $contextChildren );
285 if ( $flags & PPFrame
::NO_ARGS
) {
286 $newIterator = $this->virtualBracketedImplode(
292 $ret = $this->parser
->argSubstitution( $bits, $this );
293 if ( isset( $ret['object'] ) ) {
294 $newIterator = $ret['object'];
296 $out .= $ret['text'];
299 } elseif ( $contextName === 'comment' ) {
301 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
302 # Not in RECOVER_COMMENTS mode (msgnw) though.
303 if ( ( $this->parser
->getOutputType() === Parser
::OT_HTML
304 ||
( $this->parser
->getOutputType() === Parser
::OT_PREPROCESS
&&
305 $this->parser
->getOptions()->getRemoveComments() )
306 ||
( $flags & PPFrame
::STRIP_COMMENTS
)
307 ) && !( $flags & PPFrame
::RECOVER_COMMENTS
)
311 $this->parser
->getOutputType() === Parser
::OT_WIKI
&&
312 !( $flags & PPFrame
::RECOVER_COMMENTS
)
314 # Add a strip marker in PST mode so that pstPass2() can
315 # run some old-fashioned regexes on the result.
316 # Not in RECOVER_COMMENTS mode (extractSections) though.
317 $out .= $this->parser
->insertStripItem( $contextChildren[0] );
319 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
320 $out .= $contextChildren[0];
322 } elseif ( $contextName === 'ignore' ) {
323 # Output suppression used by <includeonly> etc.
324 # OT_WIKI will only respect <ignore> in substed templates.
325 # The other output types respect it unless NO_IGNORE is set.
326 # extractSections() sets NO_IGNORE and so never respects it.
327 if ( ( !isset( $this->parent
) && $this->parser
->getOutputType() === Parser
::OT_WIKI
)
328 ||
( $flags & PPFrame
::NO_IGNORE
)
330 $out .= $contextChildren[0];
334 } elseif ( $contextName === 'ext' ) {
336 $bits = PPNode_Hash_Tree
::splitRawExt( $contextChildren ) +
337 [ 'attr' => null, 'inner' => null, 'close' => null ];
338 if ( $flags & PPFrame
::NO_TAGS
) {
339 $s = '<' . $bits['name']->getFirstChild()->value
;
340 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
341 if ( $bits['attr'] ) {
342 $s .= $bits['attr']->getFirstChild()->value
;
344 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
345 if ( $bits['inner'] ) {
346 $s .= '>' . $bits['inner']->getFirstChild()->value
;
347 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
348 if ( $bits['close'] ) {
349 $s .= $bits['close']->getFirstChild()->value
;
356 $out .= $this->parser
->extensionSubstitution( $bits, $this,
357 (bool)( $flags & PPFrame
::PROCESS_NOWIKI
) );
359 } elseif ( $contextName === 'h' ) {
361 if ( $this->parser
->getOutputType() === Parser
::OT_HTML
) {
362 # Expand immediately and insert heading index marker
363 $s = $this->expand( $contextChildren, $flags );
364 $bits = PPNode_Hash_Tree
::splitRawHeading( $contextChildren );
365 $titleText = $this->title
->getPrefixedDBkey();
366 $this->parser
->mHeadings
[] = [ $titleText, $bits['i'] ];
367 $serial = count( $this->parser
->mHeadings
) - 1;
368 $marker = Parser
::MARKER_PREFIX
. "-h-$serial-" . Parser
::MARKER_SUFFIX
;
369 $s = substr( $s, 0, $bits['level'] ) . $marker . substr( $s, $bits['level'] );
370 $this->parser
->getStripState()->addGeneral( $marker, '' );
373 # Expand in virtual stack
374 $newIterator = $contextChildren;
377 # Generic recursive expansion
378 $newIterator = $contextChildren;
381 if ( $newIterator !== false ) {
383 $iteratorStack[] = $newIterator;
385 } elseif ( $iteratorStack[$level] === false ) {
386 // Return accumulated value to parent
387 // With tail recursion
388 while ( $iteratorStack[$level] === false && $level > 0 ) {
389 $outStack[$level - 1] .= $out;
390 array_pop( $outStack );
391 array_pop( $iteratorStack );
392 array_pop( $indexStack );
404 * @param string|PPNode ...$args
407 public function implodeWithFlags( $sep, $flags, ...$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, $flags );
430 * Implode with no flags specified
431 * This previously called implodeWithFlags but has now been inlined to reduce stack depth
433 * @param string|PPNode ...$args
436 public function implode( $sep, ...$args ) {
439 foreach ( $args as $root ) {
440 if ( $root instanceof PPNode_Hash_Array
) {
441 $root = $root->value
;
443 if ( !is_array( $root ) ) {
446 foreach ( $root as $node ) {
452 $s .= $this->expand( $node );
459 * Makes an object that, when expand()ed, will be the same as one obtained
463 * @param string|PPNode ...$args
464 * @return PPNode_Hash_Array
466 public function virtualImplode( $sep, ...$args ) {
470 foreach ( $args as $root ) {
471 if ( $root instanceof PPNode_Hash_Array
) {
472 $root = $root->value
;
474 if ( !is_array( $root ) ) {
477 foreach ( $root as $node ) {
486 return new PPNode_Hash_Array( $out );
490 * Virtual implode with brackets
492 * @param string $start
495 * @param string|PPNode ...$args
496 * @return PPNode_Hash_Array
498 public function virtualBracketedImplode( $start, $sep, $end, ...$args ) {
502 foreach ( $args as $root ) {
503 if ( $root instanceof PPNode_Hash_Array
) {
504 $root = $root->value
;
506 if ( !is_array( $root ) ) {
509 foreach ( $root as $node ) {
519 return new PPNode_Hash_Array( $out );
522 public function __toString() {
527 * @param string|false $level
528 * @return false|string
530 public function getPDBK( $level = false ) {
531 if ( $level === false ) {
532 return $this->title
->getPrefixedDBkey();
534 return $this->titleCache
[$level] ??
false;
541 public function getArguments() {
548 public function getNumberedArguments() {
555 public function getNamedArguments() {
560 * Returns true if there are no arguments in this frame
564 public function isEmpty() {
569 * @param int|string $name
570 * @return bool Always false in this implementation.
572 public function getArgument( $name ) {
577 * Returns true if the infinite loop check is OK, false if a loop is detected
579 * @param Title $title
583 public function loopCheck( $title ) {
584 return !isset( $this->loopCheckHash
[$title->getPrefixedDBkey()] );
588 * Return true if the frame is a template frame
592 public function isTemplate() {
597 * Get a title of frame
601 public function getTitle() {
606 * Set the volatile flag
610 public function setVolatile( $flag = true ) {
611 $this->volatile
= $flag;
615 * Get the volatile flag
619 public function isVolatile() {
620 return $this->volatile
;
626 public function setTTL( $ttl ) {
627 if ( $ttl !== null && ( $this->ttl
=== null ||
$ttl < $this->ttl
) ) {
635 public function getTTL() {