FileRepo.php 56 KB

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