Merge "Warn if stateful ParserOutput transforms are used"
[lhc/web/wiklou.git] / includes / skins / QuickTemplate.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 */
20 use MediaWiki\MediaWikiServices;
21
22 /**
23 * Generic wrapper for template functions, with interface
24 * compatible with what we use of PHPTAL 0.7.
25 * @ingroup Skins
26 */
27 abstract class QuickTemplate {
28
29 /**
30 * @var array
31 */
32 public $data;
33
34 /**
35 * @var MediaWikiI18N
36 */
37 public $translator;
38
39 /** @var Config $config */
40 protected $config;
41
42 /**
43 * @param Config $config
44 */
45 function __construct( Config $config = null ) {
46 $this->data = [];
47 $this->translator = new MediaWikiI18N();
48 if ( $config === null ) {
49 wfDebug( __METHOD__ . ' was called with no Config instance passed to it' );
50 $config = MediaWikiServices::getInstance()->getMainConfig();
51 }
52 $this->config = $config;
53 }
54
55 /**
56 * Sets the value $value to $name
57 * @param string $name
58 * @param mixed $value
59 */
60 public function set( $name, $value ) {
61 $this->data[$name] = $value;
62 }
63
64 /**
65 * extends the value of data with name $name with the value $value
66 * @since 1.25
67 * @param string $name
68 * @param mixed $value
69 */
70 public function extend( $name, $value ) {
71 if ( $this->haveData( $name ) ) {
72 $this->data[$name] = $this->data[$name] . $value;
73 } else {
74 $this->data[$name] = $value;
75 }
76 }
77
78 /**
79 * Gets the template data requested
80 * @since 1.22
81 * @param string $name Key for the data
82 * @param mixed $default Optional default (or null)
83 * @return mixed The value of the data requested or the deafult
84 */
85 public function get( $name, $default = null ) {
86 if ( isset( $this->data[$name] ) ) {
87 return $this->data[$name];
88 } else {
89 return $default;
90 }
91 }
92
93 /**
94 * @deprecated since 1.31 This function is a now-redundant optimisation intended
95 * for very old versions of PHP. The use of references here makes the code
96 * more fragile and is incompatible with plans like T140664. Use set() instead.
97 * @param string $name
98 * @param mixed &$value
99 */
100 public function setRef( $name, &$value ) {
101 wfDeprecated( __METHOD__, '1.31' );
102 $this->data[$name] =& $value;
103 }
104
105 /**
106 * @param MediaWikiI18N &$t
107 */
108 public function setTranslator( &$t ) {
109 $this->translator = &$t;
110 }
111
112 /**
113 * Main function, used by classes that subclass QuickTemplate
114 * to show the actual HTML output
115 */
116 abstract public function execute();
117
118 /**
119 * @private
120 * @param string $str
121 */
122 function text( $str ) {
123 echo htmlspecialchars( $this->data[$str] );
124 }
125
126 /**
127 * @private
128 * @param string $str
129 */
130 function html( $str ) {
131 echo $this->data[$str];
132 }
133
134 /**
135 * @private
136 * @param string $str
137 */
138 function msg( $str ) {
139 echo htmlspecialchars( $this->translator->translate( $str ) );
140 }
141
142 /**
143 * @private
144 * @param string $str
145 */
146 function msgHtml( $str ) {
147 echo $this->translator->translate( $str );
148 }
149
150 /**
151 * An ugly, ugly hack.
152 * @private
153 * @param string $str
154 */
155 function msgWiki( $str ) {
156 global $wgOut;
157
158 $text = $this->translator->translate( $str );
159 echo $wgOut->parse( $text );
160 }
161
162 /**
163 * @private
164 * @param string $str
165 * @return bool
166 */
167 function haveData( $str ) {
168 return isset( $this->data[$str] );
169 }
170
171 /**
172 * @private
173 *
174 * @param string $str
175 * @return bool
176 */
177 function haveMsg( $str ) {
178 $msg = $this->translator->translate( $str );
179 return ( $msg != '-' ) && ( $msg != '' ); # ????
180 }
181
182 /**
183 * Get the Skin object related to this object
184 *
185 * @return Skin
186 */
187 public function getSkin() {
188 return $this->data['skin'];
189 }
190
191 /**
192 * Fetch the output of a QuickTemplate and return it
193 *
194 * @since 1.23
195 * @return string
196 */
197 public function getHTML() {
198 ob_start();
199 $this->execute();
200 $html = ob_get_contents();
201 ob_end_clean();
202 return $html;
203 }
204 }