MediaWiki  1.28.3
LocalFile.php
Go to the documentation of this file.
1 <?php
25 
43 class LocalFile extends File {
44  const VERSION = 10; // cache version
45 
46  const CACHE_FIELD_MAX_LEN = 1000;
47 
49  protected $fileExists;
50 
52  protected $width;
53 
55  protected $height;
56 
58  protected $bits;
59 
61  protected $media_type;
62 
64  protected $mime;
65 
67  protected $size;
68 
70  protected $metadata;
71 
73  protected $sha1;
74 
76  protected $dataLoaded;
77 
79  protected $extraDataLoaded;
80 
82  protected $deleted;
83 
85  protected $repoClass = 'LocalRepo';
86 
88  private $historyLine;
89 
91  private $historyRes;
92 
94  private $major_mime;
95 
97  private $minor_mime;
98 
100  private $timestamp;
101 
103  private $user;
104 
106  private $user_text;
107 
109  private $description;
110 
113 
115  private $upgraded;
116 
118  private $upgrading;
119 
121  private $locked;
122 
124  private $lockedOwnTrx;
125 
127  private $missing;
128 
129  // @note: higher than IDBAccessObject constants
130  const LOAD_ALL = 16; // integer; load all the lazy fields too (like metadata)
131 
132  const ATOMIC_SECTION_LOCK = 'LocalFile::lockingTransaction';
133 
146  static function newFromTitle( $title, $repo, $unused = null ) {
147  return new self( $title, $repo );
148  }
149 
159  static function newFromRow( $row, $repo ) {
160  $title = Title::makeTitle( NS_FILE, $row->img_name );
161  $file = new self( $title, $repo );
162  $file->loadFromRow( $row );
163 
164  return $file;
165  }
166 
176  static function newFromKey( $sha1, $repo, $timestamp = false ) {
177  $dbr = $repo->getSlaveDB();
178 
179  $conds = [ 'img_sha1' => $sha1 ];
180  if ( $timestamp ) {
181  $conds['img_timestamp'] = $dbr->timestamp( $timestamp );
182  }
183 
184  $row = $dbr->selectRow( 'image', self::selectFields(), $conds, __METHOD__ );
185  if ( $row ) {
186  return self::newFromRow( $row, $repo );
187  } else {
188  return false;
189  }
190  }
191 
196  static function selectFields() {
197  return [
198  'img_name',
199  'img_size',
200  'img_width',
201  'img_height',
202  'img_metadata',
203  'img_bits',
204  'img_media_type',
205  'img_major_mime',
206  'img_minor_mime',
207  'img_description',
208  'img_user',
209  'img_user_text',
210  'img_timestamp',
211  'img_sha1',
212  ];
213  }
214 
221  function __construct( $title, $repo ) {
222  parent::__construct( $title, $repo );
223 
224  $this->metadata = '';
225  $this->historyLine = 0;
226  $this->historyRes = null;
227  $this->dataLoaded = false;
228  $this->extraDataLoaded = false;
229 
230  $this->assertRepoDefined();
231  $this->assertTitleDefined();
232  }
233 
239  function getCacheKey() {
240  return $this->repo->getSharedCacheKey( 'file', sha1( $this->getName() ) );
241  }
242 
246  private function loadFromCache() {
247  $this->dataLoaded = false;
248  $this->extraDataLoaded = false;
249 
250  $key = $this->getCacheKey();
251  if ( !$key ) {
252  $this->loadFromDB( self::READ_NORMAL );
253 
254  return;
255  }
256 
258  $cachedValues = $cache->getWithSetCallback(
259  $key,
260  $cache::TTL_WEEK,
261  function ( $oldValue, &$ttl, array &$setOpts ) use ( $cache ) {
262  $setOpts += Database::getCacheSetOptions( $this->repo->getSlaveDB() );
263 
264  $this->loadFromDB( self::READ_NORMAL );
265 
266  $fields = $this->getCacheFields( '' );
267  $cacheVal['fileExists'] = $this->fileExists;
268  if ( $this->fileExists ) {
269  foreach ( $fields as $field ) {
270  $cacheVal[$field] = $this->$field;
271  }
272  }
273  // Strip off excessive entries from the subset of fields that can become large.
274  // If the cache value gets to large it will not fit in memcached and nothing will
275  // get cached at all, causing master queries for any file access.
276  foreach ( $this->getLazyCacheFields( '' ) as $field ) {
277  if ( isset( $cacheVal[$field] )
278  && strlen( $cacheVal[$field] ) > 100 * 1024
279  ) {
280  unset( $cacheVal[$field] ); // don't let the value get too big
281  }
282  }
283 
284  if ( $this->fileExists ) {
285  $ttl = $cache->adaptiveTTL( wfTimestamp( TS_UNIX, $this->timestamp ), $ttl );
286  } else {
287  $ttl = $cache::TTL_DAY;
288  }
289 
290  return $cacheVal;
291  },
292  [ 'version' => self::VERSION ]
293  );
294 
295  $this->fileExists = $cachedValues['fileExists'];
296  if ( $this->fileExists ) {
297  $this->setProps( $cachedValues );
298  }
299 
300  $this->dataLoaded = true;
301  $this->extraDataLoaded = true;
302  foreach ( $this->getLazyCacheFields( '' ) as $field ) {
303  $this->extraDataLoaded = $this->extraDataLoaded && isset( $cachedValues[$field] );
304  }
305  }
306 
310  public function invalidateCache() {
311  $key = $this->getCacheKey();
312  if ( !$key ) {
313  return;
314  }
315 
316  $this->repo->getMasterDB()->onTransactionPreCommitOrIdle(
317  function () use ( $key ) {
318  ObjectCache::getMainWANInstance()->delete( $key );
319  },
320  __METHOD__
321  );
322  }
323 
327  function loadFromFile() {
328  $props = $this->repo->getFileProps( $this->getVirtualUrl() );
329  $this->setProps( $props );
330  }
331 
336  function getCacheFields( $prefix = 'img_' ) {
337  static $fields = [ 'size', 'width', 'height', 'bits', 'media_type',
338  'major_mime', 'minor_mime', 'metadata', 'timestamp', 'sha1', 'user',
339  'user_text', 'description' ];
340  static $results = [];
341 
342  if ( $prefix == '' ) {
343  return $fields;
344  }
345 
346  if ( !isset( $results[$prefix] ) ) {
347  $prefixedFields = [];
348  foreach ( $fields as $field ) {
349  $prefixedFields[] = $prefix . $field;
350  }
351  $results[$prefix] = $prefixedFields;
352  }
353 
354  return $results[$prefix];
355  }
356 
361  function getLazyCacheFields( $prefix = 'img_' ) {
362  static $fields = [ 'metadata' ];
363  static $results = [];
364 
365  if ( $prefix == '' ) {
366  return $fields;
367  }
368 
369  if ( !isset( $results[$prefix] ) ) {
370  $prefixedFields = [];
371  foreach ( $fields as $field ) {
372  $prefixedFields[] = $prefix . $field;
373  }
374  $results[$prefix] = $prefixedFields;
375  }
376 
377  return $results[$prefix];
378  }
379 
384  function loadFromDB( $flags = 0 ) {
385  $fname = get_class( $this ) . '::' . __FUNCTION__;
386 
387  # Unconditionally set loaded=true, we don't want the accessors constantly rechecking
388  $this->dataLoaded = true;
389  $this->extraDataLoaded = true;
390 
391  $dbr = ( $flags & self::READ_LATEST )
392  ? $this->repo->getMasterDB()
393  : $this->repo->getSlaveDB();
394 
395  $row = $dbr->selectRow( 'image', $this->getCacheFields( 'img_' ),
396  [ 'img_name' => $this->getName() ], $fname );
397 
398  if ( $row ) {
399  $this->loadFromRow( $row );
400  } else {
401  $this->fileExists = false;
402  }
403  }
404 
409  protected function loadExtraFromDB() {
410  $fname = get_class( $this ) . '::' . __FUNCTION__;
411 
412  # Unconditionally set loaded=true, we don't want the accessors constantly rechecking
413  $this->extraDataLoaded = true;
414 
415  $fieldMap = $this->loadFieldsWithTimestamp( $this->repo->getSlaveDB(), $fname );
416  if ( !$fieldMap ) {
417  $fieldMap = $this->loadFieldsWithTimestamp( $this->repo->getMasterDB(), $fname );
418  }
419 
420  if ( $fieldMap ) {
421  foreach ( $fieldMap as $name => $value ) {
422  $this->$name = $value;
423  }
424  } else {
425  throw new MWException( "Could not find data for image '{$this->getName()}'." );
426  }
427  }
428 
434  private function loadFieldsWithTimestamp( $dbr, $fname ) {
435  $fieldMap = false;
436 
437  $row = $dbr->selectRow( 'image', $this->getLazyCacheFields( 'img_' ), [
438  'img_name' => $this->getName(),
439  'img_timestamp' => $dbr->timestamp( $this->getTimestamp() )
440  ], $fname );
441  if ( $row ) {
442  $fieldMap = $this->unprefixRow( $row, 'img_' );
443  } else {
444  # File may have been uploaded over in the meantime; check the old versions
445  $row = $dbr->selectRow( 'oldimage', $this->getLazyCacheFields( 'oi_' ), [
446  'oi_name' => $this->getName(),
447  'oi_timestamp' => $dbr->timestamp( $this->getTimestamp() )
448  ], $fname );
449  if ( $row ) {
450  $fieldMap = $this->unprefixRow( $row, 'oi_' );
451  }
452  }
453 
454  return $fieldMap;
455  }
456 
463  protected function unprefixRow( $row, $prefix = 'img_' ) {
464  $array = (array)$row;
465  $prefixLength = strlen( $prefix );
466 
467  // Sanity check prefix once
468  if ( substr( key( $array ), 0, $prefixLength ) !== $prefix ) {
469  throw new MWException( __METHOD__ . ': incorrect $prefix parameter' );
470  }
471 
472  $decoded = [];
473  foreach ( $array as $name => $value ) {
474  $decoded[substr( $name, $prefixLength )] = $value;
475  }
476 
477  return $decoded;
478  }
479 
488  function decodeRow( $row, $prefix = 'img_' ) {
489  $decoded = $this->unprefixRow( $row, $prefix );
490 
491  $decoded['timestamp'] = wfTimestamp( TS_MW, $decoded['timestamp'] );
492 
493  $decoded['metadata'] = $this->repo->getSlaveDB()->decodeBlob( $decoded['metadata'] );
494 
495  if ( empty( $decoded['major_mime'] ) ) {
496  $decoded['mime'] = 'unknown/unknown';
497  } else {
498  if ( !$decoded['minor_mime'] ) {
499  $decoded['minor_mime'] = 'unknown';
500  }
501  $decoded['mime'] = $decoded['major_mime'] . '/' . $decoded['minor_mime'];
502  }
503 
504  // Trim zero padding from char/binary field
505  $decoded['sha1'] = rtrim( $decoded['sha1'], "\0" );
506 
507  // Normalize some fields to integer type, per their database definition.
508  // Use unary + so that overflows will be upgraded to double instead of
509  // being trucated as with intval(). This is important to allow >2GB
510  // files on 32-bit systems.
511  foreach ( [ 'size', 'width', 'height', 'bits' ] as $field ) {
512  $decoded[$field] = +$decoded[$field];
513  }
514 
515  return $decoded;
516  }
517 
524  function loadFromRow( $row, $prefix = 'img_' ) {
525  $this->dataLoaded = true;
526  $this->extraDataLoaded = true;
527 
528  $array = $this->decodeRow( $row, $prefix );
529 
530  foreach ( $array as $name => $value ) {
531  $this->$name = $value;
532  }
533 
534  $this->fileExists = true;
535  $this->maybeUpgradeRow();
536  }
537 
542  function load( $flags = 0 ) {
543  if ( !$this->dataLoaded ) {
544  if ( $flags & self::READ_LATEST ) {
545  $this->loadFromDB( $flags );
546  } else {
547  $this->loadFromCache();
548  }
549  }
550 
551  if ( ( $flags & self::LOAD_ALL ) && !$this->extraDataLoaded ) {
552  // @note: loads on name/timestamp to reduce race condition problems
553  $this->loadExtraFromDB();
554  }
555  }
556 
560  function maybeUpgradeRow() {
562 
563  if ( wfReadOnly() || $this->upgrading ) {
564  return;
565  }
566 
567  $upgrade = false;
568  if ( is_null( $this->media_type ) || $this->mime == 'image/svg' ) {
569  $upgrade = true;
570  } else {
571  $handler = $this->getHandler();
572  if ( $handler ) {
573  $validity = $handler->isMetadataValid( $this, $this->getMetadata() );
574  if ( $validity === MediaHandler::METADATA_BAD ) {
575  $upgrade = true;
576  } elseif ( $validity === MediaHandler::METADATA_COMPATIBLE ) {
577  $upgrade = $wgUpdateCompatibleMetadata;
578  }
579  }
580  }
581 
582  if ( $upgrade ) {
583  $this->upgrading = true;
584  // Defer updates unless in auto-commit CLI mode
586  $this->upgrading = false; // avoid duplicate updates
587  try {
588  $this->upgradeRow();
589  } catch ( LocalFileLockError $e ) {
590  // let the other process handle it (or do it next time)
591  }
592  } );
593  }
594  }
595 
599  function getUpgraded() {
600  return $this->upgraded;
601  }
602 
606  function upgradeRow() {
607  $this->lock(); // begin
608 
609  $this->loadFromFile();
610 
611  # Don't destroy file info of missing files
612  if ( !$this->fileExists ) {
613  $this->unlock();
614  wfDebug( __METHOD__ . ": file does not exist, aborting\n" );
615 
616  return;
617  }
618 
619  $dbw = $this->repo->getMasterDB();
620  list( $major, $minor ) = self::splitMime( $this->mime );
621 
622  if ( wfReadOnly() ) {
623  $this->unlock();
624 
625  return;
626  }
627  wfDebug( __METHOD__ . ': upgrading ' . $this->getName() . " to the current schema\n" );
628 
629  $dbw->update( 'image',
630  [
631  'img_size' => $this->size, // sanity
632  'img_width' => $this->width,
633  'img_height' => $this->height,
634  'img_bits' => $this->bits,
635  'img_media_type' => $this->media_type,
636  'img_major_mime' => $major,
637  'img_minor_mime' => $minor,
638  'img_metadata' => $dbw->encodeBlob( $this->metadata ),
639  'img_sha1' => $this->sha1,
640  ],
641  [ 'img_name' => $this->getName() ],
642  __METHOD__
643  );
644 
645  $this->invalidateCache();
646 
647  $this->unlock(); // done
648  $this->upgraded = true; // avoid rework/retries
649  }
650 
661  function setProps( $info ) {
662  $this->dataLoaded = true;
663  $fields = $this->getCacheFields( '' );
664  $fields[] = 'fileExists';
665 
666  foreach ( $fields as $field ) {
667  if ( isset( $info[$field] ) ) {
668  $this->$field = $info[$field];
669  }
670  }
671 
672  // Fix up mime fields
673  if ( isset( $info['major_mime'] ) ) {
674  $this->mime = "{$info['major_mime']}/{$info['minor_mime']}";
675  } elseif ( isset( $info['mime'] ) ) {
676  $this->mime = $info['mime'];
677  list( $this->major_mime, $this->minor_mime ) = self::splitMime( $this->mime );
678  }
679  }
680 
692  function isMissing() {
693  if ( $this->missing === null ) {
694  list( $fileExists ) = $this->repo->fileExists( $this->getVirtualUrl() );
695  $this->missing = !$fileExists;
696  }
697 
698  return $this->missing;
699  }
700 
707  public function getWidth( $page = 1 ) {
708  $this->load();
709 
710  if ( $this->isMultipage() ) {
711  $handler = $this->getHandler();
712  if ( !$handler ) {
713  return 0;
714  }
715  $dim = $handler->getPageDimensions( $this, $page );
716  if ( $dim ) {
717  return $dim['width'];
718  } else {
719  // For non-paged media, the false goes through an
720  // intval, turning failure into 0, so do same here.
721  return 0;
722  }
723  } else {
724  return $this->width;
725  }
726  }
727 
734  public function getHeight( $page = 1 ) {
735  $this->load();
736 
737  if ( $this->isMultipage() ) {
738  $handler = $this->getHandler();
739  if ( !$handler ) {
740  return 0;
741  }
742  $dim = $handler->getPageDimensions( $this, $page );
743  if ( $dim ) {
744  return $dim['height'];
745  } else {
746  // For non-paged media, the false goes through an
747  // intval, turning failure into 0, so do same here.
748  return 0;
749  }
750  } else {
751  return $this->height;
752  }
753  }
754 
761  function getUser( $type = 'text' ) {
762  $this->load();
763 
764  if ( $type == 'text' ) {
765  return $this->user_text;
766  } else { // id
767  return (int)$this->user;
768  }
769  }
770 
778  public function getDescriptionShortUrl() {
779  $pageId = $this->title->getArticleID();
780 
781  if ( $pageId !== null ) {
782  $url = $this->repo->makeUrl( [ 'curid' => $pageId ] );
783  if ( $url !== false ) {
784  return $url;
785  }
786  }
787  return null;
788  }
789 
794  function getMetadata() {
795  $this->load( self::LOAD_ALL ); // large metadata is loaded in another step
796  return $this->metadata;
797  }
798 
802  function getBitDepth() {
803  $this->load();
804 
805  return (int)$this->bits;
806  }
807 
812  public function getSize() {
813  $this->load();
814 
815  return $this->size;
816  }
817 
822  function getMimeType() {
823  $this->load();
824 
825  return $this->mime;
826  }
827 
833  function getMediaType() {
834  $this->load();
835 
836  return $this->media_type;
837  }
838 
849  public function exists() {
850  $this->load();
851 
852  return $this->fileExists;
853  }
854 
870  function getThumbnails( $archiveName = false ) {
871  if ( $archiveName ) {
872  $dir = $this->getArchiveThumbPath( $archiveName );
873  } else {
874  $dir = $this->getThumbPath();
875  }
876 
877  $backend = $this->repo->getBackend();
878  $files = [ $dir ];
879  try {
880  $iterator = $backend->getFileList( [ 'dir' => $dir ] );
881  foreach ( $iterator as $file ) {
882  $files[] = $file;
883  }
884  } catch ( FileBackendError $e ) {
885  } // suppress (bug 54674)
886 
887  return $files;
888  }
889 
893  function purgeMetadataCache() {
894  $this->invalidateCache();
895  }
896 
904  function purgeCache( $options = [] ) {
905  // Refresh metadata cache
906  $this->purgeMetadataCache();
907 
908  // Delete thumbnails
909  $this->purgeThumbnails( $options );
910 
911  // Purge CDN cache for this file
913  new CdnCacheUpdate( [ $this->getUrl() ] ),
915  );
916  }
917 
922  function purgeOldThumbnails( $archiveName ) {
923  // Get a list of old thumbnails and URLs
924  $files = $this->getThumbnails( $archiveName );
925 
926  // Purge any custom thumbnail caches
927  Hooks::run( 'LocalFilePurgeThumbnails', [ $this, $archiveName ] );
928 
929  // Delete thumbnails
930  $dir = array_shift( $files );
931  $this->purgeThumbList( $dir, $files );
932 
933  // Purge the CDN
934  $urls = [];
935  foreach ( $files as $file ) {
936  $urls[] = $this->getArchiveThumbUrl( $archiveName, $file );
937  }
939  }
940 
945  public function purgeThumbnails( $options = [] ) {
946  $files = $this->getThumbnails();
947  // Always purge all files from CDN regardless of handler filters
948  $urls = [];
949  foreach ( $files as $file ) {
950  $urls[] = $this->getThumbUrl( $file );
951  }
952  array_shift( $urls ); // don't purge directory
953 
954  // Give media handler a chance to filter the file purge list
955  if ( !empty( $options['forThumbRefresh'] ) ) {
956  $handler = $this->getHandler();
957  if ( $handler ) {
959  }
960  }
961 
962  // Purge any custom thumbnail caches
963  Hooks::run( 'LocalFilePurgeThumbnails', [ $this, false ] );
964 
965  // Delete thumbnails
966  $dir = array_shift( $files );
967  $this->purgeThumbList( $dir, $files );
968 
969  // Purge the CDN
971  }
972 
978  public function prerenderThumbnails() {
980 
981  $jobs = [];
982 
984  rsort( $sizes );
985 
986  foreach ( $sizes as $size ) {
987  if ( $this->isVectorized() || $this->getWidth() > $size ) {
988  $jobs[] = new ThumbnailRenderJob(
989  $this->getTitle(),
990  [ 'transformParams' => [ 'width' => $size ] ]
991  );
992  }
993  }
994 
995  if ( $jobs ) {
996  JobQueueGroup::singleton()->lazyPush( $jobs );
997  }
998  }
999 
1005  protected function purgeThumbList( $dir, $files ) {
1006  $fileListDebug = strtr(
1007  var_export( $files, true ),
1008  [ "\n" => '' ]
1009  );
1010  wfDebug( __METHOD__ . ": $fileListDebug\n" );
1011 
1012  $purgeList = [];
1013  foreach ( $files as $file ) {
1014  # Check that the base file name is part of the thumb name
1015  # This is a basic sanity check to avoid erasing unrelated directories
1016  if ( strpos( $file, $this->getName() ) !== false
1017  || strpos( $file, "-thumbnail" ) !== false // "short" thumb name
1018  ) {
1019  $purgeList[] = "{$dir}/{$file}";
1020  }
1021  }
1022 
1023  # Delete the thumbnails
1024  $this->repo->quickPurgeBatch( $purgeList );
1025  # Clear out the thumbnail directory if empty
1026  $this->repo->quickCleanDir( $dir );
1027  }
1028 
1039  function getHistory( $limit = null, $start = null, $end = null, $inc = true ) {
1040  $dbr = $this->repo->getSlaveDB();
1041  $tables = [ 'oldimage' ];
1042  $fields = OldLocalFile::selectFields();
1043  $conds = $opts = $join_conds = [];
1044  $eq = $inc ? '=' : '';
1045  $conds[] = "oi_name = " . $dbr->addQuotes( $this->title->getDBkey() );
1046 
1047  if ( $start ) {
1048  $conds[] = "oi_timestamp <$eq " . $dbr->addQuotes( $dbr->timestamp( $start ) );
1049  }
1050 
1051  if ( $end ) {
1052  $conds[] = "oi_timestamp >$eq " . $dbr->addQuotes( $dbr->timestamp( $end ) );
1053  }
1054 
1055  if ( $limit ) {
1056  $opts['LIMIT'] = $limit;
1057  }
1058 
1059  // Search backwards for time > x queries
1060  $order = ( !$start && $end !== null ) ? 'ASC' : 'DESC';
1061  $opts['ORDER BY'] = "oi_timestamp $order";
1062  $opts['USE INDEX'] = [ 'oldimage' => 'oi_name_timestamp' ];
1063 
1064  // Avoid PHP 7.1 warning from passing $this by reference
1065  $localFile = $this;
1066  Hooks::run( 'LocalFile::getHistory', [ &$localFile, &$tables, &$fields,
1067  &$conds, &$opts, &$join_conds ] );
1068 
1069  $res = $dbr->select( $tables, $fields, $conds, __METHOD__, $opts, $join_conds );
1070  $r = [];
1071 
1072  foreach ( $res as $row ) {
1073  $r[] = $this->repo->newFileFromRow( $row );
1074  }
1075 
1076  if ( $order == 'ASC' ) {
1077  $r = array_reverse( $r ); // make sure it ends up descending
1078  }
1079 
1080  return $r;
1081  }
1082 
1092  public function nextHistoryLine() {
1093  # Polymorphic function name to distinguish foreign and local fetches
1094  $fname = get_class( $this ) . '::' . __FUNCTION__;
1095 
1096  $dbr = $this->repo->getSlaveDB();
1097 
1098  if ( $this->historyLine == 0 ) { // called for the first time, return line from cur
1099  $this->historyRes = $dbr->select( 'image',
1100  [
1101  '*',
1102  "'' AS oi_archive_name",
1103  '0 as oi_deleted',
1104  'img_sha1'
1105  ],
1106  [ 'img_name' => $this->title->getDBkey() ],
1107  $fname
1108  );
1109 
1110  if ( 0 == $dbr->numRows( $this->historyRes ) ) {
1111  $this->historyRes = null;
1112 
1113  return false;
1114  }
1115  } elseif ( $this->historyLine == 1 ) {
1116  $this->historyRes = $dbr->select( 'oldimage', '*',
1117  [ 'oi_name' => $this->title->getDBkey() ],
1118  $fname,
1119  [ 'ORDER BY' => 'oi_timestamp DESC' ]
1120  );
1121  }
1122  $this->historyLine++;
1123 
1124  return $dbr->fetchObject( $this->historyRes );
1125  }
1126 
1130  public function resetHistory() {
1131  $this->historyLine = 0;
1132 
1133  if ( !is_null( $this->historyRes ) ) {
1134  $this->historyRes = null;
1135  }
1136  }
1137 
1168  function upload( $src, $comment, $pageText, $flags = 0, $props = false,
1169  $timestamp = false, $user = null, $tags = []
1170  ) {
1172 
1173  if ( $this->getRepo()->getReadOnlyReason() !== false ) {
1174  return $this->readOnlyFatalStatus();
1175  }
1176 
1177  $srcPath = ( $src instanceof FSFile ) ? $src->getPath() : $src;
1178  if ( !$props ) {
1179  if ( $this->repo->isVirtualUrl( $srcPath )
1180  || FileBackend::isStoragePath( $srcPath )
1181  ) {
1182  $props = $this->repo->getFileProps( $srcPath );
1183  } else {
1184  $mwProps = new MWFileProps( MimeMagic::singleton() );
1185  $props = $mwProps->getPropsFromPath( $srcPath, true );
1186  }
1187  }
1188 
1189  $options = [];
1190  $handler = MediaHandler::getHandler( $props['mime'] );
1191  if ( $handler ) {
1192  $options['headers'] = $handler->getStreamHeaders( $props['metadata'] );
1193  } else {
1194  $options['headers'] = [];
1195  }
1196 
1197  // Trim spaces on user supplied text
1198  $comment = trim( $comment );
1199 
1200  // Truncate nicely or the DB will do it for us
1201  // non-nicely (dangling multi-byte chars, non-truncated version in cache).
1202  $comment = $wgContLang->truncate( $comment, 255 );
1203  $this->lock(); // begin
1204  $status = $this->publish( $src, $flags, $options );
1205 
1206  if ( $status->successCount >= 2 ) {
1207  // There will be a copy+(one of move,copy,store).
1208  // The first succeeding does not commit us to updating the DB
1209  // since it simply copied the current version to a timestamped file name.
1210  // It is only *preferable* to avoid leaving such files orphaned.
1211  // Once the second operation goes through, then the current version was
1212  // updated and we must therefore update the DB too.
1213  $oldver = $status->value;
1214  if ( !$this->recordUpload2( $oldver, $comment, $pageText, $props, $timestamp, $user, $tags ) ) {
1215  $status->fatal( 'filenotfound', $srcPath );
1216  }
1217  }
1218 
1219  $this->unlock(); // done
1220 
1221  return $status;
1222  }
1223 
1236  function recordUpload( $oldver, $desc, $license = '', $copyStatus = '', $source = '',
1237  $watch = false, $timestamp = false, User $user = null ) {
1238  if ( !$user ) {
1239  global $wgUser;
1240  $user = $wgUser;
1241  }
1242 
1243  $pageText = SpecialUpload::getInitialPageText( $desc, $license, $copyStatus, $source );
1244 
1245  if ( !$this->recordUpload2( $oldver, $desc, $pageText, false, $timestamp, $user ) ) {
1246  return false;
1247  }
1248 
1249  if ( $watch ) {
1250  $user->addWatch( $this->getTitle() );
1251  }
1252 
1253  return true;
1254  }
1255 
1267  function recordUpload2(
1268  $oldver, $comment, $pageText, $props = false, $timestamp = false, $user = null, $tags = []
1269  ) {
1270  if ( is_null( $user ) ) {
1271  global $wgUser;
1272  $user = $wgUser;
1273  }
1274 
1275  $dbw = $this->repo->getMasterDB();
1276 
1277  # Imports or such might force a certain timestamp; otherwise we generate
1278  # it and can fudge it slightly to keep (name,timestamp) unique on re-upload.
1279  if ( $timestamp === false ) {
1280  $timestamp = $dbw->timestamp();
1281  $allowTimeKludge = true;
1282  } else {
1283  $allowTimeKludge = false;
1284  }
1285 
1286  $props = $props ?: $this->repo->getFileProps( $this->getVirtualUrl() );
1287  $props['description'] = $comment;
1288  $props['user'] = $user->getId();
1289  $props['user_text'] = $user->getName();
1290  $props['timestamp'] = wfTimestamp( TS_MW, $timestamp ); // DB -> TS_MW
1291  $this->setProps( $props );
1292 
1293  # Fail now if the file isn't there
1294  if ( !$this->fileExists ) {
1295  wfDebug( __METHOD__ . ": File " . $this->getRel() . " went missing!\n" );
1296 
1297  return false;
1298  }
1299 
1300  $dbw->startAtomic( __METHOD__ );
1301 
1302  # Test to see if the row exists using INSERT IGNORE
1303  # This avoids race conditions by locking the row until the commit, and also
1304  # doesn't deadlock. SELECT FOR UPDATE causes a deadlock for every race condition.
1305  $dbw->insert( 'image',
1306  [
1307  'img_name' => $this->getName(),
1308  'img_size' => $this->size,
1309  'img_width' => intval( $this->width ),
1310  'img_height' => intval( $this->height ),
1311  'img_bits' => $this->bits,
1312  'img_media_type' => $this->media_type,
1313  'img_major_mime' => $this->major_mime,
1314  'img_minor_mime' => $this->minor_mime,
1315  'img_timestamp' => $timestamp,
1316  'img_description' => $comment,
1317  'img_user' => $user->getId(),
1318  'img_user_text' => $user->getName(),
1319  'img_metadata' => $dbw->encodeBlob( $this->metadata ),
1320  'img_sha1' => $this->sha1
1321  ],
1322  __METHOD__,
1323  'IGNORE'
1324  );
1325 
1326  $reupload = ( $dbw->affectedRows() == 0 );
1327  if ( $reupload ) {
1328  if ( $allowTimeKludge ) {
1329  # Use LOCK IN SHARE MODE to ignore any transaction snapshotting
1330  $ltimestamp = $dbw->selectField(
1331  'image',
1332  'img_timestamp',
1333  [ 'img_name' => $this->getName() ],
1334  __METHOD__,
1335  [ 'LOCK IN SHARE MODE' ]
1336  );
1337  $lUnixtime = $ltimestamp ? wfTimestamp( TS_UNIX, $ltimestamp ) : false;
1338  # Avoid a timestamp that is not newer than the last version
1339  # TODO: the image/oldimage tables should be like page/revision with an ID field
1340  if ( $lUnixtime && wfTimestamp( TS_UNIX, $timestamp ) <= $lUnixtime ) {
1341  sleep( 1 ); // fast enough re-uploads would go far in the future otherwise
1342  $timestamp = $dbw->timestamp( $lUnixtime + 1 );
1343  $this->timestamp = wfTimestamp( TS_MW, $timestamp ); // DB -> TS_MW
1344  }
1345  }
1346 
1347  # (bug 34993) Note: $oldver can be empty here, if the previous
1348  # version of the file was broken. Allow registration of the new
1349  # version to continue anyway, because that's better than having
1350  # an image that's not fixable by user operations.
1351  # Collision, this is an update of a file
1352  # Insert previous contents into oldimage
1353  $dbw->insertSelect( 'oldimage', 'image',
1354  [
1355  'oi_name' => 'img_name',
1356  'oi_archive_name' => $dbw->addQuotes( $oldver ),
1357  'oi_size' => 'img_size',
1358  'oi_width' => 'img_width',
1359  'oi_height' => 'img_height',
1360  'oi_bits' => 'img_bits',
1361  'oi_timestamp' => 'img_timestamp',
1362  'oi_description' => 'img_description',
1363  'oi_user' => 'img_user',
1364  'oi_user_text' => 'img_user_text',
1365  'oi_metadata' => 'img_metadata',
1366  'oi_media_type' => 'img_media_type',
1367  'oi_major_mime' => 'img_major_mime',
1368  'oi_minor_mime' => 'img_minor_mime',
1369  'oi_sha1' => 'img_sha1'
1370  ],
1371  [ 'img_name' => $this->getName() ],
1372  __METHOD__
1373  );
1374 
1375  # Update the current image row
1376  $dbw->update( 'image',
1377  [
1378  'img_size' => $this->size,
1379  'img_width' => intval( $this->width ),
1380  'img_height' => intval( $this->height ),
1381  'img_bits' => $this->bits,
1382  'img_media_type' => $this->media_type,
1383  'img_major_mime' => $this->major_mime,
1384  'img_minor_mime' => $this->minor_mime,
1385  'img_timestamp' => $timestamp,
1386  'img_description' => $comment,
1387  'img_user' => $user->getId(),
1388  'img_user_text' => $user->getName(),
1389  'img_metadata' => $dbw->encodeBlob( $this->metadata ),
1390  'img_sha1' => $this->sha1
1391  ],
1392  [ 'img_name' => $this->getName() ],
1393  __METHOD__
1394  );
1395  }
1396 
1397  $descTitle = $this->getTitle();
1398  $descId = $descTitle->getArticleID();
1399  $wikiPage = new WikiFilePage( $descTitle );
1400  $wikiPage->setFile( $this );
1401 
1402  // Add the log entry...
1403  $logEntry = new ManualLogEntry( 'upload', $reupload ? 'overwrite' : 'upload' );
1404  $logEntry->setTimestamp( $this->timestamp );
1405  $logEntry->setPerformer( $user );
1406  $logEntry->setComment( $comment );
1407  $logEntry->setTarget( $descTitle );
1408  // Allow people using the api to associate log entries with the upload.
1409  // Log has a timestamp, but sometimes different from upload timestamp.
1410  $logEntry->setParameters(
1411  [
1412  'img_sha1' => $this->sha1,
1413  'img_timestamp' => $timestamp,
1414  ]
1415  );
1416  // Note we keep $logId around since during new image
1417  // creation, page doesn't exist yet, so log_page = 0
1418  // but we want it to point to the page we're making,
1419  // so we later modify the log entry.
1420  // For a similar reason, we avoid making an RC entry
1421  // now and wait until the page exists.
1422  $logId = $logEntry->insert();
1423 
1424  if ( $descTitle->exists() ) {
1425  // Use own context to get the action text in content language
1426  $formatter = LogFormatter::newFromEntry( $logEntry );
1427  $formatter->setContext( RequestContext::newExtraneousContext( $descTitle ) );
1428  $editSummary = $formatter->getPlainActionText();
1429 
1430  $nullRevision = Revision::newNullRevision(
1431  $dbw,
1432  $descId,
1433  $editSummary,
1434  false,
1435  $user
1436  );
1437  if ( $nullRevision ) {
1438  $nullRevision->insertOn( $dbw );
1439  Hooks::run(
1440  'NewRevisionFromEditComplete',
1441  [ $wikiPage, $nullRevision, $nullRevision->getParentId(), $user ]
1442  );
1443  $wikiPage->updateRevisionOn( $dbw, $nullRevision );
1444  // Associate null revision id
1445  $logEntry->setAssociatedRevId( $nullRevision->getId() );
1446  }
1447 
1448  $newPageContent = null;
1449  } else {
1450  // Make the description page and RC log entry post-commit
1451  $newPageContent = ContentHandler::makeContent( $pageText, $descTitle );
1452  }
1453 
1454  # Defer purges, page creation, and link updates in case they error out.
1455  # The most important thing is that files and the DB registry stay synced.
1456  $dbw->endAtomic( __METHOD__ );
1457 
1458  # Do some cache purges after final commit so that:
1459  # a) Changes are more likely to be seen post-purge
1460  # b) They won't cause rollback of the log publish/update above
1462  new AutoCommitUpdate(
1463  $dbw,
1464  __METHOD__,
1465  function () use (
1466  $reupload, $wikiPage, $newPageContent, $comment, $user,
1467  $logEntry, $logId, $descId, $tags
1468  ) {
1469  # Update memcache after the commit
1470  $this->invalidateCache();
1471 
1472  $updateLogPage = false;
1473  if ( $newPageContent ) {
1474  # New file page; create the description page.
1475  # There's already a log entry, so don't make a second RC entry
1476  # CDN and file cache for the description page are purged by doEditContent.
1477  $status = $wikiPage->doEditContent(
1478  $newPageContent,
1479  $comment,
1481  false,
1482  $user
1483  );
1484 
1485  if ( isset( $status->value['revision'] ) ) {
1487  $rev = $status->value['revision'];
1488  // Associate new page revision id
1489  $logEntry->setAssociatedRevId( $rev->getId() );
1490  }
1491  // This relies on the resetArticleID() call in WikiPage::insertOn(),
1492  // which is triggered on $descTitle by doEditContent() above.
1493  if ( isset( $status->value['revision'] ) ) {
1495  $rev = $status->value['revision'];
1496  $updateLogPage = $rev->getPage();
1497  }
1498  } else {
1499  # Existing file page: invalidate description page cache
1500  $wikiPage->getTitle()->invalidateCache();
1501  $wikiPage->getTitle()->purgeSquid();
1502  # Allow the new file version to be patrolled from the page footer
1504  }
1505 
1506  # Update associated rev id. This should be done by $logEntry->insert() earlier,
1507  # but setAssociatedRevId() wasn't called at that point yet...
1508  $logParams = $logEntry->getParameters();
1509  $logParams['associated_rev_id'] = $logEntry->getAssociatedRevId();
1510  $update = [ 'log_params' => LogEntryBase::makeParamBlob( $logParams ) ];
1511  if ( $updateLogPage ) {
1512  # Also log page, in case where we just created it above
1513  $update['log_page'] = $updateLogPage;
1514  }
1515  $this->getRepo()->getMasterDB()->update(
1516  'logging',
1517  $update,
1518  [ 'log_id' => $logId ],
1519  __METHOD__
1520  );
1521  $this->getRepo()->getMasterDB()->insert(
1522  'log_search',
1523  [
1524  'ls_field' => 'associated_rev_id',
1525  'ls_value' => $logEntry->getAssociatedRevId(),
1526  'ls_log_id' => $logId,
1527  ],
1528  __METHOD__
1529  );
1530 
1531  # Add change tags, if any
1532  if ( $tags ) {
1533  $logEntry->setTags( $tags );
1534  }
1535 
1536  # Uploads can be patrolled
1537  $logEntry->setIsPatrollable( true );
1538 
1539  # Now that the log entry is up-to-date, make an RC entry.
1540  $logEntry->publish( $logId );
1541 
1542  # Run hook for other updates (typically more cache purging)
1543  Hooks::run( 'FileUpload', [ $this, $reupload, !$newPageContent ] );
1544 
1545  if ( $reupload ) {
1546  # Delete old thumbnails
1547  $this->purgeThumbnails();
1548  # Remove the old file from the CDN cache
1550  new CdnCacheUpdate( [ $this->getUrl() ] ),
1552  );
1553  } else {
1554  # Update backlink pages pointing to this title if created
1555  LinksUpdate::queueRecursiveJobsForTable( $this->getTitle(), 'imagelinks' );
1556  }
1557 
1558  $this->prerenderThumbnails();
1559  }
1560  ),
1562  );
1563 
1564  if ( !$reupload ) {
1565  # This is a new file, so update the image count
1566  DeferredUpdates::addUpdate( SiteStatsUpdate::factory( [ 'images' => 1 ] ) );
1567  }
1568 
1569  # Invalidate cache for all pages using this file
1570  DeferredUpdates::addUpdate( new HTMLCacheUpdate( $this->getTitle(), 'imagelinks' ) );
1571 
1572  return true;
1573  }
1574 
1590  function publish( $src, $flags = 0, array $options = [] ) {
1591  return $this->publishTo( $src, $this->getRel(), $flags, $options );
1592  }
1593 
1609  function publishTo( $src, $dstRel, $flags = 0, array $options = [] ) {
1610  $srcPath = ( $src instanceof FSFile ) ? $src->getPath() : $src;
1611 
1612  $repo = $this->getRepo();
1613  if ( $repo->getReadOnlyReason() !== false ) {
1614  return $this->readOnlyFatalStatus();
1615  }
1616 
1617  $this->lock(); // begin
1618 
1619  $archiveName = wfTimestamp( TS_MW ) . '!' . $this->getName();
1620  $archiveRel = 'archive/' . $this->getHashPath() . $archiveName;
1621 
1622  if ( $repo->hasSha1Storage() ) {
1623  $sha1 = $repo->isVirtualUrl( $srcPath )
1624  ? $repo->getFileSha1( $srcPath )
1625  : FSFile::getSha1Base36FromPath( $srcPath );
1627  $wrapperBackend = $repo->getBackend();
1628  $dst = $wrapperBackend->getPathForSHA1( $sha1 );
1629  $status = $repo->quickImport( $src, $dst );
1630  if ( $flags & File::DELETE_SOURCE ) {
1631  unlink( $srcPath );
1632  }
1633 
1634  if ( $this->exists() ) {
1635  $status->value = $archiveName;
1636  }
1637  } else {
1639  $status = $repo->publish( $srcPath, $dstRel, $archiveRel, $flags, $options );
1640 
1641  if ( $status->value == 'new' ) {
1642  $status->value = '';
1643  } else {
1644  $status->value = $archiveName;
1645  }
1646  }
1647 
1648  $this->unlock(); // done
1649 
1650  return $status;
1651  }
1652 
1670  function move( $target ) {
1671  if ( $this->getRepo()->getReadOnlyReason() !== false ) {
1672  return $this->readOnlyFatalStatus();
1673  }
1674 
1675  wfDebugLog( 'imagemove', "Got request to move {$this->name} to " . $target->getText() );
1676  $batch = new LocalFileMoveBatch( $this, $target );
1677 
1678  $this->lock(); // begin
1679  $batch->addCurrent();
1680  $archiveNames = $batch->addOlds();
1681  $status = $batch->execute();
1682  $this->unlock(); // done
1683 
1684  wfDebugLog( 'imagemove', "Finished moving {$this->name}" );
1685 
1686  // Purge the source and target files...
1687  $oldTitleFile = wfLocalFile( $this->title );
1688  $newTitleFile = wfLocalFile( $target );
1689  // To avoid slow purges in the transaction, move them outside...
1691  new AutoCommitUpdate(
1692  $this->getRepo()->getMasterDB(),
1693  __METHOD__,
1694  function () use ( $oldTitleFile, $newTitleFile, $archiveNames ) {
1695  $oldTitleFile->purgeEverything();
1696  foreach ( $archiveNames as $archiveName ) {
1697  $oldTitleFile->purgeOldThumbnails( $archiveName );
1698  }
1699  $newTitleFile->purgeEverything();
1700  }
1701  ),
1703  );
1704 
1705  if ( $status->isOK() ) {
1706  // Now switch the object
1707  $this->title = $target;
1708  // Force regeneration of the name and hashpath
1709  unset( $this->name );
1710  unset( $this->hashPath );
1711  }
1712 
1713  return $status;
1714  }
1715 
1729  function delete( $reason, $suppress = false, $user = null ) {
1730  if ( $this->getRepo()->getReadOnlyReason() !== false ) {
1731  return $this->readOnlyFatalStatus();
1732  }
1733 
1734  $batch = new LocalFileDeleteBatch( $this, $reason, $suppress, $user );
1735 
1736  $this->lock(); // begin
1737  $batch->addCurrent();
1738  // Get old version relative paths
1739  $archiveNames = $batch->addOlds();
1740  $status = $batch->execute();
1741  $this->unlock(); // done
1742 
1743  if ( $status->isOK() ) {
1744  DeferredUpdates::addUpdate( SiteStatsUpdate::factory( [ 'images' => -1 ] ) );
1745  }
1746 
1747  // To avoid slow purges in the transaction, move them outside...
1749  new AutoCommitUpdate(
1750  $this->getRepo()->getMasterDB(),
1751  __METHOD__,
1752  function () use ( $archiveNames ) {
1753  $this->purgeEverything();
1754  foreach ( $archiveNames as $archiveName ) {
1755  $this->purgeOldThumbnails( $archiveName );
1756  }
1757  }
1758  ),
1760  );
1761 
1762  // Purge the CDN
1763  $purgeUrls = [];
1764  foreach ( $archiveNames as $archiveName ) {
1765  $purgeUrls[] = $this->getArchiveUrl( $archiveName );
1766  }
1768 
1769  return $status;
1770  }
1771 
1787  function deleteOld( $archiveName, $reason, $suppress = false, $user = null ) {
1788  if ( $this->getRepo()->getReadOnlyReason() !== false ) {
1789  return $this->readOnlyFatalStatus();
1790  }
1791 
1792  $batch = new LocalFileDeleteBatch( $this, $reason, $suppress, $user );
1793 
1794  $this->lock(); // begin
1795  $batch->addOld( $archiveName );
1796  $status = $batch->execute();
1797  $this->unlock(); // done
1798 
1799  $this->purgeOldThumbnails( $archiveName );
1800  if ( $status->isOK() ) {
1801  $this->purgeDescription();
1802  }
1803 
1805  new CdnCacheUpdate( [ $this->getArchiveUrl( $archiveName ) ] ),
1807  );
1808 
1809  return $status;
1810  }
1811 
1823  function restore( $versions = [], $unsuppress = false ) {
1824  if ( $this->getRepo()->getReadOnlyReason() !== false ) {
1825  return $this->readOnlyFatalStatus();
1826  }
1827 
1828  $batch = new LocalFileRestoreBatch( $this, $unsuppress );
1829 
1830  $this->lock(); // begin
1831  if ( !$versions ) {
1832  $batch->addAll();
1833  } else {
1834  $batch->addIds( $versions );
1835  }
1836  $status = $batch->execute();
1837  if ( $status->isGood() ) {
1838  $cleanupStatus = $batch->cleanup();
1839  $cleanupStatus->successCount = 0;
1840  $cleanupStatus->failCount = 0;
1841  $status->merge( $cleanupStatus );
1842  }
1843  $this->unlock(); // done
1844 
1845  return $status;
1846  }
1847 
1857  function getDescriptionUrl() {
1858  return $this->title->getLocalURL();
1859  }
1860 
1869  function getDescriptionText( $lang = null ) {
1870  $revision = Revision::newFromTitle( $this->title, false, Revision::READ_NORMAL );
1871  if ( !$revision ) {
1872  return false;
1873  }
1874  $content = $revision->getContent();
1875  if ( !$content ) {
1876  return false;
1877  }
1878  $pout = $content->getParserOutput( $this->title, null, new ParserOptions( null, $lang ) );
1879 
1880  return $pout->getText();
1881  }
1882 
1888  function getDescription( $audience = self::FOR_PUBLIC, User $user = null ) {
1889  $this->load();
1890  if ( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_COMMENT ) ) {
1891  return '';
1892  } elseif ( $audience == self::FOR_THIS_USER
1893  && !$this->userCan( self::DELETED_COMMENT, $user )
1894  ) {
1895  return '';
1896  } else {
1897  return $this->description;
1898  }
1899  }
1900 
1904  function getTimestamp() {
1905  $this->load();
1906 
1907  return $this->timestamp;
1908  }
1909 
1913  public function getDescriptionTouched() {
1914  // The DB lookup might return false, e.g. if the file was just deleted, or the shared DB repo
1915  // itself gets it from elsewhere. To avoid repeating the DB lookups in such a case, we
1916  // need to differentiate between null (uninitialized) and false (failed to load).
1917  if ( $this->descriptionTouched === null ) {
1918  $cond = [
1919  'page_namespace' => $this->title->getNamespace(),
1920  'page_title' => $this->title->getDBkey()
1921  ];
1922  $touched = $this->repo->getSlaveDB()->selectField( 'page', 'page_touched', $cond, __METHOD__ );
1923  $this->descriptionTouched = $touched ? wfTimestamp( TS_MW, $touched ) : false;
1924  }
1925 
1927  }
1928 
1932  function getSha1() {
1933  $this->load();
1934  // Initialise now if necessary
1935  if ( $this->sha1 == '' && $this->fileExists ) {
1936  $this->lock(); // begin
1937 
1938  $this->sha1 = $this->repo->getFileSha1( $this->getPath() );
1939  if ( !wfReadOnly() && strval( $this->sha1 ) != '' ) {
1940  $dbw = $this->repo->getMasterDB();
1941  $dbw->update( 'image',
1942  [ 'img_sha1' => $this->sha1 ],
1943  [ 'img_name' => $this->getName() ],
1944  __METHOD__ );
1945  $this->invalidateCache();
1946  }
1947 
1948  $this->unlock(); // done
1949  }
1950 
1951  return $this->sha1;
1952  }
1953 
1957  function isCacheable() {
1958  $this->load();
1959 
1960  // If extra data (metadata) was not loaded then it must have been large
1961  return $this->extraDataLoaded
1962  && strlen( serialize( $this->metadata ) ) <= self::CACHE_FIELD_MAX_LEN;
1963  }
1964 
1969  public function acquireFileLock() {
1970  return $this->getRepo()->getBackend()->lockFiles(
1971  [ $this->getPath() ], LockManager::LOCK_EX, 10
1972  );
1973  }
1974 
1979  public function releaseFileLock() {
1980  return $this->getRepo()->getBackend()->unlockFiles(
1981  [ $this->getPath() ], LockManager::LOCK_EX
1982  );
1983  }
1984 
1994  public function lock() {
1995  if ( !$this->locked ) {
1996  $logger = LoggerFactory::getInstance( 'LocalFile' );
1997 
1998  $dbw = $this->repo->getMasterDB();
1999  $makesTransaction = !$dbw->trxLevel();
2000  $dbw->startAtomic( self::ATOMIC_SECTION_LOCK );
2001  // Bug 54736: use simple lock to handle when the file does not exist.
2002  // SELECT FOR UPDATE prevents changes, not other SELECTs with FOR UPDATE.
2003  // Also, that would cause contention on INSERT of similarly named rows.
2004  $status = $this->acquireFileLock(); // represents all versions of the file
2005  if ( !$status->isGood() ) {
2006  $dbw->endAtomic( self::ATOMIC_SECTION_LOCK );
2007  $logger->warning( "Failed to lock '{file}'", [ 'file' => $this->name ] );
2008 
2009  throw new LocalFileLockError( $status );
2010  }
2011  // Release the lock *after* commit to avoid row-level contention.
2012  // Make sure it triggers on rollback() as well as commit() (T132921).
2013  $dbw->onTransactionResolution(
2014  function () use ( $logger ) {
2015  $status = $this->releaseFileLock();
2016  if ( !$status->isGood() ) {
2017  $logger->error( "Failed to unlock '{file}'", [ 'file' => $this->name ] );
2018  }
2019  },
2020  __METHOD__
2021  );
2022  // Callers might care if the SELECT snapshot is safely fresh
2023  $this->lockedOwnTrx = $makesTransaction;
2024  }
2025 
2026  $this->locked++;
2027 
2028  return $this->lockedOwnTrx;
2029  }
2030 
2039  public function unlock() {
2040  if ( $this->locked ) {
2041  --$this->locked;
2042  if ( !$this->locked ) {
2043  $dbw = $this->repo->getMasterDB();
2044  $dbw->endAtomic( self::ATOMIC_SECTION_LOCK );
2045  $this->lockedOwnTrx = false;
2046  }
2047  }
2048  }
2049 
2053  protected function readOnlyFatalStatus() {
2054  return $this->getRepo()->newFatal( 'filereadonlyerror', $this->getName(),
2055  $this->getRepo()->getName(), $this->getRepo()->getReadOnlyReason() );
2056  }
2057 
2061  function __destruct() {
2062  $this->unlock();
2063  }
2064 } // LocalFile class
2065 
2066 # ------------------------------------------------------------------------------
2067 
2074  private $file;
2075 
2077  private $reason;
2078 
2080  private $srcRels = [];
2081 
2083  private $archiveUrls = [];
2084 
2087 
2089  private $suppress;
2090 
2092  private $status;
2093 
2095  private $user;
2096 
2103  function __construct( File $file, $reason = '', $suppress = false, $user = null ) {
2104  $this->file = $file;
2105  $this->reason = $reason;
2106  $this->suppress = $suppress;
2107  if ( $user ) {
2108  $this->user = $user;
2109  } else {
2110  global $wgUser;
2111  $this->user = $wgUser;
2112  }
2113  $this->status = $file->repo->newGood();
2114  }
2115 
2116  public function addCurrent() {
2117  $this->srcRels['.'] = $this->file->getRel();
2118  }
2119 
2123  public function addOld( $oldName ) {
2124  $this->srcRels[$oldName] = $this->file->getArchiveRel( $oldName );
2125  $this->archiveUrls[] = $this->file->getArchiveUrl( $oldName );
2126  }
2127 
2132  public function addOlds() {
2133  $archiveNames = [];
2134 
2135  $dbw = $this->file->repo->getMasterDB();
2136  $result = $dbw->select( 'oldimage',
2137  [ 'oi_archive_name' ],
2138  [ 'oi_name' => $this->file->getName() ],
2139  __METHOD__
2140  );
2141 
2142  foreach ( $result as $row ) {
2143  $this->addOld( $row->oi_archive_name );
2144  $archiveNames[] = $row->oi_archive_name;
2145  }
2146 
2147  return $archiveNames;
2148  }
2149 
2153  protected function getOldRels() {
2154  if ( !isset( $this->srcRels['.'] ) ) {
2155  $oldRels =& $this->srcRels;
2156  $deleteCurrent = false;
2157  } else {
2158  $oldRels = $this->srcRels;
2159  unset( $oldRels['.'] );
2160  $deleteCurrent = true;
2161  }
2162 
2163  return [ $oldRels, $deleteCurrent ];
2164  }
2165 
2169  protected function getHashes() {
2170  $hashes = [];
2171  list( $oldRels, $deleteCurrent ) = $this->getOldRels();
2172 
2173  if ( $deleteCurrent ) {
2174  $hashes['.'] = $this->file->getSha1();
2175  }
2176 
2177  if ( count( $oldRels ) ) {
2178  $dbw = $this->file->repo->getMasterDB();
2179  $res = $dbw->select(
2180  'oldimage',
2181  [ 'oi_archive_name', 'oi_sha1' ],
2182  [ 'oi_archive_name' => array_keys( $oldRels ),
2183  'oi_name' => $this->file->getName() ], // performance
2184  __METHOD__
2185  );
2186 
2187  foreach ( $res as $row ) {
2188  if ( rtrim( $row->oi_sha1, "\0" ) === '' ) {
2189  // Get the hash from the file
2190  $oldUrl = $this->file->getArchiveVirtualUrl( $row->oi_archive_name );
2191  $props = $this->file->repo->getFileProps( $oldUrl );
2192 
2193  if ( $props['fileExists'] ) {
2194  // Upgrade the oldimage row
2195  $dbw->update( 'oldimage',
2196  [ 'oi_sha1' => $props['sha1'] ],
2197  [ 'oi_name' => $this->file->getName(), 'oi_archive_name' => $row->oi_archive_name ],
2198  __METHOD__ );
2199  $hashes[$row->oi_archive_name] = $props['sha1'];
2200  } else {
2201  $hashes[$row->oi_archive_name] = false;
2202  }
2203  } else {
2204  $hashes[$row->oi_archive_name] = $row->oi_sha1;
2205  }
2206  }
2207  }
2208 
2209  $missing = array_diff_key( $this->srcRels, $hashes );
2210 
2211  foreach ( $missing as $name => $rel ) {
2212  $this->status->error( 'filedelete-old-unregistered', $name );
2213  }
2214 
2215  foreach ( $hashes as $name => $hash ) {
2216  if ( !$hash ) {
2217  $this->status->error( 'filedelete-missing', $this->srcRels[$name] );
2218  unset( $hashes[$name] );
2219  }
2220  }
2221 
2222  return $hashes;
2223  }
2224 
2225  protected function doDBInserts() {
2226  $now = time();
2227  $dbw = $this->file->repo->getMasterDB();
2228  $encTimestamp = $dbw->addQuotes( $dbw->timestamp( $now ) );
2229  $encUserId = $dbw->addQuotes( $this->user->getId() );
2230  $encReason = $dbw->addQuotes( $this->reason );
2231  $encGroup = $dbw->addQuotes( 'deleted' );
2232  $ext = $this->file->getExtension();
2233  $dotExt = $ext === '' ? '' : ".$ext";
2234  $encExt = $dbw->addQuotes( $dotExt );
2235  list( $oldRels, $deleteCurrent ) = $this->getOldRels();
2236 
2237  // Bitfields to further suppress the content
2238  if ( $this->suppress ) {
2239  $bitfield = 0;
2240  // This should be 15...
2241  $bitfield |= Revision::DELETED_TEXT;
2242  $bitfield |= Revision::DELETED_COMMENT;
2243  $bitfield |= Revision::DELETED_USER;
2244  $bitfield |= Revision::DELETED_RESTRICTED;
2245  } else {
2246  $bitfield = 'oi_deleted';
2247  }
2248 
2249  if ( $deleteCurrent ) {
2250  $dbw->insertSelect(
2251  'filearchive',
2252  'image',
2253  [
2254  'fa_storage_group' => $encGroup,
2255  'fa_storage_key' => $dbw->conditional(
2256  [ 'img_sha1' => '' ],
2257  $dbw->addQuotes( '' ),
2258  $dbw->buildConcat( [ "img_sha1", $encExt ] )
2259  ),
2260  'fa_deleted_user' => $encUserId,
2261  'fa_deleted_timestamp' => $encTimestamp,
2262  'fa_deleted_reason' => $encReason,
2263  'fa_deleted' => $this->suppress ? $bitfield : 0,
2264 
2265  'fa_name' => 'img_name',
2266  'fa_archive_name' => 'NULL',
2267  'fa_size' => 'img_size',
2268  'fa_width' => 'img_width',
2269  'fa_height' => 'img_height',
2270  'fa_metadata' => 'img_metadata',
2271  'fa_bits' => 'img_bits',
2272  'fa_media_type' => 'img_media_type',
2273  'fa_major_mime' => 'img_major_mime',
2274  'fa_minor_mime' => 'img_minor_mime',
2275  'fa_description' => 'img_description',
2276  'fa_user' => 'img_user',
2277  'fa_user_text' => 'img_user_text',
2278  'fa_timestamp' => 'img_timestamp',
2279  'fa_sha1' => 'img_sha1'
2280  ],
2281  [ 'img_name' => $this->file->getName() ],
2282  __METHOD__
2283  );
2284  }
2285 
2286  if ( count( $oldRels ) ) {
2287  $res = $dbw->select(
2288  'oldimage',
2290  [
2291  'oi_name' => $this->file->getName(),
2292  'oi_archive_name' => array_keys( $oldRels )
2293  ],
2294  __METHOD__,
2295  [ 'FOR UPDATE' ]
2296  );
2297  $rowsInsert = [];
2298  foreach ( $res as $row ) {
2299  $rowsInsert[] = [
2300  // Deletion-specific fields
2301  'fa_storage_group' => 'deleted',
2302  'fa_storage_key' => ( $row->oi_sha1 === '' )
2303  ? ''
2304  : "{$row->oi_sha1}{$dotExt}",
2305  'fa_deleted_user' => $this->user->getId(),
2306  'fa_deleted_timestamp' => $dbw->timestamp( $now ),
2307  'fa_deleted_reason' => $this->reason,
2308  // Counterpart fields
2309  'fa_deleted' => $this->suppress ? $bitfield : $row->oi_deleted,
2310  'fa_name' => $row->oi_name,
2311  'fa_archive_name' => $row->oi_archive_name,
2312  'fa_size' => $row->oi_size,
2313  'fa_width' => $row->oi_width,
2314  'fa_height' => $row->oi_height,
2315  'fa_metadata' => $row->oi_metadata,
2316  'fa_bits' => $row->oi_bits,
2317  'fa_media_type' => $row->oi_media_type,
2318  'fa_major_mime' => $row->oi_major_mime,
2319  'fa_minor_mime' => $row->oi_minor_mime,
2320  'fa_description' => $row->oi_description,
2321  'fa_user' => $row->oi_user,
2322  'fa_user_text' => $row->oi_user_text,
2323  'fa_timestamp' => $row->oi_timestamp,
2324  'fa_sha1' => $row->oi_sha1
2325  ];
2326  }
2327 
2328  $dbw->insert( 'filearchive', $rowsInsert, __METHOD__ );
2329  }
2330  }
2331 
2332  function doDBDeletes() {
2333  $dbw = $this->file->repo->getMasterDB();
2334  list( $oldRels, $deleteCurrent ) = $this->getOldRels();
2335 
2336  if ( count( $oldRels ) ) {
2337  $dbw->delete( 'oldimage',
2338  [
2339  'oi_name' => $this->file->getName(),
2340  'oi_archive_name' => array_keys( $oldRels )
2341  ], __METHOD__ );
2342  }
2343 
2344  if ( $deleteCurrent ) {
2345  $dbw->delete( 'image', [ 'img_name' => $this->file->getName() ], __METHOD__ );
2346  }
2347  }
2348 
2353  public function execute() {
2354  $repo = $this->file->getRepo();
2355  $this->file->lock();
2356 
2357  // Prepare deletion batch
2358  $hashes = $this->getHashes();
2359  $this->deletionBatch = [];
2360  $ext = $this->file->getExtension();
2361  $dotExt = $ext === '' ? '' : ".$ext";
2362 
2363  foreach ( $this->srcRels as $name => $srcRel ) {
2364  // Skip files that have no hash (e.g. missing DB record, or sha1 field and file source)
2365  if ( isset( $hashes[$name] ) ) {
2366  $hash = $hashes[$name];
2367  $key = $hash . $dotExt;
2368  $dstRel = $repo->getDeletedHashPath( $key ) . $key;
2369  $this->deletionBatch[$name] = [ $srcRel, $dstRel ];
2370  }
2371  }
2372 
2373  if ( !$repo->hasSha1Storage() ) {
2374  // Removes non-existent file from the batch, so we don't get errors.
2375  // This also handles files in the 'deleted' zone deleted via revision deletion.
2376  $checkStatus = $this->removeNonexistentFiles( $this->deletionBatch );
2377  if ( !$checkStatus->isGood() ) {
2378  $this->status->merge( $checkStatus );
2379  return $this->status;
2380  }
2381  $this->deletionBatch = $checkStatus->value;
2382 
2383  // Execute the file deletion batch
2384  $status = $this->file->repo->deleteBatch( $this->deletionBatch );
2385  if ( !$status->isGood() ) {
2386  $this->status->merge( $status );
2387  }
2388  }
2389 
2390  if ( !$this->status->isOK() ) {
2391  // Critical file deletion error; abort
2392  $this->file->unlock();
2393 
2394  return $this->status;
2395  }
2396 
2397  // Copy the image/oldimage rows to filearchive
2398  $this->doDBInserts();
2399  // Delete image/oldimage rows
2400  $this->doDBDeletes();
2401 
2402  // Commit and return
2403  $this->file->unlock();
2404 
2405  return $this->status;
2406  }
2407 
2413  protected function removeNonexistentFiles( $batch ) {
2414  $files = $newBatch = [];
2415 
2416  foreach ( $batch as $batchItem ) {
2417  list( $src, ) = $batchItem;
2418  $files[$src] = $this->file->repo->getVirtualUrl( 'public' ) . '/' . rawurlencode( $src );
2419  }
2420 
2421  $result = $this->file->repo->fileExistsBatch( $files );
2422  if ( in_array( null, $result, true ) ) {
2423  return Status::newFatal( 'backend-fail-internal',
2424  $this->file->repo->getBackend()->getName() );
2425  }
2426 
2427  foreach ( $batch as $batchItem ) {
2428  if ( $result[$batchItem[0]] ) {
2429  $newBatch[] = $batchItem;
2430  }
2431  }
2432 
2433  return Status::newGood( $newBatch );
2434  }
2435 }
2436 
2437 # ------------------------------------------------------------------------------
2438 
2445  private $file;
2446 
2448  private $cleanupBatch;
2449 
2451  private $ids;
2452 
2454  private $all;
2455 
2457  private $unsuppress = false;
2458 
2463  function __construct( File $file, $unsuppress = false ) {
2464  $this->file = $file;
2465  $this->cleanupBatch = $this->ids = [];
2466  $this->ids = [];
2467  $this->unsuppress = $unsuppress;
2468  }
2469 
2474  public function addId( $fa_id ) {
2475  $this->ids[] = $fa_id;
2476  }
2477 
2482  public function addIds( $ids ) {
2483  $this->ids = array_merge( $this->ids, $ids );
2484  }
2485 
2489  public function addAll() {
2490  $this->all = true;
2491  }
2492 
2501  public function execute() {
2503  global $wgLang;
2504 
2505  $repo = $this->file->getRepo();
2506  if ( !$this->all && !$this->ids ) {
2507  // Do nothing
2508  return $repo->newGood();
2509  }
2510 
2511  $lockOwnsTrx = $this->file->lock();
2512 
2513  $dbw = $this->file->repo->getMasterDB();
2514  $status = $this->file->repo->newGood();
2515 
2516  $exists = (bool)$dbw->selectField( 'image', '1',
2517  [ 'img_name' => $this->file->getName() ],
2518  __METHOD__,
2519  // The lock() should already prevents changes, but this still may need
2520  // to bypass any transaction snapshot. However, if lock() started the
2521  // trx (which it probably did) then snapshot is post-lock and up-to-date.
2522  $lockOwnsTrx ? [] : [ 'LOCK IN SHARE MODE' ]
2523  );
2524 
2525  // Fetch all or selected archived revisions for the file,
2526  // sorted from the most recent to the oldest.
2527  $conditions = [ 'fa_name' => $this->file->getName() ];
2528 
2529  if ( !$this->all ) {
2530  $conditions['fa_id'] = $this->ids;
2531  }
2532 
2533  $result = $dbw->select(
2534  'filearchive',
2536  $conditions,
2537  __METHOD__,
2538  [ 'ORDER BY' => 'fa_timestamp DESC' ]
2539  );
2540 
2541  $idsPresent = [];
2542  $storeBatch = [];
2543  $insertBatch = [];
2544  $insertCurrent = false;
2545  $deleteIds = [];
2546  $first = true;
2547  $archiveNames = [];
2548 
2549  foreach ( $result as $row ) {
2550  $idsPresent[] = $row->fa_id;
2551 
2552  if ( $row->fa_name != $this->file->getName() ) {
2553  $status->error( 'undelete-filename-mismatch', $wgLang->timeanddate( $row->fa_timestamp ) );
2554  $status->failCount++;
2555  continue;
2556  }
2557 
2558  if ( $row->fa_storage_key == '' ) {
2559  // Revision was missing pre-deletion
2560  $status->error( 'undelete-bad-store-key', $wgLang->timeanddate( $row->fa_timestamp ) );
2561  $status->failCount++;
2562  continue;
2563  }
2564 
2565  $deletedRel = $repo->getDeletedHashPath( $row->fa_storage_key ) .
2566  $row->fa_storage_key;
2567  $deletedUrl = $repo->getVirtualUrl() . '/deleted/' . $deletedRel;
2568 
2569  if ( isset( $row->fa_sha1 ) ) {
2570  $sha1 = $row->fa_sha1;
2571  } else {
2572  // old row, populate from key
2573  $sha1 = LocalRepo::getHashFromKey( $row->fa_storage_key );
2574  }
2575 
2576  # Fix leading zero
2577  if ( strlen( $sha1 ) == 32 && $sha1[0] == '0' ) {
2578  $sha1 = substr( $sha1, 1 );
2579  }
2580 
2581  if ( is_null( $row->fa_major_mime ) || $row->fa_major_mime == 'unknown'
2582  || is_null( $row->fa_minor_mime ) || $row->fa_minor_mime == 'unknown'
2583  || is_null( $row->fa_media_type ) || $row->fa_media_type == 'UNKNOWN'
2584  || is_null( $row->fa_metadata )
2585  ) {
2586  // Refresh our metadata
2587  // Required for a new current revision; nice for older ones too. :)
2588  $props = RepoGroup::singleton()->getFileProps( $deletedUrl );
2589  } else {
2590  $props = [
2591  'minor_mime' => $row->fa_minor_mime,
2592  'major_mime' => $row->fa_major_mime,
2593  'media_type' => $row->fa_media_type,
2594  'metadata' => $row->fa_metadata
2595  ];
2596  }
2597 
2598  if ( $first && !$exists ) {
2599  // This revision will be published as the new current version
2600  $destRel = $this->file->getRel();
2601  $insertCurrent = [
2602  'img_name' => $row->fa_name,
2603  'img_size' => $row->fa_size,
2604  'img_width' => $row->fa_width,
2605  'img_height' => $row->fa_height,
2606  'img_metadata' => $props['metadata'],
2607  'img_bits' => $row->fa_bits,
2608  'img_media_type' => $props['media_type'],
2609  'img_major_mime' => $props['major_mime'],
2610  'img_minor_mime' => $props['minor_mime'],
2611  'img_description' => $row->fa_description,
2612  'img_user' => $row->fa_user,
2613  'img_user_text' => $row->fa_user_text,
2614  'img_timestamp' => $row->fa_timestamp,
2615  'img_sha1' => $sha1
2616  ];
2617 
2618  // The live (current) version cannot be hidden!
2619  if ( !$this->unsuppress && $row->fa_deleted ) {
2620  $status->fatal( 'undeleterevdel' );
2621  $this->file->unlock();
2622  return $status;
2623  }
2624  } else {
2625  $archiveName = $row->fa_archive_name;
2626 
2627  if ( $archiveName == '' ) {
2628  // This was originally a current version; we
2629  // have to devise a new archive name for it.
2630  // Format is <timestamp of archiving>!<name>
2631  $timestamp = wfTimestamp( TS_UNIX, $row->fa_deleted_timestamp );
2632 
2633  do {
2634  $archiveName = wfTimestamp( TS_MW, $timestamp ) . '!' . $row->fa_name;
2635  $timestamp++;
2636  } while ( isset( $archiveNames[$archiveName] ) );
2637  }
2638 
2639  $archiveNames[$archiveName] = true;
2640  $destRel = $this->file->getArchiveRel( $archiveName );
2641  $insertBatch[] = [
2642  'oi_name' => $row->fa_name,
2643  'oi_archive_name' => $archiveName,
2644  'oi_size' => $row->fa_size,
2645  'oi_width' => $row->fa_width,
2646  'oi_height' => $row->fa_height,
2647  'oi_bits' => $row->fa_bits,
2648  'oi_description' => $row->fa_description,
2649  'oi_user' => $row->fa_user,
2650  'oi_user_text' => $row->fa_user_text,
2651  'oi_timestamp' => $row->fa_timestamp,
2652  'oi_metadata' => $props['metadata'],
2653  'oi_media_type' => $props['media_type'],
2654  'oi_major_mime' => $props['major_mime'],
2655  'oi_minor_mime' => $props['minor_mime'],
2656  'oi_deleted' => $this->unsuppress ? 0 : $row->fa_deleted,
2657  'oi_sha1' => $sha1 ];
2658  }
2659 
2660  $deleteIds[] = $row->fa_id;
2661 
2662  if ( !$this->unsuppress && $row->fa_deleted & File::DELETED_FILE ) {
2663  // private files can stay where they are
2664  $status->successCount++;
2665  } else {
2666  $storeBatch[] = [ $deletedUrl, 'public', $destRel ];
2667  $this->cleanupBatch[] = $row->fa_storage_key;
2668  }
2669 
2670  $first = false;
2671  }
2672 
2673  unset( $result );
2674 
2675  // Add a warning to the status object for missing IDs
2676  $missingIds = array_diff( $this->ids, $idsPresent );
2677 
2678  foreach ( $missingIds as $id ) {
2679  $status->error( 'undelete-missing-filearchive', $id );
2680  }
2681 
2682  if ( !$repo->hasSha1Storage() ) {
2683  // Remove missing files from batch, so we don't get errors when undeleting them
2684  $checkStatus = $this->removeNonexistentFiles( $storeBatch );
2685  if ( !$checkStatus->isGood() ) {
2686  $status->merge( $checkStatus );
2687  return $status;
2688  }
2689  $storeBatch = $checkStatus->value;
2690 
2691  // Run the store batch
2692  // Use the OVERWRITE_SAME flag to smooth over a common error
2693  $storeStatus = $this->file->repo->storeBatch( $storeBatch, FileRepo::OVERWRITE_SAME );
2694  $status->merge( $storeStatus );
2695 
2696  if ( !$status->isGood() ) {
2697  // Even if some files could be copied, fail entirely as that is the
2698  // easiest thing to do without data loss
2699  $this->cleanupFailedBatch( $storeStatus, $storeBatch );
2700  $status->setOK( false );
2701  $this->file->unlock();
2702 
2703  return $status;
2704  }
2705  }
2706 
2707  // Run the DB updates
2708  // Because we have locked the image row, key conflicts should be rare.
2709  // If they do occur, we can roll back the transaction at this time with
2710  // no data loss, but leaving unregistered files scattered throughout the
2711  // public zone.
2712  // This is not ideal, which is why it's important to lock the image row.
2713  if ( $insertCurrent ) {
2714  $dbw->insert( 'image', $insertCurrent, __METHOD__ );
2715  }
2716 
2717  if ( $insertBatch ) {
2718  $dbw->insert( 'oldimage', $insertBatch, __METHOD__ );
2719  }
2720 
2721  if ( $deleteIds ) {
2722  $dbw->delete( 'filearchive',
2723  [ 'fa_id' => $deleteIds ],
2724  __METHOD__ );
2725  }
2726 
2727  // If store batch is empty (all files are missing), deletion is to be considered successful
2728  if ( $status->successCount > 0 || !$storeBatch || $repo->hasSha1Storage() ) {
2729  if ( !$exists ) {
2730  wfDebug( __METHOD__ . " restored {$status->successCount} items, creating a new current\n" );
2731 
2732  DeferredUpdates::addUpdate( SiteStatsUpdate::factory( [ 'images' => 1 ] ) );
2733 
2734  $this->file->purgeEverything();
2735  } else {
2736  wfDebug( __METHOD__ . " restored {$status->successCount} as archived versions\n" );
2737  $this->file->purgeDescription();
2738  }
2739  }
2740 
2741  $this->file->unlock();
2742 
2743  return $status;
2744  }
2745 
2751  protected function removeNonexistentFiles( $triplets ) {
2752  $files = $filteredTriplets = [];
2753  foreach ( $triplets as $file ) {
2754  $files[$file[0]] = $file[0];
2755  }
2756 
2757  $result = $this->file->repo->fileExistsBatch( $files );
2758  if ( in_array( null, $result, true ) ) {
2759  return Status::newFatal( 'backend-fail-internal',
2760  $this->file->repo->getBackend()->getName() );
2761  }
2762 
2763  foreach ( $triplets as $file ) {
2764  if ( $result[$file[0]] ) {
2765  $filteredTriplets[] = $file;
2766  }
2767  }
2768 
2769  return Status::newGood( $filteredTriplets );
2770  }
2771 
2777  protected function removeNonexistentFromCleanup( $batch ) {
2778  $files = $newBatch = [];
2779  $repo = $this->file->repo;
2780 
2781  foreach ( $batch as $file ) {
2782  $files[$file] = $repo->getVirtualUrl( 'deleted' ) . '/' .
2783  rawurlencode( $repo->getDeletedHashPath( $file ) . $file );
2784  }
2785 
2786  $result = $repo->fileExistsBatch( $files );
2787 
2788  foreach ( $batch as $file ) {
2789  if ( $result[$file] ) {
2790  $newBatch[] = $file;
2791  }
2792  }
2793 
2794  return $newBatch;
2795  }
2796 
2802  public function cleanup() {
2803  if ( !$this->cleanupBatch ) {
2804  return $this->file->repo->newGood();
2805  }
2806 
2807  $this->cleanupBatch = $this->removeNonexistentFromCleanup( $this->cleanupBatch );
2808 
2809  $status = $this->file->repo->cleanupDeletedBatch( $this->cleanupBatch );
2810 
2811  return $status;
2812  }
2813 
2821  protected function cleanupFailedBatch( $storeStatus, $storeBatch ) {
2822  $cleanupBatch = [];
2823 
2824  foreach ( $storeStatus->success as $i => $success ) {
2825  // Check if this item of the batch was successfully copied
2826  if ( $success ) {
2827  // Item was successfully copied and needs to be removed again
2828  // Extract ($dstZone, $dstRel) from the batch
2829  $cleanupBatch[] = [ $storeBatch[$i][1], $storeBatch[$i][2] ];
2830  }
2831  }
2832  $this->file->repo->cleanupBatch( $cleanupBatch );
2833  }
2834 }
2835 
2836 # ------------------------------------------------------------------------------
2837 
2844  protected $file;
2845 
2847  protected $target;
2848 
2849  protected $cur;
2850 
2851  protected $olds;
2852 
2853  protected $oldCount;
2854 
2855  protected $archive;
2856 
2858  protected $db;
2859 
2865  $this->file = $file;
2866  $this->target = $target;
2867  $this->oldHash = $this->file->repo->getHashPath( $this->file->getName() );
2868  $this->newHash = $this->file->repo->getHashPath( $this->target->getDBkey() );
2869  $this->oldName = $this->file->getName();
2870  $this->newName = $this->file->repo->getNameFromTitle( $this->target );
2871  $this->oldRel = $this->oldHash . $this->oldName;
2872  $this->newRel = $this->newHash . $this->newName;
2873  $this->db = $file->getRepo()->getMasterDB();
2874  }
2875 
2879  public function addCurrent() {
2880  $this->cur = [ $this->oldRel, $this->newRel ];
2881  }
2882 
2887  public function addOlds() {
2888  $archiveBase = 'archive';
2889  $this->olds = [];
2890  $this->oldCount = 0;
2891  $archiveNames = [];
2892 
2893  $result = $this->db->select( 'oldimage',
2894  [ 'oi_archive_name', 'oi_deleted' ],
2895  [ 'oi_name' => $this->oldName ],
2896  __METHOD__,
2897  [ 'LOCK IN SHARE MODE' ] // ignore snapshot
2898  );
2899 
2900  foreach ( $result as $row ) {
2901  $archiveNames[] = $row->oi_archive_name;
2902  $oldName = $row->oi_archive_name;
2903  $bits = explode( '!', $oldName, 2 );
2904 
2905  if ( count( $bits ) != 2 ) {
2906  wfDebug( "Old file name missing !: '$oldName' \n" );
2907  continue;
2908  }
2909 
2910  list( $timestamp, $filename ) = $bits;
2911 
2912  if ( $this->oldName != $filename ) {
2913  wfDebug( "Old file name doesn't match: '$oldName' \n" );
2914  continue;
2915  }
2916 
2917  $this->oldCount++;
2918 
2919  // Do we want to add those to oldCount?
2920  if ( $row->oi_deleted & File::DELETED_FILE ) {
2921  continue;
2922  }
2923 
2924  $this->olds[] = [
2925  "{$archiveBase}/{$this->oldHash}{$oldName}",
2926  "{$archiveBase}/{$this->newHash}{$timestamp}!{$this->newName}"
2927  ];
2928  }
2929 
2930  return $archiveNames;
2931  }
2932 
2937  public function execute() {
2938  $repo = $this->file->repo;
2939  $status = $repo->newGood();
2940  $destFile = wfLocalFile( $this->target );
2941 
2942  $this->file->lock(); // begin
2943  $destFile->lock(); // quickly fail if destination is not available
2944 
2945  $triplets = $this->getMoveTriplets();
2946  $checkStatus = $this->removeNonexistentFiles( $triplets );
2947  if ( !$checkStatus->isGood() ) {
2948  $destFile->unlock();
2949  $this->file->unlock();
2950  $status->merge( $checkStatus ); // couldn't talk to file backend
2951  return $status;
2952  }
2953  $triplets = $checkStatus->value;
2954 
2955  // Verify the file versions metadata in the DB.
2956  $statusDb = $this->verifyDBUpdates();
2957  if ( !$statusDb->isGood() ) {
2958  $destFile->unlock();
2959  $this->file->unlock();
2960  $statusDb->setOK( false );
2961 
2962  return $statusDb;
2963  }
2964 
2965  if ( !$repo->hasSha1Storage() ) {
2966  // Copy the files into their new location.
2967  // If a prior process fataled copying or cleaning up files we tolerate any
2968  // of the existing files if they are identical to the ones being stored.
2969  $statusMove = $repo->storeBatch( $triplets, FileRepo::OVERWRITE_SAME );
2970  wfDebugLog( 'imagemove', "Moved files for {$this->file->getName()}: " .
2971  "{$statusMove->successCount} successes, {$statusMove->failCount} failures" );
2972  if ( !$statusMove->isGood() ) {
2973  // Delete any files copied over (while the destination is still locked)
2974  $this->cleanupTarget( $triplets );
2975  $destFile->unlock();
2976  $this->file->unlock();
2977  wfDebugLog( 'imagemove', "Error in moving files: "
2978  . $statusMove->getWikiText( false, false, 'en' ) );
2979  $statusMove->setOK( false );
2980 
2981  return $statusMove;
2982  }
2983  $status->merge( $statusMove );
2984  }
2985 
2986  // Rename the file versions metadata in the DB.
2987  $this->doDBUpdates();
2988 
2989  wfDebugLog( 'imagemove', "Renamed {$this->file->getName()} in database: " .
2990  "{$statusDb->successCount} successes, {$statusDb->failCount} failures" );
2991 
2992  $destFile->unlock();
2993  $this->file->unlock(); // done
2994 
2995  // Everything went ok, remove the source files
2996  $this->cleanupSource( $triplets );
2997 
2998  $status->merge( $statusDb );
2999 
3000  return $status;
3001  }
3002 
3009  protected function verifyDBUpdates() {
3010  $repo = $this->file->repo;
3011  $status = $repo->newGood();
3012  $dbw = $this->db;
3013 
3014  $hasCurrent = $dbw->selectField(
3015  'image',
3016  '1',
3017  [ 'img_name' => $this->oldName ],
3018  __METHOD__,
3019  [ 'FOR UPDATE' ]
3020  );
3021  $oldRowCount = $dbw->selectField(
3022  'oldimage',
3023  'COUNT(*)',
3024  [ 'oi_name' => $this->oldName ],
3025  __METHOD__,
3026  [ 'FOR UPDATE' ]
3027  );
3028 
3029  if ( $hasCurrent ) {
3030  $status->successCount++;
3031  } else {
3032  $status->failCount++;
3033  }
3034  $status->successCount += $oldRowCount;
3035  // Bug 34934: oldCount is based on files that actually exist.
3036  // There may be more DB rows than such files, in which case $affected
3037  // can be greater than $total. We use max() to avoid negatives here.
3038  $status->failCount += max( 0, $this->oldCount - $oldRowCount );
3039  if ( $status->failCount ) {
3040  $status->error( 'imageinvalidfilename' );
3041  }
3042 
3043  return $status;
3044  }
3045 
3050  protected function doDBUpdates() {
3051  $dbw = $this->db;
3052 
3053  // Update current image
3054  $dbw->update(
3055  'image',
3056  [ 'img_name' => $this->newName ],
3057  [ 'img_name' => $this->oldName ],
3058  __METHOD__
3059  );
3060  // Update old images
3061  $dbw->update(
3062  'oldimage',
3063  [
3064  'oi_name' => $this->newName,
3065  'oi_archive_name = ' . $dbw->strreplace( 'oi_archive_name',
3066  $dbw->addQuotes( $this->oldName ), $dbw->addQuotes( $this->newName ) ),
3067  ],
3068  [ 'oi_name' => $this->oldName ],
3069  __METHOD__
3070  );
3071  }
3072 
3077  protected function getMoveTriplets() {
3078  $moves = array_merge( [ $this->cur ], $this->olds );
3079  $triplets = []; // The format is: (srcUrl, destZone, destUrl)
3080 
3081  foreach ( $moves as $move ) {
3082  // $move: (oldRelativePath, newRelativePath)
3083  $srcUrl = $this->file->repo->getVirtualUrl() . '/public/' . rawurlencode( $move[0] );
3084  $triplets[] = [ $srcUrl, 'public', $move[1] ];
3085  wfDebugLog(
3086  'imagemove',
3087  "Generated move triplet for {$this->file->getName()}: {$srcUrl} :: public :: {$move[1]}"
3088  );
3089  }
3090 
3091  return $triplets;
3092  }
3093 
3099  protected function removeNonexistentFiles( $triplets ) {
3100  $files = [];
3101 
3102  foreach ( $triplets as $file ) {
3103  $files[$file[0]] = $file[0];
3104  }
3105 
3106  $result = $this->file->repo->fileExistsBatch( $files );
3107  if ( in_array( null, $result, true ) ) {
3108  return Status::newFatal( 'backend-fail-internal',
3109  $this->file->repo->getBackend()->getName() );
3110  }
3111 
3112  $filteredTriplets = [];
3113  foreach ( $triplets as $file ) {
3114  if ( $result[$file[0]] ) {
3115  $filteredTriplets[] = $file;
3116  } else {
3117  wfDebugLog( 'imagemove', "File {$file[0]} does not exist" );
3118  }
3119  }
3120 
3121  return Status::newGood( $filteredTriplets );
3122  }
3123 
3129  protected function cleanupTarget( $triplets ) {
3130  // Create dest pairs from the triplets
3131  $pairs = [];
3132  foreach ( $triplets as $triplet ) {
3133  // $triplet: (old source virtual URL, dst zone, dest rel)
3134  $pairs[] = [ $triplet[1], $triplet[2] ];
3135  }
3136 
3137  $this->file->repo->cleanupBatch( $pairs );
3138  }
3139 
3145  protected function cleanupSource( $triplets ) {
3146  // Create source file names from the triplets
3147  $files = [];
3148  foreach ( $triplets as $triplet ) {
3149  $files[] = $triplet[0];
3150  }
3151 
3152  $this->file->repo->cleanupBatch( $files );
3153  }
3154 }
3155 
3157  public function __construct( Status $status ) {
3158  parent::__construct(
3159  'actionfailed',
3160  $status->getMessage()
3161  );
3162  }
3163 
3164  public function report() {
3165  global $wgOut;
3166  $wgOut->setStatusCode( 429 );
3167  parent::report();
3168  }
3169 }
static purgePatrolFooterCache($articleID)
Purge the cache used to check if it is worth showing the patrol footer For example, it is done during re-uploads when file patrol is used.
Definition: Article.php:1221
removeNonexistentFiles($batch)
Removes non-existent files from a deletion batch.
Definition: LocalFile.php:2413
getArchiveThumbPath($archiveName, $suffix=false)
Get the path of an archived file's thumbs, or a particular thumb if $suffix is specified.
Definition: File.php:1597
static getMainWANInstance()
Get the main WAN cache object.
exists()
canRender inherited
Definition: LocalFile.php:849
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
invalidateCache()
Purge the file object/metadata cache.
Definition: LocalFile.php:310
the array() calling protocol came about after MediaWiki 1.4rc1.
MediaHandler $handler
Definition: File.php:113
string $media_type
MEDIATYPE_xxx (bitmap, drawing, audio...)
Definition: LocalFile.php:61
recordUpload($oldver, $desc, $license= '', $copyStatus= '', $source= '', $watch=false, $timestamp=false, User $user=null)
Record a file upload in the upload log and the image table.
Definition: LocalFile.php:1236
bool $extraDataLoaded
Whether or not lazy-loaded data has been loaded from the database.
Definition: LocalFile.php:79
if(count($args)==0) $dir
assertTitleDefined()
Assert that $this->title is set to a Title.
Definition: File.php:2257
const VERSION
Definition: LocalFile.php:44
$success
userCan($field, User $user=null)
Determine if the current user is allowed to view a particular field of this file, if it's marked as d...
Definition: File.php:2146
addAll()
Add all revisions of the file.
Definition: LocalFile.php:2489
cleanupTarget($triplets)
Cleanup a partially moved array of triplets by deleting the target files.
Definition: LocalFile.php:3129
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:189
cleanupSource($triplets)
Cleanup a fully moved array of triplets by deleting the source files.
Definition: LocalFile.php:3145
loadFromRow($row, $prefix= 'img_')
Load file metadata from a DB result row.
Definition: LocalFile.php:524
string $minor_mime
Minor MIME type.
Definition: LocalFile.php:97
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
getHistory($limit=null, $start=null, $end=null, $inc=true)
purgeDescription inherited
Definition: LocalFile.php:1039
restore($versions=[], $unsuppress=false)
Restore all or specified deleted revisions to the given file.
Definition: LocalFile.php:1823
static singleton()
Get an instance of this class.
Definition: MimeMagic.php:29
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException'returning false will NOT prevent logging $e
Definition: hooks.txt:2106
static newFatal($message)
Factory function for fatal errors.
Definition: StatusValue.php:63
static getCacheSetOptions(IDatabase $db1)
Merge the result of getSessionLagStatus() for several DBs using the most pessimistic values to estima...
Definition: Database.php:3039
Set options of the Parser.
purgeMetadataCache()
Refresh metadata in memcached, but don't touch thumbnails or CDN.
Definition: LocalFile.php:893
releaseFileLock()
Definition: LocalFile.php:1979
const DELETE_SOURCE
Definition: File.php:65
if(!isset($args[0])) $lang
static newFromEntry(LogEntry $entry)
Constructs a new formatter suitable for given entry.
__construct(File $file, $reason= '', $suppress=false, $user=null)
Definition: LocalFile.php:2103
cleanup()
Delete unused files in the deleted zone.
Definition: LocalFile.php:2802
getUser($type= 'text')
Returns ID or name of user who uploaded the file.
Definition: LocalFile.php:761
getThumbPath($suffix=false)
Get the path of the thumbnail directory, or a particular file if $suffix is specified.
Definition: File.php:1610
width
recordUpload2($oldver, $comment, $pageText, $props=false, $timestamp=false, $user=null, $tags=[])
Record a file upload in the upload log and the image table.
Definition: LocalFile.php:1267
addCurrent()
Add the current image to the batch.
Definition: LocalFile.php:2879
const DELETE_SOURCE
Definition: FileRepo.php:38
getSize()
Returns the size of the image file, in bytes.
Definition: LocalFile.php:812
Handles purging appropriate CDN URLs given a title (or titles)
verifyDBUpdates()
Verify the database updates and return a new FileRepoStatus indicating how many rows would be updated...
Definition: LocalFile.php:3009
$comment
Helper class for file undeletion.
Definition: LocalFile.php:2443
unlock()
Decrement the lock reference count and end the atomic section if it reaches zero. ...
Definition: LocalFile.php:2039
string $major_mime
Major MIME type.
Definition: LocalFile.php:94
string $sha1
SHA-1 base 36 content hash.
Definition: LocalFile.php:73
$source
$value
isMissing()
splitMime inherited
Definition: LocalFile.php:692
getArchiveThumbUrl($archiveName, $suffix=false)
Get the URL of the archived file's thumbs, or a particular thumb if $suffix is specified.
Definition: File.php:1654
static isVirtualUrl($url)
Determine if a string is an mwrepo:// URL.
Definition: FileRepo.php:254
isGood()
Returns whether the operation completed and didn't have any error or warnings.
$files
assertRepoDefined()
Assert that $this->repo is set to a valid FileRepo instance.
Definition: File.php:2247
it s the revision text itself In either if gzip is the revision text is gzipped $flags
Definition: hooks.txt:2707
getName()
Return the name of this file.
Definition: File.php:296
string $name
The name of a file from its title object.
Definition: File.php:122
static newFromKey($sha1, $repo, $timestamp=false)
Create a LocalFile from a SHA-1 key Do not call this except from inside a repo class.
Definition: LocalFile.php:176
getRepo()
Returns the repository.
Definition: File.php:1853
unprefixRow($row, $prefix= 'img_')
Definition: LocalFile.php:463
array $cleanupBatch
List of file IDs to restore.
Definition: LocalFile.php:2448
int $user
User ID of uploader.
Definition: LocalFile.php:103
setProps($info)
Set properties in this object to be equal to those given in the associative array $info...
Definition: LocalFile.php:661
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
const METADATA_BAD
static makeParamBlob($params)
Create a blob from a parameter array.
Definition: LogEntry.php:140
int $bits
Returned by getimagesize (loadFromXxx)
Definition: LocalFile.php:58
static newFromTitle(LinkTarget $linkTarget, $id=0, $flags=0)
Load either the current, or a specified, revision that's attached to a given link target...
Definition: Revision.php:128
title
publish($src, $flags=0, array $options=[])
Move or copy a file to its public location.
Definition: LocalFile.php:1590
const ATOMIC_SECTION_LOCK
Definition: LocalFile.php:132
bool $fileExists
Does the file exist on disk? (loadFromXxx)
Definition: LocalFile.php:49
wfLocalFile($title)
Get an object referring to a locally registered file.
doDBUpdates()
Do the database updates and return a new FileRepoStatus indicating how many rows where updated...
Definition: LocalFile.php:3050
getHeight($page=1)
Return the height of the image.
Definition: LocalFile.php:734
getTitle()
Return the associated title object.
Definition: File.php:325
Title string bool $title
Definition: File.php:98
getHashPath()
Get the filename hash component of the directory including trailing slash, e.g.
Definition: File.php:1496
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist & $tables
Definition: hooks.txt:1011
purgeEverything()
Purge metadata and all affected pages when the file is created, deleted, or majorly updated...
Definition: File.php:1440
__destruct()
Clean up any dangling locks.
Definition: LocalFile.php:2061
static queueRecursiveJobsForTable(Title $title, $table)
Queue a RefreshLinks job for any table.
wfDebug($text, $dest= 'all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
const DELETED_FILE
Definition: File.php:52
getBackend()
Get the file backend instance.
Definition: FileRepo.php:215
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as $wgLang
Definition: design.txt:56
The index of the header message $result[1]=The index of the body text message $result[2 through n]=Parameters passed to body text message.Please note the header message cannot receive/use parameters. 'ImportHandleLogItemXMLTag':When parsing a XML tag in a log item.Return false to stop further processing of the tag $reader:XMLReader object $logInfo:Array of information 'ImportHandlePageXMLTag':When parsing a XML tag in a page.Return false to stop further processing of the tag $reader:XMLReader object &$pageInfo:Array of information 'ImportHandleRevisionXMLTag':When parsing a XML tag in a page revision.Return false to stop further processing of the tag $reader:XMLReader object $pageInfo:Array of page information $revisionInfo:Array of revision information 'ImportHandleToplevelXMLTag':When parsing a top level XML tag.Return false to stop further processing of the tag $reader:XMLReader object 'ImportHandleUploadXMLTag':When parsing a XML tag in a file upload.Return false to stop further processing of the tag $reader:XMLReader object $revisionInfo:Array of information 'ImportLogInterwikiLink':Hook to change the interwiki link used in log entries and edit summaries for transwiki imports.&$fullInterwikiPrefix:Interwiki prefix, may contain colons.&$pageTitle:String that contains page title. 'ImportSources':Called when reading from the $wgImportSources configuration variable.Can be used to lazy-load the import sources list.&$importSources:The value of $wgImportSources.Modify as necessary.See the comment in DefaultSettings.php for the detail of how to structure this array. 'InfoAction':When building information to display on the action=info page.$context:IContextSource object &$pageInfo:Array of information 'InitializeArticleMaybeRedirect':MediaWiki check to see if title is a redirect.&$title:Title object for the current page &$request:WebRequest &$ignoreRedirect:boolean to skip redirect check &$target:Title/string of redirect target &$article:Article object 'InternalParseBeforeLinks':during Parser's internalParse method before links but after nowiki/noinclude/includeonly/onlyinclude and other processings.&$parser:Parser object &$text:string containing partially parsed text &$stripState:Parser's internal StripState object 'InternalParseBeforeSanitize':during Parser's internalParse method just before the parser removes unwanted/dangerous HTML tags and after nowiki/noinclude/includeonly/onlyinclude and other processings.Ideal for syntax-extensions after template/parser function execution which respect nowiki and HTML-comments.&$parser:Parser object &$text:string containing partially parsed text &$stripState:Parser's internal StripState object 'InterwikiLoadPrefix':When resolving if a given prefix is an interwiki or not.Return true without providing an interwiki to continue interwiki search.$prefix:interwiki prefix we are looking for.&$iwData:output array describing the interwiki with keys iw_url, iw_local, iw_trans and optionally iw_api and iw_wikiid. 'InvalidateEmailComplete':Called after a user's email has been invalidated successfully.$user:user(object) whose email is being invalidated 'IRCLineURL':When constructing the URL to use in an IRC notification.Callee may modify $url and $query, URL will be constructed as $url.$query &$url:URL to index.php &$query:Query string $rc:RecentChange object that triggered url generation 'IsFileCacheable':Override the result of Article::isFileCacheable()(if true) &$article:article(object) being checked 'IsTrustedProxy':Override the result of IP::isTrustedProxy() &$ip:IP being check &$result:Change this value to override the result of IP::isTrustedProxy() 'IsUploadAllowedFromUrl':Override the result of UploadFromUrl::isAllowedUrl() $url:URL used to upload from &$allowed:Boolean indicating if uploading is allowed for given URL 'isValidEmailAddr':Override the result of Sanitizer::validateEmail(), for instance to return false if the domain name doesn't match your organization.$addr:The e-mail address entered by the user &$result:Set this and return false to override the internal checks 'isValidPassword':Override the result of User::isValidPassword() $password:The password entered by the user &$result:Set this and return false to override the internal checks $user:User the password is being validated for 'Language::getMessagesFileName':$code:The language code or the language we're looking for a messages file for &$file:The messages file path, you can override this to change the location. 'LanguageGetMagic':DEPRECATED!Use $magicWords in a file listed in $wgExtensionMessagesFiles instead.Use this to define synonyms of magic words depending of the language &$magicExtensions:associative array of magic words synonyms $lang:language code(string) 'LanguageGetNamespaces':Provide custom ordering for namespaces or remove namespaces.Do not use this hook to add namespaces.Use CanonicalNamespaces for that.&$namespaces:Array of namespaces indexed by their numbers 'LanguageGetSpecialPageAliases':DEPRECATED!Use $specialPageAliases in a file listed in $wgExtensionMessagesFiles instead.Use to define aliases of special pages names depending of the language &$specialPageAliases:associative array of magic words synonyms $lang:language code(string) 'LanguageGetTranslatedLanguageNames':Provide translated language names.&$names:array of language code=> language name $code:language of the preferred translations 'LanguageLinks':Manipulate a page's language links.This is called in various places to allow extensions to define the effective language links for a page.$title:The page's Title.&$links:Associative array mapping language codes to prefixed links of the form"language:title".&$linkFlags:Associative array mapping prefixed links to arrays of flags.Currently unused, but planned to provide support for marking individual language links in the UI, e.g.for featured articles. 'LanguageSelector':Hook to change the language selector available on a page.$out:The output page.$cssClassName:CSS class name of the language selector. 'LinkBegin':DEPRECATED!Use HtmlPageLinkRendererBegin instead.Used when generating internal and interwiki links in Linker::link(), before processing starts.Return false to skip default processing and return $ret.See documentation for Linker::link() for details on the expected meanings of parameters.$skin:the Skin object $target:the Title that the link is pointing to &$html:the contents that the< a > tag should have(raw HTML) $result
Definition: hooks.txt:1938
const TS_UNIX
Unix time - the number of seconds since 1970-01-01 00:00:00 UTC.
Definition: defines.php:6
getThumbnails()
Get all thumbnail names previously generated for this file STUB Overridden by LocalFile.
Definition: File.php:1409
$batch
Definition: linkcache.txt:23
Deferrable Update for closure/callback updates that should use auto-commit mode.
getDescriptionShortUrl()
Get short description URL for a file based on the page ID.
Definition: LocalFile.php:778
nextHistoryLine()
Returns the history of this file, line by line.
Definition: LocalFile.php:1092
wfTimestamp($outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
__construct(File $file, Title $target)
Definition: LocalFile.php:2864
getPath()
Return the storage path to the file.
Definition: File.php:416
wfDebugLog($logGroup, $text, $dest= 'all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not...
removeNonexistentFiles($triplets)
Removes non-existent files from move batch.
Definition: LocalFile.php:3099
int $historyRes
Result of the query for the file's history (nextHistoryLine)
Definition: LocalFile.php:91
bool $locked
True if the image row is locked.
Definition: LocalFile.php:121
getFileSha1($virtualUrl)
Get the sha1 (base 36) of a file with a given virtual URL/storage path.
Definition: FileRepo.php:1586
deleteOld($archiveName, $reason, $suppress=false, $user=null)
Delete an old version of the file.
Definition: LocalFile.php:1787
getReadOnlyReason()
Get an explanatory message if this repo is read-only.
Definition: FileRepo.php:225
__construct(File $file, $unsuppress=false)
Definition: LocalFile.php:2463
wfReadOnly()
Check whether the wiki is in read-only mode.
string $metadata
Handler-specific metadata.
Definition: LocalFile.php:70
string $descriptionTouched
TS_MW timestamp of the last change of the file description.
Definition: LocalFile.php:112
We ve cleaned up the code here by removing clumps of infrequently used code and moving them off somewhere else It s much easier for someone working with this code to see what s _really_ going and make changes or fix bugs In we can take all the code that deals with the little used title reversing we can concentrate it all in an extension file
Definition: hooks.txt:93
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling but I prefer the flexibility This should also do the output encoding The system allocates a global one in $wgOut Title Represents the title of an and does all the work of translating among various forms such as plain database key
Definition: design.txt:25
publishTo($src, $dstRel, $flags=0, array $options=[])
Move or copy a file to a specified location.
Definition: LocalFile.php:1609
string $repoClass
Definition: LocalFile.php:85
File backend exception for checked exceptions (e.g.
An error page which can definitely be safely rendered using the OutputPage.
isDeleted($field)
Is this file a "deleted" file in a private archive? STUB.
Definition: File.php:1874
isVectorized()
Return true if the file is vectorized.
Definition: File.php:554
Class to invalidate the HTML cache of all the pages linking to a given title.
isMultipage()
Returns 'true' if this file is a type which supports multiple pages, e.g.
Definition: File.php:1958
const LOAD_ALL
Definition: LocalFile.php:130
getHandler()
Get a MediaHandler instance for this file.
Definition: File.php:1364
getCacheKey()
Get the memcached key for the main data for this file, or false if there is no access to the shared c...
Definition: LocalFile.php:239
isMetadataValid($image, $metadata)
Check if the metadata string is valid for this handler.
static factory(array $deltas)
if($limit) $timestamp
static selectFields()
Fields in the filearchive table.
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context $options
Definition: hooks.txt:1050
getDescriptionTouched()
Definition: LocalFile.php:1913
const LOCK_EX
Definition: LockManager.php:70
getRel()
Get the path of the file relative to the public zone root.
Definition: File.php:1511
$res
Definition: database.txt:21
static singleton()
Get a RepoGroup instance.
Definition: RepoGroup.php:59
static isStoragePath($path)
Check if a given path is a "mwstore://" path.
bool $unsuppress
Whether to remove all settings for suppressed fields.
Definition: LocalFile.php:2457
$wgUploadThumbnailRenderMap
When defined, is an array of thumbnail widths to be rendered at upload time.
static newNullRevision($dbw, $pageId, $summary, $minor, $user=null)
Create a new null-revision for insertion into a page's history.
Definition: Revision.php:1678
static selectFields()
Fields in the image table.
Definition: LocalFile.php:196
string $user_text
User name of uploader.
Definition: LocalFile.php:106
acquireFileLock()
Definition: LocalFile.php:1969
__construct($title, $repo)
Constructor.
Definition: LocalFile.php:221
const CACHE_FIELD_MAX_LEN
Definition: LocalFile.php:46
__construct(Status $status)
Definition: LocalFile.php:3157
purgeThumbList($dir, $files)
Delete a list of thumbnails visible at urls.
Definition: LocalFile.php:1005
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such and we might be restricted by PHP settings such as safe mode or open_basedir We cannot assume that the software even has read access anywhere useful Many shared hosts run all users web applications under the same user
Wikitext formatted, in the key only.
Definition: distributors.txt:9
upload($src, $comment, $pageText, $flags=0, $props=false, $timestamp=false, $user=null, $tags=[])
getHashPath inherited
Definition: LocalFile.php:1168
loadExtraFromDB()
Load lazy file metadata from the DB.
Definition: LocalFile.php:409
$cache
Definition: mcc.php:33
const EDIT_SUPPRESS_RC
Definition: Defines.php:149
static newFromRow($row, $repo)
Create a LocalFile from a title Do not call this except from inside a repo class. ...
Definition: LocalFile.php:159
maybeUpgradeRow()
Upgrade a row if it needs it.
Definition: LocalFile.php:560
static getInitialPageText($comment= '', $license= '', $copyStatus= '', $source= '', Config $config=null)
Get the initial image page text based on a comment and optional file status information.
quickImport($src, $dst, $options=null)
Import a file from the local file system into the repo.
Definition: FileRepo.php:957
readOnlyFatalStatus()
Definition: LocalFile.php:2053
Helper class for file movement.
Definition: LocalFile.php:2842
bool $upgraded
Whether the row was upgraded on load.
Definition: LocalFile.php:115
static newExtraneousContext(Title $title, $request=[])
Create a new extraneous context.
const TS_MW
MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS)
Definition: defines.php:11
selectField($table, $var, $cond= '', $fname=__METHOD__, $options=[])
A SELECT wrapper which returns a single field from a single result row.
static selectFields()
Fields in the oldimage table.
static newFromTitle($title, $repo, $unused=null)
Create a LocalFile from a title Do not call this except from inside a repo class. ...
Definition: LocalFile.php:146
loadFieldsWithTimestamp($dbr, $fname)
Definition: LocalFile.php:434
execute()
Run the transaction.
Definition: LocalFile.php:2353
const DELETED_RESTRICTED
Definition: Revision.php:88
getVirtualUrl($suffix=false)
Get the public zone virtual URL for a current version source file.
Definition: File.php:1712
bool $all
Add all revisions of the file.
Definition: LocalFile.php:2454
decodeRow($row, $prefix= 'img_')
Decode a row from the database (either object or array) to an array with timestamps and MIME types de...
Definition: LocalFile.php:488
bool $lockedOwnTrx
True if the image row is locked with a lock initiated transaction.
Definition: LocalFile.php:124
static newGood($value=null)
Factory function for good results.
Definition: StatusValue.php:76
getStreamHeaders($metadata)
Get useful response headers for GET/HEAD requests for a file with the given metadata.
getThumbnails($archiveName=false)
getTransformScript inherited
Definition: LocalFile.php:870
static run($event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:131
array $ids
List of file IDs to restore.
Definition: LocalFile.php:2451
bool $dataLoaded
Whether or not core data has been loaded from the database (loadFromXxx)
Definition: LocalFile.php:76
const NS_FILE
Definition: Defines.php:62
FileRepo LocalRepo ForeignAPIRepo bool $repo
Some member variables can be lazy-initialised using __get().
Definition: File.php:95
static makeContent($text, Title $title=null, $modelId=null, $format=null)
Convenience function for creating a Content object from a given textual representation.
prerenderThumbnails()
Prerenders a configurable set of thumbnails.
Definition: LocalFile.php:978
getMetadata()
Get handler-specific metadata.
Definition: LocalFile.php:794
presenting them properly to the user as errors is done by the caller return true use this to change the list i e etc $rev
Definition: hooks.txt:1725
static getSha1Base36FromPath($path)
Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case encoding, zero padded to 31 digits.
Definition: FSFile.php:218
string $mime
MIME type, determined by MimeMagic::guessMimeType.
Definition: LocalFile.php:64
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Special handling for file pages.
execute()
Perform the move.
Definition: LocalFile.php:2937
Helper class for file deletion.
Definition: LocalFile.php:2072
array $deletionBatch
Items to be processed in the deletion batch.
Definition: LocalFile.php:2086
purgeOldThumbnails($archiveName)
Delete cached transformed files for an archived version only.
Definition: LocalFile.php:922
const DELETED_TEXT
Definition: Revision.php:85
getWidth($page=1)
Return the width of the image.
Definition: LocalFile.php:707
FileRepoStatus $status
Definition: LocalFile.php:2092
static singleton($wiki=false)
removeNonexistentFiles($triplets)
Removes non-existent files from a store batch.
Definition: LocalFile.php:2751
getMessage($shortContext=false, $longContext=false, $lang=null)
Get a bullet list of the errors as a Message object.
Definition: Status.php:233
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
static addUpdate(DeferrableUpdate $update, $stage=self::POSTSEND)
Add an update to the deferred list to be run later by execute()
removeNonexistentFromCleanup($batch)
Removes non-existent files from a cleanup batch.
Definition: LocalFile.php:2777
const DELETED_USER
Definition: Revision.php:87
Class for creating log entries manually, to inject them into the database.
Definition: LogEntry.php:394
if(!defined( 'MEDIAWIKI')) $fname
This file is not a valid entry point, perform no further processing unless MEDIAWIKI is defined...
Definition: Setup.php:36
upgradeRow()
Fix assorted version-related problems with the image row by reloading it from the file...
Definition: LocalFile.php:606
Class representing a non-directory file on the file system.
Definition: FSFile.php:29
const OVERWRITE_SAME
Definition: FileRepo.php:40
static getHashFromKey($key)
Gets the SHA1 hash from a storage key.
Definition: LocalRepo.php:177
const EDIT_NEW
Definition: Defines.php:146
getCacheFields($prefix= 'img_')
Definition: LocalFile.php:336
cleanupFailedBatch($storeStatus, $storeBatch)
Cleanup a failed batch.
Definition: LocalFile.php:2821
Job for asynchronous rendering of thumbnails.
bool $upgrading
Whether the row was scheduled to upgrade on load.
Definition: LocalFile.php:118
lock()
Start an atomic DB section and lock the image for update or increments a reference counter if the loc...
Definition: LocalFile.php:1994
getDescriptionText($lang=null)
Get the HTML text of the description page This is not used by ImagePage for local files...
Definition: LocalFile.php:1869
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content $content
Definition: hooks.txt:1050
string $url
The URL corresponding to one of the four basic zones.
Definition: File.php:116
int $deleted
Bitfield akin to rev_deleted.
Definition: LocalFile.php:82
move($target)
getLinksTo inherited
Definition: LocalFile.php:1670
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at name
Definition: design.txt:12
filterThumbnailPurgeList(&$files, $options)
Remove files from the purge list.
getUrl()
Return the URL of the file.
Definition: File.php:347
$license
bool $missing
True if file is not present in file system.
Definition: LocalFile.php:127
static getHandler($type)
Get a MediaHandler for a given MIME type from the instance cache.
const METADATA_COMPATIBLE
getDescriptionUrl()
isMultipage inherited
Definition: LocalFile.php:1857
getPageDimensions(File $image, $page)
Get an associative array of page dimensions Currently "width" and "height" are understood, but this might be expanded in the future.
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context the output can only depend on parameters provided to this hook not on global state indicating whether full HTML should be generated If generation of HTML may be but other information should still be present in the ParserOutput object to manipulate or replace but no entry for that model exists in $wgContentHandlers if desired whether it is OK to use $contentModel on $title Handler functions that modify $ok should generally return false to prevent further hooks from further modifying $ok inclusive $limit
Definition: hooks.txt:1050
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the local content language as $wgContLang
Definition: design.txt:56
getDescription($audience=self::FOR_PUBLIC, User $user=null)
Definition: LocalFile.php:1888
Class to represent a local file in the wiki's own database.
Definition: LocalFile.php:43
MimeMagic helper wrapper.
Definition: MWFileProps.php:28
addOlds()
Add the old versions of the image to the batch.
Definition: LocalFile.php:2887
getThumbUrl($suffix=false)
Get the URL of the thumbnail directory, or a particular file if $suffix is specified.
Definition: File.php:1692
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set $status
Definition: hooks.txt:1050
MediaWiki Logger LoggerFactory implements a PSR[0] compatible message logging system Named Psr Log LoggerInterface instances can be obtained from the MediaWiki Logger LoggerFactory::getInstance() static method.MediaWiki\Logger\LoggerFactory expects a class implementing the MediaWiki\Logger\Spi interface to act as a factory for new Psr\Log\LoggerInterface instances.The"Spi"in MediaWiki\Logger\Spi stands for"service provider interface".An SPI is an API intended to be implemented or extended by a third party.This software design pattern is intended to enable framework extension and replaceable components.It is specifically used in the MediaWiki\Logger\LoggerFactory service to allow alternate PSR-3 logging implementations to be easily integrated with MediaWiki.The service provider interface allows the backend logging library to be implemented in multiple ways.The $wgMWLoggerDefaultSpi global provides the classname of the default MediaWiki\Logger\Spi implementation to be loaded at runtime.This can either be the name of a class implementing the MediaWiki\Logger\Spi with a zero argument const ructor or a callable that will return an MediaWiki\Logger\Spi instance.Alternately the MediaWiki\Logger\LoggerFactory MediaWiki Logger LoggerFactory
Definition: logger.txt:5
getMoveTriplets()
Generate triplets for FileRepo::storeBatch().
Definition: LocalFile.php:3077
string $timestamp
Upload timestamp.
Definition: LocalFile.php:100
getLazyCacheFields($prefix= 'img_')
Definition: LocalFile.php:361
loadFromDB($flags=0)
Load file metadata from the DB.
Definition: LocalFile.php:384
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:1161
int $height
Image height.
Definition: LocalFile.php:55
static addCallableUpdate($callable, $stage=self::POSTSEND, IDatabase $dbw=null)
Add a callable update.
loadFromFile()
Load metadata from the file itself.
Definition: LocalFile.php:327
$wgOut
Definition: Setup.php:816
serialize()
Definition: ApiMessage.php:94
const DELETED_COMMENT
Definition: Revision.php:86
string $description
Description of current revision of the file.
Definition: LocalFile.php:109
load($flags=0)
Load file metadata from cache or DB, unless already loaded.
Definition: LocalFile.php:542
purgeCache($options=[])
Delete all previously generated thumbnails, refresh metadata in memcached and purge the CDN...
Definition: LocalFile.php:904
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:50
bool $suppress
Whether to suppress all suppressable fields when deleting.
Definition: LocalFile.php:2089
getArchiveUrl($suffix=false)
Get the URL of the archive directory, or a particular file if $suffix is specified.
Definition: File.php:1634
getMediaType()
Returns the type of the media in the file.
Definition: LocalFile.php:833
loadFromCache()
Try to load file metadata from memcached, falling back to the database.
Definition: LocalFile.php:246
int $width
Image width.
Definition: LocalFile.php:52
purgeThumbnails($options=[])
Delete cached transformed files for the current version only.
Definition: LocalFile.php:945
getMimeType()
Returns the MIME type of the file.
Definition: LocalFile.php:822
resetHistory()
Reset the history pointer to the first element of the history.
Definition: LocalFile.php:1130
int $size
Size in bytes (loadFromXxx)
Definition: LocalFile.php:67
hasSha1Storage()
Returns whether or not storage is SHA-1 based.
Definition: FileRepo.php:1915
addOlds()
Add the old versions of the image to the batch.
Definition: LocalFile.php:2132
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached one of or reset my talk my contributions etc etc otherwise the built in rate limiting checks are if enabled allows for interception of redirect as a string mapping parameter names to values & $type
Definition: hooks.txt:2495
static makeTitle($ns, $title, $fragment= '', $interwiki= '')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:511
purgeDescription()
Purge the file description page, but don't go after pages using the file.
Definition: File.php:1428
$wgUpdateCompatibleMetadata
If to automatically update the img_metadata field if the metadata field is outdated but compatible wi...
addIds($ids)
Add a whole lot of files by ID.
Definition: LocalFile.php:2482
update($table, $values, $conds, $fname=__METHOD__, $options=[])
UPDATE wrapper.
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached $page
Definition: hooks.txt:2495
int $historyLine
Number of line to return by nextHistoryLine() (constructor)
Definition: LocalFile.php:88
execute()
Run the transaction, except the cleanup batch.
Definition: LocalFile.php:2501
$wgUser
Definition: Setup.php:806
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:304
addId($fa_id)
Add a file by ID.
Definition: LocalFile.php:2474