Add two new debug log groups
[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.common.interface',
121 'skins.vector.styles',
122 'mediawiki.legacy.config',
123 );
124
125 $prepend = '';
126 $css = '';
127
128 $resourceLoader = new ResourceLoader();
129 foreach ( $moduleNames as $moduleName ) {
130 /** @var ResourceLoaderFileModule $module */
131 $module = $resourceLoader->getModule( $moduleName );
132 $cssFileNames = $module->getAllStyleFiles();
133
134 wfSuppressWarnings();
135 foreach ( $cssFileNames as $cssFileName ) {
136 if ( !file_exists( $cssFileName ) ) {
137 $prepend .= ResourceLoader::makeComment( "Unable to find $cssFileName." );
138 continue;
139 }
140
141 if ( !is_readable( $cssFileName ) ) {
142 $prepend .= ResourceLoader::makeComment( "Unable to read $cssFileName. " .
143 "Please check file permissions." );
144 continue;
145 }
146
147 try {
148
149 if ( preg_match( '/\.less$/', $cssFileName ) ) {
150 // Run the LESS compiler for *.less files (bug 55589)
151 $compiler = ResourceLoader::getLessCompiler();
152 $cssFileContents = $compiler->compileFile( $cssFileName );
153 } else {
154 // Regular CSS file
155 $cssFileContents = file_get_contents( $cssFileName );
156 }
157
158 if ( $cssFileContents ) {
159 // Rewrite URLs, though don't bother embedding images. While static image
160 // files may be cached, CSS returned by this function is definitely not.
161 $cssDirName = dirname( $cssFileName );
162 $css .= CSSMin::remap(
163 /* source */ $cssFileContents,
164 /* local */ $cssDirName,
165 /* remote */ '..' . str_replace(
166 array( $GLOBALS['IP'], DIRECTORY_SEPARATOR ),
167 array( '', '/' ),
168 $cssDirName
169 ),
170 /* embedData */ false
171 );
172 } else {
173 $prepend .= ResourceLoader::makeComment( "Unable to read $cssFileName." );
174 }
175 } catch ( Exception $e ) {
176 $prepend .= ResourceLoader::formatException( $e );
177 }
178
179 $css .= "\n";
180 }
181 wfRestoreWarnings();
182 }
183
184 $css = $prepend . $css;
185
186 if ( $dir == 'rtl' ) {
187 $css = CSSJanus::transform( $css, true );
188 }
189
190 return $css;
191 }
192
193 /**
194 * "<link>" to index.php?css=foobar for the "<head>"
195 * @return String
196 */
197 private function getCssUrl() {
198 return Html::linkedStyle( $_SERVER['PHP_SELF'] . '?css=' . $this->getDir() );
199 }
200
201 public function useShortHeader( $use = true ) {
202 $this->useShortHeader = $use;
203 }
204
205 public function allowFrames( $allow = true ) {
206 $this->allowFrames = $allow;
207 }
208
209 public function flush() {
210 if ( !$this->headerDone ) {
211 $this->outputHeader();
212 }
213 if ( !$this->redirectTarget && strlen( $this->contents ) ) {
214 echo $this->contents;
215 flush();
216 $this->contents = '';
217 }
218 }
219
220 /**
221 * @return string
222 */
223 public function getDir() {
224 global $wgLang;
225
226 return is_object( $wgLang ) ? $wgLang->getDir() : 'ltr';
227 }
228
229 /**
230 * @return string
231 */
232 public function getLanguageCode() {
233 global $wgLang;
234
235 return is_object( $wgLang ) ? $wgLang->getCode() : 'en';
236 }
237
238 /**
239 * @return array
240 */
241 public function getHeadAttribs() {
242 return array(
243 'dir' => $this->getDir(),
244 'lang' => $this->getLanguageCode(),
245 );
246 }
247
248 /**
249 * Get whether the header has been output
250 * @return bool
251 */
252 public function headerDone() {
253 return $this->headerDone;
254 }
255
256 public function outputHeader() {
257 $this->headerDone = true;
258 $this->parent->request->response()->header( 'Content-Type: text/html; charset=utf-8' );
259
260 if ( !$this->allowFrames ) {
261 $this->parent->request->response()->header( 'X-Frame-Options: DENY' );
262 }
263
264 if ( $this->redirectTarget ) {
265 $this->parent->request->response()->header( 'Location: ' . $this->redirectTarget );
266
267 return;
268 }
269
270 if ( $this->useShortHeader ) {
271 $this->outputShortHeader();
272
273 return;
274 }
275 ?>
276 <?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
277 <head>
278 <meta name="robots" content="noindex, nofollow" />
279 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
280 <title><?php $this->outputTitle(); ?></title>
281 <?php echo $this->getCssUrl() . "\n"; ?>
282 <?php echo $this->getJQuery() . "\n"; ?>
283 <?php echo Html::linkedScript( '../skins/common/config.js' ) . "\n"; ?>
284 </head>
285
286 <?php echo Html::openElement( 'body', array( 'class' => $this->getDir() ) ) . "\n"; ?>
287 <div id="mw-page-base"></div>
288 <div id="mw-head-base"></div>
289 <div id="content">
290 <div id="bodyContent">
291
292 <h1><?php $this->outputTitle(); ?></h1>
293 <?php
294 }
295
296 public function outputFooter() {
297 if ( $this->useShortHeader ) {
298 echo Html::closeElement( 'body' ) . Html::closeElement( 'html' );
299
300 return;
301 }
302 ?>
303
304 </div></div>
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 }