Merge "Revert "Follow-up ee320648fd1: output mw-content-{ltr,rtl} unconditionally""
[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 */
93 public function addWikiText( $text ) {
94 $this->addHTML( $this->parent->parse( $text ) );
95 }
96
97 /**
98 * @param string $html
99 */
100 public function addHTMLNoFlush( $html ) {
101 $this->contents .= $html;
102 }
103
104 /**
105 * @param string $url
106 *
107 * @throws MWException
108 */
109 public function redirect( $url ) {
110 if ( $this->headerDone ) {
111 throw new MWException( __METHOD__ . ' called after sending headers' );
112 }
113 $this->redirectTarget = $url;
114 }
115
116 public function output() {
117 $this->flush();
118 $this->outputFooter();
119 }
120
121 /**
122 * Get the stylesheet of the MediaWiki skin.
123 *
124 * @return string
125 */
126 public function getCSS() {
127 global $wgStyleDirectory;
128
129 $moduleNames = array(
130 // See SkinTemplate::setupSkinUserCss
131 'mediawiki.legacy.shared',
132 // See Vector::setupSkinUserCss
133 'mediawiki.skinning.interface',
134 );
135
136 $resourceLoader = new ResourceLoader();
137
138 if ( file_exists( "$wgStyleDirectory/Vector/skin.json" ) ) {
139 // Force loading Vector skin if available as a fallback skin
140 // for whatever ResourceLoader wants to have as the default.
141 $registry = new ExtensionRegistry();
142 $data = $registry->readFromQueue( array(
143 "$wgStyleDirectory/Vector/skin.json" => 1,
144 ) );
145 if ( isset( $data['globals']['wgResourceModules'] ) ) {
146 $resourceLoader->register( $data['globals']['wgResourceModules'] );
147 }
148
149 $moduleNames[] = 'skins.vector.styles';
150 }
151
152 $moduleNames[] = 'mediawiki.legacy.config';
153
154 $rlContext = new ResourceLoaderContext( $resourceLoader, new FauxRequest( array(
155 'debug' => 'true',
156 'lang' => $this->getLanguageCode(),
157 'only' => 'styles',
158 ) ) );
159
160 $styles = array();
161 foreach ( $moduleNames as $moduleName ) {
162 /** @var ResourceLoaderFileModule $module */
163 $module = $resourceLoader->getModule( $moduleName );
164 if ( !$module ) {
165 // T98043: Don't fatal, but it won't look as pretty.
166 continue;
167 }
168
169 // Based on: ResourceLoaderFileModule::getStyles (without the DB query)
170 $styles = array_merge( $styles, ResourceLoader::makeCombinedStyles(
171 $module->readStyleFiles(
172 $module->getStyleFiles( $rlContext ),
173 $module->getFlip( $rlContext )
174 ) ) );
175 }
176
177 return implode( "\n", $styles );
178 }
179
180 /**
181 * "<link>" to index.php?css=1 for the "<head>"
182 *
183 * @return string
184 */
185 private function getCssUrl() {
186 return Html::linkedStyle( $_SERVER['PHP_SELF'] . '?css=1' );
187 }
188
189 public function useShortHeader( $use = true ) {
190 $this->useShortHeader = $use;
191 }
192
193 public function allowFrames( $allow = true ) {
194 $this->allowFrames = $allow;
195 }
196
197 public function flush() {
198 if ( !$this->headerDone ) {
199 $this->outputHeader();
200 }
201 if ( !$this->redirectTarget && strlen( $this->contents ) ) {
202 echo $this->contents;
203 flush();
204 $this->contents = '';
205 }
206 }
207
208 /**
209 * @return string
210 */
211 public function getDir() {
212 global $wgLang;
213
214 return is_object( $wgLang ) ? $wgLang->getDir() : 'ltr';
215 }
216
217 /**
218 * @return string
219 */
220 public function getLanguageCode() {
221 global $wgLang;
222
223 return is_object( $wgLang ) ? $wgLang->getCode() : 'en';
224 }
225
226 /**
227 * @return string[]
228 */
229 public function getHeadAttribs() {
230 return array(
231 'dir' => $this->getDir(),
232 'lang' => wfBCP47( $this->getLanguageCode() ),
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 <head>
267 <meta name="robots" content="noindex, nofollow" />
268 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
269 <title><?php $this->outputTitle(); ?></title>
270 <?php echo $this->getCssUrl() . "\n"; ?>
271 <?php echo $this->getJQuery() . "\n"; ?>
272 <?php echo Html::linkedScript( 'config.js' ) . "\n"; ?>
273 </head>
274
275 <?php echo Html::openElement( 'body', array( 'class' => $this->getDir() ) ) . "\n"; ?>
276 <div id="mw-page-base"></div>
277 <div id="mw-head-base"></div>
278 <div id="content" class="mw-body">
279 <div id="bodyContent" class="mw-body-content">
280
281 <h1><?php $this->outputTitle(); ?></h1>
282 <?php
283 }
284
285 public function outputFooter() {
286 if ( $this->useShortHeader ) {
287 echo Html::closeElement( 'body' ) . Html::closeElement( 'html' );
288
289 return;
290 }
291 ?>
292
293 </div></div>
294
295 <div id="mw-panel">
296 <div class="portal" id="p-logo">
297 <a style="background-image: url(images/installer-logo.png);"
298 href="https://www.mediawiki.org/"
299 title="Main Page"></a>
300 </div>
301 <?php
302 $message = wfMessage( 'config-sidebar' )->plain();
303 foreach ( explode( '----', $message ) as $section ) {
304 echo '<div class="portal"><div class="body">';
305 echo $this->parent->parse( $section, true );
306 echo '</div></div>';
307 }
308 ?>
309 </div>
310
311 <?php
312 echo Html::closeElement( 'body' ) . Html::closeElement( 'html' );
313 }
314
315 public function outputShortHeader() {
316 ?>
317 <?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
318 <head>
319 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
320 <meta name="robots" content="noindex, nofollow" />
321 <title><?php $this->outputTitle(); ?></title>
322 <?php echo $this->getCssUrl() . "\n"; ?>
323 <?php echo $this->getJQuery(); ?>
324 <?php echo Html::linkedScript( 'config.js' ); ?>
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 }