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