Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 68
0.00% covered (danger)
0.00%
0 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
PPTemplateFrame_Hash
0.00% covered (danger)
0.00%
0 / 68
0.00% covered (danger)
0.00%
0 / 13
812
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
12
 __toString
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
 cachedExpand
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 isEmpty
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 getArguments
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 getNumberedArguments
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getNamedArguments
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getNumberedArgument
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 getNamedArgument
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 getArgument
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 isTemplate
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setVolatile
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 setTTL
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
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
22use MediaWiki\Title\Title;
23
24/**
25 * Expansion frame with template arguments
26 * @ingroup Parser
27 */
28// phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
29class PPTemplateFrame_Hash extends PPFrame_Hash {
30
31    /** @var array */
32    public $numberedArgs;
33    /** @var array */
34    public $namedArgs;
35    /** @var PPFrame_Hash */
36    public $parent;
37    /** @var array */
38    public $numberedExpansionCache;
39    /** @var array */
40    public $namedExpansionCache;
41
42    /**
43     * @param Preprocessor $preprocessor
44     * @param false|PPFrame $parent
45     * @param array $numberedArgs
46     * @param array $namedArgs
47     * @param false|Title $title
48     */
49    public function __construct( $preprocessor, $parent = false, $numberedArgs = [],
50        $namedArgs = [], $title = false
51    ) {
52        parent::__construct( $preprocessor );
53        /** @var PPFrame_Hash $parent */
54        '@phan-var PPFrame_Hash $parent';
55
56        $this->parent = $parent;
57        $this->numberedArgs = $numberedArgs;
58        $this->namedArgs = $namedArgs;
59        $this->title = $title;
60        $pdbk = $title ? $title->getPrefixedDBkey() : false;
61        $this->titleCache = $parent->titleCache;
62        $this->titleCache[] = $pdbk;
63        $this->loopCheckHash = /*clone*/ $parent->loopCheckHash;
64        if ( $pdbk !== false ) {
65            $this->loopCheckHash[$pdbk] = true;
66        }
67        $this->depth = $parent->depth + 1;
68        $this->numberedExpansionCache = $this->namedExpansionCache = [];
69    }
70
71    public function __toString() {
72        $s = 'tplframe{';
73        $first = true;
74        $args = $this->numberedArgs + $this->namedArgs;
75        foreach ( $args as $name => $value ) {
76            if ( $first ) {
77                $first = false;
78            } else {
79                $s .= ', ';
80            }
81            $s .= "\"$name\":\"" .
82                str_replace( '"', '\\"', $value->__toString() ) . '"';
83        }
84        $s .= '}';
85        return $s;
86    }
87
88    /**
89     * @param string|int $key
90     * @param string|PPNode $root
91     * @param int $flags
92     * @return string
93     */
94    public function cachedExpand( $key, $root, $flags = 0 ) {
95        if ( isset( $this->parent->childExpansionCache[$key] ) ) {
96            return $this->parent->childExpansionCache[$key];
97        }
98        $retval = $this->expand( $root, $flags );
99        if ( !$this->isVolatile() ) {
100            $this->parent->childExpansionCache[$key] = $retval;
101        }
102        return $retval;
103    }
104
105    /**
106     * Returns true if there are no arguments in this frame
107     *
108     * @return bool
109     */
110    public function isEmpty() {
111        return !count( $this->numberedArgs ) && !count( $this->namedArgs );
112    }
113
114    /**
115     * @return array
116     */
117    public function getArguments() {
118        $arguments = [];
119        foreach ( array_merge(
120                array_keys( $this->numberedArgs ),
121                array_keys( $this->namedArgs ) ) as $key ) {
122            $arguments[$key] = $this->getArgument( $key );
123        }
124        return $arguments;
125    }
126
127    /**
128     * @return array
129     */
130    public function getNumberedArguments() {
131        $arguments = [];
132        foreach ( $this->numberedArgs as $key => $_ ) {
133            $arguments[$key] = $this->getArgument( $key );
134        }
135        return $arguments;
136    }
137
138    /**
139     * @return array
140     */
141    public function getNamedArguments() {
142        $arguments = [];
143        foreach ( $this->namedArgs as $key => $_ ) {
144            $arguments[$key] = $this->getArgument( $key );
145        }
146        return $arguments;
147    }
148
149    /**
150     * @param int $index
151     * @return string|false
152     */
153    public function getNumberedArgument( $index ) {
154        if ( !isset( $this->numberedArgs[$index] ) ) {
155            return false;
156        }
157        if ( !isset( $this->numberedExpansionCache[$index] ) ) {
158            # No trimming for unnamed arguments
159            $this->numberedExpansionCache[$index] = $this->parent->expand(
160                $this->numberedArgs[$index],
161                PPFrame::STRIP_COMMENTS
162            );
163        }
164        return $this->numberedExpansionCache[$index];
165    }
166
167    /**
168     * @param string $name
169     * @return string|false
170     */
171    public function getNamedArgument( $name ) {
172        if ( !isset( $this->namedArgs[$name] ) ) {
173            return false;
174        }
175        if ( !isset( $this->namedExpansionCache[$name] ) ) {
176            # Trim named arguments post-expand, for backwards compatibility
177            $this->namedExpansionCache[$name] = trim(
178                $this->parent->expand( $this->namedArgs[$name], PPFrame::STRIP_COMMENTS ) );
179        }
180        return $this->namedExpansionCache[$name];
181    }
182
183    /**
184     * @param int|string $name
185     * @return string|false
186     */
187    public function getArgument( $name ) {
188        $text = $this->getNumberedArgument( $name );
189        if ( $text === false ) {
190            $text = $this->getNamedArgument( $name );
191        }
192        return $text;
193    }
194
195    /**
196     * Return true if the frame is a template frame
197     *
198     * @return bool
199     */
200    public function isTemplate() {
201        return true;
202    }
203
204    public function setVolatile( $flag = true ) {
205        parent::setVolatile( $flag );
206        $this->parent->setVolatile( $flag );
207    }
208
209    public function setTTL( $ttl ) {
210        parent::setTTL( $ttl );
211        $this->parent->setTTL( $ttl );
212    }
213}