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