Merge "Provide command to adjust phpunit.xml for code coverage"
[lhc/web/wiklou.git] / includes / parser / PPTemplateFrame_Hash.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup Parser
20 */
21
22 /**
23 * Expansion frame with template arguments
24 * @ingroup Parser
25 */
26 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
27 class PPTemplateFrame_Hash extends PPFrame_Hash {
28
29 public $numberedArgs, $namedArgs, $parent;
30 public $numberedExpansionCache, $namedExpansionCache;
31
32 /**
33 * @param Preprocessor $preprocessor
34 * @param bool|PPFrame $parent
35 * @param array $numberedArgs
36 * @param array $namedArgs
37 * @param bool|Title $title
38 */
39 public function __construct( $preprocessor, $parent = false, $numberedArgs = [],
40 $namedArgs = [], $title = false
41 ) {
42 parent::__construct( $preprocessor );
43 /** @var PPFrame_Hash parent */
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
78 /**
79 * @throws MWException
80 * @param string|int $key
81 * @param string|PPNode $root
82 * @param int $flags
83 * @return string
84 */
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
96 /**
97 * Returns true if there are no arguments in this frame
98 *
99 * @return bool
100 */
101 public function isEmpty() {
102 return !count( $this->numberedArgs ) && !count( $this->namedArgs );
103 }
104
105 /**
106 * @return array
107 */
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
118 /**
119 * @return array
120 */
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
129 /**
130 * @return array
131 */
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
140 /**
141 * @param int $index
142 * @return string|bool
143 */
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],
152 PPFrame::STRIP_COMMENTS
153 );
154 }
155 return $this->numberedExpansionCache[$index];
156 }
157
158 /**
159 * @param string $name
160 * @return string|bool
161 */
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
174 /**
175 * @param int|string $name
176 * @return string|bool
177 */
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
186 /**
187 * Return true if the frame is a template frame
188 *
189 * @return bool
190 */
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 }