MediaWiki REL1_34
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;
44
50
55 public $depth;
56
57 private $volatile = false;
58 private $ttl = null;
59
64
69 public function __construct( $preprocessor ) {
70 $this->preprocessor = $preprocessor;
71 $this->parser = $preprocessor->parser;
72 $this->title = $this->parser->getTitle();
73 $this->titleCache = [ $this->title ? $this->title->getPrefixedDBkey() : false ];
74 $this->loopCheckHash = [];
75 $this->depth = 0;
76 $this->childExpansionCache = [];
77 }
78
89 public function newChild( $args = false, $title = false, $indexOffset = 0 ) {
90 $namedArgs = [];
91 $numberedArgs = [];
92 if ( $title === false ) {
94 }
95 if ( $args !== false ) {
96 if ( $args instanceof PPNode_Hash_Array ) {
97 $args = $args->value;
98 } elseif ( !is_array( $args ) ) {
99 throw new MWException( __METHOD__ . ': $args must be array or PPNode_Hash_Array' );
100 }
101 foreach ( $args as $arg ) {
102 $bits = $arg->splitArg();
103 if ( $bits['index'] !== '' ) {
104 // Numbered parameter
105 $index = $bits['index'] - $indexOffset;
106 if ( isset( $namedArgs[$index] ) || isset( $numberedArgs[$index] ) ) {
107 $this->parser->getOutput()->addWarning( wfMessage( 'duplicate-args-warning',
108 wfEscapeWikiText( $this->title ),
110 wfEscapeWikiText( $index ) )->text() );
111 $this->parser->addTrackingCategory( 'duplicate-args-category' );
112 }
113 $numberedArgs[$index] = $bits['value'];
114 unset( $namedArgs[$index] );
115 } else {
116 // Named parameter
117 $name = trim( $this->expand( $bits['name'], PPFrame::STRIP_COMMENTS ) );
118 if ( isset( $namedArgs[$name] ) || isset( $numberedArgs[$name] ) ) {
119 $this->parser->getOutput()->addWarning( wfMessage( 'duplicate-args-warning',
120 wfEscapeWikiText( $this->title ),
122 wfEscapeWikiText( $name ) )->text() );
123 $this->parser->addTrackingCategory( 'duplicate-args-category' );
124 }
125 $namedArgs[$name] = $bits['value'];
126 unset( $numberedArgs[$name] );
127 }
128 }
129 }
130 return new PPTemplateFrame_Hash( $this->preprocessor, $this, $numberedArgs, $namedArgs, $title );
131 }
132
140 public function cachedExpand( $key, $root, $flags = 0 ) {
141 // we don't have a parent, so we don't have a cache
142 return $this->expand( $root, $flags );
143 }
144
151 public function expand( $root, $flags = 0 ) {
152 static $expansionDepth = 0;
153 if ( is_string( $root ) ) {
154 return $root;
155 }
156
157 if ( ++$this->parser->mPPNodeCount > $this->parser->mOptions->getMaxPPNodeCount() ) {
158 $this->parser->limitationWarn( 'node-count-exceeded',
159 $this->parser->mPPNodeCount,
160 $this->parser->mOptions->getMaxPPNodeCount()
161 );
162 return '<span class="error">Node-count limit exceeded</span>';
163 }
164 if ( $expansionDepth > $this->parser->mOptions->getMaxPPExpandDepth() ) {
165 $this->parser->limitationWarn( 'expansion-depth-exceeded',
166 $expansionDepth,
167 $this->parser->mOptions->getMaxPPExpandDepth()
168 );
169 return '<span class="error">Expansion depth limit exceeded</span>';
170 }
171 ++$expansionDepth;
172 if ( $expansionDepth > $this->parser->mHighestExpansionDepth ) {
173 $this->parser->mHighestExpansionDepth = $expansionDepth;
174 }
175
176 $outStack = [ '', '' ];
177 $iteratorStack = [ false, $root ];
178 $indexStack = [ 0, 0 ];
179
180 while ( count( $iteratorStack ) > 1 ) {
181 $level = count( $outStack ) - 1;
182 $iteratorNode =& $iteratorStack[$level];
183 $out =& $outStack[$level];
184 $index =& $indexStack[$level];
185
186 if ( is_array( $iteratorNode ) ) {
187 if ( $index >= count( $iteratorNode ) ) {
188 // All done with this iterator
189 $iteratorStack[$level] = false;
190 $contextNode = false;
191 } else {
192 $contextNode = $iteratorNode[$index];
193 $index++;
194 }
195 } elseif ( $iteratorNode instanceof PPNode_Hash_Array ) {
196 if ( $index >= $iteratorNode->getLength() ) {
197 // All done with this iterator
198 $iteratorStack[$level] = false;
199 $contextNode = false;
200 } else {
201 $contextNode = $iteratorNode->item( $index );
202 $index++;
203 }
204 } else {
205 // Copy to $contextNode and then delete from iterator stack,
206 // because this is not an iterator but we do have to execute it once
207 $contextNode = $iteratorStack[$level];
208 $iteratorStack[$level] = false;
209 }
210
211 $newIterator = false;
212 $contextName = false;
213 $contextChildren = false;
214
215 if ( $contextNode === false ) {
216 // nothing to do
217 } elseif ( is_string( $contextNode ) ) {
218 $out .= $contextNode;
219 } elseif ( $contextNode instanceof PPNode_Hash_Array ) {
220 $newIterator = $contextNode;
221 } elseif ( $contextNode instanceof PPNode_Hash_Attr ) {
222 // No output
223 } elseif ( $contextNode instanceof PPNode_Hash_Text ) {
224 $out .= $contextNode->value;
225 } elseif ( $contextNode instanceof PPNode_Hash_Tree ) {
226 $contextName = $contextNode->name;
227 $contextChildren = $contextNode->getRawChildren();
228 } elseif ( is_array( $contextNode ) ) {
229 // Node descriptor array
230 if ( count( $contextNode ) !== 2 ) {
231 throw new MWException( __METHOD__ .
232 ': found an array where a node descriptor should be' );
233 }
234 list( $contextName, $contextChildren ) = $contextNode;
235 } else {
236 throw new MWException( __METHOD__ . ': Invalid parameter type' );
237 }
238
239 // Handle node descriptor array or tree object
240 if ( $contextName === false ) {
241 // Not a node, already handled above
242 } elseif ( $contextName[0] === '@' ) {
243 // Attribute: no output
244 } elseif ( $contextName === 'template' ) {
245 # Double-brace expansion
246 $bits = PPNode_Hash_Tree::splitRawTemplate( $contextChildren );
247 if ( $flags & PPFrame::NO_TEMPLATES ) {
248 $newIterator = $this->virtualBracketedImplode(
249 '{{', '|', '}}',
250 $bits['title'],
251 $bits['parts']
252 );
253 } else {
254 $ret = $this->parser->braceSubstitution( $bits, $this );
255 if ( isset( $ret['object'] ) ) {
256 $newIterator = $ret['object'];
257 } else {
258 $out .= $ret['text'];
259 }
260 }
261 } elseif ( $contextName === 'tplarg' ) {
262 # Triple-brace expansion
263 $bits = PPNode_Hash_Tree::splitRawTemplate( $contextChildren );
264 if ( $flags & PPFrame::NO_ARGS ) {
265 $newIterator = $this->virtualBracketedImplode(
266 '{{{', '|', '}}}',
267 $bits['title'],
268 $bits['parts']
269 );
270 } else {
271 $ret = $this->parser->argSubstitution( $bits, $this );
272 if ( isset( $ret['object'] ) ) {
273 $newIterator = $ret['object'];
274 } else {
275 $out .= $ret['text'];
276 }
277 }
278 } elseif ( $contextName === 'comment' ) {
279 # HTML-style comment
280 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
281 # Not in RECOVER_COMMENTS mode (msgnw) though.
282 if ( ( $this->parser->ot['html']
283 || ( $this->parser->ot['pre'] && $this->parser->mOptions->getRemoveComments() )
284 || ( $flags & PPFrame::STRIP_COMMENTS )
285 ) && !( $flags & PPFrame::RECOVER_COMMENTS )
286 ) {
287 $out .= '';
288 } elseif ( $this->parser->ot['wiki'] && !( $flags & PPFrame::RECOVER_COMMENTS ) ) {
289 # Add a strip marker in PST mode so that pstPass2() can
290 # run some old-fashioned regexes on the result.
291 # Not in RECOVER_COMMENTS mode (extractSections) though.
292 $out .= $this->parser->insertStripItem( $contextChildren[0] );
293 } else {
294 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
295 $out .= $contextChildren[0];
296 }
297 } elseif ( $contextName === 'ignore' ) {
298 # Output suppression used by <includeonly> etc.
299 # OT_WIKI will only respect <ignore> in substed templates.
300 # The other output types respect it unless NO_IGNORE is set.
301 # extractSections() sets NO_IGNORE and so never respects it.
302 if ( ( !isset( $this->parent ) && $this->parser->ot['wiki'] )
303 || ( $flags & PPFrame::NO_IGNORE )
304 ) {
305 $out .= $contextChildren[0];
306 } else {
307 // $out .= '';
308 }
309 } elseif ( $contextName === 'ext' ) {
310 # Extension tag
311 $bits = PPNode_Hash_Tree::splitRawExt( $contextChildren ) +
312 [ 'attr' => null, 'inner' => null, 'close' => null ];
313 if ( $flags & PPFrame::NO_TAGS ) {
314 $s = '<' . $bits['name']->getFirstChild()->value;
315 if ( $bits['attr'] ) {
316 $s .= $bits['attr']->getFirstChild()->value;
317 }
318 if ( $bits['inner'] ) {
319 $s .= '>' . $bits['inner']->getFirstChild()->value;
320 if ( $bits['close'] ) {
321 $s .= $bits['close']->getFirstChild()->value;
322 }
323 } else {
324 $s .= '/>';
325 }
326 $out .= $s;
327 } else {
328 $out .= $this->parser->extensionSubstitution( $bits, $this );
329 }
330 } elseif ( $contextName === 'h' ) {
331 # Heading
332 if ( $this->parser->ot['html'] ) {
333 # Expand immediately and insert heading index marker
334 $s = $this->expand( $contextChildren, $flags );
335 $bits = PPNode_Hash_Tree::splitRawHeading( $contextChildren );
336 $titleText = $this->title->getPrefixedDBkey();
337 $this->parser->mHeadings[] = [ $titleText, $bits['i'] ];
338 $serial = count( $this->parser->mHeadings ) - 1;
339 $marker = Parser::MARKER_PREFIX . "-h-$serial-" . Parser::MARKER_SUFFIX;
340 $s = substr( $s, 0, $bits['level'] ) . $marker . substr( $s, $bits['level'] );
341 $this->parser->mStripState->addGeneral( $marker, '' );
342 $out .= $s;
343 } else {
344 # Expand in virtual stack
345 $newIterator = $contextChildren;
346 }
347 } else {
348 # Generic recursive expansion
349 $newIterator = $contextChildren;
350 }
351
352 if ( $newIterator !== false ) {
353 $outStack[] = '';
354 $iteratorStack[] = $newIterator;
355 $indexStack[] = 0;
356 } elseif ( $iteratorStack[$level] === false ) {
357 // Return accumulated value to parent
358 // With tail recursion
359 while ( $iteratorStack[$level] === false && $level > 0 ) {
360 $outStack[$level - 1] .= $out;
361 array_pop( $outStack );
362 array_pop( $iteratorStack );
363 array_pop( $indexStack );
364 $level--;
365 }
366 }
367 }
368 --$expansionDepth;
369 return $outStack[0];
370 }
371
378 public function implodeWithFlags( $sep, $flags, ...$args ) {
379 $first = true;
380 $s = '';
381 foreach ( $args as $root ) {
382 if ( $root instanceof PPNode_Hash_Array ) {
383 $root = $root->value;
384 }
385 if ( !is_array( $root ) ) {
386 $root = [ $root ];
387 }
388 foreach ( $root as $node ) {
389 if ( $first ) {
390 $first = false;
391 } else {
392 $s .= $sep;
393 }
394 $s .= $this->expand( $node, $flags );
395 }
396 }
397 return $s;
398 }
399
407 public function implode( $sep, ...$args ) {
408 $first = true;
409 $s = '';
410 foreach ( $args as $root ) {
411 if ( $root instanceof PPNode_Hash_Array ) {
412 $root = $root->value;
413 }
414 if ( !is_array( $root ) ) {
415 $root = [ $root ];
416 }
417 foreach ( $root as $node ) {
418 if ( $first ) {
419 $first = false;
420 } else {
421 $s .= $sep;
422 }
423 $s .= $this->expand( $node );
424 }
425 }
426 return $s;
427 }
428
437 public function virtualImplode( $sep, ...$args ) {
438 $out = [];
439 $first = true;
440
441 foreach ( $args as $root ) {
442 if ( $root instanceof PPNode_Hash_Array ) {
443 $root = $root->value;
444 }
445 if ( !is_array( $root ) ) {
446 $root = [ $root ];
447 }
448 foreach ( $root as $node ) {
449 if ( $first ) {
450 $first = false;
451 } else {
452 $out[] = $sep;
453 }
454 $out[] = $node;
455 }
456 }
457 return new PPNode_Hash_Array( $out );
458 }
459
469 public function virtualBracketedImplode( $start, $sep, $end, ...$args ) {
470 $out = [ $start ];
471 $first = true;
472
473 foreach ( $args as $root ) {
474 if ( $root instanceof PPNode_Hash_Array ) {
475 $root = $root->value;
476 }
477 if ( !is_array( $root ) ) {
478 $root = [ $root ];
479 }
480 foreach ( $root as $node ) {
481 if ( $first ) {
482 $first = false;
483 } else {
484 $out[] = $sep;
485 }
486 $out[] = $node;
487 }
488 }
489 $out[] = $end;
490 return new PPNode_Hash_Array( $out );
491 }
492
493 public function __toString() {
494 return 'frame{}';
495 }
496
501 public function getPDBK( $level = false ) {
502 if ( $level === false ) {
503 return $this->title->getPrefixedDBkey();
504 } else {
505 return $this->titleCache[$level] ?? false;
506 }
507 }
508
512 public function getArguments() {
513 return [];
514 }
515
519 public function getNumberedArguments() {
520 return [];
521 }
522
526 public function getNamedArguments() {
527 return [];
528 }
529
535 public function isEmpty() {
536 return true;
537 }
538
543 public function getArgument( $name ) {
544 return false;
545 }
546
554 public function loopCheck( $title ) {
555 return !isset( $this->loopCheckHash[$title->getPrefixedDBkey()] );
556 }
557
563 public function isTemplate() {
564 return false;
565 }
566
572 public function getTitle() {
573 return $this->title;
574 }
575
581 public function setVolatile( $flag = true ) {
582 $this->volatile = $flag;
583 }
584
590 public function isVolatile() {
591 return $this->volatile;
592 }
593
599 public function setTTL( $ttl ) {
600 if ( $ttl !== null && ( $this->ttl === null || $ttl < $this->ttl ) ) {
601 $this->ttl = $ttl;
602 }
603 }
604
610 public function getTTL() {
611 return $this->ttl;
612 }
613}
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
if( $line===false) $args
Definition cdb.php:64
MediaWiki exception.
An expansion frame, used as a context to expand the result of preprocessToObj()
$loopCheckHash
Hashtable listing templates which are disallowed for expansion in this frame, having been encountered...
getArgument( $name)
isEmpty()
Returns true if there are no arguments in this frame.
loopCheck( $title)
Returns true if the infinite loop check is OK, false if a loop is detected.
setVolatile( $flag=true)
Set the volatile flag.
$depth
Recursion depth of this frame, top = 0 Note that this is NOT the same as expansion depth in expand()
expand( $root, $flags=0)
implodeWithFlags( $sep, $flags,... $args)
getTitle()
Get a title of frame.
Preprocessor $preprocessor
setTTL( $ttl)
Set the TTL.
implode( $sep,... $args)
Implode with no flags specified This previously called implodeWithFlags but has now been inlined to r...
getTTL()
Get the TTL.
cachedExpand( $key, $root, $flags=0)
__construct( $preprocessor)
Construct a new preprocessor frame.
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:74
Represents a title within MediaWiki.
Definition Title.php:42
getPrefixedDBkey()
Get the prefixed database key form.
Definition Title.php:1806
const NO_TEMPLATES
Definition PPFrame.php:30
const NO_TAGS
Definition PPFrame.php:34
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