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