merged master
[lhc/web/wiklou.git] / includes / installer / WebInstallerOutput.php
1 <?php
2 /**
3 * Output handler for the web installer.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Deployment
22 */
23
24 /**
25 * Output class modelled on OutputPage.
26 *
27 * I've opted to use a distinct class rather than derive from OutputPage here in
28 * the interests of separation of concerns: if we used a subclass, there would be
29 * quite a lot of things you could do in OutputPage that would break the installer,
30 * that wouldn't be immediately obvious.
31 *
32 * @ingroup Deployment
33 * @since 1.17
34 */
35 class WebInstallerOutput {
36 /**
37 * The WebInstaller object this WebInstallerOutput is used by.
38 *
39 * @var WebInstaller
40 */
41 public $parent;
42
43 /**
44 * Buffered contents that haven't been output yet
45 * @var String
46 */
47 private $contents = '';
48
49 /**
50 * Has the header (or short header) been output?
51 * @var bool
52 */
53 private $headerDone = false;
54
55 public $redirectTarget;
56
57 /**
58 * Does the current page need to allow being used as a frame?
59 * If not, X-Frame-Options will be output to forbid it.
60 *
61 * @var bool
62 */
63 public $allowFrames = false;
64
65 /**
66 * Whether to use the limited header (used during CC license callbacks)
67 * @var bool
68 */
69 private $useShortHeader = false;
70
71 /**
72 * Constructor.
73 *
74 * @param $parent WebInstaller
75 */
76 public function __construct( WebInstaller $parent ) {
77 $this->parent = $parent;
78 }
79
80 public function addHTML( $html ) {
81 $this->contents .= $html;
82 $this->flush();
83 }
84
85 public function addWikiText( $text ) {
86 $this->addHTML( $this->parent->parse( $text ) );
87 }
88
89 public function addHTMLNoFlush( $html ) {
90 $this->contents .= $html;
91 }
92
93 public function redirect( $url ) {
94 if ( $this->headerDone ) {
95 throw new MWException( __METHOD__ . ' called after sending headers' );
96 }
97 $this->redirectTarget = $url;
98 }
99
100 public function output() {
101 $this->flush();
102 $this->outputFooter();
103 }
104
105 /**
106 * Get the raw vector CSS, flipping if needed
107 * @param $dir String 'ltr' or 'rtl'
108 * @return String
109 */
110 public function getCSS( $dir ) {
111 $skinDir = dirname( dirname( dirname( __FILE__ ) ) ) . '/skins';
112
113 // All these files will be concatenated in sequence and loaded
114 // as one file.
115 // The string 'images/' in the files' contents will be replaced
116 // by '../skins/$skinName/images/', where $skinName is what appears
117 // before the last '/' in each of the strings.
118 $cssFileNames = array(
119
120 // Basically the "skins.vector" ResourceLoader module styles
121 'common/commonElements.css',
122 'common/commonContent.css',
123 'common/commonInterface.css',
124 'vector/screen.css',
125
126 // mw-config specific
127 'common/config.css',
128 );
129
130 $css = '';
131
132 wfSuppressWarnings();
133 foreach ( $cssFileNames as $cssFileName ) {
134 $fullCssFileName = "$skinDir/$cssFileName";
135 $cssFileContents = file_get_contents( $fullCssFileName );
136 if ( $cssFileContents ) {
137 preg_match( "/^(\w+)\//", $cssFileName, $match );
138 $skinName = $match[1];
139 $css .= str_replace( 'images/', "../skins/$skinName/images/", $cssFileContents );
140 } else {
141 $css .= "/** Your webserver cannot read $fullCssFileName. Please check file permissions. */";
142 }
143
144 $css .= "\n";
145 }
146 wfRestoreWarnings();
147
148 if( $dir == 'rtl' ) {
149 $css = CSSJanus::transform( $css, true );
150 }
151
152 return $css;
153 }
154
155 /**
156 * "<link>" to index.php?css=foobar for the "<head>"
157 * @return String
158 */
159 private function getCssUrl( ) {
160 return Html::linkedStyle( $_SERVER['PHP_SELF'] . '?css=' . $this->getDir() );
161 }
162
163 public function useShortHeader( $use = true ) {
164 $this->useShortHeader = $use;
165 }
166
167 public function allowFrames( $allow = true ) {
168 $this->allowFrames = $allow;
169 }
170
171 public function flush() {
172 if ( !$this->headerDone ) {
173 $this->outputHeader();
174 }
175 if ( !$this->redirectTarget && strlen( $this->contents ) ) {
176 echo $this->contents;
177 flush();
178 $this->contents = '';
179 }
180 }
181
182 /**
183 * @return string
184 */
185 public function getDir() {
186 global $wgLang;
187 return is_object( $wgLang ) ? $wgLang->getDir() : 'ltr';
188 }
189
190 /**
191 * @return string
192 */
193 public function getLanguageCode() {
194 global $wgLang;
195 return is_object( $wgLang ) ? $wgLang->getCode() : 'en';
196 }
197
198 /**
199 * @return array
200 */
201 public function getHeadAttribs() {
202 return array(
203 'dir' => $this->getDir(),
204 'lang' => $this->getLanguageCode(),
205 );
206 }
207
208 /**
209 * Get whether the header has been output
210 * @return bool
211 */
212 public function headerDone() {
213 return $this->headerDone;
214 }
215
216 public function outputHeader() {
217 $this->headerDone = true;
218 $dbTypes = $this->parent->getDBTypes();
219
220 $this->parent->request->response()->header( 'Content-Type: text/html; charset=utf-8' );
221 if (!$this->allowFrames) {
222 $this->parent->request->response()->header( 'X-Frame-Options: DENY' );
223 }
224 if ( $this->redirectTarget ) {
225 $this->parent->request->response()->header( 'Location: '.$this->redirectTarget );
226 return;
227 }
228
229 if ( $this->useShortHeader ) {
230 $this->outputShortHeader();
231 return;
232 }
233
234 ?>
235 <?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
236 <head>
237 <meta name="robots" content="noindex, nofollow" />
238 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
239 <title><?php $this->outputTitle(); ?></title>
240 <?php echo $this->getCssUrl() . "\n"; ?>
241 <?php echo Html::inlineScript( "var dbTypes = " . Xml::encodeJsVar( $dbTypes ) ) . "\n"; ?>
242 <?php echo $this->getJQuery() . "\n"; ?>
243 <?php echo Html::linkedScript( '../skins/common/config.js' ) . "\n"; ?>
244 </head>
245
246 <?php echo Html::openElement( 'body', array( 'class' => $this->getDir() ) ) . "\n"; ?>
247 <div id="mw-page-base"></div>
248 <div id="mw-head-base"></div>
249 <div id="content">
250 <div id="bodyContent">
251
252 <h1><?php $this->outputTitle(); ?></h1>
253 <?php
254 }
255
256 public function outputFooter() {
257 if ( $this->useShortHeader ) {
258 ?>
259 </body></html>
260 <?php
261 return;
262 }
263 ?>
264
265 </div></div>
266
267
268 <div id="mw-panel">
269 <div class="portal" id="p-logo">
270 <a style="background-image: url(../skins/common/images/mediawiki.png);"
271 href="http://www.mediawiki.org/"
272 title="Main Page"></a>
273 </div>
274 <div class="portal"><div class="body">
275 <?php
276 echo $this->parent->parse( wfMsgNoTrans( 'config-sidebar' ), true );
277 ?>
278 </div></div>
279 </div>
280
281 </body>
282 </html>
283 <?php
284 }
285
286 public function outputShortHeader() {
287 ?>
288 <?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
289 <head>
290 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
291 <meta name="robots" content="noindex, nofollow" />
292 <title><?php $this->outputTitle(); ?></title>
293 <?php echo $this->getCssUrl() . "\n"; ?>
294 <?php echo $this->getJQuery(); ?>
295 <?php echo Html::linkedScript( '../skins/common/config.js' ); ?>
296 </head>
297
298 <body style="background-image: none">
299 <?php
300 }
301
302 public function outputTitle() {
303 global $wgVersion;
304 echo htmlspecialchars( wfMsg( 'config-title', $wgVersion ) );
305 }
306
307 public function getJQuery() {
308 return Html::linkedScript( "../resources/jquery/jquery.js" );
309 }
310 }