MediaWiki  master
WebInstallerOutput.php
Go to the documentation of this file.
1 <?php
25 
39 
45  public $parent;
46 
51  private $contents = '';
52 
57  private $headerDone = false;
58 
63 
70  public $allowFrames = false;
71 
76  private $useShortHeader = false;
77 
81  public function __construct( WebInstaller $parent ) {
82  $this->parent = $parent;
83  }
84 
88  public function addHTML( $html ) {
89  $this->contents .= $html;
90  $this->flush();
91  }
92 
97  public function addWikiTextAsInterface( $text ) {
98  $this->addHTML( $this->parent->parse( $text ) );
99  }
100 
104  public function addHTMLNoFlush( $html ) {
105  $this->contents .= $html;
106  }
107 
113  public function redirect( $url ) {
114  if ( $this->headerDone ) {
115  throw new MWException( __METHOD__ . ' called after sending headers' );
116  }
117  $this->redirectTarget = $url;
118  }
119 
120  public function output() {
121  $this->flush();
122 
123  if ( !$this->redirectTarget ) {
124  $this->outputFooter();
125  }
126  }
127 
133  public function getCSS() {
134  global $wgStyleDirectory;
135 
136  $moduleNames = [
137  // Based on Skin::getDefaultModules
138  'mediawiki.legacy.shared',
139  // Based on Vector::setupSkinUserCss
140  'mediawiki.skinning.interface',
141  ];
142 
143  $resourceLoader = MediaWikiServices::getInstance()->getResourceLoader();
144 
145  if ( file_exists( "$wgStyleDirectory/Vector/skin.json" ) ) {
146  // Force loading Vector skin if available as a fallback skin
147  // for whatever ResourceLoader wants to have as the default.
148  $registry = new ExtensionRegistry();
149  $data = $registry->readFromQueue( [
150  "$wgStyleDirectory/Vector/skin.json" => 1,
151  ] );
152  if ( isset( $data['globals']['wgResourceModules'] ) ) {
153  $resourceLoader->register( $data['globals']['wgResourceModules'] );
154  }
155 
156  $moduleNames[] = 'skins.vector.styles';
157  }
158 
159  $moduleNames[] = 'mediawiki.legacy.config';
160 
161  $rlContext = new ResourceLoaderContext( $resourceLoader, new FauxRequest( [
162  'debug' => 'true',
163  'lang' => $this->getLanguage()->getCode(),
164  'only' => 'styles',
165  ] ) );
166 
167  $styles = [];
168  foreach ( $moduleNames as $moduleName ) {
170  $module = $resourceLoader->getModule( $moduleName );
171  '@phan-var ResourceLoaderFileModule $module';
172  if ( !$module ) {
173  // T98043: Don't fatal, but it won't look as pretty.
174  continue;
175  }
176 
177  // Based on: ResourceLoaderFileModule::getStyles (without the DB query)
178  $styles = array_merge( $styles, ResourceLoader::makeCombinedStyles(
179  $module->readStyleFiles(
180  $module->getStyleFiles( $rlContext ),
181  $module->getFlip( $rlContext ),
182  $rlContext
183  ) ) );
184  }
185 
186  return implode( "\n", $styles );
187  }
188 
194  private function getCssUrl() {
195  return Html::linkedStyle( $this->parent->getUrl( [ 'css' => 1 ] ) );
196  }
197 
198  public function useShortHeader( $use = true ) {
199  $this->useShortHeader = $use;
200  }
201 
202  public function allowFrames( $allow = true ) {
203  $this->allowFrames = $allow;
204  }
205 
206  public function flush() {
207  if ( !$this->headerDone ) {
208  $this->outputHeader();
209  }
210  if ( !$this->redirectTarget && strlen( $this->contents ) ) {
211  echo $this->contents;
212  flush();
213  $this->contents = '';
214  }
215  }
216 
221  private function getLanguage() {
222  global $wgLang;
223 
224  return is_object( $wgLang ) ? $wgLang
225  : MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( 'en' );
226  }
227 
231  public function getHeadAttribs() {
232  return [
233  'dir' => $this->getLanguage()->getDir(),
234  'lang' => $this->getLanguage()->getHtmlCode(),
235  ];
236  }
237 
243  public function headerDone() {
244  return $this->headerDone;
245  }
246 
247  public function outputHeader() {
248  $this->headerDone = true;
249  $this->parent->request->response()->header( 'Content-Type: text/html; charset=utf-8' );
250 
251  if ( !$this->allowFrames ) {
252  $this->parent->request->response()->header( 'X-Frame-Options: DENY' );
253  }
254 
255  if ( $this->redirectTarget ) {
256  $this->parent->request->response()->header( 'Location: ' . $this->redirectTarget );
257 
258  return;
259  }
260 
261  if ( $this->useShortHeader ) {
262  $this->outputShortHeader();
263 
264  return;
265  }
266 ?>
267 <?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
268 
269 <head>
270  <meta name="robots" content="noindex, nofollow" />
271  <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
272  <title><?php $this->outputTitle(); ?></title>
273  <?php echo $this->getCssUrl() . "\n"; ?>
274  <?php echo $this->getJQuery() . "\n"; ?>
275  <?php echo Html::linkedScript( 'config.js' ) . "\n"; ?>
276 </head>
277 
278 <?php echo Html::openElement( 'body', [ 'class' => $this->getLanguage()->getDir() ] ) . "\n"; ?>
279 <div id="mw-page-base"></div>
280 <div id="mw-head-base"></div>
281 <div id="content" class="mw-body" role="main">
282 <div id="bodyContent" class="mw-body-content">
283 
284 <h1><?php $this->outputTitle(); ?></h1>
285 <?php
286  }
287 
288  public function outputFooter() {
289  if ( $this->useShortHeader ) {
290  echo Html::closeElement( 'body' ) . Html::closeElement( 'html' );
291 
292  return;
293  }
294 ?>
295 
296 </div></div>
297 
298 <div id="mw-panel">
299  <div class="portal" id="p-logo">
300  <a href="https://www.mediawiki.org/" title="Main Page"></a>
301  </div>
302 <?php
303  $message = wfMessage( 'config-sidebar' )->plain();
304  // Section 1: External links
305  // @todo FIXME: Migrate to plain link label messages (T227297).
306  foreach ( explode( '----', $message ) as $section ) {
307  echo '<div class="portal"><div class="body">';
308  echo $this->parent->parse( $section, true );
309  echo '</div></div>';
310  }
311  // Section 2: Installer pages
312  echo '<div class="portal"><div class="body"><ul>';
313  foreach ( [
314  'config-sidebar-readme' => 'Readme',
315  'config-sidebar-relnotes' => 'ReleaseNotes',
316  'config-sidebar-license' => 'Copying',
317  'config-sidebar-upgrade' => 'UpgradeDoc',
318  ] as $msgKey => $pageName ) {
319  echo $this->parent->makeLinkItem(
320  $this->parent->getDocUrl( $pageName ),
321  wfMessage( $msgKey )->text()
322  );
323  }
324  echo '</ul></div></div>';
325 ?>
326 </div>
327 
328 <?php
329  echo Html::closeElement( 'body' ) . Html::closeElement( 'html' );
330  }
331 
332  public function outputShortHeader() {
333 ?>
334 <?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
335 
336 <head>
337  <meta name="robots" content="noindex, nofollow" />
338  <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
339  <title><?php $this->outputTitle(); ?></title>
340  <?php echo $this->getCssUrl() . "\n"; ?>
341  <?php echo $this->getJQuery() . "\n"; ?>
342  <?php echo Html::linkedScript( 'config.js' ) . "\n"; ?>
343 </head>
344 
345 <body style="background-image: none">
346 <?php
347  }
348 
349  public function outputTitle() {
350  global $wgVersion;
351  echo wfMessage( 'config-title', $wgVersion )->escaped();
352  }
353 
357  public function getJQuery() {
358  return Html::linkedScript( "../resources/lib/jquery/jquery.js" );
359  }
360 
361 }
WebInstallerOutput\addHTMLNoFlush
addHTMLNoFlush( $html)
Definition: WebInstallerOutput.php:104
ResourceLoaderContext
Context object that contains information about the state of a specific ResourceLoader web request.
Definition: ResourceLoaderContext.php:33
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: FauxRequest.php:33
WebInstallerOutput\redirect
redirect( $url)
Definition: WebInstallerOutput.php:113
WebInstallerOutput\$useShortHeader
bool $useShortHeader
Whether to use the limited header (used during CC license callbacks)
Definition: WebInstallerOutput.php:76
WebInstallerOutput\$headerDone
bool $headerDone
Has the header (or short header) been output?
Definition: WebInstallerOutput.php:57
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:129
WebInstallerOutput\getCssUrl
getCssUrl()
"<link>" to index.php?css=1 for the "<head>"
Definition: WebInstallerOutput.php:194
ExtensionRegistry
ExtensionRegistry class.
Definition: ExtensionRegistry.php:18
$wgVersion
$wgVersion
MediaWiki version number.
Definition: DefaultSettings.php:75
$resourceLoader
$resourceLoader
Definition: load.php:39
WebInstaller
Class for the core installer web interface.
Definition: WebInstaller.php:32
WebInstallerOutput\outputHeader
outputHeader()
Definition: WebInstallerOutput.php:247
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1263
WebInstallerOutput\outputFooter
outputFooter()
Definition: WebInstallerOutput.php:288
WebInstallerOutput\getCSS
getCSS()
Get the stylesheet of the MediaWiki skin.
Definition: WebInstallerOutput.php:133
WebInstallerOutput\__construct
__construct(WebInstaller $parent)
Definition: WebInstallerOutput.php:81
WebInstallerOutput
Output class modelled on OutputPage.
Definition: WebInstallerOutput.php:38
WebInstallerOutput\getJQuery
getJQuery()
Definition: WebInstallerOutput.php:357
WebInstallerOutput\headerDone
headerDone()
Get whether the header has been output.
Definition: WebInstallerOutput.php:243
WebInstallerOutput\$contents
string $contents
Buffered contents that haven't been output yet.
Definition: WebInstallerOutput.php:51
MWException
MediaWiki exception.
Definition: MWException.php:26
WebInstallerOutput\$redirectTarget
string $redirectTarget
Definition: WebInstallerOutput.php:62
WebInstallerOutput\$parent
WebInstaller $parent
The WebInstaller object this WebInstallerOutput is used by.
Definition: WebInstallerOutput.php:45
$wgLang
$wgLang
Definition: Setup.php:858
WebInstallerOutput\allowFrames
allowFrames( $allow=true)
Definition: WebInstallerOutput.php:202
WebInstallerOutput\outputShortHeader
outputShortHeader()
Definition: WebInstallerOutput.php:332
WebInstallerOutput\useShortHeader
useShortHeader( $use=true)
Definition: WebInstallerOutput.php:198
WebInstallerOutput\addWikiTextAsInterface
addWikiTextAsInterface( $text)
Definition: WebInstallerOutput.php:97
WebInstallerOutput\getHeadAttribs
getHeadAttribs()
Definition: WebInstallerOutput.php:231
WebInstallerOutput\outputTitle
outputTitle()
Definition: WebInstallerOutput.php:349
WebInstallerOutput\$allowFrames
bool $allowFrames
Does the current page need to allow being used as a frame? If not, X-Frame-Options will be output to ...
Definition: WebInstallerOutput.php:70
WebInstallerOutput\getLanguage
getLanguage()
Definition: WebInstallerOutput.php:221
WebInstallerOutput\output
output()
Definition: WebInstallerOutput.php:120
WebInstallerOutput\flush
flush()
Definition: WebInstallerOutput.php:206
WebInstallerOutput\addHTML
addHTML( $html)
Definition: WebInstallerOutput.php:88
$wgStyleDirectory
$wgStyleDirectory
Filesystem stylesheets directory.
Definition: DefaultSettings.php:236