ccaf52385def9bb35ab274cfcf3e313db174b29c
[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 // Horrible, horrible hack: the installer is currently hardcoded to use the Vector skin, so load
128 // it here. Include instead of require, as this will work without it, it will just look bad.
129 // We need the 'global' statement for $wgResourceModules because the Vector skin adds the
130 // definitions for its RL modules there that we use implicitly below.
131 // @codingStandardsIgnoreStart
132 global $wgResourceModules; // This is NOT UNUSED!
133 // @codingStandardsIgnoreEnd
134 global $wgStyleDirectory;
135 include_once "$wgStyleDirectory/Vector/Vector.php";
136
137 $moduleNames = array(
138 // See SkinTemplate::setupSkinUserCss
139 'mediawiki.legacy.shared',
140 // See Vector::setupSkinUserCss
141 'mediawiki.skinning.interface',
142 'skins.vector.styles',
143
144 'mediawiki.legacy.config',
145 );
146
147 $css = '';
148
149 $resourceLoader = new ResourceLoader();
150 $rlContext = new ResourceLoaderContext( $resourceLoader, new FauxRequest( array(
151 'debug' => 'true',
152 'lang' => $this->getLanguageCode(),
153 'only' => 'styles',
154 'skin' => 'vector',
155 ) ) );
156 foreach ( $moduleNames as $moduleName ) {
157 /** @var ResourceLoaderFileModule $module */
158 $module = $resourceLoader->getModule( $moduleName );
159 // One of the modules will be missing if Vector is unavailable
160 if ( !$module ) {
161 continue;
162 }
163
164 // Based on: ResourceLoaderFileModule::getStyles (without the DB query)
165 $styles = ResourceLoader::makeCombinedStyles( $module->readStyleFiles(
166 $module->getStyleFiles( $rlContext ),
167 $module->getFlip( $rlContext )
168 ) );
169
170 $css .= implode( "\n", $styles );
171 }
172
173 return $css;
174 }
175
176 /**
177 * "<link>" to index.php?css=1 for the "<head>"
178 *
179 * @return string
180 */
181 private function getCssUrl() {
182 return Html::linkedStyle( $_SERVER['PHP_SELF'] . '?css=1' );
183 }
184
185 public function useShortHeader( $use = true ) {
186 $this->useShortHeader = $use;
187 }
188
189 public function allowFrames( $allow = true ) {
190 $this->allowFrames = $allow;
191 }
192
193 public function flush() {
194 if ( !$this->headerDone ) {
195 $this->outputHeader();
196 }
197 if ( !$this->redirectTarget && strlen( $this->contents ) ) {
198 echo $this->contents;
199 flush();
200 $this->contents = '';
201 }
202 }
203
204 /**
205 * @return string
206 */
207 public function getDir() {
208 global $wgLang;
209
210 return is_object( $wgLang ) ? $wgLang->getDir() : 'ltr';
211 }
212
213 /**
214 * @return string
215 */
216 public function getLanguageCode() {
217 global $wgLang;
218
219 return is_object( $wgLang ) ? $wgLang->getCode() : 'en';
220 }
221
222 /**
223 * @return string[]
224 */
225 public function getHeadAttribs() {
226 return array(
227 'dir' => $this->getDir(),
228 'lang' => $this->getLanguageCode(),
229 );
230 }
231
232 /**
233 * Get whether the header has been output
234 *
235 * @return bool
236 */
237 public function headerDone() {
238 return $this->headerDone;
239 }
240
241 public function outputHeader() {
242 $this->headerDone = true;
243 $this->parent->request->response()->header( 'Content-Type: text/html; charset=utf-8' );
244
245 if ( !$this->allowFrames ) {
246 $this->parent->request->response()->header( 'X-Frame-Options: DENY' );
247 }
248
249 if ( $this->redirectTarget ) {
250 $this->parent->request->response()->header( 'Location: ' . $this->redirectTarget );
251
252 return;
253 }
254
255 if ( $this->useShortHeader ) {
256 $this->outputShortHeader();
257
258 return;
259 }
260 ?>
261 <?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
262 <head>
263 <meta name="robots" content="noindex, nofollow" />
264 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
265 <title><?php $this->outputTitle(); ?></title>
266 <?php echo $this->getCssUrl() . "\n"; ?>
267 <?php echo $this->getJQuery() . "\n"; ?>
268 <?php echo Html::linkedScript( 'config.js' ) . "\n"; ?>
269 </head>
270
271 <?php echo Html::openElement( 'body', array( 'class' => $this->getDir() ) ) . "\n"; ?>
272 <div id="mw-page-base"></div>
273 <div id="mw-head-base"></div>
274 <div id="content" class="mw-body">
275 <div id="bodyContent" class="mw-body-content">
276
277 <h1><?php $this->outputTitle(); ?></h1>
278 <?php
279 }
280
281 public function outputFooter() {
282 if ( $this->useShortHeader ) {
283 echo Html::closeElement( 'body' ) . Html::closeElement( 'html' );
284
285 return;
286 }
287 ?>
288
289 </div></div>
290
291 <div id="mw-panel">
292 <div class="portal" id="p-logo">
293 <a style="background-image: url(images/installer-logo.png);"
294 href="https://www.mediawiki.org/"
295 title="Main Page"></a>
296 </div>
297 <div class="portal"><div class="body">
298 <?php
299 echo $this->parent->parse( wfMessage( 'config-sidebar' )->plain(), true );
300 ?>
301 </div></div>
302 </div>
303
304 <?php
305 echo Html::closeElement( 'body' ) . Html::closeElement( 'html' );
306 }
307
308 public function outputShortHeader() {
309 ?>
310 <?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
311 <head>
312 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
313 <meta name="robots" content="noindex, nofollow" />
314 <title><?php $this->outputTitle(); ?></title>
315 <?php echo $this->getCssUrl() . "\n"; ?>
316 <?php echo $this->getJQuery(); ?>
317 <?php echo Html::linkedScript( 'config.js' ); ?>
318 </head>
319
320 <body style="background-image: none">
321 <?php
322 }
323
324 public function outputTitle() {
325 global $wgVersion;
326 echo wfMessage( 'config-title', $wgVersion )->escaped();
327 }
328
329 /**
330 * @return string
331 */
332 public function getJQuery() {
333 return Html::linkedScript( "../resources/lib/jquery/jquery.js" );
334 }
335
336 }