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 
69  public function __construct( $preprocessor ) {
70  $this->preprocessor = $preprocessor;
71  $this->parser = $preprocessor->parser;
72  $this->title = $this->parser->mTitle;
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 }
const MARKER_PREFIX
Definition: Parser.php:136
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking, formatting, etc.
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
getArgument( $name)
$depth
Recursion depth of this frame, top = 0 Note that this is NOT the same as expansion depth in expand() ...
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition: hooks.txt:1972
expand( $root, $flags=0)
implodeWithFlags( $sep, $flags,... $args)
const RECOVER_COMMENTS
Definition: PPFrame.php:30
title
__construct( $preprocessor)
Construct a new preprocessor frame.
const NO_ARGS
Definition: PPFrame.php:26
setVolatile( $flag=true)
Set the volatile flag.
This list may contain false positives That usually means there is additional text with links below the first Each row contains links to the first and second as well as the first line of the second redirect text
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:29
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...
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition: hooks.txt:767
const NO_TEMPLATES
Definition: PPFrame.php:27
Parser $parser
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation use $formDescriptor instead default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt
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.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:767
cachedExpand( $key, $root, $flags=0)
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
loopCheck( $title)
Returns true if the infinite loop check is OK, false if a loop is detected.
isVolatile()
Get the volatile flag.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
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:31
array $childExpansionCache
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:271
$loopCheckHash
Hashtable listing templates which are disallowed for expansion in this frame, having been encountered...
const STRIP_COMMENTS
Definition: PPFrame.php:28
Preprocessor $preprocessor
virtualBracketedImplode( $start, $sep, $end,... $args)
Virtual implode with brackets.
getPrefixedDBkey()
Get the prefixed database key form.
Definition: Title.php:1711