MediaWiki  master
FileRepo.php
Go to the documentation of this file.
1 <?php
11 
41 class FileRepo {
42  public const DELETE_SOURCE = 1;
43  public const OVERWRITE = 2;
44  public const OVERWRITE_SAME = 4;
45  public const SKIP_LOCKING = 8;
46 
47  public const NAME_AND_TIME_ONLY = 1;
48 
53 
56 
58  protected $hasSha1Storage = false;
59 
61  protected $supportsSha1URLs = false;
62 
64  protected $backend;
65 
67  protected $zones = [];
68 
70  protected $thumbScriptUrl;
71 
75  protected $transformVia404;
76 
80  protected $descBaseUrl;
81 
85  protected $scriptDirUrl;
86 
88  protected $articleUrl;
89 
95  protected $initialCapital;
96 
102  protected $pathDisclosureProtection = 'simple';
103 
105  protected $url;
106 
108  protected $thumbUrl;
109 
111  protected $hashLevels;
112 
115 
120  protected $abbrvThreshold;
121 
123  protected $favicon;
124 
126  protected $isPrivate;
127 
129  protected $fileFactory = [ UnregisteredLocalFile::class, 'newFromTitle' ];
131  protected $oldFileFactory = false;
133  protected $fileFactoryKey = false;
135  protected $oldFileFactoryKey = false;
136 
140  protected $thumbProxyUrl;
142  protected $thumbProxySecret;
143 
145  protected $wanCache;
146 
152  public $name;
153 
160  public function __construct( array $info = null ) {
161  // Verify required settings presence
162  if (
163  $info === null
164  || !array_key_exists( 'name', $info )
165  || !array_key_exists( 'backend', $info )
166  ) {
167  throw new MWException( __CLASS__ .
168  " requires an array of options having both 'name' and 'backend' keys.\n" );
169  }
170 
171  // Required settings
172  $this->name = $info['name'];
173  if ( $info['backend'] instanceof FileBackend ) {
174  $this->backend = $info['backend']; // useful for testing
175  } else {
176  $this->backend =
177  MediaWikiServices::getInstance()->getFileBackendGroup()->get( $info['backend'] );
178  }
179 
180  // Optional settings that can have no value
181  $optionalSettings = [
182  'descBaseUrl', 'scriptDirUrl', 'articleUrl', 'fetchDescription',
183  'thumbScriptUrl', 'pathDisclosureProtection', 'descriptionCacheExpiry',
184  'favicon', 'thumbProxyUrl', 'thumbProxySecret',
185  ];
186  foreach ( $optionalSettings as $var ) {
187  if ( isset( $info[$var] ) ) {
188  $this->$var = $info[$var];
189  }
190  }
191 
192  // Optional settings that have a default
193  $localCapitalLinks =
194  MediaWikiServices::getInstance()->getNamespaceInfo()->isCapitalized( NS_FILE );
195  $this->initialCapital = $info['initialCapital'] ?? $localCapitalLinks;
196  if ( $localCapitalLinks && !$this->initialCapital ) {
197  // If the local wiki's file namespace requires an initial capital, but a foreign file
198  // repo doesn't, complications will result. Linker code will want to auto-capitalize the
199  // first letter of links to files, but those links might actually point to files on
200  // foreign wikis with initial-lowercase names. This combination is not likely to be
201  // used by anyone anyway, so we just outlaw it to save ourselves the bugs. If you want
202  // to include a foreign file repo with initialCapital false, set your local file
203  // namespace to not be capitalized either.
204  throw new InvalidArgumentException(
205  'File repos with initial capital false are not allowed on wikis where the File ' .
206  'namespace has initial capital true' );
207  }
208 
209  $this->url = $info['url'] ?? false; // a subclass may set the URL (e.g. ForeignAPIRepo)
210  if ( isset( $info['thumbUrl'] ) ) {
211  $this->thumbUrl = $info['thumbUrl'];
212  } else {
213  $this->thumbUrl = $this->url ? "{$this->url}/thumb" : false;
214  }
215  $this->hashLevels = $info['hashLevels'] ?? 2;
216  $this->deletedHashLevels = $info['deletedHashLevels'] ?? $this->hashLevels;
217  $this->transformVia404 = !empty( $info['transformVia404'] );
218  $this->abbrvThreshold = $info['abbrvThreshold'] ?? 255;
219  $this->isPrivate = !empty( $info['isPrivate'] );
220  // Give defaults for the basic zones...
221  $this->zones = $info['zones'] ?? [];
222  foreach ( [ 'public', 'thumb', 'transcoded', 'temp', 'deleted' ] as $zone ) {
223  if ( !isset( $this->zones[$zone]['container'] ) ) {
224  $this->zones[$zone]['container'] = "{$this->name}-{$zone}";
225  }
226  if ( !isset( $this->zones[$zone]['directory'] ) ) {
227  $this->zones[$zone]['directory'] = '';
228  }
229  if ( !isset( $this->zones[$zone]['urlsByExt'] ) ) {
230  $this->zones[$zone]['urlsByExt'] = [];
231  }
232  }
233 
234  $this->supportsSha1URLs = !empty( $info['supportsSha1URLs'] );
235 
236  $this->wanCache = $info['wanCache'] ?? WANObjectCache::newEmpty();
237  }
238 
244  public function getBackend() {
245  return $this->backend;
246  }
247 
254  public function getReadOnlyReason() {
255  return $this->backend->getReadOnlyReason();
256  }
257 
265  protected function initZones( $doZones = [] ) {
266  $status = $this->newGood();
267  foreach ( (array)$doZones as $zone ) {
268  $root = $this->getZonePath( $zone );
269  if ( $root === null ) {
270  throw new MWException( "No '$zone' zone defined in the {$this->name} repo." );
271  }
272  }
273 
274  return $status;
275  }
276 
283  public static function isVirtualUrl( $url ) {
284  return substr( $url, 0, 9 ) == 'mwrepo://';
285  }
286 
295  public function getVirtualUrl( $suffix = false ) {
296  $path = 'mwrepo://' . $this->name;
297  if ( $suffix !== false ) {
298  $path .= '/' . rawurlencode( $suffix );
299  }
300 
301  return $path;
302  }
303 
311  public function getZoneUrl( $zone, $ext = null ) {
312  if ( in_array( $zone, [ 'public', 'thumb', 'transcoded' ] ) ) {
313  // standard public zones
314  if ( $ext !== null && isset( $this->zones[$zone]['urlsByExt'][$ext] ) ) {
315  // custom URL for extension/zone
316  return $this->zones[$zone]['urlsByExt'][$ext];
317  } elseif ( isset( $this->zones[$zone]['url'] ) ) {
318  // custom URL for zone
319  return $this->zones[$zone]['url'];
320  }
321  }
322  switch ( $zone ) {
323  case 'public':
324  return $this->url;
325  case 'temp':
326  case 'deleted':
327  return false; // no public URL
328  case 'thumb':
329  return $this->thumbUrl;
330  case 'transcoded':
331  return "{$this->url}/transcoded";
332  default:
333  return false;
334  }
335  }
336 
340  public function backendSupportsUnicodePaths() {
341  return (bool)( $this->getBackend()->getFeatures() & FileBackend::ATTR_UNICODE_PATHS );
342  }
343 
352  public function resolveVirtualUrl( $url ) {
353  if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
354  throw new MWException( __METHOD__ . ': unknown protocol' );
355  }
356  $bits = explode( '/', substr( $url, 9 ), 3 );
357  if ( count( $bits ) != 3 ) {
358  throw new MWException( __METHOD__ . ": invalid mwrepo URL: $url" );
359  }
360  list( $repo, $zone, $rel ) = $bits;
361  if ( $repo !== $this->name ) {
362  throw new MWException( __METHOD__ . ": fetching from a foreign repo is not supported" );
363  }
364  $base = $this->getZonePath( $zone );
365  if ( !$base ) {
366  throw new MWException( __METHOD__ . ": invalid zone: $zone" );
367  }
368 
369  return $base . '/' . rawurldecode( $rel );
370  }
371 
378  protected function getZoneLocation( $zone ) {
379  if ( !isset( $this->zones[$zone] ) ) {
380  return [ null, null ]; // bogus
381  }
382 
383  return [ $this->zones[$zone]['container'], $this->zones[$zone]['directory'] ];
384  }
385 
392  public function getZonePath( $zone ) {
393  list( $container, $base ) = $this->getZoneLocation( $zone );
394  if ( $container === null || $base === null ) {
395  return null;
396  }
397  $backendName = $this->backend->getName();
398  if ( $base != '' ) { // may not be set
399  $base = "/{$base}";
400  }
401 
402  return "mwstore://$backendName/{$container}{$base}";
403  }
404 
416  public function newFile( $title, $time = false ) {
418  if ( !$title ) {
419  return null;
420  }
421  if ( $time ) {
422  if ( $this->oldFileFactory ) {
423  return call_user_func( $this->oldFileFactory, $title, $this, $time );
424  } else {
425  return null;
426  }
427  } else {
428  return call_user_func( $this->fileFactory, $title, $this );
429  }
430  }
431 
449  public function findFile( $title, $options = [] ) {
451  if ( !$title ) {
452  return false;
453  }
454  if ( isset( $options['bypassCache'] ) ) {
455  $options['latest'] = $options['bypassCache']; // b/c
456  }
457  $time = $options['time'] ?? false;
458  $flags = !empty( $options['latest'] ) ? File::READ_LATEST : 0;
459  # First try the current version of the file to see if it precedes the timestamp
460  $img = $this->newFile( $title );
461  if ( !$img ) {
462  return false;
463  }
464  $img->load( $flags );
465  if ( $img->exists() && ( !$time || $img->getTimestamp() == $time ) ) {
466  return $img;
467  }
468  # Now try an old version of the file
469  if ( $time !== false ) {
470  $img = $this->newFile( $title, $time );
471  if ( $img ) {
472  $img->load( $flags );
473  if ( $img->exists() ) {
474  if ( !$img->isDeleted( File::DELETED_FILE ) ) {
475  return $img; // always OK
476  } elseif ( !empty( $options['private'] ) &&
477  $img->userCan( File::DELETED_FILE,
478  $options['private'] instanceof User ? $options['private'] : null
479  )
480  ) {
481  return $img;
482  }
483  }
484  }
485  }
486 
487  # Now try redirects
488  if ( !empty( $options['ignoreRedirect'] ) ) {
489  return false;
490  }
491  $redir = $this->checkRedirect( $title );
492  if ( $redir && $title->getNamespace() === NS_FILE ) {
493  $img = $this->newFile( $redir );
494  if ( !$img ) {
495  return false;
496  }
497  $img->load( $flags );
498  if ( $img->exists() ) {
499  $img->redirectedFrom( $title->getDBkey() );
500 
501  return $img;
502  }
503  }
504 
505  return false;
506  }
507 
525  public function findFiles( array $items, $flags = 0 ) {
526  $result = [];
527  foreach ( $items as $item ) {
528  if ( is_array( $item ) ) {
529  $title = $item['title'];
530  $options = $item;
531  unset( $options['title'] );
532  } else {
533  $title = $item;
534  $options = [];
535  }
536  $file = $this->findFile( $title, $options );
537  if ( $file ) {
538  $searchName = File::normalizeTitle( $title )->getDBkey(); // must be valid
539  if ( $flags & self::NAME_AND_TIME_ONLY ) {
540  $result[$searchName] = [
541  'title' => $file->getTitle()->getDBkey(),
542  'timestamp' => $file->getTimestamp()
543  ];
544  } else {
545  $result[$searchName] = $file;
546  }
547  }
548  }
549 
550  return $result;
551  }
552 
562  public function findFileFromKey( $sha1, $options = [] ) {
563  $time = $options['time'] ?? false;
564  # First try to find a matching current version of a file...
565  if ( !$this->fileFactoryKey ) {
566  return false; // find-by-sha1 not supported
567  }
568  $img = call_user_func( $this->fileFactoryKey, $sha1, $this, $time );
569  if ( $img && $img->exists() ) {
570  return $img;
571  }
572  # Now try to find a matching old version of a file...
573  if ( $time !== false && $this->oldFileFactoryKey ) { // find-by-sha1 supported?
574  $img = call_user_func( $this->oldFileFactoryKey, $sha1, $this, $time );
575  if ( $img && $img->exists() ) {
576  if ( !$img->isDeleted( File::DELETED_FILE ) ) {
577  return $img; // always OK
578  } elseif ( !empty( $options['private'] ) &&
579  $img->userCan( File::DELETED_FILE,
580  $options['private'] instanceof User ? $options['private'] : null
581  )
582  ) {
583  return $img;
584  }
585  }
586  }
587 
588  return false;
589  }
590 
599  public function findBySha1( $hash ) {
600  return [];
601  }
602 
610  public function findBySha1s( array $hashes ) {
611  $result = [];
612  foreach ( $hashes as $hash ) {
613  $files = $this->findBySha1( $hash );
614  if ( count( $files ) ) {
615  $result[$hash] = $files;
616  }
617  }
618 
619  return $result;
620  }
621 
630  public function findFilesByPrefix( $prefix, $limit ) {
631  return [];
632  }
633 
639  public function getThumbScriptUrl() {
640  return $this->thumbScriptUrl;
641  }
642 
648  public function getThumbProxyUrl() {
649  return $this->thumbProxyUrl;
650  }
651 
657  public function getThumbProxySecret() {
659  }
660 
666  public function canTransformVia404() {
667  return $this->transformVia404;
668  }
669 
676  public function getNameFromTitle( Title $title ) {
677  if (
678  $this->initialCapital !=
679  MediaWikiServices::getInstance()->getNamespaceInfo()->isCapitalized( NS_FILE )
680  ) {
681  $name = $title->getDBkey();
682  if ( $this->initialCapital ) {
683  $name = MediaWikiServices::getInstance()->getContentLanguage()->ucfirst( $name );
684  }
685  } else {
686  $name = $title->getDBkey();
687  }
688 
689  return $name;
690  }
691 
697  public function getRootDirectory() {
698  return $this->getZonePath( 'public' );
699  }
700 
708  public function getHashPath( $name ) {
709  return self::getHashPathForLevel( $name, $this->hashLevels );
710  }
711 
719  public function getTempHashPath( $suffix ) {
720  $parts = explode( '!', $suffix, 2 ); // format is <timestamp>!<name> or just <name>
721  $name = $parts[1] ?? $suffix; // hash path is not based on timestamp
722  return self::getHashPathForLevel( $name, $this->hashLevels );
723  }
724 
730  protected static function getHashPathForLevel( $name, $levels ) {
731  if ( $levels == 0 ) {
732  return '';
733  } else {
734  $hash = md5( $name );
735  $path = '';
736  for ( $i = 1; $i <= $levels; $i++ ) {
737  $path .= substr( $hash, 0, $i ) . '/';
738  }
739 
740  return $path;
741  }
742  }
743 
749  public function getHashLevels() {
750  return $this->hashLevels;
751  }
752 
758  public function getName() {
759  return $this->name;
760  }
761 
769  public function makeUrl( $query = '', $entry = 'index' ) {
770  if ( isset( $this->scriptDirUrl ) ) {
771  return wfAppendQuery( "{$this->scriptDirUrl}/{$entry}.php", $query );
772  }
773 
774  return false;
775  }
776 
789  public function getDescriptionUrl( $name ) {
790  $encName = wfUrlencode( $name );
791  if ( $this->descBaseUrl !== null ) {
792  # "http://example.com/wiki/File:"
793  return $this->descBaseUrl . $encName;
794  }
795  if ( $this->articleUrl !== null ) {
796  # "http://example.com/wiki/$1"
797  # We use "Image:" as the canonical namespace for
798  # compatibility across all MediaWiki versions.
799  return str_replace( '$1',
800  "Image:$encName", $this->articleUrl );
801  }
802  if ( $this->scriptDirUrl !== null ) {
803  # "http://example.com/w"
804  # We use "Image:" as the canonical namespace for
805  # compatibility across all MediaWiki versions,
806  # and just sort of hope index.php is right. ;)
807  return $this->makeUrl( "title=Image:$encName" );
808  }
809 
810  return false;
811  }
812 
823  public function getDescriptionRenderUrl( $name, $lang = null ) {
824  $query = 'action=render';
825  if ( $lang !== null ) {
826  $query .= '&uselang=' . urlencode( $lang );
827  }
828  if ( isset( $this->scriptDirUrl ) ) {
829  return $this->makeUrl(
830  'title=' .
831  wfUrlencode( 'Image:' . $name ) .
832  "&$query" );
833  } else {
834  $descUrl = $this->getDescriptionUrl( $name );
835  if ( $descUrl ) {
836  return wfAppendQuery( $descUrl, $query );
837  } else {
838  return false;
839  }
840  }
841  }
842 
848  public function getDescriptionStylesheetUrl() {
849  if ( isset( $this->scriptDirUrl ) ) {
850  // Must match canonical query parameter order for optimum caching
851  // See Title::getCdnUrls
852  return $this->makeUrl( 'title=MediaWiki:Filepage.css&action=raw&ctype=text/css' );
853  }
854 
855  return false;
856  }
857 
875  public function store( $srcPath, $dstZone, $dstRel, $flags = 0 ) {
876  $this->assertWritableRepo(); // fail out if read-only
877 
878  $status = $this->storeBatch( [ [ $srcPath, $dstZone, $dstRel ] ], $flags );
879  if ( $status->successCount == 0 ) {
880  $status->setOK( false );
881  }
882 
883  return $status;
884  }
885 
900  public function storeBatch( array $triplets, $flags = 0 ) {
901  $this->assertWritableRepo(); // fail out if read-only
902 
903  if ( $flags & self::DELETE_SOURCE ) {
904  throw new InvalidArgumentException( "DELETE_SOURCE not supported in " . __METHOD__ );
905  }
906 
907  $status = $this->newGood();
908  $backend = $this->backend; // convenience
909 
910  $operations = [];
911  // Validate each triplet and get the store operation...
912  foreach ( $triplets as $triplet ) {
913  list( $src, $dstZone, $dstRel ) = $triplet;
914  $srcPath = ( $src instanceof FSFile ) ? $src->getPath() : $src;
915  wfDebug( __METHOD__
916  . "( \$src='$srcPath', \$dstZone='$dstZone', \$dstRel='$dstRel' )"
917  );
918  // Resolve source path
919  if ( $src instanceof FSFile ) {
920  $op = 'store';
921  } else {
922  $src = $this->resolveToStoragePathIfVirtual( $src );
923  $op = FileBackend::isStoragePath( $src ) ? 'copy' : 'store';
924  }
925  // Resolve destination path
926  $root = $this->getZonePath( $dstZone );
927  if ( !$root ) {
928  throw new MWException( "Invalid zone: $dstZone" );
929  }
930  if ( !$this->validateFilename( $dstRel ) ) {
931  throw new MWException( 'Validation error in $dstRel' );
932  }
933  $dstPath = "$root/$dstRel";
934  $dstDir = dirname( $dstPath );
935  // Create destination directories for this triplet
936  if ( !$this->initDirectory( $dstDir )->isOK() ) {
937  return $this->newFatal( 'directorycreateerror', $dstDir );
938  }
939 
940  // Copy the source file to the destination
941  $operations[] = [
942  'op' => $op,
943  'src' => $src, // storage path (copy) or local file path (store)
944  'dst' => $dstPath,
945  'overwrite' => ( $flags & self::OVERWRITE ) ? true : false,
946  'overwriteSame' => ( $flags & self::OVERWRITE_SAME ) ? true : false,
947  ];
948  }
949 
950  // Execute the store operation for each triplet
951  $opts = [ 'force' => true ];
952  if ( $flags & self::SKIP_LOCKING ) {
953  $opts['nonLocking'] = true;
954  }
955  $status->merge( $backend->doOperations( $operations, $opts ) );
956 
957  return $status;
958  }
959 
970  public function cleanupBatch( array $files, $flags = 0 ) {
971  $this->assertWritableRepo(); // fail out if read-only
972 
973  $status = $this->newGood();
974 
975  $operations = [];
976  foreach ( $files as $path ) {
977  if ( is_array( $path ) ) {
978  // This is a pair, extract it
979  list( $zone, $rel ) = $path;
980  $path = $this->getZonePath( $zone ) . "/$rel";
981  } else {
982  // Resolve source to a storage path if virtual
983  $path = $this->resolveToStoragePathIfVirtual( $path );
984  }
985  $operations[] = [ 'op' => 'delete', 'src' => $path ];
986  }
987  // Actually delete files from storage...
988  $opts = [ 'force' => true ];
989  if ( $flags & self::SKIP_LOCKING ) {
990  $opts['nonLocking'] = true;
991  }
992  $status->merge( $this->backend->doOperations( $operations, $opts ) );
993 
994  return $status;
995  }
996 
1014  final public function quickImport( $src, $dst, $options = null ) {
1015  return $this->quickImportBatch( [ [ $src, $dst, $options ] ] );
1016  }
1017 
1032  public function quickImportBatch( array $triples ) {
1033  $status = $this->newGood();
1034  $operations = [];
1035  foreach ( $triples as $triple ) {
1036  list( $src, $dst ) = $triple;
1037  if ( $src instanceof FSFile ) {
1038  $op = 'store';
1039  } else {
1040  $src = $this->resolveToStoragePathIfVirtual( $src );
1041  $op = FileBackend::isStoragePath( $src ) ? 'copy' : 'store';
1042  }
1043  $dst = $this->resolveToStoragePathIfVirtual( $dst );
1044 
1045  if ( !isset( $triple[2] ) ) {
1046  $headers = [];
1047  } elseif ( is_string( $triple[2] ) ) {
1048  // back-compat
1049  $headers = [ 'Content-Disposition' => $triple[2] ];
1050  } elseif ( is_array( $triple[2] ) && isset( $triple[2]['headers'] ) ) {
1051  $headers = $triple[2]['headers'];
1052  } else {
1053  $headers = [];
1054  }
1055 
1056  $operations[] = [
1057  'op' => $op,
1058  'src' => $src, // storage path (copy) or local path/FSFile (store)
1059  'dst' => $dst,
1060  'headers' => $headers
1061  ];
1062  $status->merge( $this->initDirectory( dirname( $dst ) ) );
1063  }
1064  $status->merge( $this->backend->doQuickOperations( $operations ) );
1065 
1066  return $status;
1067  }
1068 
1077  final public function quickPurge( $path ) {
1078  return $this->quickPurgeBatch( [ $path ] );
1079  }
1080 
1088  public function quickCleanDir( $dir ) {
1089  $status = $this->newGood();
1090  $status->merge( $this->backend->clean(
1091  [ 'dir' => $this->resolveToStoragePathIfVirtual( $dir ) ] ) );
1092 
1093  return $status;
1094  }
1095 
1104  public function quickPurgeBatch( array $paths ) {
1105  $status = $this->newGood();
1106  $operations = [];
1107  foreach ( $paths as $path ) {
1108  $operations[] = [
1109  'op' => 'delete',
1110  'src' => $this->resolveToStoragePathIfVirtual( $path ),
1111  'ignoreMissingSource' => true
1112  ];
1113  }
1114  $status->merge( $this->backend->doQuickOperations( $operations ) );
1115 
1116  return $status;
1117  }
1118 
1129  public function storeTemp( $originalName, $srcPath ) {
1130  $this->assertWritableRepo(); // fail out if read-only
1131 
1132  $date = MWTimestamp::getInstance()->format( 'YmdHis' );
1133  $hashPath = $this->getHashPath( $originalName );
1134  $dstUrlRel = $hashPath . $date . '!' . rawurlencode( $originalName );
1135  $virtualUrl = $this->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
1136 
1137  $result = $this->quickImport( $srcPath, $virtualUrl );
1138  $result->value = $virtualUrl;
1139 
1140  return $result;
1141  }
1142 
1149  public function freeTemp( $virtualUrl ) {
1150  $this->assertWritableRepo(); // fail out if read-only
1151 
1152  $temp = $this->getVirtualUrl( 'temp' );
1153  if ( substr( $virtualUrl, 0, strlen( $temp ) ) != $temp ) {
1154  wfDebug( __METHOD__ . ": Invalid temp virtual URL" );
1155 
1156  return false;
1157  }
1158 
1159  return $this->quickPurge( $virtualUrl )->isOK();
1160  }
1161 
1171  public function concatenate( array $srcPaths, $dstPath, $flags = 0 ) {
1172  $this->assertWritableRepo(); // fail out if read-only
1173 
1174  $status = $this->newGood();
1175 
1176  $sources = [];
1177  foreach ( $srcPaths as $srcPath ) {
1178  // Resolve source to a storage path if virtual
1179  $source = $this->resolveToStoragePathIfVirtual( $srcPath );
1180  $sources[] = $source; // chunk to merge
1181  }
1182 
1183  // Concatenate the chunks into one FS file
1184  $params = [ 'srcs' => $sources, 'dst' => $dstPath ];
1185  $status->merge( $this->backend->concatenate( $params ) );
1186  if ( !$status->isOK() ) {
1187  return $status;
1188  }
1189 
1190  // Delete the sources if required
1191  if ( $flags & self::DELETE_SOURCE ) {
1192  $status->merge( $this->quickPurgeBatch( $srcPaths ) );
1193  }
1194 
1195  // Make sure status is OK, despite any quickPurgeBatch() fatals
1196  $status->setResult( true );
1197 
1198  return $status;
1199  }
1200 
1224  public function publish(
1225  $src, $dstRel, $archiveRel, $flags = 0, array $options = []
1226  ) {
1227  $this->assertWritableRepo(); // fail out if read-only
1228 
1229  $status = $this->publishBatch(
1230  [ [ $src, $dstRel, $archiveRel, $options ] ], $flags );
1231  if ( $status->successCount == 0 ) {
1232  $status->setOK( false );
1233  }
1234  $status->value = $status->value[0] ?? false;
1235 
1236  return $status;
1237  }
1238 
1251  public function publishBatch( array $ntuples, $flags = 0 ) {
1252  $this->assertWritableRepo(); // fail out if read-only
1253 
1254  $backend = $this->backend; // convenience
1255  // Try creating directories
1256  $status = $this->initZones( 'public' );
1257  if ( !$status->isOK() ) {
1258  return $status;
1259  }
1260 
1261  $status = $this->newGood( [] );
1262 
1263  $operations = [];
1264  $sourceFSFilesToDelete = []; // cleanup for disk source files
1265  // Validate each triplet and get the store operation...
1266  foreach ( $ntuples as $ntuple ) {
1267  list( $src, $dstRel, $archiveRel ) = $ntuple;
1268  $srcPath = ( $src instanceof FSFile ) ? $src->getPath() : $src;
1269 
1270  $options = $ntuple[3] ?? [];
1271  // Resolve source to a storage path if virtual
1272  $srcPath = $this->resolveToStoragePathIfVirtual( $srcPath );
1273  if ( !$this->validateFilename( $dstRel ) ) {
1274  throw new MWException( 'Validation error in $dstRel' );
1275  }
1276  if ( !$this->validateFilename( $archiveRel ) ) {
1277  throw new MWException( 'Validation error in $archiveRel' );
1278  }
1279 
1280  $publicRoot = $this->getZonePath( 'public' );
1281  $dstPath = "$publicRoot/$dstRel";
1282  $archivePath = "$publicRoot/$archiveRel";
1283 
1284  $dstDir = dirname( $dstPath );
1285  $archiveDir = dirname( $archivePath );
1286  // Abort immediately on directory creation errors since they're likely to be repetitive
1287  if ( !$this->initDirectory( $dstDir )->isOK() ) {
1288  return $this->newFatal( 'directorycreateerror', $dstDir );
1289  }
1290  if ( !$this->initDirectory( $archiveDir )->isOK() ) {
1291  return $this->newFatal( 'directorycreateerror', $archiveDir );
1292  }
1293 
1294  // Set any desired headers to be use in GET/HEAD responses
1295  $headers = $options['headers'] ?? [];
1296 
1297  // Archive destination file if it exists.
1298  // This will check if the archive file also exists and fail if does.
1299  // This is a sanity check to avoid data loss. On Windows and Linux,
1300  // copy() will overwrite, so the existence check is vulnerable to
1301  // race conditions unless a functioning LockManager is used.
1302  // LocalFile also uses SELECT FOR UPDATE for synchronization.
1303  $operations[] = [
1304  'op' => 'copy',
1305  'src' => $dstPath,
1306  'dst' => $archivePath,
1307  'ignoreMissingSource' => true
1308  ];
1309 
1310  // Copy (or move) the source file to the destination
1311  if ( FileBackend::isStoragePath( $srcPath ) ) {
1312  $operations[] = [
1313  'op' => ( $flags & self::DELETE_SOURCE ) ? 'move' : 'copy',
1314  'src' => $srcPath,
1315  'dst' => $dstPath,
1316  'overwrite' => true, // replace current
1317  'headers' => $headers
1318  ];
1319  } else {
1320  $operations[] = [
1321  'op' => 'store',
1322  'src' => $src, // storage path (copy) or local path/FSFile (store)
1323  'dst' => $dstPath,
1324  'overwrite' => true, // replace current
1325  'headers' => $headers
1326  ];
1327  if ( $flags & self::DELETE_SOURCE ) {
1328  $sourceFSFilesToDelete[] = $srcPath;
1329  }
1330  }
1331  }
1332 
1333  // Execute the operations for each triplet
1334  $status->merge( $backend->doOperations( $operations ) );
1335  // Find out which files were archived...
1336  foreach ( $ntuples as $i => $ntuple ) {
1337  list( , , $archiveRel ) = $ntuple;
1338  $archivePath = $this->getZonePath( 'public' ) . "/$archiveRel";
1339  if ( $this->fileExists( $archivePath ) ) {
1340  $status->value[$i] = 'archived';
1341  } else {
1342  $status->value[$i] = 'new';
1343  }
1344  }
1345  // Cleanup for disk source files...
1346  foreach ( $sourceFSFilesToDelete as $file ) {
1347  Wikimedia\suppressWarnings();
1348  unlink( $file ); // FS cleanup
1349  Wikimedia\restoreWarnings();
1350  }
1351 
1352  return $status;
1353  }
1354 
1362  protected function initDirectory( $dir ) {
1363  $path = $this->resolveToStoragePathIfVirtual( $dir );
1364  list( , $container, ) = FileBackend::splitStoragePath( $path );
1365 
1366  $params = [ 'dir' => $path ];
1367  if ( $this->isPrivate
1368  || $container === $this->zones['deleted']['container']
1369  || $container === $this->zones['temp']['container']
1370  ) {
1371  # Take all available measures to prevent web accessibility of new deleted
1372  # directories, in case the user has not configured offline storage
1373  $params = [ 'noAccess' => true, 'noListing' => true ] + $params;
1374  }
1375 
1376  $status = $this->newGood();
1377  $status->merge( $this->backend->prepare( $params ) );
1378 
1379  return $status;
1380  }
1381 
1388  public function cleanDir( $dir ) {
1389  $this->assertWritableRepo(); // fail out if read-only
1390 
1391  $status = $this->newGood();
1392  $status->merge( $this->backend->clean(
1393  [ 'dir' => $this->resolveToStoragePathIfVirtual( $dir ) ] ) );
1394 
1395  return $status;
1396  }
1397 
1404  public function fileExists( $file ) {
1405  $result = $this->fileExistsBatch( [ $file ] );
1406 
1407  return $result[0];
1408  }
1409 
1416  public function fileExistsBatch( array $files ) {
1417  $paths = array_map( [ $this, 'resolveToStoragePathIfVirtual' ], $files );
1418  $this->backend->preloadFileStat( [ 'srcs' => $paths ] );
1419 
1420  $result = [];
1421  foreach ( $files as $key => $file ) {
1423  $result[$key] = $this->backend->fileExists( [ 'src' => $path ] );
1424  }
1425 
1426  return $result;
1427  }
1428 
1439  public function delete( $srcRel, $archiveRel ) {
1440  $this->assertWritableRepo(); // fail out if read-only
1441 
1442  return $this->deleteBatch( [ [ $srcRel, $archiveRel ] ] );
1443  }
1444 
1462  public function deleteBatch( array $sourceDestPairs ) {
1463  $this->assertWritableRepo(); // fail out if read-only
1464 
1465  // Try creating directories
1466  $status = $this->initZones( [ 'public', 'deleted' ] );
1467  if ( !$status->isOK() ) {
1468  return $status;
1469  }
1470 
1471  $status = $this->newGood();
1472 
1473  $backend = $this->backend; // convenience
1474  $operations = [];
1475  // Validate filenames and create archive directories
1476  foreach ( $sourceDestPairs as $pair ) {
1477  list( $srcRel, $archiveRel ) = $pair;
1478  if ( !$this->validateFilename( $srcRel ) ) {
1479  throw new MWException( __METHOD__ . ':Validation error in $srcRel' );
1480  } elseif ( !$this->validateFilename( $archiveRel ) ) {
1481  throw new MWException( __METHOD__ . ':Validation error in $archiveRel' );
1482  }
1483 
1484  $publicRoot = $this->getZonePath( 'public' );
1485  $srcPath = "{$publicRoot}/$srcRel";
1486 
1487  $deletedRoot = $this->getZonePath( 'deleted' );
1488  $archivePath = "{$deletedRoot}/{$archiveRel}";
1489  $archiveDir = dirname( $archivePath ); // does not touch FS
1490 
1491  // Create destination directories
1492  if ( !$this->initDirectory( $archiveDir )->isOK() ) {
1493  return $this->newFatal( 'directorycreateerror', $archiveDir );
1494  }
1495 
1496  $operations[] = [
1497  'op' => 'move',
1498  'src' => $srcPath,
1499  'dst' => $archivePath,
1500  // We may have 2+ identical files being deleted,
1501  // all of which will map to the same destination file
1502  'overwriteSame' => true // also see T33792
1503  ];
1504  }
1505 
1506  // Move the files by execute the operations for each pair.
1507  // We're now committed to returning an OK result, which will
1508  // lead to the files being moved in the DB also.
1509  $opts = [ 'force' => true ];
1510  $status->merge( $backend->doOperations( $operations, $opts ) );
1511 
1512  return $status;
1513  }
1514 
1521  public function cleanupDeletedBatch( array $storageKeys ) {
1522  $this->assertWritableRepo();
1523  }
1524 
1533  public function getDeletedHashPath( $key ) {
1534  if ( strlen( $key ) < 31 ) {
1535  throw new MWException( "Invalid storage key '$key'." );
1536  }
1537  $path = '';
1538  for ( $i = 0; $i < $this->deletedHashLevels; $i++ ) {
1539  $path .= $key[$i] . '/';
1540  }
1541 
1542  return $path;
1543  }
1544 
1553  protected function resolveToStoragePathIfVirtual( $path ) {
1554  if ( self::isVirtualUrl( $path ) ) {
1555  return $this->resolveVirtualUrl( $path );
1556  }
1557 
1558  return $path;
1559  }
1560 
1568  public function getLocalCopy( $virtualUrl ) {
1569  $path = $this->resolveToStoragePathIfVirtual( $virtualUrl );
1570 
1571  return $this->backend->getLocalCopy( [ 'src' => $path ] );
1572  }
1573 
1582  public function getLocalReference( $virtualUrl ) {
1583  $path = $this->resolveToStoragePathIfVirtual( $virtualUrl );
1584 
1585  return $this->backend->getLocalReference( [ 'src' => $path ] );
1586  }
1587 
1595  public function getFileProps( $virtualUrl ) {
1596  $fsFile = $this->getLocalReference( $virtualUrl );
1597  $mwProps = new MWFileProps( MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer() );
1598  if ( $fsFile ) {
1599  $props = $mwProps->getPropsFromPath( $fsFile->getPath(), true );
1600  } else {
1601  $props = $mwProps->newPlaceholderProps();
1602  }
1603 
1604  return $props;
1605  }
1606 
1613  public function getFileTimestamp( $virtualUrl ) {
1614  $path = $this->resolveToStoragePathIfVirtual( $virtualUrl );
1615 
1616  return $this->backend->getFileTimestamp( [ 'src' => $path ] );
1617  }
1618 
1625  public function getFileSize( $virtualUrl ) {
1626  $path = $this->resolveToStoragePathIfVirtual( $virtualUrl );
1627 
1628  return $this->backend->getFileSize( [ 'src' => $path ] );
1629  }
1630 
1637  public function getFileSha1( $virtualUrl ) {
1638  $path = $this->resolveToStoragePathIfVirtual( $virtualUrl );
1639 
1640  return $this->backend->getFileSha1Base36( [ 'src' => $path ] );
1641  }
1642 
1652  public function streamFileWithStatus( $virtualUrl, $headers = [], $optHeaders = [] ) {
1653  $path = $this->resolveToStoragePathIfVirtual( $virtualUrl );
1654  $params = [ 'src' => $path, 'headers' => $headers, 'options' => $optHeaders ];
1655 
1656  // T172851: HHVM does not flush the output properly, causing OOM
1657  ob_start( null, 1048576 );
1658  ob_implicit_flush( true );
1659 
1660  $status = $this->newGood();
1661  $status->merge( $this->backend->streamFile( $params ) );
1662 
1663  // T186565: Close the buffer, unless it has already been closed
1664  // in HTTPFileStreamer::resetOutputBuffers().
1665  if ( ob_get_status() ) {
1666  ob_end_flush();
1667  }
1668 
1669  return $status;
1670  }
1671 
1680  public function enumFiles( $callback ) {
1681  $this->enumFilesInStorage( $callback );
1682  }
1683 
1691  protected function enumFilesInStorage( $callback ) {
1692  $publicRoot = $this->getZonePath( 'public' );
1693  $numDirs = 1 << ( $this->hashLevels * 4 );
1694  // Use a priori assumptions about directory structure
1695  // to reduce the tree height of the scanning process.
1696  for ( $flatIndex = 0; $flatIndex < $numDirs; $flatIndex++ ) {
1697  $hexString = sprintf( "%0{$this->hashLevels}x", $flatIndex );
1698  $path = $publicRoot;
1699  for ( $hexPos = 0; $hexPos < $this->hashLevels; $hexPos++ ) {
1700  $path .= '/' . substr( $hexString, 0, $hexPos + 1 );
1701  }
1702  $iterator = $this->backend->getFileList( [ 'dir' => $path ] );
1703  if ( $iterator === null ) {
1704  throw new MWException( __METHOD__ . ': could not get file listing for ' . $path );
1705  }
1706  foreach ( $iterator as $name ) {
1707  // Each item returned is a public file
1708  call_user_func( $callback, "{$path}/{$name}" );
1709  }
1710  }
1711  }
1712 
1719  public function validateFilename( $filename ) {
1720  if ( strval( $filename ) == '' ) {
1721  return false;
1722  }
1723 
1724  return FileBackend::isPathTraversalFree( $filename );
1725  }
1726 
1732  private function getErrorCleanupFunction() {
1733  switch ( $this->pathDisclosureProtection ) {
1734  case 'none':
1735  case 'simple': // b/c
1736  $callback = [ $this, 'passThrough' ];
1737  break;
1738  default: // 'paranoid'
1739  $callback = [ $this, 'paranoidClean' ];
1740  }
1741  return $callback;
1742  }
1743 
1750  public function paranoidClean( $param ) {
1751  return '[hidden]';
1752  }
1753 
1760  public function passThrough( $param ) {
1761  return $param;
1762  }
1763 
1771  public function newFatal( $message, ...$parameters ) {
1772  $status = Status::newFatal( $message, ...$parameters );
1773  $status->cleanCallback = $this->getErrorCleanupFunction();
1774 
1775  return $status;
1776  }
1777 
1784  public function newGood( $value = null ) {
1785  $status = Status::newGood( $value );
1786  $status->cleanCallback = $this->getErrorCleanupFunction();
1787 
1788  return $status;
1789  }
1790 
1799  public function checkRedirect( Title $title ) {
1800  return false;
1801  }
1802 
1810  public function invalidateImageRedirect( Title $title ) {
1811  }
1812 
1818  public function getDisplayName() {
1819  global $wgSitename;
1820 
1821  if ( $this->isLocal() ) {
1822  return $wgSitename;
1823  }
1824 
1825  // 'shared-repo-name-wikimediacommons' is used when $wgUseInstantCommons = true
1826  return wfMessageFallback( 'shared-repo-name-' . $this->name, 'shared-repo' )->text();
1827  }
1828 
1836  public function nameForThumb( $name ) {
1837  if ( strlen( $name ) > $this->abbrvThreshold ) {
1839  $name = ( $ext == '' ) ? 'thumbnail' : "thumbnail.$ext";
1840  }
1841 
1842  return $name;
1843  }
1844 
1850  public function isLocal() {
1851  return $this->getName() == 'local';
1852  }
1853 
1863  public function getSharedCacheKey( ...$args ) {
1864  return false;
1865  }
1866 
1875  public function getLocalCacheKey( ...$args ) {
1876  array_unshift( $args, 'filerepo', $this->getName() );
1877 
1878  return $this->wanCache->makeKey( ...$args );
1879  }
1880 
1889  public function getTempRepo() {
1890  return new TempFileRepo( [
1891  'name' => "{$this->name}-temp",
1892  'backend' => $this->backend,
1893  'zones' => [
1894  'public' => [
1895  // Same place storeTemp() uses in the base repo, though
1896  // the path hashing is mismatched, which is annoying.
1897  'container' => $this->zones['temp']['container'],
1898  'directory' => $this->zones['temp']['directory']
1899  ],
1900  'thumb' => [
1901  'container' => $this->zones['temp']['container'],
1902  'directory' => $this->zones['temp']['directory'] == ''
1903  ? 'thumb'
1904  : $this->zones['temp']['directory'] . '/thumb'
1905  ],
1906  'transcoded' => [
1907  'container' => $this->zones['temp']['container'],
1908  'directory' => $this->zones['temp']['directory'] == ''
1909  ? 'transcoded'
1910  : $this->zones['temp']['directory'] . '/transcoded'
1911  ]
1912  ],
1913  'hashLevels' => $this->hashLevels, // performance
1914  'isPrivate' => true // all in temp zone
1915  ] );
1916  }
1917 
1924  public function getUploadStash( User $user = null ) {
1925  return new UploadStash( $this, $user );
1926  }
1927 
1935  protected function assertWritableRepo() {
1936  }
1937 
1944  public function getInfo() {
1945  $ret = [
1946  'name' => $this->getName(),
1947  'displayname' => $this->getDisplayName(),
1948  'rootUrl' => $this->getZoneUrl( 'public' ),
1949  'local' => $this->isLocal(),
1950  ];
1951 
1952  $optionalSettings = [
1953  'url', 'thumbUrl', 'initialCapital', 'descBaseUrl', 'scriptDirUrl', 'articleUrl',
1954  'fetchDescription', 'descriptionCacheExpiry', 'favicon'
1955  ];
1956  foreach ( $optionalSettings as $k ) {
1957  if ( isset( $this->$k ) ) {
1958  $ret[$k] = $this->$k;
1959  }
1960  }
1961 
1962  return $ret;
1963  }
1964 
1969  public function hasSha1Storage() {
1970  return $this->hasSha1Storage;
1971  }
1972 
1977  public function supportsSha1URLs() {
1978  return $this->supportsSha1URLs;
1979  }
1980 }
FileBackend\splitStoragePath
static splitStoragePath( $storagePath)
Split a storage path into a backend name, a container name, and a relative file path.
Definition: FileBackend.php:1540
FileBackend\doOperations
doOperations(array $ops, array $opts=[])
This is the main entry point into the backend for write operations.
Definition: FileBackend.php:468
FileRepo\getReadOnlyReason
getReadOnlyReason()
Get an explanatory message if this repo is read-only.
Definition: FileRepo.php:254
FileRepo\$supportsSha1URLs
bool $supportsSha1URLs
Definition: FileRepo.php:61
FileRepo\initZones
initZones( $doZones=[])
Check if a single zone or list of zones is defined for usage.
Definition: FileRepo.php:265
wfMessageFallback
wfMessageFallback(... $keys)
This function accepts multiple message keys and returns a message instance for the first message whic...
Definition: GlobalFunctions.php:1242
FileRepo\$hasSha1Storage
bool $hasSha1Storage
Definition: FileRepo.php:58
FileRepo\passThrough
passThrough( $param)
Path disclosure protection function.
Definition: FileRepo.php:1760
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:599
FileRepo\getThumbScriptUrl
getThumbScriptUrl()
Get the URL of thumb.php.
Definition: FileRepo.php:639
FileRepo\newGood
newGood( $value=null)
Create a new good result.
Definition: FileRepo.php:1784
FileRepo\getDescriptionRenderUrl
getDescriptionRenderUrl( $name, $lang=null)
Get the URL of the content-only fragment of the description page.
Definition: FileRepo.php:823
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:114
FileRepo\enumFilesInStorage
enumFilesInStorage( $callback)
Call a callback function for every public file in the repository.
Definition: FileRepo.php:1691
FileRepo\$favicon
string $favicon
The URL of the repo's favicon, if any.
Definition: FileRepo.php:123
FileRepo\OVERWRITE_SAME
const OVERWRITE_SAME
Definition: FileRepo.php:44
FileRepo\$isPrivate
bool $isPrivate
Whether all zones should be private (e.g.
Definition: FileRepo.php:126
FileRepo\validateFilename
validateFilename( $filename)
Determine if a relative path is valid, i.e.
Definition: FileRepo.php:1719
FileRepo\getRootDirectory
getRootDirectory()
Get the public zone root storage directory of the repository.
Definition: FileRepo.php:697
FileRepo\$fileFactoryKey
callable false $fileFactoryKey
Override these in the base class.
Definition: FileRepo.php:133
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:154
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:37
FileRepo\OVERWRITE
const OVERWRITE
Definition: FileRepo.php:43
FileRepo\getVirtualUrl
getVirtualUrl( $suffix=false)
Get a URL referring to this repository, with the private mwrepo protocol.
Definition: FileRepo.php:295
FileRepo\makeUrl
makeUrl( $query='', $entry='index')
Make an url to this repo.
Definition: FileRepo.php:769
FileRepo\$oldFileFactory
callable false $oldFileFactory
Override these in the base class.
Definition: FileRepo.php:131
FileRepo\storeBatch
storeBatch(array $triplets, $flags=0)
Store a batch of files.
Definition: FileRepo.php:900
FileRepo\$pathDisclosureProtection
string $pathDisclosureProtection
May be 'paranoid' to remove all parameters from error messages, 'none' to leave the paths in unchange...
Definition: FileRepo.php:102
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:1652
FileRepo\getTempRepo
getTempRepo()
Get a temporary private FileRepo associated with this repo.
Definition: FileRepo.php:1889
FileRepo\resolveToStoragePathIfVirtual
resolveToStoragePathIfVirtual( $path)
If a path is a virtual URL, resolve it to a storage path.
Definition: FileRepo.php:1553
FileRepo\getInfo
getInfo()
Return information about the repository.
Definition: FileRepo.php:1944
UploadStash
UploadStash is intended to accomplish a few things:
Definition: UploadStash.php:53
FileRepo\$descriptionCacheExpiry
int $descriptionCacheExpiry
Definition: FileRepo.php:55
FileRepo\getName
getName()
Get the name of this repository, as specified by $info['name]' to the constructor.
Definition: FileRepo.php:758
FileRepo\$thumbUrl
string $thumbUrl
The base thumbnail URL.
Definition: FileRepo.php:108
wfUrlencode
wfUrlencode( $s)
We want some things to be included as literal characters in our title URLs for prettiness,...
Definition: GlobalFunctions.php:308
FileRepo\quickImportBatch
quickImportBatch(array $triples)
Import a batch of files from the local file system into the repo.
Definition: FileRepo.php:1032
FileBackend\extensionFromPath
static extensionFromPath( $path, $case='lowercase')
Get the final extension from a storage or FS path.
Definition: FileBackend.php:1602
NS_FILE
const NS_FILE
Definition: Defines.php:75
$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:730
FileRepo\getZoneUrl
getZoneUrl( $zone, $ext=null)
Get the URL corresponding to one of the four basic zones.
Definition: FileRepo.php:311
FileRepo\$oldFileFactoryKey
callable false $oldFileFactoryKey
Override these in the base class.
Definition: FileRepo.php:135
FileRepo\getZonePath
getZonePath( $zone)
Get the storage path corresponding to one of the zones.
Definition: FileRepo.php:392
FileRepo\getLocalReference
getLocalReference( $virtualUrl)
Get a local FS file with a given virtual URL/storage path.
Definition: FileRepo.php:1582
FileRepo\getZoneLocation
getZoneLocation( $zone)
The storage container and base path of a zone.
Definition: FileRepo.php:378
FileRepo\$backend
FileBackend $backend
Definition: FileRepo.php:64
$base
$base
Definition: generateLocalAutoload.php:11
FileRepo\$wanCache
WANObjectCache $wanCache
Definition: FileRepo.php:145
FileRepo\$initialCapital
bool $initialCapital
Equivalent to $wgCapitalLinks (or $wgCapitalLinkOverrides[NS_FILE], determines whether filenames impl...
Definition: FileRepo.php:95
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:199
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:1224
FileRepo\getDescriptionStylesheetUrl
getDescriptionStylesheetUrl()
Get the URL of the stylesheet to apply to description pages.
Definition: FileRepo.php:848
FileRepo\fileExistsBatch
fileExistsBatch(array $files)
Checks existence of an array of files.
Definition: FileRepo.php:1416
FileRepo
Base class for file repositories.
Definition: FileRepo.php:41
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:438
WANObjectCache\newEmpty
static newEmpty()
Get an instance that wraps EmptyBagOStuff.
Definition: WANObjectCache.php:348
FileRepo\freeTemp
freeTemp( $virtualUrl)
Remove a temporary file or mark it for garbage collection.
Definition: FileRepo.php:1149
FileRepo\findFiles
findFiles(array $items, $flags=0)
Find many files at once.
Definition: FileRepo.php:525
FileRepo\quickPurge
quickPurge( $path)
Purge a file from the repo.
Definition: FileRepo.php:1077
FileRepo\hasSha1Storage
hasSha1Storage()
Returns whether or not storage is SHA-1 based.
Definition: FileRepo.php:1969
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:562
FileRepo\newFile
newFile( $title, $time=false)
Create a new File object from the local repository.
Definition: FileRepo.php:416
MWException
MediaWiki exception.
Definition: MWException.php:29
FileRepo\getLocalCacheKey
getLocalCacheKey(... $args)
Get a key for this repo in the local cache domain.
Definition: FileRepo.php:1875
FileRepo\enumFiles
enumFiles( $callback)
Call a callback function for every public regular file in the repository.
Definition: FileRepo.php:1680
FileRepo\checkRedirect
checkRedirect(Title $title)
Checks if there is a redirect named as $title.
Definition: FileRepo.php:1799
FileRepo\quickImport
quickImport( $src, $dst, $options=null)
Import a file from the local file system into the repo.
Definition: FileRepo.php:1014
FileBackend\isStoragePath
static isStoragePath( $path)
Check if a given path is a "mwstore://" path.
Definition: FileBackend.php:1528
FileRepo\getUploadStash
getUploadStash(User $user=null)
Get an UploadStash associated with this repo.
Definition: FileRepo.php:1924
FileRepo\$thumbScriptUrl
string $thumbScriptUrl
URL of thumb.php.
Definition: FileRepo.php:70
FileRepo\findFilesByPrefix
findFilesByPrefix( $prefix, $limit)
Return an array of files where the name starts with $prefix.
Definition: FileRepo.php:630
FileRepo\$zones
array $zones
Map of zones to config.
Definition: FileRepo.php:67
FileRepo\paranoidClean
paranoidClean( $param)
Path disclosure protection function.
Definition: FileRepo.php:1750
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:789
FileRepo\getThumbProxySecret
getThumbProxySecret()
Get the secret key for the proxied thumb service.
Definition: FileRepo.php:657
$args
if( $line===false) $args
Definition: mcc.php:124
FileRepo\store
store( $srcPath, $dstZone, $dstRel, $flags=0)
Store a file to a given destination.
Definition: FileRepo.php:875
MWTimestamp\getInstance
static getInstance( $ts=false)
Get a timestamp instance in GMT.
Definition: MWTimestamp.php:41
$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:1836
FileRepo\invalidateImageRedirect
invalidateImageRedirect(Title $title)
Invalidates image redirect cache related to that image Doesn't do anything for repositories that don'...
Definition: FileRepo.php:1810
FileRepo\getThumbProxyUrl
getThumbProxyUrl()
Get the URL thumb.php requests are being proxied to.
Definition: FileRepo.php:648
FileRepo\getLocalCopy
getLocalCopy( $virtualUrl)
Get a local FS copy of a file with a given virtual URL/storage path.
Definition: FileRepo.php:1568
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:970
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:909
FileRepo\fileExists
fileExists( $file)
Checks existence of a file.
Definition: FileRepo.php:1404
FileRepo\getFileSha1
getFileSha1( $virtualUrl)
Get the sha1 (base 36) of a file with a given virtual URL/storage path.
Definition: FileRepo.php:1637
FileRepo\backendSupportsUnicodePaths
backendSupportsUnicodePaths()
Definition: FileRepo.php:340
FileRepo\newFatal
newFatal( $message,... $parameters)
Create a new fatal error.
Definition: FileRepo.php:1771
FileRepo\getNameFromTitle
getNameFromTitle(Title $title)
Get the name of a file from its title object.
Definition: FileRepo.php:676
FileRepo\getDisplayName
getDisplayName()
Get the human-readable name of the repo.
Definition: FileRepo.php:1818
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:449
FileRepo\SKIP_LOCKING
const SKIP_LOCKING
Definition: FileRepo.php:45
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:1129
FileRepo\publishBatch
publishBatch(array $ntuples, $flags=0)
Publish a batch of files.
Definition: FileRepo.php:1251
WANObjectCache
Multi-datacenter aware caching interface.
Definition: WANObjectCache.php:125
$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:85
FileRepo\$articleUrl
string $articleUrl
Equivalent to $wgArticlePath, e.g.
Definition: FileRepo.php:88
FileRepo\getHashLevels
getHashLevels()
Get the number of hash directory levels.
Definition: FileRepo.php:749
FileRepo\initDirectory
initDirectory( $dir)
Creates a directory with the appropriate zone permissions.
Definition: FileRepo.php:1362
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:244
FileRepo\$fileFactory
callable $fileFactory
Override these in the base class.
Definition: FileRepo.php:129
FileRepo\quickPurgeBatch
quickPurgeBatch(array $paths)
Purge a batch of files from the repo.
Definition: FileRepo.php:1104
FileRepo\getFileSize
getFileSize( $virtualUrl)
Get the size of a file with a given virtual URL/storage path.
Definition: FileRepo.php:1625
FileRepo\isLocal
isLocal()
Returns true if this the local file repository.
Definition: FileRepo.php:1850
FileRepo\$thumbProxySecret
string $thumbProxySecret
Secret key to pass as an X-Swift-Secret header to the proxied thumb service.
Definition: FileRepo.php:142
FileRepo\getHashPath
getHashPath( $name)
Get a relative path including trailing slash, e.g.
Definition: FileRepo.php:708
FileRepo\$name
string $name
Definition: FileRepo.php:152
FileRepo\getErrorCleanupFunction
getErrorCleanupFunction()
Get a callback function to use for cleaning error message parameters.
Definition: FileRepo.php:1732
Title
Represents a title within MediaWiki.
Definition: Title.php:42
FileRepo\__construct
__construct(array $info=null)
Definition: FileRepo.php:160
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:610
FileRepo\getSharedCacheKey
getSharedCacheKey(... $args)
Get a key on the primary cache for this repository.
Definition: FileRepo.php:1863
FileRepo\getTempHashPath
getTempHashPath( $suffix)
Get a relative path including trailing slash, e.g.
Definition: FileRepo.php:719
FileRepo\$descBaseUrl
string $descBaseUrl
URL of image description pages, e.g.
Definition: FileRepo.php:80
$path
$path
Definition: NoLocalSettings.php:25
FileRepo\quickCleanDir
quickCleanDir( $dir)
Deletes a directory if empty.
Definition: FileRepo.php:1088
FileRepo\isVirtualUrl
static isVirtualUrl( $url)
Determine if a string is an mwrepo:// URL.
Definition: FileRepo.php:283
FileRepo\getFileTimestamp
getFileTimestamp( $virtualUrl)
Get the timestamp of a file with a given virtual URL/storage path.
Definition: FileRepo.php:1613
FileRepo\concatenate
concatenate(array $srcPaths, $dstPath, $flags=0)
Concatenate a list of temporary files into a target file location.
Definition: FileRepo.php:1171
FileRepo\deleteBatch
deleteBatch(array $sourceDestPairs)
Move a group of files to the deletion archive.
Definition: FileRepo.php:1462
FileRepo\resolveVirtualUrl
resolveVirtualUrl( $url)
Get the backend storage path corresponding to a virtual URL.
Definition: FileRepo.php:352
$source
$source
Definition: mwdoc-filter.php:34
FileBackend\isPathTraversalFree
static isPathTraversalFree( $path)
Check if a relative path has no directory traversals.
Definition: FileBackend.php:1624
FileRepo\cleanDir
cleanDir( $dir)
Deletes a directory if empty.
Definition: FileRepo.php:1388
$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:42
File\DELETED_FILE
const DELETED_FILE
Definition: File.php:67
FileRepo\getFileProps
getFileProps( $virtualUrl)
Get properties of a file with a given virtual URL/storage path.
Definition: FileRepo.php:1595
FileRepo\$abbrvThreshold
int $abbrvThreshold
File names over this size will use the short form of thumbnail names.
Definition: FileRepo.php:120
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:55
FileRepo\$transformVia404
bool $transformVia404
Whether to skip media file transformation on parse and rely on a 404 handler instead.
Definition: FileRepo.php:75
FileRepo\supportsSha1URLs
supportsSha1URLs()
Returns whether or not repo supports having originals SHA-1s in the thumb URLs.
Definition: FileRepo.php:1977
FileRepo\$hashLevels
int $hashLevels
The number of directory levels for hash-based division of files.
Definition: FileRepo.php:111
FileRepo\getDeletedHashPath
getDeletedHashPath( $key)
Get a relative path for a deletion archive key, e.g.
Definition: FileRepo.php:1533
FileRepo\assertWritableRepo
assertWritableRepo()
Throw an exception if this repo is read-only by design.
Definition: FileRepo.php:1935
FileRepo\NAME_AND_TIME_ONLY
const NAME_AND_TIME_ONLY
Definition: FileRepo.php:47
FileRepo\cleanupDeletedBatch
cleanupDeletedBatch(array $storageKeys)
Delete files in the deleted directory if they are not referenced in the filearchive table.
Definition: FileRepo.php:1521
FileRepo\$url
string false $url
Public zone URL.
Definition: FileRepo.php:105
FileRepo\canTransformVia404
canTransformVia404()
Returns true if the repository can transform files via a 404 handler.
Definition: FileRepo.php:666
FileRepo\$fetchDescription
bool $fetchDescription
Whether to fetch commons image description pages and display them on the local wiki.
Definition: FileRepo.php:52
FileRepo\$thumbProxyUrl
string $thumbProxyUrl
URL of where to proxy thumb.php requests to.
Definition: FileRepo.php:140