RepoGroup.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. <?php
  2. /**
  3. * Prioritized list of file repositories.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup FileRepo
  22. */
  23. /**
  24. * Prioritized list of file repositories
  25. *
  26. * @ingroup FileRepo
  27. */
  28. class RepoGroup {
  29. /** @var LocalRepo */
  30. protected $localRepo;
  31. /** @var FileRepo[] */
  32. protected $foreignRepos;
  33. /** @var bool */
  34. protected $reposInitialised = false;
  35. /** @var array */
  36. protected $localInfo;
  37. /** @var array */
  38. protected $foreignInfo;
  39. /** @var ProcessCacheLRU */
  40. protected $cache;
  41. /** @var RepoGroup */
  42. protected static $instance;
  43. /** Maximum number of cache items */
  44. const MAX_CACHE_SIZE = 500;
  45. /**
  46. * Get a RepoGroup instance. At present only one instance of RepoGroup is
  47. * needed in a MediaWiki invocation, this may change in the future.
  48. * @return RepoGroup
  49. */
  50. static function singleton() {
  51. if ( self::$instance ) {
  52. return self::$instance;
  53. }
  54. global $wgLocalFileRepo, $wgForeignFileRepos;
  55. self::$instance = new RepoGroup( $wgLocalFileRepo, $wgForeignFileRepos );
  56. return self::$instance;
  57. }
  58. /**
  59. * Destroy the singleton instance, so that a new one will be created next
  60. * time singleton() is called.
  61. */
  62. static function destroySingleton() {
  63. self::$instance = null;
  64. }
  65. /**
  66. * Set the singleton instance to a given object
  67. * Used by extensions which hook into the Repo chain.
  68. * It's not enough to just create a superclass ... you have
  69. * to get people to call into it even though all they know is RepoGroup::singleton()
  70. *
  71. * @param RepoGroup $instance
  72. */
  73. static function setSingleton( $instance ) {
  74. self::$instance = $instance;
  75. }
  76. /**
  77. * Construct a group of file repositories.
  78. *
  79. * @param array $localInfo Associative array for local repo's info
  80. * @param array $foreignInfo Array of repository info arrays.
  81. * Each info array is an associative array with the 'class' member
  82. * giving the class name. The entire array is passed to the repository
  83. * constructor as the first parameter.
  84. */
  85. function __construct( $localInfo, $foreignInfo ) {
  86. $this->localInfo = $localInfo;
  87. $this->foreignInfo = $foreignInfo;
  88. $this->cache = new ProcessCacheLRU( self::MAX_CACHE_SIZE );
  89. }
  90. /**
  91. * Search repositories for an image.
  92. * You can also use wfFindFile() to do this.
  93. *
  94. * @param Title|string $title Title object or string
  95. * @param array $options Associative array of options:
  96. * time: requested time for an archived image, or false for the
  97. * current version. An image object will be returned which was
  98. * created at the specified time.
  99. * ignoreRedirect: If true, do not follow file redirects
  100. * private: If true, return restricted (deleted) files if the current
  101. * user is allowed to view them. Otherwise, such files will not
  102. * be found.
  103. * latest: If true, load from the latest available data into File objects
  104. * @return File|bool False if title is not found
  105. */
  106. function findFile( $title, $options = [] ) {
  107. if ( !is_array( $options ) ) {
  108. // MW 1.15 compat
  109. $options = [ 'time' => $options ];
  110. }
  111. if ( isset( $options['bypassCache'] ) ) {
  112. $options['latest'] = $options['bypassCache']; // b/c
  113. }
  114. if ( !$this->reposInitialised ) {
  115. $this->initialiseRepos();
  116. }
  117. $title = File::normalizeTitle( $title );
  118. if ( !$title ) {
  119. return false;
  120. }
  121. # Check the cache
  122. $dbkey = $title->getDBkey();
  123. if ( empty( $options['ignoreRedirect'] )
  124. && empty( $options['private'] )
  125. && empty( $options['latest'] )
  126. ) {
  127. $time = isset( $options['time'] ) ? $options['time'] : '';
  128. if ( $this->cache->has( $dbkey, $time, 60 ) ) {
  129. return $this->cache->get( $dbkey, $time );
  130. }
  131. $useCache = true;
  132. } else {
  133. $time = false;
  134. $useCache = false;
  135. }
  136. # Check the local repo
  137. $image = $this->localRepo->findFile( $title, $options );
  138. # Check the foreign repos
  139. if ( !$image ) {
  140. foreach ( $this->foreignRepos as $repo ) {
  141. $image = $repo->findFile( $title, $options );
  142. if ( $image ) {
  143. break;
  144. }
  145. }
  146. }
  147. $image = $image ? $image : false; // type sanity
  148. # Cache file existence or non-existence
  149. if ( $useCache && ( !$image || $image->isCacheable() ) ) {
  150. $this->cache->set( $dbkey, $time, $image );
  151. }
  152. return $image;
  153. }
  154. /**
  155. * Search repositories for many files at once.
  156. *
  157. * @param array $inputItems An array of titles, or an array of findFile() options with
  158. * the "title" option giving the title. Example:
  159. *
  160. * $findItem = [ 'title' => $title, 'private' => true ];
  161. * $findBatch = [ $findItem ];
  162. * $repo->findFiles( $findBatch );
  163. *
  164. * No title should appear in $items twice, as the result use titles as keys
  165. * @param int $flags Supports:
  166. * - FileRepo::NAME_AND_TIME_ONLY : return a (search title => (title,timestamp)) map.
  167. * The search title uses the input titles; the other is the final post-redirect title.
  168. * All titles are returned as string DB keys and the inner array is associative.
  169. * @return array Map of (file name => File objects) for matches
  170. */
  171. function findFiles( array $inputItems, $flags = 0 ) {
  172. if ( !$this->reposInitialised ) {
  173. $this->initialiseRepos();
  174. }
  175. $items = [];
  176. foreach ( $inputItems as $item ) {
  177. if ( !is_array( $item ) ) {
  178. $item = [ 'title' => $item ];
  179. }
  180. $item['title'] = File::normalizeTitle( $item['title'] );
  181. if ( $item['title'] ) {
  182. $items[$item['title']->getDBkey()] = $item;
  183. }
  184. }
  185. $images = $this->localRepo->findFiles( $items, $flags );
  186. foreach ( $this->foreignRepos as $repo ) {
  187. // Remove found files from $items
  188. foreach ( $images as $name => $image ) {
  189. unset( $items[$name] );
  190. }
  191. $images = array_merge( $images, $repo->findFiles( $items, $flags ) );
  192. }
  193. return $images;
  194. }
  195. /**
  196. * Interface for FileRepo::checkRedirect()
  197. * @param Title $title
  198. * @return bool|Title
  199. */
  200. function checkRedirect( Title $title ) {
  201. if ( !$this->reposInitialised ) {
  202. $this->initialiseRepos();
  203. }
  204. $redir = $this->localRepo->checkRedirect( $title );
  205. if ( $redir ) {
  206. return $redir;
  207. }
  208. foreach ( $this->foreignRepos as $repo ) {
  209. $redir = $repo->checkRedirect( $title );
  210. if ( $redir ) {
  211. return $redir;
  212. }
  213. }
  214. return false;
  215. }
  216. /**
  217. * Find an instance of the file with this key, created at the specified time
  218. * Returns false if the file does not exist.
  219. *
  220. * @param string $hash Base 36 SHA-1 hash
  221. * @param array $options Option array, same as findFile()
  222. * @return File|bool File object or false if it is not found
  223. */
  224. function findFileFromKey( $hash, $options = [] ) {
  225. if ( !$this->reposInitialised ) {
  226. $this->initialiseRepos();
  227. }
  228. $file = $this->localRepo->findFileFromKey( $hash, $options );
  229. if ( !$file ) {
  230. foreach ( $this->foreignRepos as $repo ) {
  231. $file = $repo->findFileFromKey( $hash, $options );
  232. if ( $file ) {
  233. break;
  234. }
  235. }
  236. }
  237. return $file;
  238. }
  239. /**
  240. * Find all instances of files with this key
  241. *
  242. * @param string $hash Base 36 SHA-1 hash
  243. * @return File[]
  244. */
  245. function findBySha1( $hash ) {
  246. if ( !$this->reposInitialised ) {
  247. $this->initialiseRepos();
  248. }
  249. $result = $this->localRepo->findBySha1( $hash );
  250. foreach ( $this->foreignRepos as $repo ) {
  251. $result = array_merge( $result, $repo->findBySha1( $hash ) );
  252. }
  253. usort( $result, 'File::compare' );
  254. return $result;
  255. }
  256. /**
  257. * Find all instances of files with this keys
  258. *
  259. * @param array $hashes Base 36 SHA-1 hashes
  260. * @return array Array of array of File objects
  261. */
  262. function findBySha1s( array $hashes ) {
  263. if ( !$this->reposInitialised ) {
  264. $this->initialiseRepos();
  265. }
  266. $result = $this->localRepo->findBySha1s( $hashes );
  267. foreach ( $this->foreignRepos as $repo ) {
  268. $result = array_merge_recursive( $result, $repo->findBySha1s( $hashes ) );
  269. }
  270. // sort the merged (and presorted) sublist of each hash
  271. foreach ( $result as $hash => $files ) {
  272. usort( $result[$hash], 'File::compare' );
  273. }
  274. return $result;
  275. }
  276. /**
  277. * Get the repo instance with a given key.
  278. * @param string|int $index
  279. * @return bool|LocalRepo
  280. */
  281. function getRepo( $index ) {
  282. if ( !$this->reposInitialised ) {
  283. $this->initialiseRepos();
  284. }
  285. if ( $index === 'local' ) {
  286. return $this->localRepo;
  287. } elseif ( isset( $this->foreignRepos[$index] ) ) {
  288. return $this->foreignRepos[$index];
  289. } else {
  290. return false;
  291. }
  292. }
  293. /**
  294. * Get the repo instance by its name
  295. * @param string $name
  296. * @return FileRepo|bool
  297. */
  298. function getRepoByName( $name ) {
  299. if ( !$this->reposInitialised ) {
  300. $this->initialiseRepos();
  301. }
  302. foreach ( $this->foreignRepos as $repo ) {
  303. if ( $repo->name == $name ) {
  304. return $repo;
  305. }
  306. }
  307. return false;
  308. }
  309. /**
  310. * Get the local repository, i.e. the one corresponding to the local image
  311. * table. Files are typically uploaded to the local repository.
  312. *
  313. * @return LocalRepo
  314. */
  315. function getLocalRepo() {
  316. return $this->getRepo( 'local' );
  317. }
  318. /**
  319. * Call a function for each foreign repo, with the repo object as the
  320. * first parameter.
  321. *
  322. * @param callable $callback The function to call
  323. * @param array $params Optional additional parameters to pass to the function
  324. * @return bool
  325. */
  326. function forEachForeignRepo( $callback, $params = [] ) {
  327. if ( !$this->reposInitialised ) {
  328. $this->initialiseRepos();
  329. }
  330. foreach ( $this->foreignRepos as $repo ) {
  331. $args = array_merge( [ $repo ], $params );
  332. if ( call_user_func_array( $callback, $args ) ) {
  333. return true;
  334. }
  335. }
  336. return false;
  337. }
  338. /**
  339. * Does the installation have any foreign repos set up?
  340. * @return bool
  341. */
  342. function hasForeignRepos() {
  343. if ( !$this->reposInitialised ) {
  344. $this->initialiseRepos();
  345. }
  346. return (bool)$this->foreignRepos;
  347. }
  348. /**
  349. * Initialise the $repos array
  350. */
  351. function initialiseRepos() {
  352. if ( $this->reposInitialised ) {
  353. return;
  354. }
  355. $this->reposInitialised = true;
  356. $this->localRepo = $this->newRepo( $this->localInfo );
  357. $this->foreignRepos = [];
  358. foreach ( $this->foreignInfo as $key => $info ) {
  359. $this->foreignRepos[$key] = $this->newRepo( $info );
  360. }
  361. }
  362. /**
  363. * Create a repo class based on an info structure
  364. * @param array $info
  365. * @return FileRepo
  366. */
  367. protected function newRepo( $info ) {
  368. $class = $info['class'];
  369. return new $class( $info );
  370. }
  371. /**
  372. * Split a virtual URL into repo, zone and rel parts
  373. * @param string $url
  374. * @throws MWException
  375. * @return string[] Containing repo, zone and rel
  376. */
  377. function splitVirtualUrl( $url ) {
  378. if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
  379. throw new MWException( __METHOD__ . ': unknown protocol' );
  380. }
  381. $bits = explode( '/', substr( $url, 9 ), 3 );
  382. if ( count( $bits ) != 3 ) {
  383. throw new MWException( __METHOD__ . ": invalid mwrepo URL: $url" );
  384. }
  385. return $bits;
  386. }
  387. /**
  388. * @param string $fileName
  389. * @return array
  390. */
  391. function getFileProps( $fileName ) {
  392. if ( FileRepo::isVirtualUrl( $fileName ) ) {
  393. list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
  394. if ( $repoName === '' ) {
  395. $repoName = 'local';
  396. }
  397. $repo = $this->getRepo( $repoName );
  398. return $repo->getFileProps( $fileName );
  399. } else {
  400. $mwProps = new MWFileProps( MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer() );
  401. return $mwProps->getPropsFromPath( $fileName, true );
  402. }
  403. }
  404. /**
  405. * Clear RepoGroup process cache used for finding a file
  406. * @param Title|null $title Title of the file or null to clear all files
  407. */
  408. public function clearCache( Title $title = null ) {
  409. if ( $title == null ) {
  410. $this->cache->clear();
  411. } else {
  412. $this->cache->clear( $title->getDBkey() );
  413. }
  414. }
  415. }