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 * Set the singleton instance to a given object
37 static function setSingleton( $instance ) {
38 self
::$instance = $instance;
42 * Construct a group of file repositories.
43 * @param array $data Array of repository info arrays.
44 * Each info array is an associative array with the 'class' member
45 * giving the class name. The entire array is passed to the repository
46 * constructor as the first parameter.
48 function __construct( $localInfo, $foreignInfo ) {
49 $this->localInfo
= $localInfo;
50 $this->foreignInfo
= $foreignInfo;
54 * Search repositories for an image.
55 * You can also use wfGetFile() to do this.
56 * @param mixed $title Title object or string
57 * @param mixed $time The 14-char timestamp before which the file should
58 * have been uploaded, or false for the current version
59 * @return File object or false if it is not found
61 function findFile( $title, $time = false ) {
62 if ( !$this->reposInitialised
) {
63 $this->initialiseRepos();
66 $image = $this->localRepo
->findFile( $title, $time );
70 foreach ( $this->foreignRepos
as $repo ) {
71 $image = $repo->findFile( $title, $time );
80 * Get the repo instance with a given key.
82 function getRepo( $index ) {
83 if ( !$this->reposInitialised
) {
84 $this->initialiseRepos();
86 if ( $index === 'local' ) {
87 return $this->localRepo
;
88 } elseif ( isset( $this->foreignRepos
[$index] ) ) {
89 return $this->foreignRepos
[$index];
96 * Get the local repository, i.e. the one corresponding to the local image
97 * table. Files are typically uploaded to the local repository.
99 function getLocalRepo() {
100 return $this->getRepo( 'local' );
104 * Initialise the $repos array
106 function initialiseRepos() {
107 if ( $this->reposInitialised
) {
110 $this->reposInitialised
= true;
112 $this->localRepo
= $this->newRepo( $this->localInfo
);
113 $this->foreignRepos
= array();
114 foreach ( $this->foreignInfo
as $key => $info ) {
115 $this->foreignRepos
[$key] = $this->newRepo( $info );
120 * Create a repo class based on an info structure
122 protected function newRepo( $info ) {
123 $class = $info['class'];
124 return new $class( $info );
128 * Split a virtual URL into repo, zone and rel parts
129 * @return an array containing repo, zone and rel
131 function splitVirtualUrl( $url ) {
132 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
133 throw new MWException( __METHOD__
.': unknown protoocl' );
136 $bits = explode( '/', substr( $url, 9 ), 3 );
137 if ( count( $bits ) != 3 ) {
138 throw new MWException( __METHOD__
.": invalid mwrepo URL: $url" );
143 function getFileProps( $fileName ) {
144 if ( FileRepo
::isVirtualUrl( $fileName ) ) {
145 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
146 if ( $repoName === '' ) {
149 $repo = $this->getRepo( $repoName );
150 return $repo->getFileProps( $fileName );
152 return File
::getPropsFromPath( $fileName );