6c1f2ec2b705f45547feb461b1fc4e766bb6e4a5
[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 * @deprecated since 1.32; use addWikiTextAsInterface instead
93 */
94 public function addWikiText( $text ) {
95 wfDeprecated( __METHOD__, '1.32' );
96 $this->addWikiTextAsInterface( $text );
97 }
98
99 /**
100 * @param string $text
101 * @since 1.32
102 */
103 public function addWikiTextAsInterface( $text ) {
104 $this->addHTML( $this->parent->parse( $text ) );
105 }
106
107 /**
108 * @param string $html
109 */
110 public function addHTMLNoFlush( $html ) {
111 $this->contents .= $html;
112 }
113
114 /**
115 * @param string $url
116 *
117 * @throws MWException
118 */
119 public function redirect( $url ) {
120 if ( $this->headerDone ) {
121 throw new MWException( __METHOD__ . ' called after sending headers' );
122 }
123 $this->redirectTarget = $url;
124 }
125
126 public function output() {
127 $this->flush();
128
129 if ( !$this->redirectTarget ) {
130 $this->outputFooter();
131 }
132 }
133
134 /**
135 * Get the stylesheet of the MediaWiki skin.
136 *
137 * @return string
138 */
139 public function getCSS() {
140 global $wgStyleDirectory;
141
142 $moduleNames = [
143 // Based on Skin::getDefaultModules
144 'mediawiki.legacy.shared',
145 // Based on Vector::setupSkinUserCss
146 'mediawiki.skinning.interface',
147 ];
148
149 $resourceLoader = new ResourceLoader();
150
151 if ( file_exists( "$wgStyleDirectory/Vector/skin.json" ) ) {
152 // Force loading Vector skin if available as a fallback skin
153 // for whatever ResourceLoader wants to have as the default.
154 $registry = new ExtensionRegistry();
155 $data = $registry->readFromQueue( [
156 "$wgStyleDirectory/Vector/skin.json" => 1,
157 ] );
158 if ( isset( $data['globals']['wgResourceModules'] ) ) {
159 $resourceLoader->register( $data['globals']['wgResourceModules'] );
160 }
161
162 $moduleNames[] = 'skins.vector.styles';
163 }
164
165 $moduleNames[] = 'mediawiki.legacy.config';
166
167 $rlContext = new ResourceLoaderContext( $resourceLoader, new FauxRequest( [
168 'debug' => 'true',
169 'lang' => $this->getLanguageCode(),
170 'only' => 'styles',
171 ] ) );
172
173 $styles = [];
174 foreach ( $moduleNames as $moduleName ) {
175 /** @var ResourceLoaderFileModule $module */
176 $module = $resourceLoader->getModule( $moduleName );
177 if ( !$module ) {
178 // T98043: Don't fatal, but it won't look as pretty.
179 continue;
180 }
181
182 // Based on: ResourceLoaderFileModule::getStyles (without the DB query)
183 $styles = array_merge( $styles, ResourceLoader::makeCombinedStyles(
184 $module->readStyleFiles(
185 $module->getStyleFiles( $rlContext ),
186 $module->getFlip( $rlContext ),
187 $rlContext
188 ) ) );
189 }
190
191 return implode( "\n", $styles );
192 }
193
194 /**
195 * "<link>" to index.php?css=1 for the "<head>"
196 *
197 * @return string
198 */
199 private function getCssUrl() {
200 return Html::linkedStyle( $_SERVER['PHP_SELF'] . '?css=1' );
201 }
202
203 public function useShortHeader( $use = true ) {
204 $this->useShortHeader = $use;
205 }
206
207 public function allowFrames( $allow = true ) {
208 $this->allowFrames = $allow;
209 }
210
211 public function flush() {
212 if ( !$this->headerDone ) {
213 $this->outputHeader();
214 }
215 if ( !$this->redirectTarget && strlen( $this->contents ) ) {
216 echo $this->contents;
217 flush();
218 $this->contents = '';
219 }
220 }
221
222 /**
223 * @return string
224 */
225 public function getDir() {
226 global $wgLang;
227
228 return is_object( $wgLang ) ? $wgLang->getDir() : 'ltr';
229 }
230
231 /**
232 * @return string
233 */
234 public function getLanguageCode() {
235 global $wgLang;
236
237 return is_object( $wgLang ) ? $wgLang->getCode() : 'en';
238 }
239
240 /**
241 * @return string[]
242 */
243 public function getHeadAttribs() {
244 return [
245 'dir' => $this->getDir(),
246 'lang' => LanguageCode::bcp47( $this->getLanguageCode() ),
247 ];
248 }
249
250 /**
251 * Get whether the header has been output
252 *
253 * @return bool
254 */
255 public function headerDone() {
256 return $this->headerDone;
257 }
258
259 public function outputHeader() {
260 $this->headerDone = true;
261 $this->parent->request->response()->header( 'Content-Type: text/html; charset=utf-8' );
262
263 if ( !$this->allowFrames ) {
264 $this->parent->request->response()->header( 'X-Frame-Options: DENY' );
265 }
266
267 if ( $this->redirectTarget ) {
268 $this->parent->request->response()->header( 'Location: ' . $this->redirectTarget );
269
270 return;
271 }
272
273 if ( $this->useShortHeader ) {
274 $this->outputShortHeader();
275
276 return;
277 }
278 ?>
279 <?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
280
281 <head>
282 <meta name="robots" content="noindex, nofollow" />
283 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
284 <title><?php $this->outputTitle(); ?></title>
285 <?php echo $this->getCssUrl() . "\n"; ?>
286 <?php echo $this->getJQuery() . "\n"; ?>
287 <?php echo Html::linkedScript( 'config.js' ) . "\n"; ?>
288 </head>
289
290 <?php echo Html::openElement( 'body', [ 'class' => $this->getDir() ] ) . "\n"; ?>
291 <div id="mw-page-base"></div>
292 <div id="mw-head-base"></div>
293 <div id="content" class="mw-body">
294 <div id="bodyContent" class="mw-body-content">
295
296 <h1><?php $this->outputTitle(); ?></h1>
297 <?php
298 }
299
300 public function outputFooter() {
301 if ( $this->useShortHeader ) {
302 echo Html::closeElement( 'body' ) . Html::closeElement( 'html' );
303
304 return;
305 }
306 ?>
307
308 </div></div>
309
310 <div id="mw-panel">
311 <div class="portal" id="p-logo">
312 <a style="background-image: url(images/installer-logo.png);"
313 href="https://www.mediawiki.org/"
314 title="Main Page"></a>
315 </div>
316 <?php
317 $message = wfMessage( 'config-sidebar' )->plain();
318 foreach ( explode( '----', $message ) as $section ) {
319 echo '<div class="portal"><div class="body">';
320 echo $this->parent->parse( $section, true );
321 echo '</div></div>';
322 }
323 ?>
324 </div>
325
326 <?php
327 echo Html::closeElement( 'body' ) . Html::closeElement( 'html' );
328 }
329
330 public function outputShortHeader() {
331 ?>
332 <?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
333 <head>
334 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
335 <meta name="robots" content="noindex, nofollow" />
336 <title><?php $this->outputTitle(); ?></title>
337 <?php echo $this->getCssUrl() . "\n"; ?>
338 <?php echo $this->getJQuery(); ?>
339 <?php echo Html::linkedScript( 'config.js' ); ?>
340 </head>
341
342 <body style="background-image: none">
343 <?php
344 }
345
346 public function outputTitle() {
347 global $wgVersion;
348 echo wfMessage( 'config-title', $wgVersion )->escaped();
349 }
350
351 /**
352 * @return string
353 */
354 public function getJQuery() {
355 return Html::linkedScript( "../resources/lib/jquery/jquery.js" );
356 }
357
358 }