MediaWiki master
PPFrame_Hash.php
Go to the documentation of this file.
1<?php
24
29// phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
30class PPFrame_Hash implements PPFrame {
31
35 public $parser;
36
41
45 public $title;
46
51
58
64 public $depth;
65
67 private $volatile = false;
69 private $ttl = null;
70
78 private $maxPPNodeCount;
82 private $maxPPExpandDepth;
83
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 = [];
95 $this->depth = 0;
96 $this->childExpansionCache = [];
97 }
98
108 public function newChild( $args = false, $title = false, $indexOffset = 0 ) {
109 $namedArgs = [];
110 $numberedArgs = [];
111 if ( $title === false ) {
113 }
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' );
119 }
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 )
131 );
132 $this->parser->addTrackingCategory( 'duplicate-args-category' );
133 }
134 $numberedArgs[$index] = $bits['value'];
135 unset( $namedArgs[$index] );
136 } else {
137 // Named parameter
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 )
145 );
146 $this->parser->addTrackingCategory( 'duplicate-args-category' );
147 }
148 $namedArgs[$name] = $bits['value'];
149 unset( $numberedArgs[$name] );
150 }
151 }
152 }
153 return new PPTemplateFrame_Hash( $this->preprocessor, $this, $numberedArgs, $namedArgs, $title );
154 }
155
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 );
165 }
166
172 public function expand( $root, $flags = 0 ) {
173 static $expansionDepth = 0;
174 if ( is_string( $root ) ) {
175 return $root;
176 }
177
178 if ( ++$this->parser->mPPNodeCount > $this->maxPPNodeCount ) {
179 $this->parser->limitationWarn( 'node-count-exceeded',
180 $this->parser->mPPNodeCount,
181 $this->maxPPNodeCount
182 );
183 return '<span class="error">Node-count limit exceeded</span>';
184 }
185 if ( $expansionDepth > $this->maxPPExpandDepth ) {
186 $this->parser->limitationWarn( 'expansion-depth-exceeded',
187 $expansionDepth,
188 $this->maxPPExpandDepth
189 );
190 return '<span class="error">Expansion depth limit exceeded</span>';
191 }
192 ++$expansionDepth;
193 if ( $expansionDepth > $this->parser->mHighestExpansionDepth ) {
194 $this->parser->mHighestExpansionDepth = $expansionDepth;
195 }
196
197 $outStack = [ '', '' ];
198 $iteratorStack = [ false, $root ];
199 $indexStack = [ 0, 0 ];
200
201 while ( count( $iteratorStack ) > 1 ) {
202 $level = count( $outStack ) - 1;
203 $iteratorNode =& $iteratorStack[$level];
204 $out =& $outStack[$level];
205 $index =& $indexStack[$level];
206
207 if ( is_array( $iteratorNode ) ) {
208 if ( $index >= count( $iteratorNode ) ) {
209 // All done with this iterator
210 $iteratorStack[$level] = false;
211 $contextNode = false;
212 } else {
213 $contextNode = $iteratorNode[$index];
214 $index++;
215 }
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;
221 } else {
222 $contextNode = $iteratorNode->item( $index );
223 $index++;
224 }
225 } else {
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;
230 }
231
232 $newIterator = false;
233 $contextName = false;
234 $contextChildren = false;
235
236 if ( $contextNode === false ) {
237 // nothing to do
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 ) {
243 // No output
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' );
254 }
255 [ $contextName, $contextChildren ] = $contextNode;
256 } else {
257 throw new RuntimeException( __METHOD__ . ': Invalid parameter type' );
258 }
259
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(
270 '{{', '|', '}}',
271 $bits['title'],
272 $bits['parts']
273 );
274 } else {
275 $ret = $this->parser->braceSubstitution( $bits, $this );
276 if ( isset( $ret['object'] ) ) {
277 $newIterator = $ret['object'];
278 } else {
279 $out .= $ret['text'];
280 }
281 }
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(
287 '{{{', '|', '}}}',
288 $bits['title'],
289 $bits['parts']
290 );
291 } else {
292 $ret = $this->parser->argSubstitution( $bits, $this );
293 if ( isset( $ret['object'] ) ) {
294 $newIterator = $ret['object'];
295 } else {
296 $out .= $ret['text'];
297 }
298 }
299 } elseif ( $contextName === 'comment' ) {
300 # HTML-style 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 )
308 ) {
309 $out .= '';
310 } elseif (
311 $this->parser->getOutputType() === Parser::OT_WIKI &&
312 !( $flags & PPFrame::RECOVER_COMMENTS )
313 ) {
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] );
318 } else {
319 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
320 $out .= $contextChildren[0];
321 }
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 ) &&
328 $this->parser->getOutputType() === Parser::OT_WIKI )
329 || ( $flags & PPFrame::NO_IGNORE )
330 ) {
331 $out .= $contextChildren[0];
332 } else {
333 // $out .= '';
334 }
335 } elseif ( $contextName === 'ext' ) {
336 # Extension tag
337 $bits = PPNode_Hash_Tree::splitRawExt( $contextChildren ) +
338 [ 'attr' => null, 'inner' => null, 'close' => null ];
339 if ( $flags & PPFrame::NO_TAGS ) {
340 $s = '<' . $bits['name']->getFirstChild()->value;
341 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
342 if ( $bits['attr'] ) {
343 $s .= $bits['attr']->getFirstChild()->value;
344 }
345 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
346 if ( $bits['inner'] ) {
347 $s .= '>' . $bits['inner']->getFirstChild()->value;
348 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
349 if ( $bits['close'] ) {
350 $s .= $bits['close']->getFirstChild()->value;
351 }
352 } else {
353 $s .= '/>';
354 }
355 $out .= $s;
356 } else {
357 $out .= $this->parser->extensionSubstitution( $bits, $this,
358 (bool)( $flags & PPFrame::PROCESS_NOWIKI ) );
359 }
360 } elseif ( $contextName === 'h' ) {
361 # Heading
362 if ( $this->parser->getOutputType() === Parser::OT_HTML ) {
363 # Expand immediately and insert heading index marker
364 $s = $this->expand( $contextChildren, $flags );
365 $bits = PPNode_Hash_Tree::splitRawHeading( $contextChildren );
366 $titleText = $this->title->getPrefixedDBkey();
367 $this->parser->mHeadings[] = [ $titleText, $bits['i'] ];
368 $serial = count( $this->parser->mHeadings ) - 1;
369 $marker = Parser::MARKER_PREFIX . "-h-$serial-" . Parser::MARKER_SUFFIX;
370 $s = substr( $s, 0, $bits['level'] ) . $marker . substr( $s, $bits['level'] );
371 $this->parser->getStripState()->addGeneral( $marker, '' );
372 $out .= $s;
373 } else {
374 # Expand in virtual stack
375 $newIterator = $contextChildren;
376 }
377 } else {
378 # Generic recursive expansion
379 $newIterator = $contextChildren;
380 }
381
382 if ( $newIterator !== false ) {
383 $outStack[] = '';
384 $iteratorStack[] = $newIterator;
385 $indexStack[] = 0;
386 } elseif ( $iteratorStack[$level] === false ) {
387 // Return accumulated value to parent
388 // With tail recursion
389 while ( $iteratorStack[$level] === false && $level > 0 ) {
390 $outStack[$level - 1] .= $out;
391 array_pop( $outStack );
392 array_pop( $iteratorStack );
393 array_pop( $indexStack );
394 $level--;
395 }
396 }
397 }
398 --$expansionDepth;
399 return $outStack[0];
400 }
401
408 public function implodeWithFlags( $sep, $flags, ...$args ) {
409 $first = true;
410 $s = '';
411 foreach ( $args as $root ) {
412 if ( $root instanceof PPNode_Hash_Array ) {
413 $root = $root->value;
414 }
415 if ( !is_array( $root ) ) {
416 $root = [ $root ];
417 }
418 foreach ( $root as $node ) {
419 if ( $first ) {
420 $first = false;
421 } else {
422 $s .= $sep;
423 }
424 $s .= $this->expand( $node, $flags );
425 }
426 }
427 return $s;
428 }
429
437 public function implode( $sep, ...$args ) {
438 $first = true;
439 $s = '';
440 foreach ( $args as $root ) {
441 if ( $root instanceof PPNode_Hash_Array ) {
442 $root = $root->value;
443 }
444 if ( !is_array( $root ) ) {
445 $root = [ $root ];
446 }
447 foreach ( $root as $node ) {
448 if ( $first ) {
449 $first = false;
450 } else {
451 $s .= $sep;
452 }
453 $s .= $this->expand( $node );
454 }
455 }
456 return $s;
457 }
458
467 public function virtualImplode( $sep, ...$args ) {
468 $out = [];
469 $first = true;
470
471 foreach ( $args as $root ) {
472 if ( $root instanceof PPNode_Hash_Array ) {
473 $root = $root->value;
474 }
475 if ( !is_array( $root ) ) {
476 $root = [ $root ];
477 }
478 foreach ( $root as $node ) {
479 if ( $first ) {
480 $first = false;
481 } else {
482 $out[] = $sep;
483 }
484 $out[] = $node;
485 }
486 }
487 return new PPNode_Hash_Array( $out );
488 }
489
499 public function virtualBracketedImplode( $start, $sep, $end, ...$args ) {
500 $out = [ $start ];
501 $first = true;
502
503 foreach ( $args as $root ) {
504 if ( $root instanceof PPNode_Hash_Array ) {
505 $root = $root->value;
506 }
507 if ( !is_array( $root ) ) {
508 $root = [ $root ];
509 }
510 foreach ( $root as $node ) {
511 if ( $first ) {
512 $first = false;
513 } else {
514 $out[] = $sep;
515 }
516 $out[] = $node;
517 }
518 }
519 $out[] = $end;
520 return new PPNode_Hash_Array( $out );
521 }
522
523 public function __toString() {
524 return 'frame{}';
525 }
526
531 public function getPDBK( $level = false ) {
532 if ( $level === false ) {
533 return $this->title->getPrefixedDBkey();
534 } else {
535 return $this->titleCache[$level] ?? false;
536 }
537 }
538
542 public function getArguments() {
543 return [];
544 }
545
549 public function getNumberedArguments() {
550 return [];
551 }
552
556 public function getNamedArguments() {
557 return [];
558 }
559
565 public function isEmpty() {
566 return true;
567 }
568
573 public function getArgument( $name ) {
574 return false;
575 }
576
584 public function loopCheck( $title ) {
585 return !isset( $this->loopCheckHash[$title->getPrefixedDBkey()] );
586 }
587
593 public function isTemplate() {
594 return false;
595 }
596
602 public function getTitle() {
603 return $this->title;
604 }
605
611 public function setVolatile( $flag = true ) {
612 $this->volatile = $flag;
613 }
614
620 public function isVolatile() {
621 return $this->volatile;
622 }
623
627 public function setTTL( $ttl ) {
628 if ( $ttl !== null && ( $this->ttl === null || $ttl < $this->ttl ) ) {
629 $this->ttl = $ttl;
630 }
631 }
632
636 public function getTTL() {
637 return $this->ttl;
638 }
639}
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:156
Represents a title within MediaWiki.
Definition Title.php:78
getPrefixedDBkey()
Get the prefixed database key form.
Definition Title.php:1849
An expansion frame, used as a context to expand the result of preprocessToObj()
getArgument( $name)
isEmpty()
Returns true if there are no arguments in this frame.
int $depth
Recursion depth of this frame, top = 0 Note that this is NOT the same as expansion depth in expand()
loopCheck( $title)
Returns true if the infinite loop check is OK, false if a loop is detected.
setVolatile( $flag=true)
Set the volatile flag.
string false[] $titleCache
expand( $root, $flags=0)
implodeWithFlags( $sep, $flags,... $args)
getTitle()
Get a title of frame.
Preprocessor $preprocessor
implode( $sep,... $args)
Implode with no flags specified This previously called implodeWithFlags but has now been inlined to r...
true[] $loopCheckHash
Hashtable listing templates which are disallowed for expansion in this frame, having been encountered...
cachedExpand( $key, $root, $flags=0)
__construct( $preprocessor)
array $childExpansionCache
virtualImplode( $sep,... $args)
Makes an object that, when expand()ed, will be the same as one obtained with implode()
virtualBracketedImplode( $start, $sep, $end,... $args)
Virtual implode with brackets.
isVolatile()
Get the volatile flag.
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...
isTemplate()
Return true if the frame is a template frame.
getPDBK( $level=false)
static splitRawTemplate(array $children)
Like splitTemplate() but for a raw child array.
static splitRawHeading(array $children)
Like splitHeading() but for a raw child array.
static splitRawExt(array $children)
Like splitExt() but for a raw child array.
Expansion frame with template arguments.
const NO_TEMPLATES
Definition PPFrame.php:32
const NO_TAGS
Definition PPFrame.php:36
const PROCESS_NOWIKI
Definition PPFrame.php:37
const RECOVER_COMMENTS
Definition PPFrame.php:35
const NO_ARGS
Definition PPFrame.php:31
const NO_IGNORE
Definition PPFrame.php:34
const STRIP_COMMENTS
Definition PPFrame.php:33