Remove underscore from classes WebInstaller_*
[mediawiki.git] / includes / installer / WebInstallerOutput.php
blobc3fd51de76886d6b005636b7ae3d7a71c85b93f6
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 {
37 /**
38 * The WebInstaller object this WebInstallerOutput is used by.
40 * @var WebInstaller
42 public $parent;
44 /**
45 * Buffered contents that haven't been output yet
46 * @var string
48 private $contents = '';
50 /**
51 * Has the header (or short header) been output?
52 * @var bool
54 private $headerDone = false;
56 /**
57 * @var string
59 public $redirectTarget;
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.
65 * @var bool
67 public $allowFrames = false;
69 /**
70 * Whether to use the limited header (used during CC license callbacks)
71 * @var bool
73 private $useShortHeader = false;
75 /**
76 * @param WebInstaller $parent
78 public function __construct( WebInstaller $parent ) {
79 $this->parent = $parent;
82 /**
83 * @param string $html
85 public function addHTML( $html ) {
86 $this->contents .= $html;
87 $this->flush();
90 /**
91 * @param string $text
93 public function addWikiText( $text ) {
94 $this->addHTML( $this->parent->parse( $text ) );
97 /**
98 * @param string $html
100 public function addHTMLNoFlush( $html ) {
101 $this->contents .= $html;
105 * @param string $url
107 * @throws MWException
109 public function redirect( $url ) {
110 if ( $this->headerDone ) {
111 throw new MWException( __METHOD__ . ' called after sending headers' );
113 $this->redirectTarget = $url;
116 public function output() {
117 $this->flush();
118 $this->outputFooter();
122 * Get the raw vector CSS, flipping if needed
124 * @todo Possibly get rid of this function and use ResourceLoader in the manner it was
125 * designed to be used in, rather than just grabbing a list of filenames from it,
126 * and not properly handling such details as media types in module definitions.
128 * @param string $dir 'ltr' or 'rtl'
130 * @return string
132 public function getCSS( $dir ) {
133 // All CSS files these modules reference will be concatenated in sequence
134 // and loaded as one file.
135 $moduleNames = array(
136 'mediawiki.legacy.shared',
137 'mediawiki.skinning.interface',
138 'skins.vector.styles',
139 'mediawiki.legacy.config',
142 $prepend = '';
143 $css = '';
145 $resourceLoader = new ResourceLoader();
146 foreach ( $moduleNames as $moduleName ) {
147 /** @var ResourceLoaderFileModule $module */
148 $module = $resourceLoader->getModule( $moduleName );
149 $cssFileNames = $module->getAllStyleFiles();
151 wfSuppressWarnings();
152 foreach ( $cssFileNames as $cssFileName ) {
153 if ( !file_exists( $cssFileName ) ) {
154 $prepend .= ResourceLoader::makeComment( "Unable to find $cssFileName." );
155 continue;
158 if ( !is_readable( $cssFileName ) ) {
159 $prepend .= ResourceLoader::makeComment( "Unable to read $cssFileName. " .
160 "Please check file permissions." );
161 continue;
164 try {
166 if ( preg_match( '/\.less$/', $cssFileName ) ) {
167 // Run the LESS compiler for *.less files (bug 55589)
168 $compiler = ResourceLoader::getLessCompiler();
169 $cssFileContents = $compiler->compileFile( $cssFileName );
170 } else {
171 // Regular CSS file
172 $cssFileContents = file_get_contents( $cssFileName );
175 if ( $cssFileContents ) {
176 // Rewrite URLs, though don't bother embedding images. While static image
177 // files may be cached, CSS returned by this function is definitely not.
178 $cssDirName = dirname( $cssFileName );
179 $css .= CSSMin::remap(
180 /* source */ $cssFileContents,
181 /* local */ $cssDirName,
182 /* remote */ '..' . str_replace(
183 array( $GLOBALS['IP'], DIRECTORY_SEPARATOR ),
184 array( '', '/' ),
185 $cssDirName
187 /* embedData */ false
189 } else {
190 $prepend .= ResourceLoader::makeComment( "Unable to read $cssFileName." );
192 } catch ( Exception $e ) {
193 $prepend .= ResourceLoader::formatException( $e );
196 $css .= "\n";
198 wfRestoreWarnings();
201 $css = $prepend . $css;
203 if ( $dir == 'rtl' ) {
204 $css = CSSJanus::transform( $css, true );
207 return $css;
211 * "<link>" to index.php?css=foobar for the "<head>"
213 * @return string
215 private function getCssUrl() {
216 return Html::linkedStyle( $_SERVER['PHP_SELF'] . '?css=' . $this->getDir() );
219 public function useShortHeader( $use = true ) {
220 $this->useShortHeader = $use;
223 public function allowFrames( $allow = true ) {
224 $this->allowFrames = $allow;
227 public function flush() {
228 if ( !$this->headerDone ) {
229 $this->outputHeader();
231 if ( !$this->redirectTarget && strlen( $this->contents ) ) {
232 echo $this->contents;
233 flush();
234 $this->contents = '';
239 * @return string
241 public function getDir() {
242 global $wgLang;
244 return is_object( $wgLang ) ? $wgLang->getDir() : 'ltr';
248 * @return string
250 public function getLanguageCode() {
251 global $wgLang;
253 return is_object( $wgLang ) ? $wgLang->getCode() : 'en';
257 * @return string[]
259 public function getHeadAttribs() {
260 return array(
261 'dir' => $this->getDir(),
262 'lang' => $this->getLanguageCode(),
267 * Get whether the header has been output
269 * @return bool
271 public function headerDone() {
272 return $this->headerDone;
275 public function outputHeader() {
276 $this->headerDone = true;
277 $this->parent->request->response()->header( 'Content-Type: text/html; charset=utf-8' );
279 if ( !$this->allowFrames ) {
280 $this->parent->request->response()->header( 'X-Frame-Options: DENY' );
283 if ( $this->redirectTarget ) {
284 $this->parent->request->response()->header( 'Location: ' . $this->redirectTarget );
286 return;
289 if ( $this->useShortHeader ) {
290 $this->outputShortHeader();
292 return;
295 <?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
296 <head>
297 <meta name="robots" content="noindex, nofollow" />
298 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
299 <title><?php $this->outputTitle(); ?></title>
300 <?php echo $this->getCssUrl() . "\n"; ?>
301 <?php echo $this->getJQuery() . "\n"; ?>
302 <?php echo Html::linkedScript( '../skins/common/config.js' ) . "\n"; ?>
303 </head>
305 <?php echo Html::openElement( 'body', array( 'class' => $this->getDir() ) ) . "\n"; ?>
306 <div id="mw-page-base"></div>
307 <div id="mw-head-base"></div>
308 <div id="content">
309 <div id="bodyContent">
311 <h1><?php $this->outputTitle(); ?></h1>
312 <?php
315 public function outputFooter() {
316 if ( $this->useShortHeader ) {
317 echo Html::closeElement( 'body' ) . Html::closeElement( 'html' );
319 return;
323 </div></div>
325 <div id="mw-panel">
326 <div class="portal" id="p-logo">
327 <a style="background-image: url(../skins/common/images/mediawiki.png);"
328 href="https://www.mediawiki.org/"
329 title="Main Page"></a>
330 </div>
331 <div class="portal"><div class="body">
332 <?php
333 echo $this->parent->parse( wfMessage( 'config-sidebar' )->plain(), true );
335 </div></div>
336 </div>
338 <?php
339 echo Html::closeElement( 'body' ) . Html::closeElement( 'html' );
342 public function outputShortHeader() {
344 <?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
345 <head>
346 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
347 <meta name="robots" content="noindex, nofollow" />
348 <title><?php $this->outputTitle(); ?></title>
349 <?php echo $this->getCssUrl() . "\n"; ?>
350 <?php echo $this->getJQuery(); ?>
351 <?php echo Html::linkedScript( '../skins/common/config.js' ); ?>
352 </head>
354 <body style="background-image: none">
355 <?php
358 public function outputTitle() {
359 global $wgVersion;
360 echo wfMessage( 'config-title', $wgVersion )->escaped();
364 * @return string
366 public function getJQuery() {
367 return Html::linkedScript( "../resources/lib/jquery/jquery.js" );