MediaWiki  master
FileRepo.php
Go to the documentation of this file.
1 <?php
15 
45 class FileRepo {
46  public const DELETE_SOURCE = 1;
47  public const OVERWRITE = 2;
48  public const OVERWRITE_SAME = 4;
49  public const SKIP_LOCKING = 8;
50 
51  public const NAME_AND_TIME_ONLY = 1;
52 
57 
60 
62  protected $hasSha1Storage = false;
63 
65  protected $supportsSha1URLs = false;
66 
68  protected $backend;
69 
71  protected $zones = [];
72 
74  protected $thumbScriptUrl;
75 
79  protected $transformVia404;
80 
84  protected $descBaseUrl;
85 
89  protected $scriptDirUrl;
90 
92  protected $articleUrl;
93 
99  protected $initialCapital;
100 
106  protected $pathDisclosureProtection = 'simple';
107 
109  protected $url;
110 
112  protected $thumbUrl;
113 
115  protected $hashLevels;
116 
119 
124  protected $abbrvThreshold;
125 
127  protected $favicon;
128 
130  protected $isPrivate;
131 
133  protected $fileFactory = [ UnregisteredLocalFile::class, 'newFromTitle' ];
135  protected $oldFileFactory = false;
137  protected $fileFactoryKey = false;
139  protected $oldFileFactoryKey = false;
140 
144  protected $thumbProxyUrl;
146  protected $thumbProxySecret;
147 
149  protected $disableLocalTransform = false;
150 
152  protected $wanCache;
153 
159  public $name;
160 
167  public function __construct( array $info = null ) {
168  // Verify required settings presence
169  if (
170  $info === null
171  || !array_key_exists( 'name', $info )
172  || !array_key_exists( 'backend', $info )
173  ) {
174  throw new MWException( __CLASS__ .
175  " requires an array of options having both 'name' and 'backend' keys.\n" );
176  }
177 
178  // Required settings
179  $this->name = $info['name'];
180  if ( $info['backend'] instanceof FileBackend ) {
181  $this->backend = $info['backend']; // useful for testing
182  } else {
183  $this->backend =
184  MediaWikiServices::getInstance()->getFileBackendGroup()->get( $info['backend'] );
185  }
186 
187  // Optional settings that can have no value
188  $optionalSettings = [
189  'descBaseUrl', 'scriptDirUrl', 'articleUrl', 'fetchDescription',
190  'thumbScriptUrl', 'pathDisclosureProtection', 'descriptionCacheExpiry',
191  'favicon', 'thumbProxyUrl', 'thumbProxySecret', 'disableLocalTransform'
192  ];
193  foreach ( $optionalSettings as $var ) {
194  if ( isset( $info[$var] ) ) {
195  $this->$var = $info[$var];
196  }
197  }
198 
199  // Optional settings that have a default
200  $localCapitalLinks =
201  MediaWikiServices::getInstance()->getNamespaceInfo()->isCapitalized( NS_FILE );
202  $this->initialCapital = $info['initialCapital'] ?? $localCapitalLinks;
203  if ( $localCapitalLinks && !$this->initialCapital ) {
204  // If the local wiki's file namespace requires an initial capital, but a foreign file
205  // repo doesn't, complications will result. Linker code will want to auto-capitalize the
206  // first letter of links to files, but those links might actually point to files on
207  // foreign wikis with initial-lowercase names. This combination is not likely to be
208  // used by anyone anyway, so we just outlaw it to save ourselves the bugs. If you want
209  // to include a foreign file repo with initialCapital false, set your local file
210  // namespace to not be capitalized either.
211  throw new InvalidArgumentException(
212  'File repos with initial capital false are not allowed on wikis where the File ' .
213  'namespace has initial capital true' );
214  }
215 
216  $this->url = $info['url'] ?? false; // a subclass may set the URL (e.g. ForeignAPIRepo)
217  if ( isset( $info['thumbUrl'] ) ) {
218  $this->thumbUrl = $info['thumbUrl'];
219  } else {
220  $this->thumbUrl = $this->url ? "{$this->url}/thumb" : false;
221  }
222  $this->hashLevels = $info['hashLevels'] ?? 2;
223  $this->deletedHashLevels = $info['deletedHashLevels'] ?? $this->hashLevels;
224  $this->transformVia404 = !empty( $info['transformVia404'] );
225  $this->abbrvThreshold = $info['abbrvThreshold'] ?? 255;
226  $this->isPrivate = !empty( $info['isPrivate'] );
227  // Give defaults for the basic zones...
228  $this->zones = $info['zones'] ?? [];
229  foreach ( [ 'public', 'thumb', 'transcoded', 'temp', 'deleted' ] as $zone ) {
230  if ( !isset( $this->zones[$zone]['container'] ) ) {
231  $this->zones[$zone]['container'] = "{$this->name}-{$zone}";
232  }
233  if ( !isset( $this->zones[$zone]['directory'] ) ) {
234  $this->zones[$zone]['directory'] = '';
235  }
236  if ( !isset( $this->zones[$zone]['urlsByExt'] ) ) {
237  $this->zones[$zone]['urlsByExt'] = [];
238  }
239  }
240 
241  $this->supportsSha1URLs = !empty( $info['supportsSha1URLs'] );
242 
243  $this->wanCache = $info['wanCache'] ?? WANObjectCache::newEmpty();
244  }
245 
251  public function getBackend() {
252  return $this->backend;
253  }
254 
261  public function getReadOnlyReason() {
262  return $this->backend->getReadOnlyReason();
263  }
264 
272  protected function initZones( $doZones = [] ) {
273  $status = $this->newGood();
274  foreach ( (array)$doZones as $zone ) {
275  $root = $this->getZonePath( $zone );
276  if ( $root === null ) {
277  throw new MWException( "No '$zone' zone defined in the {$this->name} repo." );
278  }
279  }
280 
281  return $status;
282  }
283 
290  public static function isVirtualUrl( $url ) {
291  return substr( $url, 0, 9 ) == 'mwrepo://';
292  }
293 
302  public function getVirtualUrl( $suffix = false ) {
303  $path = 'mwrepo://' . $this->name;
304  if ( $suffix !== false ) {
305  $path .= '/' . rawurlencode( $suffix );
306  }
307 
308  return $path;
309  }
310 
318  public function getZoneUrl( $zone, $ext = null ) {
319  if ( in_array( $zone, [ 'public', 'thumb', 'transcoded' ] ) ) {
320  // standard public zones
321  if ( $ext !== null && isset( $this->zones[$zone]['urlsByExt'][$ext] ) ) {
322  // custom URL for extension/zone
323  return $this->zones[$zone]['urlsByExt'][$ext];
324  } elseif ( isset( $this->zones[$zone]['url'] ) ) {
325  // custom URL for zone
326  return $this->zones[$zone]['url'];
327  }
328  }
329  switch ( $zone ) {
330  case 'public':
331  return $this->url;
332  case 'temp':
333  case 'deleted':
334  return false; // no public URL
335  case 'thumb':
336  return $this->thumbUrl;
337  case 'transcoded':
338  return "{$this->url}/transcoded";
339  default:
340  return false;
341  }
342  }
343 
347  public function backendSupportsUnicodePaths() {
348  return (bool)( $this->getBackend()->getFeatures() & FileBackend::ATTR_UNICODE_PATHS );
349  }
350 
359  public function resolveVirtualUrl( $url ) {
360  if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
361  throw new MWException( __METHOD__ . ': unknown protocol' );
362  }
363  $bits = explode( '/', substr( $url, 9 ), 3 );
364  if ( count( $bits ) != 3 ) {
365  throw new MWException( __METHOD__ . ": invalid mwrepo URL: $url" );
366  }
367  list( $repo, $zone, $rel ) = $bits;
368  if ( $repo !== $this->name ) {
369  throw new MWException( __METHOD__ . ": fetching from a foreign repo is not supported" );
370  }
371  $base = $this->getZonePath( $zone );
372  if ( !$base ) {
373  throw new MWException( __METHOD__ . ": invalid zone: $zone" );
374  }
375 
376  return $base . '/' . rawurldecode( $rel );
377  }
378 
385  protected function getZoneLocation( $zone ) {
386  if ( !isset( $this->zones[$zone] ) ) {
387  return [ null, null ]; // bogus
388  }
389 
390  return [ $this->zones[$zone]['container'], $this->zones[$zone]['directory'] ];
391  }
392 
399  public function getZonePath( $zone ) {
400  list( $container, $base ) = $this->getZoneLocation( $zone );
401  if ( $container === null || $base === null ) {
402  return null;
403  }
404  $backendName = $this->backend->getName();
405  if ( $base != '' ) { // may not be set
406  $base = "/{$base}";
407  }
408 
409  return "mwstore://$backendName/{$container}{$base}";
410  }
411 
423  public function newFile( $title, $time = false ) {
425  if ( !$title ) {
426  return null;
427  }
428  if ( $time ) {
429  if ( $this->oldFileFactory ) {
430  return call_user_func( $this->oldFileFactory, $title, $this, $time );
431  } else {
432  return null;
433  }
434  } else {
435  return call_user_func( $this->fileFactory, $title, $this );
436  }
437  }
438 
458  public function findFile( $title, $options = [] ) {
459  if ( !empty( $options['private'] ) && !( $options['private'] instanceof Authority ) ) {
460  throw new InvalidArgumentException(
461  __METHOD__ . ' called with the `private` option set to something ' .
462  'other than an Authority object'
463  );
464  }
465 
467  if ( !$title ) {
468  return false;
469  }
470  if ( isset( $options['bypassCache'] ) ) {
471  $options['latest'] = $options['bypassCache']; // b/c
472  }
473  $time = $options['time'] ?? false;
474  $flags = !empty( $options['latest'] ) ? File::READ_LATEST : 0;
475  # First try the current version of the file to see if it precedes the timestamp
476  $img = $this->newFile( $title );
477  if ( !$img ) {
478  return false;
479  }
480  $img->load( $flags );
481  if ( $img->exists() && ( !$time || $img->getTimestamp() == $time ) ) {
482  return $img;
483  }
484  # Now try an old version of the file
485  if ( $time !== false ) {
486  $img = $this->newFile( $title, $time );
487  if ( $img ) {
488  $img->load( $flags );
489  if ( $img->exists() ) {
490  if ( !$img->isDeleted( File::DELETED_FILE ) ) {
491  return $img; // always OK
492  } elseif (
493  // If its not empty, its an Authority object
494  !empty( $options['private'] ) &&
495  $img->userCan( File::DELETED_FILE, $options['private'] )
496  ) {
497  return $img;
498  }
499  }
500  }
501  }
502 
503  # Now try redirects
504  if ( !empty( $options['ignoreRedirect'] ) ) {
505  return false;
506  }
507  $redir = $this->checkRedirect( $title );
508  if ( $redir && $title->getNamespace() === NS_FILE ) {
509  $img = $this->newFile( $redir );
510  if ( !$img ) {
511  return false;
512  }
513  $img->load( $flags );
514  if ( $img->exists() ) {
515  $img->redirectedFrom( $title->getDBkey() );
516 
517  return $img;
518  }
519  }
520 
521  return false;
522  }
523 
541  public function findFiles( array $items, $flags = 0 ) {
542  $result = [];
543  foreach ( $items as $item ) {
544  if ( is_array( $item ) ) {
545  $title = $item['title'];
546  $options = $item;
547  unset( $options['title'] );
548 
549  if (
550  !empty( $options['private'] ) &&
551  !( $options['private'] instanceof Authority )
552  ) {
553  $options['private'] = RequestContext::getMain()->getAuthority();
554  }
555  } else {
556  $title = $item;
557  $options = [];
558  }
559  $file = $this->findFile( $title, $options );
560  if ( $file ) {
561  $searchName = File::normalizeTitle( $title )->getDBkey(); // must be valid
562  if ( $flags & self::NAME_AND_TIME_ONLY ) {
563  $result[$searchName] = [
564  'title' => $file->getTitle()->getDBkey(),
565  'timestamp' => $file->getTimestamp()
566  ];
567  } else {
568  $result[$searchName] = $file;
569  }
570  }
571  }
572 
573  return $result;
574  }
575 
586  public function findFileFromKey( $sha1, $options = [] ) {
587  if ( !empty( $options['private'] ) && !( $options['private'] instanceof Authority ) ) {
588  throw new InvalidArgumentException(
589  __METHOD__ . ' called with the `private` option set to something ' .
590  'other than an Authority object'
591  );
592  }
593 
594  $time = $options['time'] ?? false;
595  # First try to find a matching current version of a file...
596  if ( !$this->fileFactoryKey ) {
597  return false; // find-by-sha1 not supported
598  }
599  $img = call_user_func( $this->fileFactoryKey, $sha1, $this, $time );
600  if ( $img && $img->exists() ) {
601  return $img;
602  }
603  # Now try to find a matching old version of a file...
604  if ( $time !== false && $this->oldFileFactoryKey ) { // find-by-sha1 supported?
605  $img = call_user_func( $this->oldFileFactoryKey, $sha1, $this, $time );
606  if ( $img && $img->exists() ) {
607  if ( !$img->isDeleted( File::DELETED_FILE ) ) {
608  return $img; // always OK
609  } elseif (
610  // If its not empty, its an Authority object
611  !empty( $options['private'] ) &&
612  $img->userCan( File::DELETED_FILE, $options['private'] )
613  ) {
614  return $img;
615  }
616  }
617  }
618 
619  return false;
620  }
621 
630  public function findBySha1( $hash ) {
631  return [];
632  }
633 
641  public function findBySha1s( array $hashes ) {
642  $result = [];
643  foreach ( $hashes as $hash ) {
644  $files = $this->findBySha1( $hash );
645  if ( count( $files ) ) {
646  $result[$hash] = $files;
647  }
648  }
649 
650  return $result;
651  }
652 
661  public function findFilesByPrefix( $prefix, $limit ) {
662  return [];
663  }
664 
670  public function getThumbScriptUrl() {
671  return $this->thumbScriptUrl;
672  }
673 
679  public function getThumbProxyUrl() {
680  return $this->thumbProxyUrl;
681  }
682 
688  public function getThumbProxySecret() {
690  }
691 
697  public function canTransformVia404() {
698  return $this->transformVia404;
699  }
700 
707  public function canTransformLocally() {
709  }
710 
717  public function getNameFromTitle( $title ) {
718  if (
719  $this->initialCapital !=
720  MediaWikiServices::getInstance()->getNamespaceInfo()->isCapitalized( NS_FILE )
721  ) {
722  $name = $title->getDBkey();
723  if ( $this->initialCapital ) {
724  $name = MediaWikiServices::getInstance()->getContentLanguage()->ucfirst( $name );
725  }
726  } else {
727  $name = $title->getDBkey();
728  }
729 
730  return $name;
731  }
732 
738  public function getRootDirectory() {
739  return $this->getZonePath( 'public' );
740  }
741 
749  public function getHashPath( $name ) {
750  return self::getHashPathForLevel( $name, $this->hashLevels );
751  }
752 
760  public function getTempHashPath( $suffix ) {
761  $parts = explode( '!', $suffix, 2 ); // format is <timestamp>!<name> or just <name>
762  $name = $parts[1] ?? $suffix; // hash path is not based on timestamp
763  return self::getHashPathForLevel( $name, $this->hashLevels );
764  }
765 
771  protected static function getHashPathForLevel( $name, $levels ) {
772  if ( $levels == 0 ) {
773  return '';
774  } else {
775  $hash = md5( $name );
776  $path = '';
777  for ( $i = 1; $i <= $levels; $i++ ) {
778  $path .= substr( $hash, 0, $i ) . '/';
779  }
780 
781  return $path;
782  }
783  }
784 
790  public function getHashLevels() {
791  return $this->hashLevels;
792  }
793 
799  public function getName() {
800  return $this->name;
801  }
802 
810  public function makeUrl( $query = '', $entry = 'index' ) {
811  if ( isset( $this->scriptDirUrl ) ) {
812  return wfAppendQuery( "{$this->scriptDirUrl}/{$entry}.php", $query );
813  }
814 
815  return false;
816  }
817 
830  public function getDescriptionUrl( $name ) {
831  $encName = wfUrlencode( $name );
832  if ( $this->descBaseUrl !== null ) {
833  # "http://example.com/wiki/File:"
834  return $this->descBaseUrl . $encName;
835  }
836  if ( $this->articleUrl !== null ) {
837  # "http://example.com/wiki/$1"
838  # We use "Image:" as the canonical namespace for
839  # compatibility across all MediaWiki versions.
840  return str_replace( '$1',
841  "Image:$encName", $this->articleUrl );
842  }
843  if ( $this->scriptDirUrl !== null ) {
844  # "http://example.com/w"
845  # We use "Image:" as the canonical namespace for
846  # compatibility across all MediaWiki versions,
847  # and just sort of hope index.php is right. ;)
848  return $this->makeUrl( "title=Image:$encName" );
849  }
850 
851  return false;
852  }
853 
864  public function getDescriptionRenderUrl( $name, $lang = null ) {
865  $query = 'action=render';
866  if ( $lang !== null ) {
867  $query .= '&uselang=' . urlencode( $lang );
868  }
869  if ( isset( $this->scriptDirUrl ) ) {
870  return $this->makeUrl(
871  'title=' .
872  wfUrlencode( 'Image:' . $name ) .
873  "&$query" );
874  } else {
875  $descUrl = $this->getDescriptionUrl( $name );
876  if ( $descUrl ) {
877  return wfAppendQuery( $descUrl, $query );
878  } else {
879  return false;
880  }
881  }
882  }
883 
889  public function getDescriptionStylesheetUrl() {
890  if ( isset( $this->scriptDirUrl ) ) {
891  // Must match canonical query parameter order for optimum caching
892  // See HtmlCacheUpdater::getUrls
893  return $this->makeUrl( 'title=MediaWiki:Filepage.css&action=raw&ctype=text/css' );
894  }
895 
896  return false;
897  }
898 
916  public function store( $srcPath, $dstZone, $dstRel, $flags = 0 ) {
917  $this->assertWritableRepo(); // fail out if read-only
918 
919  $status = $this->storeBatch( [ [ $srcPath, $dstZone, $dstRel ] ], $flags );
920  if ( $status->successCount == 0 ) {
921  $status->setOK( false );
922  }
923 
924  return $status;
925  }
926 
941  public function storeBatch( array $triplets, $flags = 0 ) {
942  $this->assertWritableRepo(); // fail out if read-only
943 
944  if ( $flags & self::DELETE_SOURCE ) {
945  throw new InvalidArgumentException( "DELETE_SOURCE not supported in " . __METHOD__ );
946  }
947 
948  $status = $this->newGood();
949  $backend = $this->backend; // convenience
950 
951  $operations = [];
952  // Validate each triplet and get the store operation...
953  foreach ( $triplets as $triplet ) {
954  list( $src, $dstZone, $dstRel ) = $triplet;
955  $srcPath = ( $src instanceof FSFile ) ? $src->getPath() : $src;
956  wfDebug( __METHOD__
957  . "( \$src='$srcPath', \$dstZone='$dstZone', \$dstRel='$dstRel' )"
958  );
959  // Resolve source path
960  if ( $src instanceof FSFile ) {
961  $op = 'store';
962  } else {
963  $src = $this->resolveToStoragePathIfVirtual( $src );
964  $op = FileBackend::isStoragePath( $src ) ? 'copy' : 'store';
965  }
966  // Resolve destination path
967  $root = $this->getZonePath( $dstZone );
968  if ( !$root ) {
969  throw new MWException( "Invalid zone: $dstZone" );
970  }
971  if ( !$this->validateFilename( $dstRel ) ) {
972  throw new MWException( 'Validation error in $dstRel' );
973  }
974  $dstPath = "$root/$dstRel";
975  $dstDir = dirname( $dstPath );
976  // Create destination directories for this triplet
977  if ( !$this->initDirectory( $dstDir )->isOK() ) {
978  return $this->newFatal( 'directorycreateerror', $dstDir );
979  }
980 
981  // Copy the source file to the destination
982  $operations[] = [
983  'op' => $op,
984  'src' => $src, // storage path (copy) or local file path (store)
985  'dst' => $dstPath,
986  'overwrite' => ( $flags & self::OVERWRITE ) ? true : false,
987  'overwriteSame' => ( $flags & self::OVERWRITE_SAME ) ? true : false,
988  ];
989  }
990 
991  // Execute the store operation for each triplet
992  $opts = [ 'force' => true ];
993  if ( $flags & self::SKIP_LOCKING ) {
994  $opts['nonLocking'] = true;
995  }
996  $status->merge( $backend->doOperations( $operations, $opts ) );
997 
998  return $status;
999  }
1000 
1011  public function cleanupBatch( array $files, $flags = 0 ) {
1012  $this->assertWritableRepo(); // fail out if read-only
1013 
1014  $status = $this->newGood();
1015 
1016  $operations = [];
1017  foreach ( $files as $path ) {
1018  if ( is_array( $path ) ) {
1019  // This is a pair, extract it
1020  list( $zone, $rel ) = $path;
1021  $path = $this->getZonePath( $zone ) . "/$rel";
1022  } else {
1023  // Resolve source to a storage path if virtual
1024  $path = $this->resolveToStoragePathIfVirtual( $path );
1025  }
1026  $operations[] = [ 'op' => 'delete', 'src' => $path ];
1027  }
1028  // Actually delete files from storage...
1029  $opts = [ 'force' => true ];
1030  if ( $flags & self::SKIP_LOCKING ) {
1031  $opts['nonLocking'] = true;
1032  }
1033  $status->merge( $this->backend->doOperations( $operations, $opts ) );
1034 
1035  return $status;
1036  }
1037 
1055  final public function quickImport( $src, $dst, $options = null ) {
1056  return $this->quickImportBatch( [ [ $src, $dst, $options ] ] );
1057  }
1058 
1073  public function quickImportBatch( array $triples ) {
1074  $status = $this->newGood();
1075  $operations = [];
1076  foreach ( $triples as $triple ) {
1077  list( $src, $dst ) = $triple;
1078  if ( $src instanceof FSFile ) {
1079  $op = 'store';
1080  } else {
1081  $src = $this->resolveToStoragePathIfVirtual( $src );
1082  $op = FileBackend::isStoragePath( $src ) ? 'copy' : 'store';
1083  }
1084  $dst = $this->resolveToStoragePathIfVirtual( $dst );
1085 
1086  if ( !isset( $triple[2] ) ) {
1087  $headers = [];
1088  } elseif ( is_string( $triple[2] ) ) {
1089  // back-compat
1090  $headers = [ 'Content-Disposition' => $triple[2] ];
1091  } elseif ( is_array( $triple[2] ) && isset( $triple[2]['headers'] ) ) {
1092  $headers = $triple[2]['headers'];
1093  } else {
1094  $headers = [];
1095  }
1096 
1097  $operations[] = [
1098  'op' => $op,
1099  'src' => $src, // storage path (copy) or local path/FSFile (store)
1100  'dst' => $dst,
1101  'headers' => $headers
1102  ];
1103  $status->merge( $this->initDirectory( dirname( $dst ) ) );
1104  }
1105  $status->merge( $this->backend->doQuickOperations( $operations ) );
1106 
1107  return $status;
1108  }
1109 
1118  final public function quickPurge( $path ) {
1119  return $this->quickPurgeBatch( [ $path ] );
1120  }
1121 
1129  public function quickCleanDir( $dir ) {
1130  $status = $this->newGood();
1131  $status->merge( $this->backend->clean(
1132  [ 'dir' => $this->resolveToStoragePathIfVirtual( $dir ) ] ) );
1133 
1134  return $status;
1135  }
1136 
1145  public function quickPurgeBatch( array $paths ) {
1146  $status = $this->newGood();
1147  $operations = [];
1148  foreach ( $paths as $path ) {
1149  $operations[] = [
1150  'op' => 'delete',
1151  'src' => $this->resolveToStoragePathIfVirtual( $path ),
1152  'ignoreMissingSource' => true
1153  ];
1154  }
1155  $status->merge( $this->backend->doQuickOperations( $operations ) );
1156 
1157  return $status;
1158  }
1159 
1170  public function storeTemp( $originalName, $srcPath ) {
1171  $this->assertWritableRepo(); // fail out if read-only
1172 
1173  $date = MWTimestamp::getInstance()->format( 'YmdHis' );
1174  $hashPath = $this->getHashPath( $originalName );
1175  $dstUrlRel = $hashPath . $date . '!' . rawurlencode( $originalName );
1176  $virtualUrl = $this->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
1177 
1178  $result = $this->quickImport( $srcPath, $virtualUrl );
1179  $result->value = $virtualUrl;
1180 
1181  return $result;
1182  }
1183 
1190  public function freeTemp( $virtualUrl ) {
1191  $this->assertWritableRepo(); // fail out if read-only
1192 
1193  $temp = $this->getVirtualUrl( 'temp' );
1194  if ( substr( $virtualUrl, 0, strlen( $temp ) ) != $temp ) {
1195  wfDebug( __METHOD__ . ": Invalid temp virtual URL" );
1196 
1197  return false;
1198  }
1199 
1200  return $this->quickPurge( $virtualUrl )->isOK();
1201  }
1202 
1212  public function concatenate( array $srcPaths, $dstPath, $flags = 0 ) {
1213  $this->assertWritableRepo(); // fail out if read-only
1214 
1215  $status = $this->newGood();
1216 
1217  $sources = [];
1218  foreach ( $srcPaths as $srcPath ) {
1219  // Resolve source to a storage path if virtual
1220  $source = $this->resolveToStoragePathIfVirtual( $srcPath );
1221  $sources[] = $source; // chunk to merge
1222  }
1223 
1224  // Concatenate the chunks into one FS file
1225  $params = [ 'srcs' => $sources, 'dst' => $dstPath ];
1226  $status->merge( $this->backend->concatenate( $params ) );
1227  if ( !$status->isOK() ) {
1228  return $status;
1229  }
1230 
1231  // Delete the sources if required
1232  if ( $flags & self::DELETE_SOURCE ) {
1233  $status->merge( $this->quickPurgeBatch( $srcPaths ) );
1234  }
1235 
1236  // Make sure status is OK, despite any quickPurgeBatch() fatals
1237  $status->setResult( true );
1238 
1239  return $status;
1240  }
1241 
1265  public function publish(
1266  $src, $dstRel, $archiveRel, $flags = 0, array $options = []
1267  ) {
1268  $this->assertWritableRepo(); // fail out if read-only
1269 
1270  $status = $this->publishBatch(
1271  [ [ $src, $dstRel, $archiveRel, $options ] ], $flags );
1272  if ( $status->successCount == 0 ) {
1273  $status->setOK( false );
1274  }
1275  $status->value = $status->value[0] ?? false;
1276 
1277  return $status;
1278  }
1279 
1292  public function publishBatch( array $ntuples, $flags = 0 ) {
1293  $this->assertWritableRepo(); // fail out if read-only
1294 
1295  $backend = $this->backend; // convenience
1296  // Try creating directories
1297  $status = $this->initZones( 'public' );
1298  if ( !$status->isOK() ) {
1299  return $status;
1300  }
1301 
1302  $status = $this->newGood( [] );
1303 
1304  $operations = [];
1305  $sourceFSFilesToDelete = []; // cleanup for disk source files
1306  // Validate each triplet and get the store operation...
1307  foreach ( $ntuples as $ntuple ) {
1308  list( $src, $dstRel, $archiveRel ) = $ntuple;
1309  $srcPath = ( $src instanceof FSFile ) ? $src->getPath() : $src;
1310 
1311  $options = $ntuple[3] ?? [];
1312  // Resolve source to a storage path if virtual
1313  $srcPath = $this->resolveToStoragePathIfVirtual( $srcPath );
1314  if ( !$this->validateFilename( $dstRel ) ) {
1315  throw new MWException( 'Validation error in $dstRel' );
1316  }
1317  if ( !$this->validateFilename( $archiveRel ) ) {
1318  throw new MWException( 'Validation error in $archiveRel' );
1319  }
1320 
1321  $publicRoot = $this->getZonePath( 'public' );
1322  $dstPath = "$publicRoot/$dstRel";
1323  $archivePath = "$publicRoot/$archiveRel";
1324 
1325  $dstDir = dirname( $dstPath );
1326  $archiveDir = dirname( $archivePath );
1327  // Abort immediately on directory creation errors since they're likely to be repetitive
1328  if ( !$this->initDirectory( $dstDir )->isOK() ) {
1329  return $this->newFatal( 'directorycreateerror', $dstDir );
1330  }
1331  if ( !$this->initDirectory( $archiveDir )->isOK() ) {
1332  return $this->newFatal( 'directorycreateerror', $archiveDir );
1333  }
1334 
1335  // Set any desired headers to be use in GET/HEAD responses
1336  $headers = $options['headers'] ?? [];
1337 
1338  // Archive destination file if it exists.
1339  // This will check if the archive file also exists and fail if does.
1340  // This is a sanity check to avoid data loss. On Windows and Linux,
1341  // copy() will overwrite, so the existence check is vulnerable to
1342  // race conditions unless a functioning LockManager is used.
1343  // LocalFile also uses SELECT FOR UPDATE for synchronization.
1344  $operations[] = [
1345  'op' => 'copy',
1346  'src' => $dstPath,
1347  'dst' => $archivePath,
1348  'ignoreMissingSource' => true
1349  ];
1350 
1351  // Copy (or move) the source file to the destination
1352  if ( FileBackend::isStoragePath( $srcPath ) ) {
1353  $operations[] = [
1354  'op' => ( $flags & self::DELETE_SOURCE ) ? 'move' : 'copy',
1355  'src' => $srcPath,
1356  'dst' => $dstPath,
1357  'overwrite' => true, // replace current
1358  'headers' => $headers
1359  ];
1360  } else {
1361  $operations[] = [
1362  'op' => 'store',
1363  'src' => $src, // storage path (copy) or local path/FSFile (store)
1364  'dst' => $dstPath,
1365  'overwrite' => true, // replace current
1366  'headers' => $headers
1367  ];
1368  if ( $flags & self::DELETE_SOURCE ) {
1369  $sourceFSFilesToDelete[] = $srcPath;
1370  }
1371  }
1372  }
1373 
1374  // Execute the operations for each triplet
1375  $status->merge( $backend->doOperations( $operations ) );
1376  // Find out which files were archived...
1377  foreach ( $ntuples as $i => $ntuple ) {
1378  list( , , $archiveRel ) = $ntuple;
1379  $archivePath = $this->getZonePath( 'public' ) . "/$archiveRel";
1380  if ( $this->fileExists( $archivePath ) ) {
1381  $status->value[$i] = 'archived';
1382  } else {
1383  $status->value[$i] = 'new';
1384  }
1385  }
1386  // Cleanup for disk source files...
1387  foreach ( $sourceFSFilesToDelete as $file ) {
1388  Wikimedia\suppressWarnings();
1389  unlink( $file ); // FS cleanup
1390  Wikimedia\restoreWarnings();
1391  }
1392 
1393  return $status;
1394  }
1395 
1403  protected function initDirectory( $dir ) {
1404  $path = $this->resolveToStoragePathIfVirtual( $dir );
1405  list( , $container, ) = FileBackend::splitStoragePath( $path );
1406 
1407  $params = [ 'dir' => $path ];
1408  if ( $this->isPrivate
1409  || $container === $this->zones['deleted']['container']
1410  || $container === $this->zones['temp']['container']
1411  ) {
1412  # Take all available measures to prevent web accessibility of new deleted
1413  # directories, in case the user has not configured offline storage
1414  $params = [ 'noAccess' => true, 'noListing' => true ] + $params;
1415  }
1416 
1417  $status = $this->newGood();
1418  $status->merge( $this->backend->prepare( $params ) );
1419 
1420  return $status;
1421  }
1422 
1429  public function cleanDir( $dir ) {
1430  $this->assertWritableRepo(); // fail out if read-only
1431 
1432  $status = $this->newGood();
1433  $status->merge( $this->backend->clean(
1434  [ 'dir' => $this->resolveToStoragePathIfVirtual( $dir ) ] ) );
1435 
1436  return $status;
1437  }
1438 
1445  public function fileExists( $file ) {
1446  $result = $this->fileExistsBatch( [ $file ] );
1447 
1448  return $result[0];
1449  }
1450 
1457  public function fileExistsBatch( array $files ) {
1458  $paths = array_map( [ $this, 'resolveToStoragePathIfVirtual' ], $files );
1459  $this->backend->preloadFileStat( [ 'srcs' => $paths ] );
1460 
1461  $result = [];
1462  foreach ( $files as $key => $file ) {
1464  $result[$key] = $this->backend->fileExists( [ 'src' => $path ] );
1465  }
1466 
1467  return $result;
1468  }
1469 
1480  public function delete( $srcRel, $archiveRel ) {
1481  $this->assertWritableRepo(); // fail out if read-only
1482 
1483  return $this->deleteBatch( [ [ $srcRel, $archiveRel ] ] );
1484  }
1485 
1503  public function deleteBatch( array $sourceDestPairs ) {
1504  $this->assertWritableRepo(); // fail out if read-only
1505 
1506  // Try creating directories
1507  $status = $this->initZones( [ 'public', 'deleted' ] );
1508  if ( !$status->isOK() ) {
1509  return $status;
1510  }
1511 
1512  $status = $this->newGood();
1513 
1514  $backend = $this->backend; // convenience
1515  $operations = [];
1516  // Validate filenames and create archive directories
1517  foreach ( $sourceDestPairs as [ $srcRel, $archiveRel ] ) {
1518  if ( !$this->validateFilename( $srcRel ) ) {
1519  throw new MWException( __METHOD__ . ':Validation error in $srcRel' );
1520  } elseif ( !$this->validateFilename( $archiveRel ) ) {
1521  throw new MWException( __METHOD__ . ':Validation error in $archiveRel' );
1522  }
1523 
1524  $publicRoot = $this->getZonePath( 'public' );
1525  $srcPath = "{$publicRoot}/$srcRel";
1526 
1527  $deletedRoot = $this->getZonePath( 'deleted' );
1528  $archivePath = "{$deletedRoot}/{$archiveRel}";
1529  $archiveDir = dirname( $archivePath ); // does not touch FS
1530 
1531  // Create destination directories
1532  if ( !$this->initDirectory( $archiveDir )->isOK() ) {
1533  return $this->newFatal( 'directorycreateerror', $archiveDir );
1534  }
1535 
1536  $operations[] = [
1537  'op' => 'move',
1538  'src' => $srcPath,
1539  'dst' => $archivePath,
1540  // We may have 2+ identical files being deleted,
1541  // all of which will map to the same destination file
1542  'overwriteSame' => true // also see T33792
1543  ];
1544  }
1545 
1546  // Move the files by execute the operations for each pair.
1547  // We're now committed to returning an OK result, which will
1548  // lead to the files being moved in the DB also.
1549  $opts = [ 'force' => true ];
1550  $status->merge( $backend->doOperations( $operations, $opts ) );
1551 
1552  return $status;
1553  }
1554 
1561  public function cleanupDeletedBatch( array $storageKeys ) {
1562  $this->assertWritableRepo();
1563  }
1564 
1573  public function getDeletedHashPath( $key ) {
1574  if ( strlen( $key ) < 31 ) {
1575  throw new MWException( "Invalid storage key '$key'." );
1576  }
1577  $path = '';
1578  for ( $i = 0; $i < $this->deletedHashLevels; $i++ ) {
1579  $path .= $key[$i] . '/';
1580  }
1581 
1582  return $path;
1583  }
1584 
1593  protected function resolveToStoragePathIfVirtual( $path ) {
1594  if ( self::isVirtualUrl( $path ) ) {
1595  return $this->resolveVirtualUrl( $path );
1596  }
1597 
1598  return $path;
1599  }
1600 
1608  public function getLocalCopy( $virtualUrl ) {
1609  $path = $this->resolveToStoragePathIfVirtual( $virtualUrl );
1610 
1611  return $this->backend->getLocalCopy( [ 'src' => $path ] );
1612  }
1613 
1622  public function getLocalReference( $virtualUrl ) {
1623  $path = $this->resolveToStoragePathIfVirtual( $virtualUrl );
1624 
1625  return $this->backend->getLocalReference( [ 'src' => $path ] );
1626  }
1627 
1635  public function getFileProps( $virtualUrl ) {
1636  $fsFile = $this->getLocalReference( $virtualUrl );
1637  $mwProps = new MWFileProps( MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer() );
1638  if ( $fsFile ) {
1639  $props = $mwProps->getPropsFromPath( $fsFile->getPath(), true );
1640  } else {
1641  $props = $mwProps->newPlaceholderProps();
1642  }
1643 
1644  return $props;
1645  }
1646 
1653  public function getFileTimestamp( $virtualUrl ) {
1654  $path = $this->resolveToStoragePathIfVirtual( $virtualUrl );
1655 
1656  return $this->backend->getFileTimestamp( [ 'src' => $path ] );
1657  }
1658 
1665  public function getFileSize( $virtualUrl ) {
1666  $path = $this->resolveToStoragePathIfVirtual( $virtualUrl );
1667 
1668  return $this->backend->getFileSize( [ 'src' => $path ] );
1669  }
1670 
1677  public function getFileSha1( $virtualUrl ) {
1678  $path = $this->resolveToStoragePathIfVirtual( $virtualUrl );
1679 
1680  return $this->backend->getFileSha1Base36( [ 'src' => $path ] );
1681  }
1682 
1692  public function streamFileWithStatus( $virtualUrl, $headers = [], $optHeaders = [] ) {
1693  $path = $this->resolveToStoragePathIfVirtual( $virtualUrl );
1694  $params = [ 'src' => $path, 'headers' => $headers, 'options' => $optHeaders ];
1695 
1696  // T172851: HHVM does not flush the output properly, causing OOM
1697  ob_start( null, 1048576 );
1698  ob_implicit_flush( true );
1699 
1700  $status = $this->newGood();
1701  $status->merge( $this->backend->streamFile( $params ) );
1702 
1703  // T186565: Close the buffer, unless it has already been closed
1704  // in HTTPFileStreamer::resetOutputBuffers().
1705  if ( ob_get_status() ) {
1706  ob_end_flush();
1707  }
1708 
1709  return $status;
1710  }
1711 
1720  public function enumFiles( $callback ) {
1721  $this->enumFilesInStorage( $callback );
1722  }
1723 
1731  protected function enumFilesInStorage( $callback ) {
1732  $publicRoot = $this->getZonePath( 'public' );
1733  $numDirs = 1 << ( $this->hashLevels * 4 );
1734  // Use a priori assumptions about directory structure
1735  // to reduce the tree height of the scanning process.
1736  for ( $flatIndex = 0; $flatIndex < $numDirs; $flatIndex++ ) {
1737  $hexString = sprintf( "%0{$this->hashLevels}x", $flatIndex );
1738  $path = $publicRoot;
1739  for ( $hexPos = 0; $hexPos < $this->hashLevels; $hexPos++ ) {
1740  $path .= '/' . substr( $hexString, 0, $hexPos + 1 );
1741  }
1742  $iterator = $this->backend->getFileList( [ 'dir' => $path ] );
1743  if ( $iterator === null ) {
1744  throw new MWException( __METHOD__ . ': could not get file listing for ' . $path );
1745  }
1746  foreach ( $iterator as $name ) {
1747  // Each item returned is a public file
1748  call_user_func( $callback, "{$path}/{$name}" );
1749  }
1750  }
1751  }
1752 
1759  public function validateFilename( $filename ) {
1760  if ( strval( $filename ) == '' ) {
1761  return false;
1762  }
1763 
1764  return FileBackend::isPathTraversalFree( $filename );
1765  }
1766 
1772  private function getErrorCleanupFunction() {
1773  switch ( $this->pathDisclosureProtection ) {
1774  case 'none':
1775  case 'simple': // b/c
1776  $callback = [ $this, 'passThrough' ];
1777  break;
1778  default: // 'paranoid'
1779  $callback = [ $this, 'paranoidClean' ];
1780  }
1781  return $callback;
1782  }
1783 
1790  public function paranoidClean( $param ) {
1791  return '[hidden]';
1792  }
1793 
1800  public function passThrough( $param ) {
1801  return $param;
1802  }
1803 
1811  public function newFatal( $message, ...$parameters ) {
1812  $status = Status::newFatal( $message, ...$parameters );
1813  $status->cleanCallback = $this->getErrorCleanupFunction();
1814 
1815  return $status;
1816  }
1817 
1824  public function newGood( $value = null ) {
1825  $status = Status::newGood( $value );
1826  $status->cleanCallback = $this->getErrorCleanupFunction();
1827 
1828  return $status;
1829  }
1830 
1839  public function checkRedirect( $title ) {
1840  return false;
1841  }
1842 
1850  public function invalidateImageRedirect( $title ) {
1851  }
1852 
1858  public function getDisplayName() {
1859  global $wgSitename;
1860 
1861  if ( $this->isLocal() ) {
1862  return $wgSitename;
1863  }
1864 
1865  // 'shared-repo-name-wikimediacommons' is used when $wgUseInstantCommons = true
1866  return wfMessageFallback( 'shared-repo-name-' . $this->name, 'shared-repo' )->text();
1867  }
1868 
1876  public function nameForThumb( $name ) {
1877  if ( strlen( $name ) > $this->abbrvThreshold ) {
1879  $name = ( $ext == '' ) ? 'thumbnail' : "thumbnail.$ext";
1880  }
1881 
1882  return $name;
1883  }
1884 
1890  public function isLocal() {
1891  return $this->getName() == 'local';
1892  }
1893 
1905  public function getSharedCacheKey( $kClassSuffix, ...$components ) {
1906  return false;
1907  }
1908 
1920  public function getLocalCacheKey( $kClassSuffix, ...$components ) {
1921  return $this->wanCache->makeKey(
1922  'filerepo-' . $kClassSuffix,
1923  $this->getName(),
1924  ...$components
1925  );
1926  }
1927 
1936  public function getTempRepo() {
1937  return new TempFileRepo( [
1938  'name' => "{$this->name}-temp",
1939  'backend' => $this->backend,
1940  'zones' => [
1941  'public' => [
1942  // Same place storeTemp() uses in the base repo, though
1943  // the path hashing is mismatched, which is annoying.
1944  'container' => $this->zones['temp']['container'],
1945  'directory' => $this->zones['temp']['directory']
1946  ],
1947  'thumb' => [
1948  'container' => $this->zones['temp']['container'],
1949  'directory' => $this->zones['temp']['directory'] == ''
1950  ? 'thumb'
1951  : $this->zones['temp']['directory'] . '/thumb'
1952  ],
1953  'transcoded' => [
1954  'container' => $this->zones['temp']['container'],
1955  'directory' => $this->zones['temp']['directory'] == ''
1956  ? 'transcoded'
1957  : $this->zones['temp']['directory'] . '/transcoded'
1958  ]
1959  ],
1960  'hashLevels' => $this->hashLevels, // performance
1961  'isPrivate' => true // all in temp zone
1962  ] );
1963  }
1964 
1971  public function getUploadStash( UserIdentity $user = null ) {
1972  return new UploadStash( $this, $user );
1973  }
1974 
1982  protected function assertWritableRepo() {
1983  }
1984 
1991  public function getInfo() {
1992  $ret = [
1993  'name' => $this->getName(),
1994  'displayname' => $this->getDisplayName(),
1995  'rootUrl' => $this->getZoneUrl( 'public' ),
1996  'local' => $this->isLocal(),
1997  ];
1998 
1999  $optionalSettings = [
2000  'url', 'thumbUrl', 'initialCapital', 'descBaseUrl', 'scriptDirUrl', 'articleUrl',
2001  'fetchDescription', 'descriptionCacheExpiry', 'favicon'
2002  ];
2003  foreach ( $optionalSettings as $k ) {
2004  if ( isset( $this->$k ) ) {
2005  $ret[$k] = $this->$k;
2006  }
2007  }
2008 
2009  return $ret;
2010  }
2011 
2016  public function hasSha1Storage() {
2017  return $this->hasSha1Storage;
2018  }
2019 
2024  public function supportsSha1URLs() {
2025  return $this->supportsSha1URLs;
2026  }
2027 }
FileBackend\splitStoragePath
static splitStoragePath( $storagePath)
Split a storage path into a backend name, a container name, and a relative file path.
Definition: FileBackend.php:1537
Page\PageIdentity
Interface for objects (potentially) representing an editable wiki page.
Definition: PageIdentity.php:64
FileBackend\doOperations
doOperations(array $ops, array $opts=[])
This is the main entry point into the backend for write operations.
Definition: FileBackend.php:467
FileRepo\getReadOnlyReason
getReadOnlyReason()
Get an explanatory message if this repo is read-only.
Definition: FileRepo.php:261
FileRepo\$supportsSha1URLs
bool $supportsSha1URLs
Definition: FileRepo.php:65
FileRepo\initZones
initZones( $doZones=[])
Check if a single zone or list of zones is defined for usage.
Definition: FileRepo.php:272
wfMessageFallback
wfMessageFallback(... $keys)
This function accepts multiple message keys and returns a message instance for the first message whic...
Definition: GlobalFunctions.php:1205
FileRepo\$hasSha1Storage
bool $hasSha1Storage
Definition: FileRepo.php:62
FileRepo\passThrough
passThrough( $param)
Path disclosure protection function.
Definition: FileRepo.php:1800
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:70
FileRepo\findBySha1
findBySha1( $hash)
Get an array or iterator of file objects for files that have a given SHA-1 content hash.
Definition: FileRepo.php:630
FileRepo\getThumbScriptUrl
getThumbScriptUrl()
Get the URL of thumb.php.
Definition: FileRepo.php:670
FileRepo\newGood
newGood( $value=null)
Create a new good result.
Definition: FileRepo.php:1824
FileRepo\getDescriptionRenderUrl
getDescriptionRenderUrl( $name, $lang=null)
Get the URL of the content-only fragment of the description page.
Definition: FileRepo.php:864
FileBackend
Base class for all file backend classes (including multi-write backends).
Definition: FileBackend.php:98
FileRepo\$deletedHashLevels
int $deletedHashLevels
The number of directory levels for hash-based division of deleted files.
Definition: FileRepo.php:118
FileRepo\enumFilesInStorage
enumFilesInStorage( $callback)
Call a callback function for every public file in the repository.
Definition: FileRepo.php:1731
FileRepo\$favicon
string $favicon
The URL of the repo's favicon, if any.
Definition: FileRepo.php:127
FileRepo\OVERWRITE_SAME
const OVERWRITE_SAME
Definition: FileRepo.php:48
FileRepo\$isPrivate
bool $isPrivate
Whether all zones should be private (e.g.
Definition: FileRepo.php:130
FileRepo\validateFilename
validateFilename( $filename)
Determine if a relative path is valid, i.e.
Definition: FileRepo.php:1759
FileRepo\getRootDirectory
getRootDirectory()
Get the public zone root storage directory of the repository.
Definition: FileRepo.php:738
FileRepo\$fileFactoryKey
callable false $fileFactoryKey
Override these in the base class.
Definition: FileRepo.php:137
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:186
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:37
FileRepo\OVERWRITE
const OVERWRITE
Definition: FileRepo.php:47
FileRepo\getVirtualUrl
getVirtualUrl( $suffix=false)
Get a URL referring to this repository, with the private mwrepo protocol.
Definition: FileRepo.php:302
FileRepo\makeUrl
makeUrl( $query='', $entry='index')
Make an url to this repo.
Definition: FileRepo.php:810
FileRepo\$oldFileFactory
callable false $oldFileFactory
Override these in the base class.
Definition: FileRepo.php:135
FileRepo\storeBatch
storeBatch(array $triplets, $flags=0)
Store a batch of files.
Definition: FileRepo.php:941
FileRepo\canTransformLocally
canTransformLocally()
Returns true if the repository can transform files locally.
Definition: FileRepo.php:707
FileRepo\$pathDisclosureProtection
string $pathDisclosureProtection
May be 'paranoid' to remove all parameters from error messages, 'none' to leave the paths in unchange...
Definition: FileRepo.php:106
true
return true
Definition: router.php:90
FileRepo\streamFileWithStatus
streamFileWithStatus( $virtualUrl, $headers=[], $optHeaders=[])
Attempt to stream a file with the given virtual URL/storage path.
Definition: FileRepo.php:1692
FileRepo\$disableLocalTransform
bool $disableLocalTransform
Disable local image scaling.
Definition: FileRepo.php:149
FileRepo\getTempRepo
getTempRepo()
Get a temporary private FileRepo associated with this repo.
Definition: FileRepo.php:1936
FileRepo\resolveToStoragePathIfVirtual
resolveToStoragePathIfVirtual( $path)
If a path is a virtual URL, resolve it to a storage path.
Definition: FileRepo.php:1593
FileRepo\getInfo
getInfo()
Return information about the repository.
Definition: FileRepo.php:1991
UploadStash
UploadStash is intended to accomplish a few things:
Definition: UploadStash.php:55
FileRepo\$descriptionCacheExpiry
int $descriptionCacheExpiry
Definition: FileRepo.php:59
FileRepo\getName
getName()
Get the name of this repository, as specified by $info['name]' to the constructor.
Definition: FileRepo.php:799
FileRepo\$thumbUrl
string $thumbUrl
The base thumbnail URL.
Definition: FileRepo.php:112
wfUrlencode
wfUrlencode( $s)
We want some things to be included as literal characters in our title URLs for prettiness,...
Definition: GlobalFunctions.php:292
FileRepo\quickImportBatch
quickImportBatch(array $triples)
Import a batch of files from the local file system into the repo.
Definition: FileRepo.php:1073
FileBackend\extensionFromPath
static extensionFromPath( $path, $case='lowercase')
Get the final extension from a storage or FS path.
Definition: FileBackend.php:1599
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
FileRepo\getHashPathForLevel
static getHashPathForLevel( $name, $levels)
Definition: FileRepo.php:771
FileRepo\getZoneUrl
getZoneUrl( $zone, $ext=null)
Get the URL corresponding to one of the four basic zones.
Definition: FileRepo.php:318
FileRepo\$oldFileFactoryKey
callable false $oldFileFactoryKey
Override these in the base class.
Definition: FileRepo.php:139
FileRepo\getZonePath
getZonePath( $zone)
Get the storage path corresponding to one of the zones.
Definition: FileRepo.php:399
FileRepo\getLocalReference
getLocalReference( $virtualUrl)
Get a local FS file with a given virtual URL/storage path.
Definition: FileRepo.php:1622
FileRepo\getZoneLocation
getZoneLocation( $zone)
The storage container and base path of a zone.
Definition: FileRepo.php:385
FileRepo\$backend
FileBackend $backend
Definition: FileRepo.php:68
$base
$base
Definition: generateLocalAutoload.php:11
FileRepo\getUploadStash
getUploadStash(UserIdentity $user=null)
Get an UploadStash associated with this repo.
Definition: FileRepo.php:1971
FileRepo\$wanCache
WANObjectCache $wanCache
Definition: FileRepo.php:152
FileRepo\$initialCapital
bool $initialCapital
Equivalent to $wgCapitalLinks (or $wgCapitalLinkOverrides[NS_FILE], determines whether filenames impl...
Definition: FileRepo.php:99
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
File\normalizeTitle
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:205
FileRepo\publish
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:1265
FileRepo\getNameFromTitle
getNameFromTitle( $title)
Get the name of a file from its title.
Definition: FileRepo.php:717
FileRepo\getDescriptionStylesheetUrl
getDescriptionStylesheetUrl()
Get the URL of the stylesheet to apply to description pages.
Definition: FileRepo.php:889
FileRepo\fileExistsBatch
fileExistsBatch(array $files)
Checks existence of an array of files.
Definition: FileRepo.php:1457
FileRepo
Base class for file repositories.
Definition: FileRepo.php:45
wfAppendQuery
wfAppendQuery( $url, $query)
Append a query string to an existing URL, which may or may not already have query string parameters a...
Definition: GlobalFunctions.php:422
WANObjectCache\newEmpty
static newEmpty()
Get an instance that wraps EmptyBagOStuff.
Definition: WANObjectCache.php:382
FileRepo\freeTemp
freeTemp( $virtualUrl)
Remove a temporary file or mark it for garbage collection.
Definition: FileRepo.php:1190
FileRepo\findFiles
findFiles(array $items, $flags=0)
Find many files at once.
Definition: FileRepo.php:541
FileRepo\quickPurge
quickPurge( $path)
Purge a file from the repo.
Definition: FileRepo.php:1118
FileRepo\hasSha1Storage
hasSha1Storage()
Returns whether or not storage is SHA-1 based.
Definition: FileRepo.php:2016
FileRepo\findFileFromKey
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:586
FileRepo\newFile
newFile( $title, $time=false)
Create a new File object from the local repository.
Definition: FileRepo.php:423
MWException
MediaWiki exception.
Definition: MWException.php:29
FileRepo\enumFiles
enumFiles( $callback)
Call a callback function for every public regular file in the repository.
Definition: FileRepo.php:1720
FileRepo\quickImport
quickImport( $src, $dst, $options=null)
Import a file from the local file system into the repo.
Definition: FileRepo.php:1055
FileBackend\isStoragePath
static isStoragePath( $path)
Check if a given path is a "mwstore://" path.
Definition: FileBackend.php:1525
FileRepo\$thumbScriptUrl
string $thumbScriptUrl
URL of thumb.php.
Definition: FileRepo.php:74
FileRepo\findFilesByPrefix
findFilesByPrefix( $prefix, $limit)
Return an array of files where the name starts with $prefix.
Definition: FileRepo.php:661
FileRepo\$zones
array $zones
Map of zones to config.
Definition: FileRepo.php:71
FileRepo\paranoidClean
paranoidClean( $param)
Path disclosure protection function.
Definition: FileRepo.php:1790
FileRepo\invalidateImageRedirect
invalidateImageRedirect( $title)
Invalidates image redirect cache related to that image Doesn't do anything for repositories that don'...
Definition: FileRepo.php:1850
MediaWiki
A helper class for throttling authentication attempts.
FileBackend\ATTR_UNICODE_PATHS
const ATTR_UNICODE_PATHS
Definition: FileBackend.php:136
FileRepo\getDescriptionUrl
getDescriptionUrl( $name)
Get the URL of an image description page.
Definition: FileRepo.php:830
FileRepo\getThumbProxySecret
getThumbProxySecret()
Get the secret key for the proxied thumb service.
Definition: FileRepo.php:688
FileRepo\store
store( $srcPath, $dstZone, $dstRel, $flags=0)
Store a file to a given destination.
Definition: FileRepo.php:916
FileRepo\getSharedCacheKey
getSharedCacheKey( $kClassSuffix,... $components)
Get a global, repository-qualified, WAN cache key.
Definition: FileRepo.php:1905
MWTimestamp\getInstance
static getInstance( $ts=false)
Get a timestamp instance in GMT.
Definition: MWTimestamp.php:45
$title
$title
Definition: testCompression.php:38
MWFileProps
MimeMagic helper wrapper.
Definition: MWFileProps.php:28
FileRepo\nameForThumb
nameForThumb( $name)
Get the portion of the file that contains the origin file name.
Definition: FileRepo.php:1876
FileRepo\getThumbProxyUrl
getThumbProxyUrl()
Get the URL thumb.php requests are being proxied to.
Definition: FileRepo.php:679
FileRepo\getLocalCopy
getLocalCopy( $virtualUrl)
Get a local FS copy of a file with a given virtual URL/storage path.
Definition: FileRepo.php:1608
TempFileRepo
FileRepo for temporary files created via FileRepo::getTempRepo()
Definition: TempFileRepo.php:5
FileRepo\cleanupBatch
cleanupBatch(array $files, $flags=0)
Deletes a batch of files.
Definition: FileRepo.php:1011
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:894
FileRepo\fileExists
fileExists( $file)
Checks existence of a file.
Definition: FileRepo.php:1445
FileRepo\getFileSha1
getFileSha1( $virtualUrl)
Get the sha1 (base 36) of a file with a given virtual URL/storage path.
Definition: FileRepo.php:1677
FileRepo\backendSupportsUnicodePaths
backendSupportsUnicodePaths()
Definition: FileRepo.php:347
FileRepo\newFatal
newFatal( $message,... $parameters)
Create a new fatal error.
Definition: FileRepo.php:1811
MediaWiki\Permissions\Authority
This interface represents the authority associated the current execution context, such as a web reque...
Definition: Authority.php:37
FileRepo\getLocalCacheKey
getLocalCacheKey( $kClassSuffix,... $components)
Get a site-local, repository-qualified, WAN cache key.
Definition: FileRepo.php:1920
FileRepo\getDisplayName
getDisplayName()
Get the human-readable name of the repo.
Definition: FileRepo.php:1858
FileRepo\findFile
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:458
FileRepo\SKIP_LOCKING
const SKIP_LOCKING
Definition: FileRepo.php:49
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:82
FileRepo\storeTemp
storeTemp( $originalName, $srcPath)
Pick a random name in the temp zone and store a file to it.
Definition: FileRepo.php:1170
FileRepo\publishBatch
publishBatch(array $ntuples, $flags=0)
Publish a batch of files.
Definition: FileRepo.php:1292
WANObjectCache
Multi-datacenter aware caching interface.
Definition: WANObjectCache.php:128
$wgSitename
$wgSitename
Name of the site.
Definition: DefaultSettings.php:80
FileRepo\$scriptDirUrl
string $scriptDirUrl
URL of the MediaWiki installation, equivalent to $wgScriptPath, e.g.
Definition: FileRepo.php:89
FileRepo\$articleUrl
string $articleUrl
Equivalent to $wgArticlePath, e.g.
Definition: FileRepo.php:92
FileRepo\getHashLevels
getHashLevels()
Get the number of hash directory levels.
Definition: FileRepo.php:790
FileRepo\initDirectory
initDirectory( $dir)
Creates a directory with the appropriate zone permissions.
Definition: FileRepo.php:1403
FSFile
Class representing a non-directory file on the file system.
Definition: FSFile.php:32
FileRepo\getBackend
getBackend()
Get the file backend instance.
Definition: FileRepo.php:251
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:484
FileRepo\$fileFactory
callable $fileFactory
Override these in the base class.
Definition: FileRepo.php:133
FileRepo\quickPurgeBatch
quickPurgeBatch(array $paths)
Purge a batch of files from the repo.
Definition: FileRepo.php:1145
FileRepo\getFileSize
getFileSize( $virtualUrl)
Get the size of a file with a given virtual URL/storage path.
Definition: FileRepo.php:1665
FileRepo\isLocal
isLocal()
Returns true if this the local file repository.
Definition: FileRepo.php:1890
FileRepo\$thumbProxySecret
string $thumbProxySecret
Secret key to pass as an X-Swift-Secret header to the proxied thumb service.
Definition: FileRepo.php:146
FileRepo\getHashPath
getHashPath( $name)
Get a relative path including trailing slash, e.g.
Definition: FileRepo.php:749
FileRepo\$name
string $name
Definition: FileRepo.php:159
FileRepo\getErrorCleanupFunction
getErrorCleanupFunction()
Get a callback function to use for cleaning error message parameters.
Definition: FileRepo.php:1772
FileRepo\__construct
__construct(array $info=null)
Definition: FileRepo.php:167
FileRepo\findBySha1s
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:641
FileRepo\getTempHashPath
getTempHashPath( $suffix)
Get a relative path including trailing slash, e.g.
Definition: FileRepo.php:760
FileRepo\$descBaseUrl
string $descBaseUrl
URL of image description pages, e.g.
Definition: FileRepo.php:84
$path
$path
Definition: NoLocalSettings.php:25
FileRepo\quickCleanDir
quickCleanDir( $dir)
Deletes a directory if empty.
Definition: FileRepo.php:1129
FileRepo\isVirtualUrl
static isVirtualUrl( $url)
Determine if a string is an mwrepo:// URL.
Definition: FileRepo.php:290
FileRepo\getFileTimestamp
getFileTimestamp( $virtualUrl)
Get the timestamp of a file with a given virtual URL/storage path.
Definition: FileRepo.php:1653
FileRepo\concatenate
concatenate(array $srcPaths, $dstPath, $flags=0)
Concatenate a list of temporary files into a target file location.
Definition: FileRepo.php:1212
FileRepo\deleteBatch
deleteBatch(array $sourceDestPairs)
Move a group of files to the deletion archive.
Definition: FileRepo.php:1503
FileRepo\resolveVirtualUrl
resolveVirtualUrl( $url)
Get the backend storage path corresponding to a virtual URL.
Definition: FileRepo.php:359
$source
$source
Definition: mwdoc-filter.php:34
FileBackend\isPathTraversalFree
static isPathTraversalFree( $path)
Check if a relative path has no directory traversals.
Definition: FileBackend.php:1621
FileRepo\cleanDir
cleanDir( $dir)
Deletes a directory if empty.
Definition: FileRepo.php:1429
$ext
if(!is_readable( $file)) $ext
Definition: router.php:48
$hashes
$hashes
Definition: testCompression.php:71
FileRepo\DELETE_SOURCE
const DELETE_SOURCE
Definition: FileRepo.php:46
File\DELETED_FILE
const DELETED_FILE
Definition: File.php:70
FileRepo\checkRedirect
checkRedirect( $title)
Checks if there is a redirect named as $title.
Definition: FileRepo.php:1839
NS_FILE
const NS_FILE
Definition: Defines.php:70
FileRepo\getFileProps
getFileProps( $virtualUrl)
Get properties of a file with a given virtual URL/storage path.
Definition: FileRepo.php:1635
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
FileRepo\$abbrvThreshold
int $abbrvThreshold
File names over this size will use the short form of thumbnail names.
Definition: FileRepo.php:124
FileRepo\$transformVia404
bool $transformVia404
Whether to skip media file transformation on parse and rely on a 404 handler instead.
Definition: FileRepo.php:79
FileRepo\supportsSha1URLs
supportsSha1URLs()
Returns whether or not repo supports having originals SHA-1s in the thumb URLs.
Definition: FileRepo.php:2024
FileRepo\$hashLevels
int $hashLevels
The number of directory levels for hash-based division of files.
Definition: FileRepo.php:115
FileRepo\getDeletedHashPath
getDeletedHashPath( $key)
Get a relative path for a deletion archive key, e.g.
Definition: FileRepo.php:1573
FileRepo\assertWritableRepo
assertWritableRepo()
Throw an exception if this repo is read-only by design.
Definition: FileRepo.php:1982
FileRepo\NAME_AND_TIME_ONLY
const NAME_AND_TIME_ONLY
Definition: FileRepo.php:51
FileRepo\cleanupDeletedBatch
cleanupDeletedBatch(array $storageKeys)
Delete files in the deleted directory if they are not referenced in the filearchive table.
Definition: FileRepo.php:1561
FileRepo\$url
string false $url
Public zone URL.
Definition: FileRepo.php:109
FileRepo\canTransformVia404
canTransformVia404()
Returns true if the repository can transform files via a 404 handler.
Definition: FileRepo.php:697
FileRepo\$fetchDescription
bool $fetchDescription
Whether to fetch commons image description pages and display them on the local wiki.
Definition: FileRepo.php:56
FileRepo\$thumbProxyUrl
string $thumbProxyUrl
URL of where to proxy thumb.php requests to.
Definition: FileRepo.php:144