Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / installer / WebInstallerOutput.php
blob25634baf686f2ee74c749a1f823f17e52d5f409e
1 <?php
2 /**
3 * Output handler for the web installer.
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.
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.
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
20 * @file
21 * @ingroup Deployment
24 /**
25 * Output class modelled on OutputPage.
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.
32 * @ingroup Deployment
33 * @since 1.17
35 class WebInstallerOutput {
36 /**
37 * The WebInstaller object this WebInstallerOutput is used by.
39 * @var WebInstaller
41 public $parent;
43 /**
44 * Buffered contents that haven't been output yet
45 * @var String
47 private $contents = '';
49 /**
50 * Has the header (or short header) been output?
51 * @var bool
53 private $headerDone = false;
55 public $redirectTarget;
57 /**
58 * Does the current page need to allow being used as a frame?
59 * If not, X-Frame-Options will be output to forbid it.
61 * @var bool
63 public $allowFrames = false;
65 /**
66 * Whether to use the limited header (used during CC license callbacks)
67 * @var bool
69 private $useShortHeader = false;
71 /**
72 * Constructor.
74 * @param $parent WebInstaller
76 public function __construct( WebInstaller $parent ) {
77 $this->parent = $parent;
80 public function addHTML( $html ) {
81 $this->contents .= $html;
82 $this->flush();
85 public function addWikiText( $text ) {
86 $this->addHTML( $this->parent->parse( $text ) );
89 public function addHTMLNoFlush( $html ) {
90 $this->contents .= $html;
93 public function redirect( $url ) {
94 if ( $this->headerDone ) {
95 throw new MWException( __METHOD__ . ' called after sending headers' );
97 $this->redirectTarget = $url;
100 public function output() {
101 $this->flush();
102 $this->outputFooter();
106 * Get the raw vector CSS, flipping if needed
108 * @todo Possibly get rid of this function and use ResourceLoader in the manner it was
109 * designed to be used in, rather than just grabbing a list of filenames from it,
110 * and not properly handling such details as media types in module definitions.
112 * @param string $dir 'ltr' or 'rtl'
113 * @return String
115 public function getCSS( $dir ) {
116 // All CSS files these modules reference will be concatenated in sequence
117 // and loaded as one file.
118 $moduleNames = array(
119 'mediawiki.legacy.shared',
120 'skins.common.interface',
121 'skins.vector.styles',
122 'mediawiki.legacy.config',
125 $prepend = '';
126 $css = '';
128 $resourceLoader = new ResourceLoader();
129 foreach ( $moduleNames as $moduleName ) {
130 /** @var ResourceLoaderFileModule $module */
131 $module = $resourceLoader->getModule( $moduleName );
132 $cssFileNames = $module->getAllStyleFiles();
134 wfSuppressWarnings();
135 foreach ( $cssFileNames as $cssFileName ) {
136 if ( !file_exists( $cssFileName ) ) {
137 $prepend .= ResourceLoader::makeComment( "Unable to find $cssFileName." );
138 continue;
141 if ( !is_readable( $cssFileName ) ) {
142 $prepend .= ResourceLoader::makeComment( "Unable to read $cssFileName. " .
143 "Please check file permissions." );
144 continue;
147 try {
149 if ( preg_match( '/\.less$/', $cssFileName ) ) {
150 // Run the LESS compiler for *.less files (bug 55589)
151 $compiler = ResourceLoader::getLessCompiler();
152 $cssFileContents = $compiler->compileFile( $cssFileName );
153 } else {
154 // Regular CSS file
155 $cssFileContents = file_get_contents( $cssFileName );
158 if ( $cssFileContents ) {
159 // Rewrite URLs, though don't bother embedding images. While static image
160 // files may be cached, CSS returned by this function is definitely not.
161 $cssDirName = dirname( $cssFileName );
162 $css .= CSSMin::remap(
163 /* source */ $cssFileContents,
164 /* local */ $cssDirName,
165 /* remote */ '..' . str_replace(
166 array( $GLOBALS['IP'], DIRECTORY_SEPARATOR ),
167 array( '', '/' ),
168 $cssDirName
170 /* embedData */ false
172 } else {
173 $prepend .= ResourceLoader::makeComment( "Unable to read $cssFileName." );
175 } catch ( Exception $e ) {
176 $prepend .= ResourceLoader::formatException( $e );
179 $css .= "\n";
181 wfRestoreWarnings();
184 $css = $prepend . $css;
186 if ( $dir == 'rtl' ) {
187 $css = CSSJanus::transform( $css, true );
190 return $css;
194 * "<link>" to index.php?css=foobar for the "<head>"
195 * @return String
197 private function getCssUrl() {
198 return Html::linkedStyle( $_SERVER['PHP_SELF'] . '?css=' . $this->getDir() );
201 public function useShortHeader( $use = true ) {
202 $this->useShortHeader = $use;
205 public function allowFrames( $allow = true ) {
206 $this->allowFrames = $allow;
209 public function flush() {
210 if ( !$this->headerDone ) {
211 $this->outputHeader();
213 if ( !$this->redirectTarget && strlen( $this->contents ) ) {
214 echo $this->contents;
215 flush();
216 $this->contents = '';
221 * @return string
223 public function getDir() {
224 global $wgLang;
226 return is_object( $wgLang ) ? $wgLang->getDir() : 'ltr';
230 * @return string
232 public function getLanguageCode() {
233 global $wgLang;
235 return is_object( $wgLang ) ? $wgLang->getCode() : 'en';
239 * @return array
241 public function getHeadAttribs() {
242 return array(
243 'dir' => $this->getDir(),
244 'lang' => $this->getLanguageCode(),
249 * Get whether the header has been output
250 * @return bool
252 public function headerDone() {
253 return $this->headerDone;
256 public function outputHeader() {
257 $this->headerDone = true;
258 $this->parent->request->response()->header( 'Content-Type: text/html; charset=utf-8' );
260 if ( !$this->allowFrames ) {
261 $this->parent->request->response()->header( 'X-Frame-Options: DENY' );
264 if ( $this->redirectTarget ) {
265 $this->parent->request->response()->header( 'Location: ' . $this->redirectTarget );
267 return;
270 if ( $this->useShortHeader ) {
271 $this->outputShortHeader();
273 return;
276 <?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
277 <head>
278 <meta name="robots" content="noindex, nofollow" />
279 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
280 <title><?php $this->outputTitle(); ?></title>
281 <?php echo $this->getCssUrl() . "\n"; ?>
282 <?php echo $this->getJQuery() . "\n"; ?>
283 <?php echo Html::linkedScript( '../skins/common/config.js' ) . "\n"; ?>
284 </head>
286 <?php echo Html::openElement( 'body', array( 'class' => $this->getDir() ) ) . "\n"; ?>
287 <div id="mw-page-base"></div>
288 <div id="mw-head-base"></div>
289 <div id="content">
290 <div id="bodyContent">
292 <h1><?php $this->outputTitle(); ?></h1>
293 <?php
296 public function outputFooter() {
297 if ( $this->useShortHeader ) {
298 echo Html::closeElement( 'body' ) . Html::closeElement( 'html' );
300 return;
304 </div></div>
306 <div id="mw-panel">
307 <div class="portal" id="p-logo">
308 <a style="background-image: url(../skins/common/images/mediawiki.png);"
309 href="http://www.mediawiki.org/"
310 title="Main Page"></a>
311 </div>
312 <div class="portal"><div class="body">
313 <?php
314 echo $this->parent->parse( wfMessage( 'config-sidebar' )->plain(), true );
316 </div></div>
317 </div>
319 <?php
320 echo Html::closeElement( 'body' ) . Html::closeElement( 'html' );
323 public function outputShortHeader() {
325 <?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
326 <head>
327 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
328 <meta name="robots" content="noindex, nofollow" />
329 <title><?php $this->outputTitle(); ?></title>
330 <?php echo $this->getCssUrl() . "\n"; ?>
331 <?php echo $this->getJQuery(); ?>
332 <?php echo Html::linkedScript( '../skins/common/config.js' ); ?>
333 </head>
335 <body style="background-image: none">
336 <?php
339 public function outputTitle() {
340 global $wgVersion;
341 echo wfMessage( 'config-title', $wgVersion )->escaped();
344 public function getJQuery() {
345 return Html::linkedScript( "../resources/jquery/jquery.js" );