Debian Codegraph @

includes/parser/PPTemplateFrame_Hash.php

raw symbols SCIP
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 namespace MediaWiki\Parser;
23
24 use MediaWiki\Title\Title;
25
26 /**
27 * Expansion frame with template arguments
28 * @ingroup Parser
29 */
30 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
31 class PPTemplateFrame_Hash extends PPFrame_Hash {
32
33 /** @var array */
34 public $numberedArgs;
35 /** @var array */
36 public $namedArgs;
37 /** @var PPFrame_Hash */
38 public $parent;
39 /** @var array */
40 public $numberedExpansionCache;
41 /** @var array */
42 public $namedExpansionCache;
43
44 /**
45 * @param Preprocessor $preprocessor
46 * @param false|PPFrame $parent
47 * @param array $numberedArgs
48 * @param array $namedArgs
49 * @param false|Title $title
50 */
51 public function __construct( $preprocessor, $parent = false, $numberedArgs = [],
52 $namedArgs = [], $title = false
53 ) {
54 parent::__construct( $preprocessor );
55 /** @var PPFrame_Hash $parent */
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
90 /**
91 * @param string|int $key
92 * @param string|PPNode $root
93 * @param int $flags
94 * @return string
95 */
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
107 /**
108 * Returns true if there are no arguments in this frame
109 *
110 * @return bool
111 */
112 public function isEmpty() {
113 return !count( $this->numberedArgs ) && !count( $this->namedArgs );
114 }
115
116 /**
117 * @return array
118 */
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
129 /**
130 * @return array
131 */
132 public function getNumberedArguments() {
133 $arguments = [];
134 foreach ( $this->numberedArgs as $key => $_ ) {
135 $arguments[$key] = $this->getArgument( $key );
136 }
137 return $arguments;
138 }
139
140 /**
141 * @return array
142 */
143 public function getNamedArguments() {
144 $arguments = [];
145 foreach ( $this->namedArgs as $key => $_ ) {
146 $arguments[$key] = $this->getArgument( $key );
147 }
148 return $arguments;
149 }
150
151 /**
152 * @param int $index
153 * @return string|false
154 */
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],
163 PPFrame::STRIP_COMMENTS
164 );
165 }
166 return $this->numberedExpansionCache[$index];
167 }
168
169 /**
170 * @param string $name
171 * @return string|false
172 */
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
185 /**
186 * @param int|string $name
187 * @return string|false
188 */
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
197 /**
198 * Return true if the frame is a template frame
199 *
200 * @return bool
201 */
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
217 /** @deprecated class alias since 1.43 */
218 class_alias( PPTemplateFrame_Hash::class, 'PPTemplateFrame_Hash' );