3 * Local file in the wiki's own database.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup FileAbstraction
24 use \MediaWiki\Logger\LoggerFactory
;
27 * Class to represent a local file in the wiki's own database
29 * Provides methods to retrieve paths (physical, logical, URL),
30 * to generate image thumbnails or for uploading.
32 * Note that only the repo object knows what its file class is called. You should
33 * never name a file class explictly outside of the repo class. Instead use the
34 * repo's factory functions to generate file objects, for example:
36 * RepoGroup::singleton()->getLocalRepo()->newFile( $title );
38 * The convenience functions wfLocalFile() and wfFindFile() should be sufficient
41 * @ingroup FileAbstraction
43 class LocalFile
extends File
{
44 const VERSION
= 10; // cache version
46 const CACHE_FIELD_MAX_LEN
= 1000;
48 /** @var bool Does the file exist on disk? (loadFromXxx) */
49 protected $fileExists;
51 /** @var int Image width */
54 /** @var int Image height */
57 /** @var int Returned by getimagesize (loadFromXxx) */
60 /** @var string MEDIATYPE_xxx (bitmap, drawing, audio...) */
61 protected $media_type;
63 /** @var string MIME type, determined by MimeMagic::guessMimeType */
66 /** @var int Size in bytes (loadFromXxx) */
69 /** @var string Handler-specific metadata */
72 /** @var string SHA-1 base 36 content hash */
75 /** @var bool Whether or not core data has been loaded from the database (loadFromXxx) */
76 protected $dataLoaded;
78 /** @var bool Whether or not lazy-loaded data has been loaded from the database */
79 protected $extraDataLoaded;
81 /** @var int Bitfield akin to rev_deleted */
85 protected $repoClass = 'LocalRepo';
87 /** @var int Number of line to return by nextHistoryLine() (constructor) */
90 /** @var int Result of the query for the file's history (nextHistoryLine) */
93 /** @var string Major MIME type */
96 /** @var string Minor MIME type */
99 /** @var string Upload timestamp */
102 /** @var int User ID of uploader */
105 /** @var string User name of uploader */
108 /** @var string Description of current revision of the file */
109 private $description;
111 /** @var string TS_MW timestamp of the last change of the file description */
112 private $descriptionTouched;
114 /** @var bool Whether the row was upgraded on load */
117 /** @var bool Whether the row was scheduled to upgrade on load */
120 /** @var bool True if the image row is locked */
123 /** @var bool True if the image row is locked with a lock initiated transaction */
124 private $lockedOwnTrx;
126 /** @var bool True if file is not present in file system. Not to be cached in memcached */
129 // @note: higher than IDBAccessObject constants
130 const LOAD_ALL
= 16; // integer; load all the lazy fields too (like metadata)
132 const ATOMIC_SECTION_LOCK
= 'LocalFile::lockingTransaction';
135 * Create a LocalFile from a title
136 * Do not call this except from inside a repo class.
138 * Note: $unused param is only here to avoid an E_STRICT
140 * @param Title $title
141 * @param FileRepo $repo
142 * @param null $unused
146 static function newFromTitle( $title, $repo, $unused = null ) {
147 return new self( $title, $repo );
151 * Create a LocalFile from a title
152 * Do not call this except from inside a repo class.
154 * @param stdClass $row
155 * @param FileRepo $repo
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 );
168 * Create a LocalFile from a SHA-1 key
169 * Do not call this except from inside a repo class.
171 * @param string $sha1 Base-36 SHA-1
172 * @param LocalRepo $repo
173 * @param string|bool $timestamp MW_timestamp (optional)
174 * @return bool|LocalFile
176 static function newFromKey( $sha1, $repo, $timestamp = false ) {
177 $dbr = $repo->getSlaveDB();
179 $conds = [ 'img_sha1' => $sha1 ];
181 $conds['img_timestamp'] = $dbr->timestamp( $timestamp );
184 $row = $dbr->selectRow( 'image', self
::selectFields(), $conds, __METHOD__
);
186 return self
::newFromRow( $row, $repo );
193 * Fields in the image table
196 static function selectFields() {
217 * Do not call this except from inside a repo class.
218 * @param Title $title
219 * @param FileRepo $repo
221 function __construct( $title, $repo ) {
222 parent
::__construct( $title, $repo );
224 $this->metadata
= '';
225 $this->historyLine
= 0;
226 $this->historyRes
= null;
227 $this->dataLoaded
= false;
228 $this->extraDataLoaded
= false;
230 $this->assertRepoDefined();
231 $this->assertTitleDefined();
235 * Get the memcached key for the main data for this file, or false if
236 * there is no access to the shared cache.
237 * @return string|bool
239 function getCacheKey() {
240 return $this->repo
->getSharedCacheKey( 'file', sha1( $this->getName() ) );
244 * Try to load file metadata from memcached, falling back to the database
246 private function loadFromCache() {
247 $this->dataLoaded
= false;
248 $this->extraDataLoaded
= false;
250 $key = $this->getCacheKey();
252 $this->loadFromDB( self
::READ_NORMAL
);
257 $cache = ObjectCache
::getMainWANInstance();
258 $cachedValues = $cache->getWithSetCallback(
261 function ( $oldValue, &$ttl, array &$setOpts ) use ( $cache ) {
262 $setOpts +
= Database
::getCacheSetOptions( $this->repo
->getSlaveDB() );
264 $this->loadFromDB( self
::READ_NORMAL
);
266 $fields = $this->getCacheFields( '' );
267 $cacheVal['fileExists'] = $this->fileExists
;
268 if ( $this->fileExists
) {
269 foreach ( $fields as $field ) {
270 $cacheVal[$field] = $this->$field;
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
280 unset( $cacheVal[$field] ); // don't let the value get too big
284 if ( $this->fileExists
) {
285 $ttl = $cache->adaptiveTTL( wfTimestamp( TS_UNIX
, $this->timestamp
), $ttl );
287 $ttl = $cache::TTL_DAY
;
292 [ 'version' => self
::VERSION
]
295 $this->fileExists
= $cachedValues['fileExists'];
296 if ( $this->fileExists
) {
297 $this->setProps( $cachedValues );
300 $this->dataLoaded
= true;
301 $this->extraDataLoaded
= true;
302 foreach ( $this->getLazyCacheFields( '' ) as $field ) {
303 $this->extraDataLoaded
= $this->extraDataLoaded
&& isset( $cachedValues[$field] );
308 * Purge the file object/metadata cache
310 public function invalidateCache() {
311 $key = $this->getCacheKey();
316 $this->repo
->getMasterDB()->onTransactionPreCommitOrIdle( function() use ( $key ) {
317 ObjectCache
::getMainWANInstance()->delete( $key );
322 * Load metadata from the file itself
324 function loadFromFile() {
325 $props = $this->repo
->getFileProps( $this->getVirtualUrl() );
326 $this->setProps( $props );
330 * @param string $prefix
333 function getCacheFields( $prefix = 'img_' ) {
334 static $fields = [ 'size', 'width', 'height', 'bits', 'media_type',
335 'major_mime', 'minor_mime', 'metadata', 'timestamp', 'sha1', 'user',
336 'user_text', 'description' ];
337 static $results = [];
339 if ( $prefix == '' ) {
343 if ( !isset( $results[$prefix] ) ) {
344 $prefixedFields = [];
345 foreach ( $fields as $field ) {
346 $prefixedFields[] = $prefix . $field;
348 $results[$prefix] = $prefixedFields;
351 return $results[$prefix];
355 * @param string $prefix
358 function getLazyCacheFields( $prefix = 'img_' ) {
359 static $fields = [ 'metadata' ];
360 static $results = [];
362 if ( $prefix == '' ) {
366 if ( !isset( $results[$prefix] ) ) {
367 $prefixedFields = [];
368 foreach ( $fields as $field ) {
369 $prefixedFields[] = $prefix . $field;
371 $results[$prefix] = $prefixedFields;
374 return $results[$prefix];
378 * Load file metadata from the DB
381 function loadFromDB( $flags = 0 ) {
382 $fname = get_class( $this ) . '::' . __FUNCTION__
;
384 # Unconditionally set loaded=true, we don't want the accessors constantly rechecking
385 $this->dataLoaded
= true;
386 $this->extraDataLoaded
= true;
388 $dbr = ( $flags & self
::READ_LATEST
)
389 ?
$this->repo
->getMasterDB()
390 : $this->repo
->getSlaveDB();
392 $row = $dbr->selectRow( 'image', $this->getCacheFields( 'img_' ),
393 [ 'img_name' => $this->getName() ], $fname );
396 $this->loadFromRow( $row );
398 $this->fileExists
= false;
403 * Load lazy file metadata from the DB.
404 * This covers fields that are sometimes not cached.
406 protected function loadExtraFromDB() {
407 $fname = get_class( $this ) . '::' . __FUNCTION__
;
409 # Unconditionally set loaded=true, we don't want the accessors constantly rechecking
410 $this->extraDataLoaded
= true;
412 $fieldMap = $this->loadFieldsWithTimestamp( $this->repo
->getSlaveDB(), $fname );
414 $fieldMap = $this->loadFieldsWithTimestamp( $this->repo
->getMasterDB(), $fname );
418 foreach ( $fieldMap as $name => $value ) {
419 $this->$name = $value;
422 throw new MWException( "Could not find data for image '{$this->getName()}'." );
427 * @param IDatabase $dbr
428 * @param string $fname
431 private function loadFieldsWithTimestamp( $dbr, $fname ) {
434 $row = $dbr->selectRow( 'image', $this->getLazyCacheFields( 'img_' ),
435 [ 'img_name' => $this->getName(), 'img_timestamp' => $this->getTimestamp() ],
438 $fieldMap = $this->unprefixRow( $row, 'img_' );
440 # File may have been uploaded over in the meantime; check the old versions
441 $row = $dbr->selectRow( 'oldimage', $this->getLazyCacheFields( 'oi_' ),
442 [ 'oi_name' => $this->getName(), 'oi_timestamp' => $this->getTimestamp() ],
445 $fieldMap = $this->unprefixRow( $row, 'oi_' );
453 * @param array|object $row
454 * @param string $prefix
455 * @throws MWException
458 protected function unprefixRow( $row, $prefix = 'img_' ) {
459 $array = (array)$row;
460 $prefixLength = strlen( $prefix );
462 // Sanity check prefix once
463 if ( substr( key( $array ), 0, $prefixLength ) !== $prefix ) {
464 throw new MWException( __METHOD__
. ': incorrect $prefix parameter' );
468 foreach ( $array as $name => $value ) {
469 $decoded[substr( $name, $prefixLength )] = $value;
476 * Decode a row from the database (either object or array) to an array
477 * with timestamps and MIME types decoded, and the field prefix removed.
479 * @param string $prefix
480 * @throws MWException
483 function decodeRow( $row, $prefix = 'img_' ) {
484 $decoded = $this->unprefixRow( $row, $prefix );
486 $decoded['timestamp'] = wfTimestamp( TS_MW
, $decoded['timestamp'] );
488 $decoded['metadata'] = $this->repo
->getSlaveDB()->decodeBlob( $decoded['metadata'] );
490 if ( empty( $decoded['major_mime'] ) ) {
491 $decoded['mime'] = 'unknown/unknown';
493 if ( !$decoded['minor_mime'] ) {
494 $decoded['minor_mime'] = 'unknown';
496 $decoded['mime'] = $decoded['major_mime'] . '/' . $decoded['minor_mime'];
499 // Trim zero padding from char/binary field
500 $decoded['sha1'] = rtrim( $decoded['sha1'], "\0" );
502 // Normalize some fields to integer type, per their database definition.
503 // Use unary + so that overflows will be upgraded to double instead of
504 // being trucated as with intval(). This is important to allow >2GB
505 // files on 32-bit systems.
506 foreach ( [ 'size', 'width', 'height', 'bits' ] as $field ) {
507 $decoded[$field] = +
$decoded[$field];
514 * Load file metadata from a DB result row
517 * @param string $prefix
519 function loadFromRow( $row, $prefix = 'img_' ) {
520 $this->dataLoaded
= true;
521 $this->extraDataLoaded
= true;
523 $array = $this->decodeRow( $row, $prefix );
525 foreach ( $array as $name => $value ) {
526 $this->$name = $value;
529 $this->fileExists
= true;
530 $this->maybeUpgradeRow();
534 * Load file metadata from cache or DB, unless already loaded
537 function load( $flags = 0 ) {
538 if ( !$this->dataLoaded
) {
539 if ( $flags & self
::READ_LATEST
) {
540 $this->loadFromDB( $flags );
542 $this->loadFromCache();
546 if ( ( $flags & self
::LOAD_ALL
) && !$this->extraDataLoaded
) {
547 // @note: loads on name/timestamp to reduce race condition problems
548 $this->loadExtraFromDB();
553 * Upgrade a row if it needs it
555 function maybeUpgradeRow() {
556 global $wgUpdateCompatibleMetadata;
558 if ( wfReadOnly() ||
$this->upgrading
) {
563 if ( is_null( $this->media_type
) ||
$this->mime
== 'image/svg' ) {
566 $handler = $this->getHandler();
568 $validity = $handler->isMetadataValid( $this, $this->getMetadata() );
569 if ( $validity === MediaHandler
::METADATA_BAD
) {
571 } elseif ( $validity === MediaHandler
::METADATA_COMPATIBLE
) {
572 $upgrade = $wgUpdateCompatibleMetadata;
578 $this->upgrading
= true;
579 // Defer updates unless in auto-commit CLI mode
580 DeferredUpdates
::addCallableUpdate( function() {
581 $this->upgrading
= false; // avoid duplicate updates
584 } catch ( LocalFileLockError
$e ) {
585 // let the other process handle it (or do it next time)
592 * @return bool Whether upgradeRow() ran for this object
594 function getUpgraded() {
595 return $this->upgraded
;
599 * Fix assorted version-related problems with the image row by reloading it from the file
601 function upgradeRow() {
602 $this->lock(); // begin
604 $this->loadFromFile();
606 # Don't destroy file info of missing files
607 if ( !$this->fileExists
) {
609 wfDebug( __METHOD__
. ": file does not exist, aborting\n" );
614 $dbw = $this->repo
->getMasterDB();
615 list( $major, $minor ) = self
::splitMime( $this->mime
);
617 if ( wfReadOnly() ) {
622 wfDebug( __METHOD__
. ': upgrading ' . $this->getName() . " to the current schema\n" );
624 $dbw->update( 'image',
626 'img_size' => $this->size
, // sanity
627 'img_width' => $this->width
,
628 'img_height' => $this->height
,
629 'img_bits' => $this->bits
,
630 'img_media_type' => $this->media_type
,
631 'img_major_mime' => $major,
632 'img_minor_mime' => $minor,
633 'img_metadata' => $dbw->encodeBlob( $this->metadata
),
634 'img_sha1' => $this->sha1
,
636 [ 'img_name' => $this->getName() ],
640 $this->invalidateCache();
642 $this->unlock(); // done
643 $this->upgraded
= true; // avoid rework/retries
647 * Set properties in this object to be equal to those given in the
648 * associative array $info. Only cacheable fields can be set.
649 * All fields *must* be set in $info except for getLazyCacheFields().
651 * If 'mime' is given, it will be split into major_mime/minor_mime.
652 * If major_mime/minor_mime are given, $this->mime will also be set.
656 function setProps( $info ) {
657 $this->dataLoaded
= true;
658 $fields = $this->getCacheFields( '' );
659 $fields[] = 'fileExists';
661 foreach ( $fields as $field ) {
662 if ( isset( $info[$field] ) ) {
663 $this->$field = $info[$field];
667 // Fix up mime fields
668 if ( isset( $info['major_mime'] ) ) {
669 $this->mime
= "{$info['major_mime']}/{$info['minor_mime']}";
670 } elseif ( isset( $info['mime'] ) ) {
671 $this->mime
= $info['mime'];
672 list( $this->major_mime
, $this->minor_mime
) = self
::splitMime( $this->mime
);
676 /** splitMime inherited */
677 /** getName inherited */
678 /** getTitle inherited */
679 /** getURL inherited */
680 /** getViewURL inherited */
681 /** getPath inherited */
682 /** isVisible inherited */
687 function isMissing() {
688 if ( $this->missing
=== null ) {
689 list( $fileExists ) = $this->repo
->fileExists( $this->getVirtualUrl() );
690 $this->missing
= !$fileExists;
693 return $this->missing
;
697 * Return the width of the image
702 public function getWidth( $page = 1 ) {
705 if ( $this->isMultipage() ) {
706 $handler = $this->getHandler();
710 $dim = $handler->getPageDimensions( $this, $page );
712 return $dim['width'];
714 // For non-paged media, the false goes through an
715 // intval, turning failure into 0, so do same here.
724 * Return the height of the image
729 public function getHeight( $page = 1 ) {
732 if ( $this->isMultipage() ) {
733 $handler = $this->getHandler();
737 $dim = $handler->getPageDimensions( $this, $page );
739 return $dim['height'];
741 // For non-paged media, the false goes through an
742 // intval, turning failure into 0, so do same here.
746 return $this->height
;
751 * Returns ID or name of user who uploaded the file
753 * @param string $type 'text' or 'id'
756 function getUser( $type = 'text' ) {
759 if ( $type == 'text' ) {
760 return $this->user_text
;
762 return (int)$this->user
;
767 * Get short description URL for a file based on the page ID.
769 * @return string|null
770 * @throws MWException
773 public function getDescriptionShortUrl() {
774 $pageId = $this->title
->getArticleID();
776 if ( $pageId !== null ) {
777 $url = $this->repo
->makeUrl( [ 'curid' => $pageId ] );
778 if ( $url !== false ) {
786 * Get handler-specific metadata
789 function getMetadata() {
790 $this->load( self
::LOAD_ALL
); // large metadata is loaded in another step
791 return $this->metadata
;
797 function getBitDepth() {
800 return (int)$this->bits
;
804 * Returns the size of the image file, in bytes
807 public function getSize() {
814 * Returns the MIME type of the file.
817 function getMimeType() {
824 * Returns the type of the media in the file.
825 * Use the value returned by this function with the MEDIATYPE_xxx constants.
828 function getMediaType() {
831 return $this->media_type
;
834 /** canRender inherited */
835 /** mustRender inherited */
836 /** allowInlineDisplay inherited */
837 /** isSafeFile inherited */
838 /** isTrustedFile inherited */
841 * Returns true if the file exists on disk.
842 * @return bool Whether file exist on disk.
844 public function exists() {
847 return $this->fileExists
;
850 /** getTransformScript inherited */
851 /** getUnscaledThumb inherited */
852 /** thumbName inherited */
853 /** createThumb inherited */
854 /** transform inherited */
856 /** getHandler inherited */
857 /** iconThumb inherited */
858 /** getLastError inherited */
861 * Get all thumbnail names previously generated for this file
862 * @param string|bool $archiveName Name of an archive file, default false
863 * @return array First element is the base dir, then files in that base dir.
865 function getThumbnails( $archiveName = false ) {
866 if ( $archiveName ) {
867 $dir = $this->getArchiveThumbPath( $archiveName );
869 $dir = $this->getThumbPath();
872 $backend = $this->repo
->getBackend();
875 $iterator = $backend->getFileList( [ 'dir' => $dir ] );
876 foreach ( $iterator as $file ) {
879 } catch ( FileBackendError
$e ) {
880 } // suppress (bug 54674)
886 * Refresh metadata in memcached, but don't touch thumbnails or CDN
888 function purgeMetadataCache() {
889 $this->invalidateCache();
893 * Delete all previously generated thumbnails, refresh metadata in memcached and purge the CDN.
895 * @param array $options An array potentially with the key forThumbRefresh.
897 * @note This used to purge old thumbnails by default as well, but doesn't anymore.
899 function purgeCache( $options = [] ) {
900 // Refresh metadata cache
901 $this->purgeMetadataCache();
904 $this->purgeThumbnails( $options );
906 // Purge CDN cache for this file
907 DeferredUpdates
::addUpdate(
908 new CdnCacheUpdate( [ $this->getUrl() ] ),
909 DeferredUpdates
::PRESEND
914 * Delete cached transformed files for an archived version only.
915 * @param string $archiveName Name of the archived file
917 function purgeOldThumbnails( $archiveName ) {
918 // Get a list of old thumbnails and URLs
919 $files = $this->getThumbnails( $archiveName );
921 // Purge any custom thumbnail caches
922 Hooks
::run( 'LocalFilePurgeThumbnails', [ $this, $archiveName ] );
925 $dir = array_shift( $files );
926 $this->purgeThumbList( $dir, $files );
930 foreach ( $files as $file ) {
931 $urls[] = $this->getArchiveThumbUrl( $archiveName, $file );
933 DeferredUpdates
::addUpdate( new CdnCacheUpdate( $urls ), DeferredUpdates
::PRESEND
);
937 * Delete cached transformed files for the current version only.
938 * @param array $options
940 public function purgeThumbnails( $options = [] ) {
941 $files = $this->getThumbnails();
942 // Always purge all files from CDN regardless of handler filters
944 foreach ( $files as $file ) {
945 $urls[] = $this->getThumbUrl( $file );
947 array_shift( $urls ); // don't purge directory
949 // Give media handler a chance to filter the file purge list
950 if ( !empty( $options['forThumbRefresh'] ) ) {
951 $handler = $this->getHandler();
953 $handler->filterThumbnailPurgeList( $files, $options );
957 // Purge any custom thumbnail caches
958 Hooks
::run( 'LocalFilePurgeThumbnails', [ $this, false ] );
961 $dir = array_shift( $files );
962 $this->purgeThumbList( $dir, $files );
965 DeferredUpdates
::addUpdate( new CdnCacheUpdate( $urls ), DeferredUpdates
::PRESEND
);
969 * Prerenders a configurable set of thumbnails
973 public function prerenderThumbnails() {
974 global $wgUploadThumbnailRenderMap;
978 $sizes = $wgUploadThumbnailRenderMap;
981 foreach ( $sizes as $size ) {
982 if ( $this->isVectorized() ||
$this->getWidth() > $size ) {
983 $jobs[] = new ThumbnailRenderJob(
985 [ 'transformParams' => [ 'width' => $size ] ]
991 JobQueueGroup
::singleton()->lazyPush( $jobs );
996 * Delete a list of thumbnails visible at urls
997 * @param string $dir Base dir of the files.
998 * @param array $files Array of strings: relative filenames (to $dir)
1000 protected function purgeThumbList( $dir, $files ) {
1001 $fileListDebug = strtr(
1002 var_export( $files, true ),
1005 wfDebug( __METHOD__
. ": $fileListDebug\n" );
1008 foreach ( $files as $file ) {
1009 # Check that the base file name is part of the thumb name
1010 # This is a basic sanity check to avoid erasing unrelated directories
1011 if ( strpos( $file, $this->getName() ) !== false
1012 ||
strpos( $file, "-thumbnail" ) !== false // "short" thumb name
1014 $purgeList[] = "{$dir}/{$file}";
1018 # Delete the thumbnails
1019 $this->repo
->quickPurgeBatch( $purgeList );
1020 # Clear out the thumbnail directory if empty
1021 $this->repo
->quickCleanDir( $dir );
1024 /** purgeDescription inherited */
1025 /** purgeEverything inherited */
1028 * @param int $limit Optional: Limit to number of results
1029 * @param int $start Optional: Timestamp, start from
1030 * @param int $end Optional: Timestamp, end at
1032 * @return OldLocalFile[]
1034 function getHistory( $limit = null, $start = null, $end = null, $inc = true ) {
1035 $dbr = $this->repo
->getSlaveDB();
1036 $tables = [ 'oldimage' ];
1037 $fields = OldLocalFile
::selectFields();
1038 $conds = $opts = $join_conds = [];
1039 $eq = $inc ?
'=' : '';
1040 $conds[] = "oi_name = " . $dbr->addQuotes( $this->title
->getDBkey() );
1043 $conds[] = "oi_timestamp <$eq " . $dbr->addQuotes( $dbr->timestamp( $start ) );
1047 $conds[] = "oi_timestamp >$eq " . $dbr->addQuotes( $dbr->timestamp( $end ) );
1051 $opts['LIMIT'] = $limit;
1054 // Search backwards for time > x queries
1055 $order = ( !$start && $end !== null ) ?
'ASC' : 'DESC';
1056 $opts['ORDER BY'] = "oi_timestamp $order";
1057 $opts['USE INDEX'] = [ 'oldimage' => 'oi_name_timestamp' ];
1059 Hooks
::run( 'LocalFile::getHistory', [ &$this, &$tables, &$fields,
1060 &$conds, &$opts, &$join_conds ] );
1062 $res = $dbr->select( $tables, $fields, $conds, __METHOD__
, $opts, $join_conds );
1065 foreach ( $res as $row ) {
1066 $r[] = $this->repo
->newFileFromRow( $row );
1069 if ( $order == 'ASC' ) {
1070 $r = array_reverse( $r ); // make sure it ends up descending
1077 * Returns the history of this file, line by line.
1078 * starts with current version, then old versions.
1079 * uses $this->historyLine to check which line to return:
1080 * 0 return line for current version
1081 * 1 query for old versions, return first one
1082 * 2, ... return next old version from above query
1085 public function nextHistoryLine() {
1086 # Polymorphic function name to distinguish foreign and local fetches
1087 $fname = get_class( $this ) . '::' . __FUNCTION__
;
1089 $dbr = $this->repo
->getSlaveDB();
1091 if ( $this->historyLine
== 0 ) { // called for the first time, return line from cur
1092 $this->historyRes
= $dbr->select( 'image',
1095 "'' AS oi_archive_name",
1099 [ 'img_name' => $this->title
->getDBkey() ],
1103 if ( 0 == $dbr->numRows( $this->historyRes
) ) {
1104 $this->historyRes
= null;
1108 } elseif ( $this->historyLine
== 1 ) {
1109 $this->historyRes
= $dbr->select( 'oldimage', '*',
1110 [ 'oi_name' => $this->title
->getDBkey() ],
1112 [ 'ORDER BY' => 'oi_timestamp DESC' ]
1115 $this->historyLine++
;
1117 return $dbr->fetchObject( $this->historyRes
);
1121 * Reset the history pointer to the first element of the history
1123 public function resetHistory() {
1124 $this->historyLine
= 0;
1126 if ( !is_null( $this->historyRes
) ) {
1127 $this->historyRes
= null;
1131 /** getHashPath inherited */
1132 /** getRel inherited */
1133 /** getUrlRel inherited */
1134 /** getArchiveRel inherited */
1135 /** getArchivePath inherited */
1136 /** getThumbPath inherited */
1137 /** getArchiveUrl inherited */
1138 /** getThumbUrl inherited */
1139 /** getArchiveVirtualUrl inherited */
1140 /** getThumbVirtualUrl inherited */
1141 /** isHashed inherited */
1144 * Upload a file and record it in the DB
1145 * @param string|FSFile $src Source storage path, virtual URL, or filesystem path
1146 * @param string $comment Upload description
1147 * @param string $pageText Text to use for the new description page,
1148 * if a new description page is created
1149 * @param int|bool $flags Flags for publish()
1150 * @param array|bool $props File properties, if known. This can be used to
1151 * reduce the upload time when uploading virtual URLs for which the file
1152 * info is already known
1153 * @param string|bool $timestamp Timestamp for img_timestamp, or false to use the
1155 * @param User|null $user User object or null to use $wgUser
1156 * @param string[] $tags Change tags to add to the log entry and page revision.
1157 * (This doesn't check $user's permissions.)
1158 * @return FileRepoStatus On success, the value member contains the
1159 * archive name, or an empty string if it was a new file.
1161 function upload( $src, $comment, $pageText, $flags = 0, $props = false,
1162 $timestamp = false, $user = null, $tags = []
1166 if ( $this->getRepo()->getReadOnlyReason() !== false ) {
1167 return $this->readOnlyFatalStatus();
1170 $srcPath = ( $src instanceof FSFile
) ?
$src->getPath() : $src;
1172 if ( $this->repo
->isVirtualUrl( $srcPath )
1173 || FileBackend
::isStoragePath( $srcPath )
1175 $props = $this->repo
->getFileProps( $srcPath );
1177 $props = FSFile
::getPropsFromPath( $srcPath );
1182 $handler = MediaHandler
::getHandler( $props['mime'] );
1184 $options['headers'] = $handler->getStreamHeaders( $props['metadata'] );
1186 $options['headers'] = [];
1189 // Trim spaces on user supplied text
1190 $comment = trim( $comment );
1192 // Truncate nicely or the DB will do it for us
1193 // non-nicely (dangling multi-byte chars, non-truncated version in cache).
1194 $comment = $wgContLang->truncate( $comment, 255 );
1195 $this->lock(); // begin
1196 $status = $this->publish( $src, $flags, $options );
1198 if ( $status->successCount
>= 2 ) {
1199 // There will be a copy+(one of move,copy,store).
1200 // The first succeeding does not commit us to updating the DB
1201 // since it simply copied the current version to a timestamped file name.
1202 // It is only *preferable* to avoid leaving such files orphaned.
1203 // Once the second operation goes through, then the current version was
1204 // updated and we must therefore update the DB too.
1205 $oldver = $status->value
;
1206 if ( !$this->recordUpload2( $oldver, $comment, $pageText, $props, $timestamp, $user, $tags ) ) {
1207 $status->fatal( 'filenotfound', $srcPath );
1211 $this->unlock(); // done
1217 * Record a file upload in the upload log and the image table
1218 * @param string $oldver
1219 * @param string $desc
1220 * @param string $license
1221 * @param string $copyStatus
1222 * @param string $source
1223 * @param bool $watch
1224 * @param string|bool $timestamp
1225 * @param User|null $user User object or null to use $wgUser
1228 function recordUpload( $oldver, $desc, $license = '', $copyStatus = '', $source = '',
1229 $watch = false, $timestamp = false, User
$user = null ) {
1235 $pageText = SpecialUpload
::getInitialPageText( $desc, $license, $copyStatus, $source );
1237 if ( !$this->recordUpload2( $oldver, $desc, $pageText, false, $timestamp, $user ) ) {
1242 $user->addWatch( $this->getTitle() );
1249 * Record a file upload in the upload log and the image table
1250 * @param string $oldver
1251 * @param string $comment
1252 * @param string $pageText
1253 * @param bool|array $props
1254 * @param string|bool $timestamp
1255 * @param null|User $user
1256 * @param string[] $tags
1259 function recordUpload2(
1260 $oldver, $comment, $pageText, $props = false, $timestamp = false, $user = null, $tags = []
1262 if ( is_null( $user ) ) {
1267 $dbw = $this->repo
->getMasterDB();
1269 # Imports or such might force a certain timestamp; otherwise we generate
1270 # it and can fudge it slightly to keep (name,timestamp) unique on re-upload.
1271 if ( $timestamp === false ) {
1272 $timestamp = $dbw->timestamp();
1273 $allowTimeKludge = true;
1275 $allowTimeKludge = false;
1278 $props = $props ?
: $this->repo
->getFileProps( $this->getVirtualUrl() );
1279 $props['description'] = $comment;
1280 $props['user'] = $user->getId();
1281 $props['user_text'] = $user->getName();
1282 $props['timestamp'] = wfTimestamp( TS_MW
, $timestamp ); // DB -> TS_MW
1283 $this->setProps( $props );
1285 # Fail now if the file isn't there
1286 if ( !$this->fileExists
) {
1287 wfDebug( __METHOD__
. ": File " . $this->getRel() . " went missing!\n" );
1292 $dbw->startAtomic( __METHOD__
);
1294 # Test to see if the row exists using INSERT IGNORE
1295 # This avoids race conditions by locking the row until the commit, and also
1296 # doesn't deadlock. SELECT FOR UPDATE causes a deadlock for every race condition.
1297 $dbw->insert( 'image',
1299 'img_name' => $this->getName(),
1300 'img_size' => $this->size
,
1301 'img_width' => intval( $this->width
),
1302 'img_height' => intval( $this->height
),
1303 'img_bits' => $this->bits
,
1304 'img_media_type' => $this->media_type
,
1305 'img_major_mime' => $this->major_mime
,
1306 'img_minor_mime' => $this->minor_mime
,
1307 'img_timestamp' => $timestamp,
1308 'img_description' => $comment,
1309 'img_user' => $user->getId(),
1310 'img_user_text' => $user->getName(),
1311 'img_metadata' => $dbw->encodeBlob( $this->metadata
),
1312 'img_sha1' => $this->sha1
1318 $reupload = ( $dbw->affectedRows() == 0 );
1320 if ( $allowTimeKludge ) {
1321 # Use LOCK IN SHARE MODE to ignore any transaction snapshotting
1322 $ltimestamp = $dbw->selectField(
1325 [ 'img_name' => $this->getName() ],
1327 [ 'LOCK IN SHARE MODE' ]
1329 $lUnixtime = $ltimestamp ?
wfTimestamp( TS_UNIX
, $ltimestamp ) : false;
1330 # Avoid a timestamp that is not newer than the last version
1331 # TODO: the image/oldimage tables should be like page/revision with an ID field
1332 if ( $lUnixtime && wfTimestamp( TS_UNIX
, $timestamp ) <= $lUnixtime ) {
1333 sleep( 1 ); // fast enough re-uploads would go far in the future otherwise
1334 $timestamp = $dbw->timestamp( $lUnixtime +
1 );
1335 $this->timestamp
= wfTimestamp( TS_MW
, $timestamp ); // DB -> TS_MW
1339 # (bug 34993) Note: $oldver can be empty here, if the previous
1340 # version of the file was broken. Allow registration of the new
1341 # version to continue anyway, because that's better than having
1342 # an image that's not fixable by user operations.
1343 # Collision, this is an update of a file
1344 # Insert previous contents into oldimage
1345 $dbw->insertSelect( 'oldimage', 'image',
1347 'oi_name' => 'img_name',
1348 'oi_archive_name' => $dbw->addQuotes( $oldver ),
1349 'oi_size' => 'img_size',
1350 'oi_width' => 'img_width',
1351 'oi_height' => 'img_height',
1352 'oi_bits' => 'img_bits',
1353 'oi_timestamp' => 'img_timestamp',
1354 'oi_description' => 'img_description',
1355 'oi_user' => 'img_user',
1356 'oi_user_text' => 'img_user_text',
1357 'oi_metadata' => 'img_metadata',
1358 'oi_media_type' => 'img_media_type',
1359 'oi_major_mime' => 'img_major_mime',
1360 'oi_minor_mime' => 'img_minor_mime',
1361 'oi_sha1' => 'img_sha1'
1363 [ 'img_name' => $this->getName() ],
1367 # Update the current image row
1368 $dbw->update( 'image',
1370 'img_size' => $this->size
,
1371 'img_width' => intval( $this->width
),
1372 'img_height' => intval( $this->height
),
1373 'img_bits' => $this->bits
,
1374 'img_media_type' => $this->media_type
,
1375 'img_major_mime' => $this->major_mime
,
1376 'img_minor_mime' => $this->minor_mime
,
1377 'img_timestamp' => $timestamp,
1378 'img_description' => $comment,
1379 'img_user' => $user->getId(),
1380 'img_user_text' => $user->getName(),
1381 'img_metadata' => $dbw->encodeBlob( $this->metadata
),
1382 'img_sha1' => $this->sha1
1384 [ 'img_name' => $this->getName() ],
1389 $descTitle = $this->getTitle();
1390 $descId = $descTitle->getArticleID();
1391 $wikiPage = new WikiFilePage( $descTitle );
1392 $wikiPage->setFile( $this );
1394 // Add the log entry...
1395 $logEntry = new ManualLogEntry( 'upload', $reupload ?
'overwrite' : 'upload' );
1396 $logEntry->setTimestamp( $this->timestamp
);
1397 $logEntry->setPerformer( $user );
1398 $logEntry->setComment( $comment );
1399 $logEntry->setTarget( $descTitle );
1400 // Allow people using the api to associate log entries with the upload.
1401 // Log has a timestamp, but sometimes different from upload timestamp.
1402 $logEntry->setParameters(
1404 'img_sha1' => $this->sha1
,
1405 'img_timestamp' => $timestamp,
1408 // Note we keep $logId around since during new image
1409 // creation, page doesn't exist yet, so log_page = 0
1410 // but we want it to point to the page we're making,
1411 // so we later modify the log entry.
1412 // For a similar reason, we avoid making an RC entry
1413 // now and wait until the page exists.
1414 $logId = $logEntry->insert();
1416 if ( $descTitle->exists() ) {
1417 // Use own context to get the action text in content language
1418 $formatter = LogFormatter
::newFromEntry( $logEntry );
1419 $formatter->setContext( RequestContext
::newExtraneousContext( $descTitle ) );
1420 $editSummary = $formatter->getPlainActionText();
1422 $nullRevision = Revision
::newNullRevision(
1429 if ( $nullRevision ) {
1430 $nullRevision->insertOn( $dbw );
1432 'NewRevisionFromEditComplete',
1433 [ $wikiPage, $nullRevision, $nullRevision->getParentId(), $user ]
1435 $wikiPage->updateRevisionOn( $dbw, $nullRevision );
1436 // Associate null revision id
1437 $logEntry->setAssociatedRevId( $nullRevision->getId() );
1440 $newPageContent = null;
1442 // Make the description page and RC log entry post-commit
1443 $newPageContent = ContentHandler
::makeContent( $pageText, $descTitle );
1446 # Defer purges, page creation, and link updates in case they error out.
1447 # The most important thing is that files and the DB registry stay synced.
1448 $dbw->endAtomic( __METHOD__
);
1450 # Do some cache purges after final commit so that:
1451 # a) Changes are more likely to be seen post-purge
1452 # b) They won't cause rollback of the log publish/update above
1453 DeferredUpdates
::addUpdate(
1454 new AutoCommitUpdate(
1458 $reupload, $wikiPage, $newPageContent, $comment, $user,
1459 $logEntry, $logId, $descId, $tags
1461 # Update memcache after the commit
1462 $this->invalidateCache();
1464 $updateLogPage = false;
1465 if ( $newPageContent ) {
1466 # New file page; create the description page.
1467 # There's already a log entry, so don't make a second RC entry
1468 # CDN and file cache for the description page are purged by doEditContent.
1469 $status = $wikiPage->doEditContent(
1472 EDIT_NEW | EDIT_SUPPRESS_RC
,
1477 if ( isset( $status->value
['revision'] ) ) {
1478 // Associate new page revision id
1479 $logEntry->setAssociatedRevId( $status->value
['revision']->getId() );
1481 // This relies on the resetArticleID() call in WikiPage::insertOn(),
1482 // which is triggered on $descTitle by doEditContent() above.
1483 if ( isset( $status->value
['revision'] ) ) {
1484 /** @var $rev Revision */
1485 $rev = $status->value
['revision'];
1486 $updateLogPage = $rev->getPage();
1489 # Existing file page: invalidate description page cache
1490 $wikiPage->getTitle()->invalidateCache();
1491 $wikiPage->getTitle()->purgeSquid();
1492 # Allow the new file version to be patrolled from the page footer
1493 Article
::purgePatrolFooterCache( $descId );
1496 # Update associated rev id. This should be done by $logEntry->insert() earlier,
1497 # but setAssociatedRevId() wasn't called at that point yet...
1498 $logParams = $logEntry->getParameters();
1499 $logParams['associated_rev_id'] = $logEntry->getAssociatedRevId();
1500 $update = [ 'log_params' => LogEntryBase
::makeParamBlob( $logParams ) ];
1501 if ( $updateLogPage ) {
1502 # Also log page, in case where we just created it above
1503 $update['log_page'] = $updateLogPage;
1505 $this->getRepo()->getMasterDB()->update(
1508 [ 'log_id' => $logId ],
1511 $this->getRepo()->getMasterDB()->insert(
1514 'ls_field' => 'associated_rev_id',
1515 'ls_value' => $logEntry->getAssociatedRevId(),
1516 'ls_log_id' => $logId,
1521 # Add change tags, if any
1523 $logEntry->setTags( $tags );
1526 # Uploads can be patrolled
1527 $logEntry->setIsPatrollable( true );
1529 # Now that the log entry is up-to-date, make an RC entry.
1530 $logEntry->publish( $logId );
1532 # Run hook for other updates (typically more cache purging)
1533 Hooks
::run( 'FileUpload', [ $this, $reupload, !$newPageContent ] );
1536 # Delete old thumbnails
1537 $this->purgeThumbnails();
1538 # Remove the old file from the CDN cache
1539 DeferredUpdates
::addUpdate(
1540 new CdnCacheUpdate( [ $this->getUrl() ] ),
1541 DeferredUpdates
::PRESEND
1544 # Update backlink pages pointing to this title if created
1545 LinksUpdate
::queueRecursiveJobsForTable( $this->getTitle(), 'imagelinks' );
1548 $this->prerenderThumbnails();
1551 DeferredUpdates
::PRESEND
1555 # This is a new file, so update the image count
1556 DeferredUpdates
::addUpdate( SiteStatsUpdate
::factory( [ 'images' => 1 ] ) );
1559 # Invalidate cache for all pages using this file
1560 DeferredUpdates
::addUpdate( new HTMLCacheUpdate( $this->getTitle(), 'imagelinks' ) );
1566 * Move or copy a file to its public location. If a file exists at the
1567 * destination, move it to an archive. Returns a FileRepoStatus object with
1568 * the archive name in the "value" member on success.
1570 * The archive name should be passed through to recordUpload for database
1573 * @param string|FSFile $src Local filesystem path or virtual URL to the source image
1574 * @param int $flags A bitwise combination of:
1575 * File::DELETE_SOURCE Delete the source file, i.e. move rather than copy
1576 * @param array $options Optional additional parameters
1577 * @return FileRepoStatus On success, the value member contains the
1578 * archive name, or an empty string if it was a new file.
1580 function publish( $src, $flags = 0, array $options = [] ) {
1581 return $this->publishTo( $src, $this->getRel(), $flags, $options );
1585 * Move or copy a file to a specified location. Returns a FileRepoStatus
1586 * object with the archive name in the "value" member on success.
1588 * The archive name should be passed through to recordUpload for database
1591 * @param string|FSFile $src Local filesystem path or virtual URL to the source image
1592 * @param string $dstRel Target relative path
1593 * @param int $flags A bitwise combination of:
1594 * File::DELETE_SOURCE Delete the source file, i.e. move rather than copy
1595 * @param array $options Optional additional parameters
1596 * @return FileRepoStatus On success, the value member contains the
1597 * archive name, or an empty string if it was a new file.
1599 function publishTo( $src, $dstRel, $flags = 0, array $options = [] ) {
1600 $srcPath = ( $src instanceof FSFile
) ?
$src->getPath() : $src;
1602 $repo = $this->getRepo();
1603 if ( $repo->getReadOnlyReason() !== false ) {
1604 return $this->readOnlyFatalStatus();
1607 $this->lock(); // begin
1609 $archiveName = wfTimestamp( TS_MW
) . '!' . $this->getName();
1610 $archiveRel = 'archive/' . $this->getHashPath() . $archiveName;
1612 if ( $repo->hasSha1Storage() ) {
1613 $sha1 = $repo->isVirtualUrl( $srcPath )
1614 ?
$repo->getFileSha1( $srcPath )
1615 : FSFile
::getSha1Base36FromPath( $srcPath );
1616 /** @var FileBackendDBRepoWrapper $wrapperBackend */
1617 $wrapperBackend = $repo->getBackend();
1618 $dst = $wrapperBackend->getPathForSHA1( $sha1 );
1619 $status = $repo->quickImport( $src, $dst );
1620 if ( $flags & File
::DELETE_SOURCE
) {
1624 if ( $this->exists() ) {
1625 $status->value
= $archiveName;
1628 $flags = $flags & File
::DELETE_SOURCE ? LocalRepo
::DELETE_SOURCE
: 0;
1629 $status = $repo->publish( $srcPath, $dstRel, $archiveRel, $flags, $options );
1631 if ( $status->value
== 'new' ) {
1632 $status->value
= '';
1634 $status->value
= $archiveName;
1638 $this->unlock(); // done
1643 /** getLinksTo inherited */
1644 /** getExifData inherited */
1645 /** isLocal inherited */
1646 /** wasDeleted inherited */
1649 * Move file to the new title
1651 * Move current, old version and all thumbnails
1652 * to the new filename. Old file is deleted.
1654 * Cache purging is done; checks for validity
1655 * and logging are caller's responsibility
1657 * @param Title $target New file name
1658 * @return FileRepoStatus
1660 function move( $target ) {
1661 if ( $this->getRepo()->getReadOnlyReason() !== false ) {
1662 return $this->readOnlyFatalStatus();
1665 wfDebugLog( 'imagemove', "Got request to move {$this->name} to " . $target->getText() );
1666 $batch = new LocalFileMoveBatch( $this, $target );
1668 $this->lock(); // begin
1669 $batch->addCurrent();
1670 $archiveNames = $batch->addOlds();
1671 $status = $batch->execute();
1672 $this->unlock(); // done
1674 wfDebugLog( 'imagemove', "Finished moving {$this->name}" );
1676 // Purge the source and target files...
1677 $oldTitleFile = wfLocalFile( $this->title
);
1678 $newTitleFile = wfLocalFile( $target );
1679 // To avoid slow purges in the transaction, move them outside...
1680 DeferredUpdates
::addUpdate(
1681 new AutoCommitUpdate(
1682 $this->getRepo()->getMasterDB(),
1684 function () use ( $oldTitleFile, $newTitleFile, $archiveNames ) {
1685 $oldTitleFile->purgeEverything();
1686 foreach ( $archiveNames as $archiveName ) {
1687 $oldTitleFile->purgeOldThumbnails( $archiveName );
1689 $newTitleFile->purgeEverything();
1692 DeferredUpdates
::PRESEND
1695 if ( $status->isOK() ) {
1696 // Now switch the object
1697 $this->title
= $target;
1698 // Force regeneration of the name and hashpath
1699 unset( $this->name
);
1700 unset( $this->hashPath
);
1707 * Delete all versions of the file.
1709 * Moves the files into an archive directory (or deletes them)
1710 * and removes the database rows.
1712 * Cache purging is done; logging is caller's responsibility.
1714 * @param string $reason
1715 * @param bool $suppress
1716 * @param User|null $user
1717 * @return FileRepoStatus
1719 function delete( $reason, $suppress = false, $user = null ) {
1720 if ( $this->getRepo()->getReadOnlyReason() !== false ) {
1721 return $this->readOnlyFatalStatus();
1724 $batch = new LocalFileDeleteBatch( $this, $reason, $suppress, $user );
1726 $this->lock(); // begin
1727 $batch->addCurrent();
1728 // Get old version relative paths
1729 $archiveNames = $batch->addOlds();
1730 $status = $batch->execute();
1731 $this->unlock(); // done
1733 if ( $status->isOK() ) {
1734 DeferredUpdates
::addUpdate( SiteStatsUpdate
::factory( [ 'images' => -1 ] ) );
1737 // To avoid slow purges in the transaction, move them outside...
1738 DeferredUpdates
::addUpdate(
1739 new AutoCommitUpdate(
1740 $this->getRepo()->getMasterDB(),
1742 function () use ( $archiveNames ) {
1743 $this->purgeEverything();
1744 foreach ( $archiveNames as $archiveName ) {
1745 $this->purgeOldThumbnails( $archiveName );
1749 DeferredUpdates
::PRESEND
1754 foreach ( $archiveNames as $archiveName ) {
1755 $purgeUrls[] = $this->getArchiveUrl( $archiveName );
1757 DeferredUpdates
::addUpdate( new CdnCacheUpdate( $purgeUrls ), DeferredUpdates
::PRESEND
);
1763 * Delete an old version of the file.
1765 * Moves the file into an archive directory (or deletes it)
1766 * and removes the database row.
1768 * Cache purging is done; logging is caller's responsibility.
1770 * @param string $archiveName
1771 * @param string $reason
1772 * @param bool $suppress
1773 * @param User|null $user
1774 * @throws MWException Exception on database or file store failure
1775 * @return FileRepoStatus
1777 function deleteOld( $archiveName, $reason, $suppress = false, $user = null ) {
1778 if ( $this->getRepo()->getReadOnlyReason() !== false ) {
1779 return $this->readOnlyFatalStatus();
1782 $batch = new LocalFileDeleteBatch( $this, $reason, $suppress, $user );
1784 $this->lock(); // begin
1785 $batch->addOld( $archiveName );
1786 $status = $batch->execute();
1787 $this->unlock(); // done
1789 $this->purgeOldThumbnails( $archiveName );
1790 if ( $status->isOK() ) {
1791 $this->purgeDescription();
1794 DeferredUpdates
::addUpdate(
1795 new CdnCacheUpdate( [ $this->getArchiveUrl( $archiveName ) ] ),
1796 DeferredUpdates
::PRESEND
1803 * Restore all or specified deleted revisions to the given file.
1804 * Permissions and logging are left to the caller.
1806 * May throw database exceptions on error.
1808 * @param array $versions Set of record ids of deleted items to restore,
1809 * or empty to restore all revisions.
1810 * @param bool $unsuppress
1811 * @return FileRepoStatus
1813 function restore( $versions = [], $unsuppress = false ) {
1814 if ( $this->getRepo()->getReadOnlyReason() !== false ) {
1815 return $this->readOnlyFatalStatus();
1818 $batch = new LocalFileRestoreBatch( $this, $unsuppress );
1820 $this->lock(); // begin
1824 $batch->addIds( $versions );
1826 $status = $batch->execute();
1827 if ( $status->isGood() ) {
1828 $cleanupStatus = $batch->cleanup();
1829 $cleanupStatus->successCount
= 0;
1830 $cleanupStatus->failCount
= 0;
1831 $status->merge( $cleanupStatus );
1833 $this->unlock(); // done
1838 /** isMultipage inherited */
1839 /** pageCount inherited */
1840 /** scaleHeight inherited */
1841 /** getImageSize inherited */
1844 * Get the URL of the file description page.
1847 function getDescriptionUrl() {
1848 return $this->title
->getLocalURL();
1852 * Get the HTML text of the description page
1853 * This is not used by ImagePage for local files, since (among other things)
1854 * it skips the parser cache.
1856 * @param Language $lang What language to get description in (Optional)
1857 * @return bool|mixed
1859 function getDescriptionText( $lang = null ) {
1860 $revision = Revision
::newFromTitle( $this->title
, false, Revision
::READ_NORMAL
);
1864 $content = $revision->getContent();
1868 $pout = $content->getParserOutput( $this->title
, null, new ParserOptions( null, $lang ) );
1870 return $pout->getText();
1874 * @param int $audience
1878 function getDescription( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
1880 if ( $audience == self
::FOR_PUBLIC
&& $this->isDeleted( self
::DELETED_COMMENT
) ) {
1882 } elseif ( $audience == self
::FOR_THIS_USER
1883 && !$this->userCan( self
::DELETED_COMMENT
, $user )
1887 return $this->description
;
1892 * @return bool|string
1894 function getTimestamp() {
1897 return $this->timestamp
;
1901 * @return bool|string
1903 public function getDescriptionTouched() {
1904 // The DB lookup might return false, e.g. if the file was just deleted, or the shared DB repo
1905 // itself gets it from elsewhere. To avoid repeating the DB lookups in such a case, we
1906 // need to differentiate between null (uninitialized) and false (failed to load).
1907 if ( $this->descriptionTouched
=== null ) {
1909 'page_namespace' => $this->title
->getNamespace(),
1910 'page_title' => $this->title
->getDBkey()
1912 $touched = $this->repo
->getSlaveDB()->selectField( 'page', 'page_touched', $cond, __METHOD__
);
1913 $this->descriptionTouched
= $touched ?
wfTimestamp( TS_MW
, $touched ) : false;
1916 return $this->descriptionTouched
;
1922 function getSha1() {
1924 // Initialise now if necessary
1925 if ( $this->sha1
== '' && $this->fileExists
) {
1926 $this->lock(); // begin
1928 $this->sha1
= $this->repo
->getFileSha1( $this->getPath() );
1929 if ( !wfReadOnly() && strval( $this->sha1
) != '' ) {
1930 $dbw = $this->repo
->getMasterDB();
1931 $dbw->update( 'image',
1932 [ 'img_sha1' => $this->sha1
],
1933 [ 'img_name' => $this->getName() ],
1935 $this->invalidateCache();
1938 $this->unlock(); // done
1945 * @return bool Whether to cache in RepoGroup (this avoids OOMs)
1947 function isCacheable() {
1950 // If extra data (metadata) was not loaded then it must have been large
1951 return $this->extraDataLoaded
1952 && strlen( serialize( $this->metadata
) ) <= self
::CACHE_FIELD_MAX_LEN
;
1959 public function acquireFileLock() {
1960 return $this->getRepo()->getBackend()->lockFiles(
1961 [ $this->getPath() ], LockManager
::LOCK_EX
, 10
1969 public function releaseFileLock() {
1970 return $this->getRepo()->getBackend()->unlockFiles(
1971 [ $this->getPath() ], LockManager
::LOCK_EX
1976 * Start an atomic DB section and lock the image for update
1977 * or increments a reference counter if the lock is already held
1979 * This method should not be used outside of LocalFile/LocalFile*Batch
1981 * @throws LocalFileLockError Throws an error if the lock was not acquired
1982 * @return bool Whether the file lock owns/spawned the DB transaction
1984 public function lock() {
1985 if ( !$this->locked
) {
1986 $logger = LoggerFactory
::getInstance( 'LocalFile' );
1988 $dbw = $this->repo
->getMasterDB();
1989 $makesTransaction = !$dbw->trxLevel();
1990 $dbw->startAtomic( self
::ATOMIC_SECTION_LOCK
);
1991 // Bug 54736: use simple lock to handle when the file does not exist.
1992 // SELECT FOR UPDATE prevents changes, not other SELECTs with FOR UPDATE.
1993 // Also, that would cause contention on INSERT of similarly named rows.
1994 $status = $this->acquireFileLock(); // represents all versions of the file
1995 if ( !$status->isGood() ) {
1996 $dbw->endAtomic( self
::ATOMIC_SECTION_LOCK
);
1997 $logger->warning( "Failed to lock '{file}'", [ 'file' => $this->name
] );
1999 throw new LocalFileLockError( $status );
2001 // Release the lock *after* commit to avoid row-level contention.
2002 // Make sure it triggers on rollback() as well as commit() (T132921).
2003 $dbw->onTransactionResolution( function () use ( $logger ) {
2004 $status = $this->releaseFileLock();
2005 if ( !$status->isGood() ) {
2006 $logger->error( "Failed to unlock '{file}'", [ 'file' => $this->name
] );
2009 // Callers might care if the SELECT snapshot is safely fresh
2010 $this->lockedOwnTrx
= $makesTransaction;
2015 return $this->lockedOwnTrx
;
2019 * Decrement the lock reference count and end the atomic section if it reaches zero
2021 * This method should not be used outside of LocalFile/LocalFile*Batch
2023 * The commit and loc release will happen when no atomic sections are active, which
2024 * may happen immediately or at some point after calling this
2026 public function unlock() {
2027 if ( $this->locked
) {
2029 if ( !$this->locked
) {
2030 $dbw = $this->repo
->getMasterDB();
2031 $dbw->endAtomic( self
::ATOMIC_SECTION_LOCK
);
2032 $this->lockedOwnTrx
= false;
2040 protected function readOnlyFatalStatus() {
2041 return $this->getRepo()->newFatal( 'filereadonlyerror', $this->getName(),
2042 $this->getRepo()->getName(), $this->getRepo()->getReadOnlyReason() );
2046 * Clean up any dangling locks
2048 function __destruct() {
2051 } // LocalFile class
2053 # ------------------------------------------------------------------------------
2056 * Helper class for file deletion
2057 * @ingroup FileAbstraction
2059 class LocalFileDeleteBatch
{
2060 /** @var LocalFile */
2067 private $srcRels = [];
2070 private $archiveUrls = [];
2072 /** @var array Items to be processed in the deletion batch */
2073 private $deletionBatch;
2075 /** @var bool Whether to suppress all suppressable fields when deleting */
2078 /** @var FileRepoStatus */
2086 * @param string $reason
2087 * @param bool $suppress
2088 * @param User|null $user
2090 function __construct( File
$file, $reason = '', $suppress = false, $user = null ) {
2091 $this->file
= $file;
2092 $this->reason
= $reason;
2093 $this->suppress
= $suppress;
2095 $this->user
= $user;
2098 $this->user
= $wgUser;
2100 $this->status
= $file->repo
->newGood();
2103 public function addCurrent() {
2104 $this->srcRels
['.'] = $this->file
->getRel();
2108 * @param string $oldName
2110 public function addOld( $oldName ) {
2111 $this->srcRels
[$oldName] = $this->file
->getArchiveRel( $oldName );
2112 $this->archiveUrls
[] = $this->file
->getArchiveUrl( $oldName );
2116 * Add the old versions of the image to the batch
2117 * @return array List of archive names from old versions
2119 public function addOlds() {
2122 $dbw = $this->file
->repo
->getMasterDB();
2123 $result = $dbw->select( 'oldimage',
2124 [ 'oi_archive_name' ],
2125 [ 'oi_name' => $this->file
->getName() ],
2129 foreach ( $result as $row ) {
2130 $this->addOld( $row->oi_archive_name
);
2131 $archiveNames[] = $row->oi_archive_name
;
2134 return $archiveNames;
2140 protected function getOldRels() {
2141 if ( !isset( $this->srcRels
['.'] ) ) {
2142 $oldRels =& $this->srcRels
;
2143 $deleteCurrent = false;
2145 $oldRels = $this->srcRels
;
2146 unset( $oldRels['.'] );
2147 $deleteCurrent = true;
2150 return [ $oldRels, $deleteCurrent ];
2156 protected function getHashes() {
2158 list( $oldRels, $deleteCurrent ) = $this->getOldRels();
2160 if ( $deleteCurrent ) {
2161 $hashes['.'] = $this->file
->getSha1();
2164 if ( count( $oldRels ) ) {
2165 $dbw = $this->file
->repo
->getMasterDB();
2166 $res = $dbw->select(
2168 [ 'oi_archive_name', 'oi_sha1' ],
2169 [ 'oi_archive_name' => array_keys( $oldRels ),
2170 'oi_name' => $this->file
->getName() ], // performance
2174 foreach ( $res as $row ) {
2175 if ( rtrim( $row->oi_sha1
, "\0" ) === '' ) {
2176 // Get the hash from the file
2177 $oldUrl = $this->file
->getArchiveVirtualUrl( $row->oi_archive_name
);
2178 $props = $this->file
->repo
->getFileProps( $oldUrl );
2180 if ( $props['fileExists'] ) {
2181 // Upgrade the oldimage row
2182 $dbw->update( 'oldimage',
2183 [ 'oi_sha1' => $props['sha1'] ],
2184 [ 'oi_name' => $this->file
->getName(), 'oi_archive_name' => $row->oi_archive_name
],
2186 $hashes[$row->oi_archive_name
] = $props['sha1'];
2188 $hashes[$row->oi_archive_name
] = false;
2191 $hashes[$row->oi_archive_name
] = $row->oi_sha1
;
2196 $missing = array_diff_key( $this->srcRels
, $hashes );
2198 foreach ( $missing as $name => $rel ) {
2199 $this->status
->error( 'filedelete-old-unregistered', $name );
2202 foreach ( $hashes as $name => $hash ) {
2204 $this->status
->error( 'filedelete-missing', $this->srcRels
[$name] );
2205 unset( $hashes[$name] );
2212 protected function doDBInserts() {
2214 $dbw = $this->file
->repo
->getMasterDB();
2215 $encTimestamp = $dbw->addQuotes( $dbw->timestamp( $now ) );
2216 $encUserId = $dbw->addQuotes( $this->user
->getId() );
2217 $encReason = $dbw->addQuotes( $this->reason
);
2218 $encGroup = $dbw->addQuotes( 'deleted' );
2219 $ext = $this->file
->getExtension();
2220 $dotExt = $ext === '' ?
'' : ".$ext";
2221 $encExt = $dbw->addQuotes( $dotExt );
2222 list( $oldRels, $deleteCurrent ) = $this->getOldRels();
2224 // Bitfields to further suppress the content
2225 if ( $this->suppress
) {
2227 // This should be 15...
2228 $bitfield |
= Revision
::DELETED_TEXT
;
2229 $bitfield |
= Revision
::DELETED_COMMENT
;
2230 $bitfield |
= Revision
::DELETED_USER
;
2231 $bitfield |
= Revision
::DELETED_RESTRICTED
;
2233 $bitfield = 'oi_deleted';
2236 if ( $deleteCurrent ) {
2241 'fa_storage_group' => $encGroup,
2242 'fa_storage_key' => $dbw->conditional(
2243 [ 'img_sha1' => '' ],
2244 $dbw->addQuotes( '' ),
2245 $dbw->buildConcat( [ "img_sha1", $encExt ] )
2247 'fa_deleted_user' => $encUserId,
2248 'fa_deleted_timestamp' => $encTimestamp,
2249 'fa_deleted_reason' => $encReason,
2250 'fa_deleted' => $this->suppress ?
$bitfield : 0,
2252 'fa_name' => 'img_name',
2253 'fa_archive_name' => 'NULL',
2254 'fa_size' => 'img_size',
2255 'fa_width' => 'img_width',
2256 'fa_height' => 'img_height',
2257 'fa_metadata' => 'img_metadata',
2258 'fa_bits' => 'img_bits',
2259 'fa_media_type' => 'img_media_type',
2260 'fa_major_mime' => 'img_major_mime',
2261 'fa_minor_mime' => 'img_minor_mime',
2262 'fa_description' => 'img_description',
2263 'fa_user' => 'img_user',
2264 'fa_user_text' => 'img_user_text',
2265 'fa_timestamp' => 'img_timestamp',
2266 'fa_sha1' => 'img_sha1'
2268 [ 'img_name' => $this->file
->getName() ],
2273 if ( count( $oldRels ) ) {
2274 $res = $dbw->select(
2276 OldLocalFile
::selectFields(),
2278 'oi_name' => $this->file
->getName(),
2279 'oi_archive_name' => array_keys( $oldRels )
2285 foreach ( $res as $row ) {
2287 // Deletion-specific fields
2288 'fa_storage_group' => 'deleted',
2289 'fa_storage_key' => ( $row->oi_sha1
=== '' )
2291 : "{$row->oi_sha1}{$dotExt}",
2292 'fa_deleted_user' => $this->user
->getId(),
2293 'fa_deleted_timestamp' => $dbw->timestamp( $now ),
2294 'fa_deleted_reason' => $this->reason
,
2295 // Counterpart fields
2296 'fa_deleted' => $this->suppress ?
$bitfield : $row->oi_deleted
,
2297 'fa_name' => $row->oi_name
,
2298 'fa_archive_name' => $row->oi_archive_name
,
2299 'fa_size' => $row->oi_size
,
2300 'fa_width' => $row->oi_width
,
2301 'fa_height' => $row->oi_height
,
2302 'fa_metadata' => $row->oi_metadata
,
2303 'fa_bits' => $row->oi_bits
,
2304 'fa_media_type' => $row->oi_media_type
,
2305 'fa_major_mime' => $row->oi_major_mime
,
2306 'fa_minor_mime' => $row->oi_minor_mime
,
2307 'fa_description' => $row->oi_description
,
2308 'fa_user' => $row->oi_user
,
2309 'fa_user_text' => $row->oi_user_text
,
2310 'fa_timestamp' => $row->oi_timestamp
,
2311 'fa_sha1' => $row->oi_sha1
2315 $dbw->insert( 'filearchive', $rowsInsert, __METHOD__
);
2319 function doDBDeletes() {
2320 $dbw = $this->file
->repo
->getMasterDB();
2321 list( $oldRels, $deleteCurrent ) = $this->getOldRels();
2323 if ( count( $oldRels ) ) {
2324 $dbw->delete( 'oldimage',
2326 'oi_name' => $this->file
->getName(),
2327 'oi_archive_name' => array_keys( $oldRels )
2331 if ( $deleteCurrent ) {
2332 $dbw->delete( 'image', [ 'img_name' => $this->file
->getName() ], __METHOD__
);
2337 * Run the transaction
2338 * @return FileRepoStatus
2340 public function execute() {
2341 $repo = $this->file
->getRepo();
2342 $this->file
->lock();
2344 // Prepare deletion batch
2345 $hashes = $this->getHashes();
2346 $this->deletionBatch
= [];
2347 $ext = $this->file
->getExtension();
2348 $dotExt = $ext === '' ?
'' : ".$ext";
2350 foreach ( $this->srcRels
as $name => $srcRel ) {
2351 // Skip files that have no hash (e.g. missing DB record, or sha1 field and file source)
2352 if ( isset( $hashes[$name] ) ) {
2353 $hash = $hashes[$name];
2354 $key = $hash . $dotExt;
2355 $dstRel = $repo->getDeletedHashPath( $key ) . $key;
2356 $this->deletionBatch
[$name] = [ $srcRel, $dstRel ];
2360 if ( !$repo->hasSha1Storage() ) {
2361 // Removes non-existent file from the batch, so we don't get errors.
2362 // This also handles files in the 'deleted' zone deleted via revision deletion.
2363 $checkStatus = $this->removeNonexistentFiles( $this->deletionBatch
);
2364 if ( !$checkStatus->isGood() ) {
2365 $this->status
->merge( $checkStatus );
2366 return $this->status
;
2368 $this->deletionBatch
= $checkStatus->value
;
2370 // Execute the file deletion batch
2371 $status = $this->file
->repo
->deleteBatch( $this->deletionBatch
);
2372 if ( !$status->isGood() ) {
2373 $this->status
->merge( $status );
2377 if ( !$this->status
->isOK() ) {
2378 // Critical file deletion error; abort
2379 $this->file
->unlock();
2381 return $this->status
;
2384 // Copy the image/oldimage rows to filearchive
2385 $this->doDBInserts();
2386 // Delete image/oldimage rows
2387 $this->doDBDeletes();
2389 // Commit and return
2390 $this->file
->unlock();
2392 return $this->status
;
2396 * Removes non-existent files from a deletion batch.
2397 * @param array $batch
2400 protected function removeNonexistentFiles( $batch ) {
2401 $files = $newBatch = [];
2403 foreach ( $batch as $batchItem ) {
2404 list( $src, ) = $batchItem;
2405 $files[$src] = $this->file
->repo
->getVirtualUrl( 'public' ) . '/' . rawurlencode( $src );
2408 $result = $this->file
->repo
->fileExistsBatch( $files );
2409 if ( in_array( null, $result, true ) ) {
2410 return Status
::newFatal( 'backend-fail-internal',
2411 $this->file
->repo
->getBackend()->getName() );
2414 foreach ( $batch as $batchItem ) {
2415 if ( $result[$batchItem[0]] ) {
2416 $newBatch[] = $batchItem;
2420 return Status
::newGood( $newBatch );
2424 # ------------------------------------------------------------------------------
2427 * Helper class for file undeletion
2428 * @ingroup FileAbstraction
2430 class LocalFileRestoreBatch
{
2431 /** @var LocalFile */
2434 /** @var array List of file IDs to restore */
2435 private $cleanupBatch;
2437 /** @var array List of file IDs to restore */
2440 /** @var bool Add all revisions of the file */
2443 /** @var bool Whether to remove all settings for suppressed fields */
2444 private $unsuppress = false;
2448 * @param bool $unsuppress
2450 function __construct( File
$file, $unsuppress = false ) {
2451 $this->file
= $file;
2452 $this->cleanupBatch
= $this->ids
= [];
2454 $this->unsuppress
= $unsuppress;
2461 public function addId( $fa_id ) {
2462 $this->ids
[] = $fa_id;
2466 * Add a whole lot of files by ID
2469 public function addIds( $ids ) {
2470 $this->ids
= array_merge( $this->ids
, $ids );
2474 * Add all revisions of the file
2476 public function addAll() {
2481 * Run the transaction, except the cleanup batch.
2482 * The cleanup batch should be run in a separate transaction, because it locks different
2483 * rows and there's no need to keep the image row locked while it's acquiring those locks
2484 * The caller may have its own transaction open.
2485 * So we save the batch and let the caller call cleanup()
2486 * @return FileRepoStatus
2488 public function execute() {
2489 /** @var Language */
2492 $repo = $this->file
->getRepo();
2493 if ( !$this->all
&& !$this->ids
) {
2495 return $repo->newGood();
2498 $lockOwnsTrx = $this->file
->lock();
2500 $dbw = $this->file
->repo
->getMasterDB();
2501 $status = $this->file
->repo
->newGood();
2503 $exists = (bool)$dbw->selectField( 'image', '1',
2504 [ 'img_name' => $this->file
->getName() ],
2506 // The lock() should already prevents changes, but this still may need
2507 // to bypass any transaction snapshot. However, if lock() started the
2508 // trx (which it probably did) then snapshot is post-lock and up-to-date.
2509 $lockOwnsTrx ?
[] : [ 'LOCK IN SHARE MODE' ]
2512 // Fetch all or selected archived revisions for the file,
2513 // sorted from the most recent to the oldest.
2514 $conditions = [ 'fa_name' => $this->file
->getName() ];
2516 if ( !$this->all
) {
2517 $conditions['fa_id'] = $this->ids
;
2520 $result = $dbw->select(
2522 ArchivedFile
::selectFields(),
2525 [ 'ORDER BY' => 'fa_timestamp DESC' ]
2531 $insertCurrent = false;
2536 foreach ( $result as $row ) {
2537 $idsPresent[] = $row->fa_id
;
2539 if ( $row->fa_name
!= $this->file
->getName() ) {
2540 $status->error( 'undelete-filename-mismatch', $wgLang->timeanddate( $row->fa_timestamp
) );
2541 $status->failCount++
;
2545 if ( $row->fa_storage_key
== '' ) {
2546 // Revision was missing pre-deletion
2547 $status->error( 'undelete-bad-store-key', $wgLang->timeanddate( $row->fa_timestamp
) );
2548 $status->failCount++
;
2552 $deletedRel = $repo->getDeletedHashPath( $row->fa_storage_key
) .
2553 $row->fa_storage_key
;
2554 $deletedUrl = $repo->getVirtualUrl() . '/deleted/' . $deletedRel;
2556 if ( isset( $row->fa_sha1
) ) {
2557 $sha1 = $row->fa_sha1
;
2559 // old row, populate from key
2560 $sha1 = LocalRepo
::getHashFromKey( $row->fa_storage_key
);
2564 if ( strlen( $sha1 ) == 32 && $sha1[0] == '0' ) {
2565 $sha1 = substr( $sha1, 1 );
2568 if ( is_null( $row->fa_major_mime
) ||
$row->fa_major_mime
== 'unknown'
2569 ||
is_null( $row->fa_minor_mime
) ||
$row->fa_minor_mime
== 'unknown'
2570 ||
is_null( $row->fa_media_type
) ||
$row->fa_media_type
== 'UNKNOWN'
2571 ||
is_null( $row->fa_metadata
)
2573 // Refresh our metadata
2574 // Required for a new current revision; nice for older ones too. :)
2575 $props = RepoGroup
::singleton()->getFileProps( $deletedUrl );
2578 'minor_mime' => $row->fa_minor_mime
,
2579 'major_mime' => $row->fa_major_mime
,
2580 'media_type' => $row->fa_media_type
,
2581 'metadata' => $row->fa_metadata
2585 if ( $first && !$exists ) {
2586 // This revision will be published as the new current version
2587 $destRel = $this->file
->getRel();
2589 'img_name' => $row->fa_name
,
2590 'img_size' => $row->fa_size
,
2591 'img_width' => $row->fa_width
,
2592 'img_height' => $row->fa_height
,
2593 'img_metadata' => $props['metadata'],
2594 'img_bits' => $row->fa_bits
,
2595 'img_media_type' => $props['media_type'],
2596 'img_major_mime' => $props['major_mime'],
2597 'img_minor_mime' => $props['minor_mime'],
2598 'img_description' => $row->fa_description
,
2599 'img_user' => $row->fa_user
,
2600 'img_user_text' => $row->fa_user_text
,
2601 'img_timestamp' => $row->fa_timestamp
,
2605 // The live (current) version cannot be hidden!
2606 if ( !$this->unsuppress
&& $row->fa_deleted
) {
2607 $status->fatal( 'undeleterevdel' );
2608 $this->file
->unlock();
2612 $archiveName = $row->fa_archive_name
;
2614 if ( $archiveName == '' ) {
2615 // This was originally a current version; we
2616 // have to devise a new archive name for it.
2617 // Format is <timestamp of archiving>!<name>
2618 $timestamp = wfTimestamp( TS_UNIX
, $row->fa_deleted_timestamp
);
2621 $archiveName = wfTimestamp( TS_MW
, $timestamp ) . '!' . $row->fa_name
;
2623 } while ( isset( $archiveNames[$archiveName] ) );
2626 $archiveNames[$archiveName] = true;
2627 $destRel = $this->file
->getArchiveRel( $archiveName );
2629 'oi_name' => $row->fa_name
,
2630 'oi_archive_name' => $archiveName,
2631 'oi_size' => $row->fa_size
,
2632 'oi_width' => $row->fa_width
,
2633 'oi_height' => $row->fa_height
,
2634 'oi_bits' => $row->fa_bits
,
2635 'oi_description' => $row->fa_description
,
2636 'oi_user' => $row->fa_user
,
2637 'oi_user_text' => $row->fa_user_text
,
2638 'oi_timestamp' => $row->fa_timestamp
,
2639 'oi_metadata' => $props['metadata'],
2640 'oi_media_type' => $props['media_type'],
2641 'oi_major_mime' => $props['major_mime'],
2642 'oi_minor_mime' => $props['minor_mime'],
2643 'oi_deleted' => $this->unsuppress ?
0 : $row->fa_deleted
,
2644 'oi_sha1' => $sha1 ];
2647 $deleteIds[] = $row->fa_id
;
2649 if ( !$this->unsuppress
&& $row->fa_deleted
& File
::DELETED_FILE
) {
2650 // private files can stay where they are
2651 $status->successCount++
;
2653 $storeBatch[] = [ $deletedUrl, 'public', $destRel ];
2654 $this->cleanupBatch
[] = $row->fa_storage_key
;
2662 // Add a warning to the status object for missing IDs
2663 $missingIds = array_diff( $this->ids
, $idsPresent );
2665 foreach ( $missingIds as $id ) {
2666 $status->error( 'undelete-missing-filearchive', $id );
2669 if ( !$repo->hasSha1Storage() ) {
2670 // Remove missing files from batch, so we don't get errors when undeleting them
2671 $checkStatus = $this->removeNonexistentFiles( $storeBatch );
2672 if ( !$checkStatus->isGood() ) {
2673 $status->merge( $checkStatus );
2676 $storeBatch = $checkStatus->value
;
2678 // Run the store batch
2679 // Use the OVERWRITE_SAME flag to smooth over a common error
2680 $storeStatus = $this->file
->repo
->storeBatch( $storeBatch, FileRepo
::OVERWRITE_SAME
);
2681 $status->merge( $storeStatus );
2683 if ( !$status->isGood() ) {
2684 // Even if some files could be copied, fail entirely as that is the
2685 // easiest thing to do without data loss
2686 $this->cleanupFailedBatch( $storeStatus, $storeBatch );
2687 $status->ok
= false;
2688 $this->file
->unlock();
2694 // Run the DB updates
2695 // Because we have locked the image row, key conflicts should be rare.
2696 // If they do occur, we can roll back the transaction at this time with
2697 // no data loss, but leaving unregistered files scattered throughout the
2699 // This is not ideal, which is why it's important to lock the image row.
2700 if ( $insertCurrent ) {
2701 $dbw->insert( 'image', $insertCurrent, __METHOD__
);
2704 if ( $insertBatch ) {
2705 $dbw->insert( 'oldimage', $insertBatch, __METHOD__
);
2709 $dbw->delete( 'filearchive',
2710 [ 'fa_id' => $deleteIds ],
2714 // If store batch is empty (all files are missing), deletion is to be considered successful
2715 if ( $status->successCount
> 0 ||
!$storeBatch ||
$repo->hasSha1Storage() ) {
2717 wfDebug( __METHOD__
. " restored {$status->successCount} items, creating a new current\n" );
2719 DeferredUpdates
::addUpdate( SiteStatsUpdate
::factory( [ 'images' => 1 ] ) );
2721 $this->file
->purgeEverything();
2723 wfDebug( __METHOD__
. " restored {$status->successCount} as archived versions\n" );
2724 $this->file
->purgeDescription();
2728 $this->file
->unlock();
2734 * Removes non-existent files from a store batch.
2735 * @param array $triplets
2738 protected function removeNonexistentFiles( $triplets ) {
2739 $files = $filteredTriplets = [];
2740 foreach ( $triplets as $file ) {
2741 $files[$file[0]] = $file[0];
2744 $result = $this->file
->repo
->fileExistsBatch( $files );
2745 if ( in_array( null, $result, true ) ) {
2746 return Status
::newFatal( 'backend-fail-internal',
2747 $this->file
->repo
->getBackend()->getName() );
2750 foreach ( $triplets as $file ) {
2751 if ( $result[$file[0]] ) {
2752 $filteredTriplets[] = $file;
2756 return Status
::newGood( $filteredTriplets );
2760 * Removes non-existent files from a cleanup batch.
2761 * @param array $batch
2764 protected function removeNonexistentFromCleanup( $batch ) {
2765 $files = $newBatch = [];
2766 $repo = $this->file
->repo
;
2768 foreach ( $batch as $file ) {
2769 $files[$file] = $repo->getVirtualUrl( 'deleted' ) . '/' .
2770 rawurlencode( $repo->getDeletedHashPath( $file ) . $file );
2773 $result = $repo->fileExistsBatch( $files );
2775 foreach ( $batch as $file ) {
2776 if ( $result[$file] ) {
2777 $newBatch[] = $file;
2785 * Delete unused files in the deleted zone.
2786 * This should be called from outside the transaction in which execute() was called.
2787 * @return FileRepoStatus
2789 public function cleanup() {
2790 if ( !$this->cleanupBatch
) {
2791 return $this->file
->repo
->newGood();
2794 $this->cleanupBatch
= $this->removeNonexistentFromCleanup( $this->cleanupBatch
);
2796 $status = $this->file
->repo
->cleanupDeletedBatch( $this->cleanupBatch
);
2802 * Cleanup a failed batch. The batch was only partially successful, so
2803 * rollback by removing all items that were succesfully copied.
2805 * @param Status $storeStatus
2806 * @param array $storeBatch
2808 protected function cleanupFailedBatch( $storeStatus, $storeBatch ) {
2811 foreach ( $storeStatus->success
as $i => $success ) {
2812 // Check if this item of the batch was successfully copied
2814 // Item was successfully copied and needs to be removed again
2815 // Extract ($dstZone, $dstRel) from the batch
2816 $cleanupBatch[] = [ $storeBatch[$i][1], $storeBatch[$i][2] ];
2819 $this->file
->repo
->cleanupBatch( $cleanupBatch );
2823 # ------------------------------------------------------------------------------
2826 * Helper class for file movement
2827 * @ingroup FileAbstraction
2829 class LocalFileMoveBatch
{
2830 /** @var LocalFile */
2840 protected $oldCount;
2844 /** @var DatabaseBase */
2849 * @param Title $target
2851 function __construct( File
$file, Title
$target ) {
2852 $this->file
= $file;
2853 $this->target
= $target;
2854 $this->oldHash
= $this->file
->repo
->getHashPath( $this->file
->getName() );
2855 $this->newHash
= $this->file
->repo
->getHashPath( $this->target
->getDBkey() );
2856 $this->oldName
= $this->file
->getName();
2857 $this->newName
= $this->file
->repo
->getNameFromTitle( $this->target
);
2858 $this->oldRel
= $this->oldHash
. $this->oldName
;
2859 $this->newRel
= $this->newHash
. $this->newName
;
2860 $this->db
= $file->getRepo()->getMasterDB();
2864 * Add the current image to the batch
2866 public function addCurrent() {
2867 $this->cur
= [ $this->oldRel
, $this->newRel
];
2871 * Add the old versions of the image to the batch
2872 * @return array List of archive names from old versions
2874 public function addOlds() {
2875 $archiveBase = 'archive';
2877 $this->oldCount
= 0;
2880 $result = $this->db
->select( 'oldimage',
2881 [ 'oi_archive_name', 'oi_deleted' ],
2882 [ 'oi_name' => $this->oldName
],
2884 [ 'LOCK IN SHARE MODE' ] // ignore snapshot
2887 foreach ( $result as $row ) {
2888 $archiveNames[] = $row->oi_archive_name
;
2889 $oldName = $row->oi_archive_name
;
2890 $bits = explode( '!', $oldName, 2 );
2892 if ( count( $bits ) != 2 ) {
2893 wfDebug( "Old file name missing !: '$oldName' \n" );
2897 list( $timestamp, $filename ) = $bits;
2899 if ( $this->oldName
!= $filename ) {
2900 wfDebug( "Old file name doesn't match: '$oldName' \n" );
2906 // Do we want to add those to oldCount?
2907 if ( $row->oi_deleted
& File
::DELETED_FILE
) {
2912 "{$archiveBase}/{$this->oldHash}{$oldName}",
2913 "{$archiveBase}/{$this->newHash}{$timestamp}!{$this->newName}"
2917 return $archiveNames;
2922 * @return FileRepoStatus
2924 public function execute() {
2925 $repo = $this->file
->repo
;
2926 $status = $repo->newGood();
2927 $destFile = wfLocalFile( $this->target
);
2929 $this->file
->lock(); // begin
2930 $destFile->lock(); // quickly fail if destination is not available
2932 $triplets = $this->getMoveTriplets();
2933 $checkStatus = $this->removeNonexistentFiles( $triplets );
2934 if ( !$checkStatus->isGood() ) {
2935 $destFile->unlock();
2936 $this->file
->unlock();
2937 $status->merge( $checkStatus ); // couldn't talk to file backend
2940 $triplets = $checkStatus->value
;
2942 // Verify the file versions metadata in the DB.
2943 $statusDb = $this->verifyDBUpdates();
2944 if ( !$statusDb->isGood() ) {
2945 $destFile->unlock();
2946 $this->file
->unlock();
2947 $statusDb->ok
= false;
2952 if ( !$repo->hasSha1Storage() ) {
2953 // Copy the files into their new location.
2954 // If a prior process fataled copying or cleaning up files we tolerate any
2955 // of the existing files if they are identical to the ones being stored.
2956 $statusMove = $repo->storeBatch( $triplets, FileRepo
::OVERWRITE_SAME
);
2957 wfDebugLog( 'imagemove', "Moved files for {$this->file->getName()}: " .
2958 "{$statusMove->successCount} successes, {$statusMove->failCount} failures" );
2959 if ( !$statusMove->isGood() ) {
2960 // Delete any files copied over (while the destination is still locked)
2961 $this->cleanupTarget( $triplets );
2962 $destFile->unlock();
2963 $this->file
->unlock();
2964 wfDebugLog( 'imagemove', "Error in moving files: "
2965 . $statusMove->getWikiText( false, false, 'en' ) );
2966 $statusMove->ok
= false;
2970 $status->merge( $statusMove );
2973 // Rename the file versions metadata in the DB.
2974 $this->doDBUpdates();
2976 wfDebugLog( 'imagemove', "Renamed {$this->file->getName()} in database: " .
2977 "{$statusDb->successCount} successes, {$statusDb->failCount} failures" );
2979 $destFile->unlock();
2980 $this->file
->unlock(); // done
2982 // Everything went ok, remove the source files
2983 $this->cleanupSource( $triplets );
2985 $status->merge( $statusDb );
2991 * Verify the database updates and return a new FileRepoStatus indicating how
2992 * many rows would be updated.
2994 * @return FileRepoStatus
2996 protected function verifyDBUpdates() {
2997 $repo = $this->file
->repo
;
2998 $status = $repo->newGood();
3001 $hasCurrent = $dbw->selectField(
3004 [ 'img_name' => $this->oldName
],
3008 $oldRowCount = $dbw->selectField(
3011 [ 'oi_name' => $this->oldName
],
3016 if ( $hasCurrent ) {
3017 $status->successCount++
;
3019 $status->failCount++
;
3021 $status->successCount +
= $oldRowCount;
3022 // Bug 34934: oldCount is based on files that actually exist.
3023 // There may be more DB rows than such files, in which case $affected
3024 // can be greater than $total. We use max() to avoid negatives here.
3025 $status->failCount +
= max( 0, $this->oldCount
- $oldRowCount );
3026 if ( $status->failCount
) {
3027 $status->error( 'imageinvalidfilename' );
3034 * Do the database updates and return a new FileRepoStatus indicating how
3035 * many rows where updated.
3037 protected function doDBUpdates() {
3040 // Update current image
3043 [ 'img_name' => $this->newName
],
3044 [ 'img_name' => $this->oldName
],
3047 // Update old images
3051 'oi_name' => $this->newName
,
3052 'oi_archive_name = ' . $dbw->strreplace( 'oi_archive_name',
3053 $dbw->addQuotes( $this->oldName
), $dbw->addQuotes( $this->newName
) ),
3055 [ 'oi_name' => $this->oldName
],
3061 * Generate triplets for FileRepo::storeBatch().
3064 protected function getMoveTriplets() {
3065 $moves = array_merge( [ $this->cur
], $this->olds
);
3066 $triplets = []; // The format is: (srcUrl, destZone, destUrl)
3068 foreach ( $moves as $move ) {
3069 // $move: (oldRelativePath, newRelativePath)
3070 $srcUrl = $this->file
->repo
->getVirtualUrl() . '/public/' . rawurlencode( $move[0] );
3071 $triplets[] = [ $srcUrl, 'public', $move[1] ];
3074 "Generated move triplet for {$this->file->getName()}: {$srcUrl} :: public :: {$move[1]}"
3082 * Removes non-existent files from move batch.
3083 * @param array $triplets
3086 protected function removeNonexistentFiles( $triplets ) {
3089 foreach ( $triplets as $file ) {
3090 $files[$file[0]] = $file[0];
3093 $result = $this->file
->repo
->fileExistsBatch( $files );
3094 if ( in_array( null, $result, true ) ) {
3095 return Status
::newFatal( 'backend-fail-internal',
3096 $this->file
->repo
->getBackend()->getName() );
3099 $filteredTriplets = [];
3100 foreach ( $triplets as $file ) {
3101 if ( $result[$file[0]] ) {
3102 $filteredTriplets[] = $file;
3104 wfDebugLog( 'imagemove', "File {$file[0]} does not exist" );
3108 return Status
::newGood( $filteredTriplets );
3112 * Cleanup a partially moved array of triplets by deleting the target
3113 * files. Called if something went wrong half way.
3114 * @param array $triplets
3116 protected function cleanupTarget( $triplets ) {
3117 // Create dest pairs from the triplets
3119 foreach ( $triplets as $triplet ) {
3120 // $triplet: (old source virtual URL, dst zone, dest rel)
3121 $pairs[] = [ $triplet[1], $triplet[2] ];
3124 $this->file
->repo
->cleanupBatch( $pairs );
3128 * Cleanup a fully moved array of triplets by deleting the source files.
3129 * Called at the end of the move process if everything else went ok.
3130 * @param array $triplets
3132 protected function cleanupSource( $triplets ) {
3133 // Create source file names from the triplets
3135 foreach ( $triplets as $triplet ) {
3136 $files[] = $triplet[0];
3139 $this->file
->repo
->cleanupBatch( $files );
3143 class LocalFileLockError
extends ErrorPageError
{
3144 public function __construct( Status
$status ) {
3145 parent
::__construct(
3147 $status->getMessage()
3151 public function report() {
3153 $wgOut->setStatusCode( 429 );