MediaWiki  1.34.0
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 
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 }
PPFrame_Hash\getArguments
getArguments()
Definition: PPFrame_Hash.php:512
PPFrame\STRIP_COMMENTS
const STRIP_COMMENTS
Definition: PPFrame.php:31
PPFrame_Hash\$depth
$depth
Recursion depth of this frame, top = 0 Note that this is NOT the same as expansion depth in expand()
Definition: PPFrame_Hash.php:55
PPFrame_Hash\$childExpansionCache
array $childExpansionCache
Definition: PPFrame_Hash.php:63
PPFrame_Hash\getPDBK
getPDBK( $level=false)
Definition: PPFrame_Hash.php:501
PPFrame_Hash\getNumberedArguments
getNumberedArguments()
Definition: PPFrame_Hash.php:519
PPFrame_Hash\expand
expand( $root, $flags=0)
Definition: PPFrame_Hash.php:151
PPFrame\NO_ARGS
const NO_ARGS
Definition: PPFrame.php:29
PPNode_Hash_Tree\splitRawExt
static splitRawExt(array $children)
Like splitExt() but for a raw child array.
Definition: PPNode_Hash_Tree.php:266
Title\getPrefixedDBkey
getPrefixedDBkey()
Get the prefixed database key form.
Definition: Title.php:1806
PPFrame_Hash
An expansion frame, used as a context to expand the result of preprocessToObj()
Definition: PPFrame_Hash.php:27
PPFrame\NO_IGNORE
const NO_IGNORE
Definition: PPFrame.php:32
PPFrame_Hash\getNamedArguments
getNamedArguments()
Definition: PPFrame_Hash.php:526
PPFrame_Hash\getTTL
getTTL()
Get the TTL.
Definition: PPFrame_Hash.php:610
PPFrame_Hash\__construct
__construct( $preprocessor)
Construct a new preprocessor frame.
Definition: PPFrame_Hash.php:69
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1264
$s
$s
Definition: mergeMessageFileList.php:185
PPNode_Hash_Tree\splitRawHeading
static splitRawHeading(array $children)
Like splitHeading() but for a raw child array.
Definition: PPNode_Hash_Tree.php:311
PPFrame_Hash\$loopCheckHash
$loopCheckHash
Hashtable listing templates which are disallowed for expansion in this frame, having been encountered...
Definition: PPFrame_Hash.php:49
PPFrame_Hash\implode
implode( $sep,... $args)
Implode with no flags specified This previously called implodeWithFlags but has now been inlined to r...
Definition: PPFrame_Hash.php:407
PPFrame\RECOVER_COMMENTS
const RECOVER_COMMENTS
Definition: PPFrame.php:33
PPFrame\NO_TEMPLATES
const NO_TEMPLATES
Definition: PPFrame.php:30
Preprocessor
Definition: Preprocessor.php:30
PPFrame_Hash\$titleCache
$titleCache
Definition: PPFrame_Hash.php:43
PPFrame_Hash\setVolatile
setVolatile( $flag=true)
Set the volatile flag.
Definition: PPFrame_Hash.php:581
MWException
MediaWiki exception.
Definition: MWException.php:26
PPFrame_Hash\__toString
__toString()
Definition: PPFrame_Hash.php:493
PPFrame_Hash\setTTL
setTTL( $ttl)
Set the TTL.
Definition: PPFrame_Hash.php:599
PPFrame_Hash\isEmpty
isEmpty()
Returns true if there are no arguments in this frame.
Definition: PPFrame_Hash.php:535
PPFrame_Hash\getArgument
getArgument( $name)
Definition: PPFrame_Hash.php:543
PPNode_Hash_Tree\splitRawTemplate
static splitRawTemplate(array $children)
Like splitTemplate() but for a raw child array.
Definition: PPNode_Hash_Tree.php:344
PPFrame_Hash\virtualBracketedImplode
virtualBracketedImplode( $start, $sep, $end,... $args)
Virtual implode with brackets.
Definition: PPFrame_Hash.php:469
PPFrame_Hash\virtualImplode
virtualImplode( $sep,... $args)
Makes an object that, when expand()ed, will be the same as one obtained with implode()
Definition: PPFrame_Hash.php:437
PPNode_Hash_Array
Definition: PPNode_Hash_Array.php:26
PPTemplateFrame_Hash
Expansion frame with template arguments.
Definition: PPTemplateFrame_Hash.php:27
PPFrame_Hash\getTitle
getTitle()
Get a title of frame.
Definition: PPFrame_Hash.php:572
PPFrame_Hash\$preprocessor
Preprocessor $preprocessor
Definition: PPFrame_Hash.php:37
PPFrame\NO_TAGS
const NO_TAGS
Definition: PPFrame.php:34
PPFrame
Definition: PPFrame.php:28
PPFrame_Hash\$volatile
$volatile
Definition: PPFrame_Hash.php:57
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1551
PPNode_Hash_Attr
Definition: PPNode_Hash_Attr.php:26
PPFrame_Hash\newChild
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...
Definition: PPFrame_Hash.php:89
$args
if( $line===false) $args
Definition: cdb.php:64
Title
Represents a title within MediaWiki.
Definition: Title.php:42
PPNode_Hash_Text
Definition: PPNode_Hash_Text.php:26
PPFrame_Hash\isVolatile
isVolatile()
Get the volatile flag.
Definition: PPFrame_Hash.php:590
PPFrame_Hash\implodeWithFlags
implodeWithFlags( $sep, $flags,... $args)
Definition: PPFrame_Hash.php:378
PPFrame_Hash\$parser
Parser $parser
Definition: PPFrame_Hash.php:32
PPFrame_Hash\$ttl
$ttl
Definition: PPFrame_Hash.php:58
PPFrame_Hash\loopCheck
loopCheck( $title)
Returns true if the infinite loop check is OK, false if a loop is detected.
Definition: PPFrame_Hash.php:554
PPFrame_Hash\isTemplate
isTemplate()
Return true if the frame is a template frame.
Definition: PPFrame_Hash.php:563
PPNode_Hash_Tree
Definition: PPNode_Hash_Tree.php:26
PPFrame_Hash\$title
Title $title
Definition: PPFrame_Hash.php:42
PPFrame_Hash\cachedExpand
cachedExpand( $key, $root, $flags=0)
Definition: PPFrame_Hash.php:140