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