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