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_Hash
extends PPFrame_Hash
{
29 public $numberedArgs, $namedArgs, $parent;
30 public $numberedExpansionCache, $namedExpansionCache;
33 * @param Preprocessor $preprocessor
34 * @param bool|PPFrame $parent
35 * @param array $numberedArgs
36 * @param array $namedArgs
37 * @param bool|Title $title
39 public function __construct( $preprocessor, $parent = false, $numberedArgs = [],
40 $namedArgs = [], $title = false
42 parent
::__construct( $preprocessor );
43 /** @var PPFrame_Hash parent */
44 '@phan-var PPFrame_Hash $parent';
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;
57 $this->depth
= $parent->depth +
1;
58 $this->numberedExpansionCache
= $this->namedExpansionCache
= [];
61 public function __toString() {
64 $args = $this->numberedArgs +
$this->namedArgs
;
65 foreach ( $args as $name => $value ) {
71 $s .= "\"$name\":\"" .
72 str_replace( '"', '\\"', $value->__toString() ) . '"';
80 * @param string|int $key
81 * @param string|PPNode $root
85 public function cachedExpand( $key, $root, $flags = 0 ) {
86 if ( isset( $this->parent
->childExpansionCache
[$key] ) ) {
87 return $this->parent
->childExpansionCache
[$key];
89 $retval = $this->expand( $root, $flags );
90 if ( !$this->isVolatile() ) {
91 $this->parent
->childExpansionCache
[$key] = $retval;
97 * Returns true if there are no arguments in this frame
101 public function isEmpty() {
102 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 );
121 public function getNumberedArguments() {
123 foreach ( array_keys( $this->numberedArgs
) as $key ) {
124 $arguments[$key] = $this->getArgument( $key );
132 public function getNamedArguments() {
134 foreach ( array_keys( $this->namedArgs
) as $key ) {
135 $arguments[$key] = $this->getArgument( $key );
142 * @return string|bool
144 public function getNumberedArgument( $index ) {
145 if ( !isset( $this->numberedArgs
[$index] ) ) {
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
155 return $this->numberedExpansionCache
[$index];
159 * @param string $name
160 * @return string|bool
162 public function getNamedArgument( $name ) {
163 if ( !isset( $this->namedArgs
[$name] ) ) {
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
) );
171 return $this->namedExpansionCache
[$name];
175 * @param int|string $name
176 * @return string|bool
178 public function getArgument( $name ) {
179 $text = $this->getNumberedArgument( $name );
180 if ( $text === false ) {
181 $text = $this->getNamedArgument( $name );
187 * Return true if the frame is a template frame
191 public function isTemplate() {
195 public function setVolatile( $flag = true ) {
196 parent
::setVolatile( $flag );
197 $this->parent
->setVolatile( $flag );
200 public function setTTL( $ttl ) {
201 parent
::setTTL( $ttl );
202 $this->parent
->setTTL( $ttl );