|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File backend registration handling. |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software; you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU General Public License as published by |
|
7
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
|
8
|
|
|
* (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU General Public License along |
|
16
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., |
|
17
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
18
|
|
|
* http://www.gnu.org/copyleft/gpl.html |
|
19
|
|
|
* |
|
20
|
|
|
* @file |
|
21
|
|
|
* @ingroup FileBackend |
|
22
|
|
|
* @author Aaron Schulz |
|
23
|
|
|
*/ |
|
24
|
|
|
use \MediaWiki\Logger\LoggerFactory; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Class to handle file backend registration |
|
28
|
|
|
* |
|
29
|
|
|
* @ingroup FileBackend |
|
30
|
|
|
* @since 1.19 |
|
31
|
|
|
*/ |
|
32
|
|
|
class FileBackendGroup { |
|
33
|
|
|
/** @var FileBackendGroup */ |
|
34
|
|
|
protected static $instance = null; |
|
35
|
|
|
|
|
36
|
|
|
/** @var array (name => ('class' => string, 'config' => array, 'instance' => object)) */ |
|
37
|
|
|
protected $backends = []; |
|
38
|
|
|
|
|
39
|
|
|
protected function __construct() { |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @return FileBackendGroup |
|
44
|
|
|
*/ |
|
45
|
|
|
public static function singleton() { |
|
46
|
|
|
if ( self::$instance == null ) { |
|
47
|
|
|
self::$instance = new self(); |
|
48
|
|
|
self::$instance->initFromGlobals(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return self::$instance; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Destroy the singleton instance |
|
56
|
|
|
*/ |
|
57
|
|
|
public static function destroySingleton() { |
|
58
|
|
|
self::$instance = null; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Register file backends from the global variables |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function initFromGlobals() { |
|
65
|
|
|
global $wgLocalFileRepo, $wgForeignFileRepos, $wgFileBackends, $wgDirectoryMode; |
|
66
|
|
|
|
|
67
|
|
|
// Register explicitly defined backends |
|
68
|
|
|
$this->register( $wgFileBackends, wfConfiguredReadOnlyReason() ); |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
$autoBackends = []; |
|
71
|
|
|
// Automatically create b/c backends for file repos... |
|
72
|
|
|
$repos = array_merge( $wgForeignFileRepos, [ $wgLocalFileRepo ] ); |
|
73
|
|
|
foreach ( $repos as $info ) { |
|
74
|
|
|
$backendName = $info['backend']; |
|
75
|
|
|
if ( is_object( $backendName ) || isset( $this->backends[$backendName] ) ) { |
|
76
|
|
|
continue; // already defined (or set to the object for some reason) |
|
77
|
|
|
} |
|
78
|
|
|
$repoName = $info['name']; |
|
79
|
|
|
// Local vars that used to be FSRepo members... |
|
80
|
|
|
$directory = $info['directory']; |
|
81
|
|
|
$deletedDir = isset( $info['deletedDir'] ) |
|
82
|
|
|
? $info['deletedDir'] |
|
83
|
|
|
: false; // deletion disabled |
|
84
|
|
|
$thumbDir = isset( $info['thumbDir'] ) |
|
85
|
|
|
? $info['thumbDir'] |
|
86
|
|
|
: "{$directory}/thumb"; |
|
87
|
|
|
$transcodedDir = isset( $info['transcodedDir'] ) |
|
88
|
|
|
? $info['transcodedDir'] |
|
89
|
|
|
: "{$directory}/transcoded"; |
|
90
|
|
|
// Get the FS backend configuration |
|
91
|
|
|
$autoBackends[] = [ |
|
92
|
|
|
'name' => $backendName, |
|
93
|
|
|
'class' => 'FSFileBackend', |
|
94
|
|
|
'lockManager' => 'fsLockManager', |
|
95
|
|
|
'containerPaths' => [ |
|
96
|
|
|
"{$repoName}-public" => "{$directory}", |
|
97
|
|
|
"{$repoName}-thumb" => $thumbDir, |
|
98
|
|
|
"{$repoName}-transcoded" => $transcodedDir, |
|
99
|
|
|
"{$repoName}-deleted" => $deletedDir, |
|
100
|
|
|
"{$repoName}-temp" => "{$directory}/temp" |
|
101
|
|
|
], |
|
102
|
|
|
'fileMode' => isset( $info['fileMode'] ) ? $info['fileMode'] : 0644, |
|
103
|
|
|
'directoryMode' => $wgDirectoryMode, |
|
104
|
|
|
]; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
// Register implicitly defined backends |
|
108
|
|
|
$this->register( $autoBackends, wfConfiguredReadOnlyReason() ); |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Register an array of file backend configurations |
|
113
|
|
|
* |
|
114
|
|
|
* @param array $configs |
|
115
|
|
|
* @param string|null $readOnlyReason |
|
116
|
|
|
* @throws InvalidArgumentException |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function register( array $configs, $readOnlyReason = null ) { |
|
119
|
|
|
foreach ( $configs as $config ) { |
|
120
|
|
|
if ( !isset( $config['name'] ) ) { |
|
121
|
|
|
throw new InvalidArgumentException( "Cannot register a backend with no name." ); |
|
122
|
|
|
} |
|
123
|
|
|
$name = $config['name']; |
|
124
|
|
|
if ( isset( $this->backends[$name] ) ) { |
|
125
|
|
|
throw new LogicException( "Backend with name `{$name}` already registered." ); |
|
126
|
|
|
} elseif ( !isset( $config['class'] ) ) { |
|
127
|
|
|
throw new InvalidArgumentException( "Backend with name `{$name}` has no class." ); |
|
128
|
|
|
} |
|
129
|
|
|
$class = $config['class']; |
|
130
|
|
|
|
|
131
|
|
|
$config['readOnly'] = !empty( $config['readOnly'] ) |
|
132
|
|
|
? $config['readOnly'] |
|
133
|
|
|
: $readOnlyReason; |
|
134
|
|
|
|
|
135
|
|
|
unset( $config['class'] ); // backend won't need this |
|
136
|
|
|
$this->backends[$name] = [ |
|
137
|
|
|
'class' => $class, |
|
138
|
|
|
'config' => $config, |
|
139
|
|
|
'instance' => null |
|
140
|
|
|
]; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Get the backend object with a given name |
|
146
|
|
|
* |
|
147
|
|
|
* @param string $name |
|
148
|
|
|
* @return FileBackend |
|
149
|
|
|
* @throws InvalidArgumentException |
|
150
|
|
|
*/ |
|
151
|
|
|
public function get( $name ) { |
|
152
|
|
|
if ( !isset( $this->backends[$name] ) ) { |
|
153
|
|
|
throw new InvalidArgumentException( "No backend defined with the name `$name`." ); |
|
154
|
|
|
} |
|
155
|
|
|
// Lazy-load the actual backend instance |
|
156
|
|
|
if ( !isset( $this->backends[$name]['instance'] ) ) { |
|
157
|
|
|
$class = $this->backends[$name]['class']; |
|
158
|
|
|
$config = $this->backends[$name]['config']; |
|
159
|
|
|
$config += [ |
|
160
|
|
|
'wikiId' => wfWikiID(), // e.g. "my_wiki-en_" |
|
161
|
|
|
'mimeCallback' => [ $this, 'guessMimeInternal' ], |
|
162
|
|
|
'obResetFunc' => 'wfResetOutputBuffers', |
|
163
|
|
|
'streamMimeFunc' => [ 'StreamFile', 'contentTypeFromPath' ] |
|
164
|
|
|
]; |
|
165
|
|
|
$config['lockManager'] = |
|
166
|
|
|
LockManagerGroup::singleton( $config['wikiId'] )->get( $config['lockManager'] ); |
|
167
|
|
|
$config['fileJournal'] = isset( $config['fileJournal'] ) |
|
168
|
|
|
? FileJournal::factory( $config['fileJournal'], $name ) |
|
169
|
|
|
: FileJournal::factory( [ 'class' => 'NullFileJournal' ], $name ); |
|
170
|
|
|
$config['wanCache'] = ObjectCache::getMainWANInstance(); |
|
171
|
|
|
$config['srvCache'] = ObjectCache::getLocalServerInstance( 'hash' ); |
|
172
|
|
|
$config['statusWrapper'] = [ 'Status', 'wrap' ]; |
|
173
|
|
|
$config['tmpDirectory'] = wfTempDir(); |
|
174
|
|
|
$config['logger'] = LoggerFactory::getInstance( 'FileOperation' ); |
|
175
|
|
|
$config['profiler'] = Profiler::instance(); |
|
176
|
|
|
if ( $class === 'FileBackendMultiWrite' ) { |
|
177
|
|
|
foreach ( $config['backends'] as $index => $beConfig ) { |
|
178
|
|
|
if ( isset( $beConfig['template'] ) ) { |
|
179
|
|
|
// Config is just a modified version of a registered backend's. |
|
180
|
|
|
// This should only be used when that config is used only by this backend. |
|
181
|
|
|
$config['backends'][$index] += $this->config( $beConfig['template'] ); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
$this->backends[$name]['instance'] = new $class( $config ); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
return $this->backends[$name]['instance']; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Get the config array for a backend object with a given name |
|
194
|
|
|
* |
|
195
|
|
|
* @param string $name |
|
196
|
|
|
* @return array |
|
197
|
|
|
* @throws InvalidArgumentException |
|
198
|
|
|
*/ |
|
199
|
|
|
public function config( $name ) { |
|
200
|
|
|
if ( !isset( $this->backends[$name] ) ) { |
|
201
|
|
|
throw new InvalidArgumentException( "No backend defined with the name `$name`." ); |
|
202
|
|
|
} |
|
203
|
|
|
$class = $this->backends[$name]['class']; |
|
204
|
|
|
|
|
205
|
|
|
return [ 'class' => $class ] + $this->backends[$name]['config']; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Get an appropriate backend object from a storage path |
|
210
|
|
|
* |
|
211
|
|
|
* @param string $storagePath |
|
212
|
|
|
* @return FileBackend|null Backend or null on failure |
|
213
|
|
|
*/ |
|
214
|
|
|
public function backendFromPath( $storagePath ) { |
|
215
|
|
|
list( $backend, , ) = FileBackend::splitStoragePath( $storagePath ); |
|
216
|
|
|
if ( $backend !== null && isset( $this->backends[$backend] ) ) { |
|
217
|
|
|
return $this->get( $backend ); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
return null; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @param string $storagePath |
|
225
|
|
|
* @param string|null $content |
|
226
|
|
|
* @param string|null $fsPath |
|
227
|
|
|
* @return string |
|
228
|
|
|
* @since 1.27 |
|
229
|
|
|
*/ |
|
230
|
|
|
public function guessMimeInternal( $storagePath, $content, $fsPath ) { |
|
231
|
|
|
$magic = MimeMagic::singleton(); |
|
232
|
|
|
// Trust the extension of the storage path (caller must validate) |
|
233
|
|
|
$ext = FileBackend::extensionFromPath( $storagePath ); |
|
234
|
|
|
$type = $magic->guessTypesForExtension( $ext ); |
|
235
|
|
|
// For files without a valid extension (or one at all), inspect the contents |
|
236
|
|
|
if ( !$type && $fsPath ) { |
|
|
|
|
|
|
237
|
|
|
$type = $magic->guessMimeType( $fsPath, false ); |
|
238
|
|
|
} elseif ( !$type && strlen( $content ) ) { |
|
|
|
|
|
|
239
|
|
|
$tmpFile = TempFSFile::factory( 'mime_', '', wfTempDir() ); |
|
240
|
|
|
file_put_contents( $tmpFile->getPath(), $content ); |
|
241
|
|
|
$type = $magic->guessMimeType( $tmpFile->getPath(), false ); |
|
242
|
|
|
} |
|
243
|
|
|
return $type ?: 'unknown/unknown'; |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
|