Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[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 use MediaWiki\MediaWikiServices;
25
26 /**
27 * Output class modelled on OutputPage.
28 *
29 * I've opted to use a distinct class rather than derive from OutputPage here in
30 * the interests of separation of concerns: if we used a subclass, there would be
31 * quite a lot of things you could do in OutputPage that would break the installer,
32 * that wouldn't be immediately obvious.
33 *
34 * @ingroup Deployment
35 * @since 1.17
36 * @private
37 */
38 class WebInstallerOutput {
39
40 /**
41 * The WebInstaller object this WebInstallerOutput is used by.
42 *
43 * @var WebInstaller
44 */
45 public $parent;
46
47 /**
48 * Buffered contents that haven't been output yet
49 * @var string
50 */
51 private $contents = '';
52
53 /**
54 * Has the header (or short header) been output?
55 * @var bool
56 */
57 private $headerDone = false;
58
59 /**
60 * @var string
61 */
62 public $redirectTarget;
63
64 /**
65 * Does the current page need to allow being used as a frame?
66 * If not, X-Frame-Options will be output to forbid it.
67 *
68 * @var bool
69 */
70 public $allowFrames = false;
71
72 /**
73 * Whether to use the limited header (used during CC license callbacks)
74 * @var bool
75 */
76 private $useShortHeader = false;
77
78 /**
79 * @param WebInstaller $parent
80 */
81 public function __construct( WebInstaller $parent ) {
82 $this->parent = $parent;
83 }
84
85 /**
86 * @param string $html
87 */
88 public function addHTML( $html ) {
89 $this->contents .= $html;
90 $this->flush();
91 }
92
93 /**
94 * @param string $text
95 * @since 1.32
96 */
97 public function addWikiTextAsInterface( $text ) {
98 $this->addHTML( $this->parent->parse( $text ) );
99 }
100
101 /**
102 * @param string $html
103 */
104 public function addHTMLNoFlush( $html ) {
105 $this->contents .= $html;
106 }
107
108 /**
109 * @param string $url
110 *
111 * @throws MWException
112 */
113 public function redirect( $url ) {
114 if ( $this->headerDone ) {
115 throw new MWException( __METHOD__ . ' called after sending headers' );
116 }
117 $this->redirectTarget = $url;
118 }
119
120 public function output() {
121 $this->flush();
122
123 if ( !$this->redirectTarget ) {
124 $this->outputFooter();
125 }
126 }
127
128 /**
129 * Get the stylesheet of the MediaWiki skin.
130 *
131 * @return string
132 */
133 public function getCSS() {
134 global $wgStyleDirectory;
135
136 $moduleNames = [
137 // Based on Skin::getDefaultModules
138 'mediawiki.legacy.shared',
139 // Based on Vector::setupSkinUserCss
140 'mediawiki.skinning.interface',
141 ];
142
143 $resourceLoader = MediaWikiServices::getInstance()->getResourceLoader();
144
145 if ( file_exists( "$wgStyleDirectory/Vector/skin.json" ) ) {
146 // Force loading Vector skin if available as a fallback skin
147 // for whatever ResourceLoader wants to have as the default.
148 $registry = new ExtensionRegistry();
149 $data = $registry->readFromQueue( [
150 "$wgStyleDirectory/Vector/skin.json" => 1,
151 ] );
152 if ( isset( $data['globals']['wgResourceModules'] ) ) {
153 $resourceLoader->register( $data['globals']['wgResourceModules'] );
154 }
155
156 $moduleNames[] = 'skins.vector.styles';
157 }
158
159 $moduleNames[] = 'mediawiki.legacy.config';
160
161 $rlContext = new ResourceLoaderContext( $resourceLoader, new FauxRequest( [
162 'debug' => 'true',
163 'lang' => $this->getLanguage()->getCode(),
164 'only' => 'styles',
165 ] ) );
166
167 $styles = [];
168 foreach ( $moduleNames as $moduleName ) {
169 /** @var ResourceLoaderFileModule $module */
170 $module = $resourceLoader->getModule( $moduleName );
171 if ( !$module ) {
172 // T98043: Don't fatal, but it won't look as pretty.
173 continue;
174 }
175
176 // Based on: ResourceLoaderFileModule::getStyles (without the DB query)
177 $styles = array_merge( $styles, ResourceLoader::makeCombinedStyles(
178 $module->readStyleFiles(
179 $module->getStyleFiles( $rlContext ),
180 $module->getFlip( $rlContext ),
181 $rlContext
182 ) ) );
183 }
184
185 return implode( "\n", $styles );
186 }
187
188 /**
189 * "<link>" to index.php?css=1 for the "<head>"
190 *
191 * @return string
192 */
193 private function getCssUrl() {
194 return Html::linkedStyle( $this->parent->getUrl( [ 'css' => 1 ] ) );
195 }
196
197 public function useShortHeader( $use = true ) {
198 $this->useShortHeader = $use;
199 }
200
201 public function allowFrames( $allow = true ) {
202 $this->allowFrames = $allow;
203 }
204
205 public function flush() {
206 if ( !$this->headerDone ) {
207 $this->outputHeader();
208 }
209 if ( !$this->redirectTarget && strlen( $this->contents ) ) {
210 echo $this->contents;
211 flush();
212 $this->contents = '';
213 }
214 }
215
216 /**
217 * @since 1.33
218 * @return Language
219 */
220 private function getLanguage() {
221 global $wgLang;
222
223 return is_object( $wgLang ) ? $wgLang : Language::factory( 'en' );
224 }
225
226 /**
227 * @return string[]
228 */
229 public function getHeadAttribs() {
230 return [
231 'dir' => $this->getLanguage()->getDir(),
232 'lang' => $this->getLanguage()->getHtmlCode(),
233 ];
234 }
235
236 /**
237 * Get whether the header has been output
238 *
239 * @return bool
240 */
241 public function headerDone() {
242 return $this->headerDone;
243 }
244
245 public function outputHeader() {
246 $this->headerDone = true;
247 $this->parent->request->response()->header( 'Content-Type: text/html; charset=utf-8' );
248
249 if ( !$this->allowFrames ) {
250 $this->parent->request->response()->header( 'X-Frame-Options: DENY' );
251 }
252
253 if ( $this->redirectTarget ) {
254 $this->parent->request->response()->header( 'Location: ' . $this->redirectTarget );
255
256 return;
257 }
258
259 if ( $this->useShortHeader ) {
260 $this->outputShortHeader();
261
262 return;
263 }
264 ?>
265 <?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
266
267 <head>
268 <meta name="robots" content="noindex, nofollow" />
269 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
270 <title><?php $this->outputTitle(); ?></title>
271 <?php echo $this->getCssUrl() . "\n"; ?>
272 <?php echo $this->getJQuery() . "\n"; ?>
273 <?php echo Html::linkedScript( 'config.js' ) . "\n"; ?>
274 </head>
275
276 <?php echo Html::openElement( 'body', [ 'class' => $this->getLanguage()->getDir() ] ) . "\n"; ?>
277 <div id="mw-page-base"></div>
278 <div id="mw-head-base"></div>
279 <div id="content" class="mw-body" role="main">
280 <div id="bodyContent" class="mw-body-content">
281
282 <h1><?php $this->outputTitle(); ?></h1>
283 <?php
284 }
285
286 public function outputFooter() {
287 if ( $this->useShortHeader ) {
288 echo Html::closeElement( 'body' ) . Html::closeElement( 'html' );
289
290 return;
291 }
292 ?>
293
294 </div></div>
295
296 <div id="mw-panel">
297 <div class="portal" id="p-logo">
298 <a href="https://www.mediawiki.org/" title="Main Page"></a>
299 </div>
300 <?php
301 $message = wfMessage( 'config-sidebar' )->plain();
302 foreach ( explode( '----', $message ) as $section ) {
303 echo '<div class="portal"><div class="body">';
304 echo $this->parent->parse( $section, true );
305 echo '</div></div>';
306 }
307 ?>
308 </div>
309
310 <?php
311 echo Html::closeElement( 'body' ) . Html::closeElement( 'html' );
312 }
313
314 public function outputShortHeader() {
315 ?>
316 <?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
317
318 <head>
319 <meta name="robots" content="noindex, nofollow" />
320 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
321 <title><?php $this->outputTitle(); ?></title>
322 <?php echo $this->getCssUrl() . "\n"; ?>
323 <?php echo $this->getJQuery() . "\n"; ?>
324 <?php echo Html::linkedScript( 'config.js' ) . "\n"; ?>
325 </head>
326
327 <body style="background-image: none">
328 <?php
329 }
330
331 public function outputTitle() {
332 global $wgVersion;
333 echo wfMessage( 'config-title', $wgVersion )->escaped();
334 }
335
336 /**
337 * @return string
338 */
339 public function getJQuery() {
340 return Html::linkedScript( "../resources/lib/jquery/jquery.js" );
341 }
342
343 }