SkinFallbackTemplate.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * Skin template for the fallback skin.
  4. *
  5. * The structure is copied from the example skin (mediawiki/skins/Example).
  6. *
  7. * @since 1.24
  8. * @file
  9. */
  10. use MediaWiki\MediaWikiServices;
  11. /**
  12. * BaseTemplate class for the fallback skin
  13. */
  14. class SkinFallbackTemplate extends BaseTemplate {
  15. /**
  16. * @return array
  17. */
  18. private function findInstalledSkins() {
  19. $styleDirectory = $this->config->get( 'StyleDirectory' );
  20. // Get all subdirectories which might contains skins
  21. $possibleSkins = scandir( $styleDirectory );
  22. $possibleSkins = array_filter( $possibleSkins, function ( $maybeDir ) use ( $styleDirectory ) {
  23. return $maybeDir !== '.' && $maybeDir !== '..' && is_dir( "$styleDirectory/$maybeDir" );
  24. } );
  25. // Filter out skins that aren't installed
  26. $possibleSkins = array_filter( $possibleSkins, function ( $skinDir ) use ( $styleDirectory ) {
  27. return is_file( "$styleDirectory/$skinDir/skin.json" )
  28. || is_file( "$styleDirectory/$skinDir/$skinDir.php" );
  29. } );
  30. return $possibleSkins;
  31. }
  32. /**
  33. * Inform the user why they are seeing this skin.
  34. *
  35. * @return string
  36. */
  37. private function buildHelpfulInformationMessage() {
  38. $defaultSkin = $this->config->get( 'DefaultSkin' );
  39. $installedSkins = $this->findInstalledSkins();
  40. $skinFactory = MediaWikiServices::getInstance()->getSkinFactory();
  41. $enabledSkins = $skinFactory->getSkinNames();
  42. $enabledSkins = array_change_key_case( $enabledSkins, CASE_LOWER );
  43. if ( $installedSkins ) {
  44. $skinsInstalledText = [];
  45. $skinsInstalledSnippet = [];
  46. foreach ( $installedSkins as $skin ) {
  47. $normalizedKey = strtolower( $skin );
  48. $isEnabled = array_key_exists( $normalizedKey, $enabledSkins );
  49. if ( $isEnabled ) {
  50. $skinsInstalledText[] = $this->getMsg( 'default-skin-not-found-row-enabled' )
  51. ->params( $normalizedKey, $skin )->plain();
  52. } else {
  53. $skinsInstalledText[] = $this->getMsg( 'default-skin-not-found-row-disabled' )
  54. ->params( $normalizedKey, $skin )->plain();
  55. $skinsInstalledSnippet[] = $this->getSnippetForSkin( $skin );
  56. }
  57. }
  58. return $this->getMsg( 'default-skin-not-found' )->params(
  59. $defaultSkin,
  60. implode( "\n", $skinsInstalledText ),
  61. implode( "\n", $skinsInstalledSnippet ) )->numParams(
  62. count( $skinsInstalledText ),
  63. count( $skinsInstalledSnippet )
  64. )->parseAsBlock();
  65. } else {
  66. return $this->getMsg( 'default-skin-not-found-no-skins' )->params(
  67. $defaultSkin
  68. )->parseAsBlock();
  69. }
  70. }
  71. /**
  72. * Get the appropriate LocalSettings.php snippet to enable the given skin
  73. *
  74. * @param string $skin
  75. * @return string
  76. */
  77. private function getSnippetForSkin( $skin ) {
  78. global $IP;
  79. if ( file_exists( "$IP/skins/$skin/skin.json" ) ) {
  80. return "wfLoadSkin( '$skin' );";
  81. } else {
  82. return "require_once \"\$IP/skins/$skin/$skin.php\";";
  83. }
  84. }
  85. /**
  86. * Outputs the entire contents of the page. No navigation (other than search box), just the big
  87. * warning message and page content.
  88. */
  89. public function execute() {
  90. $this->html( 'headelement' );
  91. echo Html::warningBox( $this->buildHelpfulInformationMessage() );
  92. ?>
  93. <form action="<?php $this->text( 'wgScript' ) ?>">
  94. <input type="hidden" name="title" value="<?php $this->text( 'searchtitle' ) ?>" />
  95. <h3><label for="searchInput"><?php $this->msg( 'search' ) ?></label></h3>
  96. <?php echo $this->makeSearchInput( [ "id" => "searchInput" ] ) ?>
  97. <?php echo $this->makeSearchButton( 'go' ) ?>
  98. </form>
  99. <div class="mw-body" role="main">
  100. <h1 class="firstHeading"><?php $this->html( 'title' ) ?></h1>
  101. <div class="mw-body-content">
  102. <?php $this->html( 'bodytext' ) ?>
  103. <?php $this->html( 'catlinks' ) ?>
  104. </div>
  105. </div>
  106. <?php $this->printTrail() ?>
  107. </body></html>
  108. <?php
  109. }
  110. }