Merge "Rename autonym for 'no' from 'norsk bokmål' to 'norsk'"
[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 * @param string $name
95 * @param mixed $value
96 */
97 public function setRef( $name, &$value ) {
98 $this->data[$name] =& $value;
99 }
100
101 /**
102 * @param MediaWikiI18N $t
103 */
104 public function setTranslator( &$t ) {
105 $this->translator = &$t;
106 }
107
108 /**
109 * Main function, used by classes that subclass QuickTemplate
110 * to show the actual HTML output
111 */
112 abstract public function execute();
113
114 /**
115 * @private
116 * @param string $str
117 */
118 function text( $str ) {
119 echo htmlspecialchars( $this->data[$str] );
120 }
121
122 /**
123 * @private
124 * @param string $str
125 */
126 function html( $str ) {
127 echo $this->data[$str];
128 }
129
130 /**
131 * @private
132 * @param string $str
133 */
134 function msg( $str ) {
135 echo htmlspecialchars( $this->translator->translate( $str ) );
136 }
137
138 /**
139 * @private
140 * @param string $str
141 */
142 function msgHtml( $str ) {
143 echo $this->translator->translate( $str );
144 }
145
146 /**
147 * An ugly, ugly hack.
148 * @private
149 * @param string $str
150 */
151 function msgWiki( $str ) {
152 global $wgOut;
153
154 $text = $this->translator->translate( $str );
155 echo $wgOut->parse( $text );
156 }
157
158 /**
159 * @private
160 * @param string $str
161 * @return bool
162 */
163 function haveData( $str ) {
164 return isset( $this->data[$str] );
165 }
166
167 /**
168 * @private
169 *
170 * @param string $str
171 * @return bool
172 */
173 function haveMsg( $str ) {
174 $msg = $this->translator->translate( $str );
175 return ( $msg != '-' ) && ( $msg != '' ); # ????
176 }
177
178 /**
179 * Get the Skin object related to this object
180 *
181 * @return Skin
182 */
183 public function getSkin() {
184 return $this->data['skin'];
185 }
186
187 /**
188 * Fetch the output of a QuickTemplate and return it
189 *
190 * @since 1.23
191 * @return string
192 */
193 public function getHTML() {
194 ob_start();
195 $this->execute();
196 $html = ob_get_contents();
197 ob_end_clean();
198 return $html;
199 }
200 }