fd6ed009941679b21b770e32df5f75eaf24b76eb
[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 /**
38 * The WebInstaller object this WebInstallerOutput is used by.
39 *
40 * @var WebInstaller
41 */
42 public $parent;
43
44 /**
45 * Buffered contents that haven't been output yet
46 * @var string
47 */
48 private $contents = '';
49
50 /**
51 * Has the header (or short header) been output?
52 * @var bool
53 */
54 private $headerDone = false;
55
56 /**
57 * @var string
58 */
59 public $redirectTarget;
60
61 /**
62 * Does the current page need to allow being used as a frame?
63 * If not, X-Frame-Options will be output to forbid it.
64 *
65 * @var bool
66 */
67 public $allowFrames = false;
68
69 /**
70 * Whether to use the limited header (used during CC license callbacks)
71 * @var bool
72 */
73 private $useShortHeader = false;
74
75 /**
76 * @param WebInstaller $parent
77 */
78 public function __construct( WebInstaller $parent ) {
79 $this->parent = $parent;
80 }
81
82 /**
83 * @param string $html
84 */
85 public function addHTML( $html ) {
86 $this->contents .= $html;
87 $this->flush();
88 }
89
90 /**
91 * @param string $text
92 */
93 public function addWikiText( $text ) {
94 $this->addHTML( $this->parent->parse( $text ) );
95 }
96
97 /**
98 * @param string $html
99 */
100 public function addHTMLNoFlush( $html ) {
101 $this->contents .= $html;
102 }
103
104 /**
105 * @param string $url
106 *
107 * @throws MWException
108 */
109 public function redirect( $url ) {
110 if ( $this->headerDone ) {
111 throw new MWException( __METHOD__ . ' called after sending headers' );
112 }
113 $this->redirectTarget = $url;
114 }
115
116 public function output() {
117 $this->flush();
118 $this->outputFooter();
119 }
120
121 /**
122 * Get the raw vector CSS, flipping if needed
123 *
124 * @todo Possibly get rid of this function and use ResourceLoader in the manner it was
125 * designed to be used in, rather than just grabbing a list of filenames from it,
126 * and not properly handling such details as media types in module definitions.
127 *
128 * @return string
129 */
130 public function getCSS() {
131 // All CSS files these modules reference will be concatenated in sequence
132 // and loaded as one file.
133 $moduleNames = array(
134 'mediawiki.legacy.shared',
135 'mediawiki.skinning.interface',
136 'skins.vector.styles',
137 'mediawiki.legacy.config',
138 );
139
140 $prepend = '';
141 $css = '';
142
143 $resourceLoader = new ResourceLoader();
144 foreach ( $moduleNames as $moduleName ) {
145 /** @var ResourceLoaderFileModule $module */
146 $module = $resourceLoader->getModule( $moduleName );
147 $cssFileNames = $module->getAllStyleFiles();
148
149 wfSuppressWarnings();
150 foreach ( $cssFileNames as $cssFileName ) {
151 if ( !file_exists( $cssFileName ) ) {
152 $prepend .= ResourceLoader::makeComment( "Unable to find $cssFileName." );
153 continue;
154 }
155
156 if ( !is_readable( $cssFileName ) ) {
157 $prepend .= ResourceLoader::makeComment( "Unable to read $cssFileName. " .
158 "Please check file permissions." );
159 continue;
160 }
161
162 try {
163
164 if ( preg_match( '/\.less$/', $cssFileName ) ) {
165 // Run the LESS compiler for *.less files (bug 55589)
166 $compiler = ResourceLoader::getLessCompiler();
167 $cssFileContents = $compiler->compileFile( $cssFileName );
168 } else {
169 // Regular CSS file
170 $cssFileContents = file_get_contents( $cssFileName );
171 }
172
173 if ( $cssFileContents ) {
174 // Rewrite URLs, though don't bother embedding images. While static image
175 // files may be cached, CSS returned by this function is definitely not.
176 $cssDirName = dirname( $cssFileName );
177 $css .= CSSMin::remap(
178 /* source */ $cssFileContents,
179 /* local */ $cssDirName,
180 /* remote */ '..' . str_replace(
181 array( $GLOBALS['IP'], DIRECTORY_SEPARATOR ),
182 array( '', '/' ),
183 $cssDirName
184 ),
185 /* embedData */ false
186 );
187 } else {
188 $prepend .= ResourceLoader::makeComment( "Unable to read $cssFileName." );
189 }
190 } catch ( Exception $e ) {
191 $prepend .= ResourceLoader::formatException( $e );
192 }
193
194 $css .= "\n";
195 }
196 wfRestoreWarnings();
197 }
198
199 $css = $prepend . $css;
200
201 if ( $this->getDir() == 'rtl' ) {
202 $css = CSSJanus::transform( $css, true );
203 }
204
205 return $css;
206 }
207
208 /**
209 * "<link>" to index.php?css=1 for the "<head>"
210 *
211 * @return string
212 */
213 private function getCssUrl() {
214 return Html::linkedStyle( $_SERVER['PHP_SELF'] . '?css=1' );
215 }
216
217 public function useShortHeader( $use = true ) {
218 $this->useShortHeader = $use;
219 }
220
221 public function allowFrames( $allow = true ) {
222 $this->allowFrames = $allow;
223 }
224
225 public function flush() {
226 if ( !$this->headerDone ) {
227 $this->outputHeader();
228 }
229 if ( !$this->redirectTarget && strlen( $this->contents ) ) {
230 echo $this->contents;
231 flush();
232 $this->contents = '';
233 }
234 }
235
236 /**
237 * @return string
238 */
239 public function getDir() {
240 global $wgLang;
241
242 return is_object( $wgLang ) ? $wgLang->getDir() : 'ltr';
243 }
244
245 /**
246 * @return string
247 */
248 public function getLanguageCode() {
249 global $wgLang;
250
251 return is_object( $wgLang ) ? $wgLang->getCode() : 'en';
252 }
253
254 /**
255 * @return string[]
256 */
257 public function getHeadAttribs() {
258 return array(
259 'dir' => $this->getDir(),
260 'lang' => $this->getLanguageCode(),
261 );
262 }
263
264 /**
265 * Get whether the header has been output
266 *
267 * @return bool
268 */
269 public function headerDone() {
270 return $this->headerDone;
271 }
272
273 public function outputHeader() {
274 $this->headerDone = true;
275 $this->parent->request->response()->header( 'Content-Type: text/html; charset=utf-8' );
276
277 if ( !$this->allowFrames ) {
278 $this->parent->request->response()->header( 'X-Frame-Options: DENY' );
279 }
280
281 if ( $this->redirectTarget ) {
282 $this->parent->request->response()->header( 'Location: ' . $this->redirectTarget );
283
284 return;
285 }
286
287 if ( $this->useShortHeader ) {
288 $this->outputShortHeader();
289
290 return;
291 }
292 ?>
293 <?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
294 <head>
295 <meta name="robots" content="noindex, nofollow" />
296 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
297 <title><?php $this->outputTitle(); ?></title>
298 <?php echo $this->getCssUrl() . "\n"; ?>
299 <?php echo $this->getJQuery() . "\n"; ?>
300 <?php echo Html::linkedScript( '../skins/common/config.js' ) . "\n"; ?>
301 </head>
302
303 <?php echo Html::openElement( 'body', array( 'class' => $this->getDir() ) ) . "\n"; ?>
304 <div id="mw-page-base"></div>
305 <div id="mw-head-base"></div>
306 <div id="content">
307 <div id="bodyContent">
308
309 <h1><?php $this->outputTitle(); ?></h1>
310 <?php
311 }
312
313 public function outputFooter() {
314 if ( $this->useShortHeader ) {
315 echo Html::closeElement( 'body' ) . Html::closeElement( 'html' );
316
317 return;
318 }
319 ?>
320
321 </div></div>
322
323 <div id="mw-panel">
324 <div class="portal" id="p-logo">
325 <a style="background-image: url(../skins/common/images/mediawiki.png);"
326 href="https://www.mediawiki.org/"
327 title="Main Page"></a>
328 </div>
329 <div class="portal"><div class="body">
330 <?php
331 echo $this->parent->parse( wfMessage( 'config-sidebar' )->plain(), true );
332 ?>
333 </div></div>
334 </div>
335
336 <?php
337 echo Html::closeElement( 'body' ) . Html::closeElement( 'html' );
338 }
339
340 public function outputShortHeader() {
341 ?>
342 <?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
343 <head>
344 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
345 <meta name="robots" content="noindex, nofollow" />
346 <title><?php $this->outputTitle(); ?></title>
347 <?php echo $this->getCssUrl() . "\n"; ?>
348 <?php echo $this->getJQuery(); ?>
349 <?php echo Html::linkedScript( '../skins/common/config.js' ); ?>
350 </head>
351
352 <body style="background-image: none">
353 <?php
354 }
355
356 public function outputTitle() {
357 global $wgVersion;
358 echo wfMessage( 'config-title', $wgVersion )->escaped();
359 }
360
361 /**
362 * @return string
363 */
364 public function getJQuery() {
365 return Html::linkedScript( "../resources/lib/jquery/jquery.js" );
366 }
367
368 }