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