MediaWiki  master
FileRepo.php
Go to the documentation of this file.
1 <?php
16 use Wikimedia\AtEase\AtEase;
17 
47 class FileRepo {
48  public const DELETE_SOURCE = 1;
49  public const OVERWRITE = 2;
50  public const OVERWRITE_SAME = 4;
51  public const SKIP_LOCKING = 8;
52 
53  public const NAME_AND_TIME_ONLY = 1;
54 
59 
62 
64  protected $hasSha1Storage = false;
65 
67  protected $supportsSha1URLs = false;
68 
70  protected $backend;
71 
73  protected $zones = [];
74 
76  protected $thumbScriptUrl;
77 
81  protected $transformVia404;
82 
86  protected $descBaseUrl;
87 
91  protected $scriptDirUrl;
92 
94  protected $articleUrl;
95 
101  protected $initialCapital;
102 
108  protected $pathDisclosureProtection = 'simple';
109 
111  protected $url;
112 
114  protected $thumbUrl;
115 
117  protected $hashLevels;
118 
121 
126  protected $abbrvThreshold;
127 
129  protected $favicon = null;
130 
132  protected $isPrivate;
133 
135  protected $fileFactory = [ UnregisteredLocalFile::class, 'newFromTitle' ];
137  protected $oldFileFactory = false;
139  protected $fileFactoryKey = false;
141  protected $oldFileFactoryKey = false;
142 
146  protected $thumbProxyUrl;
148  protected $thumbProxySecret;
149 
151  protected $disableLocalTransform = false;
152 
154  protected $wanCache;
155 
161  public $name;
162 
169  public function __construct( array $info = null ) {
170  // Verify required settings presence
171  if (
172  $info === null
173  || !array_key_exists( 'name', $info )
174  || !array_key_exists( 'backend', $info )
175  ) {
176  throw new MWException( __CLASS__ .
177  " requires an array of options having both 'name' and 'backend' keys.\n" );
178  }
179 
180  // Required settings
181  $this->name = $info['name'];
182  if ( $info['backend'] instanceof FileBackend ) {
183  $this->backend = $info['backend']; // useful for testing
184  } else {
185  $this->backend =
186  MediaWikiServices::getInstance()->getFileBackendGroup()->get( $info['backend'] );
187  }
188 
189  // Optional settings that can have no value
190  $optionalSettings = [
191  'descBaseUrl', 'scriptDirUrl', 'articleUrl', 'fetchDescription',
192  'thumbScriptUrl', 'pathDisclosureProtection', 'descriptionCacheExpiry',
193  'favicon', 'thumbProxyUrl', 'thumbProxySecret', 'disableLocalTransform'
194  ];
195  foreach ( $optionalSettings as $var ) {
196  if ( isset( $info[$var] ) ) {
197  $this->$var = $info[$var];
198  }
199  }
200 
201  // Optional settings that have a default
202  $localCapitalLinks =
203  MediaWikiServices::getInstance()->getNamespaceInfo()->isCapitalized( NS_FILE );
204  $this->initialCapital = $info['initialCapital'] ?? $localCapitalLinks;
205  if ( $localCapitalLinks && !$this->initialCapital ) {
206  // If the local wiki's file namespace requires an initial capital, but a foreign file
207  // repo doesn't, complications will result. Linker code will want to auto-capitalize the
208  // first letter of links to files, but those links might actually point to files on
209  // foreign wikis with initial-lowercase names. This combination is not likely to be
210  // used by anyone anyway, so we just outlaw it to save ourselves the bugs. If you want
211  // to include a foreign file repo with initialCapital false, set your local file
212  // namespace to not be capitalized either.
213  throw new InvalidArgumentException(
214  'File repos with initial capital false are not allowed on wikis where the File ' .
215  'namespace has initial capital true' );
216  }
217 
218  $this->url = $info['url'] ?? false; // a subclass may set the URL (e.g. ForeignAPIRepo)
219  $defaultThumbUrl = $this->url ? $this->url . '/thumb' : false;
220  $this->thumbUrl = $info['thumbUrl'] ?? $defaultThumbUrl;
221  $this->hashLevels = $info['hashLevels'] ?? 2;
222  $this->deletedHashLevels = $info['deletedHashLevels'] ?? $this->hashLevels;
223  $this->transformVia404 = !empty( $info['transformVia404'] );
224  $this->abbrvThreshold = $info['abbrvThreshold'] ?? 255;
225  $this->isPrivate = !empty( $info['isPrivate'] );
226  // Give defaults for the basic zones...
227  $this->zones = $info['zones'] ?? [];
228  foreach ( [ 'public', 'thumb', 'transcoded', 'temp', 'deleted' ] as $zone ) {
229  if ( !isset( $this->zones[$zone]['container'] ) ) {
230  $this->zones[$zone]['container'] = "{$this->name}-{$zone}";
231  }
232  if ( !isset( $this->zones[$zone]['directory'] ) ) {
233  $this->zones[$zone]['directory'] = '';
234  }
235  if ( !isset( $this->zones[$zone]['urlsByExt'] ) ) {
236  $this->zones[$zone]['urlsByExt'] = [];
237  }
238  }
239 
240  $this->supportsSha1URLs = !empty( $info['supportsSha1URLs'] );
241 
242  $this->wanCache = $info['wanCache'] ?? WANObjectCache::newEmpty();
243  }
244 
250  public function getBackend() {
251  return $this->backend;
252  }
253 
260  public function getReadOnlyReason() {
261  return $this->backend->getReadOnlyReason();
262  }
263 
270  protected function initZones( $doZones = [] ): void {
271  foreach ( (array)$doZones as $zone ) {
272  $root = $this->getZonePath( $zone );
273  if ( $root === null ) {
274  throw new MWException( "No '$zone' zone defined in the {$this->name} repo." );
275  }
276  }
277  }
278 
285  public static function isVirtualUrl( $url ) {
286  return substr( $url, 0, 9 ) == 'mwrepo://';
287  }
288 
297  public function getVirtualUrl( $suffix = false ) {
298  $path = 'mwrepo://' . $this->name;
299  if ( $suffix !== false ) {
300  $path .= '/' . rawurlencode( $suffix );
301  }
302 
303  return $path;
304  }
305 
313  public function getZoneUrl( $zone, $ext = null ) {
314  if ( in_array( $zone, [ 'public', 'thumb', 'transcoded' ] ) ) {
315  // standard public zones
316  if ( $ext !== null && isset( $this->zones[$zone]['urlsByExt'][$ext] ) ) {
317  // custom URL for extension/zone
318  // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
319  return $this->zones[$zone]['urlsByExt'][$ext];
320  } elseif ( isset( $this->zones[$zone]['url'] ) ) {
321  // custom URL for zone
322  return $this->zones[$zone]['url'];
323  }
324  }
325  switch ( $zone ) {
326  case 'public':
327  return $this->url;
328  case 'temp':
329  case 'deleted':
330  return false; // no public URL
331  case 'thumb':
332  return $this->thumbUrl;
333  case 'transcoded':
334  return "{$this->url}/transcoded";
335  default:
336  return false;
337  }
338  }
339 
343  public function backendSupportsUnicodePaths() {
344  return (bool)( $this->getBackend()->getFeatures() & FileBackend::ATTR_UNICODE_PATHS );
345  }
346 
355  public function resolveVirtualUrl( $url ) {
356  if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
357  throw new MWException( __METHOD__ . ': unknown protocol' );
358  }
359  $bits = explode( '/', substr( $url, 9 ), 3 );
360  if ( count( $bits ) != 3 ) {
361  throw new MWException( __METHOD__ . ": invalid mwrepo URL: $url" );
362  }
363  list( $repo, $zone, $rel ) = $bits;
364  if ( $repo !== $this->name ) {
365  throw new MWException( __METHOD__ . ": fetching from a foreign repo is not supported" );
366  }
367  $base = $this->getZonePath( $zone );
368  if ( !$base ) {
369  throw new MWException( __METHOD__ . ": invalid zone: $zone" );
370  }
371 
372  return $base . '/' . rawurldecode( $rel );
373  }
374 
381  protected function getZoneLocation( $zone ) {
382  if ( !isset( $this->zones[$zone] ) ) {
383  return [ null, null ]; // bogus
384  }
385 
386  return [ $this->zones[$zone]['container'], $this->zones[$zone]['directory'] ];
387  }
388 
395  public function getZonePath( $zone ) {
396  list( $container, $base ) = $this->getZoneLocation( $zone );
397  if ( $container === null || $base === null ) {
398  return null;
399  }
400  $backendName = $this->backend->getName();
401  if ( $base != '' ) { // may not be set
402  $base = "/{$base}";
403  }
404 
405  return "mwstore://$backendName/{$container}{$base}";
406  }
407 
419  public function newFile( $title, $time = false ) {
421  if ( !$title ) {
422  return null;
423  }
424  if ( $time ) {
425  if ( $this->oldFileFactory ) {
426  return call_user_func( $this->oldFileFactory, $title, $this, $time );
427  } else {
428  return null;
429  }
430  } else {
431  return call_user_func( $this->fileFactory, $title, $this );
432  }
433  }
434 
454  public function findFile( $title, $options = [] ) {
455  if ( !empty( $options['private'] ) && !( $options['private'] instanceof Authority ) ) {
456  throw new InvalidArgumentException(
457  __METHOD__ . ' called with the `private` option set to something ' .
458  'other than an Authority object'
459  );
460  }
461 
463  if ( !$title ) {
464  return false;
465  }
466  if ( isset( $options['bypassCache'] ) ) {
467  $options['latest'] = $options['bypassCache']; // b/c
468  }
469  $time = $options['time'] ?? false;
470  $flags = !empty( $options['latest'] ) ? File::READ_LATEST : 0;
471  # First try the current version of the file to see if it precedes the timestamp
472  $img = $this->newFile( $title );
473  if ( !$img ) {
474  return false;
475  }
476  $img->load( $flags );
477  if ( $img->exists() && ( !$time || $img->getTimestamp() == $time ) ) {
478  return $img;
479  }
480  # Now try an old version of the file
481  if ( $time !== false ) {
482  $img = $this->newFile( $title, $time );
483  if ( $img ) {
484  $img->load( $flags );
485  if ( $img->exists() ) {
486  if ( !$img->isDeleted( File::DELETED_FILE ) ) {
487  return $img; // always OK
488  } elseif (
489  // If its not empty, its an Authority object
490  !empty( $options['private'] ) &&
491  $img->userCan( File::DELETED_FILE, $options['private'] )
492  ) {
493  return $img;
494  }
495  }
496  }
497  }
498 
499  # Now try redirects
500  if ( !empty( $options['ignoreRedirect'] ) ) {
501  return false;
502  }
503  $redir = $this->checkRedirect( $title );
504  if ( $redir && $title->getNamespace() === NS_FILE ) {
505  $img = $this->newFile( $redir );
506  if ( !$img ) {
507  return false;
508  }
509  $img->load( $flags );
510  if ( $img->exists() ) {
511  $img->redirectedFrom( $title->getDBkey() );
512 
513  return $img;
514  }
515  }
516 
517  return false;
518  }
519 
537  public function findFiles( array $items, $flags = 0 ) {
538  $result = [];
539  foreach ( $items as $item ) {
540  if ( is_array( $item ) ) {
541  $title = $item['title'];
542  $options = $item;
543  unset( $options['title'] );
544 
545  if (
546  !empty( $options['private'] ) &&
547  !( $options['private'] instanceof Authority )
548  ) {
549  $options['private'] = RequestContext::getMain()->getAuthority();
550  }
551  } else {
552  $title = $item;
553  $options = [];
554  }
555  $file = $this->findFile( $title, $options );
556  if ( $file ) {
557  $searchName = File::normalizeTitle( $title )->getDBkey(); // must be valid
558  if ( $flags & self::NAME_AND_TIME_ONLY ) {
559  $result[$searchName] = [
560  'title' => $file->getTitle()->getDBkey(),
561  'timestamp' => $file->getTimestamp()
562  ];
563  } else {
564  $result[$searchName] = $file;
565  }
566  }
567  }
568 
569  return $result;
570  }
571 
582  public function findFileFromKey( $sha1, $options = [] ) {
583  if ( !empty( $options['private'] ) && !( $options['private'] instanceof Authority ) ) {
584  throw new InvalidArgumentException(
585  __METHOD__ . ' called with the `private` option set to something ' .
586  'other than an Authority object'
587  );
588  }
589 
590  $time = $options['time'] ?? false;
591  # First try to find a matching current version of a file...
592  if ( !$this->fileFactoryKey ) {
593  return false; // find-by-sha1 not supported
594  }
595  $img = call_user_func( $this->fileFactoryKey, $sha1, $this, $time );
596  if ( $img && $img->exists() ) {
597  return $img;
598  }
599  # Now try to find a matching old version of a file...
600  if ( $time !== false && $this->oldFileFactoryKey ) { // find-by-sha1 supported?
601  $img = call_user_func( $this->oldFileFactoryKey, $sha1, $this, $time );
602  if ( $img && $img->exists() ) {
603  if ( !$img->isDeleted( File::DELETED_FILE ) ) {
604  return $img; // always OK
605  } elseif (
606  // If its not empty, its an Authority object
607  !empty( $options['private'] ) &&
608  $img->userCan( File::DELETED_FILE, $options['private'] )
609  ) {
610  return $img;
611  }
612  }
613  }
614 
615  return false;
616  }
617 
626  public function findBySha1( $hash ) {
627  return [];
628  }
629 
637  public function findBySha1s( array $hashes ) {
638  $result = [];
639  foreach ( $hashes as $hash ) {
640  $files = $this->findBySha1( $hash );
641  if ( count( $files ) ) {
642  $result[$hash] = $files;
643  }
644  }
645 
646  return $result;
647  }
648 
657  public function findFilesByPrefix( $prefix, $limit ) {
658  return [];
659  }
660 
666  public function getThumbScriptUrl() {
667  return $this->thumbScriptUrl;
668  }
669 
675  public function getThumbProxyUrl() {
676  return $this->thumbProxyUrl;
677  }
678 
684  public function getThumbProxySecret() {
685  return $this->thumbProxySecret;
686  }
687 
693  public function canTransformVia404() {
694  return $this->transformVia404;
695  }
696 
703  public function canTransformLocally() {
704  return !$this->disableLocalTransform;
705  }
706 
713  public function getNameFromTitle( $title ) {
714  if (
715  $this->initialCapital !=
716  MediaWikiServices::getInstance()->getNamespaceInfo()->isCapitalized( NS_FILE )
717  ) {
718  $name = $title->getDBkey();
719  if ( $this->initialCapital ) {
720  $name = MediaWikiServices::getInstance()->getContentLanguage()->ucfirst( $name );
721  }
722  } else {
723  $name = $title->getDBkey();
724  }
725 
726  return $name;
727  }
728 
734  public function getRootDirectory() {
735  return $this->getZonePath( 'public' );
736  }
737 
745  public function getHashPath( $name ) {
746  return self::getHashPathForLevel( $name, $this->hashLevels );
747  }
748 
756  public function getTempHashPath( $suffix ) {
757  $parts = explode( '!', $suffix, 2 ); // format is <timestamp>!<name> or just <name>
758  $name = $parts[1] ?? $suffix; // hash path is not based on timestamp
759  return self::getHashPathForLevel( $name, $this->hashLevels );
760  }
761 
767  protected static function getHashPathForLevel( $name, $levels ) {
768  if ( $levels == 0 ) {
769  return '';
770  } else {
771  $hash = md5( $name );
772  $path = '';
773  for ( $i = 1; $i <= $levels; $i++ ) {
774  $path .= substr( $hash, 0, $i ) . '/';
775  }
776 
777  return $path;
778  }
779  }
780 
786  public function getHashLevels() {
787  return $this->hashLevels;
788  }
789 
795  public function getName() {
796  return $this->name;
797  }
798 
806  public function makeUrl( $query = '', $entry = 'index' ) {
807  if ( isset( $this->scriptDirUrl ) ) {
808  return wfAppendQuery( "{$this->scriptDirUrl}/{$entry}.php", $query );
809  }
810 
811  return false;
812  }
813 
826  public function getDescriptionUrl( $name ) {
827  $encName = wfUrlencode( $name );
828  if ( $this->descBaseUrl !== null ) {
829  # "http://example.com/wiki/File:"
830  return $this->descBaseUrl . $encName;
831  }
832  if ( $this->articleUrl !== null ) {
833  # "http://example.com/wiki/$1"
834  # We use "Image:" as the canonical namespace for
835  # compatibility across all MediaWiki versions.
836  return str_replace( '$1',
837  "Image:$encName", $this->articleUrl );
838  }
839  if ( $this->scriptDirUrl !== null ) {
840  # "http://example.com/w"
841  # We use "Image:" as the canonical namespace for
842  # compatibility across all MediaWiki versions,
843  # and just sort of hope index.php is right. ;)
844  return $this->makeUrl( "title=Image:$encName" );
845  }
846 
847  return false;
848  }
849 
860  public function getDescriptionRenderUrl( $name, $lang = null ) {
861  $query = 'action=render';
862  if ( $lang !== null ) {
863  $query .= '&uselang=' . urlencode( $lang );
864  }
865  if ( isset( $this->scriptDirUrl ) ) {
866  return $this->makeUrl(
867  'title=' .
868  wfUrlencode( 'Image:' . $name ) .
869  "&$query" );
870  } else {
871  $descUrl = $this->getDescriptionUrl( $name );
872  if ( $descUrl ) {
873  return wfAppendQuery( $descUrl, $query );
874  } else {
875  return false;
876  }
877  }
878  }
879 
885  public function getDescriptionStylesheetUrl() {
886  if ( isset( $this->scriptDirUrl ) ) {
887  // Must match canonical query parameter order for optimum caching
888  // See HtmlCacheUpdater::getUrls
889  return $this->makeUrl( 'title=MediaWiki:Filepage.css&action=raw&ctype=text/css' );
890  }
891 
892  return false;
893  }
894 
912  public function store( $srcPath, $dstZone, $dstRel, $flags = 0 ) {
913  $this->assertWritableRepo(); // fail out if read-only
914 
915  $status = $this->storeBatch( [ [ $srcPath, $dstZone, $dstRel ] ], $flags );
916  if ( $status->successCount == 0 ) {
917  $status->setOK( false );
918  }
919 
920  return $status;
921  }
922 
937  public function storeBatch( array $triplets, $flags = 0 ) {
938  $this->assertWritableRepo(); // fail out if read-only
939 
940  if ( $flags & self::DELETE_SOURCE ) {
941  throw new InvalidArgumentException( "DELETE_SOURCE not supported in " . __METHOD__ );
942  }
943 
944  $status = $this->newGood();
945  $backend = $this->backend; // convenience
946 
947  $operations = [];
948  // Validate each triplet and get the store operation...
949  foreach ( $triplets as $triplet ) {
950  list( $src, $dstZone, $dstRel ) = $triplet;
951  $srcPath = ( $src instanceof FSFile ) ? $src->getPath() : $src;
952  wfDebug( __METHOD__
953  . "( \$src='$srcPath', \$dstZone='$dstZone', \$dstRel='$dstRel' )"
954  );
955  // Resolve source path
956  if ( $src instanceof FSFile ) {
957  $op = 'store';
958  } else {
959  $src = $this->resolveToStoragePathIfVirtual( $src );
960  $op = FileBackend::isStoragePath( $src ) ? 'copy' : 'store';
961  }
962  // Resolve destination path
963  $root = $this->getZonePath( $dstZone );
964  if ( !$root ) {
965  throw new MWException( "Invalid zone: $dstZone" );
966  }
967  if ( !$this->validateFilename( $dstRel ) ) {
968  throw new MWException( 'Validation error in $dstRel' );
969  }
970  $dstPath = "$root/$dstRel";
971  $dstDir = dirname( $dstPath );
972  // Create destination directories for this triplet
973  if ( !$this->initDirectory( $dstDir )->isOK() ) {
974  return $this->newFatal( 'directorycreateerror', $dstDir );
975  }
976 
977  // Copy the source file to the destination
978  $operations[] = [
979  'op' => $op,
980  'src' => $src, // storage path (copy) or local file path (store)
981  'dst' => $dstPath,
982  'overwrite' => ( $flags & self::OVERWRITE ) ? true : false,
983  'overwriteSame' => ( $flags & self::OVERWRITE_SAME ) ? true : false,
984  ];
985  }
986 
987  // Execute the store operation for each triplet
988  $opts = [ 'force' => true ];
989  if ( $flags & self::SKIP_LOCKING ) {
990  $opts['nonLocking'] = true;
991  }
992 
993  return $status->merge( $backend->doOperations( $operations, $opts ) );
994  }
995 
1006  public function cleanupBatch( array $files, $flags = 0 ) {
1007  $this->assertWritableRepo(); // fail out if read-only
1008 
1009  $status = $this->newGood();
1010 
1011  $operations = [];
1012  foreach ( $files as $path ) {
1013  if ( is_array( $path ) ) {
1014  // This is a pair, extract it
1015  list( $zone, $rel ) = $path;
1016  $path = $this->getZonePath( $zone ) . "/$rel";
1017  } else {
1018  // Resolve source to a storage path if virtual
1019  $path = $this->resolveToStoragePathIfVirtual( $path );
1020  }
1021  $operations[] = [ 'op' => 'delete', 'src' => $path ];
1022  }
1023  // Actually delete files from storage...
1024  $opts = [ 'force' => true ];
1025  if ( $flags & self::SKIP_LOCKING ) {
1026  $opts['nonLocking'] = true;
1027  }
1028 
1029  return $status->merge( $this->backend->doOperations( $operations, $opts ) );
1030  }
1031 
1049  final public function quickImport( $src, $dst, $options = null ) {
1050  return $this->quickImportBatch( [ [ $src, $dst, $options ] ] );
1051  }
1052 
1067  public function quickImportBatch( array $triples ) {
1068  $status = $this->newGood();
1069  $operations = [];
1070  foreach ( $triples as $triple ) {
1071  list( $src, $dst ) = $triple;
1072  if ( $src instanceof FSFile ) {
1073  $op = 'store';
1074  } else {
1075  $src = $this->resolveToStoragePathIfVirtual( $src );
1076  $op = FileBackend::isStoragePath( $src ) ? 'copy' : 'store';
1077  }
1078  $dst = $this->resolveToStoragePathIfVirtual( $dst );
1079 
1080  if ( !isset( $triple[2] ) ) {
1081  $headers = [];
1082  } elseif ( is_string( $triple[2] ) ) {
1083  // back-compat
1084  $headers = [ 'Content-Disposition' => $triple[2] ];
1085  } elseif ( is_array( $triple[2] ) && isset( $triple[2]['headers'] ) ) {
1086  $headers = $triple[2]['headers'];
1087  } else {
1088  $headers = [];
1089  }
1090 
1091  $operations[] = [
1092  'op' => $op,
1093  'src' => $src, // storage path (copy) or local path/FSFile (store)
1094  'dst' => $dst,
1095  'headers' => $headers
1096  ];
1097  $status->merge( $this->initDirectory( dirname( $dst ) ) );
1098  }
1099 
1100  return $status->merge( $this->backend->doQuickOperations( $operations ) );
1101  }
1102 
1111  final public function quickPurge( $path ) {
1112  return $this->quickPurgeBatch( [ $path ] );
1113  }
1114 
1122  public function quickCleanDir( $dir ) {
1123  return $this->newGood()->merge(
1124  $this->backend->clean(
1125  [ 'dir' => $this->resolveToStoragePathIfVirtual( $dir ) ]
1126  )
1127  );
1128  }
1129 
1138  public function quickPurgeBatch( array $paths ) {
1139  $status = $this->newGood();
1140  $operations = [];
1141  foreach ( $paths as $path ) {
1142  $operations[] = [
1143  'op' => 'delete',
1144  'src' => $this->resolveToStoragePathIfVirtual( $path ),
1145  'ignoreMissingSource' => true
1146  ];
1147  }
1148  $status->merge( $this->backend->doQuickOperations( $operations ) );
1149 
1150  return $status;
1151  }
1152 
1163  public function storeTemp( $originalName, $srcPath ) {
1164  $this->assertWritableRepo(); // fail out if read-only
1165 
1166  $date = MWTimestamp::getInstance()->format( 'YmdHis' );
1167  $hashPath = $this->getHashPath( $originalName );
1168  $dstUrlRel = $hashPath . $date . '!' . rawurlencode( $originalName );
1169  $virtualUrl = $this->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
1170 
1171  $result = $this->quickImport( $srcPath, $virtualUrl );
1172  $result->value = $virtualUrl;
1173 
1174  return $result;
1175  }
1176 
1183  public function freeTemp( $virtualUrl ) {
1184  $this->assertWritableRepo(); // fail out if read-only
1185 
1186  $temp = $this->getVirtualUrl( 'temp' );
1187  if ( !str_starts_with( $virtualUrl, $temp ) ) {
1188  wfDebug( __METHOD__ . ": Invalid temp virtual URL" );
1189 
1190  return false;
1191  }
1192 
1193  return $this->quickPurge( $virtualUrl )->isOK();
1194  }
1195 
1205  public function concatenate( array $srcPaths, $dstPath, $flags = 0 ) {
1206  $this->assertWritableRepo(); // fail out if read-only
1207 
1208  $status = $this->newGood();
1209 
1210  $sources = [];
1211  foreach ( $srcPaths as $srcPath ) {
1212  // Resolve source to a storage path if virtual
1213  $source = $this->resolveToStoragePathIfVirtual( $srcPath );
1214  $sources[] = $source; // chunk to merge
1215  }
1216 
1217  // Concatenate the chunks into one FS file
1218  $params = [ 'srcs' => $sources, 'dst' => $dstPath ];
1219  $status->merge( $this->backend->concatenate( $params ) );
1220  if ( !$status->isOK() ) {
1221  return $status;
1222  }
1223 
1224  // Delete the sources if required
1225  if ( $flags & self::DELETE_SOURCE ) {
1226  $status->merge( $this->quickPurgeBatch( $srcPaths ) );
1227  }
1228 
1229  // Make sure status is OK, despite any quickPurgeBatch() fatals
1230  $status->setResult( true );
1231 
1232  return $status;
1233  }
1234 
1258  public function publish(
1259  $src, $dstRel, $archiveRel, $flags = 0, array $options = []
1260  ) {
1261  $this->assertWritableRepo(); // fail out if read-only
1262 
1263  $status = $this->publishBatch(
1264  [ [ $src, $dstRel, $archiveRel, $options ] ], $flags );
1265  if ( $status->successCount == 0 ) {
1266  $status->setOK( false );
1267  }
1268  $status->value = $status->value[0] ?? false;
1269 
1270  return $status;
1271  }
1272 
1285  public function publishBatch( array $ntuples, $flags = 0 ) {
1286  $this->assertWritableRepo(); // fail out if read-only
1287 
1288  $backend = $this->backend; // convenience
1289  // Try creating directories
1290  $this->initZones( 'public' );
1291 
1292  $status = $this->newGood( [] );
1293 
1294  $operations = [];
1295  $sourceFSFilesToDelete = []; // cleanup for disk source files
1296  // Validate each triplet and get the store operation...
1297  foreach ( $ntuples as $ntuple ) {
1298  list( $src, $dstRel, $archiveRel ) = $ntuple;
1299  $srcPath = ( $src instanceof FSFile ) ? $src->getPath() : $src;
1300 
1301  $options = $ntuple[3] ?? [];
1302  // Resolve source to a storage path if virtual
1303  $srcPath = $this->resolveToStoragePathIfVirtual( $srcPath );
1304  if ( !$this->validateFilename( $dstRel ) ) {
1305  throw new MWException( 'Validation error in $dstRel' );
1306  }
1307  if ( !$this->validateFilename( $archiveRel ) ) {
1308  throw new MWException( 'Validation error in $archiveRel' );
1309  }
1310 
1311  $publicRoot = $this->getZonePath( 'public' );
1312  $dstPath = "$publicRoot/$dstRel";
1313  $archivePath = "$publicRoot/$archiveRel";
1314 
1315  $dstDir = dirname( $dstPath );
1316  $archiveDir = dirname( $archivePath );
1317  // Abort immediately on directory creation errors since they're likely to be repetitive
1318  if ( !$this->initDirectory( $dstDir )->isOK() ) {
1319  return $this->newFatal( 'directorycreateerror', $dstDir );
1320  }
1321  if ( !$this->initDirectory( $archiveDir )->isOK() ) {
1322  return $this->newFatal( 'directorycreateerror', $archiveDir );
1323  }
1324 
1325  // Set any desired headers to be use in GET/HEAD responses
1326  $headers = $options['headers'] ?? [];
1327 
1328  // Archive destination file if it exists.
1329  // This will check if the archive file also exists and fail if does.
1330  // This is a check to avoid data loss. On Windows and Linux,
1331  // copy() will overwrite, so the existence check is vulnerable to
1332  // race conditions unless a functioning LockManager is used.
1333  // LocalFile also uses SELECT FOR UPDATE for synchronization.
1334  $operations[] = [
1335  'op' => 'copy',
1336  'src' => $dstPath,
1337  'dst' => $archivePath,
1338  'ignoreMissingSource' => true
1339  ];
1340 
1341  // Copy (or move) the source file to the destination
1342  if ( FileBackend::isStoragePath( $srcPath ) ) {
1343  $operations[] = [
1344  'op' => ( $flags & self::DELETE_SOURCE ) ? 'move' : 'copy',
1345  'src' => $srcPath,
1346  'dst' => $dstPath,
1347  'overwrite' => true, // replace current
1348  'headers' => $headers
1349  ];
1350  } else {
1351  $operations[] = [
1352  'op' => 'store',
1353  'src' => $src, // storage path (copy) or local path/FSFile (store)
1354  'dst' => $dstPath,
1355  'overwrite' => true, // replace current
1356  'headers' => $headers
1357  ];
1358  if ( $flags & self::DELETE_SOURCE ) {
1359  $sourceFSFilesToDelete[] = $srcPath;
1360  }
1361  }
1362  }
1363 
1364  // Execute the operations for each triplet
1365  $status->merge( $backend->doOperations( $operations ) );
1366  // Find out which files were archived...
1367  foreach ( $ntuples as $i => $ntuple ) {
1368  list( , , $archiveRel ) = $ntuple;
1369  $archivePath = $this->getZonePath( 'public' ) . "/$archiveRel";
1370  if ( $this->fileExists( $archivePath ) ) {
1371  $status->value[$i] = 'archived';
1372  } else {
1373  $status->value[$i] = 'new';
1374  }
1375  }
1376  // Cleanup for disk source files...
1377  foreach ( $sourceFSFilesToDelete as $file ) {
1378  AtEase::suppressWarnings();
1379  unlink( $file ); // FS cleanup
1380  AtEase::restoreWarnings();
1381  }
1382 
1383  return $status;
1384  }
1385 
1393  protected function initDirectory( $dir ) {
1394  $path = $this->resolveToStoragePathIfVirtual( $dir );
1395  list( , $container, ) = FileBackend::splitStoragePath( $path );
1396 
1397  $params = [ 'dir' => $path ];
1398  if ( $this->isPrivate
1399  || $container === $this->zones['deleted']['container']
1400  || $container === $this->zones['temp']['container']
1401  ) {
1402  # Take all available measures to prevent web accessibility of new deleted
1403  # directories, in case the user has not configured offline storage
1404  $params = [ 'noAccess' => true, 'noListing' => true ] + $params;
1405  }
1406 
1407  return $this->newGood()->merge( $this->backend->prepare( $params ) );
1408  }
1409 
1416  public function cleanDir( $dir ) {
1417  $this->assertWritableRepo(); // fail out if read-only
1418 
1419  return $this->newGood()->merge(
1420  $this->backend->clean(
1421  [ 'dir' => $this->resolveToStoragePathIfVirtual( $dir ) ]
1422  )
1423  );
1424  }
1425 
1432  public function fileExists( $file ) {
1433  $result = $this->fileExistsBatch( [ $file ] );
1434 
1435  return $result[0];
1436  }
1437 
1445  public function fileExistsBatch( array $files ) {
1446  $paths = array_map( [ $this, 'resolveToStoragePathIfVirtual' ], $files );
1447  $this->backend->preloadFileStat( [ 'srcs' => $paths ] );
1448 
1449  $result = [];
1450  foreach ( $files as $key => $file ) {
1451  $path = $this->resolveToStoragePathIfVirtual( $file );
1452  $result[$key] = $this->backend->fileExists( [ 'src' => $path ] );
1453  }
1454 
1455  return $result;
1456  }
1457 
1468  public function delete( $srcRel, $archiveRel ) {
1469  $this->assertWritableRepo(); // fail out if read-only
1470 
1471  return $this->deleteBatch( [ [ $srcRel, $archiveRel ] ] );
1472  }
1473 
1491  public function deleteBatch( array $sourceDestPairs ) {
1492  $this->assertWritableRepo(); // fail out if read-only
1493 
1494  // Try creating directories
1495  $this->initZones( [ 'public', 'deleted' ] );
1496 
1497  $status = $this->newGood();
1498 
1499  $backend = $this->backend; // convenience
1500  $operations = [];
1501  // Validate filenames and create archive directories
1502  foreach ( $sourceDestPairs as [ $srcRel, $archiveRel ] ) {
1503  if ( !$this->validateFilename( $srcRel ) ) {
1504  throw new MWException( __METHOD__ . ':Validation error in $srcRel' );
1505  } elseif ( !$this->validateFilename( $archiveRel ) ) {
1506  throw new MWException( __METHOD__ . ':Validation error in $archiveRel' );
1507  }
1508 
1509  $publicRoot = $this->getZonePath( 'public' );
1510  $srcPath = "{$publicRoot}/$srcRel";
1511 
1512  $deletedRoot = $this->getZonePath( 'deleted' );
1513  $archivePath = "{$deletedRoot}/{$archiveRel}";
1514  $archiveDir = dirname( $archivePath ); // does not touch FS
1515 
1516  // Create destination directories
1517  if ( !$this->initDirectory( $archiveDir )->isGood() ) {
1518  return $this->newFatal( 'directorycreateerror', $archiveDir );
1519  }
1520 
1521  $operations[] = [
1522  'op' => 'move',
1523  'src' => $srcPath,
1524  'dst' => $archivePath,
1525  // We may have 2+ identical files being deleted,
1526  // all of which will map to the same destination file
1527  'overwriteSame' => true // also see T33792
1528  ];
1529  }
1530 
1531  // Move the files by execute the operations for each pair.
1532  // We're now committed to returning an OK result, which will
1533  // lead to the files being moved in the DB also.
1534  $opts = [ 'force' => true ];
1535  return $status->merge( $backend->doOperations( $operations, $opts ) );
1536  }
1537 
1544  public function cleanupDeletedBatch( array $storageKeys ) {
1545  $this->assertWritableRepo();
1546  }
1547 
1556  public function getDeletedHashPath( $key ) {
1557  if ( strlen( $key ) < 31 ) {
1558  throw new MWException( "Invalid storage key '$key'." );
1559  }
1560  $path = '';
1561  for ( $i = 0; $i < $this->deletedHashLevels; $i++ ) {
1562  $path .= $key[$i] . '/';
1563  }
1564 
1565  return $path;
1566  }
1567 
1576  protected function resolveToStoragePathIfVirtual( $path ) {
1577  if ( self::isVirtualUrl( $path ) ) {
1578  return $this->resolveVirtualUrl( $path );
1579  }
1580 
1581  return $path;
1582  }
1583 
1591  public function getLocalCopy( $virtualUrl ) {
1592  $path = $this->resolveToStoragePathIfVirtual( $virtualUrl );
1593 
1594  return $this->backend->getLocalCopy( [ 'src' => $path ] );
1595  }
1596 
1605  public function getLocalReference( $virtualUrl ) {
1606  $path = $this->resolveToStoragePathIfVirtual( $virtualUrl );
1607 
1608  return $this->backend->getLocalReference( [ 'src' => $path ] );
1609  }
1610 
1618  public function getFileProps( $virtualUrl ) {
1619  $fsFile = $this->getLocalReference( $virtualUrl );
1620  $mwProps = new MWFileProps( MediaWikiServices::getInstance()->getMimeAnalyzer() );
1621  if ( $fsFile ) {
1622  $props = $mwProps->getPropsFromPath( $fsFile->getPath(), true );
1623  } else {
1624  $props = $mwProps->newPlaceholderProps();
1625  }
1626 
1627  return $props;
1628  }
1629 
1636  public function getFileTimestamp( $virtualUrl ) {
1637  $path = $this->resolveToStoragePathIfVirtual( $virtualUrl );
1638 
1639  return $this->backend->getFileTimestamp( [ 'src' => $path ] );
1640  }
1641 
1648  public function getFileSize( $virtualUrl ) {
1649  $path = $this->resolveToStoragePathIfVirtual( $virtualUrl );
1650 
1651  return $this->backend->getFileSize( [ 'src' => $path ] );
1652  }
1653 
1660  public function getFileSha1( $virtualUrl ) {
1661  $path = $this->resolveToStoragePathIfVirtual( $virtualUrl );
1662 
1663  return $this->backend->getFileSha1Base36( [ 'src' => $path ] );
1664  }
1665 
1675  public function streamFileWithStatus( $virtualUrl, $headers = [], $optHeaders = [] ) {
1676  $path = $this->resolveToStoragePathIfVirtual( $virtualUrl );
1677  $params = [ 'src' => $path, 'headers' => $headers, 'options' => $optHeaders ];
1678 
1679  // T172851: HHVM does not flush the output properly, causing OOM
1680  ob_start( null, 1048576 );
1681  ob_implicit_flush( true );
1682 
1683  $status = $this->newGood()->merge( $this->backend->streamFile( $params ) );
1684 
1685  // T186565: Close the buffer, unless it has already been closed
1686  // in HTTPFileStreamer::resetOutputBuffers().
1687  if ( ob_get_status() ) {
1688  ob_end_flush();
1689  }
1690 
1691  return $status;
1692  }
1693 
1702  public function enumFiles( $callback ) {
1703  $this->enumFilesInStorage( $callback );
1704  }
1705 
1713  protected function enumFilesInStorage( $callback ) {
1714  $publicRoot = $this->getZonePath( 'public' );
1715  $numDirs = 1 << ( $this->hashLevels * 4 );
1716  // Use a priori assumptions about directory structure
1717  // to reduce the tree height of the scanning process.
1718  for ( $flatIndex = 0; $flatIndex < $numDirs; $flatIndex++ ) {
1719  $hexString = sprintf( "%0{$this->hashLevels}x", $flatIndex );
1720  $path = $publicRoot;
1721  for ( $hexPos = 0; $hexPos < $this->hashLevels; $hexPos++ ) {
1722  $path .= '/' . substr( $hexString, 0, $hexPos + 1 );
1723  }
1724  $iterator = $this->backend->getFileList( [ 'dir' => $path ] );
1725  if ( $iterator === null ) {
1726  throw new MWException( __METHOD__ . ': could not get file listing for ' . $path );
1727  }
1728  foreach ( $iterator as $name ) {
1729  // Each item returned is a public file
1730  call_user_func( $callback, "{$path}/{$name}" );
1731  }
1732  }
1733  }
1734 
1741  public function validateFilename( $filename ) {
1742  if ( strval( $filename ) == '' ) {
1743  return false;
1744  }
1745 
1746  return FileBackend::isPathTraversalFree( $filename );
1747  }
1748 
1754  private function getErrorCleanupFunction() {
1755  switch ( $this->pathDisclosureProtection ) {
1756  case 'none':
1757  case 'simple': // b/c
1758  $callback = [ $this, 'passThrough' ];
1759  break;
1760  default: // 'paranoid'
1761  $callback = [ $this, 'paranoidClean' ];
1762  }
1763  return $callback;
1764  }
1765 
1772  public function paranoidClean( $param ) {
1773  return '[hidden]';
1774  }
1775 
1782  public function passThrough( $param ) {
1783  return $param;
1784  }
1785 
1793  public function newFatal( $message, ...$parameters ) {
1794  $status = Status::newFatal( $message, ...$parameters );
1795  $status->cleanCallback = $this->getErrorCleanupFunction();
1796 
1797  return $status;
1798  }
1799 
1806  public function newGood( $value = null ) {
1807  $status = Status::newGood( $value );
1808  $status->cleanCallback = $this->getErrorCleanupFunction();
1809 
1810  return $status;
1811  }
1812 
1821  public function checkRedirect( $title ) {
1822  return false;
1823  }
1824 
1832  public function invalidateImageRedirect( $title ) {
1833  }
1834 
1840  public function getDisplayName() {
1841  $sitename = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::Sitename );
1842 
1843  if ( $this->isLocal() ) {
1844  return $sitename;
1845  }
1846 
1847  // 'shared-repo-name-wikimediacommons' is used when $wgUseInstantCommons = true
1848  return wfMessageFallback( 'shared-repo-name-' . $this->name, 'shared-repo' )->text();
1849  }
1850 
1858  public function nameForThumb( $name ) {
1859  if ( strlen( $name ) > $this->abbrvThreshold ) {
1861  $name = ( $ext == '' ) ? 'thumbnail' : "thumbnail.$ext";
1862  }
1863 
1864  return $name;
1865  }
1866 
1872  public function isLocal() {
1873  return $this->getName() == 'local';
1874  }
1875 
1887  public function getSharedCacheKey( $kClassSuffix, ...$components ) {
1888  return false;
1889  }
1890 
1902  public function getLocalCacheKey( $kClassSuffix, ...$components ) {
1903  return $this->wanCache->makeKey(
1904  'filerepo-' . $kClassSuffix,
1905  $this->getName(),
1906  ...$components
1907  );
1908  }
1909 
1918  public function getTempRepo() {
1919  return new TempFileRepo( [
1920  'name' => "{$this->name}-temp",
1921  'backend' => $this->backend,
1922  'zones' => [
1923  'public' => [
1924  // Same place storeTemp() uses in the base repo, though
1925  // the path hashing is mismatched, which is annoying.
1926  'container' => $this->zones['temp']['container'],
1927  'directory' => $this->zones['temp']['directory']
1928  ],
1929  'thumb' => [
1930  'container' => $this->zones['temp']['container'],
1931  'directory' => $this->zones['temp']['directory'] == ''
1932  ? 'thumb'
1933  : $this->zones['temp']['directory'] . '/thumb'
1934  ],
1935  'transcoded' => [
1936  'container' => $this->zones['temp']['container'],
1937  'directory' => $this->zones['temp']['directory'] == ''
1938  ? 'transcoded'
1939  : $this->zones['temp']['directory'] . '/transcoded'
1940  ]
1941  ],
1942  'hashLevels' => $this->hashLevels, // performance
1943  'isPrivate' => true // all in temp zone
1944  ] );
1945  }
1946 
1953  public function getUploadStash( UserIdentity $user = null ) {
1954  return new UploadStash( $this, $user );
1955  }
1956 
1964  protected function assertWritableRepo() {
1965  }
1966 
1973  public function getInfo() {
1974  $ret = [
1975  'name' => $this->getName(),
1976  'displayname' => $this->getDisplayName(),
1977  'rootUrl' => $this->getZoneUrl( 'public' ),
1978  'local' => $this->isLocal(),
1979  ];
1980 
1981  $optionalSettings = [
1982  'url',
1983  'thumbUrl',
1984  'initialCapital',
1985  'descBaseUrl',
1986  'scriptDirUrl',
1987  'articleUrl',
1988  'fetchDescription',
1989  'descriptionCacheExpiry',
1990  ];
1991  foreach ( $optionalSettings as $k ) {
1992  if ( isset( $this->$k ) ) {
1993  $ret[$k] = $this->$k;
1994  }
1995  }
1996  if ( isset( $this->favicon ) ) {
1997  // Expand any local path to full URL to improve API usability (T77093).
1998  $ret['favicon'] = wfExpandUrl( $this->favicon );
1999  }
2000 
2001  return $ret;
2002  }
2003 
2008  public function hasSha1Storage() {
2009  return $this->hasSha1Storage;
2010  }
2011 
2016  public function supportsSha1URLs() {
2017  return $this->supportsSha1URLs;
2018  }
2019 }
const NS_FILE
Definition: Defines.php:70
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,...
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
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...
Class representing a non-directory file on the file system.
Definition: FSFile.php:32
Base class for all file backend classes (including multi-write backends).
Definition: FileBackend.php:99
static isStoragePath( $path)
Check if a given path is a "mwstore://" path.
const ATTR_UNICODE_PATHS
static splitStoragePath( $storagePath)
Split a storage path into a backend name, a container name, and a relative file path.
static extensionFromPath( $path, $case='lowercase')
Get the final extension from a storage or FS path.
static isPathTraversalFree( $path)
Check if a relative path has no directory traversals.
Base class for file repositories.
Definition: FileRepo.php:47
string $pathDisclosureProtection
May be 'paranoid' to remove all parameters from error messages, 'none' to leave the paths in unchange...
Definition: FileRepo.php:108
getTempHashPath( $suffix)
Get a relative path including trailing slash, e.g.
Definition: FileRepo.php:756
int $hashLevels
The number of directory levels for hash-based division of files.
Definition: FileRepo.php:117
getTempRepo()
Get a temporary private FileRepo associated with this repo.
Definition: FileRepo.php:1918
cleanupDeletedBatch(array $storageKeys)
Delete files in the deleted directory if they are not referenced in the filearchive table.
Definition: FileRepo.php:1544
const OVERWRITE_SAME
Definition: FileRepo.php:50
resolveVirtualUrl( $url)
Get the backend storage path corresponding to a virtual URL.
Definition: FileRepo.php:355
nameForThumb( $name)
Get the portion of the file that contains the origin file name.
Definition: FileRepo.php:1858
publishBatch(array $ntuples, $flags=0)
Publish a batch of files.
Definition: FileRepo.php:1285
findFiles(array $items, $flags=0)
Find many files at once.
Definition: FileRepo.php:537
newFatal( $message,... $parameters)
Create a new fatal error.
Definition: FileRepo.php:1793
getThumbProxyUrl()
Get the URL thumb.php requests are being proxied to.
Definition: FileRepo.php:675
getZoneLocation( $zone)
The storage container and base path of a zone.
Definition: FileRepo.php:381
fileExists( $file)
Checks existence of a file.
Definition: FileRepo.php:1432
getFileSha1( $virtualUrl)
Get the sha1 (base 36) of a file with a given virtual URL/storage path.
Definition: FileRepo.php:1660
bool $supportsSha1URLs
Definition: FileRepo.php:67
quickImportBatch(array $triples)
Import a batch of files from the local file system into the repo.
Definition: FileRepo.php:1067
assertWritableRepo()
Throw an exception if this repo is read-only by design.
Definition: FileRepo.php:1964
getRootDirectory()
Get the public zone root storage directory of the repository.
Definition: FileRepo.php:734
supportsSha1URLs()
Returns whether or not repo supports having originals SHA-1s in the thumb URLs.
Definition: FileRepo.php:2016
newGood( $value=null)
Create a new good result.
Definition: FileRepo.php:1806
findFilesByPrefix( $prefix, $limit)
Return an array of files where the name starts with $prefix.
Definition: FileRepo.php:657
getHashLevels()
Get the number of hash directory levels.
Definition: FileRepo.php:786
string $thumbProxySecret
Secret key to pass as an X-Swift-Secret header to the proxied thumb service.
Definition: FileRepo.php:148
streamFileWithStatus( $virtualUrl, $headers=[], $optHeaders=[])
Attempt to stream a file with the given virtual URL/storage path.
Definition: FileRepo.php:1675
getName()
Get the name of this repository, as specified by $info['name]' to the constructor.
Definition: FileRepo.php:795
store( $srcPath, $dstZone, $dstRel, $flags=0)
Store a file to a given destination.
Definition: FileRepo.php:912
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:454
callable false $oldFileFactoryKey
Override these in the base class.
Definition: FileRepo.php:141
getVirtualUrl( $suffix=false)
Get a URL referring to this repository, with the private mwrepo protocol.
Definition: FileRepo.php:297
const NAME_AND_TIME_ONLY
Definition: FileRepo.php:53
quickPurge( $path)
Purge a file from the repo.
Definition: FileRepo.php:1111
quickPurgeBatch(array $paths)
Purge a batch of files from the repo.
Definition: FileRepo.php:1138
passThrough( $param)
Path disclosure protection function.
Definition: FileRepo.php:1782
static getHashPathForLevel( $name, $levels)
Definition: FileRepo.php:767
array $zones
Map of zones to config.
Definition: FileRepo.php:73
callable false $fileFactoryKey
Override these in the base class.
Definition: FileRepo.php:139
checkRedirect( $title)
Checks if there is a redirect named as $title.
Definition: FileRepo.php:1821
getDisplayName()
Get the human-readable name of the repo.
Definition: FileRepo.php:1840
getSharedCacheKey( $kClassSuffix,... $components)
Get a global, repository-qualified, WAN cache key.
Definition: FileRepo.php:1887
getLocalCacheKey( $kClassSuffix,... $components)
Get a site-local, repository-qualified, WAN cache key.
Definition: FileRepo.php:1902
bool $disableLocalTransform
Disable local image scaling.
Definition: FileRepo.php:151
storeBatch(array $triplets, $flags=0)
Store a batch of files.
Definition: FileRepo.php:937
enumFiles( $callback)
Call a callback function for every public regular file in the repository.
Definition: FileRepo.php:1702
canTransformLocally()
Returns true if the repository can transform files locally.
Definition: FileRepo.php:703
hasSha1Storage()
Returns whether or not storage is SHA-1 based.
Definition: FileRepo.php:2008
cleanupBatch(array $files, $flags=0)
Deletes a batch of files.
Definition: FileRepo.php:1006
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...
Definition: FileRepo.php:1258
initDirectory( $dir)
Creates a directory with the appropriate zone permissions.
Definition: FileRepo.php:1393
int $abbrvThreshold
File names over this size will use the short form of thumbnail names.
Definition: FileRepo.php:126
makeUrl( $query='', $entry='index')
Make an url to this repo.
Definition: FileRepo.php:806
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:637
string $thumbProxyUrl
URL of where to proxy thumb.php requests to.
Definition: FileRepo.php:146
concatenate(array $srcPaths, $dstPath, $flags=0)
Concatenate a list of temporary files into a target file location.
Definition: FileRepo.php:1205
FileBackend $backend
Definition: FileRepo.php:70
null string $favicon
The URL to a favicon (optional, may be a server-local path URL).
Definition: FileRepo.php:129
fileExistsBatch(array $files)
Checks existence of an array of files.
Definition: FileRepo.php:1445
int $descriptionCacheExpiry
Definition: FileRepo.php:61
paranoidClean( $param)
Path disclosure protection function.
Definition: FileRepo.php:1772
WANObjectCache $wanCache
Definition: FileRepo.php:154
const SKIP_LOCKING
Definition: FileRepo.php:51
initZones( $doZones=[])
Ensure that a single zone or list of zones is defined for usage.
Definition: FileRepo.php:270
getFileProps( $virtualUrl)
Get properties of a file with a given virtual URL/storage path.
Definition: FileRepo.php:1618
const OVERWRITE
Definition: FileRepo.php:49
isLocal()
Returns true if this the local file repository.
Definition: FileRepo.php:1872
getZonePath( $zone)
Get the storage path corresponding to one of the zones.
Definition: FileRepo.php:395
getUploadStash(UserIdentity $user=null)
Get an UploadStash associated with this repo.
Definition: FileRepo.php:1953
getDescriptionUrl( $name)
Get the URL of an image description page.
Definition: FileRepo.php:826
cleanDir( $dir)
Deletes a directory if empty.
Definition: FileRepo.php:1416
resolveToStoragePathIfVirtual( $path)
If a path is a virtual URL, resolve it to a storage path.
Definition: FileRepo.php:1576
getDeletedHashPath( $key)
Get a relative path for a deletion archive key, e.g.
Definition: FileRepo.php:1556
bool $hasSha1Storage
Definition: FileRepo.php:64
const DELETE_SOURCE
Definition: FileRepo.php:48
getNameFromTitle( $title)
Get the name of a file from its title.
Definition: FileRepo.php:713
invalidateImageRedirect( $title)
Invalidates image redirect cache related to that image Doesn't do anything for repositories that don'...
Definition: FileRepo.php:1832
getFileSize( $virtualUrl)
Get the size of a file with a given virtual URL/storage path.
Definition: FileRepo.php:1648
getThumbProxySecret()
Get the secret key for the proxied thumb service.
Definition: FileRepo.php:684
bool $fetchDescription
Whether to fetch commons image description pages and display them on the local wiki.
Definition: FileRepo.php:58
string false $url
Public zone URL.
Definition: FileRepo.php:111
callable $fileFactory
Override these in the base class.
Definition: FileRepo.php:135
static isVirtualUrl( $url)
Determine if a string is an mwrepo:// URL.
Definition: FileRepo.php:285
getDescriptionStylesheetUrl()
Get the URL of the stylesheet to apply to description pages.
Definition: FileRepo.php:885
bool $transformVia404
Whether to skip media file transformation on parse and rely on a 404 handler instead.
Definition: FileRepo.php:81
getFileTimestamp( $virtualUrl)
Get the timestamp of a file with a given virtual URL/storage path.
Definition: FileRepo.php:1636
bool $isPrivate
Whether all zones should be private (e.g.
Definition: FileRepo.php:132
string $scriptDirUrl
URL of the MediaWiki installation, equivalent to $wgScriptPath, e.g.
Definition: FileRepo.php:91
string $descBaseUrl
URL of image description pages, e.g.
Definition: FileRepo.php:86
getZoneUrl( $zone, $ext=null)
Get the URL corresponding to one of the four basic zones.
Definition: FileRepo.php:313
getReadOnlyReason()
Get an explanatory message if this repo is read-only.
Definition: FileRepo.php:260
newFile( $title, $time=false)
Create a new File object from the local repository.
Definition: FileRepo.php:419
storeTemp( $originalName, $srcPath)
Pick a random name in the temp zone and store a file to it.
Definition: FileRepo.php:1163
quickCleanDir( $dir)
Deletes a directory if empty.
Definition: FileRepo.php:1122
canTransformVia404()
Returns true if the repository can transform files via a 404 handler.
Definition: FileRepo.php:693
enumFilesInStorage( $callback)
Call a callback function for every public file in the repository.
Definition: FileRepo.php:1713
validateFilename( $filename)
Determine if a relative path is valid, i.e.
Definition: FileRepo.php:1741
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:582
int $deletedHashLevels
The number of directory levels for hash-based division of deleted files.
Definition: FileRepo.php:120
string $thumbScriptUrl
URL of thumb.php.
Definition: FileRepo.php:76
backendSupportsUnicodePaths()
Definition: FileRepo.php:343
__construct(array $info=null)
Definition: FileRepo.php:169
string $name
Definition: FileRepo.php:161
string false $thumbUrl
The base thumbnail URL.
Definition: FileRepo.php:114
bool $initialCapital
Equivalent to $wgCapitalLinks (or $wgCapitalLinkOverrides[NS_FILE], determines whether filenames impl...
Definition: FileRepo.php:101
getLocalCopy( $virtualUrl)
Get a local FS copy of a file with a given virtual URL/storage path.
Definition: FileRepo.php:1591
deleteBatch(array $sourceDestPairs)
Move a group of files to the deletion archive.
Definition: FileRepo.php:1491
getHashPath( $name)
Get a relative path including trailing slash, e.g.
Definition: FileRepo.php:745
callable false $oldFileFactory
Override these in the base class.
Definition: FileRepo.php:137
quickImport( $src, $dst, $options=null)
Import a file from the local file system into the repo.
Definition: FileRepo.php:1049
string $articleUrl
Equivalent to $wgArticlePath, e.g.
Definition: FileRepo.php:94
getDescriptionRenderUrl( $name, $lang=null)
Get the URL of the content-only fragment of the description page.
Definition: FileRepo.php:860
freeTemp( $virtualUrl)
Remove a temporary file or mark it for garbage collection.
Definition: FileRepo.php:1183
findBySha1( $hash)
Get an array or iterator of file objects for files that have a given SHA-1 content hash.
Definition: FileRepo.php:626
getBackend()
Get the file backend instance.
Definition: FileRepo.php:250
getInfo()
Return information about the repository.
Definition: FileRepo.php:1973
getThumbScriptUrl()
Get the URL of thumb.php.
Definition: FileRepo.php:666
getLocalReference( $virtualUrl)
Get a local FS file with a given virtual URL/storage path.
Definition: FileRepo.php:1605
static normalizeTitle( $title, $exception=false)
Given a string or Title object return either a valid Title object with namespace NS_FILE or null.
Definition: File.php:207
const DELETED_FILE
Definition: File.php:72
MediaWiki exception.
Definition: MWException.php:29
MimeMagic helper wrapper.
Definition: MWFileProps.php:28
static getInstance( $ts=false)
Get a timestamp instance in GMT.
Definition: MWTimestamp.php:46
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
static getMain()
Get the RequestContext object associated with the main request.
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:73
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:85
FileRepo for temporary files created by FileRepo::getTempRepo()
Definition: TempFileRepo.php:8
UploadStash is intended to accomplish a few things:
Definition: UploadStash.php:57
static newEmpty()
Get an instance that wraps EmptyBagOStuff.
Interface for objects (potentially) representing an editable wiki page.
This interface represents the authority associated the current execution context, such as a web reque...
Definition: Authority.php:37
Interface for objects representing user identity.
$source
return true
Definition: router.php:90
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
if(!is_readable( $file)) $ext
Definition: router.php:48
if(!isset( $args[0])) $lang