MediaWiki master
PPFrame_Hash.php
Go to the documentation of this file.
1<?php
8namespace MediaWiki\Parser;
9
10use InvalidArgumentException;
13use RuntimeException;
14use Stringable;
15
20// phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
21class PPFrame_Hash implements Stringable, PPFrame {
22
26 public $parser;
27
32
36 public $title;
37
42
49
55 public $depth;
56
58 private $volatile = false;
60 private $ttl = null;
61
69 private $maxPPNodeCount;
73 private $maxPPExpandDepth;
74
78 public function __construct( $preprocessor ) {
79 $this->preprocessor = $preprocessor;
80 $this->parser = $preprocessor->parser;
81 $this->title = $this->parser->getTitle();
82 $this->maxPPNodeCount = $this->parser->getOptions()->getMaxPPNodeCount();
83 $this->maxPPExpandDepth = $this->parser->getOptions()->getMaxPPExpandDepth();
84 $this->titleCache = [ $this->title ? $this->title->getPrefixedDBkey() : false ];
85 $this->loopCheckHash = [];
86 $this->depth = 0;
87 $this->childExpansionCache = [];
88 }
89
99 public function newChild( $args = false, $title = false, $indexOffset = 0 ) {
100 $namedArgs = [];
101 $numberedArgs = [];
102 if ( $title === false ) {
104 }
105 if ( $args !== false ) {
106 if ( $args instanceof PPNode_Hash_Array ) {
107 $args = $args->value;
108 } elseif ( !is_array( $args ) ) {
109 throw new InvalidArgumentException( __METHOD__ . ': $args must be array or PPNode_Hash_Array' );
110 }
111 foreach ( $args as $arg ) {
112 $bits = $arg->splitArg();
113 if ( $bits['index'] !== '' ) {
114 // Numbered parameter
115 $index = $bits['index'] - $indexOffset;
116 if ( isset( $namedArgs[$index] ) || isset( $numberedArgs[$index] ) ) {
117 $this->parser->getOutput()->addWarningMsg(
118 'duplicate-args-warning',
119 Message::plaintextParam( (string)$this->title ),
121 Message::numParam( $index )
122 );
123 $this->parser->addTrackingCategory( 'duplicate-args-category' );
124 }
125 $numberedArgs[$index] = $bits['value'];
126 unset( $namedArgs[$index] );
127 } else {
128 // Named parameter
129 $name = trim( $this->expand( $bits['name'], PPFrame::STRIP_COMMENTS ) );
130 if ( isset( $namedArgs[$name] ) || isset( $numberedArgs[$name] ) ) {
131 $this->parser->getOutput()->addWarningMsg(
132 'duplicate-args-warning',
133 Message::plaintextParam( (string)$this->title ),
136 );
137 $this->parser->addTrackingCategory( 'duplicate-args-category' );
138 }
139 $namedArgs[$name] = $bits['value'];
140 unset( $numberedArgs[$name] );
141 }
142 }
143 }
144 return new PPTemplateFrame_Hash( $this->preprocessor, $this, $numberedArgs, $namedArgs, $title );
145 }
146
153 public function cachedExpand( $key, $root, $flags = 0 ) {
154 // we don't have a parent, so we don't have a cache
155 return $this->expand( $root, $flags );
156 }
157
163 public function expand( $root, $flags = 0 ) {
164 static $expansionDepth = 0;
165 if ( is_string( $root ) ) {
166 return $root;
167 }
168
169 if ( ++$this->parser->mPPNodeCount > $this->maxPPNodeCount ) {
170 $this->parser->limitationWarn( 'node-count-exceeded',
171 $this->parser->mPPNodeCount,
172 $this->maxPPNodeCount
173 );
174 return '<span class="error">Node-count limit exceeded</span>';
175 }
176 if ( $expansionDepth > $this->maxPPExpandDepth ) {
177 $this->parser->limitationWarn( 'expansion-depth-exceeded',
178 $expansionDepth,
179 $this->maxPPExpandDepth
180 );
181 return '<span class="error">Expansion depth limit exceeded</span>';
182 }
183 ++$expansionDepth;
184 if ( $expansionDepth > $this->parser->mHighestExpansionDepth ) {
185 $this->parser->mHighestExpansionDepth = $expansionDepth;
186 }
187
188 $outStack = [ '', '' ];
189 $iteratorStack = [ false, $root ];
190 $indexStack = [ 0, 0 ];
191
192 while ( count( $iteratorStack ) > 1 ) {
193 $level = count( $outStack ) - 1;
194 $iteratorNode =& $iteratorStack[$level];
195 $out =& $outStack[$level];
196 $index =& $indexStack[$level];
197
198 if ( is_array( $iteratorNode ) ) {
199 if ( $index >= count( $iteratorNode ) ) {
200 // All done with this iterator
201 $iteratorStack[$level] = false;
202 $contextNode = false;
203 } else {
204 $contextNode = $iteratorNode[$index];
205 $index++;
206 }
207 } elseif ( $iteratorNode instanceof PPNode_Hash_Array ) {
208 if ( $index >= $iteratorNode->getLength() ) {
209 // All done with this iterator
210 $iteratorStack[$level] = false;
211 $contextNode = false;
212 } else {
213 $contextNode = $iteratorNode->item( $index );
214 $index++;
215 }
216 } else {
217 // Copy to $contextNode and then delete from iterator stack,
218 // because this is not an iterator but we do have to execute it once
219 $contextNode = $iteratorStack[$level];
220 $iteratorStack[$level] = false;
221 }
222
223 $newIterator = false;
224 $contextName = false;
225 $contextChildren = false;
226
227 if ( $contextNode === false ) {
228 // nothing to do
229 } elseif ( is_string( $contextNode ) ) {
230 $out .= $contextNode;
231 } elseif ( $contextNode instanceof PPNode_Hash_Array ) {
232 $newIterator = $contextNode;
233 } elseif ( $contextNode instanceof PPNode_Hash_Attr ) {
234 // No output
235 } elseif ( $contextNode instanceof PPNode_Hash_Text ) {
236 $out .= $contextNode->value;
237 } elseif ( $contextNode instanceof PPNode_Hash_Tree ) {
238 $contextName = $contextNode->name;
239 $contextChildren = $contextNode->getRawChildren();
240 } elseif ( is_array( $contextNode ) ) {
241 // Node descriptor array
242 if ( count( $contextNode ) !== 2 ) {
243 throw new RuntimeException( __METHOD__ .
244 ': found an array where a node descriptor should be' );
245 }
246 [ $contextName, $contextChildren ] = $contextNode;
247 } else {
248 throw new RuntimeException( __METHOD__ . ': Invalid parameter type' );
249 }
250
251 // Handle node descriptor array or tree object
252 if ( $contextName === false ) {
253 // Not a node, already handled above
254 } elseif ( $contextName[0] === '@' ) {
255 // Attribute: no output
256 } elseif ( $contextName === 'template' ) {
257 # Double-brace expansion
258 $bits = PPNode_Hash_Tree::splitRawTemplate( $contextChildren );
259 if ( $flags & PPFrame::NO_TEMPLATES ) {
260 $newIterator = $this->virtualBracketedImplode(
261 '{{', '|', '}}',
262 $bits['title'],
263 $bits['parts']
264 );
265 } else {
266 $ret = $this->parser->braceSubstitution( $bits, $this );
267 if ( isset( $ret['object'] ) ) {
268 $newIterator = $ret['object'];
269 } else {
270 $out .= $ret['text'];
271 }
272 }
273 } elseif ( $contextName === 'tplarg' ) {
274 # Triple-brace expansion
275 $bits = PPNode_Hash_Tree::splitRawTemplate( $contextChildren );
276 if ( $flags & PPFrame::NO_ARGS ) {
277 $newIterator = $this->virtualBracketedImplode(
278 '{{{', '|', '}}}',
279 $bits['title'],
280 $bits['parts']
281 );
282 } else {
283 $ret = $this->parser->argSubstitution( $bits, $this );
284 if ( isset( $ret['object'] ) ) {
285 $newIterator = $ret['object'];
286 } else {
287 $out .= $ret['text'];
288 }
289 }
290 } elseif ( $contextName === 'comment' ) {
291 # HTML-style comment
292 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
293 # Not in RECOVER_COMMENTS mode (msgnw) though.
294 if ( ( $this->parser->getOutputType() === Parser::OT_HTML
295 || ( $this->parser->getOutputType() === Parser::OT_PREPROCESS &&
296 $this->parser->getOptions()->getRemoveComments() )
297 || ( $flags & PPFrame::STRIP_COMMENTS )
298 ) && !( $flags & PPFrame::RECOVER_COMMENTS )
299 ) {
300 $out .= '';
301 } elseif (
302 $this->parser->getOutputType() === Parser::OT_WIKI &&
303 !( $flags & PPFrame::RECOVER_COMMENTS )
304 ) {
305 # Add a strip marker in PST mode so that pstPass2() can
306 # run some old-fashioned regexes on the result.
307 # Not in RECOVER_COMMENTS mode (extractSections) though.
308 $out .= $this->parser->insertStripItem( $contextChildren[0] );
309 } else {
310 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
311 $out .= $contextChildren[0];
312 }
313 } elseif ( $contextName === 'ignore' ) {
314 # Output suppression used by <includeonly> etc.
315 # OT_WIKI will only respect <ignore> in substed templates.
316 # The other output types respect it unless NO_IGNORE is set.
317 # extractSections() sets NO_IGNORE and so never respects it.
318 if ( ( !isset( $this->parent ) && $this->parser->getOutputType() === Parser::OT_WIKI )
319 || ( $flags & PPFrame::NO_IGNORE )
320 ) {
321 $out .= $contextChildren[0];
322 } else {
323 // $out .= '';
324 }
325 } elseif ( $contextName === 'ext' ) {
326 # Extension tag
327 $bits = PPNode_Hash_Tree::splitRawExt( $contextChildren ) +
328 [ 'attr' => null, 'inner' => null, 'close' => null ];
329 if ( $flags & PPFrame::NO_TAGS ) {
330 $s = '<' . $bits['name']->getFirstChild()->value;
331 if ( $bits['attr'] ) {
332 $s .= $bits['attr']->getFirstChild()->value;
333 }
334 if ( $bits['inner'] ) {
335 $s .= '>' . $bits['inner']->getFirstChild()->value;
336 if ( $bits['close'] ) {
337 $s .= $bits['close']->getFirstChild()->value;
338 }
339 } else {
340 $s .= '/>';
341 }
342 $out .= $s;
343 } else {
344 $out .= $this->parser->extensionSubstitution( $bits, $this,
345 (bool)( $flags & PPFrame::PROCESS_NOWIKI ) );
346 }
347 } elseif ( $contextName === 'h' ) {
348 # Heading
349 if ( $this->parser->getOutputType() === Parser::OT_HTML ) {
350 # Expand immediately and insert heading index marker
351 $s = $this->expand( $contextChildren, $flags );
352 $bits = PPNode_Hash_Tree::splitRawHeading( $contextChildren );
353 $titleText = $this->title->getPrefixedDBkey();
354 $this->parser->mHeadings[] = [ $titleText, $bits['i'] ];
355 $serial = count( $this->parser->mHeadings ) - 1;
356 $marker = Parser::MARKER_PREFIX . "-h-$serial-" . Parser::MARKER_SUFFIX;
357 $s = substr( $s, 0, $bits['level'] ) . $marker . substr( $s, $bits['level'] );
358 $this->parser->getStripState()->addGeneral( $marker, '' );
359 $out .= $s;
360 } else {
361 # Expand in virtual stack
362 $newIterator = $contextChildren;
363 }
364 } else {
365 # Generic recursive expansion
366 $newIterator = $contextChildren;
367 }
368
369 if ( $newIterator !== false ) {
370 $outStack[] = '';
371 $iteratorStack[] = $newIterator;
372 $indexStack[] = 0;
373 } elseif ( $iteratorStack[$level] === false ) {
374 // Return accumulated value to parent
375 // With tail recursion
376 while ( $iteratorStack[$level] === false && $level > 0 ) {
377 $outStack[$level - 1] .= $out;
378 array_pop( $outStack );
379 array_pop( $iteratorStack );
380 array_pop( $indexStack );
381 $level--;
382 }
383 }
384 }
385 --$expansionDepth;
386 return $outStack[0];
387 }
388
395 public function implodeWithFlags( $sep, $flags, ...$args ) {
396 $first = true;
397 $s = '';
398 foreach ( $args as $root ) {
399 if ( $root instanceof PPNode_Hash_Array ) {
400 $root = $root->value;
401 }
402 if ( !is_array( $root ) ) {
403 $root = [ $root ];
404 }
405 foreach ( $root as $node ) {
406 if ( $first ) {
407 $first = false;
408 } else {
409 $s .= $sep;
410 }
411 $s .= $this->expand( $node, $flags );
412 }
413 }
414 return $s;
415 }
416
424 public function implode( $sep, ...$args ) {
425 $first = true;
426 $s = '';
427 foreach ( $args as $root ) {
428 if ( $root instanceof PPNode_Hash_Array ) {
429 $root = $root->value;
430 }
431 if ( !is_array( $root ) ) {
432 $root = [ $root ];
433 }
434 foreach ( $root as $node ) {
435 if ( $first ) {
436 $first = false;
437 } else {
438 $s .= $sep;
439 }
440 $s .= $this->expand( $node );
441 }
442 }
443 return $s;
444 }
445
454 public function virtualImplode( $sep, ...$args ) {
455 $out = [];
456 $first = true;
457
458 foreach ( $args as $root ) {
459 if ( $root instanceof PPNode_Hash_Array ) {
460 $root = $root->value;
461 }
462 if ( !is_array( $root ) ) {
463 $root = [ $root ];
464 }
465 foreach ( $root as $node ) {
466 if ( $first ) {
467 $first = false;
468 } else {
469 $out[] = $sep;
470 }
471 $out[] = $node;
472 }
473 }
474 return new PPNode_Hash_Array( $out );
475 }
476
486 public function virtualBracketedImplode( $start, $sep, $end, ...$args ) {
487 $out = [ $start ];
488 $first = true;
489
490 foreach ( $args as $root ) {
491 if ( $root instanceof PPNode_Hash_Array ) {
492 $root = $root->value;
493 }
494 if ( !is_array( $root ) ) {
495 $root = [ $root ];
496 }
497 foreach ( $root as $node ) {
498 if ( $first ) {
499 $first = false;
500 } else {
501 $out[] = $sep;
502 }
503 $out[] = $node;
504 }
505 }
506 $out[] = $end;
507 return new PPNode_Hash_Array( $out );
508 }
509
510 public function __toString() {
511 return 'frame{}';
512 }
513
518 public function getPDBK( $level = false ) {
519 if ( $level === false ) {
520 return $this->title->getPrefixedDBkey();
521 } else {
522 return $this->titleCache[$level] ?? false;
523 }
524 }
525
529 public function getArguments() {
530 return [];
531 }
532
536 public function getNumberedArguments() {
537 return [];
538 }
539
543 public function getNamedArguments() {
544 return [];
545 }
546
552 public function isEmpty() {
553 return true;
554 }
555
560 public function getArgument( $name ) {
561 return false;
562 }
563
571 public function loopCheck( $title ) {
572 return !isset( $this->loopCheckHash[$title->getPrefixedDBkey()] );
573 }
574
580 public function isTemplate() {
581 return false;
582 }
583
589 public function getTitle() {
590 return $this->title;
591 }
592
598 public function setVolatile( $flag = true ) {
599 $this->volatile = $flag;
600 }
601
607 public function isVolatile() {
608 return $this->volatile;
609 }
610
615 public function setTTL( $ttl ) {
616 wfDeprecated( __METHOD__, '1.44' );
617 if ( $ttl !== null && ( $this->ttl === null || $ttl < $this->ttl ) ) {
618 $this->ttl = $ttl;
619 }
620 }
621
626 public function getTTL() {
627 wfDeprecated( __METHOD__, '1.46' );
628 return $this->ttl;
629 }
630}
631
633class_alias( PPFrame_Hash::class, 'PPFrame_Hash' );
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:144
static plaintextParam( $plaintext)
Definition Message.php:1341
An expansion frame, used as a context to expand the result of preprocessToObj()
implodeWithFlags( $sep, $flags,... $args)
setVolatile( $flag=true)
Set the volatile flag.
loopCheck( $title)
Returns true if the infinite loop check is OK, false if a loop is detected.
int $depth
Recursion depth of this frame, top = 0 Note that this is NOT the same as expansion depth in expand()
true[] $loopCheckHash
Hashtable listing templates which are disallowed for expansion in this frame, having been encountered...
isEmpty()
Returns true if there are no arguments in this frame.
cachedExpand( $key, $root, $flags=0)
virtualImplode( $sep,... $args)
Makes an object that, when expand()ed, will be the same as one obtained with implode()
implode( $sep,... $args)
Implode with no flags specified This previously called implodeWithFlags but has now been inlined to r...
getTitle()
Get a title of frame.
isTemplate()
Return true if the frame is a template frame.
isVolatile()
Get the volatile flag.
virtualBracketedImplode( $start, $sep, $end,... $args)
Virtual implode with brackets.
newChild( $args=false, $title=false, $indexOffset=0)
Create a new child frame $args is optionally a multi-root PPNode or array containing the template arg...
static splitRawHeading(array $children)
Like splitHeading() but for a raw child array.
static splitRawExt(array $children)
Like splitExt() but for a raw child array.
static splitRawTemplate(array $children)
Like splitTemplate() but for a raw child array.
Expansion frame with template arguments.
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:135
const OT_PREPROCESS
Output type: like Parser::preprocess()
Definition Parser.php:177
const OT_WIKI
Output type: like Parser::preSaveTransform()
Definition Parser.php:175
const OT_HTML
Output type: like Parser::parse()
Definition Parser.php:173
Represents a title within MediaWiki.
Definition Title.php:69
getPrefixedDBkey()
Get the prefixed database key form.
Definition Title.php:1845