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