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