Merge "Ensure users are able to edit the page after changing the content model"
[lhc/web/wiklou.git] / includes / filebackend / FileBackendGroup.php
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
25 /**
26 * Class to handle file backend registration
27 *
28 * @ingroup FileBackend
29 * @since 1.19
30 */
31 class FileBackendGroup {
32 /** @var FileBackendGroup */
33 protected static $instance = null;
34
35 /** @var array (name => ('class' => string, 'config' => array, 'instance' => object)) */
36 protected $backends = [];
37
38 protected function __construct() {
39 }
40
41 /**
42 * @return FileBackendGroup
43 */
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
53 /**
54 * Destroy the singleton instance
55 */
56 public static function destroySingleton() {
57 self::$instance = null;
58 }
59
60 /**
61 * Register file backends from the global variables
62 */
63 protected function initFromGlobals() {
64 global $wgLocalFileRepo, $wgForeignFileRepos, $wgFileBackends;
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
112 /**
113 * Register an array of file backend configurations
114 *
115 * @param array $configs
116 * @param string|null $readOnlyReason
117 * @throws InvalidArgumentException
118 */
119 protected function register( array $configs, $readOnlyReason = null ) {
120 foreach ( $configs as $config ) {
121 if ( !isset( $config['name'] ) ) {
122 throw new InvalidArgumentException( "Cannot register a backend with no name." );
123 }
124 $name = $config['name'];
125 if ( isset( $this->backends[$name] ) ) {
126 throw new LogicException( "Backend with name `{$name}` already registered." );
127 } elseif ( !isset( $config['class'] ) ) {
128 throw new InvalidArgumentException( "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
145 /**
146 * Get the backend object with a given name
147 *
148 * @param string $name
149 * @return FileBackend
150 * @throws InvalidArgumentException
151 */
152 public function get( $name ) {
153 if ( !isset( $this->backends[$name] ) ) {
154 throw new InvalidArgumentException( "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 $config['statusWrapper'] = [ 'Status', 'wrap' ];
171 $config['tmpDirectory'] = wfTempDir();
172
173 $this->backends[$name]['instance'] = new $class( $config );
174 }
175
176 return $this->backends[$name]['instance'];
177 }
178
179 /**
180 * Get the config array for a backend object with a given name
181 *
182 * @param string $name
183 * @return array
184 * @throws InvalidArgumentException
185 */
186 public function config( $name ) {
187 if ( !isset( $this->backends[$name] ) ) {
188 throw new InvalidArgumentException( "No backend defined with the name `$name`." );
189 }
190 $class = $this->backends[$name]['class'];
191
192 return [ 'class' => $class ] + $this->backends[$name]['config'];
193 }
194
195 /**
196 * Get an appropriate backend object from a storage path
197 *
198 * @param string $storagePath
199 * @return FileBackend|null Backend or null on failure
200 */
201 public function backendFromPath( $storagePath ) {
202 list( $backend, , ) = FileBackend::splitStoragePath( $storagePath );
203 if ( $backend !== null && isset( $this->backends[$backend] ) ) {
204 return $this->get( $backend );
205 }
206
207 return null;
208 }
209
210 /**
211 * @param string $storagePath
212 * @param string|null $content
213 * @param string|null $fsPath
214 * @return string
215 * @since 1.27
216 */
217 public function guessMimeInternal( $storagePath, $content, $fsPath ) {
218 $magic = MimeMagic::singleton();
219 // Trust the extension of the storage path (caller must validate)
220 $ext = FileBackend::extensionFromPath( $storagePath );
221 $type = $magic->guessTypesForExtension( $ext );
222 // For files without a valid extension (or one at all), inspect the contents
223 if ( !$type && $fsPath ) {
224 $type = $magic->guessMimeType( $fsPath, false );
225 } elseif ( !$type && strlen( $content ) ) {
226 $tmpFile = TempFSFile::factory( 'mime_', '', wfTempDir() );
227 file_put_contents( $tmpFile->getPath(), $content );
228 $type = $magic->guessMimeType( $tmpFile->getPath(), false );
229 }
230 return $type ?: 'unknown/unknown';
231 }
232 }