MediaWiki REL1_32
FileBackendGroup.php
Go to the documentation of this file.
1<?php
26
35 protected static $instance = null;
36
38 protected $backends = [];
39
40 protected function __construct() {
41 }
42
46 public static function singleton() {
47 if ( self::$instance == null ) {
48 self::$instance = new self();
49 self::$instance->initFromGlobals();
50 }
51
52 return self::$instance;
53 }
54
58 public static function destroySingleton() {
59 self::$instance = null;
60 }
61
65 protected function initFromGlobals() {
67
68 // Register explicitly defined backends
69 $this->register( $wgFileBackends, wfConfiguredReadOnlyReason() );
70
71 $autoBackends = [];
72 // Automatically create b/c backends for file repos...
73 $repos = array_merge( $wgForeignFileRepos, [ $wgLocalFileRepo ] );
74 foreach ( $repos as $info ) {
75 $backendName = $info['backend'];
76 if ( is_object( $backendName ) || isset( $this->backends[$backendName] ) ) {
77 continue; // already defined (or set to the object for some reason)
78 }
79 $repoName = $info['name'];
80 // Local vars that used to be FSRepo members...
81 $directory = $info['directory'];
82 $deletedDir = $info['deletedDir'] ?? false; // deletion disabled
83 $thumbDir = $info['thumbDir'] ?? "{$directory}/thumb";
84 $transcodedDir = $info['transcodedDir'] ?? "{$directory}/transcoded";
85 // Get the FS backend configuration
86 $autoBackends[] = [
87 'name' => $backendName,
88 'class' => FSFileBackend::class,
89 'lockManager' => 'fsLockManager',
90 'containerPaths' => [
91 "{$repoName}-public" => "{$directory}",
92 "{$repoName}-thumb" => $thumbDir,
93 "{$repoName}-transcoded" => $transcodedDir,
94 "{$repoName}-deleted" => $deletedDir,
95 "{$repoName}-temp" => "{$directory}/temp"
96 ],
97 'fileMode' => $info['fileMode'] ?? 0644,
98 'directoryMode' => $wgDirectoryMode,
99 ];
100 }
101
102 // Register implicitly defined backends
103 $this->register( $autoBackends, wfConfiguredReadOnlyReason() );
104 }
105
113 protected function register( array $configs, $readOnlyReason = null ) {
114 foreach ( $configs as $config ) {
115 if ( !isset( $config['name'] ) ) {
116 throw new InvalidArgumentException( "Cannot register a backend with no name." );
117 }
118 $name = $config['name'];
119 if ( isset( $this->backends[$name] ) ) {
120 throw new LogicException( "Backend with name `{$name}` already registered." );
121 } elseif ( !isset( $config['class'] ) ) {
122 throw new InvalidArgumentException( "Backend with name `{$name}` has no class." );
123 }
124 $class = $config['class'];
125
126 $config['readOnly'] = $config['readOnly'] ?? $readOnlyReason;
127
128 unset( $config['class'] ); // backend won't need this
129 $this->backends[$name] = [
130 'class' => $class,
131 'config' => $config,
132 'instance' => null
133 ];
134 }
135 }
136
144 public function get( $name ) {
145 // Lazy-load the actual backend instance
146 if ( !isset( $this->backends[$name]['instance'] ) ) {
147 $config = $this->config( $name );
148
149 $class = $config['class'];
150 if ( $class === FileBackendMultiWrite::class ) {
151 foreach ( $config['backends'] as $index => $beConfig ) {
152 if ( isset( $beConfig['template'] ) ) {
153 // Config is just a modified version of a registered backend's.
154 // This should only be used when that config is used only by this backend.
155 $config['backends'][$index] += $this->config( $beConfig['template'] );
156 }
157 }
158 }
159
160 $this->backends[$name]['instance'] = new $class( $config );
161 }
162
163 return $this->backends[$name]['instance'];
164 }
165
173 public function config( $name ) {
174 if ( !isset( $this->backends[$name] ) ) {
175 throw new InvalidArgumentException( "No backend defined with the name `$name`." );
176 }
177 $class = $this->backends[$name]['class'];
178
179 $config = $this->backends[$name]['config'];
180 $config['class'] = $class;
181 $config += [ // set defaults
182 'wikiId' => wfWikiID(), // e.g. "my_wiki-en_"
183 'mimeCallback' => [ $this, 'guessMimeInternal' ],
184 'obResetFunc' => 'wfResetOutputBuffers',
185 'streamMimeFunc' => [ StreamFile::class, 'contentTypeFromPath' ],
186 'tmpDirectory' => wfTempDir(),
187 'statusWrapper' => [ Status::class, 'wrap' ],
188 'wanCache' => MediaWikiServices::getInstance()->getMainWANObjectCache(),
189 'srvCache' => ObjectCache::getLocalServerInstance( 'hash' ),
190 'logger' => LoggerFactory::getInstance( 'FileOperation' ),
191 'profiler' => Profiler::instance()
192 ];
193 $config['lockManager'] =
194 LockManagerGroup::singleton( $config['wikiId'] )->get( $config['lockManager'] );
195 $config['fileJournal'] = isset( $config['fileJournal'] )
196 ? FileJournal::factory( $config['fileJournal'], $name )
197 : FileJournal::factory( [ 'class' => NullFileJournal::class ], $name );
198
199 return $config;
200 }
201
208 public function backendFromPath( $storagePath ) {
209 list( $backend, , ) = FileBackend::splitStoragePath( $storagePath );
210 if ( $backend !== null && isset( $this->backends[$backend] ) ) {
211 return $this->get( $backend );
212 }
213
214 return null;
215 }
216
224 public function guessMimeInternal( $storagePath, $content, $fsPath ) {
225 $magic = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
226 // Trust the extension of the storage path (caller must validate)
227 $ext = FileBackend::extensionFromPath( $storagePath );
228 $type = $magic->guessTypesForExtension( $ext );
229 // For files without a valid extension (or one at all), inspect the contents
230 if ( !$type && $fsPath ) {
231 $type = $magic->guessMimeType( $fsPath, false );
232 } elseif ( !$type && strlen( $content ) ) {
233 $tmpFile = TempFSFile::factory( 'mime_', '', wfTempDir() );
234 file_put_contents( $tmpFile->getPath(), $content );
235 $type = $magic->guessMimeType( $tmpFile->getPath(), false );
236 }
237 return $type ?: 'unknown/unknown';
238 }
239}
$wgFileBackends
File backend structure configuration.
$wgDirectoryMode
Default value for chmoding of new directories.
$wgLocalFileRepo
File repository structures.
$wgForeignFileRepos
Enable the use of files from one or more other wikis.
wfConfiguredReadOnlyReason()
Get the value of $wgReadOnly or the contents of $wgReadOnlyFile.
wfTempDir()
Tries to get the system directory for temporary files.
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
Class to handle file backend registration.
static destroySingleton()
Destroy the singleton instance.
config( $name)
Get the config array for a backend object with a given name.
guessMimeInternal( $storagePath, $content, $fsPath)
initFromGlobals()
Register file backends from the global variables.
static FileBackendGroup $instance
array $backends
(name => ('class' => string, 'config' => array, 'instance' => object))
backendFromPath( $storagePath)
Get an appropriate backend object from a storage path.
static splitStoragePath( $storagePath)
Split a storage path into a backend name, a container name, and a relative file path.
static extensionFromPath( $path, $case='lowercase')
Get the final extension from a storage or FS path.
static factory(array $config, $backend)
Create an appropriate FileJournal object from config.
static singleton( $domain=false)
PSR-3 logger instance factory.
MediaWikiServices is the service locator for the application scope of MediaWiki.
static factory( $prefix, $extension='', $tmpDirectory=null)
Make a new temporary file on the file system.
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:302
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
$content
if(!is_readable( $file)) $ext
Definition router.php:55