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