MediaWiki  1.28.0
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  Hooks::run( 'LocalFile::getHistory', [ &$this, &$tables, &$fields,
1065  &$conds, &$opts, &$join_conds ] );
1066 
1067  $res = $dbr->select( $tables, $fields, $conds, __METHOD__, $opts, $join_conds );
1068  $r = [];
1069 
1070  foreach ( $res as $row ) {
1071  $r[] = $this->repo->newFileFromRow( $row );
1072  }
1073 
1074  if ( $order == 'ASC' ) {
1075  $r = array_reverse( $r ); // make sure it ends up descending
1076  }
1077 
1078  return $r;
1079  }
1080 
1090  public function nextHistoryLine() {
1091  # Polymorphic function name to distinguish foreign and local fetches
1092  $fname = get_class( $this ) . '::' . __FUNCTION__;
1093 
1094  $dbr = $this->repo->getSlaveDB();
1095 
1096  if ( $this->historyLine == 0 ) { // called for the first time, return line from cur
1097  $this->historyRes = $dbr->select( 'image',
1098  [
1099  '*',
1100  "'' AS oi_archive_name",
1101  '0 as oi_deleted',
1102  'img_sha1'
1103  ],
1104  [ 'img_name' => $this->title->getDBkey() ],
1105  $fname
1106  );
1107 
1108  if ( 0 == $dbr->numRows( $this->historyRes ) ) {
1109  $this->historyRes = null;
1110 
1111  return false;
1112  }
1113  } elseif ( $this->historyLine == 1 ) {
1114  $this->historyRes = $dbr->select( 'oldimage', '*',
1115  [ 'oi_name' => $this->title->getDBkey() ],
1116  $fname,
1117  [ 'ORDER BY' => 'oi_timestamp DESC' ]
1118  );
1119  }
1120  $this->historyLine++;
1121 
1122  return $dbr->fetchObject( $this->historyRes );
1123  }
1124 
1128  public function resetHistory() {
1129  $this->historyLine = 0;
1130 
1131  if ( !is_null( $this->historyRes ) ) {
1132  $this->historyRes = null;
1133  }
1134  }
1135 
1166  function upload( $src, $comment, $pageText, $flags = 0, $props = false,
1167  $timestamp = false, $user = null, $tags = []
1168  ) {
1170 
1171  if ( $this->getRepo()->getReadOnlyReason() !== false ) {
1172  return $this->readOnlyFatalStatus();
1173  }
1174 
1175  $srcPath = ( $src instanceof FSFile ) ? $src->getPath() : $src;
1176  if ( !$props ) {
1177  if ( $this->repo->isVirtualUrl( $srcPath )
1178  || FileBackend::isStoragePath( $srcPath )
1179  ) {
1180  $props = $this->repo->getFileProps( $srcPath );
1181  } else {
1182  $mwProps = new MWFileProps( MimeMagic::singleton() );
1183  $props = $mwProps->getPropsFromPath( $srcPath, true );
1184  }
1185  }
1186 
1187  $options = [];
1188  $handler = MediaHandler::getHandler( $props['mime'] );
1189  if ( $handler ) {
1190  $options['headers'] = $handler->getStreamHeaders( $props['metadata'] );
1191  } else {
1192  $options['headers'] = [];
1193  }
1194 
1195  // Trim spaces on user supplied text
1196  $comment = trim( $comment );
1197 
1198  // Truncate nicely or the DB will do it for us
1199  // non-nicely (dangling multi-byte chars, non-truncated version in cache).
1200  $comment = $wgContLang->truncate( $comment, 255 );
1201  $this->lock(); // begin
1202  $status = $this->publish( $src, $flags, $options );
1203 
1204  if ( $status->successCount >= 2 ) {
1205  // There will be a copy+(one of move,copy,store).
1206  // The first succeeding does not commit us to updating the DB
1207  // since it simply copied the current version to a timestamped file name.
1208  // It is only *preferable* to avoid leaving such files orphaned.
1209  // Once the second operation goes through, then the current version was
1210  // updated and we must therefore update the DB too.
1211  $oldver = $status->value;
1212  if ( !$this->recordUpload2( $oldver, $comment, $pageText, $props, $timestamp, $user, $tags ) ) {
1213  $status->fatal( 'filenotfound', $srcPath );
1214  }
1215  }
1216 
1217  $this->unlock(); // done
1218 
1219  return $status;
1220  }
1221 
1234  function recordUpload( $oldver, $desc, $license = '', $copyStatus = '', $source = '',
1235  $watch = false, $timestamp = false, User $user = null ) {
1236  if ( !$user ) {
1237  global $wgUser;
1238  $user = $wgUser;
1239  }
1240 
1241  $pageText = SpecialUpload::getInitialPageText( $desc, $license, $copyStatus, $source );
1242 
1243  if ( !$this->recordUpload2( $oldver, $desc, $pageText, false, $timestamp, $user ) ) {
1244  return false;
1245  }
1246 
1247  if ( $watch ) {
1248  $user->addWatch( $this->getTitle() );
1249  }
1250 
1251  return true;
1252  }
1253 
1265  function recordUpload2(
1266  $oldver, $comment, $pageText, $props = false, $timestamp = false, $user = null, $tags = []
1267  ) {
1268  if ( is_null( $user ) ) {
1269  global $wgUser;
1270  $user = $wgUser;
1271  }
1272 
1273  $dbw = $this->repo->getMasterDB();
1274 
1275  # Imports or such might force a certain timestamp; otherwise we generate
1276  # it and can fudge it slightly to keep (name,timestamp) unique on re-upload.
1277  if ( $timestamp === false ) {
1278  $timestamp = $dbw->timestamp();
1279  $allowTimeKludge = true;
1280  } else {
1281  $allowTimeKludge = false;
1282  }
1283 
1284  $props = $props ?: $this->repo->getFileProps( $this->getVirtualUrl() );
1285  $props['description'] = $comment;
1286  $props['user'] = $user->getId();
1287  $props['user_text'] = $user->getName();
1288  $props['timestamp'] = wfTimestamp( TS_MW, $timestamp ); // DB -> TS_MW
1289  $this->setProps( $props );
1290 
1291  # Fail now if the file isn't there
1292  if ( !$this->fileExists ) {
1293  wfDebug( __METHOD__ . ": File " . $this->getRel() . " went missing!\n" );
1294 
1295  return false;
1296  }
1297 
1298  $dbw->startAtomic( __METHOD__ );
1299 
1300  # Test to see if the row exists using INSERT IGNORE
1301  # This avoids race conditions by locking the row until the commit, and also
1302  # doesn't deadlock. SELECT FOR UPDATE causes a deadlock for every race condition.
1303  $dbw->insert( 'image',
1304  [
1305  'img_name' => $this->getName(),
1306  'img_size' => $this->size,
1307  'img_width' => intval( $this->width ),
1308  'img_height' => intval( $this->height ),
1309  'img_bits' => $this->bits,
1310  'img_media_type' => $this->media_type,
1311  'img_major_mime' => $this->major_mime,
1312  'img_minor_mime' => $this->minor_mime,
1313  'img_timestamp' => $timestamp,
1314  'img_description' => $comment,
1315  'img_user' => $user->getId(),
1316  'img_user_text' => $user->getName(),
1317  'img_metadata' => $dbw->encodeBlob( $this->metadata ),
1318  'img_sha1' => $this->sha1
1319  ],
1320  __METHOD__,
1321  'IGNORE'
1322  );
1323 
1324  $reupload = ( $dbw->affectedRows() == 0 );
1325  if ( $reupload ) {
1326  if ( $allowTimeKludge ) {
1327  # Use LOCK IN SHARE MODE to ignore any transaction snapshotting
1328  $ltimestamp = $dbw->selectField(
1329  'image',
1330  'img_timestamp',
1331  [ 'img_name' => $this->getName() ],
1332  __METHOD__,
1333  [ 'LOCK IN SHARE MODE' ]
1334  );
1335  $lUnixtime = $ltimestamp ? wfTimestamp( TS_UNIX, $ltimestamp ) : false;
1336  # Avoid a timestamp that is not newer than the last version
1337  # TODO: the image/oldimage tables should be like page/revision with an ID field
1338  if ( $lUnixtime && wfTimestamp( TS_UNIX, $timestamp ) <= $lUnixtime ) {
1339  sleep( 1 ); // fast enough re-uploads would go far in the future otherwise
1340  $timestamp = $dbw->timestamp( $lUnixtime + 1 );
1341  $this->timestamp = wfTimestamp( TS_MW, $timestamp ); // DB -> TS_MW
1342  }
1343  }
1344 
1345  # (bug 34993) Note: $oldver can be empty here, if the previous
1346  # version of the file was broken. Allow registration of the new
1347  # version to continue anyway, because that's better than having
1348  # an image that's not fixable by user operations.
1349  # Collision, this is an update of a file
1350  # Insert previous contents into oldimage
1351  $dbw->insertSelect( 'oldimage', 'image',
1352  [
1353  'oi_name' => 'img_name',
1354  'oi_archive_name' => $dbw->addQuotes( $oldver ),
1355  'oi_size' => 'img_size',
1356  'oi_width' => 'img_width',
1357  'oi_height' => 'img_height',
1358  'oi_bits' => 'img_bits',
1359  'oi_timestamp' => 'img_timestamp',
1360  'oi_description' => 'img_description',
1361  'oi_user' => 'img_user',
1362  'oi_user_text' => 'img_user_text',
1363  'oi_metadata' => 'img_metadata',
1364  'oi_media_type' => 'img_media_type',
1365  'oi_major_mime' => 'img_major_mime',
1366  'oi_minor_mime' => 'img_minor_mime',
1367  'oi_sha1' => 'img_sha1'
1368  ],
1369  [ 'img_name' => $this->getName() ],
1370  __METHOD__
1371  );
1372 
1373  # Update the current image row
1374  $dbw->update( 'image',
1375  [
1376  'img_size' => $this->size,
1377  'img_width' => intval( $this->width ),
1378  'img_height' => intval( $this->height ),
1379  'img_bits' => $this->bits,
1380  'img_media_type' => $this->media_type,
1381  'img_major_mime' => $this->major_mime,
1382  'img_minor_mime' => $this->minor_mime,
1383  'img_timestamp' => $timestamp,
1384  'img_description' => $comment,
1385  'img_user' => $user->getId(),
1386  'img_user_text' => $user->getName(),
1387  'img_metadata' => $dbw->encodeBlob( $this->metadata ),
1388  'img_sha1' => $this->sha1
1389  ],
1390  [ 'img_name' => $this->getName() ],
1391  __METHOD__
1392  );
1393  }
1394 
1395  $descTitle = $this->getTitle();
1396  $descId = $descTitle->getArticleID();
1397  $wikiPage = new WikiFilePage( $descTitle );
1398  $wikiPage->setFile( $this );
1399 
1400  // Add the log entry...
1401  $logEntry = new ManualLogEntry( 'upload', $reupload ? 'overwrite' : 'upload' );
1402  $logEntry->setTimestamp( $this->timestamp );
1403  $logEntry->setPerformer( $user );
1404  $logEntry->setComment( $comment );
1405  $logEntry->setTarget( $descTitle );
1406  // Allow people using the api to associate log entries with the upload.
1407  // Log has a timestamp, but sometimes different from upload timestamp.
1408  $logEntry->setParameters(
1409  [
1410  'img_sha1' => $this->sha1,
1411  'img_timestamp' => $timestamp,
1412  ]
1413  );
1414  // Note we keep $logId around since during new image
1415  // creation, page doesn't exist yet, so log_page = 0
1416  // but we want it to point to the page we're making,
1417  // so we later modify the log entry.
1418  // For a similar reason, we avoid making an RC entry
1419  // now and wait until the page exists.
1420  $logId = $logEntry->insert();
1421 
1422  if ( $descTitle->exists() ) {
1423  // Use own context to get the action text in content language
1424  $formatter = LogFormatter::newFromEntry( $logEntry );
1425  $formatter->setContext( RequestContext::newExtraneousContext( $descTitle ) );
1426  $editSummary = $formatter->getPlainActionText();
1427 
1428  $nullRevision = Revision::newNullRevision(
1429  $dbw,
1430  $descId,
1431  $editSummary,
1432  false,
1433  $user
1434  );
1435  if ( $nullRevision ) {
1436  $nullRevision->insertOn( $dbw );
1437  Hooks::run(
1438  'NewRevisionFromEditComplete',
1439  [ $wikiPage, $nullRevision, $nullRevision->getParentId(), $user ]
1440  );
1441  $wikiPage->updateRevisionOn( $dbw, $nullRevision );
1442  // Associate null revision id
1443  $logEntry->setAssociatedRevId( $nullRevision->getId() );
1444  }
1445 
1446  $newPageContent = null;
1447  } else {
1448  // Make the description page and RC log entry post-commit
1449  $newPageContent = ContentHandler::makeContent( $pageText, $descTitle );
1450  }
1451 
1452  # Defer purges, page creation, and link updates in case they error out.
1453  # The most important thing is that files and the DB registry stay synced.
1454  $dbw->endAtomic( __METHOD__ );
1455 
1456  # Do some cache purges after final commit so that:
1457  # a) Changes are more likely to be seen post-purge
1458  # b) They won't cause rollback of the log publish/update above
1460  new AutoCommitUpdate(
1461  $dbw,
1462  __METHOD__,
1463  function () use (
1464  $reupload, $wikiPage, $newPageContent, $comment, $user,
1465  $logEntry, $logId, $descId, $tags
1466  ) {
1467  # Update memcache after the commit
1468  $this->invalidateCache();
1469 
1470  $updateLogPage = false;
1471  if ( $newPageContent ) {
1472  # New file page; create the description page.
1473  # There's already a log entry, so don't make a second RC entry
1474  # CDN and file cache for the description page are purged by doEditContent.
1475  $status = $wikiPage->doEditContent(
1476  $newPageContent,
1477  $comment,
1479  false,
1480  $user
1481  );
1482 
1483  if ( isset( $status->value['revision'] ) ) {
1485  $rev = $status->value['revision'];
1486  // Associate new page revision id
1487  $logEntry->setAssociatedRevId( $rev->getId() );
1488  }
1489  // This relies on the resetArticleID() call in WikiPage::insertOn(),
1490  // which is triggered on $descTitle by doEditContent() above.
1491  if ( isset( $status->value['revision'] ) ) {
1493  $rev = $status->value['revision'];
1494  $updateLogPage = $rev->getPage();
1495  }
1496  } else {
1497  # Existing file page: invalidate description page cache
1498  $wikiPage->getTitle()->invalidateCache();
1499  $wikiPage->getTitle()->purgeSquid();
1500  # Allow the new file version to be patrolled from the page footer
1502  }
1503 
1504  # Update associated rev id. This should be done by $logEntry->insert() earlier,
1505  # but setAssociatedRevId() wasn't called at that point yet...
1506  $logParams = $logEntry->getParameters();
1507  $logParams['associated_rev_id'] = $logEntry->getAssociatedRevId();
1508  $update = [ 'log_params' => LogEntryBase::makeParamBlob( $logParams ) ];
1509  if ( $updateLogPage ) {
1510  # Also log page, in case where we just created it above
1511  $update['log_page'] = $updateLogPage;
1512  }
1513  $this->getRepo()->getMasterDB()->update(
1514  'logging',
1515  $update,
1516  [ 'log_id' => $logId ],
1517  __METHOD__
1518  );
1519  $this->getRepo()->getMasterDB()->insert(
1520  'log_search',
1521  [
1522  'ls_field' => 'associated_rev_id',
1523  'ls_value' => $logEntry->getAssociatedRevId(),
1524  'ls_log_id' => $logId,
1525  ],
1526  __METHOD__
1527  );
1528 
1529  # Add change tags, if any
1530  if ( $tags ) {
1531  $logEntry->setTags( $tags );
1532  }
1533 
1534  # Uploads can be patrolled
1535  $logEntry->setIsPatrollable( true );
1536 
1537  # Now that the log entry is up-to-date, make an RC entry.
1538  $logEntry->publish( $logId );
1539 
1540  # Run hook for other updates (typically more cache purging)
1541  Hooks::run( 'FileUpload', [ $this, $reupload, !$newPageContent ] );
1542 
1543  if ( $reupload ) {
1544  # Delete old thumbnails
1545  $this->purgeThumbnails();
1546  # Remove the old file from the CDN cache
1548  new CdnCacheUpdate( [ $this->getUrl() ] ),
1550  );
1551  } else {
1552  # Update backlink pages pointing to this title if created
1553  LinksUpdate::queueRecursiveJobsForTable( $this->getTitle(), 'imagelinks' );
1554  }
1555 
1556  $this->prerenderThumbnails();
1557  }
1558  ),
1560  );
1561 
1562  if ( !$reupload ) {
1563  # This is a new file, so update the image count
1564  DeferredUpdates::addUpdate( SiteStatsUpdate::factory( [ 'images' => 1 ] ) );
1565  }
1566 
1567  # Invalidate cache for all pages using this file
1568  DeferredUpdates::addUpdate( new HTMLCacheUpdate( $this->getTitle(), 'imagelinks' ) );
1569 
1570  return true;
1571  }
1572 
1588  function publish( $src, $flags = 0, array $options = [] ) {
1589  return $this->publishTo( $src, $this->getRel(), $flags, $options );
1590  }
1591 
1607  function publishTo( $src, $dstRel, $flags = 0, array $options = [] ) {
1608  $srcPath = ( $src instanceof FSFile ) ? $src->getPath() : $src;
1609 
1610  $repo = $this->getRepo();
1611  if ( $repo->getReadOnlyReason() !== false ) {
1612  return $this->readOnlyFatalStatus();
1613  }
1614 
1615  $this->lock(); // begin
1616 
1617  $archiveName = wfTimestamp( TS_MW ) . '!' . $this->getName();
1618  $archiveRel = 'archive/' . $this->getHashPath() . $archiveName;
1619 
1620  if ( $repo->hasSha1Storage() ) {
1621  $sha1 = $repo->isVirtualUrl( $srcPath )
1622  ? $repo->getFileSha1( $srcPath )
1623  : FSFile::getSha1Base36FromPath( $srcPath );
1625  $wrapperBackend = $repo->getBackend();
1626  $dst = $wrapperBackend->getPathForSHA1( $sha1 );
1627  $status = $repo->quickImport( $src, $dst );
1628  if ( $flags & File::DELETE_SOURCE ) {
1629  unlink( $srcPath );
1630  }
1631 
1632  if ( $this->exists() ) {
1633  $status->value = $archiveName;
1634  }
1635  } else {
1637  $status = $repo->publish( $srcPath, $dstRel, $archiveRel, $flags, $options );
1638 
1639  if ( $status->value == 'new' ) {
1640  $status->value = '';
1641  } else {
1642  $status->value = $archiveName;
1643  }
1644  }
1645 
1646  $this->unlock(); // done
1647 
1648  return $status;
1649  }
1650 
1668  function move( $target ) {
1669  if ( $this->getRepo()->getReadOnlyReason() !== false ) {
1670  return $this->readOnlyFatalStatus();
1671  }
1672 
1673  wfDebugLog( 'imagemove', "Got request to move {$this->name} to " . $target->getText() );
1674  $batch = new LocalFileMoveBatch( $this, $target );
1675 
1676  $this->lock(); // begin
1677  $batch->addCurrent();
1678  $archiveNames = $batch->addOlds();
1679  $status = $batch->execute();
1680  $this->unlock(); // done
1681 
1682  wfDebugLog( 'imagemove', "Finished moving {$this->name}" );
1683 
1684  // Purge the source and target files...
1685  $oldTitleFile = wfLocalFile( $this->title );
1686  $newTitleFile = wfLocalFile( $target );
1687  // To avoid slow purges in the transaction, move them outside...
1689  new AutoCommitUpdate(
1690  $this->getRepo()->getMasterDB(),
1691  __METHOD__,
1692  function () use ( $oldTitleFile, $newTitleFile, $archiveNames ) {
1693  $oldTitleFile->purgeEverything();
1694  foreach ( $archiveNames as $archiveName ) {
1695  $oldTitleFile->purgeOldThumbnails( $archiveName );
1696  }
1697  $newTitleFile->purgeEverything();
1698  }
1699  ),
1701  );
1702 
1703  if ( $status->isOK() ) {
1704  // Now switch the object
1705  $this->title = $target;
1706  // Force regeneration of the name and hashpath
1707  unset( $this->name );
1708  unset( $this->hashPath );
1709  }
1710 
1711  return $status;
1712  }
1713 
1727  function delete( $reason, $suppress = false, $user = null ) {
1728  if ( $this->getRepo()->getReadOnlyReason() !== false ) {
1729  return $this->readOnlyFatalStatus();
1730  }
1731 
1732  $batch = new LocalFileDeleteBatch( $this, $reason, $suppress, $user );
1733 
1734  $this->lock(); // begin
1735  $batch->addCurrent();
1736  // Get old version relative paths
1737  $archiveNames = $batch->addOlds();
1738  $status = $batch->execute();
1739  $this->unlock(); // done
1740 
1741  if ( $status->isOK() ) {
1742  DeferredUpdates::addUpdate( SiteStatsUpdate::factory( [ 'images' => -1 ] ) );
1743  }
1744 
1745  // To avoid slow purges in the transaction, move them outside...
1747  new AutoCommitUpdate(
1748  $this->getRepo()->getMasterDB(),
1749  __METHOD__,
1750  function () use ( $archiveNames ) {
1751  $this->purgeEverything();
1752  foreach ( $archiveNames as $archiveName ) {
1753  $this->purgeOldThumbnails( $archiveName );
1754  }
1755  }
1756  ),
1758  );
1759 
1760  // Purge the CDN
1761  $purgeUrls = [];
1762  foreach ( $archiveNames as $archiveName ) {
1763  $purgeUrls[] = $this->getArchiveUrl( $archiveName );
1764  }
1766 
1767  return $status;
1768  }
1769 
1785  function deleteOld( $archiveName, $reason, $suppress = false, $user = null ) {
1786  if ( $this->getRepo()->getReadOnlyReason() !== false ) {
1787  return $this->readOnlyFatalStatus();
1788  }
1789 
1790  $batch = new LocalFileDeleteBatch( $this, $reason, $suppress, $user );
1791 
1792  $this->lock(); // begin
1793  $batch->addOld( $archiveName );
1794  $status = $batch->execute();
1795  $this->unlock(); // done
1796 
1797  $this->purgeOldThumbnails( $archiveName );
1798  if ( $status->isOK() ) {
1799  $this->purgeDescription();
1800  }
1801 
1803  new CdnCacheUpdate( [ $this->getArchiveUrl( $archiveName ) ] ),
1805  );
1806 
1807  return $status;
1808  }
1809 
1821  function restore( $versions = [], $unsuppress = false ) {
1822  if ( $this->getRepo()->getReadOnlyReason() !== false ) {
1823  return $this->readOnlyFatalStatus();
1824  }
1825 
1826  $batch = new LocalFileRestoreBatch( $this, $unsuppress );
1827 
1828  $this->lock(); // begin
1829  if ( !$versions ) {
1830  $batch->addAll();
1831  } else {
1832  $batch->addIds( $versions );
1833  }
1834  $status = $batch->execute();
1835  if ( $status->isGood() ) {
1836  $cleanupStatus = $batch->cleanup();
1837  $cleanupStatus->successCount = 0;
1838  $cleanupStatus->failCount = 0;
1839  $status->merge( $cleanupStatus );
1840  }
1841  $this->unlock(); // done
1842 
1843  return $status;
1844  }
1845 
1855  function getDescriptionUrl() {
1856  return $this->title->getLocalURL();
1857  }
1858 
1867  function getDescriptionText( $lang = null ) {
1868  $revision = Revision::newFromTitle( $this->title, false, Revision::READ_NORMAL );
1869  if ( !$revision ) {
1870  return false;
1871  }
1872  $content = $revision->getContent();
1873  if ( !$content ) {
1874  return false;
1875  }
1876  $pout = $content->getParserOutput( $this->title, null, new ParserOptions( null, $lang ) );
1877 
1878  return $pout->getText();
1879  }
1880 
1886  function getDescription( $audience = self::FOR_PUBLIC, User $user = null ) {
1887  $this->load();
1888  if ( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_COMMENT ) ) {
1889  return '';
1890  } elseif ( $audience == self::FOR_THIS_USER
1891  && !$this->userCan( self::DELETED_COMMENT, $user )
1892  ) {
1893  return '';
1894  } else {
1895  return $this->description;
1896  }
1897  }
1898 
1902  function getTimestamp() {
1903  $this->load();
1904 
1905  return $this->timestamp;
1906  }
1907 
1911  public function getDescriptionTouched() {
1912  // The DB lookup might return false, e.g. if the file was just deleted, or the shared DB repo
1913  // itself gets it from elsewhere. To avoid repeating the DB lookups in such a case, we
1914  // need to differentiate between null (uninitialized) and false (failed to load).
1915  if ( $this->descriptionTouched === null ) {
1916  $cond = [
1917  'page_namespace' => $this->title->getNamespace(),
1918  'page_title' => $this->title->getDBkey()
1919  ];
1920  $touched = $this->repo->getSlaveDB()->selectField( 'page', 'page_touched', $cond, __METHOD__ );
1921  $this->descriptionTouched = $touched ? wfTimestamp( TS_MW, $touched ) : false;
1922  }
1923 
1925  }
1926 
1930  function getSha1() {
1931  $this->load();
1932  // Initialise now if necessary
1933  if ( $this->sha1 == '' && $this->fileExists ) {
1934  $this->lock(); // begin
1935 
1936  $this->sha1 = $this->repo->getFileSha1( $this->getPath() );
1937  if ( !wfReadOnly() && strval( $this->sha1 ) != '' ) {
1938  $dbw = $this->repo->getMasterDB();
1939  $dbw->update( 'image',
1940  [ 'img_sha1' => $this->sha1 ],
1941  [ 'img_name' => $this->getName() ],
1942  __METHOD__ );
1943  $this->invalidateCache();
1944  }
1945 
1946  $this->unlock(); // done
1947  }
1948 
1949  return $this->sha1;
1950  }
1951 
1955  function isCacheable() {
1956  $this->load();
1957 
1958  // If extra data (metadata) was not loaded then it must have been large
1959  return $this->extraDataLoaded
1960  && strlen( serialize( $this->metadata ) ) <= self::CACHE_FIELD_MAX_LEN;
1961  }
1962 
1967  public function acquireFileLock() {
1968  return $this->getRepo()->getBackend()->lockFiles(
1969  [ $this->getPath() ], LockManager::LOCK_EX, 10
1970  );
1971  }
1972 
1977  public function releaseFileLock() {
1978  return $this->getRepo()->getBackend()->unlockFiles(
1979  [ $this->getPath() ], LockManager::LOCK_EX
1980  );
1981  }
1982 
1992  public function lock() {
1993  if ( !$this->locked ) {
1994  $logger = LoggerFactory::getInstance( 'LocalFile' );
1995 
1996  $dbw = $this->repo->getMasterDB();
1997  $makesTransaction = !$dbw->trxLevel();
1998  $dbw->startAtomic( self::ATOMIC_SECTION_LOCK );
1999  // Bug 54736: use simple lock to handle when the file does not exist.
2000  // SELECT FOR UPDATE prevents changes, not other SELECTs with FOR UPDATE.
2001  // Also, that would cause contention on INSERT of similarly named rows.
2002  $status = $this->acquireFileLock(); // represents all versions of the file
2003  if ( !$status->isGood() ) {
2004  $dbw->endAtomic( self::ATOMIC_SECTION_LOCK );
2005  $logger->warning( "Failed to lock '{file}'", [ 'file' => $this->name ] );
2006 
2007  throw new LocalFileLockError( $status );
2008  }
2009  // Release the lock *after* commit to avoid row-level contention.
2010  // Make sure it triggers on rollback() as well as commit() (T132921).
2011  $dbw->onTransactionResolution(
2012  function () use ( $logger ) {
2013  $status = $this->releaseFileLock();
2014  if ( !$status->isGood() ) {
2015  $logger->error( "Failed to unlock '{file}'", [ 'file' => $this->name ] );
2016  }
2017  },
2018  __METHOD__
2019  );
2020  // Callers might care if the SELECT snapshot is safely fresh
2021  $this->lockedOwnTrx = $makesTransaction;
2022  }
2023 
2024  $this->locked++;
2025 
2026  return $this->lockedOwnTrx;
2027  }
2028 
2037  public function unlock() {
2038  if ( $this->locked ) {
2039  --$this->locked;
2040  if ( !$this->locked ) {
2041  $dbw = $this->repo->getMasterDB();
2042  $dbw->endAtomic( self::ATOMIC_SECTION_LOCK );
2043  $this->lockedOwnTrx = false;
2044  }
2045  }
2046  }
2047 
2051  protected function readOnlyFatalStatus() {
2052  return $this->getRepo()->newFatal( 'filereadonlyerror', $this->getName(),
2053  $this->getRepo()->getName(), $this->getRepo()->getReadOnlyReason() );
2054  }
2055 
2059  function __destruct() {
2060  $this->unlock();
2061  }
2062 } // LocalFile class
2063 
2064 # ------------------------------------------------------------------------------
2065 
2072  private $file;
2073 
2075  private $reason;
2076 
2078  private $srcRels = [];
2079 
2081  private $archiveUrls = [];
2082 
2085 
2087  private $suppress;
2088 
2090  private $status;
2091 
2093  private $user;
2094 
2101  function __construct( File $file, $reason = '', $suppress = false, $user = null ) {
2102  $this->file = $file;
2103  $this->reason = $reason;
2104  $this->suppress = $suppress;
2105  if ( $user ) {
2106  $this->user = $user;
2107  } else {
2108  global $wgUser;
2109  $this->user = $wgUser;
2110  }
2111  $this->status = $file->repo->newGood();
2112  }
2113 
2114  public function addCurrent() {
2115  $this->srcRels['.'] = $this->file->getRel();
2116  }
2117 
2121  public function addOld( $oldName ) {
2122  $this->srcRels[$oldName] = $this->file->getArchiveRel( $oldName );
2123  $this->archiveUrls[] = $this->file->getArchiveUrl( $oldName );
2124  }
2125 
2130  public function addOlds() {
2131  $archiveNames = [];
2132 
2133  $dbw = $this->file->repo->getMasterDB();
2134  $result = $dbw->select( 'oldimage',
2135  [ 'oi_archive_name' ],
2136  [ 'oi_name' => $this->file->getName() ],
2137  __METHOD__
2138  );
2139 
2140  foreach ( $result as $row ) {
2141  $this->addOld( $row->oi_archive_name );
2142  $archiveNames[] = $row->oi_archive_name;
2143  }
2144 
2145  return $archiveNames;
2146  }
2147 
2151  protected function getOldRels() {
2152  if ( !isset( $this->srcRels['.'] ) ) {
2153  $oldRels =& $this->srcRels;
2154  $deleteCurrent = false;
2155  } else {
2156  $oldRels = $this->srcRels;
2157  unset( $oldRels['.'] );
2158  $deleteCurrent = true;
2159  }
2160 
2161  return [ $oldRels, $deleteCurrent ];
2162  }
2163 
2167  protected function getHashes() {
2168  $hashes = [];
2169  list( $oldRels, $deleteCurrent ) = $this->getOldRels();
2170 
2171  if ( $deleteCurrent ) {
2172  $hashes['.'] = $this->file->getSha1();
2173  }
2174 
2175  if ( count( $oldRels ) ) {
2176  $dbw = $this->file->repo->getMasterDB();
2177  $res = $dbw->select(
2178  'oldimage',
2179  [ 'oi_archive_name', 'oi_sha1' ],
2180  [ 'oi_archive_name' => array_keys( $oldRels ),
2181  'oi_name' => $this->file->getName() ], // performance
2182  __METHOD__
2183  );
2184 
2185  foreach ( $res as $row ) {
2186  if ( rtrim( $row->oi_sha1, "\0" ) === '' ) {
2187  // Get the hash from the file
2188  $oldUrl = $this->file->getArchiveVirtualUrl( $row->oi_archive_name );
2189  $props = $this->file->repo->getFileProps( $oldUrl );
2190 
2191  if ( $props['fileExists'] ) {
2192  // Upgrade the oldimage row
2193  $dbw->update( 'oldimage',
2194  [ 'oi_sha1' => $props['sha1'] ],
2195  [ 'oi_name' => $this->file->getName(), 'oi_archive_name' => $row->oi_archive_name ],
2196  __METHOD__ );
2197  $hashes[$row->oi_archive_name] = $props['sha1'];
2198  } else {
2199  $hashes[$row->oi_archive_name] = false;
2200  }
2201  } else {
2202  $hashes[$row->oi_archive_name] = $row->oi_sha1;
2203  }
2204  }
2205  }
2206 
2207  $missing = array_diff_key( $this->srcRels, $hashes );
2208 
2209  foreach ( $missing as $name => $rel ) {
2210  $this->status->error( 'filedelete-old-unregistered', $name );
2211  }
2212 
2213  foreach ( $hashes as $name => $hash ) {
2214  if ( !$hash ) {
2215  $this->status->error( 'filedelete-missing', $this->srcRels[$name] );
2216  unset( $hashes[$name] );
2217  }
2218  }
2219 
2220  return $hashes;
2221  }
2222 
2223  protected function doDBInserts() {
2224  $now = time();
2225  $dbw = $this->file->repo->getMasterDB();
2226  $encTimestamp = $dbw->addQuotes( $dbw->timestamp( $now ) );
2227  $encUserId = $dbw->addQuotes( $this->user->getId() );
2228  $encReason = $dbw->addQuotes( $this->reason );
2229  $encGroup = $dbw->addQuotes( 'deleted' );
2230  $ext = $this->file->getExtension();
2231  $dotExt = $ext === '' ? '' : ".$ext";
2232  $encExt = $dbw->addQuotes( $dotExt );
2233  list( $oldRels, $deleteCurrent ) = $this->getOldRels();
2234 
2235  // Bitfields to further suppress the content
2236  if ( $this->suppress ) {
2237  $bitfield = 0;
2238  // This should be 15...
2239  $bitfield |= Revision::DELETED_TEXT;
2240  $bitfield |= Revision::DELETED_COMMENT;
2241  $bitfield |= Revision::DELETED_USER;
2242  $bitfield |= Revision::DELETED_RESTRICTED;
2243  } else {
2244  $bitfield = 'oi_deleted';
2245  }
2246 
2247  if ( $deleteCurrent ) {
2248  $dbw->insertSelect(
2249  'filearchive',
2250  'image',
2251  [
2252  'fa_storage_group' => $encGroup,
2253  'fa_storage_key' => $dbw->conditional(
2254  [ 'img_sha1' => '' ],
2255  $dbw->addQuotes( '' ),
2256  $dbw->buildConcat( [ "img_sha1", $encExt ] )
2257  ),
2258  'fa_deleted_user' => $encUserId,
2259  'fa_deleted_timestamp' => $encTimestamp,
2260  'fa_deleted_reason' => $encReason,
2261  'fa_deleted' => $this->suppress ? $bitfield : 0,
2262 
2263  'fa_name' => 'img_name',
2264  'fa_archive_name' => 'NULL',
2265  'fa_size' => 'img_size',
2266  'fa_width' => 'img_width',
2267  'fa_height' => 'img_height',
2268  'fa_metadata' => 'img_metadata',
2269  'fa_bits' => 'img_bits',
2270  'fa_media_type' => 'img_media_type',
2271  'fa_major_mime' => 'img_major_mime',
2272  'fa_minor_mime' => 'img_minor_mime',
2273  'fa_description' => 'img_description',
2274  'fa_user' => 'img_user',
2275  'fa_user_text' => 'img_user_text',
2276  'fa_timestamp' => 'img_timestamp',
2277  'fa_sha1' => 'img_sha1'
2278  ],
2279  [ 'img_name' => $this->file->getName() ],
2280  __METHOD__
2281  );
2282  }
2283 
2284  if ( count( $oldRels ) ) {
2285  $res = $dbw->select(
2286  'oldimage',
2288  [
2289  'oi_name' => $this->file->getName(),
2290  'oi_archive_name' => array_keys( $oldRels )
2291  ],
2292  __METHOD__,
2293  [ 'FOR UPDATE' ]
2294  );
2295  $rowsInsert = [];
2296  foreach ( $res as $row ) {
2297  $rowsInsert[] = [
2298  // Deletion-specific fields
2299  'fa_storage_group' => 'deleted',
2300  'fa_storage_key' => ( $row->oi_sha1 === '' )
2301  ? ''
2302  : "{$row->oi_sha1}{$dotExt}",
2303  'fa_deleted_user' => $this->user->getId(),
2304  'fa_deleted_timestamp' => $dbw->timestamp( $now ),
2305  'fa_deleted_reason' => $this->reason,
2306  // Counterpart fields
2307  'fa_deleted' => $this->suppress ? $bitfield : $row->oi_deleted,
2308  'fa_name' => $row->oi_name,
2309  'fa_archive_name' => $row->oi_archive_name,
2310  'fa_size' => $row->oi_size,
2311  'fa_width' => $row->oi_width,
2312  'fa_height' => $row->oi_height,
2313  'fa_metadata' => $row->oi_metadata,
2314  'fa_bits' => $row->oi_bits,
2315  'fa_media_type' => $row->oi_media_type,
2316  'fa_major_mime' => $row->oi_major_mime,
2317  'fa_minor_mime' => $row->oi_minor_mime,
2318  'fa_description' => $row->oi_description,
2319  'fa_user' => $row->oi_user,
2320  'fa_user_text' => $row->oi_user_text,
2321  'fa_timestamp' => $row->oi_timestamp,
2322  'fa_sha1' => $row->oi_sha1
2323  ];
2324  }
2325 
2326  $dbw->insert( 'filearchive', $rowsInsert, __METHOD__ );
2327  }
2328  }
2329 
2330  function doDBDeletes() {
2331  $dbw = $this->file->repo->getMasterDB();
2332  list( $oldRels, $deleteCurrent ) = $this->getOldRels();
2333 
2334  if ( count( $oldRels ) ) {
2335  $dbw->delete( 'oldimage',
2336  [
2337  'oi_name' => $this->file->getName(),
2338  'oi_archive_name' => array_keys( $oldRels )
2339  ], __METHOD__ );
2340  }
2341 
2342  if ( $deleteCurrent ) {
2343  $dbw->delete( 'image', [ 'img_name' => $this->file->getName() ], __METHOD__ );
2344  }
2345  }
2346 
2351  public function execute() {
2352  $repo = $this->file->getRepo();
2353  $this->file->lock();
2354 
2355  // Prepare deletion batch
2356  $hashes = $this->getHashes();
2357  $this->deletionBatch = [];
2358  $ext = $this->file->getExtension();
2359  $dotExt = $ext === '' ? '' : ".$ext";
2360 
2361  foreach ( $this->srcRels as $name => $srcRel ) {
2362  // Skip files that have no hash (e.g. missing DB record, or sha1 field and file source)
2363  if ( isset( $hashes[$name] ) ) {
2364  $hash = $hashes[$name];
2365  $key = $hash . $dotExt;
2366  $dstRel = $repo->getDeletedHashPath( $key ) . $key;
2367  $this->deletionBatch[$name] = [ $srcRel, $dstRel ];
2368  }
2369  }
2370 
2371  if ( !$repo->hasSha1Storage() ) {
2372  // Removes non-existent file from the batch, so we don't get errors.
2373  // This also handles files in the 'deleted' zone deleted via revision deletion.
2374  $checkStatus = $this->removeNonexistentFiles( $this->deletionBatch );
2375  if ( !$checkStatus->isGood() ) {
2376  $this->status->merge( $checkStatus );
2377  return $this->status;
2378  }
2379  $this->deletionBatch = $checkStatus->value;
2380 
2381  // Execute the file deletion batch
2382  $status = $this->file->repo->deleteBatch( $this->deletionBatch );
2383  if ( !$status->isGood() ) {
2384  $this->status->merge( $status );
2385  }
2386  }
2387 
2388  if ( !$this->status->isOK() ) {
2389  // Critical file deletion error; abort
2390  $this->file->unlock();
2391 
2392  return $this->status;
2393  }
2394 
2395  // Copy the image/oldimage rows to filearchive
2396  $this->doDBInserts();
2397  // Delete image/oldimage rows
2398  $this->doDBDeletes();
2399 
2400  // Commit and return
2401  $this->file->unlock();
2402 
2403  return $this->status;
2404  }
2405 
2411  protected function removeNonexistentFiles( $batch ) {
2412  $files = $newBatch = [];
2413 
2414  foreach ( $batch as $batchItem ) {
2415  list( $src, ) = $batchItem;
2416  $files[$src] = $this->file->repo->getVirtualUrl( 'public' ) . '/' . rawurlencode( $src );
2417  }
2418 
2419  $result = $this->file->repo->fileExistsBatch( $files );
2420  if ( in_array( null, $result, true ) ) {
2421  return Status::newFatal( 'backend-fail-internal',
2422  $this->file->repo->getBackend()->getName() );
2423  }
2424 
2425  foreach ( $batch as $batchItem ) {
2426  if ( $result[$batchItem[0]] ) {
2427  $newBatch[] = $batchItem;
2428  }
2429  }
2430 
2431  return Status::newGood( $newBatch );
2432  }
2433 }
2434 
2435 # ------------------------------------------------------------------------------
2436 
2443  private $file;
2444 
2446  private $cleanupBatch;
2447 
2449  private $ids;
2450 
2452  private $all;
2453 
2455  private $unsuppress = false;
2456 
2461  function __construct( File $file, $unsuppress = false ) {
2462  $this->file = $file;
2463  $this->cleanupBatch = $this->ids = [];
2464  $this->ids = [];
2465  $this->unsuppress = $unsuppress;
2466  }
2467 
2472  public function addId( $fa_id ) {
2473  $this->ids[] = $fa_id;
2474  }
2475 
2480  public function addIds( $ids ) {
2481  $this->ids = array_merge( $this->ids, $ids );
2482  }
2483 
2487  public function addAll() {
2488  $this->all = true;
2489  }
2490 
2499  public function execute() {
2501  global $wgLang;
2502 
2503  $repo = $this->file->getRepo();
2504  if ( !$this->all && !$this->ids ) {
2505  // Do nothing
2506  return $repo->newGood();
2507  }
2508 
2509  $lockOwnsTrx = $this->file->lock();
2510 
2511  $dbw = $this->file->repo->getMasterDB();
2512  $status = $this->file->repo->newGood();
2513 
2514  $exists = (bool)$dbw->selectField( 'image', '1',
2515  [ 'img_name' => $this->file->getName() ],
2516  __METHOD__,
2517  // The lock() should already prevents changes, but this still may need
2518  // to bypass any transaction snapshot. However, if lock() started the
2519  // trx (which it probably did) then snapshot is post-lock and up-to-date.
2520  $lockOwnsTrx ? [] : [ 'LOCK IN SHARE MODE' ]
2521  );
2522 
2523  // Fetch all or selected archived revisions for the file,
2524  // sorted from the most recent to the oldest.
2525  $conditions = [ 'fa_name' => $this->file->getName() ];
2526 
2527  if ( !$this->all ) {
2528  $conditions['fa_id'] = $this->ids;
2529  }
2530 
2531  $result = $dbw->select(
2532  'filearchive',
2534  $conditions,
2535  __METHOD__,
2536  [ 'ORDER BY' => 'fa_timestamp DESC' ]
2537  );
2538 
2539  $idsPresent = [];
2540  $storeBatch = [];
2541  $insertBatch = [];
2542  $insertCurrent = false;
2543  $deleteIds = [];
2544  $first = true;
2545  $archiveNames = [];
2546 
2547  foreach ( $result as $row ) {
2548  $idsPresent[] = $row->fa_id;
2549 
2550  if ( $row->fa_name != $this->file->getName() ) {
2551  $status->error( 'undelete-filename-mismatch', $wgLang->timeanddate( $row->fa_timestamp ) );
2552  $status->failCount++;
2553  continue;
2554  }
2555 
2556  if ( $row->fa_storage_key == '' ) {
2557  // Revision was missing pre-deletion
2558  $status->error( 'undelete-bad-store-key', $wgLang->timeanddate( $row->fa_timestamp ) );
2559  $status->failCount++;
2560  continue;
2561  }
2562 
2563  $deletedRel = $repo->getDeletedHashPath( $row->fa_storage_key ) .
2564  $row->fa_storage_key;
2565  $deletedUrl = $repo->getVirtualUrl() . '/deleted/' . $deletedRel;
2566 
2567  if ( isset( $row->fa_sha1 ) ) {
2568  $sha1 = $row->fa_sha1;
2569  } else {
2570  // old row, populate from key
2571  $sha1 = LocalRepo::getHashFromKey( $row->fa_storage_key );
2572  }
2573 
2574  # Fix leading zero
2575  if ( strlen( $sha1 ) == 32 && $sha1[0] == '0' ) {
2576  $sha1 = substr( $sha1, 1 );
2577  }
2578 
2579  if ( is_null( $row->fa_major_mime ) || $row->fa_major_mime == 'unknown'
2580  || is_null( $row->fa_minor_mime ) || $row->fa_minor_mime == 'unknown'
2581  || is_null( $row->fa_media_type ) || $row->fa_media_type == 'UNKNOWN'
2582  || is_null( $row->fa_metadata )
2583  ) {
2584  // Refresh our metadata
2585  // Required for a new current revision; nice for older ones too. :)
2586  $props = RepoGroup::singleton()->getFileProps( $deletedUrl );
2587  } else {
2588  $props = [
2589  'minor_mime' => $row->fa_minor_mime,
2590  'major_mime' => $row->fa_major_mime,
2591  'media_type' => $row->fa_media_type,
2592  'metadata' => $row->fa_metadata
2593  ];
2594  }
2595 
2596  if ( $first && !$exists ) {
2597  // This revision will be published as the new current version
2598  $destRel = $this->file->getRel();
2599  $insertCurrent = [
2600  'img_name' => $row->fa_name,
2601  'img_size' => $row->fa_size,
2602  'img_width' => $row->fa_width,
2603  'img_height' => $row->fa_height,
2604  'img_metadata' => $props['metadata'],
2605  'img_bits' => $row->fa_bits,
2606  'img_media_type' => $props['media_type'],
2607  'img_major_mime' => $props['major_mime'],
2608  'img_minor_mime' => $props['minor_mime'],
2609  'img_description' => $row->fa_description,
2610  'img_user' => $row->fa_user,
2611  'img_user_text' => $row->fa_user_text,
2612  'img_timestamp' => $row->fa_timestamp,
2613  'img_sha1' => $sha1
2614  ];
2615 
2616  // The live (current) version cannot be hidden!
2617  if ( !$this->unsuppress && $row->fa_deleted ) {
2618  $status->fatal( 'undeleterevdel' );
2619  $this->file->unlock();
2620  return $status;
2621  }
2622  } else {
2623  $archiveName = $row->fa_archive_name;
2624 
2625  if ( $archiveName == '' ) {
2626  // This was originally a current version; we
2627  // have to devise a new archive name for it.
2628  // Format is <timestamp of archiving>!<name>
2629  $timestamp = wfTimestamp( TS_UNIX, $row->fa_deleted_timestamp );
2630 
2631  do {
2632  $archiveName = wfTimestamp( TS_MW, $timestamp ) . '!' . $row->fa_name;
2633  $timestamp++;
2634  } while ( isset( $archiveNames[$archiveName] ) );
2635  }
2636 
2637  $archiveNames[$archiveName] = true;
2638  $destRel = $this->file->getArchiveRel( $archiveName );
2639  $insertBatch[] = [
2640  'oi_name' => $row->fa_name,
2641  'oi_archive_name' => $archiveName,
2642  'oi_size' => $row->fa_size,
2643  'oi_width' => $row->fa_width,
2644  'oi_height' => $row->fa_height,
2645  'oi_bits' => $row->fa_bits,
2646  'oi_description' => $row->fa_description,
2647  'oi_user' => $row->fa_user,
2648  'oi_user_text' => $row->fa_user_text,
2649  'oi_timestamp' => $row->fa_timestamp,
2650  'oi_metadata' => $props['metadata'],
2651  'oi_media_type' => $props['media_type'],
2652  'oi_major_mime' => $props['major_mime'],
2653  'oi_minor_mime' => $props['minor_mime'],
2654  'oi_deleted' => $this->unsuppress ? 0 : $row->fa_deleted,
2655  'oi_sha1' => $sha1 ];
2656  }
2657 
2658  $deleteIds[] = $row->fa_id;
2659 
2660  if ( !$this->unsuppress && $row->fa_deleted & File::DELETED_FILE ) {
2661  // private files can stay where they are
2662  $status->successCount++;
2663  } else {
2664  $storeBatch[] = [ $deletedUrl, 'public', $destRel ];
2665  $this->cleanupBatch[] = $row->fa_storage_key;
2666  }
2667 
2668  $first = false;
2669  }
2670 
2671  unset( $result );
2672 
2673  // Add a warning to the status object for missing IDs
2674  $missingIds = array_diff( $this->ids, $idsPresent );
2675 
2676  foreach ( $missingIds as $id ) {
2677  $status->error( 'undelete-missing-filearchive', $id );
2678  }
2679 
2680  if ( !$repo->hasSha1Storage() ) {
2681  // Remove missing files from batch, so we don't get errors when undeleting them
2682  $checkStatus = $this->removeNonexistentFiles( $storeBatch );
2683  if ( !$checkStatus->isGood() ) {
2684  $status->merge( $checkStatus );
2685  return $status;
2686  }
2687  $storeBatch = $checkStatus->value;
2688 
2689  // Run the store batch
2690  // Use the OVERWRITE_SAME flag to smooth over a common error
2691  $storeStatus = $this->file->repo->storeBatch( $storeBatch, FileRepo::OVERWRITE_SAME );
2692  $status->merge( $storeStatus );
2693 
2694  if ( !$status->isGood() ) {
2695  // Even if some files could be copied, fail entirely as that is the
2696  // easiest thing to do without data loss
2697  $this->cleanupFailedBatch( $storeStatus, $storeBatch );
2698  $status->setOK( false );
2699  $this->file->unlock();
2700 
2701  return $status;
2702  }
2703  }
2704 
2705  // Run the DB updates
2706  // Because we have locked the image row, key conflicts should be rare.
2707  // If they do occur, we can roll back the transaction at this time with
2708  // no data loss, but leaving unregistered files scattered throughout the
2709  // public zone.
2710  // This is not ideal, which is why it's important to lock the image row.
2711  if ( $insertCurrent ) {
2712  $dbw->insert( 'image', $insertCurrent, __METHOD__ );
2713  }
2714 
2715  if ( $insertBatch ) {
2716  $dbw->insert( 'oldimage', $insertBatch, __METHOD__ );
2717  }
2718 
2719  if ( $deleteIds ) {
2720  $dbw->delete( 'filearchive',
2721  [ 'fa_id' => $deleteIds ],
2722  __METHOD__ );
2723  }
2724 
2725  // If store batch is empty (all files are missing), deletion is to be considered successful
2726  if ( $status->successCount > 0 || !$storeBatch || $repo->hasSha1Storage() ) {
2727  if ( !$exists ) {
2728  wfDebug( __METHOD__ . " restored {$status->successCount} items, creating a new current\n" );
2729 
2730  DeferredUpdates::addUpdate( SiteStatsUpdate::factory( [ 'images' => 1 ] ) );
2731 
2732  $this->file->purgeEverything();
2733  } else {
2734  wfDebug( __METHOD__ . " restored {$status->successCount} as archived versions\n" );
2735  $this->file->purgeDescription();
2736  }
2737  }
2738 
2739  $this->file->unlock();
2740 
2741  return $status;
2742  }
2743 
2749  protected function removeNonexistentFiles( $triplets ) {
2750  $files = $filteredTriplets = [];
2751  foreach ( $triplets as $file ) {
2752  $files[$file[0]] = $file[0];
2753  }
2754 
2755  $result = $this->file->repo->fileExistsBatch( $files );
2756  if ( in_array( null, $result, true ) ) {
2757  return Status::newFatal( 'backend-fail-internal',
2758  $this->file->repo->getBackend()->getName() );
2759  }
2760 
2761  foreach ( $triplets as $file ) {
2762  if ( $result[$file[0]] ) {
2763  $filteredTriplets[] = $file;
2764  }
2765  }
2766 
2767  return Status::newGood( $filteredTriplets );
2768  }
2769 
2775  protected function removeNonexistentFromCleanup( $batch ) {
2776  $files = $newBatch = [];
2777  $repo = $this->file->repo;
2778 
2779  foreach ( $batch as $file ) {
2780  $files[$file] = $repo->getVirtualUrl( 'deleted' ) . '/' .
2781  rawurlencode( $repo->getDeletedHashPath( $file ) . $file );
2782  }
2783 
2784  $result = $repo->fileExistsBatch( $files );
2785 
2786  foreach ( $batch as $file ) {
2787  if ( $result[$file] ) {
2788  $newBatch[] = $file;
2789  }
2790  }
2791 
2792  return $newBatch;
2793  }
2794 
2800  public function cleanup() {
2801  if ( !$this->cleanupBatch ) {
2802  return $this->file->repo->newGood();
2803  }
2804 
2805  $this->cleanupBatch = $this->removeNonexistentFromCleanup( $this->cleanupBatch );
2806 
2807  $status = $this->file->repo->cleanupDeletedBatch( $this->cleanupBatch );
2808 
2809  return $status;
2810  }
2811 
2819  protected function cleanupFailedBatch( $storeStatus, $storeBatch ) {
2820  $cleanupBatch = [];
2821 
2822  foreach ( $storeStatus->success as $i => $success ) {
2823  // Check if this item of the batch was successfully copied
2824  if ( $success ) {
2825  // Item was successfully copied and needs to be removed again
2826  // Extract ($dstZone, $dstRel) from the batch
2827  $cleanupBatch[] = [ $storeBatch[$i][1], $storeBatch[$i][2] ];
2828  }
2829  }
2830  $this->file->repo->cleanupBatch( $cleanupBatch );
2831  }
2832 }
2833 
2834 # ------------------------------------------------------------------------------
2835 
2842  protected $file;
2843 
2845  protected $target;
2846 
2847  protected $cur;
2848 
2849  protected $olds;
2850 
2851  protected $oldCount;
2852 
2853  protected $archive;
2854 
2856  protected $db;
2857 
2863  $this->file = $file;
2864  $this->target = $target;
2865  $this->oldHash = $this->file->repo->getHashPath( $this->file->getName() );
2866  $this->newHash = $this->file->repo->getHashPath( $this->target->getDBkey() );
2867  $this->oldName = $this->file->getName();
2868  $this->newName = $this->file->repo->getNameFromTitle( $this->target );
2869  $this->oldRel = $this->oldHash . $this->oldName;
2870  $this->newRel = $this->newHash . $this->newName;
2871  $this->db = $file->getRepo()->getMasterDB();
2872  }
2873 
2877  public function addCurrent() {
2878  $this->cur = [ $this->oldRel, $this->newRel ];
2879  }
2880 
2885  public function addOlds() {
2886  $archiveBase = 'archive';
2887  $this->olds = [];
2888  $this->oldCount = 0;
2889  $archiveNames = [];
2890 
2891  $result = $this->db->select( 'oldimage',
2892  [ 'oi_archive_name', 'oi_deleted' ],
2893  [ 'oi_name' => $this->oldName ],
2894  __METHOD__,
2895  [ 'LOCK IN SHARE MODE' ] // ignore snapshot
2896  );
2897 
2898  foreach ( $result as $row ) {
2899  $archiveNames[] = $row->oi_archive_name;
2900  $oldName = $row->oi_archive_name;
2901  $bits = explode( '!', $oldName, 2 );
2902 
2903  if ( count( $bits ) != 2 ) {
2904  wfDebug( "Old file name missing !: '$oldName' \n" );
2905  continue;
2906  }
2907 
2908  list( $timestamp, $filename ) = $bits;
2909 
2910  if ( $this->oldName != $filename ) {
2911  wfDebug( "Old file name doesn't match: '$oldName' \n" );
2912  continue;
2913  }
2914 
2915  $this->oldCount++;
2916 
2917  // Do we want to add those to oldCount?
2918  if ( $row->oi_deleted & File::DELETED_FILE ) {
2919  continue;
2920  }
2921 
2922  $this->olds[] = [
2923  "{$archiveBase}/{$this->oldHash}{$oldName}",
2924  "{$archiveBase}/{$this->newHash}{$timestamp}!{$this->newName}"
2925  ];
2926  }
2927 
2928  return $archiveNames;
2929  }
2930 
2935  public function execute() {
2936  $repo = $this->file->repo;
2937  $status = $repo->newGood();
2938  $destFile = wfLocalFile( $this->target );
2939 
2940  $this->file->lock(); // begin
2941  $destFile->lock(); // quickly fail if destination is not available
2942 
2943  $triplets = $this->getMoveTriplets();
2944  $checkStatus = $this->removeNonexistentFiles( $triplets );
2945  if ( !$checkStatus->isGood() ) {
2946  $destFile->unlock();
2947  $this->file->unlock();
2948  $status->merge( $checkStatus ); // couldn't talk to file backend
2949  return $status;
2950  }
2951  $triplets = $checkStatus->value;
2952 
2953  // Verify the file versions metadata in the DB.
2954  $statusDb = $this->verifyDBUpdates();
2955  if ( !$statusDb->isGood() ) {
2956  $destFile->unlock();
2957  $this->file->unlock();
2958  $statusDb->setOK( false );
2959 
2960  return $statusDb;
2961  }
2962 
2963  if ( !$repo->hasSha1Storage() ) {
2964  // Copy the files into their new location.
2965  // If a prior process fataled copying or cleaning up files we tolerate any
2966  // of the existing files if they are identical to the ones being stored.
2967  $statusMove = $repo->storeBatch( $triplets, FileRepo::OVERWRITE_SAME );
2968  wfDebugLog( 'imagemove', "Moved files for {$this->file->getName()}: " .
2969  "{$statusMove->successCount} successes, {$statusMove->failCount} failures" );
2970  if ( !$statusMove->isGood() ) {
2971  // Delete any files copied over (while the destination is still locked)
2972  $this->cleanupTarget( $triplets );
2973  $destFile->unlock();
2974  $this->file->unlock();
2975  wfDebugLog( 'imagemove', "Error in moving files: "
2976  . $statusMove->getWikiText( false, false, 'en' ) );
2977  $statusMove->setOK( false );
2978 
2979  return $statusMove;
2980  }
2981  $status->merge( $statusMove );
2982  }
2983 
2984  // Rename the file versions metadata in the DB.
2985  $this->doDBUpdates();
2986 
2987  wfDebugLog( 'imagemove', "Renamed {$this->file->getName()} in database: " .
2988  "{$statusDb->successCount} successes, {$statusDb->failCount} failures" );
2989 
2990  $destFile->unlock();
2991  $this->file->unlock(); // done
2992 
2993  // Everything went ok, remove the source files
2994  $this->cleanupSource( $triplets );
2995 
2996  $status->merge( $statusDb );
2997 
2998  return $status;
2999  }
3000 
3007  protected function verifyDBUpdates() {
3008  $repo = $this->file->repo;
3009  $status = $repo->newGood();
3010  $dbw = $this->db;
3011 
3012  $hasCurrent = $dbw->selectField(
3013  'image',
3014  '1',
3015  [ 'img_name' => $this->oldName ],
3016  __METHOD__,
3017  [ 'FOR UPDATE' ]
3018  );
3019  $oldRowCount = $dbw->selectField(
3020  'oldimage',
3021  'COUNT(*)',
3022  [ 'oi_name' => $this->oldName ],
3023  __METHOD__,
3024  [ 'FOR UPDATE' ]
3025  );
3026 
3027  if ( $hasCurrent ) {
3028  $status->successCount++;
3029  } else {
3030  $status->failCount++;
3031  }
3032  $status->successCount += $oldRowCount;
3033  // Bug 34934: oldCount is based on files that actually exist.
3034  // There may be more DB rows than such files, in which case $affected
3035  // can be greater than $total. We use max() to avoid negatives here.
3036  $status->failCount += max( 0, $this->oldCount - $oldRowCount );
3037  if ( $status->failCount ) {
3038  $status->error( 'imageinvalidfilename' );
3039  }
3040 
3041  return $status;
3042  }
3043 
3048  protected function doDBUpdates() {
3049  $dbw = $this->db;
3050 
3051  // Update current image
3052  $dbw->update(
3053  'image',
3054  [ 'img_name' => $this->newName ],
3055  [ 'img_name' => $this->oldName ],
3056  __METHOD__
3057  );
3058  // Update old images
3059  $dbw->update(
3060  'oldimage',
3061  [
3062  'oi_name' => $this->newName,
3063  'oi_archive_name = ' . $dbw->strreplace( 'oi_archive_name',
3064  $dbw->addQuotes( $this->oldName ), $dbw->addQuotes( $this->newName ) ),
3065  ],
3066  [ 'oi_name' => $this->oldName ],
3067  __METHOD__
3068  );
3069  }
3070 
3075  protected function getMoveTriplets() {
3076  $moves = array_merge( [ $this->cur ], $this->olds );
3077  $triplets = []; // The format is: (srcUrl, destZone, destUrl)
3078 
3079  foreach ( $moves as $move ) {
3080  // $move: (oldRelativePath, newRelativePath)
3081  $srcUrl = $this->file->repo->getVirtualUrl() . '/public/' . rawurlencode( $move[0] );
3082  $triplets[] = [ $srcUrl, 'public', $move[1] ];
3083  wfDebugLog(
3084  'imagemove',
3085  "Generated move triplet for {$this->file->getName()}: {$srcUrl} :: public :: {$move[1]}"
3086  );
3087  }
3088 
3089  return $triplets;
3090  }
3091 
3097  protected function removeNonexistentFiles( $triplets ) {
3098  $files = [];
3099 
3100  foreach ( $triplets as $file ) {
3101  $files[$file[0]] = $file[0];
3102  }
3103 
3104  $result = $this->file->repo->fileExistsBatch( $files );
3105  if ( in_array( null, $result, true ) ) {
3106  return Status::newFatal( 'backend-fail-internal',
3107  $this->file->repo->getBackend()->getName() );
3108  }
3109 
3110  $filteredTriplets = [];
3111  foreach ( $triplets as $file ) {
3112  if ( $result[$file[0]] ) {
3113  $filteredTriplets[] = $file;
3114  } else {
3115  wfDebugLog( 'imagemove', "File {$file[0]} does not exist" );
3116  }
3117  }
3118 
3119  return Status::newGood( $filteredTriplets );
3120  }
3121 
3127  protected function cleanupTarget( $triplets ) {
3128  // Create dest pairs from the triplets
3129  $pairs = [];
3130  foreach ( $triplets as $triplet ) {
3131  // $triplet: (old source virtual URL, dst zone, dest rel)
3132  $pairs[] = [ $triplet[1], $triplet[2] ];
3133  }
3134 
3135  $this->file->repo->cleanupBatch( $pairs );
3136  }
3137 
3143  protected function cleanupSource( $triplets ) {
3144  // Create source file names from the triplets
3145  $files = [];
3146  foreach ( $triplets as $triplet ) {
3147  $files[] = $triplet[0];
3148  }
3149 
3150  $this->file->repo->cleanupBatch( $files );
3151  }
3152 }
3153 
3155  public function __construct( Status $status ) {
3156  parent::__construct(
3157  'actionfailed',
3158  $status->getMessage()
3159  );
3160  }
3161 
3162  public function report() {
3163  global $wgOut;
3164  $wgOut->setStatusCode( 429 );
3165  parent::report();
3166  }
3167 }
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:1222
removeNonexistentFiles($batch)
Removes non-existent files from a deletion batch.
Definition: LocalFile.php:2411
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:1234
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:2487
cleanupTarget($triplets)
Cleanup a partially moved array of triplets by deleting the target files.
Definition: LocalFile.php:3127
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:3143
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:1821
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:2102
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:3025
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:1977
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:2101
cleanup()
Delete unused files in the deleted zone.
Definition: LocalFile.php:2800
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:1265
addCurrent()
Add the current image to the batch.
Definition: LocalFile.php:2877
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:3007
$comment
Helper class for file undeletion.
Definition: LocalFile.php:2441
unlock()
Decrement the lock reference count and end the atomic section if it reaches zero. ...
Definition: LocalFile.php:2037
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:2703
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:2446
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:1588
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:3048
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:1007
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:2059
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:1934
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:1090
wfTimestamp($outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
__construct(File $file, Title $target)
Definition: LocalFile.php:2862
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:3097
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:1785
getReadOnlyReason()
Get an explanatory message if this repo is read-only.
Definition: FileRepo.php:225
__construct(File $file, $unsuppress=false)
Definition: LocalFile.php:2461
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:1607
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:1046
getDescriptionTouched()
Definition: LocalFile.php:1911
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:2455
$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:1967
__construct($title, $repo)
Constructor.
Definition: LocalFile.php:221
const CACHE_FIELD_MAX_LEN
Definition: LocalFile.php:46
__construct(Status $status)
Definition: LocalFile.php:3155
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:1166
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:2051
Helper class for file movement.
Definition: LocalFile.php:2840
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:2351
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:2452
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:2449
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:1721
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:2935
Helper class for file deletion.
Definition: LocalFile.php:2070
array $deletionBatch
Items to be processed in the deletion batch.
Definition: LocalFile.php:2084
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:2090
static singleton($wiki=false)
removeNonexistentFiles($triplets)
Removes non-existent files from a store batch.
Definition: LocalFile.php:2749
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:2775
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:2819
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:1992
getDescriptionText($lang=null)
Get the HTML text of the description page This is not used by ImagePage for local files...
Definition: LocalFile.php:1867
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:1046
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:1668
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:1855
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:1046
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:1886
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:2885
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:1046
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:3075
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:2087
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:1128
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:2130
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:2491
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:2480
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:2491
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:2499
$wgUser
Definition: Setup.php:806
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:300
addId($fa_id)
Add a file by ID.
Definition: LocalFile.php:2472