FileRepo.php 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956
  1. <?php
  2. /**
  3. * @defgroup FileRepo File Repository
  4. *
  5. * @brief This module handles how MediaWiki interacts with filesystems.
  6. *
  7. * @details
  8. */
  9. /**
  10. * Base code for file repositories.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License along
  23. * with this program; if not, write to the Free Software Foundation, Inc.,
  24. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  25. * http://www.gnu.org/copyleft/gpl.html
  26. *
  27. * @file
  28. * @ingroup FileRepo
  29. */
  30. /**
  31. * Base class for file repositories
  32. *
  33. * @ingroup FileRepo
  34. */
  35. class FileRepo {
  36. const DELETE_SOURCE = 1;
  37. const OVERWRITE = 2;
  38. const OVERWRITE_SAME = 4;
  39. const SKIP_LOCKING = 8;
  40. const NAME_AND_TIME_ONLY = 1;
  41. /** @var bool Whether to fetch commons image description pages and display
  42. * them on the local wiki */
  43. public $fetchDescription;
  44. /** @var int */
  45. public $descriptionCacheExpiry;
  46. /** @var bool */
  47. protected $hasSha1Storage = false;
  48. /** @var bool */
  49. protected $supportsSha1URLs = false;
  50. /** @var FileBackend */
  51. protected $backend;
  52. /** @var array Map of zones to config */
  53. protected $zones = [];
  54. /** @var string URL of thumb.php */
  55. protected $thumbScriptUrl;
  56. /** @var bool Whether to skip media file transformation on parse and rely
  57. * on a 404 handler instead. */
  58. protected $transformVia404;
  59. /** @var string URL of image description pages, e.g.
  60. * https://en.wikipedia.org/wiki/File:
  61. */
  62. protected $descBaseUrl;
  63. /** @var string URL of the MediaWiki installation, equivalent to
  64. * $wgScriptPath, e.g. https://en.wikipedia.org/w
  65. */
  66. protected $scriptDirUrl;
  67. /** @var string Equivalent to $wgArticlePath, e.g. https://en.wikipedia.org/wiki/$1 */
  68. protected $articleUrl;
  69. /** @var bool Equivalent to $wgCapitalLinks (or $wgCapitalLinkOverrides[NS_FILE],
  70. * determines whether filenames implicitly start with a capital letter.
  71. * The current implementation may give incorrect description page links
  72. * when the local $wgCapitalLinks and initialCapital are mismatched.
  73. */
  74. protected $initialCapital;
  75. /** @var string May be 'paranoid' to remove all parameters from error
  76. * messages, 'none' to leave the paths in unchanged, or 'simple' to
  77. * replace paths with placeholders. Default for LocalRepo is
  78. * 'simple'.
  79. */
  80. protected $pathDisclosureProtection = 'simple';
  81. /** @var string|false Public zone URL. */
  82. protected $url;
  83. /** @var string The base thumbnail URL. Defaults to "<url>/thumb". */
  84. protected $thumbUrl;
  85. /** @var int The number of directory levels for hash-based division of files */
  86. protected $hashLevels;
  87. /** @var int The number of directory levels for hash-based division of deleted files */
  88. protected $deletedHashLevels;
  89. /** @var int File names over this size will use the short form of thumbnail
  90. * names. Short thumbnail names only have the width, parameters, and the
  91. * extension.
  92. */
  93. protected $abbrvThreshold;
  94. /** @var string The URL of the repo's favicon, if any */
  95. protected $favicon;
  96. /** @var bool Whether all zones should be private (e.g. private wiki repo) */
  97. protected $isPrivate;
  98. /** @var array callable Override these in the base class */
  99. protected $fileFactory = [ UnregisteredLocalFile::class, 'newFromTitle' ];
  100. /** @var array callable|bool Override these in the base class */
  101. protected $oldFileFactory = false;
  102. /** @var array callable|bool Override these in the base class */
  103. protected $fileFactoryKey = false;
  104. /** @var array callable|bool Override these in the base class */
  105. protected $oldFileFactoryKey = false;
  106. /** @var string URL of where to proxy thumb.php requests to.
  107. * Example: http://127.0.0.1:8888/wiki/dev/thumb/
  108. */
  109. protected $thumbProxyUrl;
  110. /** @var string Secret key to pass as an X-Swift-Secret header to the proxied thumb service */
  111. protected $thumbProxySecret;
  112. /**
  113. * @param array|null $info
  114. * @throws MWException
  115. */
  116. public function __construct( array $info = null ) {
  117. // Verify required settings presence
  118. if (
  119. $info === null
  120. || !array_key_exists( 'name', $info )
  121. || !array_key_exists( 'backend', $info )
  122. ) {
  123. throw new MWException( __CLASS__ .
  124. " requires an array of options having both 'name' and 'backend' keys.\n" );
  125. }
  126. // Required settings
  127. $this->name = $info['name'];
  128. if ( $info['backend'] instanceof FileBackend ) {
  129. $this->backend = $info['backend']; // useful for testing
  130. } else {
  131. $this->backend = FileBackendGroup::singleton()->get( $info['backend'] );
  132. }
  133. // Optional settings that can have no value
  134. $optionalSettings = [
  135. 'descBaseUrl', 'scriptDirUrl', 'articleUrl', 'fetchDescription',
  136. 'thumbScriptUrl', 'pathDisclosureProtection', 'descriptionCacheExpiry',
  137. 'favicon', 'thumbProxyUrl', 'thumbProxySecret',
  138. ];
  139. foreach ( $optionalSettings as $var ) {
  140. if ( isset( $info[$var] ) ) {
  141. $this->$var = $info[$var];
  142. }
  143. }
  144. // Optional settings that have a default
  145. $this->initialCapital = isset( $info['initialCapital'] )
  146. ? $info['initialCapital']
  147. : MWNamespace::isCapitalized( NS_FILE );
  148. $this->url = isset( $info['url'] )
  149. ? $info['url']
  150. : false; // a subclass may set the URL (e.g. ForeignAPIRepo)
  151. if ( isset( $info['thumbUrl'] ) ) {
  152. $this->thumbUrl = $info['thumbUrl'];
  153. } else {
  154. $this->thumbUrl = $this->url ? "{$this->url}/thumb" : false;
  155. }
  156. $this->hashLevels = isset( $info['hashLevels'] )
  157. ? $info['hashLevels']
  158. : 2;
  159. $this->deletedHashLevels = isset( $info['deletedHashLevels'] )
  160. ? $info['deletedHashLevels']
  161. : $this->hashLevels;
  162. $this->transformVia404 = !empty( $info['transformVia404'] );
  163. $this->abbrvThreshold = isset( $info['abbrvThreshold'] )
  164. ? $info['abbrvThreshold']
  165. : 255;
  166. $this->isPrivate = !empty( $info['isPrivate'] );
  167. // Give defaults for the basic zones...
  168. $this->zones = isset( $info['zones'] ) ? $info['zones'] : [];
  169. foreach ( [ 'public', 'thumb', 'transcoded', 'temp', 'deleted' ] as $zone ) {
  170. if ( !isset( $this->zones[$zone]['container'] ) ) {
  171. $this->zones[$zone]['container'] = "{$this->name}-{$zone}";
  172. }
  173. if ( !isset( $this->zones[$zone]['directory'] ) ) {
  174. $this->zones[$zone]['directory'] = '';
  175. }
  176. if ( !isset( $this->zones[$zone]['urlsByExt'] ) ) {
  177. $this->zones[$zone]['urlsByExt'] = [];
  178. }
  179. }
  180. $this->supportsSha1URLs = !empty( $info['supportsSha1URLs'] );
  181. }
  182. /**
  183. * Get the file backend instance. Use this function wisely.
  184. *
  185. * @return FileBackend
  186. */
  187. public function getBackend() {
  188. return $this->backend;
  189. }
  190. /**
  191. * Get an explanatory message if this repo is read-only.
  192. * This checks if an administrator disabled writes to the backend.
  193. *
  194. * @return string|bool Returns false if the repo is not read-only
  195. */
  196. public function getReadOnlyReason() {
  197. return $this->backend->getReadOnlyReason();
  198. }
  199. /**
  200. * Check if a single zone or list of zones is defined for usage
  201. *
  202. * @param array $doZones Only do a particular zones
  203. * @throws MWException
  204. * @return Status
  205. */
  206. protected function initZones( $doZones = [] ) {
  207. $status = $this->newGood();
  208. foreach ( (array)$doZones as $zone ) {
  209. $root = $this->getZonePath( $zone );
  210. if ( $root === null ) {
  211. throw new MWException( "No '$zone' zone defined in the {$this->name} repo." );
  212. }
  213. }
  214. return $status;
  215. }
  216. /**
  217. * Determine if a string is an mwrepo:// URL
  218. *
  219. * @param string $url
  220. * @return bool
  221. */
  222. public static function isVirtualUrl( $url ) {
  223. return substr( $url, 0, 9 ) == 'mwrepo://';
  224. }
  225. /**
  226. * Get a URL referring to this repository, with the private mwrepo protocol.
  227. * The suffix, if supplied, is considered to be unencoded, and will be
  228. * URL-encoded before being returned.
  229. *
  230. * @param string|bool $suffix
  231. * @return string
  232. */
  233. public function getVirtualUrl( $suffix = false ) {
  234. $path = 'mwrepo://' . $this->name;
  235. if ( $suffix !== false ) {
  236. $path .= '/' . rawurlencode( $suffix );
  237. }
  238. return $path;
  239. }
  240. /**
  241. * Get the URL corresponding to one of the four basic zones
  242. *
  243. * @param string $zone One of: public, deleted, temp, thumb
  244. * @param string|null $ext Optional file extension
  245. * @return string|bool
  246. */
  247. public function getZoneUrl( $zone, $ext = null ) {
  248. if ( in_array( $zone, [ 'public', 'thumb', 'transcoded' ] ) ) {
  249. // standard public zones
  250. if ( $ext !== null && isset( $this->zones[$zone]['urlsByExt'][$ext] ) ) {
  251. // custom URL for extension/zone
  252. return $this->zones[$zone]['urlsByExt'][$ext];
  253. } elseif ( isset( $this->zones[$zone]['url'] ) ) {
  254. // custom URL for zone
  255. return $this->zones[$zone]['url'];
  256. }
  257. }
  258. switch ( $zone ) {
  259. case 'public':
  260. return $this->url;
  261. case 'temp':
  262. case 'deleted':
  263. return false; // no public URL
  264. case 'thumb':
  265. return $this->thumbUrl;
  266. case 'transcoded':
  267. return "{$this->url}/transcoded";
  268. default:
  269. return false;
  270. }
  271. }
  272. /**
  273. * @return bool Whether non-ASCII path characters are allowed
  274. */
  275. public function backendSupportsUnicodePaths() {
  276. return (bool)( $this->getBackend()->getFeatures() & FileBackend::ATTR_UNICODE_PATHS );
  277. }
  278. /**
  279. * Get the backend storage path corresponding to a virtual URL.
  280. * Use this function wisely.
  281. *
  282. * @param string $url
  283. * @throws MWException
  284. * @return string
  285. */
  286. public function resolveVirtualUrl( $url ) {
  287. if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
  288. throw new MWException( __METHOD__ . ': unknown protocol' );
  289. }
  290. $bits = explode( '/', substr( $url, 9 ), 3 );
  291. if ( count( $bits ) != 3 ) {
  292. throw new MWException( __METHOD__ . ": invalid mwrepo URL: $url" );
  293. }
  294. list( $repo, $zone, $rel ) = $bits;
  295. if ( $repo !== $this->name ) {
  296. throw new MWException( __METHOD__ . ": fetching from a foreign repo is not supported" );
  297. }
  298. $base = $this->getZonePath( $zone );
  299. if ( !$base ) {
  300. throw new MWException( __METHOD__ . ": invalid zone: $zone" );
  301. }
  302. return $base . '/' . rawurldecode( $rel );
  303. }
  304. /**
  305. * The the storage container and base path of a zone
  306. *
  307. * @param string $zone
  308. * @return array (container, base path) or (null, null)
  309. */
  310. protected function getZoneLocation( $zone ) {
  311. if ( !isset( $this->zones[$zone] ) ) {
  312. return [ null, null ]; // bogus
  313. }
  314. return [ $this->zones[$zone]['container'], $this->zones[$zone]['directory'] ];
  315. }
  316. /**
  317. * Get the storage path corresponding to one of the zones
  318. *
  319. * @param string $zone
  320. * @return string|null Returns null if the zone is not defined
  321. */
  322. public function getZonePath( $zone ) {
  323. list( $container, $base ) = $this->getZoneLocation( $zone );
  324. if ( $container === null || $base === null ) {
  325. return null;
  326. }
  327. $backendName = $this->backend->getName();
  328. if ( $base != '' ) { // may not be set
  329. $base = "/{$base}";
  330. }
  331. return "mwstore://$backendName/{$container}{$base}";
  332. }
  333. /**
  334. * Create a new File object from the local repository
  335. *
  336. * @param Title|string $title Title object or string
  337. * @param bool|string $time Time at which the image was uploaded. If this
  338. * is specified, the returned object will be an instance of the
  339. * repository's old file class instead of a current file. Repositories
  340. * not supporting version control should return false if this parameter
  341. * is set.
  342. * @return File|null A File, or null if passed an invalid Title
  343. */
  344. public function newFile( $title, $time = false ) {
  345. $title = File::normalizeTitle( $title );
  346. if ( !$title ) {
  347. return null;
  348. }
  349. if ( $time ) {
  350. if ( $this->oldFileFactory ) {
  351. return call_user_func( $this->oldFileFactory, $title, $this, $time );
  352. } else {
  353. return null;
  354. }
  355. } else {
  356. return call_user_func( $this->fileFactory, $title, $this );
  357. }
  358. }
  359. /**
  360. * Find an instance of the named file created at the specified time
  361. * Returns false if the file does not exist. Repositories not supporting
  362. * version control should return false if the time is specified.
  363. *
  364. * @param Title|string $title Title object or string
  365. * @param array $options Associative array of options:
  366. * time: requested time for a specific file version, or false for the
  367. * current version. An image object will be returned which was
  368. * created at the specified time (which may be archived or current).
  369. * ignoreRedirect: If true, do not follow file redirects
  370. * private: If true, return restricted (deleted) files if the current
  371. * user is allowed to view them. Otherwise, such files will not
  372. * be found. If a User object, use that user instead of the current.
  373. * latest: If true, load from the latest available data into File objects
  374. * @return File|bool False on failure
  375. */
  376. public function findFile( $title, $options = [] ) {
  377. $title = File::normalizeTitle( $title );
  378. if ( !$title ) {
  379. return false;
  380. }
  381. if ( isset( $options['bypassCache'] ) ) {
  382. $options['latest'] = $options['bypassCache']; // b/c
  383. }
  384. $time = isset( $options['time'] ) ? $options['time'] : false;
  385. $flags = !empty( $options['latest'] ) ? File::READ_LATEST : 0;
  386. # First try the current version of the file to see if it precedes the timestamp
  387. $img = $this->newFile( $title );
  388. if ( !$img ) {
  389. return false;
  390. }
  391. $img->load( $flags );
  392. if ( $img->exists() && ( !$time || $img->getTimestamp() == $time ) ) {
  393. return $img;
  394. }
  395. # Now try an old version of the file
  396. if ( $time !== false ) {
  397. $img = $this->newFile( $title, $time );
  398. if ( $img ) {
  399. $img->load( $flags );
  400. if ( $img->exists() ) {
  401. if ( !$img->isDeleted( File::DELETED_FILE ) ) {
  402. return $img; // always OK
  403. } elseif ( !empty( $options['private'] ) &&
  404. $img->userCan( File::DELETED_FILE,
  405. $options['private'] instanceof User ? $options['private'] : null
  406. )
  407. ) {
  408. return $img;
  409. }
  410. }
  411. }
  412. }
  413. # Now try redirects
  414. if ( !empty( $options['ignoreRedirect'] ) ) {
  415. return false;
  416. }
  417. $redir = $this->checkRedirect( $title );
  418. if ( $redir && $title->getNamespace() == NS_FILE ) {
  419. $img = $this->newFile( $redir );
  420. if ( !$img ) {
  421. return false;
  422. }
  423. $img->load( $flags );
  424. if ( $img->exists() ) {
  425. $img->redirectedFrom( $title->getDBkey() );
  426. return $img;
  427. }
  428. }
  429. return false;
  430. }
  431. /**
  432. * Find many files at once.
  433. *
  434. * @param array $items An array of titles, or an array of findFile() options with
  435. * the "title" option giving the title. Example:
  436. *
  437. * $findItem = [ 'title' => $title, 'private' => true ];
  438. * $findBatch = [ $findItem ];
  439. * $repo->findFiles( $findBatch );
  440. *
  441. * No title should appear in $items twice, as the result use titles as keys
  442. * @param int $flags Supports:
  443. * - FileRepo::NAME_AND_TIME_ONLY : return a (search title => (title,timestamp)) map.
  444. * The search title uses the input titles; the other is the final post-redirect title.
  445. * All titles are returned as string DB keys and the inner array is associative.
  446. * @return array Map of (file name => File objects) for matches
  447. */
  448. public function findFiles( array $items, $flags = 0 ) {
  449. $result = [];
  450. foreach ( $items as $item ) {
  451. if ( is_array( $item ) ) {
  452. $title = $item['title'];
  453. $options = $item;
  454. unset( $options['title'] );
  455. } else {
  456. $title = $item;
  457. $options = [];
  458. }
  459. $file = $this->findFile( $title, $options );
  460. if ( $file ) {
  461. $searchName = File::normalizeTitle( $title )->getDBkey(); // must be valid
  462. if ( $flags & self::NAME_AND_TIME_ONLY ) {
  463. $result[$searchName] = [
  464. 'title' => $file->getTitle()->getDBkey(),
  465. 'timestamp' => $file->getTimestamp()
  466. ];
  467. } else {
  468. $result[$searchName] = $file;
  469. }
  470. }
  471. }
  472. return $result;
  473. }
  474. /**
  475. * Find an instance of the file with this key, created at the specified time
  476. * Returns false if the file does not exist. Repositories not supporting
  477. * version control should return false if the time is specified.
  478. *
  479. * @param string $sha1 Base 36 SHA-1 hash
  480. * @param array $options Option array, same as findFile().
  481. * @return File|bool False on failure
  482. */
  483. public function findFileFromKey( $sha1, $options = [] ) {
  484. $time = isset( $options['time'] ) ? $options['time'] : false;
  485. # First try to find a matching current version of a file...
  486. if ( !$this->fileFactoryKey ) {
  487. return false; // find-by-sha1 not supported
  488. }
  489. $img = call_user_func( $this->fileFactoryKey, $sha1, $this, $time );
  490. if ( $img && $img->exists() ) {
  491. return $img;
  492. }
  493. # Now try to find a matching old version of a file...
  494. if ( $time !== false && $this->oldFileFactoryKey ) { // find-by-sha1 supported?
  495. $img = call_user_func( $this->oldFileFactoryKey, $sha1, $this, $time );
  496. if ( $img && $img->exists() ) {
  497. if ( !$img->isDeleted( File::DELETED_FILE ) ) {
  498. return $img; // always OK
  499. } elseif ( !empty( $options['private'] ) &&
  500. $img->userCan( File::DELETED_FILE,
  501. $options['private'] instanceof User ? $options['private'] : null
  502. )
  503. ) {
  504. return $img;
  505. }
  506. }
  507. }
  508. return false;
  509. }
  510. /**
  511. * Get an array or iterator of file objects for files that have a given
  512. * SHA-1 content hash.
  513. *
  514. * STUB
  515. * @param string $hash SHA-1 hash
  516. * @return File[]
  517. */
  518. public function findBySha1( $hash ) {
  519. return [];
  520. }
  521. /**
  522. * Get an array of arrays or iterators of file objects for files that
  523. * have the given SHA-1 content hashes.
  524. *
  525. * @param string[] $hashes An array of hashes
  526. * @return array[] An Array of arrays or iterators of file objects and the hash as key
  527. */
  528. public function findBySha1s( array $hashes ) {
  529. $result = [];
  530. foreach ( $hashes as $hash ) {
  531. $files = $this->findBySha1( $hash );
  532. if ( count( $files ) ) {
  533. $result[$hash] = $files;
  534. }
  535. }
  536. return $result;
  537. }
  538. /**
  539. * Return an array of files where the name starts with $prefix.
  540. *
  541. * STUB
  542. * @param string $prefix The prefix to search for
  543. * @param int $limit The maximum amount of files to return
  544. * @return LocalFile[]
  545. */
  546. public function findFilesByPrefix( $prefix, $limit ) {
  547. return [];
  548. }
  549. /**
  550. * Get the URL of thumb.php
  551. *
  552. * @return string
  553. */
  554. public function getThumbScriptUrl() {
  555. return $this->thumbScriptUrl;
  556. }
  557. /**
  558. * Get the URL thumb.php requests are being proxied to
  559. *
  560. * @return string
  561. */
  562. public function getThumbProxyUrl() {
  563. return $this->thumbProxyUrl;
  564. }
  565. /**
  566. * Get the secret key for the proxied thumb service
  567. *
  568. * @return string
  569. */
  570. public function getThumbProxySecret() {
  571. return $this->thumbProxySecret;
  572. }
  573. /**
  574. * Returns true if the repository can transform files via a 404 handler
  575. *
  576. * @return bool
  577. */
  578. public function canTransformVia404() {
  579. return $this->transformVia404;
  580. }
  581. /**
  582. * Get the name of a file from its title object
  583. *
  584. * @param Title $title
  585. * @return string
  586. */
  587. public function getNameFromTitle( Title $title ) {
  588. global $wgContLang;
  589. if ( $this->initialCapital != MWNamespace::isCapitalized( NS_FILE ) ) {
  590. $name = $title->getUserCaseDBKey();
  591. if ( $this->initialCapital ) {
  592. $name = $wgContLang->ucfirst( $name );
  593. }
  594. } else {
  595. $name = $title->getDBkey();
  596. }
  597. return $name;
  598. }
  599. /**
  600. * Get the public zone root storage directory of the repository
  601. *
  602. * @return string
  603. */
  604. public function getRootDirectory() {
  605. return $this->getZonePath( 'public' );
  606. }
  607. /**
  608. * Get a relative path including trailing slash, e.g. f/fa/
  609. * If the repo is not hashed, returns an empty string
  610. *
  611. * @param string $name Name of file
  612. * @return string
  613. */
  614. public function getHashPath( $name ) {
  615. return self::getHashPathForLevel( $name, $this->hashLevels );
  616. }
  617. /**
  618. * Get a relative path including trailing slash, e.g. f/fa/
  619. * If the repo is not hashed, returns an empty string
  620. *
  621. * @param string $suffix Basename of file from FileRepo::storeTemp()
  622. * @return string
  623. */
  624. public function getTempHashPath( $suffix ) {
  625. $parts = explode( '!', $suffix, 2 ); // format is <timestamp>!<name> or just <name>
  626. $name = isset( $parts[1] ) ? $parts[1] : $suffix; // hash path is not based on timestamp
  627. return self::getHashPathForLevel( $name, $this->hashLevels );
  628. }
  629. /**
  630. * @param string $name
  631. * @param int $levels
  632. * @return string
  633. */
  634. protected static function getHashPathForLevel( $name, $levels ) {
  635. if ( $levels == 0 ) {
  636. return '';
  637. } else {
  638. $hash = md5( $name );
  639. $path = '';
  640. for ( $i = 1; $i <= $levels; $i++ ) {
  641. $path .= substr( $hash, 0, $i ) . '/';
  642. }
  643. return $path;
  644. }
  645. }
  646. /**
  647. * Get the number of hash directory levels
  648. *
  649. * @return int
  650. */
  651. public function getHashLevels() {
  652. return $this->hashLevels;
  653. }
  654. /**
  655. * Get the name of this repository, as specified by $info['name]' to the constructor
  656. *
  657. * @return string
  658. */
  659. public function getName() {
  660. return $this->name;
  661. }
  662. /**
  663. * Make an url to this repo
  664. *
  665. * @param string $query Query string to append
  666. * @param string $entry Entry point; defaults to index
  667. * @return string|bool False on failure
  668. */
  669. public function makeUrl( $query = '', $entry = 'index' ) {
  670. if ( isset( $this->scriptDirUrl ) ) {
  671. return wfAppendQuery( "{$this->scriptDirUrl}/{$entry}.php", $query );
  672. }
  673. return false;
  674. }
  675. /**
  676. * Get the URL of an image description page. May return false if it is
  677. * unknown or not applicable. In general this should only be called by the
  678. * File class, since it may return invalid results for certain kinds of
  679. * repositories. Use File::getDescriptionUrl() in user code.
  680. *
  681. * In particular, it uses the article paths as specified to the repository
  682. * constructor, whereas local repositories use the local Title functions.
  683. *
  684. * @param string $name
  685. * @return string|false
  686. */
  687. public function getDescriptionUrl( $name ) {
  688. $encName = wfUrlencode( $name );
  689. if ( !is_null( $this->descBaseUrl ) ) {
  690. # "http://example.com/wiki/File:"
  691. return $this->descBaseUrl . $encName;
  692. }
  693. if ( !is_null( $this->articleUrl ) ) {
  694. # "http://example.com/wiki/$1"
  695. # We use "Image:" as the canonical namespace for
  696. # compatibility across all MediaWiki versions.
  697. return str_replace( '$1',
  698. "Image:$encName", $this->articleUrl );
  699. }
  700. if ( !is_null( $this->scriptDirUrl ) ) {
  701. # "http://example.com/w"
  702. # We use "Image:" as the canonical namespace for
  703. # compatibility across all MediaWiki versions,
  704. # and just sort of hope index.php is right. ;)
  705. return $this->makeUrl( "title=Image:$encName" );
  706. }
  707. return false;
  708. }
  709. /**
  710. * Get the URL of the content-only fragment of the description page. For
  711. * MediaWiki this means action=render. This should only be called by the
  712. * repository's file class, since it may return invalid results. User code
  713. * should use File::getDescriptionText().
  714. *
  715. * @param string $name Name of image to fetch
  716. * @param string|null $lang Language to fetch it in, if any.
  717. * @return string|false
  718. */
  719. public function getDescriptionRenderUrl( $name, $lang = null ) {
  720. $query = 'action=render';
  721. if ( !is_null( $lang ) ) {
  722. $query .= '&uselang=' . urlencode( $lang );
  723. }
  724. if ( isset( $this->scriptDirUrl ) ) {
  725. return $this->makeUrl(
  726. 'title=' .
  727. wfUrlencode( 'Image:' . $name ) .
  728. "&$query" );
  729. } else {
  730. $descUrl = $this->getDescriptionUrl( $name );
  731. if ( $descUrl ) {
  732. return wfAppendQuery( $descUrl, $query );
  733. } else {
  734. return false;
  735. }
  736. }
  737. }
  738. /**
  739. * Get the URL of the stylesheet to apply to description pages
  740. *
  741. * @return string|bool False on failure
  742. */
  743. public function getDescriptionStylesheetUrl() {
  744. if ( isset( $this->scriptDirUrl ) ) {
  745. return $this->makeUrl( 'title=MediaWiki:Filepage.css&' .
  746. wfArrayToCgi( Skin::getDynamicStylesheetQuery() ) );
  747. }
  748. return false;
  749. }
  750. /**
  751. * Store a file to a given destination.
  752. *
  753. * @param string $srcPath Source file system path, storage path, or virtual URL
  754. * @param string $dstZone Destination zone
  755. * @param string $dstRel Destination relative path
  756. * @param int $flags Bitwise combination of the following flags:
  757. * self::OVERWRITE Overwrite an existing destination file instead of failing
  758. * self::OVERWRITE_SAME Overwrite the file if the destination exists and has the
  759. * same contents as the source
  760. * self::SKIP_LOCKING Skip any file locking when doing the store
  761. * @return Status
  762. */
  763. public function store( $srcPath, $dstZone, $dstRel, $flags = 0 ) {
  764. $this->assertWritableRepo(); // fail out if read-only
  765. $status = $this->storeBatch( [ [ $srcPath, $dstZone, $dstRel ] ], $flags );
  766. if ( $status->successCount == 0 ) {
  767. $status->setOK( false );
  768. }
  769. return $status;
  770. }
  771. /**
  772. * Store a batch of files
  773. *
  774. * @param array $triplets (src, dest zone, dest rel) triplets as per store()
  775. * @param int $flags Bitwise combination of the following flags:
  776. * self::OVERWRITE Overwrite an existing destination file instead of failing
  777. * self::OVERWRITE_SAME Overwrite the file if the destination exists and has the
  778. * same contents as the source
  779. * self::SKIP_LOCKING Skip any file locking when doing the store
  780. * @throws MWException
  781. * @return Status
  782. */
  783. public function storeBatch( array $triplets, $flags = 0 ) {
  784. $this->assertWritableRepo(); // fail out if read-only
  785. if ( $flags & self::DELETE_SOURCE ) {
  786. throw new InvalidArgumentException( "DELETE_SOURCE not supported in " . __METHOD__ );
  787. }
  788. $status = $this->newGood();
  789. $backend = $this->backend; // convenience
  790. $operations = [];
  791. // Validate each triplet and get the store operation...
  792. foreach ( $triplets as $triplet ) {
  793. list( $srcPath, $dstZone, $dstRel ) = $triplet;
  794. wfDebug( __METHOD__
  795. . "( \$src='$srcPath', \$dstZone='$dstZone', \$dstRel='$dstRel' )\n"
  796. );
  797. // Resolve destination path
  798. $root = $this->getZonePath( $dstZone );
  799. if ( !$root ) {
  800. throw new MWException( "Invalid zone: $dstZone" );
  801. }
  802. if ( !$this->validateFilename( $dstRel ) ) {
  803. throw new MWException( 'Validation error in $dstRel' );
  804. }
  805. $dstPath = "$root/$dstRel";
  806. $dstDir = dirname( $dstPath );
  807. // Create destination directories for this triplet
  808. if ( !$this->initDirectory( $dstDir )->isOK() ) {
  809. return $this->newFatal( 'directorycreateerror', $dstDir );
  810. }
  811. // Resolve source to a storage path if virtual
  812. $srcPath = $this->resolveToStoragePath( $srcPath );
  813. // Get the appropriate file operation
  814. if ( FileBackend::isStoragePath( $srcPath ) ) {
  815. $opName = 'copy';
  816. } else {
  817. $opName = 'store';
  818. }
  819. $operations[] = [
  820. 'op' => $opName,
  821. 'src' => $srcPath,
  822. 'dst' => $dstPath,
  823. 'overwrite' => $flags & self::OVERWRITE,
  824. 'overwriteSame' => $flags & self::OVERWRITE_SAME,
  825. ];
  826. }
  827. // Execute the store operation for each triplet
  828. $opts = [ 'force' => true ];
  829. if ( $flags & self::SKIP_LOCKING ) {
  830. $opts['nonLocking'] = true;
  831. }
  832. $status->merge( $backend->doOperations( $operations, $opts ) );
  833. return $status;
  834. }
  835. /**
  836. * Deletes a batch of files.
  837. * Each file can be a (zone, rel) pair, virtual url, storage path.
  838. * It will try to delete each file, but ignores any errors that may occur.
  839. *
  840. * @param string[] $files List of files to delete
  841. * @param int $flags Bitwise combination of the following flags:
  842. * self::SKIP_LOCKING Skip any file locking when doing the deletions
  843. * @return Status
  844. */
  845. public function cleanupBatch( array $files, $flags = 0 ) {
  846. $this->assertWritableRepo(); // fail out if read-only
  847. $status = $this->newGood();
  848. $operations = [];
  849. foreach ( $files as $path ) {
  850. if ( is_array( $path ) ) {
  851. // This is a pair, extract it
  852. list( $zone, $rel ) = $path;
  853. $path = $this->getZonePath( $zone ) . "/$rel";
  854. } else {
  855. // Resolve source to a storage path if virtual
  856. $path = $this->resolveToStoragePath( $path );
  857. }
  858. $operations[] = [ 'op' => 'delete', 'src' => $path ];
  859. }
  860. // Actually delete files from storage...
  861. $opts = [ 'force' => true ];
  862. if ( $flags & self::SKIP_LOCKING ) {
  863. $opts['nonLocking'] = true;
  864. }
  865. $status->merge( $this->backend->doOperations( $operations, $opts ) );
  866. return $status;
  867. }
  868. /**
  869. * Import a file from the local file system into the repo.
  870. * This does no locking nor journaling and overrides existing files.
  871. * This function can be used to write to otherwise read-only foreign repos.
  872. * This is intended for copying generated thumbnails into the repo.
  873. *
  874. * @param string|FSFile $src Source file system path, storage path, or virtual URL
  875. * @param string $dst Virtual URL or storage path
  876. * @param array|string|null $options An array consisting of a key named headers
  877. * listing extra headers. If a string, taken as content-disposition header.
  878. * (Support for array of options new in 1.23)
  879. * @return Status
  880. */
  881. final public function quickImport( $src, $dst, $options = null ) {
  882. return $this->quickImportBatch( [ [ $src, $dst, $options ] ] );
  883. }
  884. /**
  885. * Purge a file from the repo. This does no locking nor journaling.
  886. * This function can be used to write to otherwise read-only foreign repos.
  887. * This is intended for purging thumbnails.
  888. *
  889. * @param string $path Virtual URL or storage path
  890. * @return Status
  891. */
  892. final public function quickPurge( $path ) {
  893. return $this->quickPurgeBatch( [ $path ] );
  894. }
  895. /**
  896. * Deletes a directory if empty.
  897. * This function can be used to write to otherwise read-only foreign repos.
  898. *
  899. * @param string $dir Virtual URL (or storage path) of directory to clean
  900. * @return Status
  901. */
  902. public function quickCleanDir( $dir ) {
  903. $status = $this->newGood();
  904. $status->merge( $this->backend->clean(
  905. [ 'dir' => $this->resolveToStoragePath( $dir ) ] ) );
  906. return $status;
  907. }
  908. /**
  909. * Import a batch of files from the local file system into the repo.
  910. * This does no locking nor journaling and overrides existing files.
  911. * This function can be used to write to otherwise read-only foreign repos.
  912. * This is intended for copying generated thumbnails into the repo.
  913. *
  914. * All path parameters may be a file system path, storage path, or virtual URL.
  915. * When "headers" are given they are used as HTTP headers if supported.
  916. *
  917. * @param array $triples List of (source path or FSFile, destination path, disposition)
  918. * @return Status
  919. */
  920. public function quickImportBatch( array $triples ) {
  921. $status = $this->newGood();
  922. $operations = [];
  923. foreach ( $triples as $triple ) {
  924. list( $src, $dst ) = $triple;
  925. if ( $src instanceof FSFile ) {
  926. $op = 'store';
  927. } else {
  928. $src = $this->resolveToStoragePath( $src );
  929. $op = FileBackend::isStoragePath( $src ) ? 'copy' : 'store';
  930. }
  931. $dst = $this->resolveToStoragePath( $dst );
  932. if ( !isset( $triple[2] ) ) {
  933. $headers = [];
  934. } elseif ( is_string( $triple[2] ) ) {
  935. // back-compat
  936. $headers = [ 'Content-Disposition' => $triple[2] ];
  937. } elseif ( is_array( $triple[2] ) && isset( $triple[2]['headers'] ) ) {
  938. $headers = $triple[2]['headers'];
  939. } else {
  940. $headers = [];
  941. }
  942. $operations[] = [
  943. 'op' => $op,
  944. 'src' => $src,
  945. 'dst' => $dst,
  946. 'headers' => $headers
  947. ];
  948. $status->merge( $this->initDirectory( dirname( $dst ) ) );
  949. }
  950. $status->merge( $this->backend->doQuickOperations( $operations ) );
  951. return $status;
  952. }
  953. /**
  954. * Purge a batch of files from the repo.
  955. * This function can be used to write to otherwise read-only foreign repos.
  956. * This does no locking nor journaling and is intended for purging thumbnails.
  957. *
  958. * @param array $paths List of virtual URLs or storage paths
  959. * @return Status
  960. */
  961. public function quickPurgeBatch( array $paths ) {
  962. $status = $this->newGood();
  963. $operations = [];
  964. foreach ( $paths as $path ) {
  965. $operations[] = [
  966. 'op' => 'delete',
  967. 'src' => $this->resolveToStoragePath( $path ),
  968. 'ignoreMissingSource' => true
  969. ];
  970. }
  971. $status->merge( $this->backend->doQuickOperations( $operations ) );
  972. return $status;
  973. }
  974. /**
  975. * Pick a random name in the temp zone and store a file to it.
  976. * Returns a Status object with the file Virtual URL in the value,
  977. * file can later be disposed using FileRepo::freeTemp().
  978. *
  979. * @param string $originalName The base name of the file as specified
  980. * by the user. The file extension will be maintained.
  981. * @param string $srcPath The current location of the file.
  982. * @return Status Object with the URL in the value.
  983. */
  984. public function storeTemp( $originalName, $srcPath ) {
  985. $this->assertWritableRepo(); // fail out if read-only
  986. $date = MWTimestamp::getInstance()->format( 'YmdHis' );
  987. $hashPath = $this->getHashPath( $originalName );
  988. $dstUrlRel = $hashPath . $date . '!' . rawurlencode( $originalName );
  989. $virtualUrl = $this->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
  990. $result = $this->quickImport( $srcPath, $virtualUrl );
  991. $result->value = $virtualUrl;
  992. return $result;
  993. }
  994. /**
  995. * Remove a temporary file or mark it for garbage collection
  996. *
  997. * @param string $virtualUrl The virtual URL returned by FileRepo::storeTemp()
  998. * @return bool True on success, false on failure
  999. */
  1000. public function freeTemp( $virtualUrl ) {
  1001. $this->assertWritableRepo(); // fail out if read-only
  1002. $temp = $this->getVirtualUrl( 'temp' );
  1003. if ( substr( $virtualUrl, 0, strlen( $temp ) ) != $temp ) {
  1004. wfDebug( __METHOD__ . ": Invalid temp virtual URL\n" );
  1005. return false;
  1006. }
  1007. return $this->quickPurge( $virtualUrl )->isOK();
  1008. }
  1009. /**
  1010. * Concatenate a list of temporary files into a target file location.
  1011. *
  1012. * @param array $srcPaths Ordered list of source virtual URLs/storage paths
  1013. * @param string $dstPath Target file system path
  1014. * @param int $flags Bitwise combination of the following flags:
  1015. * self::DELETE_SOURCE Delete the source files on success
  1016. * @return Status
  1017. */
  1018. public function concatenate( array $srcPaths, $dstPath, $flags = 0 ) {
  1019. $this->assertWritableRepo(); // fail out if read-only
  1020. $status = $this->newGood();
  1021. $sources = [];
  1022. foreach ( $srcPaths as $srcPath ) {
  1023. // Resolve source to a storage path if virtual
  1024. $source = $this->resolveToStoragePath( $srcPath );
  1025. $sources[] = $source; // chunk to merge
  1026. }
  1027. // Concatenate the chunks into one FS file
  1028. $params = [ 'srcs' => $sources, 'dst' => $dstPath ];
  1029. $status->merge( $this->backend->concatenate( $params ) );
  1030. if ( !$status->isOK() ) {
  1031. return $status;
  1032. }
  1033. // Delete the sources if required
  1034. if ( $flags & self::DELETE_SOURCE ) {
  1035. $status->merge( $this->quickPurgeBatch( $srcPaths ) );
  1036. }
  1037. // Make sure status is OK, despite any quickPurgeBatch() fatals
  1038. $status->setResult( true );
  1039. return $status;
  1040. }
  1041. /**
  1042. * Copy or move a file either from a storage path, virtual URL,
  1043. * or file system path, into this repository at the specified destination location.
  1044. *
  1045. * Returns a Status object. On success, the value contains "new" or
  1046. * "archived", to indicate whether the file was new with that name.
  1047. *
  1048. * Options to $options include:
  1049. * - headers : name/value map of HTTP headers to use in response to GET/HEAD requests
  1050. *
  1051. * @param string|FSFile $src The source file system path, storage path, or URL
  1052. * @param string $dstRel The destination relative path
  1053. * @param string $archiveRel The relative path where the existing file is to
  1054. * be archived, if there is one. Relative to the public zone root.
  1055. * @param int $flags Bitfield, may be FileRepo::DELETE_SOURCE to indicate
  1056. * that the source file should be deleted if possible
  1057. * @param array $options Optional additional parameters
  1058. * @return Status
  1059. */
  1060. public function publish(
  1061. $src, $dstRel, $archiveRel, $flags = 0, array $options = []
  1062. ) {
  1063. $this->assertWritableRepo(); // fail out if read-only
  1064. $status = $this->publishBatch(
  1065. [ [ $src, $dstRel, $archiveRel, $options ] ], $flags );
  1066. if ( $status->successCount == 0 ) {
  1067. $status->setOK( false );
  1068. }
  1069. if ( isset( $status->value[0] ) ) {
  1070. $status->value = $status->value[0];
  1071. } else {
  1072. $status->value = false;
  1073. }
  1074. return $status;
  1075. }
  1076. /**
  1077. * Publish a batch of files
  1078. *
  1079. * @param array $ntuples (source, dest, archive) triplets or
  1080. * (source, dest, archive, options) 4-tuples as per publish().
  1081. * @param int $flags Bitfield, may be FileRepo::DELETE_SOURCE to indicate
  1082. * that the source files should be deleted if possible
  1083. * @throws MWException
  1084. * @return Status
  1085. */
  1086. public function publishBatch( array $ntuples, $flags = 0 ) {
  1087. $this->assertWritableRepo(); // fail out if read-only
  1088. $backend = $this->backend; // convenience
  1089. // Try creating directories
  1090. $status = $this->initZones( 'public' );
  1091. if ( !$status->isOK() ) {
  1092. return $status;
  1093. }
  1094. $status = $this->newGood( [] );
  1095. $operations = [];
  1096. $sourceFSFilesToDelete = []; // cleanup for disk source files
  1097. // Validate each triplet and get the store operation...
  1098. foreach ( $ntuples as $ntuple ) {
  1099. list( $src, $dstRel, $archiveRel ) = $ntuple;
  1100. $srcPath = ( $src instanceof FSFile ) ? $src->getPath() : $src;
  1101. $options = isset( $ntuple[3] ) ? $ntuple[3] : [];
  1102. // Resolve source to a storage path if virtual
  1103. $srcPath = $this->resolveToStoragePath( $srcPath );
  1104. if ( !$this->validateFilename( $dstRel ) ) {
  1105. throw new MWException( 'Validation error in $dstRel' );
  1106. }
  1107. if ( !$this->validateFilename( $archiveRel ) ) {
  1108. throw new MWException( 'Validation error in $archiveRel' );
  1109. }
  1110. $publicRoot = $this->getZonePath( 'public' );
  1111. $dstPath = "$publicRoot/$dstRel";
  1112. $archivePath = "$publicRoot/$archiveRel";
  1113. $dstDir = dirname( $dstPath );
  1114. $archiveDir = dirname( $archivePath );
  1115. // Abort immediately on directory creation errors since they're likely to be repetitive
  1116. if ( !$this->initDirectory( $dstDir )->isOK() ) {
  1117. return $this->newFatal( 'directorycreateerror', $dstDir );
  1118. }
  1119. if ( !$this->initDirectory( $archiveDir )->isOK() ) {
  1120. return $this->newFatal( 'directorycreateerror', $archiveDir );
  1121. }
  1122. // Set any desired headers to be use in GET/HEAD responses
  1123. $headers = isset( $options['headers'] ) ? $options['headers'] : [];
  1124. // Archive destination file if it exists.
  1125. // This will check if the archive file also exists and fail if does.
  1126. // This is a sanity check to avoid data loss. On Windows and Linux,
  1127. // copy() will overwrite, so the existence check is vulnerable to
  1128. // race conditions unless a functioning LockManager is used.
  1129. // LocalFile also uses SELECT FOR UPDATE for synchronization.
  1130. $operations[] = [
  1131. 'op' => 'copy',
  1132. 'src' => $dstPath,
  1133. 'dst' => $archivePath,
  1134. 'ignoreMissingSource' => true
  1135. ];
  1136. // Copy (or move) the source file to the destination
  1137. if ( FileBackend::isStoragePath( $srcPath ) ) {
  1138. if ( $flags & self::DELETE_SOURCE ) {
  1139. $operations[] = [
  1140. 'op' => 'move',
  1141. 'src' => $srcPath,
  1142. 'dst' => $dstPath,
  1143. 'overwrite' => true, // replace current
  1144. 'headers' => $headers
  1145. ];
  1146. } else {
  1147. $operations[] = [
  1148. 'op' => 'copy',
  1149. 'src' => $srcPath,
  1150. 'dst' => $dstPath,
  1151. 'overwrite' => true, // replace current
  1152. 'headers' => $headers
  1153. ];
  1154. }
  1155. } else { // FS source path
  1156. $operations[] = [
  1157. 'op' => 'store',
  1158. 'src' => $src, // prefer FSFile objects
  1159. 'dst' => $dstPath,
  1160. 'overwrite' => true, // replace current
  1161. 'headers' => $headers
  1162. ];
  1163. if ( $flags & self::DELETE_SOURCE ) {
  1164. $sourceFSFilesToDelete[] = $srcPath;
  1165. }
  1166. }
  1167. }
  1168. // Execute the operations for each triplet
  1169. $status->merge( $backend->doOperations( $operations ) );
  1170. // Find out which files were archived...
  1171. foreach ( $ntuples as $i => $ntuple ) {
  1172. list( , , $archiveRel ) = $ntuple;
  1173. $archivePath = $this->getZonePath( 'public' ) . "/$archiveRel";
  1174. if ( $this->fileExists( $archivePath ) ) {
  1175. $status->value[$i] = 'archived';
  1176. } else {
  1177. $status->value[$i] = 'new';
  1178. }
  1179. }
  1180. // Cleanup for disk source files...
  1181. foreach ( $sourceFSFilesToDelete as $file ) {
  1182. Wikimedia\suppressWarnings();
  1183. unlink( $file ); // FS cleanup
  1184. Wikimedia\restoreWarnings();
  1185. }
  1186. return $status;
  1187. }
  1188. /**
  1189. * Creates a directory with the appropriate zone permissions.
  1190. * Callers are responsible for doing read-only and "writable repo" checks.
  1191. *
  1192. * @param string $dir Virtual URL (or storage path) of directory to clean
  1193. * @return Status
  1194. */
  1195. protected function initDirectory( $dir ) {
  1196. $path = $this->resolveToStoragePath( $dir );
  1197. list( , $container, ) = FileBackend::splitStoragePath( $path );
  1198. $params = [ 'dir' => $path ];
  1199. if ( $this->isPrivate
  1200. || $container === $this->zones['deleted']['container']
  1201. || $container === $this->zones['temp']['container']
  1202. ) {
  1203. # Take all available measures to prevent web accessibility of new deleted
  1204. # directories, in case the user has not configured offline storage
  1205. $params = [ 'noAccess' => true, 'noListing' => true ] + $params;
  1206. }
  1207. $status = $this->newGood();
  1208. $status->merge( $this->backend->prepare( $params ) );
  1209. return $status;
  1210. }
  1211. /**
  1212. * Deletes a directory if empty.
  1213. *
  1214. * @param string $dir Virtual URL (or storage path) of directory to clean
  1215. * @return Status
  1216. */
  1217. public function cleanDir( $dir ) {
  1218. $this->assertWritableRepo(); // fail out if read-only
  1219. $status = $this->newGood();
  1220. $status->merge( $this->backend->clean(
  1221. [ 'dir' => $this->resolveToStoragePath( $dir ) ] ) );
  1222. return $status;
  1223. }
  1224. /**
  1225. * Checks existence of a a file
  1226. *
  1227. * @param string $file Virtual URL (or storage path) of file to check
  1228. * @return bool
  1229. */
  1230. public function fileExists( $file ) {
  1231. $result = $this->fileExistsBatch( [ $file ] );
  1232. return $result[0];
  1233. }
  1234. /**
  1235. * Checks existence of an array of files.
  1236. *
  1237. * @param string[] $files Virtual URLs (or storage paths) of files to check
  1238. * @return array Map of files and existence flags, or false
  1239. */
  1240. public function fileExistsBatch( array $files ) {
  1241. $paths = array_map( [ $this, 'resolveToStoragePath' ], $files );
  1242. $this->backend->preloadFileStat( [ 'srcs' => $paths ] );
  1243. $result = [];
  1244. foreach ( $files as $key => $file ) {
  1245. $path = $this->resolveToStoragePath( $file );
  1246. $result[$key] = $this->backend->fileExists( [ 'src' => $path ] );
  1247. }
  1248. return $result;
  1249. }
  1250. /**
  1251. * Move a file to the deletion archive.
  1252. * If no valid deletion archive exists, this may either delete the file
  1253. * or throw an exception, depending on the preference of the repository
  1254. *
  1255. * @param mixed $srcRel Relative path for the file to be deleted
  1256. * @param mixed $archiveRel Relative path for the archive location.
  1257. * Relative to a private archive directory.
  1258. * @return Status
  1259. */
  1260. public function delete( $srcRel, $archiveRel ) {
  1261. $this->assertWritableRepo(); // fail out if read-only
  1262. return $this->deleteBatch( [ [ $srcRel, $archiveRel ] ] );
  1263. }
  1264. /**
  1265. * Move a group of files to the deletion archive.
  1266. *
  1267. * If no valid deletion archive is configured, this may either delete the
  1268. * file or throw an exception, depending on the preference of the repository.
  1269. *
  1270. * The overwrite policy is determined by the repository -- currently LocalRepo
  1271. * assumes a naming scheme in the deleted zone based on content hash, as
  1272. * opposed to the public zone which is assumed to be unique.
  1273. *
  1274. * @param array $sourceDestPairs Array of source/destination pairs. Each element
  1275. * is a two-element array containing the source file path relative to the
  1276. * public root in the first element, and the archive file path relative
  1277. * to the deleted zone root in the second element.
  1278. * @throws MWException
  1279. * @return Status
  1280. */
  1281. public function deleteBatch( array $sourceDestPairs ) {
  1282. $this->assertWritableRepo(); // fail out if read-only
  1283. // Try creating directories
  1284. $status = $this->initZones( [ 'public', 'deleted' ] );
  1285. if ( !$status->isOK() ) {
  1286. return $status;
  1287. }
  1288. $status = $this->newGood();
  1289. $backend = $this->backend; // convenience
  1290. $operations = [];
  1291. // Validate filenames and create archive directories
  1292. foreach ( $sourceDestPairs as $pair ) {
  1293. list( $srcRel, $archiveRel ) = $pair;
  1294. if ( !$this->validateFilename( $srcRel ) ) {
  1295. throw new MWException( __METHOD__ . ':Validation error in $srcRel' );
  1296. } elseif ( !$this->validateFilename( $archiveRel ) ) {
  1297. throw new MWException( __METHOD__ . ':Validation error in $archiveRel' );
  1298. }
  1299. $publicRoot = $this->getZonePath( 'public' );
  1300. $srcPath = "{$publicRoot}/$srcRel";
  1301. $deletedRoot = $this->getZonePath( 'deleted' );
  1302. $archivePath = "{$deletedRoot}/{$archiveRel}";
  1303. $archiveDir = dirname( $archivePath ); // does not touch FS
  1304. // Create destination directories
  1305. if ( !$this->initDirectory( $archiveDir )->isOK() ) {
  1306. return $this->newFatal( 'directorycreateerror', $archiveDir );
  1307. }
  1308. $operations[] = [
  1309. 'op' => 'move',
  1310. 'src' => $srcPath,
  1311. 'dst' => $archivePath,
  1312. // We may have 2+ identical files being deleted,
  1313. // all of which will map to the same destination file
  1314. 'overwriteSame' => true // also see T33792
  1315. ];
  1316. }
  1317. // Move the files by execute the operations for each pair.
  1318. // We're now committed to returning an OK result, which will
  1319. // lead to the files being moved in the DB also.
  1320. $opts = [ 'force' => true ];
  1321. $status->merge( $backend->doOperations( $operations, $opts ) );
  1322. return $status;
  1323. }
  1324. /**
  1325. * Delete files in the deleted directory if they are not referenced in the filearchive table
  1326. *
  1327. * STUB
  1328. * @param string[] $storageKeys
  1329. */
  1330. public function cleanupDeletedBatch( array $storageKeys ) {
  1331. $this->assertWritableRepo();
  1332. }
  1333. /**
  1334. * Get a relative path for a deletion archive key,
  1335. * e.g. s/z/a/ for sza251lrxrc1jad41h5mgilp8nysje52.jpg
  1336. *
  1337. * @param string $key
  1338. * @throws MWException
  1339. * @return string
  1340. */
  1341. public function getDeletedHashPath( $key ) {
  1342. if ( strlen( $key ) < 31 ) {
  1343. throw new MWException( "Invalid storage key '$key'." );
  1344. }
  1345. $path = '';
  1346. for ( $i = 0; $i < $this->deletedHashLevels; $i++ ) {
  1347. $path .= $key[$i] . '/';
  1348. }
  1349. return $path;
  1350. }
  1351. /**
  1352. * If a path is a virtual URL, resolve it to a storage path.
  1353. * Otherwise, just return the path as it is.
  1354. *
  1355. * @param string $path
  1356. * @return string
  1357. * @throws MWException
  1358. */
  1359. protected function resolveToStoragePath( $path ) {
  1360. if ( $this->isVirtualUrl( $path ) ) {
  1361. return $this->resolveVirtualUrl( $path );
  1362. }
  1363. return $path;
  1364. }
  1365. /**
  1366. * Get a local FS copy of a file with a given virtual URL/storage path.
  1367. * Temporary files may be purged when the file object falls out of scope.
  1368. *
  1369. * @param string $virtualUrl
  1370. * @return TempFSFile|null Returns null on failure
  1371. */
  1372. public function getLocalCopy( $virtualUrl ) {
  1373. $path = $this->resolveToStoragePath( $virtualUrl );
  1374. return $this->backend->getLocalCopy( [ 'src' => $path ] );
  1375. }
  1376. /**
  1377. * Get a local FS file with a given virtual URL/storage path.
  1378. * The file is either an original or a copy. It should not be changed.
  1379. * Temporary files may be purged when the file object falls out of scope.
  1380. *
  1381. * @param string $virtualUrl
  1382. * @return FSFile|null Returns null on failure.
  1383. */
  1384. public function getLocalReference( $virtualUrl ) {
  1385. $path = $this->resolveToStoragePath( $virtualUrl );
  1386. return $this->backend->getLocalReference( [ 'src' => $path ] );
  1387. }
  1388. /**
  1389. * Get properties of a file with a given virtual URL/storage path.
  1390. * Properties should ultimately be obtained via FSFile::getProps().
  1391. *
  1392. * @param string $virtualUrl
  1393. * @return array
  1394. */
  1395. public function getFileProps( $virtualUrl ) {
  1396. $fsFile = $this->getLocalReference( $virtualUrl );
  1397. $mwProps = new MWFileProps( MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer() );
  1398. if ( $fsFile ) {
  1399. $props = $mwProps->getPropsFromPath( $fsFile->getPath(), true );
  1400. } else {
  1401. $props = $mwProps->newPlaceholderProps();
  1402. }
  1403. return $props;
  1404. }
  1405. /**
  1406. * Get the timestamp of a file with a given virtual URL/storage path
  1407. *
  1408. * @param string $virtualUrl
  1409. * @return string|bool False on failure
  1410. */
  1411. public function getFileTimestamp( $virtualUrl ) {
  1412. $path = $this->resolveToStoragePath( $virtualUrl );
  1413. return $this->backend->getFileTimestamp( [ 'src' => $path ] );
  1414. }
  1415. /**
  1416. * Get the size of a file with a given virtual URL/storage path
  1417. *
  1418. * @param string $virtualUrl
  1419. * @return int|bool False on failure
  1420. */
  1421. public function getFileSize( $virtualUrl ) {
  1422. $path = $this->resolveToStoragePath( $virtualUrl );
  1423. return $this->backend->getFileSize( [ 'src' => $path ] );
  1424. }
  1425. /**
  1426. * Get the sha1 (base 36) of a file with a given virtual URL/storage path
  1427. *
  1428. * @param string $virtualUrl
  1429. * @return string|bool
  1430. */
  1431. public function getFileSha1( $virtualUrl ) {
  1432. $path = $this->resolveToStoragePath( $virtualUrl );
  1433. return $this->backend->getFileSha1Base36( [ 'src' => $path ] );
  1434. }
  1435. /**
  1436. * Attempt to stream a file with the given virtual URL/storage path
  1437. *
  1438. * @param string $virtualUrl
  1439. * @param array $headers Additional HTTP headers to send on success
  1440. * @param array $optHeaders HTTP request headers (if-modified-since, range, ...)
  1441. * @return Status
  1442. * @since 1.27
  1443. */
  1444. public function streamFileWithStatus( $virtualUrl, $headers = [], $optHeaders = [] ) {
  1445. $path = $this->resolveToStoragePath( $virtualUrl );
  1446. $params = [ 'src' => $path, 'headers' => $headers, 'options' => $optHeaders ];
  1447. // T172851: HHVM does not flush the output properly, causing OOM
  1448. ob_start( null, 1048576 );
  1449. ob_implicit_flush( true );
  1450. $status = $this->newGood();
  1451. $status->merge( $this->backend->streamFile( $params ) );
  1452. // T186565: Close the buffer, unless it has already been closed
  1453. // in HTTPFileStreamer::resetOutputBuffers().
  1454. if ( ob_get_status() ) {
  1455. ob_end_flush();
  1456. }
  1457. return $status;
  1458. }
  1459. /**
  1460. * Attempt to stream a file with the given virtual URL/storage path
  1461. *
  1462. * @deprecated since 1.26, use streamFileWithStatus
  1463. * @param string $virtualUrl
  1464. * @param array $headers Additional HTTP headers to send on success
  1465. * @return bool Success
  1466. */
  1467. public function streamFile( $virtualUrl, $headers = [] ) {
  1468. return $this->streamFileWithStatus( $virtualUrl, $headers )->isOK();
  1469. }
  1470. /**
  1471. * Call a callback function for every public regular file in the repository.
  1472. * This only acts on the current version of files, not any old versions.
  1473. * May use either the database or the filesystem.
  1474. *
  1475. * @param callable $callback
  1476. * @return void
  1477. */
  1478. public function enumFiles( $callback ) {
  1479. $this->enumFilesInStorage( $callback );
  1480. }
  1481. /**
  1482. * Call a callback function for every public file in the repository.
  1483. * May use either the database or the filesystem.
  1484. *
  1485. * @param callable $callback
  1486. * @return void
  1487. */
  1488. protected function enumFilesInStorage( $callback ) {
  1489. $publicRoot = $this->getZonePath( 'public' );
  1490. $numDirs = 1 << ( $this->hashLevels * 4 );
  1491. // Use a priori assumptions about directory structure
  1492. // to reduce the tree height of the scanning process.
  1493. for ( $flatIndex = 0; $flatIndex < $numDirs; $flatIndex++ ) {
  1494. $hexString = sprintf( "%0{$this->hashLevels}x", $flatIndex );
  1495. $path = $publicRoot;
  1496. for ( $hexPos = 0; $hexPos < $this->hashLevels; $hexPos++ ) {
  1497. $path .= '/' . substr( $hexString, 0, $hexPos + 1 );
  1498. }
  1499. $iterator = $this->backend->getFileList( [ 'dir' => $path ] );
  1500. foreach ( $iterator as $name ) {
  1501. // Each item returned is a public file
  1502. call_user_func( $callback, "{$path}/{$name}" );
  1503. }
  1504. }
  1505. }
  1506. /**
  1507. * Determine if a relative path is valid, i.e. not blank or involving directory traveral
  1508. *
  1509. * @param string $filename
  1510. * @return bool
  1511. */
  1512. public function validateFilename( $filename ) {
  1513. if ( strval( $filename ) == '' ) {
  1514. return false;
  1515. }
  1516. return FileBackend::isPathTraversalFree( $filename );
  1517. }
  1518. /**
  1519. * Get a callback function to use for cleaning error message parameters
  1520. *
  1521. * @return string[]
  1522. */
  1523. function getErrorCleanupFunction() {
  1524. switch ( $this->pathDisclosureProtection ) {
  1525. case 'none':
  1526. case 'simple': // b/c
  1527. $callback = [ $this, 'passThrough' ];
  1528. break;
  1529. default: // 'paranoid'
  1530. $callback = [ $this, 'paranoidClean' ];
  1531. }
  1532. return $callback;
  1533. }
  1534. /**
  1535. * Path disclosure protection function
  1536. *
  1537. * @param string $param
  1538. * @return string
  1539. */
  1540. function paranoidClean( $param ) {
  1541. return '[hidden]';
  1542. }
  1543. /**
  1544. * Path disclosure protection function
  1545. *
  1546. * @param string $param
  1547. * @return string
  1548. */
  1549. function passThrough( $param ) {
  1550. return $param;
  1551. }
  1552. /**
  1553. * Create a new fatal error
  1554. *
  1555. * @param string $message
  1556. * @return Status
  1557. */
  1558. public function newFatal( $message /*, parameters...*/ ) {
  1559. $status = call_user_func_array( [ Status::class, 'newFatal' ], func_get_args() );
  1560. $status->cleanCallback = $this->getErrorCleanupFunction();
  1561. return $status;
  1562. }
  1563. /**
  1564. * Create a new good result
  1565. *
  1566. * @param null|string $value
  1567. * @return Status
  1568. */
  1569. public function newGood( $value = null ) {
  1570. $status = Status::newGood( $value );
  1571. $status->cleanCallback = $this->getErrorCleanupFunction();
  1572. return $status;
  1573. }
  1574. /**
  1575. * Checks if there is a redirect named as $title. If there is, return the
  1576. * title object. If not, return false.
  1577. * STUB
  1578. *
  1579. * @param Title $title Title of image
  1580. * @return bool
  1581. */
  1582. public function checkRedirect( Title $title ) {
  1583. return false;
  1584. }
  1585. /**
  1586. * Invalidates image redirect cache related to that image
  1587. * Doesn't do anything for repositories that don't support image redirects.
  1588. *
  1589. * STUB
  1590. * @param Title $title Title of image
  1591. */
  1592. public function invalidateImageRedirect( Title $title ) {
  1593. }
  1594. /**
  1595. * Get the human-readable name of the repo
  1596. *
  1597. * @return string
  1598. */
  1599. public function getDisplayName() {
  1600. global $wgSitename;
  1601. if ( $this->isLocal() ) {
  1602. return $wgSitename;
  1603. }
  1604. // 'shared-repo-name-wikimediacommons' is used when $wgUseInstantCommons = true
  1605. return wfMessageFallback( 'shared-repo-name-' . $this->name, 'shared-repo' )->text();
  1606. }
  1607. /**
  1608. * Get the portion of the file that contains the origin file name.
  1609. * If that name is too long, then the name "thumbnail.<ext>" will be given.
  1610. *
  1611. * @param string $name
  1612. * @return string
  1613. */
  1614. public function nameForThumb( $name ) {
  1615. if ( strlen( $name ) > $this->abbrvThreshold ) {
  1616. $ext = FileBackend::extensionFromPath( $name );
  1617. $name = ( $ext == '' ) ? 'thumbnail' : "thumbnail.$ext";
  1618. }
  1619. return $name;
  1620. }
  1621. /**
  1622. * Returns true if this the local file repository.
  1623. *
  1624. * @return bool
  1625. */
  1626. public function isLocal() {
  1627. return $this->getName() == 'local';
  1628. }
  1629. /**
  1630. * Get a key on the primary cache for this repository.
  1631. * Returns false if the repository's cache is not accessible at this site.
  1632. * The parameters are the parts of the key, as for wfMemcKey().
  1633. *
  1634. * STUB
  1635. * @return bool
  1636. */
  1637. public function getSharedCacheKey( /*...*/ ) {
  1638. return false;
  1639. }
  1640. /**
  1641. * Get a key for this repo in the local cache domain. These cache keys are
  1642. * not shared with remote instances of the repo.
  1643. * The parameters are the parts of the key, as for wfMemcKey().
  1644. *
  1645. * @return string
  1646. */
  1647. public function getLocalCacheKey( /*...*/ ) {
  1648. $args = func_get_args();
  1649. array_unshift( $args, 'filerepo', $this->getName() );
  1650. return call_user_func_array( 'wfMemcKey', $args );
  1651. }
  1652. /**
  1653. * Get a temporary private FileRepo associated with this repo.
  1654. *
  1655. * Files will be created in the temp zone of this repo.
  1656. * It will have the same backend as this repo.
  1657. *
  1658. * @return TempFileRepo
  1659. */
  1660. public function getTempRepo() {
  1661. return new TempFileRepo( [
  1662. 'name' => "{$this->name}-temp",
  1663. 'backend' => $this->backend,
  1664. 'zones' => [
  1665. 'public' => [
  1666. // Same place storeTemp() uses in the base repo, though
  1667. // the path hashing is mismatched, which is annoying.
  1668. 'container' => $this->zones['temp']['container'],
  1669. 'directory' => $this->zones['temp']['directory']
  1670. ],
  1671. 'thumb' => [
  1672. 'container' => $this->zones['temp']['container'],
  1673. 'directory' => $this->zones['temp']['directory'] == ''
  1674. ? 'thumb'
  1675. : $this->zones['temp']['directory'] . '/thumb'
  1676. ],
  1677. 'transcoded' => [
  1678. 'container' => $this->zones['temp']['container'],
  1679. 'directory' => $this->zones['temp']['directory'] == ''
  1680. ? 'transcoded'
  1681. : $this->zones['temp']['directory'] . '/transcoded'
  1682. ]
  1683. ],
  1684. 'hashLevels' => $this->hashLevels, // performance
  1685. 'isPrivate' => true // all in temp zone
  1686. ] );
  1687. }
  1688. /**
  1689. * Get an UploadStash associated with this repo.
  1690. *
  1691. * @param User|null $user
  1692. * @return UploadStash
  1693. */
  1694. public function getUploadStash( User $user = null ) {
  1695. return new UploadStash( $this, $user );
  1696. }
  1697. /**
  1698. * Throw an exception if this repo is read-only by design.
  1699. * This does not and should not check getReadOnlyReason().
  1700. *
  1701. * @return void
  1702. * @throws MWException
  1703. */
  1704. protected function assertWritableRepo() {
  1705. }
  1706. /**
  1707. * Return information about the repository.
  1708. *
  1709. * @return array
  1710. * @since 1.22
  1711. */
  1712. public function getInfo() {
  1713. $ret = [
  1714. 'name' => $this->getName(),
  1715. 'displayname' => $this->getDisplayName(),
  1716. 'rootUrl' => $this->getZoneUrl( 'public' ),
  1717. 'local' => $this->isLocal(),
  1718. ];
  1719. $optionalSettings = [
  1720. 'url', 'thumbUrl', 'initialCapital', 'descBaseUrl', 'scriptDirUrl', 'articleUrl',
  1721. 'fetchDescription', 'descriptionCacheExpiry', 'favicon'
  1722. ];
  1723. foreach ( $optionalSettings as $k ) {
  1724. if ( isset( $this->$k ) ) {
  1725. $ret[$k] = $this->$k;
  1726. }
  1727. }
  1728. return $ret;
  1729. }
  1730. /**
  1731. * Returns whether or not storage is SHA-1 based
  1732. * @return bool
  1733. */
  1734. public function hasSha1Storage() {
  1735. return $this->hasSha1Storage;
  1736. }
  1737. /**
  1738. * Returns whether or not repo supports having originals SHA-1s in the thumb URLs
  1739. * @return bool
  1740. */
  1741. public function supportsSha1URLs() {
  1742. return $this->supportsSha1URLs;
  1743. }
  1744. }