4 * Prioritized list of file repositories
8 var $localRepo, $foreignRepos, $reposInitialised = false;
9 var $localInfo, $foreignInfo;
11 protected static $instance;
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.
17 static function singleton() {
18 if ( self
::$instance ) {
19 return self
::$instance;
21 global $wgLocalFileRepo, $wgForeignFileRepos;
22 self
::$instance = new RepoGroup( $wgLocalFileRepo, $wgForeignFileRepos );
23 return self
::$instance;
27 * Destroy the singleton instance, so that a new one will be created next
28 * time singleton() is called.
30 static function destroySingleton() {
31 self
::$instance = null;
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.
41 function __construct( $localInfo, $foreignInfo ) {
42 $this->localInfo
= $localInfo;
43 $this->foreignInfo
= $foreignInfo;
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
54 function findFile( $title, $time = false ) {
55 if ( !$this->reposInitialised
) {
56 $this->initialiseRepos();
59 $image = $this->localRepo
->findFile( $title, $time );
63 foreach ( $this->foreignRepos
as $repo ) {
64 $image = $repo->findFile( $title, $time );
73 * Get the repo instance with a given key.
75 function getRepo( $index ) {
76 if ( !$this->reposInitialised
) {
77 $this->initialiseRepos();
79 if ( $index === 'local' ) {
80 return $this->localRepo
;
81 } elseif ( isset( $this->foreignRepos
[$index] ) ) {
82 return $this->foreignRepos
[$index];
89 * Get the local repository, i.e. the one corresponding to the local image
90 * table. Files are typically uploaded to the local repository.
92 function getLocalRepo() {
93 return $this->getRepo( 'local' );
97 * Initialise the $repos array
99 function initialiseRepos() {
100 if ( $this->reposInitialised
) {
103 $this->reposInitialised
= true;
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 );
113 * Create a repo class based on an info structure
115 protected function newRepo( $info ) {
116 $class = $info['class'];
117 return new $class( $info );
121 * Split a virtual URL into repo, zone and rel parts
122 * @return an array containing repo, zone and rel
124 function splitVirtualUrl( $url ) {
125 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
126 throw new MWException( __METHOD__
.': unknown protoocl' );
129 $bits = explode( '/', substr( $url, 9 ), 3 );
130 if ( count( $bits ) != 3 ) {
131 throw new MWException( __METHOD__
.": invalid mwrepo URL: $url" );
136 function getFileProps( $fileName ) {
137 if ( FileRepo
::isVirtualUrl( $fileName ) ) {
138 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
139 if ( $repoName === '' ) {
142 $repo = $this->getRepo( $repoName );
143 return $repo->getFileProps( $fileName );
145 return File
::getPropsFromPath( $fileName );