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