MediaWiki  master
PPTemplateFrame_Hash.php
Go to the documentation of this file.
1 <?php
26 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
28 
30  public $numberedArgs;
32  public $namedArgs;
34  public $parent;
39 
47  public function __construct( $preprocessor, $parent = false, $numberedArgs = [],
48  $namedArgs = [], $title = false
49  ) {
50  parent::__construct( $preprocessor );
52  '@phan-var PPFrame_Hash $parent';
53 
54  $this->parent = $parent;
55  $this->numberedArgs = $numberedArgs;
56  $this->namedArgs = $namedArgs;
57  $this->title = $title;
58  $pdbk = $title ? $title->getPrefixedDBkey() : false;
59  $this->titleCache = $parent->titleCache;
60  $this->titleCache[] = $pdbk;
61  $this->loopCheckHash = /*clone*/ $parent->loopCheckHash;
62  if ( $pdbk !== false ) {
63  $this->loopCheckHash[$pdbk] = true;
64  }
65  $this->depth = $parent->depth + 1;
66  $this->numberedExpansionCache = $this->namedExpansionCache = [];
67  }
68 
69  public function __toString() {
70  $s = 'tplframe{';
71  $first = true;
72  $args = $this->numberedArgs + $this->namedArgs;
73  foreach ( $args as $name => $value ) {
74  if ( $first ) {
75  $first = false;
76  } else {
77  $s .= ', ';
78  }
79  $s .= "\"$name\":\"" .
80  str_replace( '"', '\\"', $value->__toString() ) . '"';
81  }
82  $s .= '}';
83  return $s;
84  }
85 
93  public function cachedExpand( $key, $root, $flags = 0 ) {
94  if ( isset( $this->parent->childExpansionCache[$key] ) ) {
95  return $this->parent->childExpansionCache[$key];
96  }
97  $retval = $this->expand( $root, $flags );
98  if ( !$this->isVolatile() ) {
99  $this->parent->childExpansionCache[$key] = $retval;
100  }
101  return $retval;
102  }
103 
109  public function isEmpty() {
110  return !count( $this->numberedArgs ) && !count( $this->namedArgs );
111  }
112 
116  public function getArguments() {
117  $arguments = [];
118  foreach ( array_merge(
119  array_keys( $this->numberedArgs ),
120  array_keys( $this->namedArgs ) ) as $key ) {
121  $arguments[$key] = $this->getArgument( $key );
122  }
123  return $arguments;
124  }
125 
129  public function getNumberedArguments() {
130  $arguments = [];
131  foreach ( array_keys( $this->numberedArgs ) as $key ) {
132  $arguments[$key] = $this->getArgument( $key );
133  }
134  return $arguments;
135  }
136 
140  public function getNamedArguments() {
141  $arguments = [];
142  foreach ( array_keys( $this->namedArgs ) as $key ) {
143  $arguments[$key] = $this->getArgument( $key );
144  }
145  return $arguments;
146  }
147 
152  public function getNumberedArgument( $index ) {
153  if ( !isset( $this->numberedArgs[$index] ) ) {
154  return false;
155  }
156  if ( !isset( $this->numberedExpansionCache[$index] ) ) {
157  # No trimming for unnamed arguments
158  $this->numberedExpansionCache[$index] = $this->parent->expand(
159  $this->numberedArgs[$index],
161  );
162  }
163  return $this->numberedExpansionCache[$index];
164  }
165 
170  public function getNamedArgument( $name ) {
171  if ( !isset( $this->namedArgs[$name] ) ) {
172  return false;
173  }
174  if ( !isset( $this->namedExpansionCache[$name] ) ) {
175  # Trim named arguments post-expand, for backwards compatibility
176  $this->namedExpansionCache[$name] = trim(
177  // @phan-suppress-next-line SecurityCheck-XSS
178  $this->parent->expand( $this->namedArgs[$name], PPFrame::STRIP_COMMENTS ) );
179  }
180  return $this->namedExpansionCache[$name];
181  }
182 
187  public function getArgument( $name ) {
188  $text = $this->getNumberedArgument( $name );
189  if ( $text === false ) {
190  $text = $this->getNamedArgument( $name );
191  }
192  return $text;
193  }
194 
200  public function isTemplate() {
201  return true;
202  }
203 
204  public function setVolatile( $flag = true ) {
205  parent::setVolatile( $flag );
206  $this->parent->setVolatile( $flag );
207  }
208 
209  public function setTTL( $ttl ) {
210  parent::setTTL( $ttl );
211  $this->parent->setTTL( $ttl );
212  }
213 }
PPFrame\STRIP_COMMENTS
const STRIP_COMMENTS
Definition: PPFrame.php:31
PPTemplateFrame_Hash\setVolatile
setVolatile( $flag=true)
Set the volatile flag.
Definition: PPTemplateFrame_Hash.php:204
PPFrame_Hash\expand
expand( $root, $flags=0)
Definition: PPFrame_Hash.php:170
Title\getPrefixedDBkey
getPrefixedDBkey()
Get the prefixed database key form.
Definition: Title.php:1900
PPFrame_Hash
An expansion frame, used as a context to expand the result of preprocessToObj()
Definition: PPFrame_Hash.php:27
PPTemplateFrame_Hash\__construct
__construct( $preprocessor, $parent=false, $numberedArgs=[], $namedArgs=[], $title=false)
Definition: PPTemplateFrame_Hash.php:47
PPTemplateFrame_Hash\getNumberedArgument
getNumberedArgument( $index)
Definition: PPTemplateFrame_Hash.php:152
PPTemplateFrame_Hash\isEmpty
isEmpty()
Returns true if there are no arguments in this frame.
Definition: PPTemplateFrame_Hash.php:109
PPTemplateFrame_Hash\$numberedExpansionCache
array $numberedExpansionCache
Definition: PPTemplateFrame_Hash.php:36
PPTemplateFrame_Hash\getArguments
getArguments()
Definition: PPTemplateFrame_Hash.php:116
PPFrame_Hash\$ttl
int null $ttl
Definition: PPFrame_Hash.php:66
PPTemplateFrame_Hash\getNamedArgument
getNamedArgument( $name)
Definition: PPTemplateFrame_Hash.php:170
$args
if( $line===false) $args
Definition: mcc.php:124
PPTemplateFrame_Hash\$namedArgs
array $namedArgs
Definition: PPTemplateFrame_Hash.php:32
PPTemplateFrame_Hash\getNamedArguments
getNamedArguments()
Definition: PPTemplateFrame_Hash.php:140
PPTemplateFrame_Hash\cachedExpand
cachedExpand( $key, $root, $flags=0)
Definition: PPTemplateFrame_Hash.php:93
PPTemplateFrame_Hash
Expansion frame with template arguments.
Definition: PPTemplateFrame_Hash.php:27
$s
foreach( $mmfl['setupFiles'] as $fileName) if( $queue) if(empty( $mmfl['quiet'])) $s
Definition: mergeMessageFileList.php:206
PPFrame_Hash\$preprocessor
Preprocessor $preprocessor
Definition: PPFrame_Hash.php:37
PPFrame_Hash\isVolatile
isVolatile()
Get the volatile flag.
Definition: PPFrame_Hash.php:612
PPTemplateFrame_Hash\$numberedArgs
array $numberedArgs
Definition: PPTemplateFrame_Hash.php:30
PPTemplateFrame_Hash\isTemplate
isTemplate()
Return true if the frame is a template frame.
Definition: PPTemplateFrame_Hash.php:200
PPTemplateFrame_Hash\getArgument
getArgument( $name)
Definition: PPTemplateFrame_Hash.php:187
PPFrame_Hash\$title
Title $title
Definition: PPFrame_Hash.php:42
PPTemplateFrame_Hash\$parent
PPFrame_Hash $parent
Definition: PPTemplateFrame_Hash.php:34
PPTemplateFrame_Hash\__toString
__toString()
Definition: PPTemplateFrame_Hash.php:69
PPTemplateFrame_Hash\$namedExpansionCache
array $namedExpansionCache
Definition: PPTemplateFrame_Hash.php:38
PPTemplateFrame_Hash\getNumberedArguments
getNumberedArguments()
Definition: PPTemplateFrame_Hash.php:129
PPTemplateFrame_Hash\setTTL
setTTL( $ttl)
Definition: PPTemplateFrame_Hash.php:209