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