MediaWiki  master
AutoLoader.php
Go to the documentation of this file.
1 <?php
23 // NO_AUTOLOAD -- file scope code, can't load self
24 
30 require_once __DIR__ . '/../autoload.php';
31 
32 class AutoLoader {
33  protected static $autoloadLocalClassesLower = null;
34 
39  public static $psr4Namespaces = [];
40 
46  public static function autoload( $className ) {
49 
50  $filename = false;
51 
52  if ( isset( $wgAutoloadLocalClasses[$className] ) ) {
53  $filename = $wgAutoloadLocalClasses[$className];
54  } elseif ( isset( $wgAutoloadClasses[$className] ) ) {
55  $filename = $wgAutoloadClasses[$className];
56  } elseif ( $wgAutoloadAttemptLowercase ) {
57  /*
58  * Try a different capitalisation.
59  *
60  * PHP 4 objects are always serialized with the classname coerced to lowercase,
61  * and we are plagued with several legacy uses created by MediaWiki < 1.5, see
62  * https://wikitech.wikimedia.org/wiki/Text_storage_data
63  */
64  $lowerClass = strtolower( $className );
65 
66  if ( self::$autoloadLocalClassesLower === null ) {
67  self::$autoloadLocalClassesLower = array_change_key_case( $wgAutoloadLocalClasses, CASE_LOWER );
68  }
69 
70  if ( isset( self::$autoloadLocalClassesLower[$lowerClass] ) ) {
71  if ( function_exists( 'wfDebugLog' ) ) {
72  wfDebugLog( 'autoloader', "Class {$className} was loaded using incorrect case" );
73  }
74  $filename = self::$autoloadLocalClassesLower[$lowerClass];
75  }
76  }
77 
78  if ( !$filename && strpos( $className, '\\' ) !== false ) {
79  // This class is namespaced, so try looking at the namespace map
80  $prefix = $className;
81  while ( ( $pos = strrpos( $prefix, '\\' ) ) !== false ) {
82  // Check to see if this namespace prefix is in the map
83  $prefix = substr( $className, 0, $pos + 1 );
84  if ( isset( self::$psr4Namespaces[$prefix] ) ) {
85  $relativeClass = substr( $className, $pos + 1 );
86  // Build the expected filename, and see if it exists
87  $file = self::$psr4Namespaces[$prefix] . '/' .
88  str_replace( '\\', '/', $relativeClass ) . '.php';
89  if ( file_exists( $file ) ) {
90  $filename = $file;
91  break;
92  }
93  }
94 
95  // Remove trailing separator for next iteration
96  $prefix = rtrim( $prefix, '\\' );
97  }
98  }
99 
100  if ( !$filename ) {
101  // Class not found; let the next autoloader try to find it
102  return;
103  }
104 
105  // Make an absolute path, this improves performance by avoiding some stat calls
106  if ( substr( $filename, 0, 1 ) != '/' && substr( $filename, 1, 1 ) != ':' ) {
107  global $IP;
108  $filename = "$IP/$filename";
109  }
110 
111  require $filename;
112  }
113 
118  public static function resetAutoloadLocalClassesLower() {
119  self::$autoloadLocalClassesLower = null;
120  }
121 
132  public static function getAutoloadNamespaces() {
133  return [
134  'MediaWiki\\' => __DIR__ . '/',
135  'MediaWiki\\Api\\' => __DIR__ . '/api/',
136  'MediaWiki\\Auth\\' => __DIR__ . '/auth/',
137  'MediaWiki\\Block\\' => __DIR__ . '/block/',
138  'MediaWiki\\Cache\\' => __DIR__ . '/cache/',
139  'MediaWiki\\ChangeTags\\' => __DIR__ . '/changetags/',
140  'MediaWiki\\Config\\' => __DIR__ . '/config/',
141  'MediaWiki\\Content\\' => __DIR__ . '/content/',
142  'MediaWiki\\DB\\' => __DIR__ . '/db/',
143  'MediaWiki\\Diff\\' => __DIR__ . '/diff/',
144  'MediaWiki\\Edit\\' => __DIR__ . '/edit/',
145  'MediaWiki\\EditPage\\' => __DIR__ . '/editpage/',
146  'MediaWiki\\FileBackend\\LockManager\\' => __DIR__ . '/filebackend/lockmanager/',
147  'MediaWiki\\Json\\' => __DIR__ . '/json/',
148  'MediaWiki\\Http\\' => __DIR__ . '/http/',
149  'MediaWiki\\Installer\\' => __DIR__ . '/installer/',
150  'MediaWiki\\Interwiki\\' => __DIR__ . '/interwiki/',
151  'MediaWiki\\Linker\\' => __DIR__ . '/linker/',
152  'MediaWiki\\Logger\\' => __DIR__ . '/debug/logger/',
153  'MediaWiki\\Logger\Monolog\\' => __DIR__ . '/debug/logger/monolog/',
154  'MediaWiki\\Page\\' => __DIR__ . '/page/',
155  'MediaWiki\\Preferences\\' => __DIR__ . '/preferences/',
156  'MediaWiki\\ResourceLoader\\' => __DIR__ . '/resourceloader/',
157  'MediaWiki\\Search\\' => __DIR__ . '/search/',
158  'MediaWiki\\Search\\SearchWidgets\\' => __DIR__ . '/search/searchwidgets/',
159  'MediaWiki\\Session\\' => __DIR__ . '/session/',
160  'MediaWiki\\Shell\\' => __DIR__ . '/shell/',
161  'MediaWiki\\Site\\' => __DIR__ . '/site/',
162  'MediaWiki\\Sparql\\' => __DIR__ . '/sparql/',
163  'MediaWiki\\SpecialPage\\' => __DIR__ . '/specialpage/',
164  'MediaWiki\\Tidy\\' => __DIR__ . '/tidy/',
165  'MediaWiki\\User\\' => __DIR__ . '/user/',
166  'MediaWiki\\Widget\\' => __DIR__ . '/widget/',
167  'Wikimedia\\' => __DIR__ . '/libs/',
168  'Wikimedia\\Http\\' => __DIR__ . '/libs/http/',
169  'Wikimedia\\UUID\\' => __DIR__ . '/libs/uuid/',
170  ];
171  }
172 }
173 
175 spl_autoload_register( [ 'AutoLoader', 'autoload' ] );
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
wfDebugLog
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
Definition: GlobalFunctions.php:995
AutoLoader
Locations of core classes Extension classes are specified with $wgAutoloadClasses This array is a glo...
Definition: AutoLoader.php:32
AutoLoader\resetAutoloadLocalClassesLower
static resetAutoloadLocalClassesLower()
Method to clear the protected class property $autoloadLocalClassesLower.
Definition: AutoLoader.php:118
AutoLoader\getAutoloadNamespaces
static getAutoloadNamespaces()
Get a mapping of namespace => file path The namespaces should follow the PSR-4 standard for autoloadi...
Definition: AutoLoader.php:132
AutoLoader\$psr4Namespaces
static string[] $psr4Namespaces
Definition: AutoLoader.php:39
$wgAutoloadLocalClasses
global $wgAutoloadLocalClasses
Definition: autoload.php:4
AutoLoader\$autoloadLocalClassesLower
static $autoloadLocalClassesLower
Definition: AutoLoader.php:33
AutoLoader\autoload
static autoload( $className)
autoload - take a class name and attempt to load it
Definition: AutoLoader.php:46
$wgAutoloadClasses
$wgAutoloadClasses
Array mapping class names to filenames, for autoloading.
Definition: DefaultSettings.php:7831
$wgAutoloadAttemptLowercase
$wgAutoloadAttemptLowercase
Switch controlling legacy case-insensitive classloading.
Definition: DefaultSettings.php:7840
$IP
$IP
Definition: WebStart.php:49