Debian Codegraph @

includes/installer/WebInstallerOutput.php

raw symbols SCIP
1 <?php
2
3 /**
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 *
19 * @file
20 * @ingroup Installer
21 */
22
23 namespace MediaWiki\Installer;
24
25 use LogicException;
26 use MediaWiki\Html\Html;
27 use MediaWiki\Language\Language;
28 use MediaWiki\MediaWikiServices;
29 use MediaWiki\Request\FauxRequest;
30 use MediaWiki\ResourceLoader as RL;
31 use MediaWiki\ResourceLoader\ResourceLoader;
32
33 /**
34 * Output class modelled on OutputPage.
35 *
36 * I've opted to use a distinct class rather than derive from OutputPage here in
37 * the interests of separation of concerns: if we used a subclass, there would be
38 * quite a lot of things you could do in OutputPage that would break the installer,
39 * that wouldn't be immediately obvious.
40 *
41 * @ingroup Installer
42 * @since 1.17
43 * @internal
44 */
45 class WebInstallerOutput {
46
47 /**
48 * The WebInstaller object this WebInstallerOutput is used by.
49 *
50 * @var WebInstaller
51 */
52 public $parent;
53
54 /**
55 * Buffered contents that haven't been output yet
56 * @var string
57 */
58 private $contents = '';
59
60 /**
61 * Has the header been output?
62 * @var bool
63 */
64 private $headerDone = false;
65
66 /**
67 * @var string
68 */
69 public $redirectTarget;
70
71 /**
72 * @param WebInstaller $parent
73 */
74 public function __construct( WebInstaller $parent ) {
75 $this->parent = $parent;
76 }
77
78 /**
79 * @param string $html
80 */
81 public function addHTML( $html ) {
82 $this->contents .= $html;
83 $this->flush();
84 }
85
86 /**
87 * @param string $text
88 * @since 1.32
89 */
90 public function addWikiTextAsInterface( $text ) {
91 $this->addHTML( $this->parent->parse( $text ) );
92 }
93
94 /**
95 * @param string $html
96 */
97 public function addHTMLNoFlush( $html ) {
98 $this->contents .= $html;
99 }
100
101 /**
102 * @param string $url
103 */
104 public function redirect( $url ) {
105 if ( $this->headerDone ) {
106 throw new LogicException( __METHOD__ . ' called after sending headers' );
107 }
108 $this->redirectTarget = $url;
109 }
110
111 public function output() {
112 $this->flush();
113
114 if ( !$this->redirectTarget ) {
115 $this->outputFooter();
116 }
117 }
118
119 /**
120 * Get the stylesheet of the MediaWiki skin.
121 *
122 * @return string
123 */
124 public function getCSS() {
125 $resourceLoader = MediaWikiServices::getInstance()->getResourceLoader();
126
127 $rlContext = new RL\Context( $resourceLoader, new FauxRequest( [
128 'debug' => 'true',
129 'lang' => $this->getLanguage()->getCode(),
130 'only' => 'styles',
131 ] ) );
132
133 $module = new RL\SkinModule( [
134 'features' => [
135 'elements',
136 'interface-message-box'
137 ],
138 'styles' => [
139 'mw-config/config.css',
140 ],
141 ] );
142 $module->setConfig( $resourceLoader->getConfig() );
143
144 // Based on MediaWiki\ResourceLoader\FileModule::getStyles, without the DB query
145 $styles = ResourceLoader::makeCombinedStyles(
146 $module->readStyleFiles(
147 $module->getStyleFiles( $rlContext ),
148 $rlContext
149 ) );
150
151 return implode( "\n", $styles );
152 }
153
154 /**
155 * "<link>" to index.php?css=1 for the "<head>"
156 *
157 * @return string
158 */
159 private function getCssUrl() {
160 return Html::linkedStyle( $this->parent->getUrl( [ 'css' => 1 ] ) );
161 }
162
163 public function flush() {
164 if ( !$this->headerDone ) {
165 $this->outputHeader();
166 }
167 if ( !$this->redirectTarget && strlen( $this->contents ) ) {
168 echo $this->contents;
169 flush();
170 $this->contents = '';
171 }
172 }
173
174 /**
175 * @since 1.33
176 * @return Language
177 */
178 private function getLanguage() {
179 global $wgLang;
180
181 return is_object( $wgLang ) ? $wgLang
182 : MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( 'en' );
183 }
184
185 /**
186 * @return string[]
187 */
188 public function getHeadAttribs() {
189 return [
190 'dir' => $this->getLanguage()->getDir(),
191 'lang' => $this->getLanguage()->getHtmlCode(),
192 ];
193 }
194
195 /**
196 * Get whether the header has been output
197 *
198 * @return bool
199 */
200 public function headerDone() {
201 return $this->headerDone;
202 }
203
204 public function outputHeader() {
205 $this->headerDone = true;
206 $this->parent->request->response()->header( 'Content-Type: text/html; charset=utf-8' );
207 $this->parent->request->response()->header( 'X-Frame-Options: DENY' );
208
209 if ( $this->redirectTarget ) {
210 $this->parent->request->response()->header( 'Location: ' . $this->redirectTarget );
211
212 return;
213 }
214 ?>
215 <?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
216
217 <head>
218 <meta name="robots" content="noindex, nofollow" />
219 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
220 <title><?php $this->outputTitle(); ?></title>
221 <?php echo $this->getCodex() . "\n"; ?>
222 <?php echo $this->getCssUrl() . "\n"; ?>
223 <?php echo $this->getJQuery() . "\n"; ?>
224 <?php echo Html::linkedScript( 'config.js' ) . "\n"; ?>
225 </head>
226
227 <?php echo Html::openElement( 'body', [ 'class' => $this->getLanguage()->getDir() ] ) . "\n"; ?>
228 <div id="mw-page-base"></div>
229 <div id="mw-head-base"></div>
230 <div id="content" class="mw-body" role="main">
231 <div id="bodyContent" class="mw-body-content">
232
233 <h1><?php $this->outputTitle(); ?></h1>
234 <?php
235 }
236
237 public function outputFooter() {
238 ?>
239
240 </div></div>
241
242 <aside id="mw-panel">
243 <div class="portal" id="p-logo">
244 <a href="https://www.mediawiki.org/" title="Main Page"></a>
245 </div>
246 <?php
247 // @phpcs:disable Generic.WhiteSpace.ScopeIndent.IncorrectExact
248 $message = wfMessage( 'config-sidebar' )->plain();
249 // Section 1: External links
250 // @todo FIXME: Migrate to plain link label messages (T227297).
251 foreach ( explode( '----', $message ) as $section ) {
252 echo '<div class="portal"><div class="body">';
253 echo $this->parent->parse( $section, true );
254 echo '</div></div>';
255 }
256 // Section 2: Installer pages
257 echo '<div class="portal"><div class="body"><ul>';
258 foreach ( [
259 'config-sidebar-relnotes' => 'ReleaseNotes',
260 'config-sidebar-license' => 'Copying',
261 'config-sidebar-upgrade' => 'UpgradeDoc',
262 ] as $msgKey => $pageName ) {
263 echo $this->parent->makeLinkItem(
264 $this->parent->getDocUrl( $pageName ),
265 wfMessage( $msgKey )->text()
266 );
267 }
268 echo '</ul></div></div>';
269 // @phpcs:enable
270 ?>
271 </aside>
272
273 <?php
274 echo Html::closeElement( 'body' ) . Html::closeElement( 'html' );
275 }
276
277 public function outputTitle() {
278 echo wfMessage( 'config-title', MW_VERSION )->escaped();
279 }
280
281 /**
282 * @return string
283 */
284 public function getJQuery() {
285 return Html::linkedScript( "../resources/lib/jquery/jquery.js" );
286 }
287
288 /**
289 * @return string
290 */
291 public function getCodex() {
292 return Html::linkedStyle( "../resources/lib/codex/codex.style.css" );
293 }
294
295 }