Merge "browser acceptance tests"
[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 stylesheet of the MediaWiki skin.
123 *
124 * @return string
125 */
126 public function getCSS() {
127 // Horrible, horrible hack: the installer is currently hardcoded to use the Vector skin, so load
128 // it here. Include instead of require, as this will work without it, it will just look bad.
129 global $wgResourceModules;
130 global $wgStyleDirectory;
131 include_once "$wgStyleDirectory/Vector/Vector.php";
132
133 $moduleNames = array(
134 // See SkinTemplate::setupSkinUserCss
135 'mediawiki.legacy.shared',
136 // See Vector::setupSkinUserCss
137 'mediawiki.skinning.interface',
138 'skins.vector.styles',
139
140 'mediawiki.legacy.config',
141 );
142
143 $css = '';
144
145 $resourceLoader = new ResourceLoader();
146 $rlContext = new ResourceLoaderContext( $resourceLoader, new FauxRequest( array(
147 'debug' => 'true',
148 'lang' => $this->getLanguageCode(),
149 'only' => 'styles',
150 'skin' => 'vector',
151 ) ) );
152 foreach ( $moduleNames as $moduleName ) {
153 /** @var ResourceLoaderFileModule $module */
154 $module = $resourceLoader->getModule( $moduleName );
155 // One of the modules will be missing if Vector is unavailable
156 if ( !$module ) {
157 continue;
158 }
159
160 // Based on: ResourceLoaderFileModule::getStyles (without the DB query)
161 $styles = ResourceLoader::makeCombinedStyles( $module->readStyleFiles(
162 $module->getStyleFiles( $rlContext ),
163 $module->getFlip( $rlContext )
164 ) );
165
166 $css .= implode( "\n", $styles );
167 }
168
169 return $css;
170 }
171
172 /**
173 * "<link>" to index.php?css=1 for the "<head>"
174 *
175 * @return string
176 */
177 private function getCssUrl() {
178 return Html::linkedStyle( $_SERVER['PHP_SELF'] . '?css=1' );
179 }
180
181 public function useShortHeader( $use = true ) {
182 $this->useShortHeader = $use;
183 }
184
185 public function allowFrames( $allow = true ) {
186 $this->allowFrames = $allow;
187 }
188
189 public function flush() {
190 if ( !$this->headerDone ) {
191 $this->outputHeader();
192 }
193 if ( !$this->redirectTarget && strlen( $this->contents ) ) {
194 echo $this->contents;
195 flush();
196 $this->contents = '';
197 }
198 }
199
200 /**
201 * @return string
202 */
203 public function getDir() {
204 global $wgLang;
205
206 return is_object( $wgLang ) ? $wgLang->getDir() : 'ltr';
207 }
208
209 /**
210 * @return string
211 */
212 public function getLanguageCode() {
213 global $wgLang;
214
215 return is_object( $wgLang ) ? $wgLang->getCode() : 'en';
216 }
217
218 /**
219 * @return string[]
220 */
221 public function getHeadAttribs() {
222 return array(
223 'dir' => $this->getDir(),
224 'lang' => $this->getLanguageCode(),
225 );
226 }
227
228 /**
229 * Get whether the header has been output
230 *
231 * @return bool
232 */
233 public function headerDone() {
234 return $this->headerDone;
235 }
236
237 public function outputHeader() {
238 $this->headerDone = true;
239 $this->parent->request->response()->header( 'Content-Type: text/html; charset=utf-8' );
240
241 if ( !$this->allowFrames ) {
242 $this->parent->request->response()->header( 'X-Frame-Options: DENY' );
243 }
244
245 if ( $this->redirectTarget ) {
246 $this->parent->request->response()->header( 'Location: ' . $this->redirectTarget );
247
248 return;
249 }
250
251 if ( $this->useShortHeader ) {
252 $this->outputShortHeader();
253
254 return;
255 }
256 ?>
257 <?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
258 <head>
259 <meta name="robots" content="noindex, nofollow" />
260 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
261 <title><?php $this->outputTitle(); ?></title>
262 <?php echo $this->getCssUrl() . "\n"; ?>
263 <?php echo $this->getJQuery() . "\n"; ?>
264 <?php echo Html::linkedScript( '../skins/common/config.js' ) . "\n"; ?>
265 </head>
266
267 <?php echo Html::openElement( 'body', array( 'class' => $this->getDir() ) ) . "\n"; ?>
268 <div id="mw-page-base"></div>
269 <div id="mw-head-base"></div>
270 <div id="content">
271 <div id="bodyContent">
272
273 <h1><?php $this->outputTitle(); ?></h1>
274 <?php
275 }
276
277 public function outputFooter() {
278 if ( $this->useShortHeader ) {
279 echo Html::closeElement( 'body' ) . Html::closeElement( 'html' );
280
281 return;
282 }
283 ?>
284
285 </div></div>
286
287 <div id="mw-panel">
288 <div class="portal" id="p-logo">
289 <a style="background-image: url(../skins/common/images/mediawiki.png);"
290 href="https://www.mediawiki.org/"
291 title="Main Page"></a>
292 </div>
293 <div class="portal"><div class="body">
294 <?php
295 echo $this->parent->parse( wfMessage( 'config-sidebar' )->plain(), true );
296 ?>
297 </div></div>
298 </div>
299
300 <?php
301 echo Html::closeElement( 'body' ) . Html::closeElement( 'html' );
302 }
303
304 public function outputShortHeader() {
305 ?>
306 <?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
307 <head>
308 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
309 <meta name="robots" content="noindex, nofollow" />
310 <title><?php $this->outputTitle(); ?></title>
311 <?php echo $this->getCssUrl() . "\n"; ?>
312 <?php echo $this->getJQuery(); ?>
313 <?php echo Html::linkedScript( '../skins/common/config.js' ); ?>
314 </head>
315
316 <body style="background-image: none">
317 <?php
318 }
319
320 public function outputTitle() {
321 global $wgVersion;
322 echo wfMessage( 'config-title', $wgVersion )->escaped();
323 }
324
325 /**
326 * @return string
327 */
328 public function getJQuery() {
329 return Html::linkedScript( "../resources/lib/jquery/jquery.js" );
330 }
331
332 }