Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
QuickTemplate
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 13
272
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 deprecate
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 set
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 extend
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 checkDeprecationStatus
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 get
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 execute
n/a
0 / 0
n/a
0 / 0
0
 text
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 html
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 msg
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 haveData
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 haveMsg
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSkin
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getHTML
0.00% covered (danger)
0.00%
0 / 5
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 */
20
21namespace MediaWiki\Skin;
22
23use MediaWiki\Config\Config;
24use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
25use MediaWiki\MediaWikiServices;
26
27/**
28 * PHP-based skin template that holds data.
29 *
30 * Modern usage with returned output:
31 *
32 *     class MyTemplate extends QuickTemplate {
33 *         public function execute() {
34 *             $html = 'Hello, ' . Html::element( 'strong', [], $this->get( 'name' ) );
35 *             echo $html;
36 *         }
37 *     }
38 *     $tpl = new MyTemplate();
39 *     $tpl->set( 'name', 'World' );
40 *     $output = $tpl->getHTML();
41 *
42 * Classic usage with native HTML echo:
43 *
44 *     class MyTemplate extends QuickTemplate {
45 *         public function execute() { ?>
46 *
47 *             Hello, <strong><?php $this->text( 'name' ); ?></strong>
48 *
49 *         <?php
50 *         }
51 *     }
52 *     $tpl = new MyTemplate();
53 *     $tpl->set( 'name', 'World' );
54 *
55 *     $tpl->execute(); // echo output
56 *
57 *
58 * QuickTemplate was originally developed as drop-in replacement for PHPTAL 0.7 (<http://phptal.org/>).
59 *
60 * @stable to extend
61 * @ingroup Skins
62 */
63abstract class QuickTemplate {
64    use ProtectedHookAccessorTrait;
65
66    /**
67     * @var array
68     */
69    public $data;
70
71    /** @var Config */
72    protected $config;
73
74    /** @var array */
75    private $deprecated = [];
76
77    /**
78     * @param Config|null $config
79     */
80    public function __construct( ?Config $config = null ) {
81        $this->data = [];
82        if ( $config === null ) {
83            wfDebug( __METHOD__ . ' was called with no Config instance passed to it' );
84            $config = MediaWikiServices::getInstance()->getMainConfig();
85        }
86        $this->config = $config;
87    }
88
89    /**
90     * Sets a template key as deprecated.
91     *
92     * @internal only for usage inside Skin and SkinTemplate class.
93     * @param string $name
94     * @param string $version When it was deprecated e.g. 1.38
95     */
96    public function deprecate( string $name, string $version ) {
97        $this->deprecated[$name] = $version;
98    }
99
100    /**
101     * Sets the value $value to $name
102     * @param string $name
103     * @param mixed $value
104     */
105    public function set( $name, $value ) {
106        $this->data[$name] = $value;
107    }
108
109    /**
110     * extends the value of data with name $name with the value $value
111     * @since 1.25
112     * @param string $name
113     * @param mixed $value
114     */
115    public function extend( $name, $value ) {
116        if ( $this->haveData( $name ) ) {
117            $this->data[$name] .= $value;
118        } else {
119            $this->data[$name] = $value;
120        }
121    }
122
123    /**
124     * Checks if the template key is deprecated
125     */
126    private function checkDeprecationStatus( string $name ) {
127        $deprecated = $this->deprecated[ $name ] ?? false;
128        if ( $deprecated ) {
129            wfDeprecated(
130                'QuickTemplate::(get/html/text/haveData) with parameter `' . $name . '`',
131                $deprecated
132            );
133        }
134    }
135
136    /**
137     * Gets the template data requested
138     * @since 1.22
139     * @param string $name Key for the data
140     * @param mixed|null $default Optional default (or null)
141     * @return mixed The value of the data requested or the default
142     * @return-taint onlysafefor_htmlnoent
143     */
144    public function get( $name, $default = null ) {
145        $this->checkDeprecationStatus( $name );
146        return $this->data[$name] ?? $default;
147    }
148
149    /**
150     * Main function, used by classes that subclass QuickTemplate
151     * to show the actual HTML output
152     */
153    abstract public function execute();
154
155    /**
156     * @param string $str
157     * @suppress SecurityCheck-DoubleEscaped $this->data can be either
158     */
159    protected function text( $str ) {
160        $this->checkDeprecationStatus( $str );
161        echo htmlspecialchars( $this->data[$str] );
162    }
163
164    /**
165     * @param string $str
166     * @suppress SecurityCheck-XSS phan-taint-check cannot tell if $str is pre-escaped
167     */
168    public function html( $str ) {
169        $this->checkDeprecationStatus( $str );
170        echo $this->data[$str];
171    }
172
173    /**
174     * @param string $msgKey
175     */
176    public function msg( $msgKey ) {
177        echo htmlspecialchars( wfMessage( $msgKey )->text() );
178    }
179
180    /**
181     * @param string $str
182     * @return bool
183     */
184    private function haveData( $str ) {
185        $this->checkDeprecationStatus( $str );
186        return isset( $this->data[$str] );
187    }
188
189    /**
190     * @param string $msgKey
191     * @return bool
192     */
193    protected function haveMsg( $msgKey ) {
194        return !wfMessage( $msgKey )->isDisabled();
195    }
196
197    /**
198     * Get the Skin object related to this object
199     *
200     * @return SkinTemplate
201     */
202    public function getSkin() {
203        return $this->data['skin'];
204    }
205
206    /**
207     * Fetch the output of a QuickTemplate and return it
208     *
209     * @since 1.23
210     * @return string
211     */
212    public function getHTML() {
213        ob_start();
214        $this->execute();
215        $html = ob_get_contents();
216        ob_end_clean();
217        return $html;
218    }
219}
220
221/** @deprecated class alias since 1.44 */
222class_alias( QuickTemplate::class, 'QuickTemplate' );