MediaWiki REL1_39
PPFrame_Hash.php
Go to the documentation of this file.
1<?php
26// phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
27class PPFrame_Hash implements PPFrame {
28
32 public $parser;
33
38
42 public $title;
43
48
55
61 public $depth;
62
64 private $volatile = false;
66 private $ttl = null;
67
75 private $maxPPNodeCount;
79 private $maxPPExpandDepth;
80
84 public function __construct( $preprocessor ) {
85 $this->preprocessor = $preprocessor;
86 $this->parser = $preprocessor->parser;
87 $this->title = $this->parser->getTitle();
88 $this->maxPPNodeCount = $this->parser->getOptions()->getMaxPPNodeCount();
89 $this->maxPPExpandDepth = $this->parser->getOptions()->getMaxPPExpandDepth();
90 $this->titleCache = [ $this->title ? $this->title->getPrefixedDBkey() : false ];
91 $this->loopCheckHash = [];
92 $this->depth = 0;
93 $this->childExpansionCache = [];
94 }
95
106 public function newChild( $args = false, $title = false, $indexOffset = 0 ) {
107 $namedArgs = [];
108 $numberedArgs = [];
109 if ( $title === false ) {
111 }
112 if ( $args !== false ) {
113 if ( $args instanceof PPNode_Hash_Array ) {
114 $args = $args->value;
115 } elseif ( !is_array( $args ) ) {
116 throw new MWException( __METHOD__ . ': $args must be array or PPNode_Hash_Array' );
117 }
118 foreach ( $args as $arg ) {
119 $bits = $arg->splitArg();
120 if ( $bits['index'] !== '' ) {
121 // Numbered parameter
122 $index = $bits['index'] - $indexOffset;
123 if ( isset( $namedArgs[$index] ) || isset( $numberedArgs[$index] ) ) {
124 $this->parser->getOutput()->addWarningMsg(
125 'duplicate-args-warning',
126 Message::plaintextParam( (string)$this->title ),
128 Message::numParam( $index )
129 );
130 $this->parser->addTrackingCategory( 'duplicate-args-category' );
131 }
132 $numberedArgs[$index] = $bits['value'];
133 unset( $namedArgs[$index] );
134 } else {
135 // Named parameter
136 $name = trim( $this->expand( $bits['name'], PPFrame::STRIP_COMMENTS ) );
137 if ( isset( $namedArgs[$name] ) || isset( $numberedArgs[$name] ) ) {
138 $this->parser->getOutput()->addWarningMsg(
139 'duplicate-args-warning',
140 Message::plaintextParam( (string)$this->title ),
143 );
144 $this->parser->addTrackingCategory( 'duplicate-args-category' );
145 }
146 $namedArgs[$name] = $bits['value'];
147 unset( $numberedArgs[$name] );
148 }
149 }
150 }
151 return new PPTemplateFrame_Hash( $this->preprocessor, $this, $numberedArgs, $namedArgs, $title );
152 }
153
161 public function cachedExpand( $key, $root, $flags = 0 ) {
162 // we don't have a parent, so we don't have a cache
163 return $this->expand( $root, $flags );
164 }
165
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 MWException( __METHOD__ .
253 ': found an array where a node descriptor should be' );
254 }
255 list( $contextName, $contextChildren ) = $contextNode;
256 } else {
257 throw new MWException( __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->ot['html']
304 || ( $this->parser->ot['pre'] && $this->parser->mOptions->getRemoveComments() )
305 || ( $flags & PPFrame::STRIP_COMMENTS )
306 ) && !( $flags & PPFrame::RECOVER_COMMENTS )
307 ) {
308 $out .= '';
309 } elseif ( $this->parser->ot['wiki'] && !( $flags & PPFrame::RECOVER_COMMENTS ) ) {
310 # Add a strip marker in PST mode so that pstPass2() can
311 # run some old-fashioned regexes on the result.
312 # Not in RECOVER_COMMENTS mode (extractSections) though.
313 $out .= $this->parser->insertStripItem( $contextChildren[0] );
314 } else {
315 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
316 $out .= $contextChildren[0];
317 }
318 } elseif ( $contextName === 'ignore' ) {
319 # Output suppression used by <includeonly> etc.
320 # OT_WIKI will only respect <ignore> in substed templates.
321 # The other output types respect it unless NO_IGNORE is set.
322 # extractSections() sets NO_IGNORE and so never respects it.
323 if ( ( !isset( $this->parent ) && $this->parser->ot['wiki'] )
324 || ( $flags & PPFrame::NO_IGNORE )
325 ) {
326 $out .= $contextChildren[0];
327 } else {
328 // $out .= '';
329 }
330 } elseif ( $contextName === 'ext' ) {
331 # Extension tag
332 $bits = PPNode_Hash_Tree::splitRawExt( $contextChildren ) +
333 [ 'attr' => null, 'inner' => null, 'close' => null ];
334 if ( $flags & PPFrame::NO_TAGS ) {
335 $s = '<' . $bits['name']->getFirstChild()->value;
336 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
337 if ( $bits['attr'] ) {
338 $s .= $bits['attr']->getFirstChild()->value;
339 }
340 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
341 if ( $bits['inner'] ) {
342 $s .= '>' . $bits['inner']->getFirstChild()->value;
343 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
344 if ( $bits['close'] ) {
345 $s .= $bits['close']->getFirstChild()->value;
346 }
347 } else {
348 $s .= '/>';
349 }
350 $out .= $s;
351 } else {
352 $out .= $this->parser->extensionSubstitution( $bits, $this,
353 (bool)( $flags & PPFrame::PROCESS_NOWIKI ) );
354 }
355 } elseif ( $contextName === 'h' ) {
356 # Heading
357 if ( $this->parser->ot['html'] ) {
358 # Expand immediately and insert heading index marker
359 $s = $this->expand( $contextChildren, $flags );
360 $bits = PPNode_Hash_Tree::splitRawHeading( $contextChildren );
361 $titleText = $this->title->getPrefixedDBkey();
362 $this->parser->mHeadings[] = [ $titleText, $bits['i'] ];
363 $serial = count( $this->parser->mHeadings ) - 1;
364 $marker = Parser::MARKER_PREFIX . "-h-$serial-" . Parser::MARKER_SUFFIX;
365 $s = substr( $s, 0, $bits['level'] ) . $marker . substr( $s, $bits['level'] );
366 $this->parser->getStripState()->addGeneral( $marker, '' );
367 $out .= $s;
368 } else {
369 # Expand in virtual stack
370 $newIterator = $contextChildren;
371 }
372 } else {
373 # Generic recursive expansion
374 $newIterator = $contextChildren;
375 }
376
377 if ( $newIterator !== false ) {
378 $outStack[] = '';
379 $iteratorStack[] = $newIterator;
380 $indexStack[] = 0;
381 } elseif ( $iteratorStack[$level] === false ) {
382 // Return accumulated value to parent
383 // With tail recursion
384 while ( $iteratorStack[$level] === false && $level > 0 ) {
385 $outStack[$level - 1] .= $out;
386 array_pop( $outStack );
387 array_pop( $iteratorStack );
388 array_pop( $indexStack );
389 $level--;
390 }
391 }
392 }
393 --$expansionDepth;
394 return $outStack[0];
395 }
396
403 public function implodeWithFlags( $sep, $flags, ...$args ) {
404 $first = true;
405 $s = '';
406 foreach ( $args as $root ) {
407 if ( $root instanceof PPNode_Hash_Array ) {
408 $root = $root->value;
409 }
410 if ( !is_array( $root ) ) {
411 $root = [ $root ];
412 }
413 foreach ( $root as $node ) {
414 if ( $first ) {
415 $first = false;
416 } else {
417 $s .= $sep;
418 }
419 $s .= $this->expand( $node, $flags );
420 }
421 }
422 return $s;
423 }
424
432 public function implode( $sep, ...$args ) {
433 $first = true;
434 $s = '';
435 foreach ( $args as $root ) {
436 if ( $root instanceof PPNode_Hash_Array ) {
437 $root = $root->value;
438 }
439 if ( !is_array( $root ) ) {
440 $root = [ $root ];
441 }
442 foreach ( $root as $node ) {
443 if ( $first ) {
444 $first = false;
445 } else {
446 $s .= $sep;
447 }
448 $s .= $this->expand( $node );
449 }
450 }
451 return $s;
452 }
453
462 public function virtualImplode( $sep, ...$args ) {
463 $out = [];
464 $first = true;
465
466 foreach ( $args as $root ) {
467 if ( $root instanceof PPNode_Hash_Array ) {
468 $root = $root->value;
469 }
470 if ( !is_array( $root ) ) {
471 $root = [ $root ];
472 }
473 foreach ( $root as $node ) {
474 if ( $first ) {
475 $first = false;
476 } else {
477 $out[] = $sep;
478 }
479 $out[] = $node;
480 }
481 }
482 return new PPNode_Hash_Array( $out );
483 }
484
494 public function virtualBracketedImplode( $start, $sep, $end, ...$args ) {
495 $out = [ $start ];
496 $first = true;
497
498 foreach ( $args as $root ) {
499 if ( $root instanceof PPNode_Hash_Array ) {
500 $root = $root->value;
501 }
502 if ( !is_array( $root ) ) {
503 $root = [ $root ];
504 }
505 foreach ( $root as $node ) {
506 if ( $first ) {
507 $first = false;
508 } else {
509 $out[] = $sep;
510 }
511 $out[] = $node;
512 }
513 }
514 $out[] = $end;
515 return new PPNode_Hash_Array( $out );
516 }
517
518 public function __toString() {
519 return 'frame{}';
520 }
521
526 public function getPDBK( $level = false ) {
527 if ( $level === false ) {
528 return $this->title->getPrefixedDBkey();
529 } else {
530 return $this->titleCache[$level] ?? false;
531 }
532 }
533
537 public function getArguments() {
538 return [];
539 }
540
544 public function getNumberedArguments() {
545 return [];
546 }
547
551 public function getNamedArguments() {
552 return [];
553 }
554
560 public function isEmpty() {
561 return true;
562 }
563
568 public function getArgument( $name ) {
569 return false;
570 }
571
579 public function loopCheck( $title ) {
580 return !isset( $this->loopCheckHash[$title->getPrefixedDBkey()] );
581 }
582
588 public function isTemplate() {
589 return false;
590 }
591
597 public function getTitle() {
598 return $this->title;
599 }
600
606 public function setVolatile( $flag = true ) {
607 $this->volatile = $flag;
608 }
609
615 public function isVolatile() {
616 return $this->volatile;
617 }
618
622 public function setTTL( $ttl ) {
623 if ( $ttl !== null && ( $this->ttl === null || $ttl < $this->ttl ) ) {
624 $this->ttl = $ttl;
625 }
626 }
627
631 public function getTTL() {
632 return $this->ttl;
633 }
634}
MediaWiki exception.
static plaintextParam( $plaintext)
Definition Message.php:1266
static numParam( $num)
Definition Message.php:1145
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.
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:96
const MARKER_PREFIX
Definition Parser.php:155
Represents a title within MediaWiki.
Definition Title.php:49
getPrefixedDBkey()
Get the prefixed database key form.
Definition Title.php:1878
const NO_TEMPLATES
Definition PPFrame.php:30
const NO_TAGS
Definition PPFrame.php:34
const PROCESS_NOWIKI
Definition PPFrame.php:35
const RECOVER_COMMENTS
Definition PPFrame.php:33
const NO_ARGS
Definition PPFrame.php:29
const NO_IGNORE
Definition PPFrame.php:32
const STRIP_COMMENTS
Definition PPFrame.php:31
if( $line===false) $args
Definition mcc.php:124
foreach( $mmfl['setupFiles'] as $fileName) if($queue) if(empty( $mmfl['quiet'])) $s