MediaWiki  master
PPTemplateFrame_DOM.php
Go to the documentation of this file.
1 <?php
27 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
29 
31 
35  public $parent;
37 
45  public function __construct( $preprocessor, $parent = false, $numberedArgs = [],
46  $namedArgs = [], $title = false
47  ) {
48  parent::__construct( $preprocessor );
49 
50  $this->parent = $parent;
51  $this->numberedArgs = $numberedArgs;
52  $this->namedArgs = $namedArgs;
53  $this->title = $title;
54  $pdbk = $title ? $title->getPrefixedDBkey() : false;
55  $this->titleCache = $parent->titleCache;
56  $this->titleCache[] = $pdbk;
57  $this->loopCheckHash = /*clone*/ $parent->loopCheckHash;
58  if ( $pdbk !== false ) {
59  $this->loopCheckHash[$pdbk] = true;
60  }
61  $this->depth = $parent->depth + 1;
62  $this->numberedExpansionCache = $this->namedExpansionCache = [];
63  }
64 
65  public function __toString() {
66  $s = 'tplframe{';
67  $first = true;
68  $args = $this->numberedArgs + $this->namedArgs;
69  foreach ( $args as $name => $value ) {
70  if ( $first ) {
71  $first = false;
72  } else {
73  $s .= ', ';
74  }
75  $s .= "\"$name\":\"" .
76  str_replace( '"', '\\"', $value->ownerDocument->saveXML( $value ) ) . '"';
77  }
78  $s .= '}';
79  return $s;
80  }
81 
89  public function cachedExpand( $key, $root, $flags = 0 ) {
90  if ( isset( $this->parent->childExpansionCache[$key] ) ) {
91  return $this->parent->childExpansionCache[$key];
92  }
93  $retval = $this->expand( $root, $flags );
94  if ( !$this->isVolatile() ) {
95  $this->parent->childExpansionCache[$key] = $retval;
96  }
97  return $retval;
98  }
99 
105  public function isEmpty() {
106  return !count( $this->numberedArgs ) && !count( $this->namedArgs );
107  }
108 
109  public function getArguments() {
110  $arguments = [];
111  foreach ( array_merge(
112  array_keys( $this->numberedArgs ),
113  array_keys( $this->namedArgs ) ) as $key ) {
114  $arguments[$key] = $this->getArgument( $key );
115  }
116  return $arguments;
117  }
118 
119  public function getNumberedArguments() {
120  $arguments = [];
121  foreach ( array_keys( $this->numberedArgs ) as $key ) {
122  $arguments[$key] = $this->getArgument( $key );
123  }
124  return $arguments;
125  }
126 
127  public function getNamedArguments() {
128  $arguments = [];
129  foreach ( array_keys( $this->namedArgs ) as $key ) {
130  $arguments[$key] = $this->getArgument( $key );
131  }
132  return $arguments;
133  }
134 
139  public function getNumberedArgument( $index ) {
140  if ( !isset( $this->numberedArgs[$index] ) ) {
141  return false;
142  }
143  if ( !isset( $this->numberedExpansionCache[$index] ) ) {
144  # No trimming for unnamed arguments
145  $this->numberedExpansionCache[$index] = $this->parent->expand(
146  $this->numberedArgs[$index],
148  );
149  }
150  return $this->numberedExpansionCache[$index];
151  }
152 
157  public function getNamedArgument( $name ) {
158  if ( !isset( $this->namedArgs[$name] ) ) {
159  return false;
160  }
161  if ( !isset( $this->namedExpansionCache[$name] ) ) {
162  # Trim named arguments post-expand, for backwards compatibility
163  $this->namedExpansionCache[$name] = trim(
164  $this->parent->expand( $this->namedArgs[$name], PPFrame::STRIP_COMMENTS ) );
165  }
166  return $this->namedExpansionCache[$name];
167  }
168 
173  public function getArgument( $name ) {
174  $text = $this->getNumberedArgument( $name );
175  if ( $text === false ) {
176  $text = $this->getNamedArgument( $name );
177  }
178  return $text;
179  }
180 
186  public function isTemplate() {
187  return true;
188  }
189 
190  public function setVolatile( $flag = true ) {
191  parent::setVolatile( $flag );
192  $this->parent->setVolatile( $flag );
193  }
194 
195  public function setTTL( $ttl ) {
196  parent::setTTL( $ttl );
197  $this->parent->setTTL( $ttl );
198  }
199 }
Expansion frame with template arguments.
getArguments()
Returns all arguments of this frame.
expand( $root, $flags=0)
if( $line===false) $args
Definition: cdb.php:64
isTemplate()
Return true if the frame is a template frame.
cachedExpand( $key, $root, $flags=0)
Title $title
Definition: PPFrame_DOM.php:44
isEmpty()
Returns true if there are no arguments in this frame.
Preprocessor $preprocessor
Definition: PPFrame_DOM.php:34
An expansion frame, used as a context to expand the result of preprocessToObj()
Definition: PPFrame_DOM.php:29
setVolatile( $flag=true)
Set the "volatile" flag.
__construct( $preprocessor, $parent=false, $numberedArgs=[], $namedArgs=[], $title=false)
setTTL( $ttl)
Set the TTL of the output of this frame and all of its ancestors.
isVolatile()
Get the volatile flag.
getNamedArguments()
Returns all named arguments of this frame.
const STRIP_COMMENTS
Definition: PPFrame.php:31
getNumberedArguments()
Returns all numbered arguments of this frame.
getPrefixedDBkey()
Get the prefixed database key form.
Definition: Title.php:1846