MediaWiki  1.27.2
FileBackendGroup.php
Go to the documentation of this file.
1 <?php
33  protected static $instance = null;
34 
36  protected $backends = [];
37 
38  protected function __construct() {
39  }
40 
44  public static function singleton() {
45  if ( self::$instance == null ) {
46  self::$instance = new self();
47  self::$instance->initFromGlobals();
48  }
49 
50  return self::$instance;
51  }
52 
56  public static function destroySingleton() {
57  self::$instance = null;
58  }
59 
63  protected function initFromGlobals() {
65 
66  // Register explicitly defined backends
67  $this->register( $wgFileBackends, wfConfiguredReadOnlyReason() );
68 
69  $autoBackends = [];
70  // Automatically create b/c backends for file repos...
71  $repos = array_merge( $wgForeignFileRepos, [ $wgLocalFileRepo ] );
72  foreach ( $repos as $info ) {
73  $backendName = $info['backend'];
74  if ( is_object( $backendName ) || isset( $this->backends[$backendName] ) ) {
75  continue; // already defined (or set to the object for some reason)
76  }
77  $repoName = $info['name'];
78  // Local vars that used to be FSRepo members...
79  $directory = $info['directory'];
80  $deletedDir = isset( $info['deletedDir'] )
81  ? $info['deletedDir']
82  : false; // deletion disabled
83  $thumbDir = isset( $info['thumbDir'] )
84  ? $info['thumbDir']
85  : "{$directory}/thumb";
86  $transcodedDir = isset( $info['transcodedDir'] )
87  ? $info['transcodedDir']
88  : "{$directory}/transcoded";
89  $fileMode = isset( $info['fileMode'] )
90  ? $info['fileMode']
91  : 0644;
92  // Get the FS backend configuration
93  $autoBackends[] = [
94  'name' => $backendName,
95  'class' => 'FSFileBackend',
96  'lockManager' => 'fsLockManager',
97  'containerPaths' => [
98  "{$repoName}-public" => "{$directory}",
99  "{$repoName}-thumb" => $thumbDir,
100  "{$repoName}-transcoded" => $transcodedDir,
101  "{$repoName}-deleted" => $deletedDir,
102  "{$repoName}-temp" => "{$directory}/temp"
103  ],
104  'fileMode' => $fileMode,
105  ];
106  }
107 
108  // Register implicitly defined backends
109  $this->register( $autoBackends, wfConfiguredReadOnlyReason() );
110  }
111 
119  protected function register( array $configs, $readOnlyReason = null ) {
120  foreach ( $configs as $config ) {
121  if ( !isset( $config['name'] ) ) {
122  throw new FileBackendException( "Cannot register a backend with no name." );
123  }
124  $name = $config['name'];
125  if ( isset( $this->backends[$name] ) ) {
126  throw new FileBackendException( "Backend with name `{$name}` already registered." );
127  } elseif ( !isset( $config['class'] ) ) {
128  throw new FileBackendException( "Backend with name `{$name}` has no class." );
129  }
130  $class = $config['class'];
131 
132  $config['readOnly'] = !empty( $config['readOnly'] )
133  ? $config['readOnly']
134  : $readOnlyReason;
135 
136  unset( $config['class'] ); // backend won't need this
137  $this->backends[$name] = [
138  'class' => $class,
139  'config' => $config,
140  'instance' => null
141  ];
142  }
143  }
144 
152  public function get( $name ) {
153  if ( !isset( $this->backends[$name] ) ) {
154  throw new FileBackendException( "No backend defined with the name `$name`." );
155  }
156  // Lazy-load the actual backend instance
157  if ( !isset( $this->backends[$name]['instance'] ) ) {
158  $class = $this->backends[$name]['class'];
159  $config = $this->backends[$name]['config'];
160  $config['wikiId'] = isset( $config['wikiId'] )
161  ? $config['wikiId']
162  : wfWikiID(); // e.g. "my_wiki-en_"
163  $config['lockManager'] =
164  LockManagerGroup::singleton( $config['wikiId'] )->get( $config['lockManager'] );
165  $config['fileJournal'] = isset( $config['fileJournal'] )
166  ? FileJournal::factory( $config['fileJournal'], $name )
167  : FileJournal::factory( [ 'class' => 'NullFileJournal' ], $name );
168  $config['wanCache'] = ObjectCache::getMainWANInstance();
169  $config['mimeCallback'] = [ $this, 'guessMimeInternal' ];
170 
171  $this->backends[$name]['instance'] = new $class( $config );
172  }
173 
174  return $this->backends[$name]['instance'];
175  }
176 
184  public function config( $name ) {
185  if ( !isset( $this->backends[$name] ) ) {
186  throw new FileBackendException( "No backend defined with the name `$name`." );
187  }
188  $class = $this->backends[$name]['class'];
189 
190  return [ 'class' => $class ] + $this->backends[$name]['config'];
191  }
192 
199  public function backendFromPath( $storagePath ) {
200  list( $backend, , ) = FileBackend::splitStoragePath( $storagePath );
201  if ( $backend !== null && isset( $this->backends[$backend] ) ) {
202  return $this->get( $backend );
203  }
204 
205  return null;
206  }
207 
215  public function guessMimeInternal( $storagePath, $content, $fsPath ) {
216  $magic = MimeMagic::singleton();
217  // Trust the extension of the storage path (caller must validate)
218  $ext = FileBackend::extensionFromPath( $storagePath );
219  $type = $magic->guessTypesForExtension( $ext );
220  // For files without a valid extension (or one at all), inspect the contents
221  if ( !$type && $fsPath ) {
222  $type = $magic->guessMimeType( $fsPath, false );
223  } elseif ( !$type && strlen( $content ) ) {
224  $tmpFile = TempFSFile::factory( 'mime_' );
225  file_put_contents( $tmpFile->getPath(), $content );
226  $type = $magic->guessMimeType( $tmpFile->getPath(), false );
227  }
228  return $type ?: 'unknown/unknown';
229  }
230 }
static factory($prefix, $extension= '')
Make a new temporary file on the file system.
Definition: TempFSFile.php:54
$wgForeignFileRepos
static getMainWANInstance()
Get the main WAN cache object.
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
the array() calling protocol came about after MediaWiki 1.4rc1.
static singleton($domain=false)
static singleton()
Get an instance of this class.
Definition: MimeMagic.php:366
backendFromPath($storagePath)
Get an appropriate backend object from a storage path.
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:189
$wgFileBackends
File backend structure configuration.
$wgLocalFileRepo
File repository structures.
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
static destroySingleton()
Destroy the singleton instance.
static extensionFromPath($path, $case= 'lowercase')
Get the final extension from a storage or FS path.
guessMimeInternal($storagePath, $content, $fsPath)
Generic file backend exception for checked and unexpected (e.g.
wfConfiguredReadOnlyReason()
Get the value of $wgReadOnly or the contents of $wgReadOnlyFile.
initFromGlobals()
Register file backends from the global variables.
config($name)
Get the config array for a backend object with a given name.
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Class to handle file backend registration.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
array $backends
(name => ('class' => string, 'config' => array, 'instance' => object))
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content $content
Definition: hooks.txt:1004
static FileBackendGroup $instance
static splitStoragePath($storagePath)
Split a storage path into a backend name, a container name, and a relative file path.
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached one of or reset my talk my contributions etc etc otherwise the built in rate limiting checks are if enabled allows for interception of redirect as a string mapping parameter names to values & $type
Definition: hooks.txt:2338
static factory(array $config, $backend)
Create an appropriate FileJournal object from config.
Definition: FileJournal.php:63
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:310