WebInstallerOutput.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <?php
  2. /**
  3. * Output handler for the web installer.
  4. *
  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.
  9. *
  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.
  14. *
  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
  19. *
  20. * @file
  21. * @ingroup Deployment
  22. */
  23. use MediaWiki\MediaWikiServices;
  24. /**
  25. * Output class modelled on OutputPage.
  26. *
  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.
  31. *
  32. * @ingroup Deployment
  33. * @since 1.17
  34. * @private
  35. */
  36. class WebInstallerOutput {
  37. /**
  38. * The WebInstaller object this WebInstallerOutput is used by.
  39. *
  40. * @var WebInstaller
  41. */
  42. public $parent;
  43. /**
  44. * Buffered contents that haven't been output yet
  45. * @var string
  46. */
  47. private $contents = '';
  48. /**
  49. * Has the header (or short header) been output?
  50. * @var bool
  51. */
  52. private $headerDone = false;
  53. /**
  54. * @var string
  55. */
  56. public $redirectTarget;
  57. /**
  58. * Does the current page need to allow being used as a frame?
  59. * If not, X-Frame-Options will be output to forbid it.
  60. *
  61. * @var bool
  62. */
  63. public $allowFrames = false;
  64. /**
  65. * Whether to use the limited header (used during CC license callbacks)
  66. * @var bool
  67. */
  68. private $useShortHeader = false;
  69. /**
  70. * @param WebInstaller $parent
  71. */
  72. public function __construct( WebInstaller $parent ) {
  73. $this->parent = $parent;
  74. }
  75. /**
  76. * @param string $html
  77. */
  78. public function addHTML( $html ) {
  79. $this->contents .= $html;
  80. $this->flush();
  81. }
  82. /**
  83. * @param string $text
  84. * @since 1.32
  85. */
  86. public function addWikiTextAsInterface( $text ) {
  87. $this->addHTML( $this->parent->parse( $text ) );
  88. }
  89. /**
  90. * @param string $html
  91. */
  92. public function addHTMLNoFlush( $html ) {
  93. $this->contents .= $html;
  94. }
  95. /**
  96. * @param string $url
  97. *
  98. * @throws MWException
  99. */
  100. public function redirect( $url ) {
  101. if ( $this->headerDone ) {
  102. throw new MWException( __METHOD__ . ' called after sending headers' );
  103. }
  104. $this->redirectTarget = $url;
  105. }
  106. public function output() {
  107. $this->flush();
  108. if ( !$this->redirectTarget ) {
  109. $this->outputFooter();
  110. }
  111. }
  112. /**
  113. * Get the stylesheet of the MediaWiki skin.
  114. *
  115. * @return string
  116. */
  117. public function getCSS() {
  118. global $wgStyleDirectory;
  119. $moduleNames = [
  120. // Based on Skin::getDefaultModules
  121. 'mediawiki.legacy.shared',
  122. // Based on Vector::setupSkinUserCss
  123. 'mediawiki.skinning.interface',
  124. ];
  125. $resourceLoader = MediaWikiServices::getInstance()->getResourceLoader();
  126. if ( file_exists( "$wgStyleDirectory/Vector/skin.json" ) ) {
  127. // Force loading Vector skin if available as a fallback skin
  128. // for whatever ResourceLoader wants to have as the default.
  129. $registry = new ExtensionRegistry();
  130. $data = $registry->readFromQueue( [
  131. "$wgStyleDirectory/Vector/skin.json" => 1,
  132. ] );
  133. if ( isset( $data['globals']['wgResourceModules'] ) ) {
  134. $resourceLoader->register( $data['globals']['wgResourceModules'] );
  135. }
  136. $moduleNames[] = 'skins.vector.styles';
  137. }
  138. $moduleNames[] = 'mediawiki.legacy.config';
  139. $rlContext = new ResourceLoaderContext( $resourceLoader, new FauxRequest( [
  140. 'debug' => 'true',
  141. 'lang' => $this->getLanguage()->getCode(),
  142. 'only' => 'styles',
  143. ] ) );
  144. $styles = [];
  145. foreach ( $moduleNames as $moduleName ) {
  146. /** @var ResourceLoaderFileModule $module */
  147. $module = $resourceLoader->getModule( $moduleName );
  148. '@phan-var ResourceLoaderFileModule $module';
  149. if ( !$module ) {
  150. // T98043: Don't fatal, but it won't look as pretty.
  151. continue;
  152. }
  153. // Based on: ResourceLoaderFileModule::getStyles (without the DB query)
  154. $styles = array_merge( $styles, ResourceLoader::makeCombinedStyles(
  155. $module->readStyleFiles(
  156. $module->getStyleFiles( $rlContext ),
  157. $module->getFlip( $rlContext ),
  158. $rlContext
  159. ) ) );
  160. }
  161. return implode( "\n", $styles );
  162. }
  163. /**
  164. * "<link>" to index.php?css=1 for the "<head>"
  165. *
  166. * @return string
  167. */
  168. private function getCssUrl() {
  169. return Html::linkedStyle( $this->parent->getUrl( [ 'css' => 1 ] ) );
  170. }
  171. public function useShortHeader( $use = true ) {
  172. $this->useShortHeader = $use;
  173. }
  174. public function allowFrames( $allow = true ) {
  175. $this->allowFrames = $allow;
  176. }
  177. public function flush() {
  178. if ( !$this->headerDone ) {
  179. $this->outputHeader();
  180. }
  181. if ( !$this->redirectTarget && strlen( $this->contents ) ) {
  182. echo $this->contents;
  183. flush();
  184. $this->contents = '';
  185. }
  186. }
  187. /**
  188. * @since 1.33
  189. * @return Language
  190. */
  191. private function getLanguage() {
  192. global $wgLang;
  193. return is_object( $wgLang ) ? $wgLang : Language::factory( 'en' );
  194. }
  195. /**
  196. * @return string[]
  197. */
  198. public function getHeadAttribs() {
  199. return [
  200. 'dir' => $this->getLanguage()->getDir(),
  201. 'lang' => $this->getLanguage()->getHtmlCode(),
  202. ];
  203. }
  204. /**
  205. * Get whether the header has been output
  206. *
  207. * @return bool
  208. */
  209. public function headerDone() {
  210. return $this->headerDone;
  211. }
  212. public function outputHeader() {
  213. $this->headerDone = true;
  214. $this->parent->request->response()->header( 'Content-Type: text/html; charset=utf-8' );
  215. if ( !$this->allowFrames ) {
  216. $this->parent->request->response()->header( 'X-Frame-Options: DENY' );
  217. }
  218. if ( $this->redirectTarget ) {
  219. $this->parent->request->response()->header( 'Location: ' . $this->redirectTarget );
  220. return;
  221. }
  222. if ( $this->useShortHeader ) {
  223. $this->outputShortHeader();
  224. return;
  225. }
  226. ?>
  227. <?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
  228. <head>
  229. <meta name="robots" content="noindex, nofollow" />
  230. <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  231. <title><?php $this->outputTitle(); ?></title>
  232. <?php echo $this->getCssUrl() . "\n"; ?>
  233. <?php echo $this->getJQuery() . "\n"; ?>
  234. <?php echo Html::linkedScript( 'config.js' ) . "\n"; ?>
  235. </head>
  236. <?php echo Html::openElement( 'body', [ 'class' => $this->getLanguage()->getDir() ] ) . "\n"; ?>
  237. <div id="mw-page-base"></div>
  238. <div id="mw-head-base"></div>
  239. <div id="content" class="mw-body" role="main">
  240. <div id="bodyContent" class="mw-body-content">
  241. <h1><?php $this->outputTitle(); ?></h1>
  242. <?php
  243. }
  244. public function outputFooter() {
  245. if ( $this->useShortHeader ) {
  246. echo Html::closeElement( 'body' ) . Html::closeElement( 'html' );
  247. return;
  248. }
  249. ?>
  250. </div></div>
  251. <div id="mw-panel">
  252. <div class="portal" id="p-logo">
  253. <a href="https://www.mediawiki.org/" title="Main Page"></a>
  254. </div>
  255. <?php
  256. $message = wfMessage( 'config-sidebar' )->plain();
  257. // Section 1: External links
  258. // @todo FIXME: Migrate to plain link label messages (T227297).
  259. foreach ( explode( '----', $message ) as $section ) {
  260. echo '<div class="portal"><div class="body">';
  261. echo $this->parent->parse( $section, true );
  262. echo '</div></div>';
  263. }
  264. // Section 2: Installer pages
  265. echo '<div class="portal"><div class="body"><ul>';
  266. foreach ( [
  267. 'config-sidebar-readme' => 'Readme',
  268. 'config-sidebar-relnotes' => 'ReleaseNotes',
  269. 'config-sidebar-license' => 'Copying',
  270. 'config-sidebar-upgrade' => 'UpgradeDoc',
  271. ] as $msgKey => $pageName ) {
  272. echo $this->parent->makeLinkItem(
  273. $this->parent->getDocUrl( $pageName ),
  274. wfMessage( $msgKey )->text()
  275. );
  276. }
  277. echo '</ul></div></div>';
  278. ?>
  279. </div>
  280. <?php
  281. echo Html::closeElement( 'body' ) . Html::closeElement( 'html' );
  282. }
  283. public function outputShortHeader() {
  284. ?>
  285. <?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
  286. <head>
  287. <meta name="robots" content="noindex, nofollow" />
  288. <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  289. <title><?php $this->outputTitle(); ?></title>
  290. <?php echo $this->getCssUrl() . "\n"; ?>
  291. <?php echo $this->getJQuery() . "\n"; ?>
  292. <?php echo Html::linkedScript( 'config.js' ) . "\n"; ?>
  293. </head>
  294. <body style="background-image: none">
  295. <?php
  296. }
  297. public function outputTitle() {
  298. global $wgVersion;
  299. echo wfMessage( 'config-title', $wgVersion )->escaped();
  300. }
  301. /**
  302. * @return string
  303. */
  304. public function getJQuery() {
  305. return Html::linkedScript( "../resources/lib/jquery/jquery.js" );
  306. }
  307. }