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.
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.
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
23 * Expansion frame with template arguments
26 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
27 class PPTemplateFrame_DOM
extends PPFrame_DOM
{
29 public $numberedArgs, $namedArgs;
35 public $numberedExpansionCache, $namedExpansionCache;
38 * @param Preprocessor $preprocessor
39 * @param bool|PPFrame_DOM $parent
40 * @param array $numberedArgs
41 * @param array $namedArgs
42 * @param bool|Title $title
44 public function __construct( $preprocessor, $parent = false, $numberedArgs = [],
45 $namedArgs = [], $title = false
47 parent
::__construct( $preprocessor );
49 $this->parent
= $parent;
50 $this->numberedArgs
= $numberedArgs;
51 $this->namedArgs
= $namedArgs;
52 $this->title
= $title;
53 $pdbk = $title ?
$title->getPrefixedDBkey() : false;
54 $this->titleCache
= $parent->titleCache
;
55 $this->titleCache
[] = $pdbk;
56 $this->loopCheckHash
= /*clone*/ $parent->loopCheckHash
;
57 if ( $pdbk !== false ) {
58 $this->loopCheckHash
[$pdbk] = true;
60 $this->depth
= $parent->depth +
1;
61 $this->numberedExpansionCache
= $this->namedExpansionCache
= [];
64 public function __toString() {
67 $args = $this->numberedArgs +
$this->namedArgs
;
68 foreach ( $args as $name => $value ) {
74 $s .= "\"$name\":\"" .
75 str_replace( '"', '\\"', $value->ownerDocument
->saveXML( $value ) ) . '"';
83 * @param string|int $key
84 * @param string|PPNode_DOM|DOMDocument $root
88 public function cachedExpand( $key, $root, $flags = 0 ) {
89 if ( isset( $this->parent
->childExpansionCache
[$key] ) ) {
90 return $this->parent
->childExpansionCache
[$key];
92 $retval = $this->expand( $root, $flags );
93 if ( !$this->isVolatile() ) {
94 $this->parent
->childExpansionCache
[$key] = $retval;
100 * Returns true if there are no arguments in this frame
104 public function isEmpty() {
105 return !count( $this->numberedArgs
) && !count( $this->namedArgs
);
108 public function getArguments() {
110 foreach ( array_merge(
111 array_keys( $this->numberedArgs
),
112 array_keys( $this->namedArgs
) ) as $key ) {
113 $arguments[$key] = $this->getArgument( $key );
118 public function getNumberedArguments() {
120 foreach ( array_keys( $this->numberedArgs
) as $key ) {
121 $arguments[$key] = $this->getArgument( $key );
126 public function getNamedArguments() {
128 foreach ( array_keys( $this->namedArgs
) as $key ) {
129 $arguments[$key] = $this->getArgument( $key );
136 * @return string|bool
138 public function getNumberedArgument( $index ) {
139 if ( !isset( $this->numberedArgs
[$index] ) ) {
142 if ( !isset( $this->numberedExpansionCache
[$index] ) ) {
143 # No trimming for unnamed arguments
144 $this->numberedExpansionCache
[$index] = $this->parent
->expand(
145 $this->numberedArgs
[$index],
146 PPFrame
::STRIP_COMMENTS
149 return $this->numberedExpansionCache
[$index];
153 * @param string $name
154 * @return string|bool
156 public function getNamedArgument( $name ) {
157 if ( !isset( $this->namedArgs
[$name] ) ) {
160 if ( !isset( $this->namedExpansionCache
[$name] ) ) {
161 # Trim named arguments post-expand, for backwards compatibility
162 $this->namedExpansionCache
[$name] = trim(
163 $this->parent
->expand( $this->namedArgs
[$name], PPFrame
::STRIP_COMMENTS
) );
165 return $this->namedExpansionCache
[$name];
169 * @param int|string $name
170 * @return string|bool
172 public function getArgument( $name ) {
173 $text = $this->getNumberedArgument( $name );
174 if ( $text === false ) {
175 $text = $this->getNamedArgument( $name );
181 * Return true if the frame is a template frame
185 public function isTemplate() {
189 public function setVolatile( $flag = true ) {
190 parent
::setVolatile( $flag );
191 $this->parent
->setVolatile( $flag );
194 public function setTTL( $ttl ) {
195 parent
::setTTL( $ttl );
196 $this->parent
->setTTL( $ttl );