Fix use of GenderCache in ApiPageSet::processTitlesArray
[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
44 $this->parent = $parent;
45 $this->numberedArgs = $numberedArgs;
46 $this->namedArgs = $namedArgs;
47 $this->title = $title;
48 $pdbk = $title ? $title->getPrefixedDBkey() : false;
49 $this->titleCache = $parent->titleCache;
50 $this->titleCache[] = $pdbk;
51 $this->loopCheckHash = /*clone*/ $parent->loopCheckHash;
52 if ( $pdbk !== false ) {
53 $this->loopCheckHash[$pdbk] = true;
54 }
55 $this->depth = $parent->depth + 1;
56 $this->numberedExpansionCache = $this->namedExpansionCache = [];
57 }
58
59 public function __toString() {
60 $s = 'tplframe{';
61 $first = true;
62 $args = $this->numberedArgs + $this->namedArgs;
63 foreach ( $args as $name => $value ) {
64 if ( $first ) {
65 $first = false;
66 } else {
67 $s .= ', ';
68 }
69 $s .= "\"$name\":\"" .
70 str_replace( '"', '\\"', $value->__toString() ) . '"';
71 }
72 $s .= '}';
73 return $s;
74 }
75
76 /**
77 * @throws MWException
78 * @param string|int $key
79 * @param string|PPNode $root
80 * @param int $flags
81 * @return string
82 */
83 public function cachedExpand( $key, $root, $flags = 0 ) {
84 if ( isset( $this->parent->childExpansionCache[$key] ) ) {
85 return $this->parent->childExpansionCache[$key];
86 }
87 $retval = $this->expand( $root, $flags );
88 if ( !$this->isVolatile() ) {
89 $this->parent->childExpansionCache[$key] = $retval;
90 }
91 return $retval;
92 }
93
94 /**
95 * Returns true if there are no arguments in this frame
96 *
97 * @return bool
98 */
99 public function isEmpty() {
100 return !count( $this->numberedArgs ) && !count( $this->namedArgs );
101 }
102
103 /**
104 * @return array
105 */
106 public function getArguments() {
107 $arguments = [];
108 foreach ( array_merge(
109 array_keys( $this->numberedArgs ),
110 array_keys( $this->namedArgs ) ) as $key ) {
111 $arguments[$key] = $this->getArgument( $key );
112 }
113 return $arguments;
114 }
115
116 /**
117 * @return array
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 /**
128 * @return array
129 */
130 public function getNamedArguments() {
131 $arguments = [];
132 foreach ( array_keys( $this->namedArgs ) as $key ) {
133 $arguments[$key] = $this->getArgument( $key );
134 }
135 return $arguments;
136 }
137
138 /**
139 * @param int $index
140 * @return string|bool
141 */
142 public function getNumberedArgument( $index ) {
143 if ( !isset( $this->numberedArgs[$index] ) ) {
144 return false;
145 }
146 if ( !isset( $this->numberedExpansionCache[$index] ) ) {
147 # No trimming for unnamed arguments
148 $this->numberedExpansionCache[$index] = $this->parent->expand(
149 $this->numberedArgs[$index],
150 PPFrame::STRIP_COMMENTS
151 );
152 }
153 return $this->numberedExpansionCache[$index];
154 }
155
156 /**
157 * @param string $name
158 * @return string|bool
159 */
160 public function getNamedArgument( $name ) {
161 if ( !isset( $this->namedArgs[$name] ) ) {
162 return false;
163 }
164 if ( !isset( $this->namedExpansionCache[$name] ) ) {
165 # Trim named arguments post-expand, for backwards compatibility
166 $this->namedExpansionCache[$name] = trim(
167 $this->parent->expand( $this->namedArgs[$name], PPFrame::STRIP_COMMENTS ) );
168 }
169 return $this->namedExpansionCache[$name];
170 }
171
172 /**
173 * @param int|string $name
174 * @return string|bool
175 */
176 public function getArgument( $name ) {
177 $text = $this->getNumberedArgument( $name );
178 if ( $text === false ) {
179 $text = $this->getNamedArgument( $name );
180 }
181 return $text;
182 }
183
184 /**
185 * Return true if the frame is a template frame
186 *
187 * @return bool
188 */
189 public function isTemplate() {
190 return true;
191 }
192
193 public function setVolatile( $flag = true ) {
194 parent::setVolatile( $flag );
195 $this->parent->setVolatile( $flag );
196 }
197
198 public function setTTL( $ttl ) {
199 parent::setTTL( $ttl );
200 $this->parent->setTTL( $ttl );
201 }
202 }