MediaWiki master
FileRepo.php
Go to the documentation of this file.
1<?php
10namespace MediaWiki\FileRepo;
11
12use InvalidArgumentException;
13use LogicException;
27use MWFileProps;
28use RuntimeException;
29use Shellbox\Command\BoxedCommand;
30use StatusValue;
31use UploadStash;
32use Wikimedia\AtEase\AtEase;
38
68class FileRepo {
69 public const DELETE_SOURCE = 1;
70 public const OVERWRITE = 2;
71 public const OVERWRITE_SAME = 4;
72 public const SKIP_LOCKING = 8;
73
74 public const NAME_AND_TIME_ONLY = 1;
75
80
83
85 protected $hasSha1Storage = false;
86
88 protected $supportsSha1URLs = false;
89
91 protected $backend;
92
94 protected $zones = [];
95
97 protected $thumbScriptUrl;
98
103
107 protected $descBaseUrl;
108
112 protected $scriptDirUrl;
113
115 protected $articleUrl;
116
123
129 protected $pathDisclosureProtection = 'simple';
130
132 protected $url;
133
135 protected $thumbUrl;
136
138 protected $hashLevels;
139
142
148
150 protected $favicon = null;
151
153 protected $isPrivate;
154
156 protected $fileFactory = [ UnregisteredLocalFile::class, 'newFromTitle' ];
158 protected $oldFileFactory = false;
160 protected $fileFactoryKey = false;
162 protected $oldFileFactoryKey = false;
163
167 protected $thumbProxyUrl;
170
172 protected $disableLocalTransform = false;
173
175 protected $wanCache;
176
182 public $name;
183
189 public function __construct( ?array $info = null ) {
190 // Verify required settings presence
191 if (
192 $info === null
193 || !array_key_exists( 'name', $info )
194 || !array_key_exists( 'backend', $info )
195 ) {
196 throw new InvalidArgumentException( __CLASS__ .
197 " requires an array of options having both 'name' and 'backend' keys.\n" );
198 }
199
200 // Required settings
201 $this->name = $info['name'];
202 if ( $info['backend'] instanceof FileBackend ) {
203 $this->backend = $info['backend']; // useful for testing
204 } else {
205 $this->backend =
206 MediaWikiServices::getInstance()->getFileBackendGroup()->get( $info['backend'] );
207 }
208
209 // Optional settings that can have no value
210 $optionalSettings = [
211 'descBaseUrl', 'scriptDirUrl', 'articleUrl', 'fetchDescription',
212 'thumbScriptUrl', 'pathDisclosureProtection', 'descriptionCacheExpiry',
213 'favicon', 'thumbProxyUrl', 'thumbProxySecret', 'disableLocalTransform'
214 ];
215 foreach ( $optionalSettings as $var ) {
216 if ( isset( $info[$var] ) ) {
217 $this->$var = $info[$var];
218 }
219 }
220
221 // Optional settings that have a default
222 $localCapitalLinks =
223 MediaWikiServices::getInstance()->getNamespaceInfo()->isCapitalized( NS_FILE );
224 $this->initialCapital = $info['initialCapital'] ?? $localCapitalLinks;
225 if ( $localCapitalLinks && !$this->initialCapital ) {
226 // If the local wiki's file namespace requires an initial capital, but a foreign file
227 // repo doesn't, complications will result. Linker code will want to auto-capitalize the
228 // first letter of links to files, but those links might actually point to files on
229 // foreign wikis with initial-lowercase names. This combination is not likely to be
230 // used by anyone anyway, so we just outlaw it to save ourselves the bugs. If you want
231 // to include a foreign file repo with initialCapital false, set your local file
232 // namespace to not be capitalized either.
233 throw new InvalidArgumentException(
234 'File repos with initial capital false are not allowed on wikis where the File ' .
235 'namespace has initial capital true' );
236 }
237
238 $this->url = $info['url'] ?? false; // a subclass may set the URL (e.g. ForeignAPIRepo)
239 $defaultThumbUrl = $this->url ? $this->url . '/thumb' : false;
240 $this->thumbUrl = $info['thumbUrl'] ?? $defaultThumbUrl;
241 $this->hashLevels = $info['hashLevels'] ?? 2;
242 $this->deletedHashLevels = $info['deletedHashLevels'] ?? $this->hashLevels;
243 $this->transformVia404 = !empty( $info['transformVia404'] );
244 $this->abbrvThreshold = $info['abbrvThreshold'] ?? 255;
245 $this->isPrivate = !empty( $info['isPrivate'] );
246 // Give defaults for the basic zones...
247 $this->zones = $info['zones'] ?? [];
248 foreach ( [ 'public', 'thumb', 'transcoded', 'temp', 'deleted' ] as $zone ) {
249 if ( !isset( $this->zones[$zone]['container'] ) ) {
250 $this->zones[$zone]['container'] = "{$this->name}-{$zone}";
251 }
252 if ( !isset( $this->zones[$zone]['directory'] ) ) {
253 $this->zones[$zone]['directory'] = '';
254 }
255 if ( !isset( $this->zones[$zone]['urlsByExt'] ) ) {
256 $this->zones[$zone]['urlsByExt'] = [];
257 }
258 }
259
260 $this->supportsSha1URLs = !empty( $info['supportsSha1URLs'] );
261
262 $this->wanCache = $info['wanCache'] ?? WANObjectCache::newEmpty();
263 }
264
270 public function getBackend() {
271 return $this->backend;
272 }
273
280 public function getReadOnlyReason() {
281 return $this->backend->getReadOnlyReason();
282 }
283
289 protected function initZones( $doZones = [] ): void {
290 foreach ( (array)$doZones as $zone ) {
291 $root = $this->getZonePath( $zone );
292 if ( $root === null ) {
293 throw new RuntimeException( "No '$zone' zone defined in the {$this->name} repo." );
294 }
295 }
296 }
297
304 public static function isVirtualUrl( $url ) {
305 return str_starts_with( $url, 'mwrepo://' );
306 }
307
316 public function getVirtualUrl( $suffix = false ) {
317 $path = 'mwrepo://' . $this->name;
318 if ( $suffix !== false ) {
319 $path .= '/' . rawurlencode( $suffix );
320 }
321
322 return $path;
323 }
324
332 public function getZoneUrl( $zone, $ext = null ) {
333 if ( in_array( $zone, [ 'public', 'thumb', 'transcoded' ] ) ) {
334 // standard public zones
335 if ( $ext !== null && isset( $this->zones[$zone]['urlsByExt'][$ext] ) ) {
336 // custom URL for extension/zone
337 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
338 return $this->zones[$zone]['urlsByExt'][$ext];
339 } elseif ( isset( $this->zones[$zone]['url'] ) ) {
340 // custom URL for zone
341 return $this->zones[$zone]['url'];
342 }
343 }
344 switch ( $zone ) {
345 case 'public':
346 return $this->url;
347 case 'temp':
348 case 'deleted':
349 return false; // no public URL
350 case 'thumb':
351 return $this->thumbUrl;
352 case 'transcoded':
353 return "{$this->url}/transcoded";
354 default:
355 return false;
356 }
357 }
358
362 public function backendSupportsUnicodePaths() {
363 return (bool)( $this->getBackend()->getFeatures() & FileBackend::ATTR_UNICODE_PATHS );
364 }
365
374 public function resolveVirtualUrl( $url ) {
375 if ( !str_starts_with( $url, 'mwrepo://' ) ) {
376 throw new InvalidArgumentException( __METHOD__ . ': unknown protocol' );
377 }
378 $bits = explode( '/', substr( $url, 9 ), 3 );
379 if ( count( $bits ) != 3 ) {
380 throw new InvalidArgumentException( __METHOD__ . ": invalid mwrepo URL: $url" );
381 }
382 [ $repo, $zone, $rel ] = $bits;
383 if ( $repo !== $this->name ) {
384 throw new InvalidArgumentException( __METHOD__ . ": fetching from a foreign repo is not supported" );
385 }
386 $base = $this->getZonePath( $zone );
387 if ( !$base ) {
388 throw new InvalidArgumentException( __METHOD__ . ": invalid zone: $zone" );
389 }
390
391 return $base . '/' . rawurldecode( $rel );
392 }
393
400 protected function getZoneLocation( $zone ) {
401 if ( !isset( $this->zones[$zone] ) ) {
402 return [ null, null ]; // bogus
403 }
404
405 return [ $this->zones[$zone]['container'], $this->zones[$zone]['directory'] ];
406 }
407
414 public function getZonePath( $zone ) {
415 [ $container, $base ] = $this->getZoneLocation( $zone );
416 if ( $container === null || $base === null ) {
417 return null;
418 }
419 $backendName = $this->backend->getName();
420 if ( $base != '' ) { // may not be set
421 $base = "/{$base}";
422 }
423
424 return "mwstore://$backendName/{$container}{$base}";
425 }
426
438 public function newFile( $title, $time = false ) {
439 $title = File::normalizeTitle( $title );
440 if ( !$title ) {
441 return null;
442 }
443 if ( $time ) {
444 if ( $this->oldFileFactory ) {
445 return ( $this->oldFileFactory )( $title, $this, $time );
446 } else {
447 return null;
448 }
449 } else {
450 return ( $this->fileFactory )( $title, $this );
451 }
452 }
453
473 public function findFile( $title, $options = [] ) {
474 if ( !empty( $options['private'] ) && !( $options['private'] instanceof Authority ) ) {
475 throw new InvalidArgumentException(
476 __METHOD__ . ' called with the `private` option set to something ' .
477 'other than an Authority object'
478 );
479 }
480
481 $title = File::normalizeTitle( $title );
482 if ( !$title ) {
483 return false;
484 }
485 if ( isset( $options['bypassCache'] ) ) {
486 $options['latest'] = $options['bypassCache']; // b/c
487 }
488 $time = $options['time'] ?? false;
489 $flags = !empty( $options['latest'] ) ? IDBAccessObject::READ_LATEST : 0;
490 # First try the current version of the file to see if it precedes the timestamp
491 $img = $this->newFile( $title );
492 if ( !$img ) {
493 return false;
494 }
495 $img->load( $flags );
496 if ( $img->exists() && ( !$time || $img->getTimestamp() == $time ) ) {
497 return $img;
498 }
499 # Now try an old version of the file
500 if ( $time !== false ) {
501 $img = $this->newFile( $title, $time );
502 if ( $img ) {
503 $img->load( $flags );
504 if ( $img->exists() ) {
505 if ( !$img->isDeleted( File::DELETED_FILE ) ) {
506 return $img; // always OK
507 } elseif (
508 // If its not empty, its an Authority object
509 !empty( $options['private'] ) &&
510 $img->userCan( File::DELETED_FILE, $options['private'] )
511 ) {
512 return $img;
513 }
514 }
515 }
516 }
517
518 # Now try redirects
519 if ( !empty( $options['ignoreRedirect'] ) ) {
520 return false;
521 }
522 $redir = $this->checkRedirect( $title );
523 if ( $redir && $title->getNamespace() === NS_FILE ) {
524 $img = $this->newFile( $redir );
525 if ( !$img ) {
526 return false;
527 }
528 $img->load( $flags );
529 if ( $img->exists() ) {
530 $img->redirectedFrom( $title->getDBkey() );
531
532 return $img;
533 }
534 }
535
536 return false;
537 }
538
556 public function findFiles( array $items, $flags = 0 ) {
557 $result = [];
558 foreach ( $items as $item ) {
559 if ( is_array( $item ) ) {
560 $title = $item['title'];
561 $options = $item;
562 unset( $options['title'] );
563
564 if (
565 !empty( $options['private'] ) &&
566 !( $options['private'] instanceof Authority )
567 ) {
568 $options['private'] = RequestContext::getMain()->getAuthority();
569 }
570 } else {
571 $title = $item;
572 $options = [];
573 }
574 $file = $this->findFile( $title, $options );
575 if ( $file ) {
576 $searchName = File::normalizeTitle( $title )->getDBkey(); // must be valid
577 if ( $flags & self::NAME_AND_TIME_ONLY ) {
578 $result[$searchName] = [
579 'title' => $file->getTitle()->getDBkey(),
580 'timestamp' => $file->getTimestamp()
581 ];
582 } else {
583 $result[$searchName] = $file;
584 }
585 }
586 }
587
588 return $result;
589 }
590
601 public function findFileFromKey( $sha1, $options = [] ) {
602 if ( !empty( $options['private'] ) && !( $options['private'] instanceof Authority ) ) {
603 throw new InvalidArgumentException(
604 __METHOD__ . ' called with the `private` option set to something ' .
605 'other than an Authority object'
606 );
607 }
608
609 $time = $options['time'] ?? false;
610 # First try to find a matching current version of a file...
611 if ( !$this->fileFactoryKey ) {
612 return false; // find-by-sha1 not supported
613 }
614 $img = ( $this->fileFactoryKey )( $sha1, $this, $time );
615 if ( $img && $img->exists() ) {
616 return $img;
617 }
618 # Now try to find a matching old version of a file...
619 if ( $time !== false && $this->oldFileFactoryKey ) { // find-by-sha1 supported?
620 $img = ( $this->oldFileFactoryKey )( $sha1, $this, $time );
621 if ( $img && $img->exists() ) {
622 if ( !$img->isDeleted( File::DELETED_FILE ) ) {
623 return $img; // always OK
624 } elseif (
625 // If its not empty, its an Authority object
626 !empty( $options['private'] ) &&
627 $img->userCan( File::DELETED_FILE, $options['private'] )
628 ) {
629 return $img;
630 }
631 }
632 }
633
634 return false;
635 }
636
645 public function findBySha1( $hash ) {
646 return [];
647 }
648
656 public function findBySha1s( array $hashes ) {
657 $result = [];
658 foreach ( $hashes as $hash ) {
659 $files = $this->findBySha1( $hash );
660 if ( count( $files ) ) {
661 $result[$hash] = $files;
662 }
663 }
664
665 return $result;
666 }
667
676 public function findFilesByPrefix( $prefix, $limit ) {
677 return [];
678 }
679
685 public function getThumbScriptUrl() {
686 return $this->thumbScriptUrl;
687 }
688
694 public function getThumbProxyUrl() {
695 return $this->thumbProxyUrl;
696 }
697
703 public function getThumbProxySecret() {
704 return $this->thumbProxySecret;
705 }
706
712 public function canTransformVia404() {
713 return $this->transformVia404;
714 }
715
722 public function canTransformLocally() {
723 return !$this->disableLocalTransform;
724 }
725
732 public function getNameFromTitle( $title ) {
733 if (
734 $this->initialCapital !=
735 MediaWikiServices::getInstance()->getNamespaceInfo()->isCapitalized( NS_FILE )
736 ) {
737 $name = $title->getDBkey();
738 if ( $this->initialCapital ) {
739 $name = MediaWikiServices::getInstance()->getContentLanguage()->ucfirst( $name );
740 }
741 } else {
742 $name = $title->getDBkey();
743 }
744
745 return $name;
746 }
747
753 public function getRootDirectory() {
754 return $this->getZonePath( 'public' );
755 }
756
764 public function getHashPath( $name ) {
765 return self::getHashPathForLevel( $name, $this->hashLevels );
766 }
767
775 public function getTempHashPath( $suffix ) {
776 $parts = explode( '!', $suffix, 2 ); // format is <timestamp>!<name> or just <name>
777 $name = $parts[1] ?? $suffix; // hash path is not based on timestamp
778 return self::getHashPathForLevel( $name, $this->hashLevels );
779 }
780
786 protected static function getHashPathForLevel( $name, $levels ) {
787 if ( $levels == 0 ) {
788 return '';
789 } else {
790 $hash = md5( $name );
791 $path = '';
792 for ( $i = 1; $i <= $levels; $i++ ) {
793 $path .= substr( $hash, 0, $i ) . '/';
794 }
795
796 return $path;
797 }
798 }
799
805 public function getHashLevels() {
806 return $this->hashLevels;
807 }
808
814 public function getName() {
815 return $this->name;
816 }
817
825 public function makeUrl( $query = '', $entry = 'index' ) {
826 if ( $this->scriptDirUrl !== null ) {
827 return wfAppendQuery( "{$this->scriptDirUrl}/{$entry}.php", $query );
828 }
829
830 return false;
831 }
832
845 public function getDescriptionUrl( $name ) {
846 $encName = wfUrlencode( $name );
847 if ( $this->descBaseUrl !== null ) {
848 # "http://example.com/wiki/File:"
849 return $this->descBaseUrl . $encName;
850 }
851 if ( $this->articleUrl !== null ) {
852 # "http://example.com/wiki/$1"
853 # We use "Image:" as the canonical namespace for
854 # compatibility across all MediaWiki versions.
855 return str_replace( '$1',
856 "Image:$encName", $this->articleUrl );
857 }
858 if ( $this->scriptDirUrl !== null ) {
859 # "http://example.com/w"
860 # We use "Image:" as the canonical namespace for
861 # compatibility across all MediaWiki versions,
862 # and just sort of hope index.php is right. ;)
863 return $this->makeUrl( "title=Image:$encName" );
864 }
865
866 return false;
867 }
868
879 public function getDescriptionRenderUrl( $name, $lang = null ) {
880 $query = 'action=render';
881 if ( $lang !== null ) {
882 $query .= '&uselang=' . urlencode( $lang );
883 }
884 if ( $this->scriptDirUrl !== null ) {
885 return $this->makeUrl(
886 'title=' .
887 wfUrlencode( 'Image:' . $name ) .
888 "&$query" );
889 } else {
890 $descUrl = $this->getDescriptionUrl( $name );
891 if ( $descUrl ) {
892 return wfAppendQuery( $descUrl, $query );
893 } else {
894 return false;
895 }
896 }
897 }
898
904 public function getDescriptionStylesheetUrl() {
905 if ( $this->scriptDirUrl !== null ) {
906 // Must match canonical query parameter order for optimum caching
907 // See HTMLCacheUpdater::getUrls
908 return $this->makeUrl( 'title=MediaWiki:Filepage.css&action=raw&ctype=text/css' );
909 }
910
911 return false;
912 }
913
931 public function store( $srcPath, $dstZone, $dstRel, $flags = 0 ) {
932 $this->assertWritableRepo(); // fail out if read-only
933
934 $status = $this->storeBatch( [ [ $srcPath, $dstZone, $dstRel ] ], $flags );
935 if ( $status->successCount == 0 ) {
936 $status->setOK( false );
937 }
938
939 return $status;
940 }
941
955 public function storeBatch( array $triplets, $flags = 0 ) {
956 $this->assertWritableRepo(); // fail out if read-only
957
958 if ( $flags & self::DELETE_SOURCE ) {
959 throw new InvalidArgumentException( "DELETE_SOURCE not supported in " . __METHOD__ );
960 }
961
962 $status = $this->newGood();
963 $backend = $this->backend; // convenience
964
965 $operations = [];
966 // Validate each triplet and get the store operation...
967 foreach ( $triplets as [ $src, $dstZone, $dstRel ] ) {
968 $srcPath = ( $src instanceof FSFile ) ? $src->getPath() : $src;
969 wfDebug( __METHOD__
970 . "( \$src='$srcPath', \$dstZone='$dstZone', \$dstRel='$dstRel' )"
971 );
972 // Resolve source path
973 if ( $src instanceof FSFile ) {
974 $op = 'store';
975 } else {
976 $src = $this->resolveToStoragePathIfVirtual( $src );
977 $op = FileBackend::isStoragePath( $src ) ? 'copy' : 'store';
978 }
979 // Resolve destination path
980 $root = $this->getZonePath( $dstZone );
981 if ( !$root ) {
982 throw new RuntimeException( "Invalid zone: $dstZone" );
983 }
984 if ( !$this->validateFilename( $dstRel ) ) {
985 throw new RuntimeException( 'Validation error in $dstRel' );
986 }
987 $dstPath = "$root/$dstRel";
988 $dstDir = dirname( $dstPath );
989 // Create destination directories for this triplet
990 if ( !$this->initDirectory( $dstDir )->isOK() ) {
991 return $this->newFatal( 'directorycreateerror', $dstDir );
992 }
993
994 // Copy the source file to the destination
995 $operations[] = [
996 'op' => $op,
997 'src' => $src, // storage path (copy) or local file path (store)
998 'dst' => $dstPath,
999 'overwrite' => (bool)( $flags & self::OVERWRITE ),
1000 'overwriteSame' => (bool)( $flags & self::OVERWRITE_SAME ),
1001 ];
1002 }
1003
1004 // Execute the store operation for each triplet
1005 $opts = [ 'force' => true ];
1006 if ( $flags & self::SKIP_LOCKING ) {
1007 $opts['nonLocking'] = true;
1008 }
1009
1010 return $status->merge( $backend->doOperations( $operations, $opts ) );
1011 }
1012
1023 public function cleanupBatch( array $files, $flags = 0 ) {
1024 $this->assertWritableRepo(); // fail out if read-only
1025
1026 $status = $this->newGood();
1027
1028 $operations = [];
1029 foreach ( $files as $path ) {
1030 if ( is_array( $path ) ) {
1031 // This is a pair, extract it
1032 [ $zone, $rel ] = $path;
1033 $path = $this->getZonePath( $zone ) . "/$rel";
1034 } else {
1035 // Resolve source to a storage path if virtual
1036 $path = $this->resolveToStoragePathIfVirtual( $path );
1037 }
1038 $operations[] = [ 'op' => 'delete', 'src' => $path ];
1039 }
1040 // Actually delete files from storage...
1041 $opts = [ 'force' => true ];
1042 if ( $flags & self::SKIP_LOCKING ) {
1043 $opts['nonLocking'] = true;
1044 }
1045
1046 return $status->merge( $this->backend->doOperations( $operations, $opts ) );
1047 }
1048
1066 final public function quickImport( $src, $dst, $options = null ) {
1067 return $this->quickImportBatch( [ [ $src, $dst, $options ] ] );
1068 }
1069
1084 public function quickImportBatch( array $triples ) {
1085 $status = $this->newGood();
1086 $operations = [];
1087 foreach ( $triples as $triple ) {
1088 [ $src, $dst ] = $triple;
1089 if ( $src instanceof FSFile ) {
1090 $op = 'store';
1091 } else {
1092 $src = $this->resolveToStoragePathIfVirtual( $src );
1093 $op = FileBackend::isStoragePath( $src ) ? 'copy' : 'store';
1094 }
1095 $dst = $this->resolveToStoragePathIfVirtual( $dst );
1096
1097 if ( !isset( $triple[2] ) ) {
1098 $headers = [];
1099 } elseif ( is_string( $triple[2] ) ) {
1100 // back-compat
1101 $headers = [ 'Content-Disposition' => $triple[2] ];
1102 } elseif ( is_array( $triple[2] ) && isset( $triple[2]['headers'] ) ) {
1103 $headers = $triple[2]['headers'];
1104 } else {
1105 $headers = [];
1106 }
1107
1108 $operations[] = [
1109 'op' => $op,
1110 'src' => $src, // storage path (copy) or local path/FSFile (store)
1111 'dst' => $dst,
1112 'headers' => $headers
1113 ];
1114 $status->merge( $this->initDirectory( dirname( $dst ) ) );
1115 }
1116
1117 return $status->merge( $this->backend->doQuickOperations( $operations ) );
1118 }
1119
1128 final public function quickPurge( $path ) {
1129 return $this->quickPurgeBatch( [ $path ] );
1130 }
1131
1139 public function quickCleanDir( $dir ) {
1140 return $this->newGood()->merge(
1141 $this->backend->clean(
1142 [ 'dir' => $this->resolveToStoragePathIfVirtual( $dir ) ]
1143 )
1144 );
1145 }
1146
1155 public function quickPurgeBatch( array $paths ) {
1156 $status = $this->newGood();
1157 $operations = [];
1158 foreach ( $paths as $path ) {
1159 $operations[] = [
1160 'op' => 'delete',
1161 'src' => $this->resolveToStoragePathIfVirtual( $path ),
1162 'ignoreMissingSource' => true
1163 ];
1164 }
1165 $status->merge( $this->backend->doQuickOperations( $operations ) );
1166
1167 return $status;
1168 }
1169
1180 public function storeTemp( $originalName, $srcPath ) {
1181 $this->assertWritableRepo(); // fail out if read-only
1182
1183 $date = MWTimestamp::getInstance()->format( 'YmdHis' );
1184 $hashPath = $this->getHashPath( $originalName );
1185 $dstUrlRel = $hashPath . $date . '!' . rawurlencode( $originalName );
1186 $virtualUrl = $this->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
1187
1188 $result = $this->quickImport( $srcPath, $virtualUrl );
1189 $result->value = $virtualUrl;
1190
1191 return $result;
1192 }
1193
1200 public function freeTemp( $virtualUrl ) {
1201 $this->assertWritableRepo(); // fail out if read-only
1202
1203 $temp = $this->getVirtualUrl( 'temp' );
1204 if ( !str_starts_with( $virtualUrl, $temp ) ) {
1205 wfDebug( __METHOD__ . ": Invalid temp virtual URL" );
1206
1207 return false;
1208 }
1209
1210 return $this->quickPurge( $virtualUrl )->isOK();
1211 }
1212
1222 public function concatenate( array $srcPaths, $dstPath, $flags = 0 ) {
1223 $this->assertWritableRepo(); // fail out if read-only
1224
1225 $status = $this->newGood();
1226
1227 $sources = [];
1228 foreach ( $srcPaths as $srcPath ) {
1229 // Resolve source to a storage path if virtual
1230 $source = $this->resolveToStoragePathIfVirtual( $srcPath );
1231 $sources[] = $source; // chunk to merge
1232 }
1233
1234 // Concatenate the chunks into one FS file
1235 $params = [ 'srcs' => $sources, 'dst' => $dstPath ];
1236 $status->merge( $this->backend->concatenate( $params ) );
1237 if ( !$status->isOK() ) {
1238 return $status;
1239 }
1240
1241 // Delete the sources if required
1242 if ( $flags & self::DELETE_SOURCE ) {
1243 $status->merge( $this->quickPurgeBatch( $srcPaths ) );
1244 }
1245
1246 // Make sure status is OK, despite any quickPurgeBatch() fatals
1247 $status->setResult( true );
1248
1249 return $status;
1250 }
1251
1275 public function publish(
1276 $src, $dstRel, $archiveRel, $flags = 0, array $options = []
1277 ) {
1278 $this->assertWritableRepo(); // fail out if read-only
1279
1280 $status = $this->publishBatch(
1281 [ [ $src, $dstRel, $archiveRel, $options ] ], $flags );
1282 if ( $status->successCount == 0 ) {
1283 $status->setOK( false );
1284 }
1285 $status->value = $status->value[0] ?? false;
1286
1287 return $status;
1288 }
1289
1301 public function publishBatch( array $ntuples, $flags = 0 ) {
1302 $this->assertWritableRepo(); // fail out if read-only
1303
1304 $backend = $this->backend; // convenience
1305 // Try creating directories
1306 $this->initZones( 'public' );
1307
1308 $status = $this->newGood( [] );
1309
1310 $operations = [];
1311 $sourceFSFilesToDelete = []; // cleanup for disk source files
1312 // Validate each triplet and get the store operation...
1313 foreach ( $ntuples as $ntuple ) {
1314 [ $src, $dstRel, $archiveRel ] = $ntuple;
1315 $srcPath = ( $src instanceof FSFile ) ? $src->getPath() : $src;
1316
1317 $options = $ntuple[3] ?? [];
1318 // Resolve source to a storage path if virtual
1319 $srcPath = $this->resolveToStoragePathIfVirtual( $srcPath );
1320 if ( !$this->validateFilename( $dstRel ) ) {
1321 throw new RuntimeException( 'Validation error in $dstRel' );
1322 }
1323 if ( !$this->validateFilename( $archiveRel ) ) {
1324 throw new RuntimeException( 'Validation error in $archiveRel' );
1325 }
1326
1327 $publicRoot = $this->getZonePath( 'public' );
1328 $dstPath = "$publicRoot/$dstRel";
1329 $archivePath = "$publicRoot/$archiveRel";
1330
1331 $dstDir = dirname( $dstPath );
1332 $archiveDir = dirname( $archivePath );
1333 // Abort immediately on directory creation errors since they're likely to be repetitive
1334 if ( !$this->initDirectory( $dstDir )->isOK() ) {
1335 return $this->newFatal( 'directorycreateerror', $dstDir );
1336 }
1337 if ( !$this->initDirectory( $archiveDir )->isOK() ) {
1338 return $this->newFatal( 'directorycreateerror', $archiveDir );
1339 }
1340
1341 // Set any desired headers to be use in GET/HEAD responses
1342 $headers = $options['headers'] ?? [];
1343
1344 // Archive destination file if it exists.
1345 // This will check if the archive file also exists and fail if does.
1346 // This is a check to avoid data loss. On Windows and Linux,
1347 // copy() will overwrite, so the existence check is vulnerable to
1348 // race conditions unless a functioning LockManager is used.
1349 // LocalFile also uses SELECT FOR UPDATE for synchronization.
1350 $operations[] = [
1351 'op' => 'copy',
1352 'src' => $dstPath,
1353 'dst' => $archivePath,
1354 'ignoreMissingSource' => true
1355 ];
1356
1357 // Copy (or move) the source file to the destination
1358 if ( FileBackend::isStoragePath( $srcPath ) ) {
1359 $operations[] = [
1360 'op' => ( $flags & self::DELETE_SOURCE ) ? 'move' : 'copy',
1361 'src' => $srcPath,
1362 'dst' => $dstPath,
1363 'overwrite' => true, // replace current
1364 'headers' => $headers
1365 ];
1366 } else {
1367 $operations[] = [
1368 'op' => 'store',
1369 'src' => $src, // storage path (copy) or local path/FSFile (store)
1370 'dst' => $dstPath,
1371 'overwrite' => true, // replace current
1372 'headers' => $headers
1373 ];
1374 if ( $flags & self::DELETE_SOURCE ) {
1375 $sourceFSFilesToDelete[] = $srcPath;
1376 }
1377 }
1378 }
1379
1380 // Execute the operations for each triplet
1381 $status->merge( $backend->doOperations( $operations ) );
1382 // Find out which files were archived...
1383 foreach ( $ntuples as $i => $ntuple ) {
1384 [ , , $archiveRel ] = $ntuple;
1385 $archivePath = $this->getZonePath( 'public' ) . "/$archiveRel";
1386 if ( $this->fileExists( $archivePath ) ) {
1387 $status->value[$i] = 'archived';
1388 } else {
1389 $status->value[$i] = 'new';
1390 }
1391 }
1392 // Cleanup for disk source files...
1393 foreach ( $sourceFSFilesToDelete as $file ) {
1394 AtEase::suppressWarnings();
1395 unlink( $file ); // FS cleanup
1396 AtEase::restoreWarnings();
1397 }
1398
1399 return $status;
1400 }
1401
1409 protected function initDirectory( $dir ) {
1410 $path = $this->resolveToStoragePathIfVirtual( $dir );
1411 [ , $container, ] = FileBackend::splitStoragePath( $path );
1412
1413 $params = [ 'dir' => $path ];
1414 if ( $this->isPrivate
1415 || $container === $this->zones['deleted']['container']
1416 || $container === $this->zones['temp']['container']
1417 ) {
1418 # Take all available measures to prevent web accessibility of new deleted
1419 # directories, in case the user has not configured offline storage
1420 $params = [ 'noAccess' => true, 'noListing' => true ] + $params;
1421 }
1422
1423 return $this->newGood()->merge( $this->backend->prepare( $params ) );
1424 }
1425
1432 public function cleanDir( $dir ) {
1433 $this->assertWritableRepo(); // fail out if read-only
1434
1435 return $this->newGood()->merge(
1436 $this->backend->clean(
1437 [ 'dir' => $this->resolveToStoragePathIfVirtual( $dir ) ]
1438 )
1439 );
1440 }
1441
1448 public function fileExists( $file ) {
1449 $result = $this->fileExistsBatch( [ $file ] );
1450
1451 return $result[0];
1452 }
1453
1461 public function fileExistsBatch( array $files ) {
1462 $paths = array_map( $this->resolveToStoragePathIfVirtual( ... ), $files );
1463 $this->backend->preloadFileStat( [ 'srcs' => $paths ] );
1464
1465 $result = [];
1466 foreach ( $paths as $key => $path ) {
1467 $result[$key] = $this->backend->fileExists( [ 'src' => $path ] );
1468 }
1469
1470 return $result;
1471 }
1472
1483 public function delete( $srcRel, $archiveRel ) {
1484 $this->assertWritableRepo(); // fail out if read-only
1485
1486 return $this->deleteBatch( [ [ $srcRel, $archiveRel ] ] );
1487 }
1488
1505 public function deleteBatch( array $sourceDestPairs ) {
1506 $this->assertWritableRepo(); // fail out if read-only
1507
1508 // Try creating directories
1509 $this->initZones( [ 'public', 'deleted' ] );
1510
1511 $status = $this->newGood();
1512
1513 $backend = $this->backend; // convenience
1514 $operations = [];
1515 // Validate filenames and create archive directories
1516 foreach ( $sourceDestPairs as [ $srcRel, $archiveRel ] ) {
1517 if ( !$this->validateFilename( $srcRel ) ) {
1518 throw new RuntimeException( __METHOD__ . ':Validation error in $srcRel' );
1519 } elseif ( !$this->validateFilename( $archiveRel ) ) {
1520 throw new RuntimeException( __METHOD__ . ':Validation error in $archiveRel' );
1521 }
1522
1523 $publicRoot = $this->getZonePath( 'public' );
1524 $srcPath = "{$publicRoot}/$srcRel";
1525
1526 $deletedRoot = $this->getZonePath( 'deleted' );
1527 $archivePath = "{$deletedRoot}/{$archiveRel}";
1528 $archiveDir = dirname( $archivePath ); // does not touch FS
1529
1530 // Create destination directories
1531 if ( !$this->initDirectory( $archiveDir )->isGood() ) {
1532 return $this->newFatal( 'directorycreateerror', $archiveDir );
1533 }
1534
1535 $operations[] = [
1536 'op' => 'move',
1537 'src' => $srcPath,
1538 'dst' => $archivePath,
1539 // We may have 2+ identical files being deleted,
1540 // all of which will map to the same destination file
1541 'overwriteSame' => true // also see T33792
1542 ];
1543 }
1544
1545 // Move the files by execute the operations for each pair.
1546 // We're now committed to returning an OK result, which will
1547 // lead to the files being moved in the DB also.
1548 $opts = [ 'force' => true ];
1549 return $status->merge( $backend->doOperations( $operations, $opts ) );
1550 }
1551
1558 public function cleanupDeletedBatch( array $storageKeys ) {
1559 $this->assertWritableRepo();
1560 }
1561
1569 public function getDeletedHashPath( $key ) {
1570 if ( strlen( $key ) < 31 ) {
1571 throw new InvalidArgumentException( "Invalid storage key '$key'." );
1572 }
1573 $path = '';
1574 for ( $i = 0; $i < $this->deletedHashLevels; $i++ ) {
1575 $path .= $key[$i] . '/';
1576 }
1577
1578 return $path;
1579 }
1580
1588 protected function resolveToStoragePathIfVirtual( $path ) {
1589 if ( self::isVirtualUrl( $path ) ) {
1590 return $this->resolveVirtualUrl( $path );
1591 }
1592
1593 return $path;
1594 }
1595
1603 public function getLocalCopy( $virtualUrl ) {
1604 $path = $this->resolveToStoragePathIfVirtual( $virtualUrl );
1605
1606 return $this->backend->getLocalCopy( [ 'src' => $path ] );
1607 }
1608
1617 public function getLocalReference( $virtualUrl ) {
1618 $path = $this->resolveToStoragePathIfVirtual( $virtualUrl );
1619
1620 return $this->backend->getLocalReference( [ 'src' => $path ] );
1621 }
1622
1632 public function addShellboxInputFile( BoxedCommand $command, string $boxedName,
1633 string $virtualUrl
1634 ) {
1635 $path = $this->resolveToStoragePathIfVirtual( $virtualUrl );
1636
1637 return $this->backend->addShellboxInputFile( $command, $boxedName, [ 'src' => $path ] );
1638 }
1639
1647 public function getFileProps( $virtualUrl ) {
1648 $fsFile = $this->getLocalReference( $virtualUrl );
1649 $mwProps = new MWFileProps( MediaWikiServices::getInstance()->getMimeAnalyzer() );
1650 if ( $fsFile ) {
1651 $props = $mwProps->getPropsFromPath( $fsFile->getPath(), true );
1652 } else {
1653 $props = $mwProps->newPlaceholderProps();
1654 }
1655
1656 return $props;
1657 }
1658
1665 public function getFileTimestamp( $virtualUrl ) {
1666 $path = $this->resolveToStoragePathIfVirtual( $virtualUrl );
1667
1668 return $this->backend->getFileTimestamp( [ 'src' => $path ] );
1669 }
1670
1677 public function getFileSize( $virtualUrl ) {
1678 $path = $this->resolveToStoragePathIfVirtual( $virtualUrl );
1679
1680 return $this->backend->getFileSize( [ 'src' => $path ] );
1681 }
1682
1689 public function getFileSha1( $virtualUrl ) {
1690 $path = $this->resolveToStoragePathIfVirtual( $virtualUrl );
1691
1692 return $this->backend->getFileSha1Base36( [ 'src' => $path ] );
1693 }
1694
1704 public function streamFileWithStatus( $virtualUrl, $headers = [], $optHeaders = [] ) {
1705 $path = $this->resolveToStoragePathIfVirtual( $virtualUrl );
1706 $params = [ 'src' => $path, 'headers' => $headers, 'options' => $optHeaders ];
1707
1708 // T172851: HHVM does not flush the output properly, causing OOM
1709 ob_start( null, 1_048_576 );
1710 ob_implicit_flush( true );
1711
1712 $status = $this->newGood()->merge( $this->backend->streamFile( $params ) );
1713
1714 // T186565: Close the buffer, unless it has already been closed
1715 // in HTTPFileStreamer::resetOutputBuffers().
1716 if ( ob_get_status() ) {
1717 ob_end_flush();
1718 }
1719
1720 return $status;
1721 }
1722
1731 public function enumFiles( $callback ) {
1732 $this->enumFilesInStorage( $callback );
1733 }
1734
1742 protected function enumFilesInStorage( $callback ) {
1743 $publicRoot = $this->getZonePath( 'public' );
1744 $numDirs = 1 << ( $this->hashLevels * 4 );
1745 // Use a priori assumptions about directory structure
1746 // to reduce the tree height of the scanning process.
1747 for ( $flatIndex = 0; $flatIndex < $numDirs; $flatIndex++ ) {
1748 $hexString = sprintf( "%0{$this->hashLevels}x", $flatIndex );
1749 $path = $publicRoot;
1750 for ( $hexPos = 0; $hexPos < $this->hashLevels; $hexPos++ ) {
1751 $path .= '/' . substr( $hexString, 0, $hexPos + 1 );
1752 }
1753 $iterator = $this->backend->getFileList( [ 'dir' => $path ] );
1754 if ( $iterator === null ) {
1755 throw new RuntimeException( __METHOD__ . ': could not get file listing for ' . $path );
1756 }
1757 foreach ( $iterator as $name ) {
1758 // Each item returned is a public file
1759 $callback( "{$path}/{$name}" );
1760 }
1761 }
1762 }
1763
1770 public function validateFilename( $filename ) {
1771 if ( strval( $filename ) == '' ) {
1772 return false;
1773 }
1774
1775 return FileBackend::isPathTraversalFree( $filename );
1776 }
1777
1783 private function getErrorCleanupFunction() {
1784 switch ( $this->pathDisclosureProtection ) {
1785 case 'none':
1786 case 'simple': // b/c
1787 $callback = $this->passThrough( ... );
1788 break;
1789 default: // 'paranoid'
1790 $callback = $this->paranoidClean( ... );
1791 }
1792 return $callback;
1793 }
1794
1801 public function paranoidClean( $param ) {
1802 return '[hidden]';
1803 }
1804
1811 public function passThrough( $param ) {
1812 return $param;
1813 }
1814
1822 public function newFatal( $message, ...$parameters ) {
1823 $status = Status::newFatal( $message, ...$parameters );
1824 $status->cleanCallback = $this->getErrorCleanupFunction();
1825
1826 return $status;
1827 }
1828
1835 public function newGood( $value = null ) {
1836 $status = Status::newGood( $value );
1837 $status->cleanCallback = $this->getErrorCleanupFunction();
1838
1839 return $status;
1840 }
1841
1850 public function checkRedirect( $title ) {
1851 return false;
1852 }
1853
1861 public function invalidateImageRedirect( $title ) {
1862 }
1863
1869 public function getDisplayName() {
1870 $sitename = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::Sitename );
1871
1872 if ( $this->isLocal() ) {
1873 return $sitename;
1874 }
1875
1876 // 'shared-repo-name-wikimediacommons' is used when $wgUseInstantCommons = true
1877 return wfMessageFallback( 'shared-repo-name-' . $this->name, 'shared-repo' )->text();
1878 }
1879
1887 public function nameForThumb( $name ) {
1888 if ( strlen( $name ) > $this->abbrvThreshold ) {
1889 $ext = FileBackend::extensionFromPath( $name );
1890 $name = ( $ext == '' ) ? 'thumbnail' : "thumbnail.$ext";
1891 }
1892
1893 return $name;
1894 }
1895
1901 public function isLocal() {
1902 return $this->getName() == 'local';
1903 }
1904
1916 public function getSharedCacheKey( $kClassSuffix, ...$components ) {
1917 return false;
1918 }
1919
1931 public function getLocalCacheKey( $kClassSuffix, ...$components ) {
1932 return $this->wanCache->makeKey(
1933 'filerepo-' . $kClassSuffix,
1934 $this->getName(),
1935 ...$components
1936 );
1937 }
1938
1947 public function getTempRepo() {
1948 return new TempFileRepo( [
1949 'name' => "{$this->name}-temp",
1950 'backend' => $this->backend,
1951 'zones' => [
1952 'public' => [
1953 // Same place storeTemp() uses in the base repo, though
1954 // the path hashing is mismatched, which is annoying.
1955 'container' => $this->zones['temp']['container'],
1956 'directory' => $this->zones['temp']['directory']
1957 ],
1958 'thumb' => [
1959 'container' => $this->zones['temp']['container'],
1960 'directory' => $this->zones['temp']['directory'] == ''
1961 ? 'thumb'
1962 : $this->zones['temp']['directory'] . '/thumb'
1963 ],
1964 'transcoded' => [
1965 'container' => $this->zones['temp']['container'],
1966 'directory' => $this->zones['temp']['directory'] == ''
1967 ? 'transcoded'
1968 : $this->zones['temp']['directory'] . '/transcoded'
1969 ]
1970 ],
1971 'hashLevels' => $this->hashLevels, // performance
1972 'isPrivate' => true // all in temp zone
1973 ] );
1974 }
1975
1982 public function getUploadStash( ?UserIdentity $user = null ) {
1983 return new UploadStash( $this, $user );
1984 }
1985
1992 protected function assertWritableRepo() {
1993 }
1994
2001 public function getInfo() {
2002 $ret = [
2003 'name' => $this->getName(),
2004 'displayname' => $this->getDisplayName(),
2005 'rootUrl' => $this->getZoneUrl( 'public' ),
2006 'local' => $this->isLocal(),
2007 ];
2008
2009 $optionalSettings = [
2010 'url',
2011 'thumbUrl',
2012 'initialCapital',
2013 'descBaseUrl',
2014 'scriptDirUrl',
2015 'articleUrl',
2016 'fetchDescription',
2017 'descriptionCacheExpiry',
2018 ];
2019 foreach ( $optionalSettings as $k ) {
2020 if ( $this->$k !== null ) {
2021 $ret[$k] = $this->$k;
2022 }
2023 }
2024 if ( $this->favicon !== null ) {
2025 // Expand any local path to full URL to improve API usability (T77093).
2026 $ret['favicon'] = MediaWikiServices::getInstance()->getUrlUtils()
2027 ->expand( $this->favicon );
2028 }
2029
2030 return $ret;
2031 }
2032
2037 public function hasSha1Storage() {
2038 return $this->hasSha1Storage;
2039 }
2040
2045 public function supportsSha1URLs() {
2046 return $this->supportsSha1URLs;
2047 }
2048}
2049
2051class_alias( FileRepo::class, 'FileRepo' );
const NS_FILE
Definition Defines.php:71
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfUrlencode( $s)
We want some things to be included as literal characters in our title URLs for prettiness,...
wfMessageFallback(... $keys)
This function accepts multiple message keys and returns a message instance for the first message whic...
wfAppendQuery( $url, $query)
Append a query string to an existing URL, which may or may not already have query string parameters a...
MimeMagic helper wrapper.
Group all the pieces relevant to the context of a request into one instance.
Base class for file repositories.
Definition FileRepo.php:68
addShellboxInputFile(BoxedCommand $command, string $boxedName, string $virtualUrl)
Add a file to a Shellbox command as an input file.
getLocalReference( $virtualUrl)
Get a local FS file with a given virtual URL/storage path.
static getHashPathForLevel( $name, $levels)
Definition FileRepo.php:786
findFile( $title, $options=[])
Find an instance of the named file created at the specified time Returns false if the file does not e...
Definition FileRepo.php:473
quickPurge( $path)
Purge a file from the repo.
getDescriptionStylesheetUrl()
Get the URL of the stylesheet to apply to description pages.
Definition FileRepo.php:904
fileExistsBatch(array $files)
Checks existence of an array of files.
initZones( $doZones=[])
Ensure that a single zone or list of zones is defined for usage.
Definition FileRepo.php:289
storeTemp( $originalName, $srcPath)
Pick a random name in the temp zone and store a file to it.
getDescriptionUrl( $name)
Get the URL of an image description page.
Definition FileRepo.php:845
getFileTimestamp( $virtualUrl)
Get the timestamp of a file with a given virtual URL/storage path.
string null $descBaseUrl
URL of image description pages, e.g.
Definition FileRepo.php:107
deleteBatch(array $sourceDestPairs)
Move a group of files to the deletion archive.
findBySha1( $hash)
Get an array or iterator of file objects for files that have a given SHA-1 content hash.
Definition FileRepo.php:645
bool $initialCapital
Equivalent to $wgCapitalLinks (or $wgCapitalLinkOverrides[NS_FILE], determines whether filenames impl...
Definition FileRepo.php:122
getNameFromTitle( $title)
Get the name of a file from its title.
Definition FileRepo.php:732
invalidateImageRedirect( $title)
Invalidates image redirect cache related to that image Doesn't do anything for repositories that don'...
getZoneLocation( $zone)
The storage container and base path of a zone.
Definition FileRepo.php:400
static isVirtualUrl( $url)
Determine if a string is an mwrepo:// URL.
Definition FileRepo.php:304
store( $srcPath, $dstZone, $dstRel, $flags=0)
Store a file to a given destination.
Definition FileRepo.php:931
resolveVirtualUrl( $url)
Get the backend storage path corresponding to a virtual URL.
Definition FileRepo.php:374
int $hashLevels
The number of directory levels for hash-based division of files.
Definition FileRepo.php:138
getDeletedHashPath( $key)
Get a relative path for a deletion archive key, e.g.
quickImport( $src, $dst, $options=null)
Import a file from the local file system into the repo.
passThrough( $param)
Path disclosure protection function.
bool $transformVia404
Whether to skip media file transformation on parse and rely on a 404 handler instead.
Definition FileRepo.php:102
cleanDir( $dir)
Deletes a directory if empty.
bool $isPrivate
Whether all zones should be private (e.g.
Definition FileRepo.php:153
canTransformLocally()
Returns true if the repository can transform files locally.
Definition FileRepo.php:722
checkRedirect( $title)
Checks if there is a redirect named as $title.
newGood( $value=null)
Create a new good result.
findBySha1s(array $hashes)
Get an array of arrays or iterators of file objects for files that have the given SHA-1 content hashe...
Definition FileRepo.php:656
string false $url
Public zone URL.
Definition FileRepo.php:132
findFiles(array $items, $flags=0)
Find many files at once.
Definition FileRepo.php:556
string $pathDisclosureProtection
May be 'paranoid' to remove all parameters from error messages, 'none' to leave the paths in unchange...
Definition FileRepo.php:129
null string $favicon
The URL to a favicon (optional, may be a server-local path URL).
Definition FileRepo.php:150
enumFiles( $callback)
Call a callback function for every public regular file in the repository.
publish( $src, $dstRel, $archiveRel, $flags=0, array $options=[])
Copy or move a file either from a storage path, virtual URL, or file system path, into this repositor...
getTempHashPath( $suffix)
Get a relative path including trailing slash, e.g.
Definition FileRepo.php:775
quickPurgeBatch(array $paths)
Purge a batch of files from the repo.
getName()
Get the name of this repository, as specified by $info['name]' to the constructor.
Definition FileRepo.php:814
getInfo()
Return information about the repository.
enumFilesInStorage( $callback)
Call a callback function for every public file in the repository.
newFile( $title, $time=false)
Create a new File object from the local repository.
Definition FileRepo.php:438
getDescriptionRenderUrl( $name, $lang=null)
Get the URL of the content-only fragment of the description page.
Definition FileRepo.php:879
assertWritableRepo()
Throw an exception if this repo is read-only by design.
publishBatch(array $ntuples, $flags=0)
Publish a batch of files.
string $thumbProxySecret
Secret key to pass as an X-Swift-Secret header to the proxied thumb service.
Definition FileRepo.php:169
cleanupDeletedBatch(array $storageKeys)
Delete files in the deleted directory if they are not referenced in the filearchive table.
getVirtualUrl( $suffix=false)
Get a URL referring to this repository, with the private mwrepo protocol.
Definition FileRepo.php:316
getLocalCacheKey( $kClassSuffix,... $components)
Get a site-local, repository-qualified, WAN cache key.
getRootDirectory()
Get the public zone root storage directory of the repository.
Definition FileRepo.php:753
canTransformVia404()
Returns true if the repository can transform files via a 404 handler.
Definition FileRepo.php:712
getUploadStash(?UserIdentity $user=null)
Get an UploadStash associated with this repo.
getThumbProxyUrl()
Get the URL thumb.php requests are being proxied to.
Definition FileRepo.php:694
fileExists( $file)
Checks existence of a file.
resolveToStoragePathIfVirtual( $path)
If a path is a virtual URL, resolve it to a storage path.
getBackend()
Get the file backend instance.
Definition FileRepo.php:270
array $zones
Map of zones to config.
Definition FileRepo.php:94
__construct(?array $info=null)
Definition FileRepo.php:189
getThumbScriptUrl()
Get the URL of thumb.php.
Definition FileRepo.php:685
findFilesByPrefix( $prefix, $limit)
Return an array of files where the name starts with $prefix.
Definition FileRepo.php:676
getTempRepo()
Get a temporary private FileRepo associated with this repo.
cleanupBatch(array $files, $flags=0)
Deletes a batch of files.
string null $scriptDirUrl
URL of the MediaWiki installation, equivalent to $wgScriptPath, e.g.
Definition FileRepo.php:112
initDirectory( $dir)
Creates a directory with the appropriate zone permissions.
string $thumbProxyUrl
URL of where to proxy thumb.php requests to.
Definition FileRepo.php:167
getFileSize( $virtualUrl)
Get the size of a file with a given virtual URL/storage path.
getLocalCopy( $virtualUrl)
Get a local FS copy of a file with a given virtual URL/storage path.
streamFileWithStatus( $virtualUrl, $headers=[], $optHeaders=[])
Attempt to stream a file with the given virtual URL/storage path.
callable false $oldFileFactoryKey
Override these in the base class.
Definition FileRepo.php:162
getFileSha1( $virtualUrl)
Get the sha1 (base 36) of a file with a given virtual URL/storage path.
nameForThumb( $name)
Get the portion of the file that contains the origin file name.
quickImportBatch(array $triples)
Import a batch of files from the local file system into the repo.
getZonePath( $zone)
Get the storage path corresponding to one of the zones.
Definition FileRepo.php:414
string false $thumbUrl
The base thumbnail URL.
Definition FileRepo.php:135
string $thumbScriptUrl
URL of thumb.php.
Definition FileRepo.php:97
int $deletedHashLevels
The number of directory levels for hash-based division of deleted files.
Definition FileRepo.php:141
getHashPath( $name)
Get a relative path including trailing slash, e.g.
Definition FileRepo.php:764
string null $articleUrl
Equivalent to $wgArticlePath, e.g.
Definition FileRepo.php:115
getThumbProxySecret()
Get the secret key for the proxied thumb service.
Definition FileRepo.php:703
newFatal( $message,... $parameters)
Create a new fatal error.
getDisplayName()
Get the human-readable name of the repo.
getFileProps( $virtualUrl)
Get properties of a file with a given virtual URL/storage path.
callable false $oldFileFactory
Override these in the base class.
Definition FileRepo.php:158
bool $fetchDescription
Whether to fetch commons image description pages and display them on the local wiki.
Definition FileRepo.php:79
findFileFromKey( $sha1, $options=[])
Find an instance of the file with this key, created at the specified time Returns false if the file d...
Definition FileRepo.php:601
concatenate(array $srcPaths, $dstPath, $flags=0)
Concatenate a list of temporary files into a target file location.
callable false $fileFactoryKey
Override these in the base class.
Definition FileRepo.php:160
callable $fileFactory
Override these in the base class.
Definition FileRepo.php:156
isLocal()
Returns true if this the local file repository.
getReadOnlyReason()
Get an explanatory message if this repo is read-only.
Definition FileRepo.php:280
getZoneUrl( $zone, $ext=null)
Get the URL corresponding to one of the four basic zones.
Definition FileRepo.php:332
freeTemp( $virtualUrl)
Remove a temporary file or mark it for garbage collection.
storeBatch(array $triplets, $flags=0)
Store a batch of files.
Definition FileRepo.php:955
getHashLevels()
Get the number of hash directory levels.
Definition FileRepo.php:805
bool $disableLocalTransform
Disable local image scaling.
Definition FileRepo.php:172
getSharedCacheKey( $kClassSuffix,... $components)
Get a global, repository-qualified, WAN cache key.
quickCleanDir( $dir)
Deletes a directory if empty.
supportsSha1URLs()
Returns whether or not repo supports having originals SHA-1s in the thumb URLs.
paranoidClean( $param)
Path disclosure protection function.
hasSha1Storage()
Returns whether or not storage is SHA-1 based.
makeUrl( $query='', $entry='index')
Make an url to this repo.
Definition FileRepo.php:825
int $abbrvThreshold
File names over this size will use the short form of thumbnail names.
Definition FileRepo.php:147
validateFilename( $filename)
Determine if a relative path is valid, i.e.
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:93
Local file in the wiki's own database.
Definition LocalFile.php:93
File without associated database record.
FileRepo for temporary files created by FileRepo::getTempRepo()
A class containing constants representing the names of configuration variables.
const Sitename
Name constant for the Sitename setting, for use with Config::get()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
Represents a title within MediaWiki.
Definition Title.php:78
Library for creating and parsing MW-style timestamps.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
UploadStash is intended to accomplish a few things:
Class representing a non-directory file on the file system.
Definition FSFile.php:34
This class is used to hold the location and do limited manipulation of files stored temporarily (this...
Base class for all file backend classes (including multi-write backends).
static isPathTraversalFree( $path)
Check if a relative path has no directory traversals.
static extensionFromPath( $path, $case='lowercase')
Get the final extension from a storage or FS path.
static splitStoragePath( $storagePath)
Split a storage path into a backend name, a container name, and a relative file path.
static isStoragePath( $path)
Check if a given path is a "mwstore://" path.
Multi-datacenter aware caching interface.
Represents the target of a wiki link.
Interface for objects (potentially) representing an editable wiki page.
This interface represents the authority associated with the current execution context,...
Definition Authority.php:37
Interface for objects representing user identity.
Interface for database access objects.
$source