6178e5bfa2dbce2f56ecf5775c2dce18b8cfa32a
[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 *
108 * @todo Possibly get rid of this function and use ResourceLoader in the manner it was
109 * designed to be used in, rather than just grabbing a list of filenames from it,
110 * and not properly handling such details as media types in module definitions.
111 *
112 * @param string $dir 'ltr' or 'rtl'
113 * @return String
114 */
115 public function getCSS( $dir ) {
116 // All CSS files these modules reference will be concatenated in sequence
117 // and loaded as one file.
118 $moduleNames = array(
119 'mediawiki.legacy.shared',
120 'skins.vector',
121 'mediawiki.legacy.config',
122 );
123
124 $prepend = '';
125 $css = '';
126
127 $cssFileNames = array();
128 $resourceLoader = new ResourceLoader();
129 foreach ( $moduleNames as $moduleName ) {
130 $module = $resourceLoader->getModule( $moduleName );
131 $cssFileNames = $module->getAllStyleFiles();
132
133 wfSuppressWarnings();
134 foreach ( $cssFileNames as $cssFileName ) {
135 if ( !file_exists( $cssFileName ) ) {
136 $prepend .= ResourceLoader::makeComment( "Unable to find $cssFileName." );
137 continue;
138 }
139
140 if ( !is_readable( $cssFileName ) ) {
141 $prepend .= ResourceLoader::makeComment( "Unable to read $cssFileName. Please check file permissions." );
142 continue;
143 }
144
145 try {
146
147 if ( preg_match( '/\.less$/', $cssFileName ) ) {
148 // Run the LESS compiler for *.less files (bug 55589)
149 $compiler = ResourceLoader::getLessCompiler();
150 $cssFileContents = $compiler->compileFile( $cssFileName );
151 } else {
152 // Regular CSS file
153 $cssFileContents = file_get_contents( $cssFileName );
154 }
155
156 if ( $cssFileContents ) {
157 // Rewrite URLs, though don't bother embedding images. While static image
158 // files may be cached, CSS returned by this function is definitely not.
159 $cssDirName = dirname( $cssFileName );
160 $css .= CSSMin::remap(
161 /* source */ $cssFileContents,
162 /* local */ $cssDirName,
163 /* remote */ '..' . str_replace(
164 array( $GLOBALS['IP'], DIRECTORY_SEPARATOR ),
165 array( '', '/' ),
166 $cssDirName
167 ),
168 /* embedData */ false
169 );
170 } else {
171 $prepend .= ResourceLoader::makeComment( "Unable to read $cssFileName." );
172 }
173
174 } catch ( Exception $e ) {
175 $prepend .= ResourceLoader::formatException( $e );
176 }
177
178 $css .= "\n";
179 }
180 wfRestoreWarnings();
181 }
182
183 $css = $prepend . $css;
184
185 if ( $dir == 'rtl' ) {
186 $css = CSSJanus::transform( $css, true );
187 }
188
189 return $css;
190 }
191
192 /**
193 * "<link>" to index.php?css=foobar for the "<head>"
194 * @return String
195 */
196 private function getCssUrl() {
197 return Html::linkedStyle( $_SERVER['PHP_SELF'] . '?css=' . $this->getDir() );
198 }
199
200 public function useShortHeader( $use = true ) {
201 $this->useShortHeader = $use;
202 }
203
204 public function allowFrames( $allow = true ) {
205 $this->allowFrames = $allow;
206 }
207
208 public function flush() {
209 if ( !$this->headerDone ) {
210 $this->outputHeader();
211 }
212 if ( !$this->redirectTarget && strlen( $this->contents ) ) {
213 echo $this->contents;
214 flush();
215 $this->contents = '';
216 }
217 }
218
219 /**
220 * @return string
221 */
222 public function getDir() {
223 global $wgLang;
224
225 return is_object( $wgLang ) ? $wgLang->getDir() : 'ltr';
226 }
227
228 /**
229 * @return string
230 */
231 public function getLanguageCode() {
232 global $wgLang;
233
234 return is_object( $wgLang ) ? $wgLang->getCode() : 'en';
235 }
236
237 /**
238 * @return array
239 */
240 public function getHeadAttribs() {
241 return array(
242 'dir' => $this->getDir(),
243 'lang' => $this->getLanguageCode(),
244 );
245 }
246
247 /**
248 * Get whether the header has been output
249 * @return bool
250 */
251 public function headerDone() {
252 return $this->headerDone;
253 }
254
255 public function outputHeader() {
256 $this->headerDone = true;
257 $this->parent->request->response()->header( 'Content-Type: text/html; charset=utf-8' );
258
259 if ( !$this->allowFrames ) {
260 $this->parent->request->response()->header( 'X-Frame-Options: DENY' );
261 }
262
263 if ( $this->redirectTarget ) {
264 $this->parent->request->response()->header( 'Location: ' . $this->redirectTarget );
265
266 return;
267 }
268
269 if ( $this->useShortHeader ) {
270 $this->outputShortHeader();
271
272 return;
273 }
274 ?>
275 <?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
276 <head>
277 <meta name="robots" content="noindex, nofollow" />
278 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
279 <title><?php $this->outputTitle(); ?></title>
280 <?php echo $this->getCssUrl() . "\n"; ?>
281 <?php echo $this->getJQuery() . "\n"; ?>
282 <?php echo Html::linkedScript( '../skins/common/config.js' ) . "\n"; ?>
283 </head>
284
285 <?php echo Html::openElement( 'body', array( 'class' => $this->getDir() ) ) . "\n"; ?>
286 <div id="mw-page-base"></div>
287 <div id="mw-head-base"></div>
288 <div id="content">
289 <div id="bodyContent">
290
291 <h1><?php $this->outputTitle(); ?></h1>
292 <?php
293 }
294
295 public function outputFooter() {
296 if ( $this->useShortHeader ) {
297 echo Html::closeElement( 'body' ) . Html::closeElement( 'html' );
298
299 return;
300 }
301 ?>
302
303 </div></div>
304
305
306 <div id="mw-panel">
307 <div class="portal" id="p-logo">
308 <a style="background-image: url(../skins/common/images/mediawiki.png);"
309 href="http://www.mediawiki.org/"
310 title="Main Page"></a>
311 </div>
312 <div class="portal"><div class="body">
313 <?php
314 echo $this->parent->parse( wfMessage( 'config-sidebar' )->plain(), true );
315 ?>
316 </div></div>
317 </div>
318
319 <?php
320 echo Html::closeElement( 'body' ) . Html::closeElement( 'html' );
321 }
322
323 public function outputShortHeader() {
324 ?>
325 <?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
326 <head>
327 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
328 <meta name="robots" content="noindex, nofollow" />
329 <title><?php $this->outputTitle(); ?></title>
330 <?php echo $this->getCssUrl() . "\n"; ?>
331 <?php echo $this->getJQuery(); ?>
332 <?php echo Html::linkedScript( '../skins/common/config.js' ); ?>
333 </head>
334
335 <body style="background-image: none">
336 <?php
337 }
338
339 public function outputTitle() {
340 global $wgVersion;
341 echo wfMessage( 'config-title', $wgVersion )->escaped();
342 }
343
344 public function getJQuery() {
345 return Html::linkedScript( "../resources/jquery/jquery.js" );
346 }
347 }