2a15fd7ab6d9a8f5f471340a008539c055ecca36
[lhc/web/wiklou.git] / includes / filerepo / file / LocalFile.php
1 <?php
2 /**
3 * Local file in the wiki's own database.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup FileAbstraction
22 */
23
24 /**
25 * Bump this number when serialized cache records may be incompatible.
26 */
27 define( 'MW_FILE_VERSION', 9 );
28
29 /**
30 * Class to represent a local file in the wiki's own database
31 *
32 * Provides methods to retrieve paths (physical, logical, URL),
33 * to generate image thumbnails or for uploading.
34 *
35 * Note that only the repo object knows what its file class is called. You should
36 * never name a file class explictly outside of the repo class. Instead use the
37 * repo's factory functions to generate file objects, for example:
38 *
39 * RepoGroup::singleton()->getLocalRepo()->newFile( $title );
40 *
41 * The convenience functions wfLocalFile() and wfFindFile() should be sufficient
42 * in most cases.
43 *
44 * @ingroup FileAbstraction
45 */
46 class LocalFile extends File {
47 const CACHE_FIELD_MAX_LEN = 1000;
48
49 /** @var bool Does the file exist on disk? (loadFromXxx) */
50 protected $fileExists;
51
52 /** @var int Image width */
53 protected $width;
54
55 /** @var int Image height */
56 protected $height;
57
58 /** @var int Returned by getimagesize (loadFromXxx) */
59 protected $bits;
60
61 /** @var string MEDIATYPE_xxx (bitmap, drawing, audio...) */
62 protected $media_type;
63
64 /** @var string MIME type, determined by MimeMagic::guessMimeType */
65 protected $mime;
66
67 /** @var int Size in bytes (loadFromXxx) */
68 protected $size;
69
70 /** @var string Handler-specific metadata */
71 protected $metadata;
72
73 /** @var string SHA-1 base 36 content hash */
74 protected $sha1;
75
76 /** @var bool Whether or not core data has been loaded from the database (loadFromXxx) */
77 protected $dataLoaded;
78
79 /** @var bool Whether or not lazy-loaded data has been loaded from the database */
80 protected $extraDataLoaded;
81
82 /** @var int Bitfield akin to rev_deleted */
83 protected $deleted;
84
85 /** @var string */
86 protected $repoClass = 'LocalRepo';
87
88 /** @var int Number of line to return by nextHistoryLine() (constructor) */
89 private $historyLine;
90
91 /** @var int Result of the query for the file's history (nextHistoryLine) */
92 private $historyRes;
93
94 /** @var string Major MIME type */
95 private $major_mime;
96
97 /** @var string Minor MIME type */
98 private $minor_mime;
99
100 /** @var string Upload timestamp */
101 private $timestamp;
102
103 /** @var int User ID of uploader */
104 private $user;
105
106 /** @var string User name of uploader */
107 private $user_text;
108
109 /** @var string Description of current revision of the file */
110 private $description;
111
112 /** @var string TS_MW timestamp of the last change of the file description */
113 private $descriptionTouched;
114
115 /** @var bool Whether the row was upgraded on load */
116 private $upgraded;
117
118 /** @var bool True if the image row is locked */
119 private $locked;
120
121 /** @var bool True if the image row is locked with a lock initiated transaction */
122 private $lockedOwnTrx;
123
124 /** @var bool True if file is not present in file system. Not to be cached in memcached */
125 private $missing;
126
127 // @note: higher than IDBAccessObject constants
128 const LOAD_ALL = 16; // integer; load all the lazy fields too (like metadata)
129
130 /**
131 * Create a LocalFile from a title
132 * Do not call this except from inside a repo class.
133 *
134 * Note: $unused param is only here to avoid an E_STRICT
135 *
136 * @param Title $title
137 * @param FileRepo $repo
138 * @param null $unused
139 *
140 * @return LocalFile
141 */
142 static function newFromTitle( $title, $repo, $unused = null ) {
143 return new self( $title, $repo );
144 }
145
146 /**
147 * Create a LocalFile from a title
148 * Do not call this except from inside a repo class.
149 *
150 * @param stdClass $row
151 * @param FileRepo $repo
152 *
153 * @return LocalFile
154 */
155 static function newFromRow( $row, $repo ) {
156 $title = Title::makeTitle( NS_FILE, $row->img_name );
157 $file = new self( $title, $repo );
158 $file->loadFromRow( $row );
159
160 return $file;
161 }
162
163 /**
164 * Create a LocalFile from a SHA-1 key
165 * Do not call this except from inside a repo class.
166 *
167 * @param string $sha1 Base-36 SHA-1
168 * @param LocalRepo $repo
169 * @param string|bool $timestamp MW_timestamp (optional)
170 * @return bool|LocalFile
171 */
172 static function newFromKey( $sha1, $repo, $timestamp = false ) {
173 $dbr = $repo->getSlaveDB();
174
175 $conds = [ 'img_sha1' => $sha1 ];
176 if ( $timestamp ) {
177 $conds['img_timestamp'] = $dbr->timestamp( $timestamp );
178 }
179
180 $row = $dbr->selectRow( 'image', self::selectFields(), $conds, __METHOD__ );
181 if ( $row ) {
182 return self::newFromRow( $row, $repo );
183 } else {
184 return false;
185 }
186 }
187
188 /**
189 * Fields in the image table
190 * @return array
191 */
192 static function selectFields() {
193 return [
194 'img_name',
195 'img_size',
196 'img_width',
197 'img_height',
198 'img_metadata',
199 'img_bits',
200 'img_media_type',
201 'img_major_mime',
202 'img_minor_mime',
203 'img_description',
204 'img_user',
205 'img_user_text',
206 'img_timestamp',
207 'img_sha1',
208 ];
209 }
210
211 /**
212 * Constructor.
213 * Do not call this except from inside a repo class.
214 * @param Title $title
215 * @param FileRepo $repo
216 */
217 function __construct( $title, $repo ) {
218 parent::__construct( $title, $repo );
219
220 $this->metadata = '';
221 $this->historyLine = 0;
222 $this->historyRes = null;
223 $this->dataLoaded = false;
224 $this->extraDataLoaded = false;
225
226 $this->assertRepoDefined();
227 $this->assertTitleDefined();
228 }
229
230 /**
231 * Get the memcached key for the main data for this file, or false if
232 * there is no access to the shared cache.
233 * @return string|bool
234 */
235 function getCacheKey() {
236 $hashedName = md5( $this->getName() );
237
238 return $this->repo->getSharedCacheKey( 'file', $hashedName );
239 }
240
241 /**
242 * Try to load file metadata from memcached. Returns true on success.
243 * @return bool
244 */
245 private function loadFromCache() {
246 $this->dataLoaded = false;
247 $this->extraDataLoaded = false;
248 $key = $this->getCacheKey();
249
250 if ( !$key ) {
251 return false;
252 }
253
254 $cache = ObjectCache::getMainWANInstance();
255 $cachedValues = $cache->get( $key );
256
257 // Check if the key existed and belongs to this version of MediaWiki
258 if ( is_array( $cachedValues ) && $cachedValues['version'] == MW_FILE_VERSION ) {
259 $this->fileExists = $cachedValues['fileExists'];
260 if ( $this->fileExists ) {
261 $this->setProps( $cachedValues );
262 }
263 $this->dataLoaded = true;
264 $this->extraDataLoaded = true;
265 foreach ( $this->getLazyCacheFields( '' ) as $field ) {
266 $this->extraDataLoaded = $this->extraDataLoaded && isset( $cachedValues[$field] );
267 }
268 }
269
270 return $this->dataLoaded;
271 }
272
273 /**
274 * Save the file metadata to memcached
275 */
276 private function saveToCache() {
277 $this->load();
278
279 $key = $this->getCacheKey();
280 if ( !$key ) {
281 return;
282 }
283
284 $fields = $this->getCacheFields( '' );
285 $cacheVal = [ 'version' => MW_FILE_VERSION ];
286 $cacheVal['fileExists'] = $this->fileExists;
287
288 if ( $this->fileExists ) {
289 foreach ( $fields as $field ) {
290 $cacheVal[$field] = $this->$field;
291 }
292 }
293
294 // Strip off excessive entries from the subset of fields that can become large.
295 // If the cache value gets to large it will not fit in memcached and nothing will
296 // get cached at all, causing master queries for any file access.
297 foreach ( $this->getLazyCacheFields( '' ) as $field ) {
298 if ( isset( $cacheVal[$field] ) && strlen( $cacheVal[$field] ) > 100 * 1024 ) {
299 unset( $cacheVal[$field] ); // don't let the value get too big
300 }
301 }
302
303 // Cache presence for 1 week and negatives for 1 day
304 $ttl = $this->fileExists ? 86400 * 7 : 86400;
305 $opts = Database::getCacheSetOptions( $this->repo->getSlaveDB() );
306 ObjectCache::getMainWANInstance()->set( $key, $cacheVal, $ttl, $opts );
307 }
308
309 /**
310 * Purge the file object/metadata cache
311 */
312 public function invalidateCache() {
313 $key = $this->getCacheKey();
314 if ( !$key ) {
315 return;
316 }
317
318 $this->repo->getMasterDB()->onTransactionPreCommitOrIdle( function() use ( $key ) {
319 ObjectCache::getMainWANInstance()->delete( $key );
320 } );
321 }
322
323 /**
324 * Load metadata from the file itself
325 */
326 function loadFromFile() {
327 $props = $this->repo->getFileProps( $this->getVirtualUrl() );
328 $this->setProps( $props );
329 }
330
331 /**
332 * @param string $prefix
333 * @return array
334 */
335 function getCacheFields( $prefix = 'img_' ) {
336 static $fields = [ 'size', 'width', 'height', 'bits', 'media_type',
337 'major_mime', 'minor_mime', 'metadata', 'timestamp', 'sha1', 'user',
338 'user_text', 'description' ];
339 static $results = [];
340
341 if ( $prefix == '' ) {
342 return $fields;
343 }
344
345 if ( !isset( $results[$prefix] ) ) {
346 $prefixedFields = [];
347 foreach ( $fields as $field ) {
348 $prefixedFields[] = $prefix . $field;
349 }
350 $results[$prefix] = $prefixedFields;
351 }
352
353 return $results[$prefix];
354 }
355
356 /**
357 * @param string $prefix
358 * @return array
359 */
360 function getLazyCacheFields( $prefix = 'img_' ) {
361 static $fields = [ 'metadata' ];
362 static $results = [];
363
364 if ( $prefix == '' ) {
365 return $fields;
366 }
367
368 if ( !isset( $results[$prefix] ) ) {
369 $prefixedFields = [];
370 foreach ( $fields as $field ) {
371 $prefixedFields[] = $prefix . $field;
372 }
373 $results[$prefix] = $prefixedFields;
374 }
375
376 return $results[$prefix];
377 }
378
379 /**
380 * Load file metadata from the DB
381 * @param int $flags
382 */
383 function loadFromDB( $flags = 0 ) {
384 $fname = get_class( $this ) . '::' . __FUNCTION__;
385
386 # Unconditionally set loaded=true, we don't want the accessors constantly rechecking
387 $this->dataLoaded = true;
388 $this->extraDataLoaded = true;
389
390 $dbr = ( $flags & self::READ_LATEST )
391 ? $this->repo->getMasterDB()
392 : $this->repo->getSlaveDB();
393
394 $row = $dbr->selectRow( 'image', $this->getCacheFields( 'img_' ),
395 [ 'img_name' => $this->getName() ], $fname );
396
397 if ( $row ) {
398 $this->loadFromRow( $row );
399 } else {
400 $this->fileExists = false;
401 }
402 }
403
404 /**
405 * Load lazy file metadata from the DB.
406 * This covers fields that are sometimes not cached.
407 */
408 protected function loadExtraFromDB() {
409 $fname = get_class( $this ) . '::' . __FUNCTION__;
410
411 # Unconditionally set loaded=true, we don't want the accessors constantly rechecking
412 $this->extraDataLoaded = true;
413
414 $fieldMap = $this->loadFieldsWithTimestamp( $this->repo->getSlaveDB(), $fname );
415 if ( !$fieldMap ) {
416 $fieldMap = $this->loadFieldsWithTimestamp( $this->repo->getMasterDB(), $fname );
417 }
418
419 if ( $fieldMap ) {
420 foreach ( $fieldMap as $name => $value ) {
421 $this->$name = $value;
422 }
423 } else {
424 throw new MWException( "Could not find data for image '{$this->getName()}'." );
425 }
426 }
427
428 /**
429 * @param IDatabase $dbr
430 * @param string $fname
431 * @return array|bool
432 */
433 private function loadFieldsWithTimestamp( $dbr, $fname ) {
434 $fieldMap = false;
435
436 $row = $dbr->selectRow( 'image', $this->getLazyCacheFields( 'img_' ),
437 [ 'img_name' => $this->getName(), 'img_timestamp' => $this->getTimestamp() ],
438 $fname );
439 if ( $row ) {
440 $fieldMap = $this->unprefixRow( $row, 'img_' );
441 } else {
442 # File may have been uploaded over in the meantime; check the old versions
443 $row = $dbr->selectRow( 'oldimage', $this->getLazyCacheFields( 'oi_' ),
444 [ 'oi_name' => $this->getName(), 'oi_timestamp' => $this->getTimestamp() ],
445 $fname );
446 if ( $row ) {
447 $fieldMap = $this->unprefixRow( $row, 'oi_' );
448 }
449 }
450
451 return $fieldMap;
452 }
453
454 /**
455 * @param array|object $row
456 * @param string $prefix
457 * @throws MWException
458 * @return array
459 */
460 protected function unprefixRow( $row, $prefix = 'img_' ) {
461 $array = (array)$row;
462 $prefixLength = strlen( $prefix );
463
464 // Sanity check prefix once
465 if ( substr( key( $array ), 0, $prefixLength ) !== $prefix ) {
466 throw new MWException( __METHOD__ . ': incorrect $prefix parameter' );
467 }
468
469 $decoded = [];
470 foreach ( $array as $name => $value ) {
471 $decoded[substr( $name, $prefixLength )] = $value;
472 }
473
474 return $decoded;
475 }
476
477 /**
478 * Decode a row from the database (either object or array) to an array
479 * with timestamps and MIME types decoded, and the field prefix removed.
480 * @param object $row
481 * @param string $prefix
482 * @throws MWException
483 * @return array
484 */
485 function decodeRow( $row, $prefix = 'img_' ) {
486 $decoded = $this->unprefixRow( $row, $prefix );
487
488 $decoded['timestamp'] = wfTimestamp( TS_MW, $decoded['timestamp'] );
489
490 $decoded['metadata'] = $this->repo->getSlaveDB()->decodeBlob( $decoded['metadata'] );
491
492 if ( empty( $decoded['major_mime'] ) ) {
493 $decoded['mime'] = 'unknown/unknown';
494 } else {
495 if ( !$decoded['minor_mime'] ) {
496 $decoded['minor_mime'] = 'unknown';
497 }
498 $decoded['mime'] = $decoded['major_mime'] . '/' . $decoded['minor_mime'];
499 }
500
501 // Trim zero padding from char/binary field
502 $decoded['sha1'] = rtrim( $decoded['sha1'], "\0" );
503
504 // Normalize some fields to integer type, per their database definition.
505 // Use unary + so that overflows will be upgraded to double instead of
506 // being trucated as with intval(). This is important to allow >2GB
507 // files on 32-bit systems.
508 foreach ( [ 'size', 'width', 'height', 'bits' ] as $field ) {
509 $decoded[$field] = +$decoded[$field];
510 }
511
512 return $decoded;
513 }
514
515 /**
516 * Load file metadata from a DB result row
517 *
518 * @param object $row
519 * @param string $prefix
520 */
521 function loadFromRow( $row, $prefix = 'img_' ) {
522 $this->dataLoaded = true;
523 $this->extraDataLoaded = true;
524
525 $array = $this->decodeRow( $row, $prefix );
526
527 foreach ( $array as $name => $value ) {
528 $this->$name = $value;
529 }
530
531 $this->fileExists = true;
532 $this->maybeUpgradeRow();
533 }
534
535 /**
536 * Load file metadata from cache or DB, unless already loaded
537 * @param int $flags
538 */
539 function load( $flags = 0 ) {
540 if ( !$this->dataLoaded ) {
541 if ( ( $flags & self::READ_LATEST ) || !$this->loadFromCache() ) {
542 $this->loadFromDB( $flags );
543 $this->saveToCache();
544 }
545 $this->dataLoaded = true;
546 }
547 if ( ( $flags & self::LOAD_ALL ) && !$this->extraDataLoaded ) {
548 // @note: loads on name/timestamp to reduce race condition problems
549 $this->loadExtraFromDB();
550 }
551 }
552
553 /**
554 * Upgrade a row if it needs it
555 */
556 function maybeUpgradeRow() {
557 global $wgUpdateCompatibleMetadata;
558 if ( wfReadOnly() ) {
559 return;
560 }
561
562 $upgrade = false;
563 if ( is_null( $this->media_type ) ||
564 $this->mime == 'image/svg'
565 ) {
566 $upgrade = true;
567 } else {
568 $handler = $this->getHandler();
569 if ( $handler ) {
570 $validity = $handler->isMetadataValid( $this, $this->getMetadata() );
571 if ( $validity === MediaHandler::METADATA_BAD
572 || ( $validity === MediaHandler::METADATA_COMPATIBLE && $wgUpdateCompatibleMetadata )
573 ) {
574 $upgrade = true;
575 }
576 }
577 }
578
579 if ( $upgrade ) {
580 try {
581 $this->upgradeRow();
582 } catch ( LocalFileLockError $e ) {
583 // let the other process handle it (or do it next time)
584 }
585 $this->upgraded = true; // avoid rework/retries
586 }
587 }
588
589 function getUpgraded() {
590 return $this->upgraded;
591 }
592
593 /**
594 * Fix assorted version-related problems with the image row by reloading it from the file
595 */
596 function upgradeRow() {
597 $this->lock(); // begin
598
599 $this->loadFromFile();
600
601 # Don't destroy file info of missing files
602 if ( !$this->fileExists ) {
603 $this->unlock();
604 wfDebug( __METHOD__ . ": file does not exist, aborting\n" );
605
606 return;
607 }
608
609 $dbw = $this->repo->getMasterDB();
610 list( $major, $minor ) = self::splitMime( $this->mime );
611
612 if ( wfReadOnly() ) {
613 $this->unlock();
614
615 return;
616 }
617 wfDebug( __METHOD__ . ': upgrading ' . $this->getName() . " to the current schema\n" );
618
619 $dbw->update( 'image',
620 [
621 'img_size' => $this->size, // sanity
622 'img_width' => $this->width,
623 'img_height' => $this->height,
624 'img_bits' => $this->bits,
625 'img_media_type' => $this->media_type,
626 'img_major_mime' => $major,
627 'img_minor_mime' => $minor,
628 'img_metadata' => $dbw->encodeBlob( $this->metadata ),
629 'img_sha1' => $this->sha1,
630 ],
631 [ 'img_name' => $this->getName() ],
632 __METHOD__
633 );
634
635 $this->invalidateCache();
636
637 $this->unlock(); // done
638
639 }
640
641 /**
642 * Set properties in this object to be equal to those given in the
643 * associative array $info. Only cacheable fields can be set.
644 * All fields *must* be set in $info except for getLazyCacheFields().
645 *
646 * If 'mime' is given, it will be split into major_mime/minor_mime.
647 * If major_mime/minor_mime are given, $this->mime will also be set.
648 *
649 * @param array $info
650 */
651 function setProps( $info ) {
652 $this->dataLoaded = true;
653 $fields = $this->getCacheFields( '' );
654 $fields[] = 'fileExists';
655
656 foreach ( $fields as $field ) {
657 if ( isset( $info[$field] ) ) {
658 $this->$field = $info[$field];
659 }
660 }
661
662 // Fix up mime fields
663 if ( isset( $info['major_mime'] ) ) {
664 $this->mime = "{$info['major_mime']}/{$info['minor_mime']}";
665 } elseif ( isset( $info['mime'] ) ) {
666 $this->mime = $info['mime'];
667 list( $this->major_mime, $this->minor_mime ) = self::splitMime( $this->mime );
668 }
669 }
670
671 /** splitMime inherited */
672 /** getName inherited */
673 /** getTitle inherited */
674 /** getURL inherited */
675 /** getViewURL inherited */
676 /** getPath inherited */
677 /** isVisible inherited */
678
679 /**
680 * @return bool
681 */
682 function isMissing() {
683 if ( $this->missing === null ) {
684 list( $fileExists ) = $this->repo->fileExists( $this->getVirtualUrl() );
685 $this->missing = !$fileExists;
686 }
687
688 return $this->missing;
689 }
690
691 /**
692 * Return the width of the image
693 *
694 * @param int $page
695 * @return int
696 */
697 public function getWidth( $page = 1 ) {
698 $this->load();
699
700 if ( $this->isMultipage() ) {
701 $handler = $this->getHandler();
702 if ( !$handler ) {
703 return 0;
704 }
705 $dim = $handler->getPageDimensions( $this, $page );
706 if ( $dim ) {
707 return $dim['width'];
708 } else {
709 // For non-paged media, the false goes through an
710 // intval, turning failure into 0, so do same here.
711 return 0;
712 }
713 } else {
714 return $this->width;
715 }
716 }
717
718 /**
719 * Return the height of the image
720 *
721 * @param int $page
722 * @return int
723 */
724 public function getHeight( $page = 1 ) {
725 $this->load();
726
727 if ( $this->isMultipage() ) {
728 $handler = $this->getHandler();
729 if ( !$handler ) {
730 return 0;
731 }
732 $dim = $handler->getPageDimensions( $this, $page );
733 if ( $dim ) {
734 return $dim['height'];
735 } else {
736 // For non-paged media, the false goes through an
737 // intval, turning failure into 0, so do same here.
738 return 0;
739 }
740 } else {
741 return $this->height;
742 }
743 }
744
745 /**
746 * Returns ID or name of user who uploaded the file
747 *
748 * @param string $type 'text' or 'id'
749 * @return int|string
750 */
751 function getUser( $type = 'text' ) {
752 $this->load();
753
754 if ( $type == 'text' ) {
755 return $this->user_text;
756 } elseif ( $type == 'id' ) {
757 return (int)$this->user;
758 }
759 }
760
761 /**
762 * Get short description URL for a file based on the page ID.
763 *
764 * @return string|null
765 * @throws MWException
766 * @since 1.27
767 */
768 public function getDescriptionShortUrl() {
769 $pageId = $this->title->getArticleID();
770
771 if ( $pageId !== null ) {
772 $url = $this->repo->makeUrl( [ 'curid' => $pageId ] );
773 if ( $url !== false ) {
774 return $url;
775 }
776 }
777 return null;
778 }
779
780 /**
781 * Get handler-specific metadata
782 * @return string
783 */
784 function getMetadata() {
785 $this->load( self::LOAD_ALL ); // large metadata is loaded in another step
786 return $this->metadata;
787 }
788
789 /**
790 * @return int
791 */
792 function getBitDepth() {
793 $this->load();
794
795 return (int)$this->bits;
796 }
797
798 /**
799 * Returns the size of the image file, in bytes
800 * @return int
801 */
802 public function getSize() {
803 $this->load();
804
805 return $this->size;
806 }
807
808 /**
809 * Returns the MIME type of the file.
810 * @return string
811 */
812 function getMimeType() {
813 $this->load();
814
815 return $this->mime;
816 }
817
818 /**
819 * Returns the type of the media in the file.
820 * Use the value returned by this function with the MEDIATYPE_xxx constants.
821 * @return string
822 */
823 function getMediaType() {
824 $this->load();
825
826 return $this->media_type;
827 }
828
829 /** canRender inherited */
830 /** mustRender inherited */
831 /** allowInlineDisplay inherited */
832 /** isSafeFile inherited */
833 /** isTrustedFile inherited */
834
835 /**
836 * Returns true if the file exists on disk.
837 * @return bool Whether file exist on disk.
838 */
839 public function exists() {
840 $this->load();
841
842 return $this->fileExists;
843 }
844
845 /** getTransformScript inherited */
846 /** getUnscaledThumb inherited */
847 /** thumbName inherited */
848 /** createThumb inherited */
849 /** transform inherited */
850
851 /** getHandler inherited */
852 /** iconThumb inherited */
853 /** getLastError inherited */
854
855 /**
856 * Get all thumbnail names previously generated for this file
857 * @param string|bool $archiveName Name of an archive file, default false
858 * @return array First element is the base dir, then files in that base dir.
859 */
860 function getThumbnails( $archiveName = false ) {
861 if ( $archiveName ) {
862 $dir = $this->getArchiveThumbPath( $archiveName );
863 } else {
864 $dir = $this->getThumbPath();
865 }
866
867 $backend = $this->repo->getBackend();
868 $files = [ $dir ];
869 try {
870 $iterator = $backend->getFileList( [ 'dir' => $dir ] );
871 foreach ( $iterator as $file ) {
872 $files[] = $file;
873 }
874 } catch ( FileBackendError $e ) {
875 } // suppress (bug 54674)
876
877 return $files;
878 }
879
880 /**
881 * Refresh metadata in memcached, but don't touch thumbnails or CDN
882 */
883 function purgeMetadataCache() {
884 $this->invalidateCache();
885 }
886
887 /**
888 * Delete all previously generated thumbnails, refresh metadata in memcached and purge the CDN.
889 *
890 * @param array $options An array potentially with the key forThumbRefresh.
891 *
892 * @note This used to purge old thumbnails by default as well, but doesn't anymore.
893 */
894 function purgeCache( $options = [] ) {
895 // Refresh metadata cache
896 $this->purgeMetadataCache();
897
898 // Delete thumbnails
899 $this->purgeThumbnails( $options );
900
901 // Purge CDN cache for this file
902 DeferredUpdates::addUpdate(
903 new CdnCacheUpdate( [ $this->getUrl() ] ),
904 DeferredUpdates::PRESEND
905 );
906 }
907
908 /**
909 * Delete cached transformed files for an archived version only.
910 * @param string $archiveName Name of the archived file
911 */
912 function purgeOldThumbnails( $archiveName ) {
913 // Get a list of old thumbnails and URLs
914 $files = $this->getThumbnails( $archiveName );
915
916 // Purge any custom thumbnail caches
917 Hooks::run( 'LocalFilePurgeThumbnails', [ $this, $archiveName ] );
918
919 // Delete thumbnails
920 $dir = array_shift( $files );
921 $this->purgeThumbList( $dir, $files );
922
923 // Purge the CDN
924 $urls = [];
925 foreach ( $files as $file ) {
926 $urls[] = $this->getArchiveThumbUrl( $archiveName, $file );
927 }
928 DeferredUpdates::addUpdate( new CdnCacheUpdate( $urls ), DeferredUpdates::PRESEND );
929 }
930
931 /**
932 * Delete cached transformed files for the current version only.
933 * @param array $options
934 */
935 public function purgeThumbnails( $options = [] ) {
936 $files = $this->getThumbnails();
937 // Always purge all files from CDN regardless of handler filters
938 $urls = [];
939 foreach ( $files as $file ) {
940 $urls[] = $this->getThumbUrl( $file );
941 }
942 array_shift( $urls ); // don't purge directory
943
944 // Give media handler a chance to filter the file purge list
945 if ( !empty( $options['forThumbRefresh'] ) ) {
946 $handler = $this->getHandler();
947 if ( $handler ) {
948 $handler->filterThumbnailPurgeList( $files, $options );
949 }
950 }
951
952 // Purge any custom thumbnail caches
953 Hooks::run( 'LocalFilePurgeThumbnails', [ $this, false ] );
954
955 // Delete thumbnails
956 $dir = array_shift( $files );
957 $this->purgeThumbList( $dir, $files );
958
959 // Purge the CDN
960 DeferredUpdates::addUpdate( new CdnCacheUpdate( $urls ), DeferredUpdates::PRESEND );
961 }
962
963 /**
964 * Delete a list of thumbnails visible at urls
965 * @param string $dir Base dir of the files.
966 * @param array $files Array of strings: relative filenames (to $dir)
967 */
968 protected function purgeThumbList( $dir, $files ) {
969 $fileListDebug = strtr(
970 var_export( $files, true ),
971 [ "\n" => '' ]
972 );
973 wfDebug( __METHOD__ . ": $fileListDebug\n" );
974
975 $purgeList = [];
976 foreach ( $files as $file ) {
977 # Check that the base file name is part of the thumb name
978 # This is a basic sanity check to avoid erasing unrelated directories
979 if ( strpos( $file, $this->getName() ) !== false
980 || strpos( $file, "-thumbnail" ) !== false // "short" thumb name
981 ) {
982 $purgeList[] = "{$dir}/{$file}";
983 }
984 }
985
986 # Delete the thumbnails
987 $this->repo->quickPurgeBatch( $purgeList );
988 # Clear out the thumbnail directory if empty
989 $this->repo->quickCleanDir( $dir );
990 }
991
992 /** purgeDescription inherited */
993 /** purgeEverything inherited */
994
995 /**
996 * @param int $limit Optional: Limit to number of results
997 * @param int $start Optional: Timestamp, start from
998 * @param int $end Optional: Timestamp, end at
999 * @param bool $inc
1000 * @return OldLocalFile[]
1001 */
1002 function getHistory( $limit = null, $start = null, $end = null, $inc = true ) {
1003 $dbr = $this->repo->getSlaveDB();
1004 $tables = [ 'oldimage' ];
1005 $fields = OldLocalFile::selectFields();
1006 $conds = $opts = $join_conds = [];
1007 $eq = $inc ? '=' : '';
1008 $conds[] = "oi_name = " . $dbr->addQuotes( $this->title->getDBkey() );
1009
1010 if ( $start ) {
1011 $conds[] = "oi_timestamp <$eq " . $dbr->addQuotes( $dbr->timestamp( $start ) );
1012 }
1013
1014 if ( $end ) {
1015 $conds[] = "oi_timestamp >$eq " . $dbr->addQuotes( $dbr->timestamp( $end ) );
1016 }
1017
1018 if ( $limit ) {
1019 $opts['LIMIT'] = $limit;
1020 }
1021
1022 // Search backwards for time > x queries
1023 $order = ( !$start && $end !== null ) ? 'ASC' : 'DESC';
1024 $opts['ORDER BY'] = "oi_timestamp $order";
1025 $opts['USE INDEX'] = [ 'oldimage' => 'oi_name_timestamp' ];
1026
1027 Hooks::run( 'LocalFile::getHistory', [ &$this, &$tables, &$fields,
1028 &$conds, &$opts, &$join_conds ] );
1029
1030 $res = $dbr->select( $tables, $fields, $conds, __METHOD__, $opts, $join_conds );
1031 $r = [];
1032
1033 foreach ( $res as $row ) {
1034 $r[] = $this->repo->newFileFromRow( $row );
1035 }
1036
1037 if ( $order == 'ASC' ) {
1038 $r = array_reverse( $r ); // make sure it ends up descending
1039 }
1040
1041 return $r;
1042 }
1043
1044 /**
1045 * Returns the history of this file, line by line.
1046 * starts with current version, then old versions.
1047 * uses $this->historyLine to check which line to return:
1048 * 0 return line for current version
1049 * 1 query for old versions, return first one
1050 * 2, ... return next old version from above query
1051 * @return bool
1052 */
1053 public function nextHistoryLine() {
1054 # Polymorphic function name to distinguish foreign and local fetches
1055 $fname = get_class( $this ) . '::' . __FUNCTION__;
1056
1057 $dbr = $this->repo->getSlaveDB();
1058
1059 if ( $this->historyLine == 0 ) { // called for the first time, return line from cur
1060 $this->historyRes = $dbr->select( 'image',
1061 [
1062 '*',
1063 "'' AS oi_archive_name",
1064 '0 as oi_deleted',
1065 'img_sha1'
1066 ],
1067 [ 'img_name' => $this->title->getDBkey() ],
1068 $fname
1069 );
1070
1071 if ( 0 == $dbr->numRows( $this->historyRes ) ) {
1072 $this->historyRes = null;
1073
1074 return false;
1075 }
1076 } elseif ( $this->historyLine == 1 ) {
1077 $this->historyRes = $dbr->select( 'oldimage', '*',
1078 [ 'oi_name' => $this->title->getDBkey() ],
1079 $fname,
1080 [ 'ORDER BY' => 'oi_timestamp DESC' ]
1081 );
1082 }
1083 $this->historyLine++;
1084
1085 return $dbr->fetchObject( $this->historyRes );
1086 }
1087
1088 /**
1089 * Reset the history pointer to the first element of the history
1090 */
1091 public function resetHistory() {
1092 $this->historyLine = 0;
1093
1094 if ( !is_null( $this->historyRes ) ) {
1095 $this->historyRes = null;
1096 }
1097 }
1098
1099 /** getHashPath inherited */
1100 /** getRel inherited */
1101 /** getUrlRel inherited */
1102 /** getArchiveRel inherited */
1103 /** getArchivePath inherited */
1104 /** getThumbPath inherited */
1105 /** getArchiveUrl inherited */
1106 /** getThumbUrl inherited */
1107 /** getArchiveVirtualUrl inherited */
1108 /** getThumbVirtualUrl inherited */
1109 /** isHashed inherited */
1110
1111 /**
1112 * Upload a file and record it in the DB
1113 * @param string|FSFile $src Source storage path, virtual URL, or filesystem path
1114 * @param string $comment Upload description
1115 * @param string $pageText Text to use for the new description page,
1116 * if a new description page is created
1117 * @param int|bool $flags Flags for publish()
1118 * @param array|bool $props File properties, if known. This can be used to
1119 * reduce the upload time when uploading virtual URLs for which the file
1120 * info is already known
1121 * @param string|bool $timestamp Timestamp for img_timestamp, or false to use the
1122 * current time
1123 * @param User|null $user User object or null to use $wgUser
1124 * @param string[] $tags Change tags to add to the log entry and page revision.
1125 * (This doesn't check $user's permissions.)
1126 * @return FileRepoStatus On success, the value member contains the
1127 * archive name, or an empty string if it was a new file.
1128 */
1129 function upload( $src, $comment, $pageText, $flags = 0, $props = false,
1130 $timestamp = false, $user = null, $tags = []
1131 ) {
1132 global $wgContLang;
1133
1134 if ( $this->getRepo()->getReadOnlyReason() !== false ) {
1135 return $this->readOnlyFatalStatus();
1136 }
1137
1138 $srcPath = ( $src instanceof FSFile ) ? $src->getPath() : $src;
1139 if ( !$props ) {
1140 if ( $this->repo->isVirtualUrl( $srcPath )
1141 || FileBackend::isStoragePath( $srcPath )
1142 ) {
1143 $props = $this->repo->getFileProps( $srcPath );
1144 } else {
1145 $props = FSFile::getPropsFromPath( $srcPath );
1146 }
1147 }
1148
1149 $options = [];
1150 $handler = MediaHandler::getHandler( $props['mime'] );
1151 if ( $handler ) {
1152 $options['headers'] = $handler->getStreamHeaders( $props['metadata'] );
1153 } else {
1154 $options['headers'] = [];
1155 }
1156
1157 // Trim spaces on user supplied text
1158 $comment = trim( $comment );
1159
1160 // Truncate nicely or the DB will do it for us
1161 // non-nicely (dangling multi-byte chars, non-truncated version in cache).
1162 $comment = $wgContLang->truncate( $comment, 255 );
1163 $this->lock(); // begin
1164 $status = $this->publish( $src, $flags, $options );
1165
1166 if ( $status->successCount >= 2 ) {
1167 // There will be a copy+(one of move,copy,store).
1168 // The first succeeding does not commit us to updating the DB
1169 // since it simply copied the current version to a timestamped file name.
1170 // It is only *preferable* to avoid leaving such files orphaned.
1171 // Once the second operation goes through, then the current version was
1172 // updated and we must therefore update the DB too.
1173 $oldver = $status->value;
1174 if ( !$this->recordUpload2( $oldver, $comment, $pageText, $props, $timestamp, $user, $tags ) ) {
1175 $status->fatal( 'filenotfound', $srcPath );
1176 }
1177 }
1178
1179 $this->unlock(); // done
1180
1181 return $status;
1182 }
1183
1184 /**
1185 * Record a file upload in the upload log and the image table
1186 * @param string $oldver
1187 * @param string $desc
1188 * @param string $license
1189 * @param string $copyStatus
1190 * @param string $source
1191 * @param bool $watch
1192 * @param string|bool $timestamp
1193 * @param User|null $user User object or null to use $wgUser
1194 * @return bool
1195 */
1196 function recordUpload( $oldver, $desc, $license = '', $copyStatus = '', $source = '',
1197 $watch = false, $timestamp = false, User $user = null ) {
1198 if ( !$user ) {
1199 global $wgUser;
1200 $user = $wgUser;
1201 }
1202
1203 $pageText = SpecialUpload::getInitialPageText( $desc, $license, $copyStatus, $source );
1204
1205 if ( !$this->recordUpload2( $oldver, $desc, $pageText, false, $timestamp, $user ) ) {
1206 return false;
1207 }
1208
1209 if ( $watch ) {
1210 $user->addWatch( $this->getTitle() );
1211 }
1212
1213 return true;
1214 }
1215
1216 /**
1217 * Record a file upload in the upload log and the image table
1218 * @param string $oldver
1219 * @param string $comment
1220 * @param string $pageText
1221 * @param bool|array $props
1222 * @param string|bool $timestamp
1223 * @param null|User $user
1224 * @param string[] $tags
1225 * @return bool
1226 */
1227 function recordUpload2(
1228 $oldver, $comment, $pageText, $props = false, $timestamp = false, $user = null, $tags = []
1229 ) {
1230 if ( is_null( $user ) ) {
1231 global $wgUser;
1232 $user = $wgUser;
1233 }
1234
1235 $dbw = $this->repo->getMasterDB();
1236
1237 # Imports or such might force a certain timestamp; otherwise we generate
1238 # it and can fudge it slightly to keep (name,timestamp) unique on re-upload.
1239 if ( $timestamp === false ) {
1240 $timestamp = $dbw->timestamp();
1241 $allowTimeKludge = true;
1242 } else {
1243 $allowTimeKludge = false;
1244 }
1245
1246 $props = $props ?: $this->repo->getFileProps( $this->getVirtualUrl() );
1247 $props['description'] = $comment;
1248 $props['user'] = $user->getId();
1249 $props['user_text'] = $user->getName();
1250 $props['timestamp'] = wfTimestamp( TS_MW, $timestamp ); // DB -> TS_MW
1251 $this->setProps( $props );
1252
1253 # Fail now if the file isn't there
1254 if ( !$this->fileExists ) {
1255 wfDebug( __METHOD__ . ": File " . $this->getRel() . " went missing!\n" );
1256
1257 return false;
1258 }
1259
1260 $dbw->startAtomic( __METHOD__ );
1261
1262 # Test to see if the row exists using INSERT IGNORE
1263 # This avoids race conditions by locking the row until the commit, and also
1264 # doesn't deadlock. SELECT FOR UPDATE causes a deadlock for every race condition.
1265 $dbw->insert( 'image',
1266 [
1267 'img_name' => $this->getName(),
1268 'img_size' => $this->size,
1269 'img_width' => intval( $this->width ),
1270 'img_height' => intval( $this->height ),
1271 'img_bits' => $this->bits,
1272 'img_media_type' => $this->media_type,
1273 'img_major_mime' => $this->major_mime,
1274 'img_minor_mime' => $this->minor_mime,
1275 'img_timestamp' => $timestamp,
1276 'img_description' => $comment,
1277 'img_user' => $user->getId(),
1278 'img_user_text' => $user->getName(),
1279 'img_metadata' => $dbw->encodeBlob( $this->metadata ),
1280 'img_sha1' => $this->sha1
1281 ],
1282 __METHOD__,
1283 'IGNORE'
1284 );
1285
1286 $reupload = ( $dbw->affectedRows() == 0 );
1287 if ( $reupload ) {
1288 if ( $allowTimeKludge ) {
1289 # Use LOCK IN SHARE MODE to ignore any transaction snapshotting
1290 $ltimestamp = $dbw->selectField(
1291 'image',
1292 'img_timestamp',
1293 [ 'img_name' => $this->getName() ],
1294 __METHOD__,
1295 [ 'LOCK IN SHARE MODE' ]
1296 );
1297 $lUnixtime = $ltimestamp ? wfTimestamp( TS_UNIX, $ltimestamp ) : false;
1298 # Avoid a timestamp that is not newer than the last version
1299 # TODO: the image/oldimage tables should be like page/revision with an ID field
1300 if ( $lUnixtime && wfTimestamp( TS_UNIX, $timestamp ) <= $lUnixtime ) {
1301 sleep( 1 ); // fast enough re-uploads would go far in the future otherwise
1302 $timestamp = $dbw->timestamp( $lUnixtime + 1 );
1303 $this->timestamp = wfTimestamp( TS_MW, $timestamp ); // DB -> TS_MW
1304 }
1305 }
1306
1307 # (bug 34993) Note: $oldver can be empty here, if the previous
1308 # version of the file was broken. Allow registration of the new
1309 # version to continue anyway, because that's better than having
1310 # an image that's not fixable by user operations.
1311 # Collision, this is an update of a file
1312 # Insert previous contents into oldimage
1313 $dbw->insertSelect( 'oldimage', 'image',
1314 [
1315 'oi_name' => 'img_name',
1316 'oi_archive_name' => $dbw->addQuotes( $oldver ),
1317 'oi_size' => 'img_size',
1318 'oi_width' => 'img_width',
1319 'oi_height' => 'img_height',
1320 'oi_bits' => 'img_bits',
1321 'oi_timestamp' => 'img_timestamp',
1322 'oi_description' => 'img_description',
1323 'oi_user' => 'img_user',
1324 'oi_user_text' => 'img_user_text',
1325 'oi_metadata' => 'img_metadata',
1326 'oi_media_type' => 'img_media_type',
1327 'oi_major_mime' => 'img_major_mime',
1328 'oi_minor_mime' => 'img_minor_mime',
1329 'oi_sha1' => 'img_sha1'
1330 ],
1331 [ 'img_name' => $this->getName() ],
1332 __METHOD__
1333 );
1334
1335 # Update the current image row
1336 $dbw->update( 'image',
1337 [
1338 'img_size' => $this->size,
1339 'img_width' => intval( $this->width ),
1340 'img_height' => intval( $this->height ),
1341 'img_bits' => $this->bits,
1342 'img_media_type' => $this->media_type,
1343 'img_major_mime' => $this->major_mime,
1344 'img_minor_mime' => $this->minor_mime,
1345 'img_timestamp' => $timestamp,
1346 'img_description' => $comment,
1347 'img_user' => $user->getId(),
1348 'img_user_text' => $user->getName(),
1349 'img_metadata' => $dbw->encodeBlob( $this->metadata ),
1350 'img_sha1' => $this->sha1
1351 ],
1352 [ 'img_name' => $this->getName() ],
1353 __METHOD__
1354 );
1355 }
1356
1357 $descTitle = $this->getTitle();
1358 $descId = $descTitle->getArticleID();
1359 $wikiPage = new WikiFilePage( $descTitle );
1360 $wikiPage->setFile( $this );
1361
1362 // Add the log entry...
1363 $logEntry = new ManualLogEntry( 'upload', $reupload ? 'overwrite' : 'upload' );
1364 $logEntry->setTimestamp( $this->timestamp );
1365 $logEntry->setPerformer( $user );
1366 $logEntry->setComment( $comment );
1367 $logEntry->setTarget( $descTitle );
1368 // Allow people using the api to associate log entries with the upload.
1369 // Log has a timestamp, but sometimes different from upload timestamp.
1370 $logEntry->setParameters(
1371 [
1372 'img_sha1' => $this->sha1,
1373 'img_timestamp' => $timestamp,
1374 ]
1375 );
1376 // Note we keep $logId around since during new image
1377 // creation, page doesn't exist yet, so log_page = 0
1378 // but we want it to point to the page we're making,
1379 // so we later modify the log entry.
1380 // For a similar reason, we avoid making an RC entry
1381 // now and wait until the page exists.
1382 $logId = $logEntry->insert();
1383
1384 if ( $descTitle->exists() ) {
1385 // Use own context to get the action text in content language
1386 $formatter = LogFormatter::newFromEntry( $logEntry );
1387 $formatter->setContext( RequestContext::newExtraneousContext( $descTitle ) );
1388 $editSummary = $formatter->getPlainActionText();
1389
1390 $nullRevision = Revision::newNullRevision(
1391 $dbw,
1392 $descId,
1393 $editSummary,
1394 false,
1395 $user
1396 );
1397 if ( $nullRevision ) {
1398 $nullRevision->insertOn( $dbw );
1399 Hooks::run(
1400 'NewRevisionFromEditComplete',
1401 [ $wikiPage, $nullRevision, $nullRevision->getParentId(), $user ]
1402 );
1403 $wikiPage->updateRevisionOn( $dbw, $nullRevision );
1404 // Associate null revision id
1405 $logEntry->setAssociatedRevId( $nullRevision->getId() );
1406 }
1407
1408 $newPageContent = null;
1409 } else {
1410 // Make the description page and RC log entry post-commit
1411 $newPageContent = ContentHandler::makeContent( $pageText, $descTitle );
1412 }
1413
1414 # Defer purges, page creation, and link updates in case they error out.
1415 # The most important thing is that files and the DB registry stay synced.
1416 $dbw->endAtomic( __METHOD__ );
1417
1418 # Do some cache purges after final commit so that:
1419 # a) Changes are more likely to be seen post-purge
1420 # b) They won't cause rollback of the log publish/update above
1421 $that = $this;
1422 $dbw->onTransactionIdle( function () use (
1423 $that, $reupload, $wikiPage, $newPageContent, $comment, $user, $logEntry, $logId, $descId, $tags
1424 ) {
1425 # Update memcache after the commit
1426 $that->invalidateCache();
1427
1428 $updateLogPage = false;
1429 if ( $newPageContent ) {
1430 # New file page; create the description page.
1431 # There's already a log entry, so don't make a second RC entry
1432 # CDN and file cache for the description page are purged by doEditContent.
1433 $status = $wikiPage->doEditContent(
1434 $newPageContent,
1435 $comment,
1436 EDIT_NEW | EDIT_SUPPRESS_RC,
1437 false,
1438 $user
1439 );
1440
1441 if ( isset( $status->value['revision'] ) ) {
1442 // Associate new page revision id
1443 $logEntry->setAssociatedRevId( $status->value['revision']->getId() );
1444 }
1445 // This relies on the resetArticleID() call in WikiPage::insertOn(),
1446 // which is triggered on $descTitle by doEditContent() above.
1447 if ( isset( $status->value['revision'] ) ) {
1448 /** @var $rev Revision */
1449 $rev = $status->value['revision'];
1450 $updateLogPage = $rev->getPage();
1451 }
1452 } else {
1453 # Existing file page: invalidate description page cache
1454 $wikiPage->getTitle()->invalidateCache();
1455 $wikiPage->getTitle()->purgeSquid();
1456 # Allow the new file version to be patrolled from the page footer
1457 Article::purgePatrolFooterCache( $descId );
1458 }
1459
1460 # Update associated rev id. This should be done by $logEntry->insert() earlier,
1461 # but setAssociatedRevId() wasn't called at that point yet...
1462 $logParams = $logEntry->getParameters();
1463 $logParams['associated_rev_id'] = $logEntry->getAssociatedRevId();
1464 $update = [ 'log_params' => LogEntryBase::makeParamBlob( $logParams ) ];
1465 if ( $updateLogPage ) {
1466 # Also log page, in case where we just created it above
1467 $update['log_page'] = $updateLogPage;
1468 }
1469 $that->getRepo()->getMasterDB()->update(
1470 'logging',
1471 $update,
1472 [ 'log_id' => $logId ],
1473 __METHOD__
1474 );
1475 $that->getRepo()->getMasterDB()->insert(
1476 'log_search',
1477 [
1478 'ls_field' => 'associated_rev_id',
1479 'ls_value' => $logEntry->getAssociatedRevId(),
1480 'ls_log_id' => $logId,
1481 ],
1482 __METHOD__
1483 );
1484
1485 # Add change tags, if any
1486 if ( $tags ) {
1487 $logEntry->setTags( $tags );
1488 }
1489
1490 # Uploads can be patrolled
1491 $logEntry->setIsPatrollable( true );
1492
1493 # Now that the log entry is up-to-date, make an RC entry.
1494 $logEntry->publish( $logId );
1495
1496 # Run hook for other updates (typically more cache purging)
1497 Hooks::run( 'FileUpload', [ $that, $reupload, !$newPageContent ] );
1498
1499 if ( $reupload ) {
1500 # Delete old thumbnails
1501 $that->purgeThumbnails();
1502 # Remove the old file from the CDN cache
1503 DeferredUpdates::addUpdate(
1504 new CdnCacheUpdate( [ $that->getUrl() ] ),
1505 DeferredUpdates::PRESEND
1506 );
1507 } else {
1508 # Update backlink pages pointing to this title if created
1509 LinksUpdate::queueRecursiveJobsForTable( $that->getTitle(), 'imagelinks' );
1510 }
1511 } );
1512
1513 if ( !$reupload ) {
1514 # This is a new file, so update the image count
1515 DeferredUpdates::addUpdate( SiteStatsUpdate::factory( [ 'images' => 1 ] ) );
1516 }
1517
1518 # Invalidate cache for all pages using this file
1519 DeferredUpdates::addUpdate( new HTMLCacheUpdate( $this->getTitle(), 'imagelinks' ) );
1520
1521 return true;
1522 }
1523
1524 /**
1525 * Move or copy a file to its public location. If a file exists at the
1526 * destination, move it to an archive. Returns a FileRepoStatus object with
1527 * the archive name in the "value" member on success.
1528 *
1529 * The archive name should be passed through to recordUpload for database
1530 * registration.
1531 *
1532 * @param string|FSFile $src Local filesystem path or virtual URL to the source image
1533 * @param int $flags A bitwise combination of:
1534 * File::DELETE_SOURCE Delete the source file, i.e. move rather than copy
1535 * @param array $options Optional additional parameters
1536 * @return FileRepoStatus On success, the value member contains the
1537 * archive name, or an empty string if it was a new file.
1538 */
1539 function publish( $src, $flags = 0, array $options = [] ) {
1540 return $this->publishTo( $src, $this->getRel(), $flags, $options );
1541 }
1542
1543 /**
1544 * Move or copy a file to a specified location. Returns a FileRepoStatus
1545 * object with the archive name in the "value" member on success.
1546 *
1547 * The archive name should be passed through to recordUpload for database
1548 * registration.
1549 *
1550 * @param string|FSFile $src Local filesystem path or virtual URL to the source image
1551 * @param string $dstRel Target relative path
1552 * @param int $flags A bitwise combination of:
1553 * File::DELETE_SOURCE Delete the source file, i.e. move rather than copy
1554 * @param array $options Optional additional parameters
1555 * @return FileRepoStatus On success, the value member contains the
1556 * archive name, or an empty string if it was a new file.
1557 */
1558 function publishTo( $src, $dstRel, $flags = 0, array $options = [] ) {
1559 $srcPath = ( $src instanceof FSFile ) ? $src->getPath() : $src;
1560
1561 $repo = $this->getRepo();
1562 if ( $repo->getReadOnlyReason() !== false ) {
1563 return $this->readOnlyFatalStatus();
1564 }
1565
1566 $this->lock(); // begin
1567
1568 $archiveName = wfTimestamp( TS_MW ) . '!' . $this->getName();
1569 $archiveRel = 'archive/' . $this->getHashPath() . $archiveName;
1570
1571 if ( $repo->hasSha1Storage() ) {
1572 $sha1 = $repo->isVirtualUrl( $srcPath )
1573 ? $repo->getFileSha1( $srcPath )
1574 : FSFile::getSha1Base36FromPath( $srcPath );
1575 $dst = $repo->getBackend()->getPathForSHA1( $sha1 );
1576 $status = $repo->quickImport( $src, $dst );
1577 if ( $flags & File::DELETE_SOURCE ) {
1578 unlink( $srcPath );
1579 }
1580
1581 if ( $this->exists() ) {
1582 $status->value = $archiveName;
1583 }
1584 } else {
1585 $flags = $flags & File::DELETE_SOURCE ? LocalRepo::DELETE_SOURCE : 0;
1586 $status = $repo->publish( $srcPath, $dstRel, $archiveRel, $flags, $options );
1587
1588 if ( $status->value == 'new' ) {
1589 $status->value = '';
1590 } else {
1591 $status->value = $archiveName;
1592 }
1593 }
1594
1595 $this->unlock(); // done
1596
1597 return $status;
1598 }
1599
1600 /** getLinksTo inherited */
1601 /** getExifData inherited */
1602 /** isLocal inherited */
1603 /** wasDeleted inherited */
1604
1605 /**
1606 * Move file to the new title
1607 *
1608 * Move current, old version and all thumbnails
1609 * to the new filename. Old file is deleted.
1610 *
1611 * Cache purging is done; checks for validity
1612 * and logging are caller's responsibility
1613 *
1614 * @param Title $target New file name
1615 * @return FileRepoStatus
1616 */
1617 function move( $target ) {
1618 if ( $this->getRepo()->getReadOnlyReason() !== false ) {
1619 return $this->readOnlyFatalStatus();
1620 }
1621
1622 wfDebugLog( 'imagemove', "Got request to move {$this->name} to " . $target->getText() );
1623 $batch = new LocalFileMoveBatch( $this, $target );
1624
1625 $this->lock(); // begin
1626 $batch->addCurrent();
1627 $archiveNames = $batch->addOlds();
1628 $status = $batch->execute();
1629 $this->unlock(); // done
1630
1631 wfDebugLog( 'imagemove', "Finished moving {$this->name}" );
1632
1633 // Purge the source and target files...
1634 $oldTitleFile = wfLocalFile( $this->title );
1635 $newTitleFile = wfLocalFile( $target );
1636 // Hack: the lock()/unlock() pair is nested in a transaction so the locking is not
1637 // tied to BEGIN/COMMIT. To avoid slow purges in the transaction, move them outside.
1638 $this->getRepo()->getMasterDB()->onTransactionIdle(
1639 function () use ( $oldTitleFile, $newTitleFile, $archiveNames ) {
1640 $oldTitleFile->purgeEverything();
1641 foreach ( $archiveNames as $archiveName ) {
1642 $oldTitleFile->purgeOldThumbnails( $archiveName );
1643 }
1644 $newTitleFile->purgeEverything();
1645 }
1646 );
1647
1648 if ( $status->isOK() ) {
1649 // Now switch the object
1650 $this->title = $target;
1651 // Force regeneration of the name and hashpath
1652 unset( $this->name );
1653 unset( $this->hashPath );
1654 }
1655
1656 return $status;
1657 }
1658
1659 /**
1660 * Delete all versions of the file.
1661 *
1662 * Moves the files into an archive directory (or deletes them)
1663 * and removes the database rows.
1664 *
1665 * Cache purging is done; logging is caller's responsibility.
1666 *
1667 * @param string $reason
1668 * @param bool $suppress
1669 * @param User|null $user
1670 * @return FileRepoStatus
1671 */
1672 function delete( $reason, $suppress = false, $user = null ) {
1673 if ( $this->getRepo()->getReadOnlyReason() !== false ) {
1674 return $this->readOnlyFatalStatus();
1675 }
1676
1677 $batch = new LocalFileDeleteBatch( $this, $reason, $suppress, $user );
1678
1679 $this->lock(); // begin
1680 $batch->addCurrent();
1681 # Get old version relative paths
1682 $archiveNames = $batch->addOlds();
1683 $status = $batch->execute();
1684 $this->unlock(); // done
1685
1686 if ( $status->isOK() ) {
1687 DeferredUpdates::addUpdate( SiteStatsUpdate::factory( [ 'images' => -1 ] ) );
1688 }
1689
1690 // Hack: the lock()/unlock() pair is nested in a transaction so the locking is not
1691 // tied to BEGIN/COMMIT. To avoid slow purges in the transaction, move them outside.
1692 $that = $this;
1693 $this->getRepo()->getMasterDB()->onTransactionIdle(
1694 function () use ( $that, $archiveNames ) {
1695 $that->purgeEverything();
1696 foreach ( $archiveNames as $archiveName ) {
1697 $that->purgeOldThumbnails( $archiveName );
1698 }
1699 }
1700 );
1701
1702 // Purge the CDN
1703 $purgeUrls = [];
1704 foreach ( $archiveNames as $archiveName ) {
1705 $purgeUrls[] = $this->getArchiveUrl( $archiveName );
1706 }
1707 DeferredUpdates::addUpdate( new CdnCacheUpdate( $purgeUrls ), DeferredUpdates::PRESEND );
1708
1709 return $status;
1710 }
1711
1712 /**
1713 * Delete an old version of the file.
1714 *
1715 * Moves the file into an archive directory (or deletes it)
1716 * and removes the database row.
1717 *
1718 * Cache purging is done; logging is caller's responsibility.
1719 *
1720 * @param string $archiveName
1721 * @param string $reason
1722 * @param bool $suppress
1723 * @param User|null $user
1724 * @throws MWException Exception on database or file store failure
1725 * @return FileRepoStatus
1726 */
1727 function deleteOld( $archiveName, $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->addOld( $archiveName );
1736 $status = $batch->execute();
1737 $this->unlock(); // done
1738
1739 $this->purgeOldThumbnails( $archiveName );
1740 if ( $status->isOK() ) {
1741 $this->purgeDescription();
1742 }
1743
1744 DeferredUpdates::addUpdate(
1745 new CdnCacheUpdate( [ $this->getArchiveUrl( $archiveName ) ] ),
1746 DeferredUpdates::PRESEND
1747 );
1748
1749 return $status;
1750 }
1751
1752 /**
1753 * Restore all or specified deleted revisions to the given file.
1754 * Permissions and logging are left to the caller.
1755 *
1756 * May throw database exceptions on error.
1757 *
1758 * @param array $versions Set of record ids of deleted items to restore,
1759 * or empty to restore all revisions.
1760 * @param bool $unsuppress
1761 * @return FileRepoStatus
1762 */
1763 function restore( $versions = [], $unsuppress = false ) {
1764 if ( $this->getRepo()->getReadOnlyReason() !== false ) {
1765 return $this->readOnlyFatalStatus();
1766 }
1767
1768 $batch = new LocalFileRestoreBatch( $this, $unsuppress );
1769
1770 $this->lock(); // begin
1771 if ( !$versions ) {
1772 $batch->addAll();
1773 } else {
1774 $batch->addIds( $versions );
1775 }
1776 $status = $batch->execute();
1777 if ( $status->isGood() ) {
1778 $cleanupStatus = $batch->cleanup();
1779 $cleanupStatus->successCount = 0;
1780 $cleanupStatus->failCount = 0;
1781 $status->merge( $cleanupStatus );
1782 }
1783 $this->unlock(); // done
1784
1785 return $status;
1786 }
1787
1788 /** isMultipage inherited */
1789 /** pageCount inherited */
1790 /** scaleHeight inherited */
1791 /** getImageSize inherited */
1792
1793 /**
1794 * Get the URL of the file description page.
1795 * @return string
1796 */
1797 function getDescriptionUrl() {
1798 return $this->title->getLocalURL();
1799 }
1800
1801 /**
1802 * Get the HTML text of the description page
1803 * This is not used by ImagePage for local files, since (among other things)
1804 * it skips the parser cache.
1805 *
1806 * @param Language $lang What language to get description in (Optional)
1807 * @return bool|mixed
1808 */
1809 function getDescriptionText( $lang = null ) {
1810 $revision = Revision::newFromTitle( $this->title, false, Revision::READ_NORMAL );
1811 if ( !$revision ) {
1812 return false;
1813 }
1814 $content = $revision->getContent();
1815 if ( !$content ) {
1816 return false;
1817 }
1818 $pout = $content->getParserOutput( $this->title, null, new ParserOptions( null, $lang ) );
1819
1820 return $pout->getText();
1821 }
1822
1823 /**
1824 * @param int $audience
1825 * @param User $user
1826 * @return string
1827 */
1828 function getDescription( $audience = self::FOR_PUBLIC, User $user = null ) {
1829 $this->load();
1830 if ( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_COMMENT ) ) {
1831 return '';
1832 } elseif ( $audience == self::FOR_THIS_USER
1833 && !$this->userCan( self::DELETED_COMMENT, $user )
1834 ) {
1835 return '';
1836 } else {
1837 return $this->description;
1838 }
1839 }
1840
1841 /**
1842 * @return bool|string
1843 */
1844 function getTimestamp() {
1845 $this->load();
1846
1847 return $this->timestamp;
1848 }
1849
1850 /**
1851 * @return bool|string
1852 */
1853 public function getDescriptionTouched() {
1854 // The DB lookup might return false, e.g. if the file was just deleted, or the shared DB repo
1855 // itself gets it from elsewhere. To avoid repeating the DB lookups in such a case, we
1856 // need to differentiate between null (uninitialized) and false (failed to load).
1857 if ( $this->descriptionTouched === null ) {
1858 $cond = [
1859 'page_namespace' => $this->title->getNamespace(),
1860 'page_title' => $this->title->getDBkey()
1861 ];
1862 $touched = $this->repo->getSlaveDB()->selectField( 'page', 'page_touched', $cond, __METHOD__ );
1863 $this->descriptionTouched = $touched ? wfTimestamp( TS_MW, $touched ) : false;
1864 }
1865
1866 return $this->descriptionTouched;
1867 }
1868
1869 /**
1870 * @return string
1871 */
1872 function getSha1() {
1873 $this->load();
1874 // Initialise now if necessary
1875 if ( $this->sha1 == '' && $this->fileExists ) {
1876 $this->lock(); // begin
1877
1878 $this->sha1 = $this->repo->getFileSha1( $this->getPath() );
1879 if ( !wfReadOnly() && strval( $this->sha1 ) != '' ) {
1880 $dbw = $this->repo->getMasterDB();
1881 $dbw->update( 'image',
1882 [ 'img_sha1' => $this->sha1 ],
1883 [ 'img_name' => $this->getName() ],
1884 __METHOD__ );
1885 $this->invalidateCache();
1886 }
1887
1888 $this->unlock(); // done
1889 }
1890
1891 return $this->sha1;
1892 }
1893
1894 /**
1895 * @return bool Whether to cache in RepoGroup (this avoids OOMs)
1896 */
1897 function isCacheable() {
1898 $this->load();
1899
1900 // If extra data (metadata) was not loaded then it must have been large
1901 return $this->extraDataLoaded
1902 && strlen( serialize( $this->metadata ) ) <= self::CACHE_FIELD_MAX_LEN;
1903 }
1904
1905 /**
1906 * Start a transaction and lock the image for update
1907 * Increments a reference counter if the lock is already held
1908 * @throws LocalFileLockError Throws an error if the lock was not acquired
1909 * @return bool Whether the file lock owns/spawned the DB transaction
1910 */
1911 function lock() {
1912 if ( !$this->locked ) {
1913 $dbw = $this->repo->getMasterDB();
1914 if ( !$dbw->trxLevel() ) {
1915 $dbw->begin( __METHOD__ );
1916 $this->lockedOwnTrx = true;
1917 }
1918 // Bug 54736: use simple lock to handle when the file does not exist.
1919 // SELECT FOR UPDATE prevents changes, not other SELECTs with FOR UPDATE.
1920 // Also, that would cause contention on INSERT of similarly named rows.
1921 $backend = $this->getRepo()->getBackend();
1922 $lockPaths = [ $this->getPath() ]; // represents all versions of the file
1923 $start = microtime( true );
1924 $status = $backend->lockFiles( $lockPaths, LockManager::LOCK_EX, 10 );
1925 $waited = microtime( true ) - $start;
1926 if ( !$status->isGood() ) {
1927 if ( $this->lockedOwnTrx ) {
1928 $dbw->rollback( __METHOD__ );
1929 }
1930 throw new LocalFileLockError(
1931 "Could not acquire lock for '{$this->getName()}' ($waited sec)." );
1932 }
1933 // Release the lock *after* commit to avoid row-level contention
1934 $this->locked++;
1935 $dbw->onTransactionIdle( function () use ( $backend, $lockPaths ) {
1936 $backend->unlockFiles( $lockPaths, LockManager::LOCK_EX );
1937 } );
1938 }
1939
1940 return $this->lockedOwnTrx;
1941 }
1942
1943 /**
1944 * Decrement the lock reference count. If the reference count is reduced to zero, commits
1945 * the transaction and thereby releases the image lock.
1946 */
1947 function unlock() {
1948 if ( $this->locked ) {
1949 --$this->locked;
1950 if ( !$this->locked && $this->lockedOwnTrx ) {
1951 $dbw = $this->repo->getMasterDB();
1952 $dbw->commit( __METHOD__ );
1953 $this->lockedOwnTrx = false;
1954 }
1955 }
1956 }
1957
1958 /**
1959 * Roll back the DB transaction and mark the image unlocked
1960 */
1961 function unlockAndRollback() {
1962 $this->locked = false;
1963 $dbw = $this->repo->getMasterDB();
1964 $dbw->rollback( __METHOD__ );
1965 $this->lockedOwnTrx = false;
1966 }
1967
1968 /**
1969 * @return Status
1970 */
1971 protected function readOnlyFatalStatus() {
1972 return $this->getRepo()->newFatal( 'filereadonlyerror', $this->getName(),
1973 $this->getRepo()->getName(), $this->getRepo()->getReadOnlyReason() );
1974 }
1975
1976 /**
1977 * Clean up any dangling locks
1978 */
1979 function __destruct() {
1980 $this->unlock();
1981 }
1982 } // LocalFile class
1983
1984 # ------------------------------------------------------------------------------
1985
1986 /**
1987 * Helper class for file deletion
1988 * @ingroup FileAbstraction
1989 */
1990 class LocalFileDeleteBatch {
1991 /** @var LocalFile */
1992 private $file;
1993
1994 /** @var string */
1995 private $reason;
1996
1997 /** @var array */
1998 private $srcRels = [];
1999
2000 /** @var array */
2001 private $archiveUrls = [];
2002
2003 /** @var array Items to be processed in the deletion batch */
2004 private $deletionBatch;
2005
2006 /** @var bool Whether to suppress all suppressable fields when deleting */
2007 private $suppress;
2008
2009 /** @var FileRepoStatus */
2010 private $status;
2011
2012 /** @var User */
2013 private $user;
2014
2015 /**
2016 * @param File $file
2017 * @param string $reason
2018 * @param bool $suppress
2019 * @param User|null $user
2020 */
2021 function __construct( File $file, $reason = '', $suppress = false, $user = null ) {
2022 $this->file = $file;
2023 $this->reason = $reason;
2024 $this->suppress = $suppress;
2025 if ( $user ) {
2026 $this->user = $user;
2027 } else {
2028 global $wgUser;
2029 $this->user = $wgUser;
2030 }
2031 $this->status = $file->repo->newGood();
2032 }
2033
2034 public function addCurrent() {
2035 $this->srcRels['.'] = $this->file->getRel();
2036 }
2037
2038 /**
2039 * @param string $oldName
2040 */
2041 public function addOld( $oldName ) {
2042 $this->srcRels[$oldName] = $this->file->getArchiveRel( $oldName );
2043 $this->archiveUrls[] = $this->file->getArchiveUrl( $oldName );
2044 }
2045
2046 /**
2047 * Add the old versions of the image to the batch
2048 * @return array List of archive names from old versions
2049 */
2050 public function addOlds() {
2051 $archiveNames = [];
2052
2053 $dbw = $this->file->repo->getMasterDB();
2054 $result = $dbw->select( 'oldimage',
2055 [ 'oi_archive_name' ],
2056 [ 'oi_name' => $this->file->getName() ],
2057 __METHOD__
2058 );
2059
2060 foreach ( $result as $row ) {
2061 $this->addOld( $row->oi_archive_name );
2062 $archiveNames[] = $row->oi_archive_name;
2063 }
2064
2065 return $archiveNames;
2066 }
2067
2068 /**
2069 * @return array
2070 */
2071 protected function getOldRels() {
2072 if ( !isset( $this->srcRels['.'] ) ) {
2073 $oldRels =& $this->srcRels;
2074 $deleteCurrent = false;
2075 } else {
2076 $oldRels = $this->srcRels;
2077 unset( $oldRels['.'] );
2078 $deleteCurrent = true;
2079 }
2080
2081 return [ $oldRels, $deleteCurrent ];
2082 }
2083
2084 /**
2085 * @return array
2086 */
2087 protected function getHashes() {
2088 $hashes = [];
2089 list( $oldRels, $deleteCurrent ) = $this->getOldRels();
2090
2091 if ( $deleteCurrent ) {
2092 $hashes['.'] = $this->file->getSha1();
2093 }
2094
2095 if ( count( $oldRels ) ) {
2096 $dbw = $this->file->repo->getMasterDB();
2097 $res = $dbw->select(
2098 'oldimage',
2099 [ 'oi_archive_name', 'oi_sha1' ],
2100 [ 'oi_archive_name' => array_keys( $oldRels ),
2101 'oi_name' => $this->file->getName() ], // performance
2102 __METHOD__
2103 );
2104
2105 foreach ( $res as $row ) {
2106 if ( rtrim( $row->oi_sha1, "\0" ) === '' ) {
2107 // Get the hash from the file
2108 $oldUrl = $this->file->getArchiveVirtualUrl( $row->oi_archive_name );
2109 $props = $this->file->repo->getFileProps( $oldUrl );
2110
2111 if ( $props['fileExists'] ) {
2112 // Upgrade the oldimage row
2113 $dbw->update( 'oldimage',
2114 [ 'oi_sha1' => $props['sha1'] ],
2115 [ 'oi_name' => $this->file->getName(), 'oi_archive_name' => $row->oi_archive_name ],
2116 __METHOD__ );
2117 $hashes[$row->oi_archive_name] = $props['sha1'];
2118 } else {
2119 $hashes[$row->oi_archive_name] = false;
2120 }
2121 } else {
2122 $hashes[$row->oi_archive_name] = $row->oi_sha1;
2123 }
2124 }
2125 }
2126
2127 $missing = array_diff_key( $this->srcRels, $hashes );
2128
2129 foreach ( $missing as $name => $rel ) {
2130 $this->status->error( 'filedelete-old-unregistered', $name );
2131 }
2132
2133 foreach ( $hashes as $name => $hash ) {
2134 if ( !$hash ) {
2135 $this->status->error( 'filedelete-missing', $this->srcRels[$name] );
2136 unset( $hashes[$name] );
2137 }
2138 }
2139
2140 return $hashes;
2141 }
2142
2143 protected function doDBInserts() {
2144 $dbw = $this->file->repo->getMasterDB();
2145 $encTimestamp = $dbw->addQuotes( $dbw->timestamp() );
2146 $encUserId = $dbw->addQuotes( $this->user->getId() );
2147 $encReason = $dbw->addQuotes( $this->reason );
2148 $encGroup = $dbw->addQuotes( 'deleted' );
2149 $ext = $this->file->getExtension();
2150 $dotExt = $ext === '' ? '' : ".$ext";
2151 $encExt = $dbw->addQuotes( $dotExt );
2152 list( $oldRels, $deleteCurrent ) = $this->getOldRels();
2153
2154 // Bitfields to further suppress the content
2155 if ( $this->suppress ) {
2156 $bitfield = 0;
2157 // This should be 15...
2158 $bitfield |= Revision::DELETED_TEXT;
2159 $bitfield |= Revision::DELETED_COMMENT;
2160 $bitfield |= Revision::DELETED_USER;
2161 $bitfield |= Revision::DELETED_RESTRICTED;
2162 } else {
2163 $bitfield = 'oi_deleted';
2164 }
2165
2166 if ( $deleteCurrent ) {
2167 $concat = $dbw->buildConcat( [ "img_sha1", $encExt ] );
2168 $where = [ 'img_name' => $this->file->getName() ];
2169 $dbw->insertSelect( 'filearchive', 'image',
2170 [
2171 'fa_storage_group' => $encGroup,
2172 'fa_storage_key' => $dbw->conditional(
2173 [ 'img_sha1' => '' ],
2174 $dbw->addQuotes( '' ),
2175 $concat
2176 ),
2177 'fa_deleted_user' => $encUserId,
2178 'fa_deleted_timestamp' => $encTimestamp,
2179 'fa_deleted_reason' => $encReason,
2180 'fa_deleted' => $this->suppress ? $bitfield : 0,
2181
2182 'fa_name' => 'img_name',
2183 'fa_archive_name' => 'NULL',
2184 'fa_size' => 'img_size',
2185 'fa_width' => 'img_width',
2186 'fa_height' => 'img_height',
2187 'fa_metadata' => 'img_metadata',
2188 'fa_bits' => 'img_bits',
2189 'fa_media_type' => 'img_media_type',
2190 'fa_major_mime' => 'img_major_mime',
2191 'fa_minor_mime' => 'img_minor_mime',
2192 'fa_description' => 'img_description',
2193 'fa_user' => 'img_user',
2194 'fa_user_text' => 'img_user_text',
2195 'fa_timestamp' => 'img_timestamp',
2196 'fa_sha1' => 'img_sha1',
2197 ], $where, __METHOD__ );
2198 }
2199
2200 if ( count( $oldRels ) ) {
2201 $concat = $dbw->buildConcat( [ "oi_sha1", $encExt ] );
2202 $where = [
2203 'oi_name' => $this->file->getName(),
2204 'oi_archive_name' => array_keys( $oldRels ) ];
2205 $dbw->insertSelect( 'filearchive', 'oldimage',
2206 [
2207 'fa_storage_group' => $encGroup,
2208 'fa_storage_key' => $dbw->conditional(
2209 [ 'oi_sha1' => '' ],
2210 $dbw->addQuotes( '' ),
2211 $concat
2212 ),
2213 'fa_deleted_user' => $encUserId,
2214 'fa_deleted_timestamp' => $encTimestamp,
2215 'fa_deleted_reason' => $encReason,
2216 'fa_deleted' => $this->suppress ? $bitfield : 'oi_deleted',
2217
2218 'fa_name' => 'oi_name',
2219 'fa_archive_name' => 'oi_archive_name',
2220 'fa_size' => 'oi_size',
2221 'fa_width' => 'oi_width',
2222 'fa_height' => 'oi_height',
2223 'fa_metadata' => 'oi_metadata',
2224 'fa_bits' => 'oi_bits',
2225 'fa_media_type' => 'oi_media_type',
2226 'fa_major_mime' => 'oi_major_mime',
2227 'fa_minor_mime' => 'oi_minor_mime',
2228 'fa_description' => 'oi_description',
2229 'fa_user' => 'oi_user',
2230 'fa_user_text' => 'oi_user_text',
2231 'fa_timestamp' => 'oi_timestamp',
2232 'fa_sha1' => 'oi_sha1',
2233 ], $where, __METHOD__ );
2234 }
2235 }
2236
2237 function doDBDeletes() {
2238 $dbw = $this->file->repo->getMasterDB();
2239 list( $oldRels, $deleteCurrent ) = $this->getOldRels();
2240
2241 if ( count( $oldRels ) ) {
2242 $dbw->delete( 'oldimage',
2243 [
2244 'oi_name' => $this->file->getName(),
2245 'oi_archive_name' => array_keys( $oldRels )
2246 ], __METHOD__ );
2247 }
2248
2249 if ( $deleteCurrent ) {
2250 $dbw->delete( 'image', [ 'img_name' => $this->file->getName() ], __METHOD__ );
2251 }
2252 }
2253
2254 /**
2255 * Run the transaction
2256 * @return FileRepoStatus
2257 */
2258 public function execute() {
2259 $repo = $this->file->getRepo();
2260 $this->file->lock();
2261
2262 // Prepare deletion batch
2263 $hashes = $this->getHashes();
2264 $this->deletionBatch = [];
2265 $ext = $this->file->getExtension();
2266 $dotExt = $ext === '' ? '' : ".$ext";
2267
2268 foreach ( $this->srcRels as $name => $srcRel ) {
2269 // Skip files that have no hash (e.g. missing DB record, or sha1 field and file source)
2270 if ( isset( $hashes[$name] ) ) {
2271 $hash = $hashes[$name];
2272 $key = $hash . $dotExt;
2273 $dstRel = $repo->getDeletedHashPath( $key ) . $key;
2274 $this->deletionBatch[$name] = [ $srcRel, $dstRel ];
2275 }
2276 }
2277
2278 // Lock the filearchive rows so that the files don't get deleted by a cleanup operation
2279 // We acquire this lock by running the inserts now, before the file operations.
2280 // This potentially has poor lock contention characteristics -- an alternative
2281 // scheme would be to insert stub filearchive entries with no fa_name and commit
2282 // them in a separate transaction, then run the file ops, then update the fa_name fields.
2283 $this->doDBInserts();
2284
2285 if ( !$repo->hasSha1Storage() ) {
2286 // Removes non-existent file from the batch, so we don't get errors.
2287 // This also handles files in the 'deleted' zone deleted via revision deletion.
2288 $checkStatus = $this->removeNonexistentFiles( $this->deletionBatch );
2289 if ( !$checkStatus->isGood() ) {
2290 $this->status->merge( $checkStatus );
2291 return $this->status;
2292 }
2293 $this->deletionBatch = $checkStatus->value;
2294
2295 // Execute the file deletion batch
2296 $status = $this->file->repo->deleteBatch( $this->deletionBatch );
2297
2298 if ( !$status->isGood() ) {
2299 $this->status->merge( $status );
2300 }
2301 }
2302
2303 if ( !$this->status->isOK() ) {
2304 // Critical file deletion error
2305 // Roll back inserts, release lock and abort
2306 // TODO: delete the defunct filearchive rows if we are using a non-transactional DB
2307 $this->file->unlockAndRollback();
2308
2309 return $this->status;
2310 }
2311
2312 // Delete image/oldimage rows
2313 $this->doDBDeletes();
2314
2315 // Commit and return
2316 $this->file->unlock();
2317
2318 return $this->status;
2319 }
2320
2321 /**
2322 * Removes non-existent files from a deletion batch.
2323 * @param array $batch
2324 * @return Status
2325 */
2326 protected function removeNonexistentFiles( $batch ) {
2327 $files = $newBatch = [];
2328
2329 foreach ( $batch as $batchItem ) {
2330 list( $src, ) = $batchItem;
2331 $files[$src] = $this->file->repo->getVirtualUrl( 'public' ) . '/' . rawurlencode( $src );
2332 }
2333
2334 $result = $this->file->repo->fileExistsBatch( $files );
2335 if ( in_array( null, $result, true ) ) {
2336 return Status::newFatal( 'backend-fail-internal',
2337 $this->file->repo->getBackend()->getName() );
2338 }
2339
2340 foreach ( $batch as $batchItem ) {
2341 if ( $result[$batchItem[0]] ) {
2342 $newBatch[] = $batchItem;
2343 }
2344 }
2345
2346 return Status::newGood( $newBatch );
2347 }
2348 }
2349
2350 # ------------------------------------------------------------------------------
2351
2352 /**
2353 * Helper class for file undeletion
2354 * @ingroup FileAbstraction
2355 */
2356 class LocalFileRestoreBatch {
2357 /** @var LocalFile */
2358 private $file;
2359
2360 /** @var array List of file IDs to restore */
2361 private $cleanupBatch;
2362
2363 /** @var array List of file IDs to restore */
2364 private $ids;
2365
2366 /** @var bool Add all revisions of the file */
2367 private $all;
2368
2369 /** @var bool Whether to remove all settings for suppressed fields */
2370 private $unsuppress = false;
2371
2372 /**
2373 * @param File $file
2374 * @param bool $unsuppress
2375 */
2376 function __construct( File $file, $unsuppress = false ) {
2377 $this->file = $file;
2378 $this->cleanupBatch = $this->ids = [];
2379 $this->ids = [];
2380 $this->unsuppress = $unsuppress;
2381 }
2382
2383 /**
2384 * Add a file by ID
2385 * @param int $fa_id
2386 */
2387 public function addId( $fa_id ) {
2388 $this->ids[] = $fa_id;
2389 }
2390
2391 /**
2392 * Add a whole lot of files by ID
2393 * @param int[] $ids
2394 */
2395 public function addIds( $ids ) {
2396 $this->ids = array_merge( $this->ids, $ids );
2397 }
2398
2399 /**
2400 * Add all revisions of the file
2401 */
2402 public function addAll() {
2403 $this->all = true;
2404 }
2405
2406 /**
2407 * Run the transaction, except the cleanup batch.
2408 * The cleanup batch should be run in a separate transaction, because it locks different
2409 * rows and there's no need to keep the image row locked while it's acquiring those locks
2410 * The caller may have its own transaction open.
2411 * So we save the batch and let the caller call cleanup()
2412 * @return FileRepoStatus
2413 */
2414 public function execute() {
2415 global $wgLang;
2416
2417 $repo = $this->file->getRepo();
2418 if ( !$this->all && !$this->ids ) {
2419 // Do nothing
2420 return $repo->newGood();
2421 }
2422
2423 $lockOwnsTrx = $this->file->lock();
2424
2425 $dbw = $this->file->repo->getMasterDB();
2426 $status = $this->file->repo->newGood();
2427
2428 $exists = (bool)$dbw->selectField( 'image', '1',
2429 [ 'img_name' => $this->file->getName() ],
2430 __METHOD__,
2431 // The lock() should already prevents changes, but this still may need
2432 // to bypass any transaction snapshot. However, if lock() started the
2433 // trx (which it probably did) then snapshot is post-lock and up-to-date.
2434 $lockOwnsTrx ? [] : [ 'LOCK IN SHARE MODE' ]
2435 );
2436
2437 // Fetch all or selected archived revisions for the file,
2438 // sorted from the most recent to the oldest.
2439 $conditions = [ 'fa_name' => $this->file->getName() ];
2440
2441 if ( !$this->all ) {
2442 $conditions['fa_id'] = $this->ids;
2443 }
2444
2445 $result = $dbw->select(
2446 'filearchive',
2447 ArchivedFile::selectFields(),
2448 $conditions,
2449 __METHOD__,
2450 [ 'ORDER BY' => 'fa_timestamp DESC' ]
2451 );
2452
2453 $idsPresent = [];
2454 $storeBatch = [];
2455 $insertBatch = [];
2456 $insertCurrent = false;
2457 $deleteIds = [];
2458 $first = true;
2459 $archiveNames = [];
2460
2461 foreach ( $result as $row ) {
2462 $idsPresent[] = $row->fa_id;
2463
2464 if ( $row->fa_name != $this->file->getName() ) {
2465 $status->error( 'undelete-filename-mismatch', $wgLang->timeanddate( $row->fa_timestamp ) );
2466 $status->failCount++;
2467 continue;
2468 }
2469
2470 if ( $row->fa_storage_key == '' ) {
2471 // Revision was missing pre-deletion
2472 $status->error( 'undelete-bad-store-key', $wgLang->timeanddate( $row->fa_timestamp ) );
2473 $status->failCount++;
2474 continue;
2475 }
2476
2477 $deletedRel = $repo->getDeletedHashPath( $row->fa_storage_key ) .
2478 $row->fa_storage_key;
2479 $deletedUrl = $repo->getVirtualUrl() . '/deleted/' . $deletedRel;
2480
2481 if ( isset( $row->fa_sha1 ) ) {
2482 $sha1 = $row->fa_sha1;
2483 } else {
2484 // old row, populate from key
2485 $sha1 = LocalRepo::getHashFromKey( $row->fa_storage_key );
2486 }
2487
2488 # Fix leading zero
2489 if ( strlen( $sha1 ) == 32 && $sha1[0] == '0' ) {
2490 $sha1 = substr( $sha1, 1 );
2491 }
2492
2493 if ( is_null( $row->fa_major_mime ) || $row->fa_major_mime == 'unknown'
2494 || is_null( $row->fa_minor_mime ) || $row->fa_minor_mime == 'unknown'
2495 || is_null( $row->fa_media_type ) || $row->fa_media_type == 'UNKNOWN'
2496 || is_null( $row->fa_metadata )
2497 ) {
2498 // Refresh our metadata
2499 // Required for a new current revision; nice for older ones too. :)
2500 $props = RepoGroup::singleton()->getFileProps( $deletedUrl );
2501 } else {
2502 $props = [
2503 'minor_mime' => $row->fa_minor_mime,
2504 'major_mime' => $row->fa_major_mime,
2505 'media_type' => $row->fa_media_type,
2506 'metadata' => $row->fa_metadata
2507 ];
2508 }
2509
2510 if ( $first && !$exists ) {
2511 // This revision will be published as the new current version
2512 $destRel = $this->file->getRel();
2513 $insertCurrent = [
2514 'img_name' => $row->fa_name,
2515 'img_size' => $row->fa_size,
2516 'img_width' => $row->fa_width,
2517 'img_height' => $row->fa_height,
2518 'img_metadata' => $props['metadata'],
2519 'img_bits' => $row->fa_bits,
2520 'img_media_type' => $props['media_type'],
2521 'img_major_mime' => $props['major_mime'],
2522 'img_minor_mime' => $props['minor_mime'],
2523 'img_description' => $row->fa_description,
2524 'img_user' => $row->fa_user,
2525 'img_user_text' => $row->fa_user_text,
2526 'img_timestamp' => $row->fa_timestamp,
2527 'img_sha1' => $sha1
2528 ];
2529
2530 // The live (current) version cannot be hidden!
2531 if ( !$this->unsuppress && $row->fa_deleted ) {
2532 $storeBatch[] = [ $deletedUrl, 'public', $destRel ];
2533 $this->cleanupBatch[] = $row->fa_storage_key;
2534 }
2535 } else {
2536 $archiveName = $row->fa_archive_name;
2537
2538 if ( $archiveName == '' ) {
2539 // This was originally a current version; we
2540 // have to devise a new archive name for it.
2541 // Format is <timestamp of archiving>!<name>
2542 $timestamp = wfTimestamp( TS_UNIX, $row->fa_deleted_timestamp );
2543
2544 do {
2545 $archiveName = wfTimestamp( TS_MW, $timestamp ) . '!' . $row->fa_name;
2546 $timestamp++;
2547 } while ( isset( $archiveNames[$archiveName] ) );
2548 }
2549
2550 $archiveNames[$archiveName] = true;
2551 $destRel = $this->file->getArchiveRel( $archiveName );
2552 $insertBatch[] = [
2553 'oi_name' => $row->fa_name,
2554 'oi_archive_name' => $archiveName,
2555 'oi_size' => $row->fa_size,
2556 'oi_width' => $row->fa_width,
2557 'oi_height' => $row->fa_height,
2558 'oi_bits' => $row->fa_bits,
2559 'oi_description' => $row->fa_description,
2560 'oi_user' => $row->fa_user,
2561 'oi_user_text' => $row->fa_user_text,
2562 'oi_timestamp' => $row->fa_timestamp,
2563 'oi_metadata' => $props['metadata'],
2564 'oi_media_type' => $props['media_type'],
2565 'oi_major_mime' => $props['major_mime'],
2566 'oi_minor_mime' => $props['minor_mime'],
2567 'oi_deleted' => $this->unsuppress ? 0 : $row->fa_deleted,
2568 'oi_sha1' => $sha1 ];
2569 }
2570
2571 $deleteIds[] = $row->fa_id;
2572
2573 if ( !$this->unsuppress && $row->fa_deleted & File::DELETED_FILE ) {
2574 // private files can stay where they are
2575 $status->successCount++;
2576 } else {
2577 $storeBatch[] = [ $deletedUrl, 'public', $destRel ];
2578 $this->cleanupBatch[] = $row->fa_storage_key;
2579 }
2580
2581 $first = false;
2582 }
2583
2584 unset( $result );
2585
2586 // Add a warning to the status object for missing IDs
2587 $missingIds = array_diff( $this->ids, $idsPresent );
2588
2589 foreach ( $missingIds as $id ) {
2590 $status->error( 'undelete-missing-filearchive', $id );
2591 }
2592
2593 if ( !$repo->hasSha1Storage() ) {
2594 // Remove missing files from batch, so we don't get errors when undeleting them
2595 $checkStatus = $this->removeNonexistentFiles( $storeBatch );
2596 if ( !$checkStatus->isGood() ) {
2597 $status->merge( $checkStatus );
2598 return $status;
2599 }
2600 $storeBatch = $checkStatus->value;
2601
2602 // Run the store batch
2603 // Use the OVERWRITE_SAME flag to smooth over a common error
2604 $storeStatus = $this->file->repo->storeBatch( $storeBatch, FileRepo::OVERWRITE_SAME );
2605 $status->merge( $storeStatus );
2606
2607 if ( !$status->isGood() ) {
2608 // Even if some files could be copied, fail entirely as that is the
2609 // easiest thing to do without data loss
2610 $this->cleanupFailedBatch( $storeStatus, $storeBatch );
2611 $status->ok = false;
2612 $this->file->unlock();
2613
2614 return $status;
2615 }
2616 }
2617
2618 // Run the DB updates
2619 // Because we have locked the image row, key conflicts should be rare.
2620 // If they do occur, we can roll back the transaction at this time with
2621 // no data loss, but leaving unregistered files scattered throughout the
2622 // public zone.
2623 // This is not ideal, which is why it's important to lock the image row.
2624 if ( $insertCurrent ) {
2625 $dbw->insert( 'image', $insertCurrent, __METHOD__ );
2626 }
2627
2628 if ( $insertBatch ) {
2629 $dbw->insert( 'oldimage', $insertBatch, __METHOD__ );
2630 }
2631
2632 if ( $deleteIds ) {
2633 $dbw->delete( 'filearchive',
2634 [ 'fa_id' => $deleteIds ],
2635 __METHOD__ );
2636 }
2637
2638 // If store batch is empty (all files are missing), deletion is to be considered successful
2639 if ( $status->successCount > 0 || !$storeBatch || $repo->hasSha1Storage() ) {
2640 if ( !$exists ) {
2641 wfDebug( __METHOD__ . " restored {$status->successCount} items, creating a new current\n" );
2642
2643 DeferredUpdates::addUpdate( SiteStatsUpdate::factory( [ 'images' => 1 ] ) );
2644
2645 $this->file->purgeEverything();
2646 } else {
2647 wfDebug( __METHOD__ . " restored {$status->successCount} as archived versions\n" );
2648 $this->file->purgeDescription();
2649 }
2650 }
2651
2652 $this->file->unlock();
2653
2654 return $status;
2655 }
2656
2657 /**
2658 * Removes non-existent files from a store batch.
2659 * @param array $triplets
2660 * @return Status
2661 */
2662 protected function removeNonexistentFiles( $triplets ) {
2663 $files = $filteredTriplets = [];
2664 foreach ( $triplets as $file ) {
2665 $files[$file[0]] = $file[0];
2666 }
2667
2668 $result = $this->file->repo->fileExistsBatch( $files );
2669 if ( in_array( null, $result, true ) ) {
2670 return Status::newFatal( 'backend-fail-internal',
2671 $this->file->repo->getBackend()->getName() );
2672 }
2673
2674 foreach ( $triplets as $file ) {
2675 if ( $result[$file[0]] ) {
2676 $filteredTriplets[] = $file;
2677 }
2678 }
2679
2680 return Status::newGood( $filteredTriplets );
2681 }
2682
2683 /**
2684 * Removes non-existent files from a cleanup batch.
2685 * @param array $batch
2686 * @return array
2687 */
2688 protected function removeNonexistentFromCleanup( $batch ) {
2689 $files = $newBatch = [];
2690 $repo = $this->file->repo;
2691
2692 foreach ( $batch as $file ) {
2693 $files[$file] = $repo->getVirtualUrl( 'deleted' ) . '/' .
2694 rawurlencode( $repo->getDeletedHashPath( $file ) . $file );
2695 }
2696
2697 $result = $repo->fileExistsBatch( $files );
2698
2699 foreach ( $batch as $file ) {
2700 if ( $result[$file] ) {
2701 $newBatch[] = $file;
2702 }
2703 }
2704
2705 return $newBatch;
2706 }
2707
2708 /**
2709 * Delete unused files in the deleted zone.
2710 * This should be called from outside the transaction in which execute() was called.
2711 * @return FileRepoStatus
2712 */
2713 public function cleanup() {
2714 if ( !$this->cleanupBatch ) {
2715 return $this->file->repo->newGood();
2716 }
2717
2718 $this->cleanupBatch = $this->removeNonexistentFromCleanup( $this->cleanupBatch );
2719
2720 $status = $this->file->repo->cleanupDeletedBatch( $this->cleanupBatch );
2721
2722 return $status;
2723 }
2724
2725 /**
2726 * Cleanup a failed batch. The batch was only partially successful, so
2727 * rollback by removing all items that were succesfully copied.
2728 *
2729 * @param Status $storeStatus
2730 * @param array $storeBatch
2731 */
2732 protected function cleanupFailedBatch( $storeStatus, $storeBatch ) {
2733 $cleanupBatch = [];
2734
2735 foreach ( $storeStatus->success as $i => $success ) {
2736 // Check if this item of the batch was successfully copied
2737 if ( $success ) {
2738 // Item was successfully copied and needs to be removed again
2739 // Extract ($dstZone, $dstRel) from the batch
2740 $cleanupBatch[] = [ $storeBatch[$i][1], $storeBatch[$i][2] ];
2741 }
2742 }
2743 $this->file->repo->cleanupBatch( $cleanupBatch );
2744 }
2745 }
2746
2747 # ------------------------------------------------------------------------------
2748
2749 /**
2750 * Helper class for file movement
2751 * @ingroup FileAbstraction
2752 */
2753 class LocalFileMoveBatch {
2754 /** @var LocalFile */
2755 protected $file;
2756
2757 /** @var Title */
2758 protected $target;
2759
2760 protected $cur;
2761
2762 protected $olds;
2763
2764 protected $oldCount;
2765
2766 protected $archive;
2767
2768 /** @var DatabaseBase */
2769 protected $db;
2770
2771 /**
2772 * @param File $file
2773 * @param Title $target
2774 */
2775 function __construct( File $file, Title $target ) {
2776 $this->file = $file;
2777 $this->target = $target;
2778 $this->oldHash = $this->file->repo->getHashPath( $this->file->getName() );
2779 $this->newHash = $this->file->repo->getHashPath( $this->target->getDBkey() );
2780 $this->oldName = $this->file->getName();
2781 $this->newName = $this->file->repo->getNameFromTitle( $this->target );
2782 $this->oldRel = $this->oldHash . $this->oldName;
2783 $this->newRel = $this->newHash . $this->newName;
2784 $this->db = $file->getRepo()->getMasterDB();
2785 }
2786
2787 /**
2788 * Add the current image to the batch
2789 */
2790 public function addCurrent() {
2791 $this->cur = [ $this->oldRel, $this->newRel ];
2792 }
2793
2794 /**
2795 * Add the old versions of the image to the batch
2796 * @return array List of archive names from old versions
2797 */
2798 public function addOlds() {
2799 $archiveBase = 'archive';
2800 $this->olds = [];
2801 $this->oldCount = 0;
2802 $archiveNames = [];
2803
2804 $result = $this->db->select( 'oldimage',
2805 [ 'oi_archive_name', 'oi_deleted' ],
2806 [ 'oi_name' => $this->oldName ],
2807 __METHOD__,
2808 [ 'LOCK IN SHARE MODE' ] // ignore snapshot
2809 );
2810
2811 foreach ( $result as $row ) {
2812 $archiveNames[] = $row->oi_archive_name;
2813 $oldName = $row->oi_archive_name;
2814 $bits = explode( '!', $oldName, 2 );
2815
2816 if ( count( $bits ) != 2 ) {
2817 wfDebug( "Old file name missing !: '$oldName' \n" );
2818 continue;
2819 }
2820
2821 list( $timestamp, $filename ) = $bits;
2822
2823 if ( $this->oldName != $filename ) {
2824 wfDebug( "Old file name doesn't match: '$oldName' \n" );
2825 continue;
2826 }
2827
2828 $this->oldCount++;
2829
2830 // Do we want to add those to oldCount?
2831 if ( $row->oi_deleted & File::DELETED_FILE ) {
2832 continue;
2833 }
2834
2835 $this->olds[] = [
2836 "{$archiveBase}/{$this->oldHash}{$oldName}",
2837 "{$archiveBase}/{$this->newHash}{$timestamp}!{$this->newName}"
2838 ];
2839 }
2840
2841 return $archiveNames;
2842 }
2843
2844 /**
2845 * Perform the move.
2846 * @return FileRepoStatus
2847 */
2848 public function execute() {
2849 $repo = $this->file->repo;
2850 $status = $repo->newGood();
2851
2852 $triplets = $this->getMoveTriplets();
2853 $checkStatus = $this->removeNonexistentFiles( $triplets );
2854 if ( !$checkStatus->isGood() ) {
2855 $status->merge( $checkStatus );
2856 return $status;
2857 }
2858 $triplets = $checkStatus->value;
2859 $destFile = wfLocalFile( $this->target );
2860
2861 $this->file->lock(); // begin
2862 $destFile->lock(); // quickly fail if destination is not available
2863 // Rename the file versions metadata in the DB.
2864 // This implicitly locks the destination file, which avoids race conditions.
2865 // If we moved the files from A -> C before DB updates, another process could
2866 // move files from B -> C at this point, causing storeBatch() to fail and thus
2867 // cleanupTarget() to trigger. It would delete the C files and cause data loss.
2868 $statusDb = $this->doDBUpdates();
2869 if ( !$statusDb->isGood() ) {
2870 $destFile->unlock();
2871 $this->file->unlockAndRollback();
2872 $statusDb->ok = false;
2873
2874 return $statusDb;
2875 }
2876 wfDebugLog( 'imagemove', "Renamed {$this->file->getName()} in database: " .
2877 "{$statusDb->successCount} successes, {$statusDb->failCount} failures" );
2878
2879 if ( !$repo->hasSha1Storage() ) {
2880 // Copy the files into their new location.
2881 // If a prior process fataled copying or cleaning up files we tolerate any
2882 // of the existing files if they are identical to the ones being stored.
2883 $statusMove = $repo->storeBatch( $triplets, FileRepo::OVERWRITE_SAME );
2884 wfDebugLog( 'imagemove', "Moved files for {$this->file->getName()}: " .
2885 "{$statusMove->successCount} successes, {$statusMove->failCount} failures" );
2886 if ( !$statusMove->isGood() ) {
2887 // Delete any files copied over (while the destination is still locked)
2888 $this->cleanupTarget( $triplets );
2889 $destFile->unlock();
2890 $this->file->unlockAndRollback(); // unlocks the destination
2891 wfDebugLog( 'imagemove', "Error in moving files: "
2892 . $statusMove->getWikiText( false, false, 'en' ) );
2893 $statusMove->ok = false;
2894
2895 return $statusMove;
2896 }
2897 $status->merge( $statusMove );
2898 }
2899
2900 $destFile->unlock();
2901 $this->file->unlock(); // done
2902
2903 // Everything went ok, remove the source files
2904 $this->cleanupSource( $triplets );
2905
2906 $status->merge( $statusDb );
2907
2908 return $status;
2909 }
2910
2911 /**
2912 * Do the database updates and return a new FileRepoStatus indicating how
2913 * many rows where updated.
2914 *
2915 * @return FileRepoStatus
2916 */
2917 protected function doDBUpdates() {
2918 $repo = $this->file->repo;
2919 $status = $repo->newGood();
2920 $dbw = $this->db;
2921
2922 // Update current image
2923 $dbw->update(
2924 'image',
2925 [ 'img_name' => $this->newName ],
2926 [ 'img_name' => $this->oldName ],
2927 __METHOD__
2928 );
2929
2930 if ( $dbw->affectedRows() ) {
2931 $status->successCount++;
2932 } else {
2933 $status->failCount++;
2934 $status->fatal( 'imageinvalidfilename' );
2935
2936 return $status;
2937 }
2938
2939 // Update old images
2940 $dbw->update(
2941 'oldimage',
2942 [
2943 'oi_name' => $this->newName,
2944 'oi_archive_name = ' . $dbw->strreplace( 'oi_archive_name',
2945 $dbw->addQuotes( $this->oldName ), $dbw->addQuotes( $this->newName ) ),
2946 ],
2947 [ 'oi_name' => $this->oldName ],
2948 __METHOD__
2949 );
2950
2951 $affected = $dbw->affectedRows();
2952 $total = $this->oldCount;
2953 $status->successCount += $affected;
2954 // Bug 34934: $total is based on files that actually exist.
2955 // There may be more DB rows than such files, in which case $affected
2956 // can be greater than $total. We use max() to avoid negatives here.
2957 $status->failCount += max( 0, $total - $affected );
2958 if ( $status->failCount ) {
2959 $status->error( 'imageinvalidfilename' );
2960 }
2961
2962 return $status;
2963 }
2964
2965 /**
2966 * Generate triplets for FileRepo::storeBatch().
2967 * @return array
2968 */
2969 protected function getMoveTriplets() {
2970 $moves = array_merge( [ $this->cur ], $this->olds );
2971 $triplets = []; // The format is: (srcUrl, destZone, destUrl)
2972
2973 foreach ( $moves as $move ) {
2974 // $move: (oldRelativePath, newRelativePath)
2975 $srcUrl = $this->file->repo->getVirtualUrl() . '/public/' . rawurlencode( $move[0] );
2976 $triplets[] = [ $srcUrl, 'public', $move[1] ];
2977 wfDebugLog(
2978 'imagemove',
2979 "Generated move triplet for {$this->file->getName()}: {$srcUrl} :: public :: {$move[1]}"
2980 );
2981 }
2982
2983 return $triplets;
2984 }
2985
2986 /**
2987 * Removes non-existent files from move batch.
2988 * @param array $triplets
2989 * @return Status
2990 */
2991 protected function removeNonexistentFiles( $triplets ) {
2992 $files = [];
2993
2994 foreach ( $triplets as $file ) {
2995 $files[$file[0]] = $file[0];
2996 }
2997
2998 $result = $this->file->repo->fileExistsBatch( $files );
2999 if ( in_array( null, $result, true ) ) {
3000 return Status::newFatal( 'backend-fail-internal',
3001 $this->file->repo->getBackend()->getName() );
3002 }
3003
3004 $filteredTriplets = [];
3005 foreach ( $triplets as $file ) {
3006 if ( $result[$file[0]] ) {
3007 $filteredTriplets[] = $file;
3008 } else {
3009 wfDebugLog( 'imagemove', "File {$file[0]} does not exist" );
3010 }
3011 }
3012
3013 return Status::newGood( $filteredTriplets );
3014 }
3015
3016 /**
3017 * Cleanup a partially moved array of triplets by deleting the target
3018 * files. Called if something went wrong half way.
3019 * @param array $triplets
3020 */
3021 protected function cleanupTarget( $triplets ) {
3022 // Create dest pairs from the triplets
3023 $pairs = [];
3024 foreach ( $triplets as $triplet ) {
3025 // $triplet: (old source virtual URL, dst zone, dest rel)
3026 $pairs[] = [ $triplet[1], $triplet[2] ];
3027 }
3028
3029 $this->file->repo->cleanupBatch( $pairs );
3030 }
3031
3032 /**
3033 * Cleanup a fully moved array of triplets by deleting the source files.
3034 * Called at the end of the move process if everything else went ok.
3035 * @param array $triplets
3036 */
3037 protected function cleanupSource( $triplets ) {
3038 // Create source file names from the triplets
3039 $files = [];
3040 foreach ( $triplets as $triplet ) {
3041 $files[] = $triplet[0];
3042 }
3043
3044 $this->file->repo->cleanupBatch( $files );
3045 }
3046 }
3047
3048 class LocalFileLockError extends Exception {
3049
3050 }