MediaWiki  1.34.0
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 : Language::factory( 'en' );
225  }
226 
230  public function getHeadAttribs() {
231  return [
232  'dir' => $this->getLanguage()->getDir(),
233  'lang' => $this->getLanguage()->getHtmlCode(),
234  ];
235  }
236 
242  public function headerDone() {
243  return $this->headerDone;
244  }
245 
246  public function outputHeader() {
247  $this->headerDone = true;
248  $this->parent->request->response()->header( 'Content-Type: text/html; charset=utf-8' );
249 
250  if ( !$this->allowFrames ) {
251  $this->parent->request->response()->header( 'X-Frame-Options: DENY' );
252  }
253 
254  if ( $this->redirectTarget ) {
255  $this->parent->request->response()->header( 'Location: ' . $this->redirectTarget );
256 
257  return;
258  }
259 
260  if ( $this->useShortHeader ) {
261  $this->outputShortHeader();
262 
263  return;
264  }
265 ?>
266 <?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
267 
268 <head>
269  <meta name="robots" content="noindex, nofollow" />
270  <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
271  <title><?php $this->outputTitle(); ?></title>
272  <?php echo $this->getCssUrl() . "\n"; ?>
273  <?php echo $this->getJQuery() . "\n"; ?>
274  <?php echo Html::linkedScript( 'config.js' ) . "\n"; ?>
275 </head>
276 
277 <?php echo Html::openElement( 'body', [ 'class' => $this->getLanguage()->getDir() ] ) . "\n"; ?>
278 <div id="mw-page-base"></div>
279 <div id="mw-head-base"></div>
280 <div id="content" class="mw-body" role="main">
281 <div id="bodyContent" class="mw-body-content">
282 
283 <h1><?php $this->outputTitle(); ?></h1>
284 <?php
285  }
286 
287  public function outputFooter() {
288  if ( $this->useShortHeader ) {
289  echo Html::closeElement( 'body' ) . Html::closeElement( 'html' );
290 
291  return;
292  }
293 ?>
294 
295 </div></div>
296 
297 <div id="mw-panel">
298  <div class="portal" id="p-logo">
299  <a href="https://www.mediawiki.org/" title="Main Page"></a>
300  </div>
301 <?php
302  $message = wfMessage( 'config-sidebar' )->plain();
303  // Section 1: External links
304  // @todo FIXME: Migrate to plain link label messages (T227297).
305  foreach ( explode( '----', $message ) as $section ) {
306  echo '<div class="portal"><div class="body">';
307  echo $this->parent->parse( $section, true );
308  echo '</div></div>';
309  }
310  // Section 2: Installer pages
311  echo '<div class="portal"><div class="body"><ul>';
312  foreach ( [
313  'config-sidebar-readme' => 'Readme',
314  'config-sidebar-relnotes' => 'ReleaseNotes',
315  'config-sidebar-license' => 'Copying',
316  'config-sidebar-upgrade' => 'UpgradeDoc',
317  ] as $msgKey => $pageName ) {
318  echo $this->parent->makeLinkItem(
319  $this->parent->getDocUrl( $pageName ),
320  wfMessage( $msgKey )->text()
321  );
322  }
323  echo '</ul></div></div>';
324 ?>
325 </div>
326 
327 <?php
328  echo Html::closeElement( 'body' ) . Html::closeElement( 'html' );
329  }
330 
331  public function outputShortHeader() {
332 ?>
333 <?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
334 
335 <head>
336  <meta name="robots" content="noindex, nofollow" />
337  <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
338  <title><?php $this->outputTitle(); ?></title>
339  <?php echo $this->getCssUrl() . "\n"; ?>
340  <?php echo $this->getJQuery() . "\n"; ?>
341  <?php echo Html::linkedScript( 'config.js' ) . "\n"; ?>
342 </head>
343 
344 <body style="background-image: none">
345 <?php
346  }
347 
348  public function outputTitle() {
349  global $wgVersion;
350  echo wfMessage( 'config-title', $wgVersion )->escaped();
351  }
352 
356  public function getJQuery() {
357  return Html::linkedScript( "../resources/lib/jquery/jquery.js" );
358  }
359 
360 }
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:117
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:44
WebInstaller
Class for the core installer web interface.
Definition: WebInstaller.php:32
WebInstallerOutput\outputHeader
outputHeader()
Definition: WebInstallerOutput.php:246
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1264
WebInstallerOutput\outputFooter
outputFooter()
Definition: WebInstallerOutput.php:287
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:356
WebInstallerOutput\headerDone
headerDone()
Get whether the header has been output.
Definition: WebInstallerOutput.php:242
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:881
WebInstallerOutput\allowFrames
allowFrames( $allow=true)
Definition: WebInstallerOutput.php:202
WebInstallerOutput\outputShortHeader
outputShortHeader()
Definition: WebInstallerOutput.php:331
WebInstallerOutput\useShortHeader
useShortHeader( $use=true)
Definition: WebInstallerOutput.php:198
WebInstallerOutput\addWikiTextAsInterface
addWikiTextAsInterface( $text)
Definition: WebInstallerOutput.php:97
WebInstallerOutput\getHeadAttribs
getHeadAttribs()
Definition: WebInstallerOutput.php:230
WebInstallerOutput\outputTitle
outputTitle()
Definition: WebInstallerOutput.php:348
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
Language\factory
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:217
WebInstallerOutput\addHTML
addHTML( $html)
Definition: WebInstallerOutput.php:88
$wgStyleDirectory
$wgStyleDirectory
Filesystem stylesheets directory.
Definition: DefaultSettings.php:236