Merge "SpecialBlock: Scroll to the error's fieldset instead of field"
[mediawiki.git] / includes / skins / SkinFallback.php
blobef2910dc385980637e2f442ddb96439f3b3f3dfa
1 <?php
2 /**
3 * Skin file for the fallback skin.
5 * @since 1.24
6 * @file
7 */
9 use MediaWiki\Html\Html;
10 use MediaWiki\MainConfigNames;
11 use MediaWiki\MediaWikiServices;
12 use MediaWiki\Output\OutputPage;
14 /**
15 * SkinTemplate class for the fallback skin
17 class SkinFallback extends SkinMustache {
18 /** @inheritDoc */
19 public $skinname = 'fallback';
21 public function initPage( OutputPage $out ) {
22 parent::initPage( $out );
23 $out->disableClientCache();
26 /**
27 * @return array
29 private function findInstalledSkins() {
30 $config = $this->getConfig();
31 $styleDirectory = $config->get( MainConfigNames::StyleDirectory );
32 // Get all subdirectories which might contains skins
33 $possibleSkins = scandir( $styleDirectory );
34 $possibleSkins = array_filter( $possibleSkins, static function ( $maybeDir ) use ( $styleDirectory ) {
35 return $maybeDir !== '.' && $maybeDir !== '..' && is_dir( "$styleDirectory/$maybeDir" );
36 } );
38 // Filter out skins that aren't installed
39 $possibleSkins = array_filter( $possibleSkins, static function ( $skinDir ) use ( $styleDirectory ) {
40 return is_file( "$styleDirectory/$skinDir/skin.json" )
41 || is_file( "$styleDirectory/$skinDir/$skinDir.php" );
42 } );
44 return $possibleSkins;
47 /**
48 * Inform the user why they are seeing this skin.
50 * @return string
52 private function buildHelpfulInformationMessage() {
53 $config = $this->getConfig();
54 $defaultSkin = $config->get( MainConfigNames::DefaultSkin );
55 $installedSkins = $this->findInstalledSkins();
56 $skinFactory = MediaWikiServices::getInstance()->getSkinFactory();
57 $enabledSkins = $skinFactory->getInstalledSkins();
58 $enabledSkins = array_change_key_case( $enabledSkins, CASE_LOWER );
60 if ( $installedSkins ) {
61 $skinsInstalledText = [];
62 $skinsInstalledSnippet = [];
64 foreach ( $installedSkins as $skinKey ) {
65 $normalizedKey = strtolower( $skinKey );
66 $isEnabled = array_key_exists( $normalizedKey, $enabledSkins );
67 if ( $isEnabled ) {
68 $skinsInstalledText[] = $this->msg( 'default-skin-not-found-row-enabled' )
69 ->params( $normalizedKey, $skinKey )->plain();
70 } else {
71 $skinsInstalledText[] = $this->msg( 'default-skin-not-found-row-disabled' )
72 ->params( $normalizedKey, $skinKey )->plain();
73 $skinsInstalledSnippet[] = $this->getSnippetForSkin( $skinKey );
77 return $this->msg( 'default-skin-not-found' )->params(
78 $defaultSkin,
79 implode( "\n", $skinsInstalledText ),
80 implode( "\n", $skinsInstalledSnippet ) )->numParams(
81 count( $skinsInstalledText ),
82 count( $skinsInstalledSnippet )
83 )->parseAsBlock();
84 } else {
85 return $this->msg( 'default-skin-not-found-no-skins' )->params(
86 $defaultSkin
87 )->parseAsBlock();
91 /**
92 * Get the appropriate LocalSettings.php snippet to enable the given skin
94 * @param string $skin
95 * @return string
97 private static function getSnippetForSkin( $skin ) {
98 global $IP;
99 if ( file_exists( "$IP/skins/$skin/skin.json" ) ) {
100 return "wfLoadSkin( '$skin' );";
101 } else {
102 return "require_once \"\$IP/skins/$skin/$skin.php\";";
107 * Adds an `html-fallback-warning` template to inform system administrators
108 * that their mediawiki skin is incorrectly setup.
109 * It's recommended that skin developers do not add further to date here
110 * and instead work on improving SkinMustache::getTemplateData where necessary
111 * to improve flexibility of the data for all skin developers.
112 * @inheritDoc
113 * @return array
115 public function getTemplateData() {
116 $config = $this->getConfig();
117 $skinFactory = MediaWikiServices::getInstance()->getSkinFactory();
118 $data = parent::getTemplateData();
119 // If the default skin isn't configured correctly, append a warning to the
120 // subtitle to alert a sysadmin.
121 if ( !isset(
122 $skinFactory->getInstalledSkins()[$config->get( MainConfigNames::DefaultSkin )]
123 ) ) {
124 $data['html-fallback-warning'] = Html::warningBox( $this->buildHelpfulInformationMessage() );
126 return $data;