Merge "Add .pipeline/ with dev image variant"
[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 '@phan-var ResourceLoaderFileModule $module';
172 if ( !$module ) {
173 // T98043: Don't fatal, but it won't look as pretty.
174 continue;
175 }
176
177 // Based on: ResourceLoaderFileModule::getStyles (without the DB query)
178 $styles = array_merge( $styles, ResourceLoader::makeCombinedStyles(
179 $module->readStyleFiles(
180 $module->getStyleFiles( $rlContext ),
181 $module->getFlip( $rlContext ),
182 $rlContext
183 ) ) );
184 }
185
186 return implode( "\n", $styles );
187 }
188
189 /**
190 * "<link>" to index.php?css=1 for the "<head>"
191 *
192 * @return string
193 */
194 private function getCssUrl() {
195 return Html::linkedStyle( $this->parent->getUrl( [ 'css' => 1 ] ) );
196 }
197
198 public function useShortHeader( $use = true ) {
199 $this->useShortHeader = $use;
200 }
201
202 public function allowFrames( $allow = true ) {
203 $this->allowFrames = $allow;
204 }
205
206 public function flush() {
207 if ( !$this->headerDone ) {
208 $this->outputHeader();
209 }
210 if ( !$this->redirectTarget && strlen( $this->contents ) ) {
211 echo $this->contents;
212 flush();
213 $this->contents = '';
214 }
215 }
216
217 /**
218 * @since 1.33
219 * @return Language
220 */
221 private function getLanguage() {
222 global $wgLang;
223
224 return is_object( $wgLang ) ? $wgLang : Language::factory( 'en' );
225 }
226
227 /**
228 * @return string[]
229 */
230 public function getHeadAttribs() {
231 return [
232 'dir' => $this->getLanguage()->getDir(),
233 'lang' => $this->getLanguage()->getHtmlCode(),
234 ];
235 }
236
237 /**
238 * Get whether the header has been output
239 *
240 * @return bool
241 */
242 public function headerDone() {
243 return $this->headerDone;
244 }
245
246 public function outputHeader() {
247 $this->headerDone = true;
248 $this->parent->request->response()->header( 'Content-Type: text/html; charset=utf-8' );
249
250 if ( !$this->allowFrames ) {
251 $this->parent->request->response()->header( 'X-Frame-Options: DENY' );
252 }
253
254 if ( $this->redirectTarget ) {
255 $this->parent->request->response()->header( 'Location: ' . $this->redirectTarget );
256
257 return;
258 }
259
260 if ( $this->useShortHeader ) {
261 $this->outputShortHeader();
262
263 return;
264 }
265 ?>
266 <?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
267
268 <head>
269 <meta name="robots" content="noindex, nofollow" />
270 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
271 <title><?php $this->outputTitle(); ?></title>
272 <?php echo $this->getCssUrl() . "\n"; ?>
273 <?php echo $this->getJQuery() . "\n"; ?>
274 <?php echo Html::linkedScript( 'config.js' ) . "\n"; ?>
275 </head>
276
277 <?php echo Html::openElement( 'body', [ 'class' => $this->getLanguage()->getDir() ] ) . "\n"; ?>
278 <div id="mw-page-base"></div>
279 <div id="mw-head-base"></div>
280 <div id="content" class="mw-body" role="main">
281 <div id="bodyContent" class="mw-body-content">
282
283 <h1><?php $this->outputTitle(); ?></h1>
284 <?php
285 }
286
287 public function outputFooter() {
288 if ( $this->useShortHeader ) {
289 echo Html::closeElement( 'body' ) . Html::closeElement( 'html' );
290
291 return;
292 }
293 ?>
294
295 </div></div>
296
297 <div id="mw-panel">
298 <div class="portal" id="p-logo">
299 <a href="https://www.mediawiki.org/" title="Main Page"></a>
300 </div>
301 <?php
302 $message = wfMessage( 'config-sidebar' )->plain();
303 // Section 1: External links
304 // @todo FIXME: Migrate to plain link label messages (T227297).
305 foreach ( explode( '----', $message ) as $section ) {
306 echo '<div class="portal"><div class="body">';
307 echo $this->parent->parse( $section, true );
308 echo '</div></div>';
309 }
310 // Section 2: Installer pages
311 echo '<div class="portal"><div class="body"><ul>';
312 foreach ( [
313 'config-sidebar-readme' => 'Readme',
314 'config-sidebar-relnotes' => 'ReleaseNotes',
315 'config-sidebar-license' => 'Copying',
316 'config-sidebar-upgrade' => 'UpgradeDoc',
317 ] as $msgKey => $pageName ) {
318 echo $this->parent->makeLinkItem(
319 $this->parent->getDocUrl( $pageName ),
320 wfMessage( $msgKey )->text()
321 );
322 }
323 echo '</ul></div></div>';
324 ?>
325 </div>
326
327 <?php
328 echo Html::closeElement( 'body' ) . Html::closeElement( 'html' );
329 }
330
331 public function outputShortHeader() {
332 ?>
333 <?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
334
335 <head>
336 <meta name="robots" content="noindex, nofollow" />
337 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
338 <title><?php $this->outputTitle(); ?></title>
339 <?php echo $this->getCssUrl() . "\n"; ?>
340 <?php echo $this->getJQuery() . "\n"; ?>
341 <?php echo Html::linkedScript( 'config.js' ) . "\n"; ?>
342 </head>
343
344 <body style="background-image: none">
345 <?php
346 }
347
348 public function outputTitle() {
349 global $wgVersion;
350 echo wfMessage( 'config-title', $wgVersion )->escaped();
351 }
352
353 /**
354 * @return string
355 */
356 public function getJQuery() {
357 return Html::linkedScript( "../resources/lib/jquery/jquery.js" );
358 }
359
360 }