MediaWiki  1.34.0
PPTemplateFrame_Hash.php
Go to the documentation of this file.
1 <?php
26 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
28 
31 
39  public function __construct( $preprocessor, $parent = false, $numberedArgs = [],
40  $namedArgs = [], $title = false
41  ) {
42  parent::__construct( $preprocessor );
44  '@phan-var PPFrame_Hash $parent';
45 
46  $this->parent = $parent;
47  $this->numberedArgs = $numberedArgs;
48  $this->namedArgs = $namedArgs;
49  $this->title = $title;
50  $pdbk = $title ? $title->getPrefixedDBkey() : false;
51  $this->titleCache = $parent->titleCache;
52  $this->titleCache[] = $pdbk;
53  $this->loopCheckHash = /*clone*/ $parent->loopCheckHash;
54  if ( $pdbk !== false ) {
55  $this->loopCheckHash[$pdbk] = true;
56  }
57  $this->depth = $parent->depth + 1;
58  $this->numberedExpansionCache = $this->namedExpansionCache = [];
59  }
60 
61  public function __toString() {
62  $s = 'tplframe{';
63  $first = true;
64  $args = $this->numberedArgs + $this->namedArgs;
65  foreach ( $args as $name => $value ) {
66  if ( $first ) {
67  $first = false;
68  } else {
69  $s .= ', ';
70  }
71  $s .= "\"$name\":\"" .
72  str_replace( '"', '\\"', $value->__toString() ) . '"';
73  }
74  $s .= '}';
75  return $s;
76  }
77 
85  public function cachedExpand( $key, $root, $flags = 0 ) {
86  if ( isset( $this->parent->childExpansionCache[$key] ) ) {
87  return $this->parent->childExpansionCache[$key];
88  }
89  $retval = $this->expand( $root, $flags );
90  if ( !$this->isVolatile() ) {
91  $this->parent->childExpansionCache[$key] = $retval;
92  }
93  return $retval;
94  }
95 
101  public function isEmpty() {
102  return !count( $this->numberedArgs ) && !count( $this->namedArgs );
103  }
104 
108  public function getArguments() {
109  $arguments = [];
110  foreach ( array_merge(
111  array_keys( $this->numberedArgs ),
112  array_keys( $this->namedArgs ) ) as $key ) {
113  $arguments[$key] = $this->getArgument( $key );
114  }
115  return $arguments;
116  }
117 
121  public function getNumberedArguments() {
122  $arguments = [];
123  foreach ( array_keys( $this->numberedArgs ) as $key ) {
124  $arguments[$key] = $this->getArgument( $key );
125  }
126  return $arguments;
127  }
128 
132  public function getNamedArguments() {
133  $arguments = [];
134  foreach ( array_keys( $this->namedArgs ) as $key ) {
135  $arguments[$key] = $this->getArgument( $key );
136  }
137  return $arguments;
138  }
139 
144  public function getNumberedArgument( $index ) {
145  if ( !isset( $this->numberedArgs[$index] ) ) {
146  return false;
147  }
148  if ( !isset( $this->numberedExpansionCache[$index] ) ) {
149  # No trimming for unnamed arguments
150  $this->numberedExpansionCache[$index] = $this->parent->expand(
151  $this->numberedArgs[$index],
153  );
154  }
155  return $this->numberedExpansionCache[$index];
156  }
157 
162  public function getNamedArgument( $name ) {
163  if ( !isset( $this->namedArgs[$name] ) ) {
164  return false;
165  }
166  if ( !isset( $this->namedExpansionCache[$name] ) ) {
167  # Trim named arguments post-expand, for backwards compatibility
168  $this->namedExpansionCache[$name] = trim(
169  $this->parent->expand( $this->namedArgs[$name], PPFrame::STRIP_COMMENTS ) );
170  }
171  return $this->namedExpansionCache[$name];
172  }
173 
178  public function getArgument( $name ) {
179  $text = $this->getNumberedArgument( $name );
180  if ( $text === false ) {
181  $text = $this->getNamedArgument( $name );
182  }
183  return $text;
184  }
185 
191  public function isTemplate() {
192  return true;
193  }
194 
195  public function setVolatile( $flag = true ) {
196  parent::setVolatile( $flag );
197  $this->parent->setVolatile( $flag );
198  }
199 
200  public function setTTL( $ttl ) {
201  parent::setTTL( $ttl );
202  $this->parent->setTTL( $ttl );
203  }
204 }
PPFrame\STRIP_COMMENTS
const STRIP_COMMENTS
Definition: PPFrame.php:31
PPTemplateFrame_Hash\setVolatile
setVolatile( $flag=true)
Set the volatile flag.
Definition: PPTemplateFrame_Hash.php:195
PPFrame_Hash\expand
expand( $root, $flags=0)
Definition: PPFrame_Hash.php:151
PPTemplateFrame_Hash\$parent
$parent
Definition: PPTemplateFrame_Hash.php:29
PPTemplateFrame_Hash\$numberedArgs
$numberedArgs
Definition: PPTemplateFrame_Hash.php:29
Title\getPrefixedDBkey
getPrefixedDBkey()
Get the prefixed database key form.
Definition: Title.php:1806
PPTemplateFrame_Hash\$numberedExpansionCache
$numberedExpansionCache
Definition: PPTemplateFrame_Hash.php:30
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:39
PPTemplateFrame_Hash\getNumberedArgument
getNumberedArgument( $index)
Definition: PPTemplateFrame_Hash.php:144
$s
$s
Definition: mergeMessageFileList.php:185
PPTemplateFrame_Hash\isEmpty
isEmpty()
Returns true if there are no arguments in this frame.
Definition: PPTemplateFrame_Hash.php:101
PPTemplateFrame_Hash\$namedArgs
$namedArgs
Definition: PPTemplateFrame_Hash.php:29
PPTemplateFrame_Hash\getArguments
getArguments()
Definition: PPTemplateFrame_Hash.php:108
PPTemplateFrame_Hash\getNamedArgument
getNamedArgument( $name)
Definition: PPTemplateFrame_Hash.php:162
PPTemplateFrame_Hash\getNamedArguments
getNamedArguments()
Definition: PPTemplateFrame_Hash.php:132
PPTemplateFrame_Hash\cachedExpand
cachedExpand( $key, $root, $flags=0)
Definition: PPTemplateFrame_Hash.php:85
PPTemplateFrame_Hash
Expansion frame with template arguments.
Definition: PPTemplateFrame_Hash.php:27
PPFrame_Hash\$preprocessor
Preprocessor $preprocessor
Definition: PPFrame_Hash.php:37
$args
if( $line===false) $args
Definition: cdb.php:64
PPTemplateFrame_Hash\$namedExpansionCache
$namedExpansionCache
Definition: PPTemplateFrame_Hash.php:30
PPFrame_Hash\isVolatile
isVolatile()
Get the volatile flag.
Definition: PPFrame_Hash.php:590
PPFrame_Hash\$ttl
$ttl
Definition: PPFrame_Hash.php:58
PPTemplateFrame_Hash\isTemplate
isTemplate()
Return true if the frame is a template frame.
Definition: PPTemplateFrame_Hash.php:191
PPTemplateFrame_Hash\getArgument
getArgument( $name)
Definition: PPTemplateFrame_Hash.php:178
PPFrame_Hash\$title
Title $title
Definition: PPFrame_Hash.php:42
PPTemplateFrame_Hash\__toString
__toString()
Definition: PPTemplateFrame_Hash.php:61
PPTemplateFrame_Hash\getNumberedArguments
getNumberedArguments()
Definition: PPTemplateFrame_Hash.php:121
PPTemplateFrame_Hash\setTTL
setTTL( $ttl)
Set the TTL.
Definition: PPTemplateFrame_Hash.php:200