MediaWiki  1.34.0
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 }
PPFrame_DOM\$preprocessor
Preprocessor $preprocessor
Definition: PPFrame_DOM.php:34
PPFrame\STRIP_COMMENTS
const STRIP_COMMENTS
Definition: PPFrame.php:31
PPFrame_DOM\isVolatile
isVolatile()
Get the volatile flag.
Definition: PPFrame_DOM.php:613
PPTemplateFrame_DOM\$namedExpansionCache
$namedExpansionCache
Definition: PPTemplateFrame_DOM.php:36
Title\getPrefixedDBkey
getPrefixedDBkey()
Get the prefixed database key form.
Definition: Title.php:1806
PPTemplateFrame_DOM\$numberedExpansionCache
$numberedExpansionCache
Definition: PPTemplateFrame_DOM.php:36
PPFrame_DOM\$ttl
$ttl
Definition: PPFrame_DOM.php:60
PPTemplateFrame_DOM\getNumberedArgument
getNumberedArgument( $index)
Definition: PPTemplateFrame_DOM.php:139
PPTemplateFrame_DOM\getNumberedArguments
getNumberedArguments()
Definition: PPTemplateFrame_DOM.php:119
$s
$s
Definition: mergeMessageFileList.php:185
PPTemplateFrame_DOM\__construct
__construct( $preprocessor, $parent=false, $numberedArgs=[], $namedArgs=[], $title=false)
Definition: PPTemplateFrame_DOM.php:45
PPTemplateFrame_DOM\getNamedArguments
getNamedArguments()
Definition: PPTemplateFrame_DOM.php:127
PPTemplateFrame_DOM\isEmpty
isEmpty()
Returns true if there are no arguments in this frame.
Definition: PPTemplateFrame_DOM.php:105
PPTemplateFrame_DOM\__toString
__toString()
Definition: PPTemplateFrame_DOM.php:65
PPTemplateFrame_DOM\$parent
PPFrame_DOM $parent
Definition: PPTemplateFrame_DOM.php:35
PPTemplateFrame_DOM\$namedArgs
$namedArgs
Definition: PPTemplateFrame_DOM.php:30
PPFrame_DOM\expand
expand( $root, $flags=0)
Definition: PPFrame_DOM.php:161
PPFrame_DOM
An expansion frame, used as a context to expand the result of preprocessToObj()
Definition: PPFrame_DOM.php:29
PPTemplateFrame_DOM\$numberedArgs
$numberedArgs
Definition: PPTemplateFrame_DOM.php:30
PPTemplateFrame_DOM\getArguments
getArguments()
Definition: PPTemplateFrame_DOM.php:109
$args
if( $line===false) $args
Definition: cdb.php:64
PPTemplateFrame_DOM\cachedExpand
cachedExpand( $key, $root, $flags=0)
Definition: PPTemplateFrame_DOM.php:89
PPFrame_DOM\$title
Title $title
Definition: PPFrame_DOM.php:44
PPTemplateFrame_DOM\setTTL
setTTL( $ttl)
Set the TTL.
Definition: PPTemplateFrame_DOM.php:195
PPTemplateFrame_DOM\isTemplate
isTemplate()
Return true if the frame is a template frame.
Definition: PPTemplateFrame_DOM.php:186
PPTemplateFrame_DOM\getArgument
getArgument( $name)
Definition: PPTemplateFrame_DOM.php:173
PPTemplateFrame_DOM\setVolatile
setVolatile( $flag=true)
Set the volatile flag.
Definition: PPTemplateFrame_DOM.php:190
PPTemplateFrame_DOM\getNamedArgument
getNamedArgument( $name)
Definition: PPTemplateFrame_DOM.php:157
PPTemplateFrame_DOM
Expansion frame with template arguments.
Definition: PPTemplateFrame_DOM.php:28