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
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.
35 class WebInstallerOutput
{
37 * The WebInstaller object this WebInstallerOutput is used by.
44 * Buffered contents that haven't been output yet
47 private $contents = '';
50 * Has the header (or short header) been output?
53 private $headerDone = false;
55 public $redirectTarget;
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.
63 public $allowFrames = false;
66 * Whether to use the limited header (used during CC license callbacks)
69 private $useShortHeader = false;
74 * @param $parent WebInstaller
76 public function __construct( WebInstaller
$parent ) {
77 $this->parent
= $parent;
80 public function addHTML( $html ) {
81 $this->contents
.= $html;
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() {
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'
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',
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." );
141 if ( !is_readable( $cssFileName ) ) {
142 $prepend .= ResourceLoader
::makeComment( "Unable to read $cssFileName. " .
143 "Please check file permissions." );
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 );
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
),
170 /* embedData */ false
173 $prepend .= ResourceLoader
::makeComment( "Unable to read $cssFileName." );
175 } catch ( Exception
$e ) {
176 $prepend .= ResourceLoader
::formatException( $e );
184 $css = $prepend . $css;
186 if ( $dir == 'rtl' ) {
187 $css = CSSJanus
::transform( $css, true );
194 * "<link>" to index.php?css=foobar for the "<head>"
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
;
216 $this->contents
= '';
223 public function getDir() {
226 return is_object( $wgLang ) ?
$wgLang->getDir() : 'ltr';
232 public function getLanguageCode() {
235 return is_object( $wgLang ) ?
$wgLang->getCode() : 'en';
241 public function getHeadAttribs() {
243 'dir' => $this->getDir(),
244 'lang' => $this->getLanguageCode(),
249 * Get whether the header has been output
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
);
270 if ( $this->useShortHeader
) {
271 $this->outputShortHeader();
276 <?php
echo Html
::htmlHeader( $this->getHeadAttribs() ); ?
>
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"; ?
>
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
>
290 <div id
="bodyContent">
292 <h1
><?php
$this->outputTitle(); ?
></h1
>
296 public function outputFooter() {
297 if ( $this->useShortHeader
) {
298 echo Html
::closeElement( 'body' ) . Html
::closeElement( 'html' );
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
>
312 <div
class="portal"><div
class="body">
314 echo $this->parent
->parse( wfMessage( 'config-sidebar' )->plain(), true );
320 echo Html
::closeElement( 'body' ) . Html
::closeElement( 'html' );
323 public function outputShortHeader() {
325 <?php
echo Html
::htmlHeader( $this->getHeadAttribs() ); ?
>
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' ); ?
>
335 <body style
="background-image: none">
339 public function outputTitle() {
341 echo wfMessage( 'config-title', $wgVersion )->escaped();
344 public function getJQuery() {
345 return Html
::linkedScript( "../resources/jquery/jquery.js" );