User::isSafeToLoad() should return false if MW_NO_SESSION
[mediawiki.git] / includes / skins / QuickTemplate.php
blob905e537e580f604a1bb8c50d5ba3cc6a0c3326b9
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.
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.
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
18 * @file
21 /**
22 * Generic wrapper for template functions, with interface
23 * compatible with what we use of PHPTAL 0.7.
24 * @ingroup Skins
26 abstract class QuickTemplate {
28 /** @var Config $config */
29 protected $config;
31 /**
32 * @param Config $config
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' );
41 $this->config = $config;
44 /**
45 * Sets the value $value to $name
46 * @param string $name
47 * @param mixed $value
49 public function set( $name, $value ) {
50 $this->data[$name] = $value;
53 /**
54 * extends the value of data with name $name with the value $value
55 * @since 1.25
56 * @param string $name
57 * @param mixed $value
59 public function extend( $name, $value ) {
60 if ( $this->haveData( $name ) ) {
61 $this->data[$name] = $this->data[$name] . $value;
62 } else {
63 $this->data[$name] = $value;
67 /**
68 * Gets the template data requested
69 * @since 1.22
70 * @param string $name Key for the data
71 * @param mixed $default Optional default (or null)
72 * @return mixed The value of the data requested or the deafult
74 public function get( $name, $default = null ) {
75 if ( isset( $this->data[$name] ) ) {
76 return $this->data[$name];
77 } else {
78 return $default;
82 /**
83 * @param string $name
84 * @param mixed $value
86 public function setRef( $name, &$value ) {
87 $this->data[$name] =& $value;
90 /**
91 * @param MediaWikiI18N $t
93 public function setTranslator( &$t ) {
94 $this->translator = &$t;
97 /**
98 * Main function, used by classes that subclass QuickTemplate
99 * to show the actual HTML output
101 abstract public function execute();
104 * @private
105 * @param string $str
106 * @return string
108 function text( $str ) {
109 echo htmlspecialchars( $this->data[$str] );
113 * @private
114 * @param string $str
115 * @return string
117 function html( $str ) {
118 echo $this->data[$str];
122 * @private
123 * @param string $str
124 * @return string
126 function msg( $str ) {
127 echo htmlspecialchars( $this->translator->translate( $str ) );
131 * @private
132 * @param string $str
133 * @return string
135 function msgHtml( $str ) {
136 echo $this->translator->translate( $str );
140 * An ugly, ugly hack.
141 * @private
142 * @param string $str
143 * @return string
145 function msgWiki( $str ) {
146 global $wgOut;
148 $text = $this->translator->translate( $str );
149 echo $wgOut->parse( $text );
153 * @private
154 * @param string $str
155 * @return bool
157 function haveData( $str ) {
158 return isset( $this->data[$str] );
162 * @private
164 * @param string $str
165 * @return bool
167 function haveMsg( $str ) {
168 $msg = $this->translator->translate( $str );
169 return ( $msg != '-' ) && ( $msg != '' ); # ????
173 * Get the Skin object related to this object
175 * @return Skin
177 public function getSkin() {
178 return $this->data['skin'];
182 * Fetch the output of a QuickTemplate and return it
184 * @since 1.23
185 * @return string
187 public function getHTML() {
188 ob_start();
189 $this->execute();
190 $html = ob_get_contents();
191 ob_end_clean();
192 return $html;