MediaWiki master
PPFrame_Hash.php
Go to the documentation of this file.
1<?php
22namespace MediaWiki\Parser;
23
24use InvalidArgumentException;
27use RuntimeException;
28use Stringable;
29
34// phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
35class PPFrame_Hash implements Stringable, PPFrame {
36
40 public $parser;
41
46
50 public $title;
51
56
63
69 public $depth;
70
72 private $volatile = false;
74 private $ttl = null;
75
83 private $maxPPNodeCount;
87 private $maxPPExpandDepth;
88
92 public function __construct( $preprocessor ) {
93 $this->preprocessor = $preprocessor;
94 $this->parser = $preprocessor->parser;
95 $this->title = $this->parser->getTitle();
96 $this->maxPPNodeCount = $this->parser->getOptions()->getMaxPPNodeCount();
97 $this->maxPPExpandDepth = $this->parser->getOptions()->getMaxPPExpandDepth();
98 $this->titleCache = [ $this->title ? $this->title->getPrefixedDBkey() : false ];
99 $this->loopCheckHash = [];
100 $this->depth = 0;
101 $this->childExpansionCache = [];
102 }
103
113 public function newChild( $args = false, $title = false, $indexOffset = 0 ) {
114 $namedArgs = [];
115 $numberedArgs = [];
116 if ( $title === false ) {
118 }
119 if ( $args !== false ) {
120 if ( $args instanceof PPNode_Hash_Array ) {
121 $args = $args->value;
122 } elseif ( !is_array( $args ) ) {
123 throw new InvalidArgumentException( __METHOD__ . ': $args must be array or PPNode_Hash_Array' );
124 }
125 foreach ( $args as $arg ) {
126 $bits = $arg->splitArg();
127 if ( $bits['index'] !== '' ) {
128 // Numbered parameter
129 $index = $bits['index'] - $indexOffset;
130 if ( isset( $namedArgs[$index] ) || isset( $numberedArgs[$index] ) ) {
131 $this->parser->getOutput()->addWarningMsg(
132 'duplicate-args-warning',
133 Message::plaintextParam( (string)$this->title ),
135 Message::numParam( $index )
136 );
137 $this->parser->addTrackingCategory( 'duplicate-args-category' );
138 }
139 $numberedArgs[$index] = $bits['value'];
140 unset( $namedArgs[$index] );
141 } else {
142 // Named parameter
143 $name = trim( $this->expand( $bits['name'], PPFrame::STRIP_COMMENTS ) );
144 if ( isset( $namedArgs[$name] ) || isset( $numberedArgs[$name] ) ) {
145 $this->parser->getOutput()->addWarningMsg(
146 'duplicate-args-warning',
147 Message::plaintextParam( (string)$this->title ),
150 );
151 $this->parser->addTrackingCategory( 'duplicate-args-category' );
152 }
153 $namedArgs[$name] = $bits['value'];
154 unset( $numberedArgs[$name] );
155 }
156 }
157 }
158 return new PPTemplateFrame_Hash( $this->preprocessor, $this, $numberedArgs, $namedArgs, $title );
159 }
160
167 public function cachedExpand( $key, $root, $flags = 0 ) {
168 // we don't have a parent, so we don't have a cache
169 return $this->expand( $root, $flags );
170 }
171
177 public function expand( $root, $flags = 0 ) {
178 static $expansionDepth = 0;
179 if ( is_string( $root ) ) {
180 return $root;
181 }
182
183 if ( ++$this->parser->mPPNodeCount > $this->maxPPNodeCount ) {
184 $this->parser->limitationWarn( 'node-count-exceeded',
185 $this->parser->mPPNodeCount,
186 $this->maxPPNodeCount
187 );
188 return '<span class="error">Node-count limit exceeded</span>';
189 }
190 if ( $expansionDepth > $this->maxPPExpandDepth ) {
191 $this->parser->limitationWarn( 'expansion-depth-exceeded',
192 $expansionDepth,
193 $this->maxPPExpandDepth
194 );
195 return '<span class="error">Expansion depth limit exceeded</span>';
196 }
197 ++$expansionDepth;
198 if ( $expansionDepth > $this->parser->mHighestExpansionDepth ) {
199 $this->parser->mHighestExpansionDepth = $expansionDepth;
200 }
201
202 $outStack = [ '', '' ];
203 $iteratorStack = [ false, $root ];
204 $indexStack = [ 0, 0 ];
205
206 while ( count( $iteratorStack ) > 1 ) {
207 $level = count( $outStack ) - 1;
208 $iteratorNode =& $iteratorStack[$level];
209 $out =& $outStack[$level];
210 $index =& $indexStack[$level];
211
212 if ( is_array( $iteratorNode ) ) {
213 if ( $index >= count( $iteratorNode ) ) {
214 // All done with this iterator
215 $iteratorStack[$level] = false;
216 $contextNode = false;
217 } else {
218 $contextNode = $iteratorNode[$index];
219 $index++;
220 }
221 } elseif ( $iteratorNode instanceof PPNode_Hash_Array ) {
222 if ( $index >= $iteratorNode->getLength() ) {
223 // All done with this iterator
224 $iteratorStack[$level] = false;
225 $contextNode = false;
226 } else {
227 $contextNode = $iteratorNode->item( $index );
228 $index++;
229 }
230 } else {
231 // Copy to $contextNode and then delete from iterator stack,
232 // because this is not an iterator but we do have to execute it once
233 $contextNode = $iteratorStack[$level];
234 $iteratorStack[$level] = false;
235 }
236
237 $newIterator = false;
238 $contextName = false;
239 $contextChildren = false;
240
241 if ( $contextNode === false ) {
242 // nothing to do
243 } elseif ( is_string( $contextNode ) ) {
244 $out .= $contextNode;
245 } elseif ( $contextNode instanceof PPNode_Hash_Array ) {
246 $newIterator = $contextNode;
247 } elseif ( $contextNode instanceof PPNode_Hash_Attr ) {
248 // No output
249 } elseif ( $contextNode instanceof PPNode_Hash_Text ) {
250 $out .= $contextNode->value;
251 } elseif ( $contextNode instanceof PPNode_Hash_Tree ) {
252 $contextName = $contextNode->name;
253 $contextChildren = $contextNode->getRawChildren();
254 } elseif ( is_array( $contextNode ) ) {
255 // Node descriptor array
256 if ( count( $contextNode ) !== 2 ) {
257 throw new RuntimeException( __METHOD__ .
258 ': found an array where a node descriptor should be' );
259 }
260 [ $contextName, $contextChildren ] = $contextNode;
261 } else {
262 throw new RuntimeException( __METHOD__ . ': Invalid parameter type' );
263 }
264
265 // Handle node descriptor array or tree object
266 if ( $contextName === false ) {
267 // Not a node, already handled above
268 } elseif ( $contextName[0] === '@' ) {
269 // Attribute: no output
270 } elseif ( $contextName === 'template' ) {
271 # Double-brace expansion
272 $bits = PPNode_Hash_Tree::splitRawTemplate( $contextChildren );
273 if ( $flags & PPFrame::NO_TEMPLATES ) {
274 $newIterator = $this->virtualBracketedImplode(
275 '{{', '|', '}}',
276 $bits['title'],
277 $bits['parts']
278 );
279 } else {
280 $ret = $this->parser->braceSubstitution( $bits, $this );
281 if ( isset( $ret['object'] ) ) {
282 $newIterator = $ret['object'];
283 } else {
284 $out .= $ret['text'];
285 }
286 }
287 } elseif ( $contextName === 'tplarg' ) {
288 # Triple-brace expansion
289 $bits = PPNode_Hash_Tree::splitRawTemplate( $contextChildren );
290 if ( $flags & PPFrame::NO_ARGS ) {
291 $newIterator = $this->virtualBracketedImplode(
292 '{{{', '|', '}}}',
293 $bits['title'],
294 $bits['parts']
295 );
296 } else {
297 $ret = $this->parser->argSubstitution( $bits, $this );
298 if ( isset( $ret['object'] ) ) {
299 $newIterator = $ret['object'];
300 } else {
301 $out .= $ret['text'];
302 }
303 }
304 } elseif ( $contextName === 'comment' ) {
305 # HTML-style comment
306 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
307 # Not in RECOVER_COMMENTS mode (msgnw) though.
308 if ( ( $this->parser->getOutputType() === Parser::OT_HTML
309 || ( $this->parser->getOutputType() === Parser::OT_PREPROCESS &&
310 $this->parser->getOptions()->getRemoveComments() )
311 || ( $flags & PPFrame::STRIP_COMMENTS )
312 ) && !( $flags & PPFrame::RECOVER_COMMENTS )
313 ) {
314 $out .= '';
315 } elseif (
316 $this->parser->getOutputType() === Parser::OT_WIKI &&
317 !( $flags & PPFrame::RECOVER_COMMENTS )
318 ) {
319 # Add a strip marker in PST mode so that pstPass2() can
320 # run some old-fashioned regexes on the result.
321 # Not in RECOVER_COMMENTS mode (extractSections) though.
322 $out .= $this->parser->insertStripItem( $contextChildren[0] );
323 } else {
324 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
325 $out .= $contextChildren[0];
326 }
327 } elseif ( $contextName === 'ignore' ) {
328 # Output suppression used by <includeonly> etc.
329 # OT_WIKI will only respect <ignore> in substed templates.
330 # The other output types respect it unless NO_IGNORE is set.
331 # extractSections() sets NO_IGNORE and so never respects it.
332 if ( ( !isset( $this->parent ) && $this->parser->getOutputType() === Parser::OT_WIKI )
333 || ( $flags & PPFrame::NO_IGNORE )
334 ) {
335 $out .= $contextChildren[0];
336 } else {
337 // $out .= '';
338 }
339 } elseif ( $contextName === 'ext' ) {
340 # Extension tag
341 $bits = PPNode_Hash_Tree::splitRawExt( $contextChildren ) +
342 [ 'attr' => null, 'inner' => null, 'close' => null ];
343 if ( $flags & PPFrame::NO_TAGS ) {
344 $s = '<' . $bits['name']->getFirstChild()->value;
345 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
346 if ( $bits['attr'] ) {
347 $s .= $bits['attr']->getFirstChild()->value;
348 }
349 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
350 if ( $bits['inner'] ) {
351 $s .= '>' . $bits['inner']->getFirstChild()->value;
352 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
353 if ( $bits['close'] ) {
354 $s .= $bits['close']->getFirstChild()->value;
355 }
356 } else {
357 $s .= '/>';
358 }
359 $out .= $s;
360 } else {
361 $out .= $this->parser->extensionSubstitution( $bits, $this,
362 (bool)( $flags & PPFrame::PROCESS_NOWIKI ) );
363 }
364 } elseif ( $contextName === 'h' ) {
365 # Heading
366 if ( $this->parser->getOutputType() === Parser::OT_HTML ) {
367 # Expand immediately and insert heading index marker
368 $s = $this->expand( $contextChildren, $flags );
369 $bits = PPNode_Hash_Tree::splitRawHeading( $contextChildren );
370 $titleText = $this->title->getPrefixedDBkey();
371 $this->parser->mHeadings[] = [ $titleText, $bits['i'] ];
372 $serial = count( $this->parser->mHeadings ) - 1;
373 $marker = Parser::MARKER_PREFIX . "-h-$serial-" . Parser::MARKER_SUFFIX;
374 $s = substr( $s, 0, $bits['level'] ) . $marker . substr( $s, $bits['level'] );
375 $this->parser->getStripState()->addGeneral( $marker, '' );
376 $out .= $s;
377 } else {
378 # Expand in virtual stack
379 $newIterator = $contextChildren;
380 }
381 } else {
382 # Generic recursive expansion
383 $newIterator = $contextChildren;
384 }
385
386 if ( $newIterator !== false ) {
387 $outStack[] = '';
388 $iteratorStack[] = $newIterator;
389 $indexStack[] = 0;
390 } elseif ( $iteratorStack[$level] === false ) {
391 // Return accumulated value to parent
392 // With tail recursion
393 while ( $iteratorStack[$level] === false && $level > 0 ) {
394 $outStack[$level - 1] .= $out;
395 array_pop( $outStack );
396 array_pop( $iteratorStack );
397 array_pop( $indexStack );
398 $level--;
399 }
400 }
401 }
402 --$expansionDepth;
403 return $outStack[0];
404 }
405
412 public function implodeWithFlags( $sep, $flags, ...$args ) {
413 $first = true;
414 $s = '';
415 foreach ( $args as $root ) {
416 if ( $root instanceof PPNode_Hash_Array ) {
417 $root = $root->value;
418 }
419 if ( !is_array( $root ) ) {
420 $root = [ $root ];
421 }
422 foreach ( $root as $node ) {
423 if ( $first ) {
424 $first = false;
425 } else {
426 $s .= $sep;
427 }
428 $s .= $this->expand( $node, $flags );
429 }
430 }
431 return $s;
432 }
433
441 public function implode( $sep, ...$args ) {
442 $first = true;
443 $s = '';
444 foreach ( $args as $root ) {
445 if ( $root instanceof PPNode_Hash_Array ) {
446 $root = $root->value;
447 }
448 if ( !is_array( $root ) ) {
449 $root = [ $root ];
450 }
451 foreach ( $root as $node ) {
452 if ( $first ) {
453 $first = false;
454 } else {
455 $s .= $sep;
456 }
457 $s .= $this->expand( $node );
458 }
459 }
460 return $s;
461 }
462
471 public function virtualImplode( $sep, ...$args ) {
472 $out = [];
473 $first = true;
474
475 foreach ( $args as $root ) {
476 if ( $root instanceof PPNode_Hash_Array ) {
477 $root = $root->value;
478 }
479 if ( !is_array( $root ) ) {
480 $root = [ $root ];
481 }
482 foreach ( $root as $node ) {
483 if ( $first ) {
484 $first = false;
485 } else {
486 $out[] = $sep;
487 }
488 $out[] = $node;
489 }
490 }
491 return new PPNode_Hash_Array( $out );
492 }
493
503 public function virtualBracketedImplode( $start, $sep, $end, ...$args ) {
504 $out = [ $start ];
505 $first = true;
506
507 foreach ( $args as $root ) {
508 if ( $root instanceof PPNode_Hash_Array ) {
509 $root = $root->value;
510 }
511 if ( !is_array( $root ) ) {
512 $root = [ $root ];
513 }
514 foreach ( $root as $node ) {
515 if ( $first ) {
516 $first = false;
517 } else {
518 $out[] = $sep;
519 }
520 $out[] = $node;
521 }
522 }
523 $out[] = $end;
524 return new PPNode_Hash_Array( $out );
525 }
526
527 public function __toString() {
528 return 'frame{}';
529 }
530
535 public function getPDBK( $level = false ) {
536 if ( $level === false ) {
537 return $this->title->getPrefixedDBkey();
538 } else {
539 return $this->titleCache[$level] ?? false;
540 }
541 }
542
546 public function getArguments() {
547 return [];
548 }
549
553 public function getNumberedArguments() {
554 return [];
555 }
556
560 public function getNamedArguments() {
561 return [];
562 }
563
569 public function isEmpty() {
570 return true;
571 }
572
577 public function getArgument( $name ) {
578 return false;
579 }
580
588 public function loopCheck( $title ) {
589 return !isset( $this->loopCheckHash[$title->getPrefixedDBkey()] );
590 }
591
597 public function isTemplate() {
598 return false;
599 }
600
606 public function getTitle() {
607 return $this->title;
608 }
609
615 public function setVolatile( $flag = true ) {
616 $this->volatile = $flag;
617 }
618
624 public function isVolatile() {
625 return $this->volatile;
626 }
627
631 public function setTTL( $ttl ) {
632 if ( $ttl !== null && ( $this->ttl === null || $ttl < $this->ttl ) ) {
633 $this->ttl = $ttl;
634 }
635 }
636
640 public function getTTL() {
641 return $this->ttl;
642 }
643}
644
646class_alias( PPFrame_Hash::class, 'PPFrame_Hash' );
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:155
static plaintextParam( $plaintext)
Definition Message.php:1308
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:148
const OT_PREPROCESS
Output type: like Parser::preprocess()
Definition Parser.php:190
const OT_WIKI
Output type: like Parser::preSaveTransform()
Definition Parser.php:188
const OT_HTML
Output type: like Parser::parse()
Definition Parser.php:186
Represents a title within MediaWiki.
Definition Title.php:78
getPrefixedDBkey()
Get the prefixed database key form.
Definition Title.php:1844