Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[lhc/web/wiklou.git] / includes / parser / PPTemplateFrame_DOM.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 * @deprecated since 1.34, use PPTemplateFrame_Hash
25 * @ingroup Parser
26 */
27 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
28 class PPTemplateFrame_DOM extends PPFrame_DOM {
29
30 public $numberedArgs, $namedArgs;
31
32 /**
33 * @var PPFrame_DOM
34 */
35 public $parent;
36 public $numberedExpansionCache, $namedExpansionCache;
37
38 /**
39 * @param Preprocessor $preprocessor
40 * @param bool|PPFrame_DOM $parent
41 * @param array $numberedArgs
42 * @param array $namedArgs
43 * @param bool|Title $title
44 */
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
82 /**
83 * @throws MWException
84 * @param string|int $key
85 * @param string|PPNode_DOM|DOMDocument $root
86 * @param int $flags
87 * @return string
88 */
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
100 /**
101 * Returns true if there are no arguments in this frame
102 *
103 * @return bool
104 */
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
135 /**
136 * @param int $index
137 * @return string|bool
138 */
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],
147 PPFrame::STRIP_COMMENTS
148 );
149 }
150 return $this->numberedExpansionCache[$index];
151 }
152
153 /**
154 * @param string $name
155 * @return string|bool
156 */
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
169 /**
170 * @param int|string $name
171 * @return string|bool
172 */
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
181 /**
182 * Return true if the frame is a template frame
183 *
184 * @return bool
185 */
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 }