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