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