MediaWiki master
PPTemplateFrame_Hash.php
Go to the documentation of this file.
1<?php
22namespace MediaWiki\Parser;
23
25
30// phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
32
36 public $namedArgs;
38 public $parent;
43
51 public function __construct( $preprocessor, $parent = false, $numberedArgs = [],
52 $namedArgs = [], $title = false
53 ) {
54 parent::__construct( $preprocessor );
56 '@phan-var PPFrame_Hash $parent';
57
58 $this->parent = $parent;
59 $this->numberedArgs = $numberedArgs;
60 $this->namedArgs = $namedArgs;
61 $this->title = $title;
62 $pdbk = $title ? $title->getPrefixedDBkey() : false;
63 $this->titleCache = $parent->titleCache;
64 $this->titleCache[] = $pdbk;
65 $this->loopCheckHash = /*clone*/ $parent->loopCheckHash;
66 if ( $pdbk !== false ) {
67 $this->loopCheckHash[$pdbk] = true;
68 }
69 $this->depth = $parent->depth + 1;
70 $this->numberedExpansionCache = $this->namedExpansionCache = [];
71 }
72
73 public function __toString() {
74 $s = 'tplframe{';
75 $first = true;
76 $args = $this->numberedArgs + $this->namedArgs;
77 foreach ( $args as $name => $value ) {
78 if ( $first ) {
79 $first = false;
80 } else {
81 $s .= ', ';
82 }
83 $s .= "\"$name\":\"" .
84 str_replace( '"', '\\"', $value->__toString() ) . '"';
85 }
86 $s .= '}';
87 return $s;
88 }
89
96 public function cachedExpand( $key, $root, $flags = 0 ) {
97 if ( isset( $this->parent->childExpansionCache[$key] ) ) {
98 return $this->parent->childExpansionCache[$key];
99 }
100 $retval = $this->expand( $root, $flags );
101 if ( !$this->isVolatile() ) {
102 $this->parent->childExpansionCache[$key] = $retval;
103 }
104 return $retval;
105 }
106
112 public function isEmpty() {
113 return !count( $this->numberedArgs ) && !count( $this->namedArgs );
114 }
115
119 public function getArguments() {
120 $arguments = [];
121 foreach ( array_merge(
122 array_keys( $this->numberedArgs ),
123 array_keys( $this->namedArgs ) ) as $key ) {
124 $arguments[$key] = $this->getArgument( $key );
125 }
126 return $arguments;
127 }
128
132 public function getNumberedArguments() {
133 $arguments = [];
134 foreach ( $this->numberedArgs as $key => $_ ) {
135 $arguments[$key] = $this->getArgument( $key );
136 }
137 return $arguments;
138 }
139
143 public function getNamedArguments() {
144 $arguments = [];
145 foreach ( $this->namedArgs as $key => $_ ) {
146 $arguments[$key] = $this->getArgument( $key );
147 }
148 return $arguments;
149 }
150
155 public function getNumberedArgument( $index ) {
156 if ( !isset( $this->numberedArgs[$index] ) ) {
157 return false;
158 }
159 if ( !isset( $this->numberedExpansionCache[$index] ) ) {
160 # No trimming for unnamed arguments
161 $this->numberedExpansionCache[$index] = $this->parent->expand(
162 $this->numberedArgs[$index],
164 );
165 }
166 return $this->numberedExpansionCache[$index];
167 }
168
173 public function getNamedArgument( $name ) {
174 if ( !isset( $this->namedArgs[$name] ) ) {
175 return false;
176 }
177 if ( !isset( $this->namedExpansionCache[$name] ) ) {
178 # Trim named arguments post-expand, for backwards compatibility
179 $this->namedExpansionCache[$name] = trim(
180 $this->parent->expand( $this->namedArgs[$name], PPFrame::STRIP_COMMENTS ) );
181 }
182 return $this->namedExpansionCache[$name];
183 }
184
189 public function getArgument( $name ) {
190 $text = $this->getNumberedArgument( $name );
191 if ( $text === false ) {
192 $text = $this->getNamedArgument( $name );
193 }
194 return $text;
195 }
196
202 public function isTemplate() {
203 return true;
204 }
205
206 public function setVolatile( $flag = true ) {
207 parent::setVolatile( $flag );
208 $this->parent->setVolatile( $flag );
209 }
210
211 public function setTTL( $ttl ) {
212 parent::setTTL( $ttl );
213 $this->parent->setTTL( $ttl );
214 }
215}
216
218class_alias( PPTemplateFrame_Hash::class, 'PPTemplateFrame_Hash' );
An expansion frame, used as a context to expand the result of preprocessToObj()
isVolatile()
Get the volatile flag.
Expansion frame with template arguments.
isTemplate()
Return true if the frame is a template frame.
isEmpty()
Returns true if there are no arguments in this frame.
__construct( $preprocessor, $parent=false, $numberedArgs=[], $namedArgs=[], $title=false)
setVolatile( $flag=true)
Set the volatile flag.
Represents a title within MediaWiki.
Definition Title.php:78
getPrefixedDBkey()
Get the prefixed database key form.
Definition Title.php:1846