2006-06-06 09:50:01 +00:00
|
|
|
<?php
|
2010-08-08 14:23:14 +00:00
|
|
|
/**
|
|
|
|
|
* This defines autoloading handler for whole MediaWiki framework
|
|
|
|
|
*
|
2012-05-22 18:06:30 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2010-08-08 14:23:14 +00:00
|
|
|
* @file
|
|
|
|
|
*/
|
2006-07-02 15:57:59 +00:00
|
|
|
|
2010-08-08 14:23:14 +00:00
|
|
|
/**
|
|
|
|
|
* Locations of core classes
|
|
|
|
|
* Extension classes are specified with $wgAutoloadClasses
|
|
|
|
|
* This array is a global instead of a static member of AutoLoader to work around a bug in APC
|
|
|
|
|
*/
|
2014-09-26 23:13:23 +00:00
|
|
|
require_once __DIR__ . '/../autoload.php';
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2008-07-24 12:51:26 +00:00
|
|
|
class AutoLoader {
|
2019-02-07 09:21:19 +00:00
|
|
|
protected static $autoloadLocalClassesLower = null;
|
2013-10-27 05:59:01 +00:00
|
|
|
|
2017-08-24 18:05:26 +00:00
|
|
|
/**
|
|
|
|
|
* @private Only public for ExtensionRegistry
|
|
|
|
|
* @var string[] Namespace (ends with \) => Path (ends with /)
|
|
|
|
|
*/
|
2019-02-07 09:21:19 +00:00
|
|
|
public static $psr4Namespaces = [];
|
2017-08-24 18:05:26 +00:00
|
|
|
|
2008-07-02 19:41:58 +00:00
|
|
|
/**
|
|
|
|
|
* autoload - take a class name and attempt to load it
|
2008-09-14 19:51:25 +00:00
|
|
|
*
|
2014-07-24 17:42:24 +00:00
|
|
|
* @param string $className Name of class we're looking for.
|
2008-07-02 19:41:58 +00:00
|
|
|
*/
|
2008-06-16 20:21:26 +00:00
|
|
|
static function autoload( $className ) {
|
2013-10-27 05:59:01 +00:00
|
|
|
global $wgAutoloadClasses, $wgAutoloadLocalClasses,
|
|
|
|
|
$wgAutoloadAttemptLowercase;
|
2008-06-16 20:21:26 +00:00
|
|
|
|
2013-11-12 19:34:43 +00:00
|
|
|
$filename = false;
|
|
|
|
|
|
2008-07-24 12:45:23 +00:00
|
|
|
if ( isset( $wgAutoloadLocalClasses[$className] ) ) {
|
|
|
|
|
$filename = $wgAutoloadLocalClasses[$className];
|
2008-06-16 20:21:26 +00:00
|
|
|
} elseif ( isset( $wgAutoloadClasses[$className] ) ) {
|
|
|
|
|
$filename = $wgAutoloadClasses[$className];
|
2013-10-27 05:59:01 +00:00
|
|
|
} elseif ( $wgAutoloadAttemptLowercase ) {
|
|
|
|
|
/*
|
|
|
|
|
* Try a different capitalisation.
|
|
|
|
|
*
|
|
|
|
|
* PHP 4 objects are always serialized with the classname coerced to lowercase,
|
|
|
|
|
* and we are plagued with several legacy uses created by MediaWiki < 1.5, see
|
|
|
|
|
* https://wikitech.wikimedia.org/wiki/Text_storage_data
|
|
|
|
|
*/
|
2008-06-16 20:21:26 +00:00
|
|
|
$lowerClass = strtolower( $className );
|
2010-05-30 14:48:30 +00:00
|
|
|
|
2013-10-27 05:59:01 +00:00
|
|
|
if ( self::$autoloadLocalClassesLower === null ) {
|
|
|
|
|
self::$autoloadLocalClassesLower = array_change_key_case( $wgAutoloadLocalClasses, CASE_LOWER );
|
2008-06-16 20:21:26 +00:00
|
|
|
}
|
2010-05-30 14:48:30 +00:00
|
|
|
|
2013-10-27 05:59:01 +00:00
|
|
|
if ( isset( self::$autoloadLocalClassesLower[$lowerClass] ) ) {
|
2014-02-07 01:18:29 +00:00
|
|
|
if ( function_exists( 'wfDebugLog' ) ) {
|
|
|
|
|
wfDebugLog( 'autoloader', "Class {$className} was loaded using incorrect case" );
|
2010-10-21 18:26:17 +00:00
|
|
|
}
|
2013-10-27 05:59:01 +00:00
|
|
|
$filename = self::$autoloadLocalClassesLower[$lowerClass];
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-05-30 14:48:30 +00:00
|
|
|
|
2017-08-24 18:05:26 +00:00
|
|
|
if ( !$filename && strpos( $className, '\\' ) !== false ) {
|
|
|
|
|
// This class is namespaced, so try looking at the namespace map
|
|
|
|
|
$prefix = $className;
|
|
|
|
|
while ( false !== $pos = strrpos( $prefix, '\\' ) ) {
|
|
|
|
|
// Check to see if this namespace prefix is in the map
|
|
|
|
|
$prefix = substr( $className, 0, $pos + 1 );
|
|
|
|
|
if ( isset( self::$psr4Namespaces[$prefix] ) ) {
|
|
|
|
|
$relativeClass = substr( $className, $pos + 1 );
|
|
|
|
|
// Build the expected filename, and see if it exists
|
2018-05-20 14:51:34 +00:00
|
|
|
$file = self::$psr4Namespaces[$prefix] . '/' .
|
2017-08-24 18:05:26 +00:00
|
|
|
str_replace( '\\', '/', $relativeClass ) . '.php';
|
|
|
|
|
if ( file_exists( $file ) ) {
|
|
|
|
|
$filename = $file;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove trailing separator for next iteration
|
|
|
|
|
$prefix = rtrim( $prefix, '\\' );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-27 05:59:01 +00:00
|
|
|
if ( !$filename ) {
|
2014-03-21 15:07:08 +00:00
|
|
|
// Class not found; let the next autoloader try to find it
|
2014-01-04 19:25:44 +00:00
|
|
|
return;
|
2006-07-02 15:57:59 +00:00
|
|
|
}
|
2008-06-16 20:21:26 +00:00
|
|
|
|
2014-03-21 15:07:08 +00:00
|
|
|
// Make an absolute path, this improves performance by avoiding some stat calls
|
2008-06-16 20:21:26 +00:00
|
|
|
if ( substr( $filename, 0, 1 ) != '/' && substr( $filename, 1, 1 ) != ':' ) {
|
|
|
|
|
global $IP;
|
|
|
|
|
$filename = "$IP/$filename";
|
2006-07-02 15:57:59 +00:00
|
|
|
}
|
2010-05-30 14:48:30 +00:00
|
|
|
|
2013-05-07 23:00:15 +00:00
|
|
|
require $filename;
|
2006-06-06 09:50:01 +00:00
|
|
|
}
|
2006-07-01 07:22:59 +00:00
|
|
|
|
2013-11-07 16:50:23 +00:00
|
|
|
/**
|
|
|
|
|
* Method to clear the protected class property $autoloadLocalClassesLower.
|
|
|
|
|
* Used in tests.
|
|
|
|
|
*/
|
|
|
|
|
static function resetAutoloadLocalClassesLower() {
|
|
|
|
|
self::$autoloadLocalClassesLower = null;
|
|
|
|
|
}
|
2017-08-24 18:05:26 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a mapping of namespace => file path
|
|
|
|
|
* The namespaces should follow the PSR-4 standard for autoloading
|
|
|
|
|
*
|
|
|
|
|
* @see <http://www.php-fig.org/psr/psr-4/>
|
|
|
|
|
* @private Only public for usage in AutoloadGenerator
|
2018-05-27 04:29:36 +00:00
|
|
|
* @codeCoverageIgnore
|
2017-08-24 18:05:26 +00:00
|
|
|
* @since 1.31
|
|
|
|
|
* @return string[]
|
|
|
|
|
*/
|
|
|
|
|
public static function getAutoloadNamespaces() {
|
|
|
|
|
return [
|
2018-08-28 22:02:57 +00:00
|
|
|
'MediaWiki\\Auth\\' => __DIR__ . '/auth/',
|
2018-08-16 04:55:55 +00:00
|
|
|
'MediaWiki\\Block\\' => __DIR__ . '/block/',
|
2018-05-24 01:24:05 +00:00
|
|
|
'MediaWiki\\Edit\\' => __DIR__ . '/edit/',
|
|
|
|
|
'MediaWiki\\EditPage\\' => __DIR__ . '/editpage/',
|
2018-09-07 17:01:32 +00:00
|
|
|
'MediaWiki\\Linker\\' => __DIR__ . '/linker/',
|
2019-07-15 10:24:38 +00:00
|
|
|
'MediaWiki\\Message\\' => __DIR__ . '/Message',
|
2019-03-07 20:02:07 +00:00
|
|
|
'MediaWiki\\Permissions\\' => __DIR__ . '/Permissions/',
|
2018-09-07 17:01:32 +00:00
|
|
|
'MediaWiki\\Preferences\\' => __DIR__ . '/preferences/',
|
2019-05-09 01:36:18 +00:00
|
|
|
'MediaWiki\\Rest\\' => __DIR__ . '/Rest/',
|
2018-09-20 17:29:04 +00:00
|
|
|
'MediaWiki\\Revision\\' => __DIR__ . '/Revision/',
|
2018-09-07 17:01:32 +00:00
|
|
|
'MediaWiki\\Session\\' => __DIR__ . '/session/',
|
|
|
|
|
'MediaWiki\\Shell\\' => __DIR__ . '/shell/',
|
|
|
|
|
'MediaWiki\\Sparql\\' => __DIR__ . '/sparql/',
|
|
|
|
|
'MediaWiki\\Storage\\' => __DIR__ . '/Storage/',
|
|
|
|
|
'MediaWiki\\Tidy\\' => __DIR__ . '/tidy/',
|
2019-07-15 10:24:38 +00:00
|
|
|
'Wikimedia\\Message\\' => __DIR__ . '/libs/Message/',
|
2018-05-22 23:23:20 +00:00
|
|
|
'Wikimedia\\ParamValidator\\' => __DIR__ . '/libs/ParamValidator/',
|
2018-12-10 17:33:38 +00:00
|
|
|
'Wikimedia\\Services\\' => __DIR__ . '/libs/services/',
|
2017-08-24 18:05:26 +00:00
|
|
|
];
|
|
|
|
|
}
|
2006-06-06 09:50:01 +00:00
|
|
|
}
|
|
|
|
|
|
2017-12-15 10:58:11 +00:00
|
|
|
AutoLoader::$psr4Namespaces = AutoLoader::getAutoloadNamespaces();
|
2016-02-17 09:09:32 +00:00
|
|
|
spl_autoload_register( [ 'AutoLoader', 'autoload' ] );
|