Fix regression introduced in r23023; use the right function name when calling to...
[lhc/web/wiklou.git] / includes / filerepo / RepoGroup.php
1 <?php
2
3 /**
4 * Prioritized list of file repositories
5 * @addtogroup filerepo
6 */
7 class RepoGroup {
8 var $localRepo, $foreignRepos, $reposInitialised = false;
9 var $localInfo, $foreignInfo;
10
11 protected static $instance;
12
13 /**
14 * Get a RepoGroup instance. At present only one instance of RepoGroup is
15 * needed in a MediaWiki invocation, this may change in the future.
16 */
17 static function singleton() {
18 if ( self::$instance ) {
19 return self::$instance;
20 }
21 global $wgLocalFileRepo, $wgForeignFileRepos;
22 self::$instance = new RepoGroup( $wgLocalFileRepo, $wgForeignFileRepos );
23 return self::$instance;
24 }
25
26 /**
27 * Destroy the singleton instance, so that a new one will be created next
28 * time singleton() is called.
29 */
30 static function destroySingleton() {
31 self::$instance = null;
32 }
33
34 /**
35 * Construct a group of file repositories.
36 * @param array $data Array of repository info arrays.
37 * Each info array is an associative array with the 'class' member
38 * giving the class name. The entire array is passed to the repository
39 * constructor as the first parameter.
40 */
41 function __construct( $localInfo, $foreignInfo ) {
42 $this->localInfo = $localInfo;
43 $this->foreignInfo = $foreignInfo;
44 }
45
46 /**
47 * Search repositories for an image.
48 * You can also use wfGetFile() to do this.
49 * @param mixed $title Title object or string
50 * @param mixed $time The 14-char timestamp before which the file should
51 * have been uploaded, or false for the current version
52 * @return File object or false if it is not found
53 */
54 function findFile( $title, $time = false ) {
55 if ( !$this->reposInitialised ) {
56 $this->initialiseRepos();
57 }
58
59 $image = $this->localRepo->findFile( $title, $time );
60 if ( $image ) {
61 return $image;
62 }
63 foreach ( $this->foreignRepos as $repo ) {
64 $image = $repo->findFile( $title, $time );
65 if ( $image ) {
66 return $image;
67 }
68 }
69 return false;
70 }
71
72 /**
73 * Get the repo instance with a given key.
74 */
75 function getRepo( $index ) {
76 if ( !$this->reposInitialised ) {
77 $this->initialiseRepos();
78 }
79 if ( $index == 'local' ) {
80 return $this->localRepo;
81 } elseif ( isset( $this->foreignRepos[$index] ) ) {
82 return $this->foreignRepos[$index];
83 } else {
84 return false;
85 }
86 }
87
88 /**
89 * Get the local repository, i.e. the one corresponding to the local image
90 * table. Files are typically uploaded to the local repository.
91 */
92 function getLocalRepo() {
93 return $this->getRepo( 'local' );
94 }
95
96 /**
97 * Initialise the $repos array
98 */
99 function initialiseRepos() {
100 if ( $this->reposInitialised ) {
101 return;
102 }
103 $this->reposInitialised = true;
104
105 $this->localRepo = $this->newRepo( $this->localInfo );
106 $this->foreignRepos = array();
107 foreach ( $this->foreignInfo as $key => $info ) {
108 $this->foreignRepos[$key] = $this->newRepo( $info );
109 }
110 }
111
112 /**
113 * Create a repo class based on an info structure
114 */
115 protected function newRepo( $info ) {
116 $class = $info['class'];
117 return new $class( $info );
118 }
119
120 /**
121 * Split a virtual URL into repo, zone and rel parts
122 * @return an array containing repo, zone and rel
123 */
124 function splitVirtualUrl( $url ) {
125 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
126 throw new MWException( __METHOD__.': unknown protoocl' );
127 }
128
129 $bits = explode( '/', substr( $url, 9 ), 3 );
130 if ( count( $bits ) != 3 ) {
131 throw new MWException( __METHOD__.": invalid mwrepo URL: $url" );
132 }
133 return $bits;
134 }
135
136 function getFileProps( $fileName ) {
137 if ( FileRepo::isVirtualUrl( $fileName ) ) {
138 list( $repoName, $zone, $rel ) = $this->splitVirtualUrl( $fileName );
139 if ( $repoName === '' ) {
140 $repoName = 'local';
141 }
142 $repo = $this->getRepo( $repoName );
143 return $repo->getFileProps( $fileName );
144 } else {
145 return File::getPropsFromPath( $fileName );
146 }
147 }
148 }
149
150 ?>