d02c7d409f0e1ce357c31b92f99c6c06adb09031
[lhc/web/wiklou.git] / includes / filerepo / backend / FileBackendGroup.php
1 <?php
2 /**
3 * @file
4 * @ingroup FileBackend
5 * @author Aaron Schulz
6 */
7
8 /**
9 * Class to handle file backend registration
10 *
11 * @ingroup FileBackend
12 * @since 1.19
13 */
14 class FileBackendGroup {
15 /**
16 * @var FileBackendGroup
17 */
18 protected static $instance = null;
19
20 /** @var Array (name => ('class' => string, 'config' => array, 'instance' => object)) */
21 protected $backends = array();
22
23 protected function __construct() {}
24
25 /**
26 * @return FileBackendGroup
27 */
28 public static function singleton() {
29 if ( self::$instance == null ) {
30 self::$instance = new self();
31 self::$instance->initFromGlobals();
32 }
33 return self::$instance;
34 }
35
36 /**
37 * Destroy the singleton instance
38 *
39 * @return void
40 */
41 public static function destroySingleton() {
42 self::$instance = null;
43 }
44
45 /**
46 * Register file backends from the global variables
47 *
48 * @return void
49 */
50 protected function initFromGlobals() {
51 global $wgLocalFileRepo, $wgForeignFileRepos, $wgFileBackends;
52
53 // Register explicitly defined backends
54 $this->register( $wgFileBackends );
55
56 $autoBackends = array();
57 // Automatically create b/c backends for file repos...
58 $repos = array_merge( $wgForeignFileRepos, array( $wgLocalFileRepo ) );
59 foreach ( $repos as $info ) {
60 $backendName = $info['backend'];
61 if ( is_object( $backendName ) || isset( $this->backends[$backendName] ) ) {
62 continue; // already defined (or set to the object for some reason)
63 }
64 $repoName = $info['name'];
65 // Local vars that used to be FSRepo members...
66 $directory = $info['directory'];
67 $deletedDir = isset( $info['deletedDir'] )
68 ? $info['deletedDir']
69 : false; // deletion disabled
70 $thumbDir = isset( $info['thumbDir'] )
71 ? $info['thumbDir']
72 : "{$directory}/thumb";
73 $fileMode = isset( $info['fileMode'] )
74 ? $info['fileMode']
75 : 0644;
76 // Get the FS backend configuration
77 $autoBackends[] = array(
78 'name' => $backendName,
79 'class' => 'FSFileBackend',
80 'lockManager' => 'fsLockManager',
81 'containerPaths' => array(
82 "{$repoName}-public" => "{$directory}",
83 "{$repoName}-thumb" => $thumbDir,
84 "{$repoName}-deleted" => $deletedDir,
85 "{$repoName}-temp" => "{$directory}/temp"
86 ),
87 'fileMode' => $fileMode,
88 );
89 }
90
91 // Register implicitly defined backends
92 $this->register( $autoBackends );
93 }
94
95 /**
96 * Register an array of file backend configurations
97 *
98 * @param $configs Array
99 * @return void
100 * @throws MWException
101 */
102 protected function register( array $configs ) {
103 foreach ( $configs as $config ) {
104 if ( !isset( $config['name'] ) ) {
105 throw new MWException( "Cannot register a backend with no name." );
106 }
107 $name = $config['name'];
108 if ( !isset( $config['class'] ) ) {
109 throw new MWException( "Cannot register backend `{$name}` with no class." );
110 }
111 $class = $config['class'];
112
113 unset( $config['class'] ); // backend won't need this
114 $this->backends[$name] = array(
115 'class' => $class,
116 'config' => $config,
117 'instance' => null
118 );
119 }
120 }
121
122 /**
123 * Get the backend object with a given name
124 *
125 * @param $name string
126 * @return FileBackend
127 * @throws MWException
128 */
129 public function get( $name ) {
130 if ( !isset( $this->backends[$name] ) ) {
131 throw new MWException( "No backend defined with the name `$name`." );
132 }
133 // Lazy-load the actual backend instance
134 if ( !isset( $this->backends[$name]['instance'] ) ) {
135 $class = $this->backends[$name]['class'];
136 $config = $this->backends[$name]['config'];
137 $this->backends[$name]['instance'] = new $class( $config );
138 }
139 return $this->backends[$name]['instance'];
140 }
141
142 /**
143 * Get an appropriate backend object from a storage path
144 *
145 * @param $storagePath string
146 * @return FileBackend|null Backend or null on failure
147 */
148 public function backendFromPath( $storagePath ) {
149 list( $backend, $c, $p ) = FileBackend::splitStoragePath( $storagePath );
150 if ( $backend !== null && isset( $this->backends[$backend] ) ) {
151 return $this->get( $backend );
152 }
153 return null;
154 }
155 }