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
{
38 * The WebInstaller object this WebInstallerOutput is used by.
45 * Buffered contents that haven't been output yet
48 private $contents = '';
51 * Has the header (or short header) been output?
54 private $headerDone = false;
59 public $redirectTarget;
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.
67 public $allowFrames = false;
70 * Whether to use the limited header (used during CC license callbacks)
73 private $useShortHeader = false;
76 * @param WebInstaller $parent
78 public function __construct( WebInstaller
$parent ) {
79 $this->parent
= $parent;
85 public function addHTML( $html ) {
86 $this->contents
.= $html;
93 public function addWikiText( $text ) {
94 $this->addHTML( $this->parent
->parse( $text ) );
100 public function addHTMLNoFlush( $html ) {
101 $this->contents
.= $html;
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() {
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'
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',
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." );
158 if ( !is_readable( $cssFileName ) ) {
159 $prepend .= ResourceLoader
::makeComment( "Unable to read $cssFileName. " .
160 "Please check file permissions." );
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 );
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
),
187 /* embedData */ false
190 $prepend .= ResourceLoader
::makeComment( "Unable to read $cssFileName." );
192 } catch ( Exception
$e ) {
193 $prepend .= ResourceLoader
::formatException( $e );
201 $css = $prepend . $css;
203 if ( $dir == 'rtl' ) {
204 $css = CSSJanus
::transform( $css, true );
211 * "<link>" to index.php?css=foobar for the "<head>"
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
;
234 $this->contents
= '';
241 public function getDir() {
244 return is_object( $wgLang ) ?
$wgLang->getDir() : 'ltr';
250 public function getLanguageCode() {
253 return is_object( $wgLang ) ?
$wgLang->getCode() : 'en';
259 public function getHeadAttribs() {
261 'dir' => $this->getDir(),
262 'lang' => $this->getLanguageCode(),
267 * Get whether the header has been output
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
);
289 if ( $this->useShortHeader
) {
290 $this->outputShortHeader();
295 <?php
echo Html
::htmlHeader( $this->getHeadAttribs() ); ?
>
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"; ?
>
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
>
309 <div id
="bodyContent">
311 <h1
><?php
$this->outputTitle(); ?
></h1
>
315 public function outputFooter() {
316 if ( $this->useShortHeader
) {
317 echo Html
::closeElement( 'body' ) . Html
::closeElement( 'html' );
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
>
331 <div
class="portal"><div
class="body">
333 echo $this->parent
->parse( wfMessage( 'config-sidebar' )->plain(), true );
339 echo Html
::closeElement( 'body' ) . Html
::closeElement( 'html' );
342 public function outputShortHeader() {
344 <?php
echo Html
::htmlHeader( $this->getHeadAttribs() ); ?
>
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' ); ?
>
354 <body style
="background-image: none">
358 public function outputTitle() {
360 echo wfMessage( 'config-title', $wgVersion )->escaped();
366 public function getJQuery() {
367 return Html
::linkedScript( "../resources/lib/jquery/jquery.js" );