revert r103691 that makes update.php output garbage
[lhc/web/wiklou.git] / includes / installer / WebInstallerOutput.php
1 <?php
2 /**
3 * Output handler for the web installer.
4 *
5 * @file
6 * @ingroup Deployment
7 */
8
9 /**
10 * Output class modelled on OutputPage.
11 *
12 * I've opted to use a distinct class rather than derive from OutputPage here in
13 * the interests of separation of concerns: if we used a subclass, there would be
14 * quite a lot of things you could do in OutputPage that would break the installer,
15 * that wouldn't be immediately obvious.
16 *
17 * @ingroup Deployment
18 * @since 1.17
19 */
20 class WebInstallerOutput {
21 /**
22 * The WebInstaller object this WebInstallerOutput is used by.
23 *
24 * @var WebInstaller
25 */
26 public $parent;
27
28 /**
29 * Buffered contents that haven't been output yet
30 * @var String
31 */
32 private $contents = '';
33
34 /**
35 * Has the header (or short header) been output?
36 * @var bool
37 */
38 private $headerDone = false;
39
40 public $redirectTarget;
41
42 /**
43 * Does the current page need to allow being used as a frame?
44 * If not, X-Frame-Options will be output to forbid it.
45 *
46 * @var bool
47 */
48 public $allowFrames = false;
49
50 /**
51 * Whether to use the limited header (used during CC license callbacks)
52 * @var bool
53 */
54 private $useShortHeader = false;
55
56 /**
57 * Constructor.
58 *
59 * @param $parent WebInstaller
60 */
61 public function __construct( WebInstaller $parent ) {
62 $this->parent = $parent;
63 }
64
65 public function addHTML( $html ) {
66 $this->contents .= $html;
67 $this->flush();
68 }
69
70 public function addWikiText( $text ) {
71 $this->addHTML( $this->parent->parse( $text ) );
72 }
73
74 public function addHTMLNoFlush( $html ) {
75 $this->contents .= $html;
76 }
77
78 public function redirect( $url ) {
79 if ( $this->headerDone ) {
80 throw new MWException( __METHOD__ . ' called after sending headers' );
81 }
82 $this->redirectTarget = $url;
83 }
84
85 public function output() {
86 $this->flush();
87 $this->outputFooter();
88 }
89
90 /**
91 * Get the raw vector CSS, flipping if needed
92 * @param $dir String 'ltr' or 'rtl'
93 * @return String
94 */
95 public function getCSS( $dir ) {
96 $skinDir = dirname( dirname( dirname( __FILE__ ) ) ) . '/skins';
97 $vectorCssFile = "$skinDir/vector/screen.css";
98 $configCssFile = "$skinDir/common/config.css";
99 $css = '';
100 wfSuppressWarnings();
101 $vectorCss = file_get_contents( $vectorCssFile );
102 $configCss = file_get_contents( $configCssFile );
103 wfRestoreWarnings();
104 if( !$vectorCss || !$configCss ) {
105 $css = "/** Your webserver cannot read $vectorCssFile or $configCssFile, please check file permissions */";
106 }
107
108 $css .= str_replace( 'images/', '../skins/vector/images/', $vectorCss ) . "\n" . str_replace( 'images/', '../skins/common/images/', $configCss );
109 if( $dir == 'rtl' ) {
110 $css = CSSJanus::transform( $css, true );
111 }
112 return $css;
113 }
114
115 /**
116 * <link> to index.php?css=foobar for the <head>
117 * @return String
118 */
119 private function getCssUrl( ) {
120 return Html::linkedStyle( $_SERVER['PHP_SELF'] . '?css=' . $this->getDir() );
121 }
122
123 public function useShortHeader( $use = true ) {
124 $this->useShortHeader = $use;
125 }
126
127 public function allowFrames( $allow = true ) {
128 $this->allowFrames = $allow;
129 }
130
131 public function flush() {
132 if ( !$this->headerDone ) {
133 $this->outputHeader();
134 }
135 if ( !$this->redirectTarget && strlen( $this->contents ) ) {
136 echo $this->contents;
137 flush();
138 $this->contents = '';
139 }
140 }
141
142 /**
143 * @return string
144 */
145 public function getDir() {
146 global $wgLang;
147 if( !is_object( $wgLang ) || !$wgLang->isRtl() ) {
148 return 'ltr';
149 } else {
150 return 'rtl';
151 }
152 }
153
154 /**
155 * @return string
156 */
157 public function getLanguageCode() {
158 global $wgLang;
159 if( !is_object( $wgLang ) ) {
160 return 'en';
161 } else {
162 return $wgLang->getCode();
163 }
164 }
165
166 /**
167 * @return array
168 */
169 public function getHeadAttribs() {
170 return array(
171 'dir' => $this->getDir(),
172 'lang' => $this->getLanguageCode(),
173 );
174 }
175
176 /**
177 * Get whether the header has been output
178 * @return bool
179 */
180 public function headerDone() {
181 return $this->headerDone;
182 }
183
184 public function outputHeader() {
185 $this->headerDone = true;
186 $dbTypes = $this->parent->getDBTypes();
187
188 $this->parent->request->response()->header( 'Content-Type: text/html; charset=utf-8' );
189 if (!$this->allowFrames) {
190 $this->parent->request->response()->header( 'X-Frame-Options: DENY' );
191 }
192 if ( $this->redirectTarget ) {
193 $this->parent->request->response()->header( 'Location: '.$this->redirectTarget );
194 return;
195 }
196
197 if ( $this->useShortHeader ) {
198 $this->outputShortHeader();
199 return;
200 }
201
202 ?>
203 <?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
204 <head>
205 <meta name="robots" content="noindex, nofollow" />
206 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
207 <title><?php $this->outputTitle(); ?></title>
208 <?php echo Html::linkedStyle( '../skins/common/shared.css' ) . "\n"; ?>
209 <?php echo $this->getCssUrl() . "\n"; ?>
210 <?php echo Html::inlineScript( "var dbTypes = " . Xml::encodeJsVar( $dbTypes ) ) . "\n"; ?>
211 <?php echo $this->getJQuery() . "\n"; ?>
212 <?php echo Html::linkedScript( '../skins/common/config.js' ) . "\n"; ?>
213 </head>
214
215 <?php echo Html::openElement( 'body', array( 'class' => $this->getDir() ) ) . "\n"; ?>
216 <div id="mw-page-base"></div>
217 <div id="mw-head-base"></div>
218 <div id="content">
219 <div id="bodyContent">
220
221 <h1><?php $this->outputTitle(); ?></h1>
222 <?php
223 }
224
225 public function outputFooter() {
226 if ( $this->useShortHeader ) {
227 ?>
228 </body></html>
229 <?php
230 return;
231 }
232 ?>
233
234 </div></div>
235
236
237 <div id="mw-panel">
238 <div class="portal" id="p-logo">
239 <a style="background-image: url(../skins/common/images/mediawiki.png);"
240 href="http://www.mediawiki.org/"
241 title="Main Page"></a>
242 </div>
243 <script type="text/javascript"> if (window.isMSIE55) fixalpha(); </script>
244 <div class="portal"><div class="body">
245 <?php
246 echo $this->parent->parse( wfMsgNoTrans( 'config-sidebar' ), true );
247 ?>
248 </div></div>
249 </div>
250
251 </body>
252 </html>
253 <?php
254 }
255
256 public function outputShortHeader() {
257 ?>
258 <?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
259 <head>
260 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
261 <meta name="robots" content="noindex, nofollow" />
262 <title><?php $this->outputTitle(); ?></title>
263 <?php echo $this->getCssUrl() . "\n"; ?>
264 <?php echo $this->getJQuery(); ?>
265 <?php echo Html::linkedScript( '../skins/common/config.js' ); ?>
266 </head>
267
268 <body style="background-image: none">
269 <?php
270 }
271
272 public function outputTitle() {
273 global $wgVersion;
274 echo htmlspecialchars( wfMsg( 'config-title', $wgVersion ) );
275 }
276
277 public function getJQuery() {
278 return Html::linkedScript( "../resources/jquery/jquery.js" );
279 }
280 }