00001 <?php
00013 class RepoGroup {
00014 var $localRepo, $foreignRepos, $reposInitialised = false;
00015 var $localInfo, $foreignInfo;
00016
00017 protected static $instance;
00018
00023 static function singleton() {
00024 if ( self::$instance ) {
00025 return self::$instance;
00026 }
00027 global $wgLocalFileRepo, $wgForeignFileRepos;
00028 self::$instance = new RepoGroup( $wgLocalFileRepo, $wgForeignFileRepos );
00029 return self::$instance;
00030 }
00031
00036 static function destroySingleton() {
00037 self::$instance = null;
00038 }
00039
00043 static function setSingleton( $instance ) {
00044 self::$instance = $instance;
00045 }
00046
00054 function __construct( $localInfo, $foreignInfo ) {
00055 $this->localInfo = $localInfo;
00056 $this->foreignInfo = $foreignInfo;
00057 }
00058
00068 function findFile( $title, $time = false, $flags = 0 ) {
00069 if ( !$this->reposInitialised ) {
00070 $this->initialiseRepos();
00071 }
00072
00073 $image = $this->localRepo->findFile( $title, $time, $flags );
00074 if ( $image ) {
00075 return $image;
00076 }
00077 foreach ( $this->foreignRepos as $repo ) {
00078 $image = $repo->findFile( $title, $time, $flags );
00079 if ( $image ) {
00080 return $image;
00081 }
00082 }
00083 return false;
00084 }
00085 function findFiles( $titles ) {
00086 if ( !$this->reposInitialised ) {
00087 $this->initialiseRepos();
00088 }
00089
00090 $titleObjs = array();
00091 foreach ( $titles as $title ) {
00092 if ( !( $title instanceof Title ) )
00093 $title = Title::makeTitleSafe( NS_FILE, $title );
00094 if ( $title )
00095 $titleObjs[$title->getDBkey()] = $title;
00096 }
00097
00098 $images = $this->localRepo->findFiles( $titleObjs );
00099
00100 foreach ( $this->foreignRepos as $repo ) {
00101
00102 foreach ( $images as $name => $image )
00103 if ( isset( $titleObjs[$name] ) )
00104 unset( $titleObjs[$name] );
00105
00106 $images = array_merge( $images, $repo->findFiles( $titleObjs ) );
00107 }
00108 return $images;
00109 }
00110
00114 function checkRedirect( $title ) {
00115 if ( !$this->reposInitialised ) {
00116 $this->initialiseRepos();
00117 }
00118
00119 $redir = $this->localRepo->checkRedirect( $title );
00120 if( $redir ) {
00121 return $redir;
00122 }
00123 foreach ( $this->foreignRepos as $repo ) {
00124 $redir = $repo->checkRedirect( $title );
00125 if ( $redir ) {
00126 return $redir;
00127 }
00128 }
00129 return false;
00130 }
00131
00132 function findBySha1( $hash ) {
00133 if ( !$this->reposInitialised ) {
00134 $this->initialiseRepos();
00135 }
00136
00137 $result = $this->localRepo->findBySha1( $hash );
00138 foreach ( $this->foreignRepos as $repo )
00139 $result = array_merge( $result, $repo->findBySha1( $hash ) );
00140 return $result;
00141 }
00142
00146 function getRepo( $index ) {
00147 if ( !$this->reposInitialised ) {
00148 $this->initialiseRepos();
00149 }
00150 if ( $index === 'local' ) {
00151 return $this->localRepo;
00152 } elseif ( isset( $this->foreignRepos[$index] ) ) {
00153 return $this->foreignRepos[$index];
00154 } else {
00155 return false;
00156 }
00157 }
00161 function getRepoByName( $name ) {
00162 if ( !$this->reposInitialised ) {
00163 $this->initialiseRepos();
00164 }
00165 foreach ( $this->foreignRepos as $key => $repo ) {
00166 if ( $repo->name == $name)
00167 return $repo;
00168 }
00169 return false;
00170 }
00171
00176 function getLocalRepo() {
00177 return $this->getRepo( 'local' );
00178 }
00179
00187 function forEachForeignRepo( $callback, $params = array() ) {
00188 foreach( $this->foreignRepos as $repo ) {
00189 $args = array_merge( array( $repo ), $params );
00190 if( call_user_func_array( $callback, $args ) ) {
00191 return true;
00192 }
00193 }
00194 return false;
00195 }
00196
00201 function hasForeignRepos() {
00202 return (bool)$this->foreignRepos;
00203 }
00204
00208 function initialiseRepos() {
00209 if ( $this->reposInitialised ) {
00210 return;
00211 }
00212 $this->reposInitialised = true;
00213
00214 $this->localRepo = $this->newRepo( $this->localInfo );
00215 $this->foreignRepos = array();
00216 foreach ( $this->foreignInfo as $key => $info ) {
00217 $this->foreignRepos[$key] = $this->newRepo( $info );
00218 }
00219 }
00220
00224 protected function newRepo( $info ) {
00225 $class = $info['class'];
00226 return new $class( $info );
00227 }
00228
00233 function splitVirtualUrl( $url ) {
00234 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
00235 throw new MWException( __METHOD__.': unknown protoocl' );
00236 }
00237
00238 $bits = explode( '/', substr( $url, 9 ), 3 );
00239 if ( count( $bits ) != 3 ) {
00240 throw new MWException( __METHOD__.": invalid mwrepo URL: $url" );
00241 }
00242 return $bits;
00243 }
00244
00245 function getFileProps( $fileName ) {
00246 if ( FileRepo::isVirtualUrl( $fileName ) ) {
00247 list( $repoName, , ) = $this->splitVirtualUrl( $fileName );
00248 if ( $repoName === '' ) {
00249 $repoName = 'local';
00250 }
00251 $repo = $this->getRepo( $repoName );
00252 return $repo->getFileProps( $fileName );
00253 } else {
00254 return File::getPropsFromPath( $fileName );
00255 }
00256 }
00257 }