6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
25 * Factory class to create Skin objects
32 * Map of name => callback
35 private $factoryFunctions = array();
37 * Map of name => fallback human-readable name, used when the 'skinname-<skin>' message is not
42 private $displayNames = array();
44 * Map of name => class name without "Skin" prefix, for legacy skins using the autodiscovery
49 private $legacySkins = array();
56 public static function getDefaultInstance() {
58 self
::$self = new self
;
65 * Register a new Skin factory function.
67 * Will override if it's already registered.
69 * @param string $name Internal skin name. Should be all-lowercase (technically doesn't have
70 * to be, but doing so would change the case of i18n message keys).
71 * @param string $displayName For backwards-compatibility with old skin loading system. This is
72 * the text used as skin's human-readable name when the 'skinname-<skin>' message is not
73 * available. It should be the same as the skin name provided in $wgExtensionCredits.
74 * @param callable $callback Callback that takes the skin name as an argument
75 * @throws InvalidArgumentException If an invalid callback is provided
77 public function register( $name, $displayName, $callback ) {
78 if ( !is_callable( $callback ) ) {
79 throw new InvalidArgumentException( 'Invalid callback provided' );
81 $this->factoryFunctions
[$name] = $callback;
82 $this->displayNames
[$name] = $displayName;
88 private function getLegacySkinNames() {
89 static $skinsInitialised = false;
91 if ( !$skinsInitialised ||
!count( $this->legacySkins
) ) {
92 # Get a list of available skins
93 # Build using the regular expression '^(.*).php$'
94 # Array keys are all lower case, array value keep the case used by filename
96 wfProfileIn( __METHOD__
. '-init' );
98 global $wgStyleDirectory;
100 $skinDir = dir( $wgStyleDirectory );
102 if ( $skinDir !== false && $skinDir !== null ) {
103 # while code from www.php.net
104 while ( false !== ( $file = $skinDir->read() ) ) {
105 // Skip non-PHP files, hidden files, and '.dep' includes
108 if ( preg_match( '/^([^.]*)\.php$/', $file, $matches ) ) {
109 $aSkin = $matches[1];
111 // Explicitly disallow loading core skins via the autodiscovery mechanism.
113 // They should be loaded already (in a non-autodicovery way), but old files might still
114 // exist on the server because our MW version upgrade process is widely documented as
115 // requiring just copying over all files, without removing old ones.
117 // This is one of the reasons we should have never used autodiscovery in the first
118 // place. This hack can be safely removed when autodiscovery is gone.
119 if ( in_array( $aSkin, array( 'CologneBlue', 'Modern', 'MonoBook', 'Vector' ) ) ) {
121 "An old copy of the $aSkin skin was found in your skins/ directory. " .
122 "You should remove it to avoid problems in the future." .
123 "See https://www.mediawiki.org/wiki/Manual:Skin_autodiscovery for details."
129 "A skin using autodiscovery mechanism, $aSkin, was found in your skins/ directory. " .
130 "The mechanism will be removed in MediaWiki 1.25 and the skin will no longer be recognized. " .
131 "See https://www.mediawiki.org/wiki/Manual:Skin_autodiscovery for information how to fix this."
133 $this->legacySkins
[strtolower( $aSkin )] = $aSkin;
138 $skinsInitialised = true;
139 wfProfileOut( __METHOD__
. '-init' );
141 return $this->legacySkins
;
146 * Returns an associative array of:
147 * skin name => human readable name
151 public function getSkinNames() {
153 $this->getLegacySkinNames(),
159 * Get a legacy skin which uses the autodiscovery mechanism.
161 * @param string $name
162 * @return Skin|bool False if the skin couldn't be constructed
164 private function getLegacySkin( $name ) {
165 $skinNames = $this->getLegacySkinNames();
166 if ( !isset( $skinNames[$name] ) ) {
169 $skinName = $skinNames[$name];
170 $className = "Skin{$skinName}";
172 # Grab the skin class and initialise it.
173 if ( !class_exists( $className ) ) {
174 global $wgStyleDirectory;
175 require_once "{$wgStyleDirectory}/{$skinName}.php";
178 if ( !class_exists( $className ) ) {
179 # DO NOT die if the class isn't found. This breaks maintenance
180 # scripts and can cause a user account to be unrecoverable
181 # except by SQL manipulation if a previously valid skin name
182 # is no longer valid.
186 $skin = new $className( $name );
192 * Create a given Skin using the registered callback for $name.
193 * @param string $name Name of the skin you want
194 * @throws SkinException If a factory function isn't registered for $name
195 * @throws UnexpectedValueException If the factory function returns a non-Skin object
198 public function makeSkin( $name ) {
199 if ( !isset( $this->factoryFunctions
[$name] ) ) {
200 // Check the legacy autodiscovery method of skin loading
201 $legacy = $this->getLegacySkin( $name );
205 throw new SkinException( "No registered builder available for $name." );
207 $skin = call_user_func( $this->factoryFunctions
[$name], $name );
208 if ( $skin instanceof Skin
) {
211 throw new UnexpectedValueException( "The builder for $name returned a non-Skin object." );