3055382624f3e85ca68619a1f5684cad0541ad91
[lhc/web/wiklou.git] / includes / filerepo / RepoGroup.php
1 <?php
2
3 class RepoGroup {
4 var $localRepo, $foreignRepos, $reposInitialised = false;
5 var $localInfo, $foreignInfo;
6
7 protected static $instance;
8
9 function singleton() {
10 if ( self::$instance ) {
11 return self::$instance;
12 }
13 global $wgLocalFileRepo, $wgForeignFileRepos;
14 self::$instance = new RepoGroup( $wgLocalFileRepo, $wgForeignFileRepos );
15 return self::$instance;
16 }
17
18 /**
19 * Construct a group of file repositories.
20 * @param array $data Array of repository info arrays.
21 * Each info array is an associative array with the 'class' member
22 * giving the class name. The entire array is passed to the repository
23 * constructor as the first parameter.
24 */
25 function __construct( $localInfo, $foreignInfo ) {
26 $this->localInfo = $localInfo;
27 $this->foreignInfo = $foreignInfo;
28 }
29
30 /**
31 * Search repositories for an image.
32 * You can also use wfGetFile() to do this.
33 * @param mixed $title Title object or string
34 * @param mixed $time The 14-char timestamp before which the file should
35 * have been uploaded, or false for the current version
36 * @return File object or false if it is not found
37 */
38 function findFile( $title, $time = false ) {
39 if ( !$this->reposInitialised ) {
40 $this->initialiseRepos();
41 }
42
43 $image = $this->localRepo->findFile( $title, $time );
44 if ( $image ) {
45 return $image;
46 }
47 foreach ( $this->foreignRepos as $repo ) {
48 $image = $repo->findFile( $image, $time );
49 if ( $image ) {
50 return $image;
51 }
52 }
53 return false;
54 }
55
56 /**
57 * Get the repo instance with a given key.
58 */
59 function getRepo( $index ) {
60 if ( !$this->reposInitialised ) {
61 $this->initialiseRepos();
62 }
63 if ( $index == 'local' ) {
64 return $this->localRepo;
65 } elseif ( isset( $this->foreignRepos[$index] ) ) {
66 return $this->foreignRepos[$index];
67 } else {
68 return false;
69 }
70 }
71
72 function getLocalRepo() {
73 return $this->getRepo( 'local' );
74 }
75
76 /**
77 * Initialise the $repos array
78 */
79 function initialiseRepos() {
80 if ( $this->reposInitialised ) {
81 return;
82 }
83 $this->reposInitialised = true;
84
85 $this->localRepo = $this->newRepo( $this->localInfo );
86 $this->foreignRepos = array();
87 foreach ( $this->foreignInfo as $key => $info ) {
88 $this->foreignRepos[$key] = $this->newRepo( $info );
89 }
90 }
91
92 function newRepo( $info ) {
93 $class = $info['class'];
94 return new $class( $info );
95 }
96 }
97
98 ?>