Merge "Set noindex,nofollow on missing (404) pages"
[mediawiki.git] / includes / installer / WebInstallerOutput.php
blob97f48300bf9d1e9163bb419a7359f26a483e58e3
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 stylesheet of the MediaWiki skin.
124 * @return string
126 public function getCSS() {
127 // Horrible, horrible hack: the installer is currently hardcoded to use the Vector skin, so load
128 // it here. Include instead of require, as this will work without it, it will just look bad.
129 global $wgResourceModules;
130 global $wgStyleDirectory;
131 include_once "$wgStyleDirectory/Vector/Vector.php";
133 $moduleNames = array(
134 // See SkinTemplate::setupSkinUserCss
135 'mediawiki.legacy.shared',
136 // See Vector::setupSkinUserCss
137 'mediawiki.skinning.interface',
138 'skins.vector.styles',
140 'mediawiki.legacy.config',
143 $css = '';
145 $resourceLoader = new ResourceLoader();
146 $rlContext = new ResourceLoaderContext( $resourceLoader, new FauxRequest( array(
147 'debug' => 'true',
148 'lang' => $this->getLanguageCode(),
149 'only' => 'styles',
150 'skin' => 'vector',
151 ) ) );
152 foreach ( $moduleNames as $moduleName ) {
153 /** @var ResourceLoaderFileModule $module */
154 $module = $resourceLoader->getModule( $moduleName );
155 // One of the modules will be missing if Vector is unavailable
156 if ( !$module ) {
157 continue;
160 // Based on: ResourceLoaderFileModule::getStyles (without the DB query)
161 $styles = ResourceLoader::makeCombinedStyles( $module->readStyleFiles(
162 $module->getStyleFiles( $rlContext ),
163 $module->getFlip( $rlContext )
164 ) );
166 $css .= implode( "\n", $styles );
169 return $css;
173 * "<link>" to index.php?css=1 for the "<head>"
175 * @return string
177 private function getCssUrl() {
178 return Html::linkedStyle( $_SERVER['PHP_SELF'] . '?css=1' );
181 public function useShortHeader( $use = true ) {
182 $this->useShortHeader = $use;
185 public function allowFrames( $allow = true ) {
186 $this->allowFrames = $allow;
189 public function flush() {
190 if ( !$this->headerDone ) {
191 $this->outputHeader();
193 if ( !$this->redirectTarget && strlen( $this->contents ) ) {
194 echo $this->contents;
195 flush();
196 $this->contents = '';
201 * @return string
203 public function getDir() {
204 global $wgLang;
206 return is_object( $wgLang ) ? $wgLang->getDir() : 'ltr';
210 * @return string
212 public function getLanguageCode() {
213 global $wgLang;
215 return is_object( $wgLang ) ? $wgLang->getCode() : 'en';
219 * @return string[]
221 public function getHeadAttribs() {
222 return array(
223 'dir' => $this->getDir(),
224 'lang' => $this->getLanguageCode(),
229 * Get whether the header has been output
231 * @return bool
233 public function headerDone() {
234 return $this->headerDone;
237 public function outputHeader() {
238 $this->headerDone = true;
239 $this->parent->request->response()->header( 'Content-Type: text/html; charset=utf-8' );
241 if ( !$this->allowFrames ) {
242 $this->parent->request->response()->header( 'X-Frame-Options: DENY' );
245 if ( $this->redirectTarget ) {
246 $this->parent->request->response()->header( 'Location: ' . $this->redirectTarget );
248 return;
251 if ( $this->useShortHeader ) {
252 $this->outputShortHeader();
254 return;
257 <?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
258 <head>
259 <meta name="robots" content="noindex, nofollow" />
260 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
261 <title><?php $this->outputTitle(); ?></title>
262 <?php echo $this->getCssUrl() . "\n"; ?>
263 <?php echo $this->getJQuery() . "\n"; ?>
264 <?php echo Html::linkedScript( '../skins/common/config.js' ) . "\n"; ?>
265 </head>
267 <?php echo Html::openElement( 'body', array( 'class' => $this->getDir() ) ) . "\n"; ?>
268 <div id="mw-page-base"></div>
269 <div id="mw-head-base"></div>
270 <div id="content">
271 <div id="bodyContent">
273 <h1><?php $this->outputTitle(); ?></h1>
274 <?php
277 public function outputFooter() {
278 if ( $this->useShortHeader ) {
279 echo Html::closeElement( 'body' ) . Html::closeElement( 'html' );
281 return;
285 </div></div>
287 <div id="mw-panel">
288 <div class="portal" id="p-logo">
289 <a style="background-image: url(../skins/common/images/mediawiki.png);"
290 href="https://www.mediawiki.org/"
291 title="Main Page"></a>
292 </div>
293 <div class="portal"><div class="body">
294 <?php
295 echo $this->parent->parse( wfMessage( 'config-sidebar' )->plain(), true );
297 </div></div>
298 </div>
300 <?php
301 echo Html::closeElement( 'body' ) . Html::closeElement( 'html' );
304 public function outputShortHeader() {
306 <?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
307 <head>
308 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
309 <meta name="robots" content="noindex, nofollow" />
310 <title><?php $this->outputTitle(); ?></title>
311 <?php echo $this->getCssUrl() . "\n"; ?>
312 <?php echo $this->getJQuery(); ?>
313 <?php echo Html::linkedScript( '../skins/common/config.js' ); ?>
314 </head>
316 <body style="background-image: none">
317 <?php
320 public function outputTitle() {
321 global $wgVersion;
322 echo wfMessage( 'config-title', $wgVersion )->escaped();
326 * @return string
328 public function getJQuery() {
329 return Html::linkedScript( "../resources/lib/jquery/jquery.js" );