More rev_deleted merging
[lhc/web/wiklou.git] / includes / filerepo / LocalFile.php
1 <?php
2 /**
3 */
4
5 /**
6 * Bump this number when serialized cache records may be incompatible.
7 */
8 define( 'MW_FILE_VERSION', 7 );
9
10 /**
11 * Class to represent a local file in the wiki's own database
12 *
13 * Provides methods to retrieve paths (physical, logical, URL),
14 * to generate image thumbnails or for uploading.
15 *
16 * Note that only the repo object knows what its file class is called. You should
17 * never name a file class explictly outside of the repo class. Instead use the
18 * repo's factory functions to generate file objects, for example:
19 *
20 * RepoGroup::singleton()->getLocalRepo()->newFile($title);
21 *
22 * The convenience functions wfLocalFile() and wfFindFile() should be sufficient
23 * in most cases.
24 *
25 * @addtogroup FileRepo
26 */
27 class LocalFile extends File
28 {
29 /**#@+
30 * @private
31 */
32 var $fileExists, # does the file file exist on disk? (loadFromXxx)
33 $historyLine, # Number of line to return by nextHistoryLine() (constructor)
34 $historyRes, # result of the query for the file's history (nextHistoryLine)
35 $width, # \
36 $height, # |
37 $bits, # --- returned by getimagesize (loadFromXxx)
38 $attr, # /
39 $media_type, # MEDIATYPE_xxx (bitmap, drawing, audio...)
40 $mime, # MIME type, determined by MimeMagic::guessMimeType
41 $major_mime, # Major mime type
42 $minor_mime, # Minor mime type
43 $size, # Size in bytes (loadFromXxx)
44 $metadata, # Handler-specific metadata
45 $timestamp, # Upload timestamp
46 $sha1, # SHA-1 base 36 content hash
47 $user, $user_text, # User, who uploaded the file
48 $description, # Description of current revision of the file
49 $dataLoaded, # Whether or not all this has been loaded from the database (loadFromXxx)
50 $upgraded, # Whether the row was upgraded on load
51 $locked, # True if the image row is locked
52 $deleted; # Bitfield akin to rev_deleted
53
54 /**#@-*/
55
56 /**
57 * Create a LocalFile from a title
58 * Do not call this except from inside a repo class.
59 */
60 static function newFromTitle( $title, $repo ) {
61 return new self( $title, $repo );
62 }
63
64 /**
65 * Create a LocalFile from a title
66 * Do not call this except from inside a repo class.
67 */
68 static function newFromRow( $row, $repo ) {
69 $title = Title::makeTitle( NS_IMAGE, $row->img_name );
70 $file = new self( $title, $repo );
71 $file->loadFromRow( $row );
72 return $file;
73 }
74
75 /**
76 * Constructor.
77 * Do not call this except from inside a repo class.
78 */
79 function __construct( $title, $repo ) {
80 if( !is_object( $title ) ) {
81 throw new MWException( __CLASS__.' constructor given bogus title.' );
82 }
83 parent::__construct( $title, $repo );
84 $this->metadata = '';
85 $this->historyLine = 0;
86 $this->historyRes = null;
87 $this->dataLoaded = false;
88 }
89
90 /**
91 * Get the memcached key
92 */
93 function getCacheKey() {
94 $hashedName = md5($this->getName());
95 return wfMemcKey( 'file', $hashedName );
96 }
97
98 /**
99 * Try to load file metadata from memcached. Returns true on success.
100 */
101 function loadFromCache() {
102 global $wgMemc;
103 wfProfileIn( __METHOD__ );
104 $this->dataLoaded = false;
105 $key = $this->getCacheKey();
106 if ( !$key ) {
107 return false;
108 }
109 $cachedValues = $wgMemc->get( $key );
110
111 // Check if the key existed and belongs to this version of MediaWiki
112 if ( isset($cachedValues['version']) && ( $cachedValues['version'] == MW_FILE_VERSION ) ) {
113 wfDebug( "Pulling file metadata from cache key $key\n" );
114 $this->fileExists = $cachedValues['fileExists'];
115 if ( $this->fileExists ) {
116 $this->setProps( $cachedValues );
117 }
118 $this->dataLoaded = true;
119 }
120 if ( $this->dataLoaded ) {
121 wfIncrStats( 'image_cache_hit' );
122 } else {
123 wfIncrStats( 'image_cache_miss' );
124 }
125
126 wfProfileOut( __METHOD__ );
127 return $this->dataLoaded;
128 }
129
130 /**
131 * Save the file metadata to memcached
132 */
133 function saveToCache() {
134 global $wgMemc;
135 $this->load();
136 $key = $this->getCacheKey();
137 if ( !$key ) {
138 return;
139 }
140 $fields = $this->getCacheFields( '' );
141 $cache = array( 'version' => MW_FILE_VERSION );
142 $cache['fileExists'] = $this->fileExists;
143 if ( $this->fileExists ) {
144 foreach ( $fields as $field ) {
145 $cache[$field] = $this->$field;
146 }
147 }
148
149 $wgMemc->set( $key, $cache, 60 * 60 * 24 * 7 ); // A week
150 }
151
152 /**
153 * Load metadata from the file itself
154 */
155 function loadFromFile() {
156 $this->setProps( self::getPropsFromPath( $this->getPath() ) );
157 }
158
159 function getCacheFields( $prefix = 'img_' ) {
160 static $fields = array( 'size', 'width', 'height', 'bits', 'media_type',
161 'major_mime', 'minor_mime', 'metadata', 'timestamp', 'sha1', 'user', 'user_text', 'description' );
162 static $results = array();
163 if ( $prefix == '' ) {
164 return $fields;
165 }
166 if ( !isset( $results[$prefix] ) ) {
167 $prefixedFields = array();
168 foreach ( $fields as $field ) {
169 $prefixedFields[] = $prefix . $field;
170 }
171 $results[$prefix] = $prefixedFields;
172 }
173 return $results[$prefix];
174 }
175
176 /**
177 * Load file metadata from the DB
178 */
179 function loadFromDB() {
180 # Polymorphic function name to distinguish foreign and local fetches
181 $fname = get_class( $this ) . '::' . __FUNCTION__;
182 wfProfileIn( $fname );
183
184 # Unconditionally set loaded=true, we don't want the accessors constantly rechecking
185 $this->dataLoaded = true;
186
187 $dbr = $this->repo->getMasterDB();
188
189 $row = $dbr->selectRow( 'image', $this->getCacheFields( 'img_' ),
190 array( 'img_name' => $this->getName() ), $fname );
191 if ( $row ) {
192 $this->loadFromRow( $row );
193 } else {
194 $this->fileExists = false;
195 }
196
197 wfProfileOut( $fname );
198 }
199
200 /**
201 * Decode a row from the database (either object or array) to an array
202 * with timestamps and MIME types decoded, and the field prefix removed.
203 */
204 function decodeRow( $row, $prefix = 'img_' ) {
205 $array = (array)$row;
206 $prefixLength = strlen( $prefix );
207 // Sanity check prefix once
208 if ( substr( key( $array ), 0, $prefixLength ) !== $prefix ) {
209 throw new MWException( __METHOD__. ': incorrect $prefix parameter' );
210 }
211 $decoded = array();
212 foreach ( $array as $name => $value ) {
213 $decoded[substr( $name, $prefixLength )] = $value;
214 }
215 $decoded['timestamp'] = wfTimestamp( TS_MW, $decoded['timestamp'] );
216 if ( empty( $decoded['major_mime'] ) ) {
217 $decoded['mime'] = "unknown/unknown";
218 } else {
219 if (!$decoded['minor_mime']) {
220 $decoded['minor_mime'] = "unknown";
221 }
222 $decoded['mime'] = $decoded['major_mime'].'/'.$decoded['minor_mime'];
223 }
224 # Trim zero padding from char/binary field
225 $decoded['sha1'] = rtrim( $decoded['sha1'], "\0" );
226 return $decoded;
227 }
228
229 /*
230 * Load file metadata from a DB result row
231 */
232 function loadFromRow( $row, $prefix = 'img_' ) {
233 $this->dataLoaded = true;
234 $array = $this->decodeRow( $row, $prefix );
235 foreach ( $array as $name => $value ) {
236 $this->$name = $value;
237 }
238 $this->fileExists = true;
239 // Check if the file is hidden...
240 if( $this->isDeleted(File::DELETED_FILE) ) {
241 $this->fileExists = false; // treat as not existing
242 } else {
243 // Check for rows from a previous schema, quietly upgrade them
244 $this->maybeUpgradeRow();
245 }
246 }
247
248 /**
249 * Load file metadata from cache or DB, unless already loaded
250 */
251 function load() {
252 if ( !$this->dataLoaded ) {
253 if ( !$this->loadFromCache() ) {
254 $this->loadFromDB();
255 $this->saveToCache();
256 }
257 $this->dataLoaded = true;
258 }
259 }
260
261 /**
262 * Upgrade a row if it needs it
263 */
264 function maybeUpgradeRow() {
265 if ( wfReadOnly() ) {
266 return;
267 }
268 if ( is_null($this->media_type) ||
269 $this->mime == 'image/svg'
270 ) {
271 $this->upgradeRow();
272 $this->upgraded = true;
273 } else {
274 $handler = $this->getHandler();
275 if ( $handler && !$handler->isMetadataValid( $this, $this->metadata ) ) {
276 $this->upgradeRow();
277 $this->upgraded = true;
278 }
279 }
280 }
281
282 function getUpgraded() {
283 return $this->upgraded;
284 }
285
286 /**
287 * Fix assorted version-related problems with the image row by reloading it from the file
288 */
289 function upgradeRow() {
290 wfProfileIn( __METHOD__ );
291
292 $this->loadFromFile();
293
294 # Don't destroy file info of missing files
295 if ( !$this->fileExists ) {
296 wfDebug( __METHOD__.": file does not exist, aborting\n" );
297 return;
298 }
299 $dbw = $this->repo->getMasterDB();
300 list( $major, $minor ) = self::splitMime( $this->mime );
301
302 if ( wfReadOnly() ) {
303 return;
304 }
305 wfDebug(__METHOD__.': upgrading '.$this->getName()." to the current schema\n");
306
307 $dbw->update( 'image',
308 array(
309 'img_width' => $this->width,
310 'img_height' => $this->height,
311 'img_bits' => $this->bits,
312 'img_media_type' => $this->media_type,
313 'img_major_mime' => $major,
314 'img_minor_mime' => $minor,
315 'img_metadata' => $this->metadata,
316 'img_sha1' => $this->sha1,
317 ), array( 'img_name' => $this->getName() ),
318 __METHOD__
319 );
320 $this->saveToCache();
321 wfProfileOut( __METHOD__ );
322 }
323
324 /**
325 * Set properties in this object to be equal to those given in the
326 * associative array $info. Only cacheable fields can be set.
327 *
328 * If 'mime' is given, it will be split into major_mime/minor_mime.
329 * If major_mime/minor_mime are given, $this->mime will also be set.
330 */
331 function setProps( $info ) {
332 $this->dataLoaded = true;
333 $fields = $this->getCacheFields( '' );
334 $fields[] = 'fileExists';
335 foreach ( $fields as $field ) {
336 if ( isset( $info[$field] ) ) {
337 $this->$field = $info[$field];
338 }
339 }
340 // Fix up mime fields
341 if ( isset( $info['major_mime'] ) ) {
342 $this->mime = "{$info['major_mime']}/{$info['minor_mime']}";
343 } elseif ( isset( $info['mime'] ) ) {
344 list( $this->major_mime, $this->minor_mime ) = self::splitMime( $this->mime );
345 }
346 }
347
348 /** splitMime inherited */
349 /** getName inherited */
350 /** getTitle inherited */
351 /** getURL inherited */
352 /** getViewURL inherited */
353 /** getPath inherited */
354 /** isVisible inhereted */
355
356 /**
357 * Return the width of the image
358 *
359 * Returns false on error
360 * @public
361 */
362 function getWidth( $page = 1 ) {
363 $this->load();
364 if ( $this->isMultipage() ) {
365 $dim = $this->getHandler()->getPageDimensions( $this, $page );
366 if ( $dim ) {
367 return $dim['width'];
368 } else {
369 return false;
370 }
371 } else {
372 return $this->width;
373 }
374 }
375
376 /**
377 * Return the height of the image
378 *
379 * Returns false on error
380 * @public
381 */
382 function getHeight( $page = 1 ) {
383 $this->load();
384 if ( $this->isMultipage() ) {
385 $dim = $this->getHandler()->getPageDimensions( $this, $page );
386 if ( $dim ) {
387 return $dim['height'];
388 } else {
389 return false;
390 }
391 } else {
392 return $this->height;
393 }
394 }
395
396 /**
397 * Returns ID or name of user who uploaded the file
398 *
399 * @param $type string 'text' or 'id'
400 */
401 function getUser($type='text') {
402 $this->load();
403 if( $type == 'text' ) {
404 return $this->user_text;
405 } elseif( $type == 'id' ) {
406 return $this->user;
407 }
408 }
409
410 /**
411 * Get handler-specific metadata
412 */
413 function getMetadata() {
414 $this->load();
415 return $this->metadata;
416 }
417
418 /**
419 * Return the size of the image file, in bytes
420 * @public
421 */
422 function getSize() {
423 $this->load();
424 return $this->size;
425 }
426
427 /**
428 * Returns the mime type of the file.
429 */
430 function getMimeType() {
431 $this->load();
432 return $this->mime;
433 }
434
435 /**
436 * Return the type of the media in the file.
437 * Use the value returned by this function with the MEDIATYPE_xxx constants.
438 */
439 function getMediaType() {
440 $this->load();
441 return $this->media_type;
442 }
443
444 /** canRender inherited */
445 /** mustRender inherited */
446 /** allowInlineDisplay inherited */
447 /** isSafeFile inherited */
448 /** isTrustedFile inherited */
449
450 /**
451 * Returns true if the file file exists on disk.
452 * @return boolean Whether file file exist on disk.
453 * @public
454 */
455 function exists() {
456 $this->load();
457 return $this->fileExists;
458 }
459
460 /** getTransformScript inherited */
461 /** getUnscaledThumb inherited */
462 /** thumbName inherited */
463 /** createThumb inherited */
464 /** getThumbnail inherited */
465 /** transform inherited */
466
467 /**
468 * Fix thumbnail files from 1.4 or before, with extreme prejudice
469 */
470 function migrateThumbFile( $thumbName ) {
471 $thumbDir = $this->getThumbPath();
472 $thumbPath = "$thumbDir/$thumbName";
473 if ( is_dir( $thumbPath ) ) {
474 // Directory where file should be
475 // This happened occasionally due to broken migration code in 1.5
476 // Rename to broken-*
477 for ( $i = 0; $i < 100 ; $i++ ) {
478 $broken = $this->repo->getZonePath('public') . "/broken-$i-$thumbName";
479 if ( !file_exists( $broken ) ) {
480 rename( $thumbPath, $broken );
481 break;
482 }
483 }
484 // Doesn't exist anymore
485 clearstatcache();
486 }
487 if ( is_file( $thumbDir ) ) {
488 // File where directory should be
489 unlink( $thumbDir );
490 // Doesn't exist anymore
491 clearstatcache();
492 }
493 }
494
495 /** getHandler inherited */
496 /** iconThumb inherited */
497 /** getLastError inherited */
498
499 /**
500 * Get all thumbnail names previously generated for this file
501 */
502 function getThumbnails() {
503 if ( $this->isHashed() ) {
504 $this->load();
505 $files = array();
506 $dir = $this->getThumbPath();
507
508 if ( is_dir( $dir ) ) {
509 $handle = opendir( $dir );
510
511 if ( $handle ) {
512 while ( false !== ( $file = readdir($handle) ) ) {
513 if ( $file{0} != '.' ) {
514 $files[] = $file;
515 }
516 }
517 closedir( $handle );
518 }
519 }
520 } else {
521 $files = array();
522 }
523
524 return $files;
525 }
526
527 /**
528 * Refresh metadata in memcached, but don't touch thumbnails or squid
529 */
530 function purgeMetadataCache() {
531 $this->loadFromDB();
532 $this->saveToCache();
533 $this->purgeHistory();
534 }
535
536 /**
537 * Purge the shared history (OldLocalFile) cache
538 */
539 function purgeHistory() {
540 global $wgMemc;
541 $hashedName = md5($this->getName());
542 $oldKey = wfMemcKey( 'oldfile', $hashedName );
543 $wgMemc->delete( $oldKey );
544 }
545
546 /**
547 * Delete all previously generated thumbnails, refresh metadata in memcached and purge the squid
548 */
549 function purgeCache() {
550 // Refresh metadata cache
551 $this->purgeMetadataCache();
552
553 // Delete thumbnails
554 $this->purgeThumbnails();
555
556 // Purge squid cache for this file
557 wfPurgeSquidServers( array( $this->getURL() ) );
558 }
559
560 /**
561 * Delete cached transformed files
562 */
563 function purgeThumbnails() {
564 global $wgUseSquid;
565 // Delete thumbnails
566 $files = $this->getThumbnails();
567 $dir = $this->getThumbPath();
568 $urls = array();
569 foreach ( $files as $file ) {
570 # Check that the base file name is part of the thumb name
571 # This is a basic sanity check to avoid erasing unrelated directories
572 if ( strpos( $file, $this->getName() ) !== false ) {
573 $url = $this->getThumbUrl( $file );
574 $urls[] = $url;
575 @unlink( "$dir/$file" );
576 }
577 }
578
579 // Purge the squid
580 if ( $wgUseSquid ) {
581 wfPurgeSquidServers( $urls );
582 }
583 }
584
585 /** purgeDescription inherited */
586 /** purgeEverything inherited */
587
588 function getHistory($limit = null, $start = null, $end = null) {
589 $dbr = $this->repo->getSlaveDB();
590 $conds = $opts = array();
591 $conds[] = "oi_name = " . $dbr->addQuotes( $this->title->getDBKey() );
592 if( $start !== null ) {
593 $conds[] = "oi_timestamp <= " . $dbr->addQuotes( $dbr->timestamp( $start ) );
594 }
595 if( $end !== null ) {
596 $conds[] = "oi_timestamp >= " . $dbr->addQuotes( $dbr->timestamp( $end ) );
597 }
598 if( $limit ) {
599 $opts['LIMIT'] = $limit;
600 }
601 $opts['ORDER BY'] = 'oi_timestamp DESC';
602 $res = $dbr->select('oldimage', '*', $conds, __METHOD__, $opts);
603 $r = array();
604 while( $row = $dbr->fetchObject($res) ) {
605 $r[] = OldLocalFile::newFromRow($row, $this->repo);
606 }
607 return $r;
608 }
609
610 /**
611 * Return the history of this file, line by line.
612 * starts with current version, then old versions.
613 * uses $this->historyLine to check which line to return:
614 * 0 return line for current version
615 * 1 query for old versions, return first one
616 * 2, ... return next old version from above query
617 *
618 * @public
619 */
620 function nextHistoryLine() {
621 # Polymorphic function name to distinguish foreign and local fetches
622 $fname = get_class( $this ) . '::' . __FUNCTION__;
623
624 $dbr = $this->repo->getSlaveDB();
625
626 if ( $this->historyLine == 0 ) {// called for the first time, return line from cur
627 $this->historyRes = $dbr->select( 'image',
628 array(
629 '*',
630 "'' AS oi_archive_name",
631 '0 as oi_deleted',
632 'img_sha1'
633 ),
634 array( 'img_name' => $this->title->getDBkey() ),
635 $fname
636 );
637 if ( 0 == $dbr->numRows( $this->historyRes ) ) {
638 $dbr->freeResult($this->historyRes);
639 $this->historyRes = null;
640 return FALSE;
641 }
642 } else if ( $this->historyLine == 1 ) {
643 $dbr->freeResult($this->historyRes);
644 $this->historyRes = $dbr->select( 'oldimage', '*',
645 array( 'oi_name' => $this->title->getDBkey() ),
646 $fname,
647 array( 'ORDER BY' => 'oi_timestamp DESC' )
648 );
649 }
650 $this->historyLine ++;
651
652 return $dbr->fetchObject( $this->historyRes );
653 }
654
655 /**
656 * Reset the history pointer to the first element of the history
657 * @public
658 */
659 function resetHistory() {
660 $this->historyLine = 0;
661 if (!is_null($this->historyRes)) {
662 $this->repo->getSlaveDB()->freeResult($this->historyRes);
663 $this->historyRes = null;
664 }
665 }
666
667 /** getFullPath inherited */
668 /** getHashPath inherited */
669 /** getRel inherited */
670 /** getUrlRel inherited */
671 /** getArchiveRel inherited */
672 /** getThumbRel inherited */
673 /** getArchivePath inherited */
674 /** getThumbPath inherited */
675 /** getArchiveUrl inherited */
676 /** getThumbUrl inherited */
677 /** getArchiveVirtualUrl inherited */
678 /** getThumbVirtualUrl inherited */
679 /** isHashed inherited */
680
681 /**
682 * Upload a file and record it in the DB
683 * @param string $srcPath Source path or virtual URL
684 * @param string $comment Upload description
685 * @param string $pageText Text to use for the new description page, if a new description page is created
686 * @param integer $flags Flags for publish()
687 * @param array $props File properties, if known. This can be used to reduce the
688 * upload time when uploading virtual URLs for which the file info
689 * is already known
690 * @param string $timestamp Timestamp for img_timestamp, or false to use the current time
691 *
692 * @return FileRepoStatus object. On success, the value member contains the
693 * archive name, or an empty string if it was a new file.
694 */
695 function upload( $srcPath, $comment, $pageText, $flags = 0, $props = false, $timestamp = false ) {
696 $this->lock();
697 $status = $this->publish( $srcPath, $flags );
698 if ( $status->ok ) {
699 if ( !$this->recordUpload2( $status->value, $comment, $pageText, $props, $timestamp ) ) {
700 $status->fatal( 'filenotfound', $srcPath );
701 }
702 }
703 $this->unlock();
704 return $status;
705 }
706
707 /**
708 * Record a file upload in the upload log and the image table
709 * @deprecated use upload()
710 */
711 function recordUpload( $oldver, $desc, $license = '', $copyStatus = '', $source = '',
712 $watch = false, $timestamp = false )
713 {
714 $pageText = UploadForm::getInitialPageText( $desc, $license, $copyStatus, $source );
715 if ( !$this->recordUpload2( $oldver, $desc, $pageText ) ) {
716 return false;
717 }
718 if ( $watch ) {
719 global $wgUser;
720 $wgUser->addWatch( $this->getTitle() );
721 }
722 return true;
723
724 }
725
726 /**
727 * Record a file upload in the upload log and the image table
728 */
729 function recordUpload2( $oldver, $comment, $pageText, $props = false, $timestamp = false )
730 {
731 global $wgUser;
732
733 $dbw = $this->repo->getMasterDB();
734
735 if ( !$props ) {
736 $props = $this->repo->getFileProps( $this->getVirtualUrl() );
737 }
738 $props['description'] = $comment;
739 $props['user'] = $wgUser->getID();
740 $props['user_text'] = $wgUser->getName();
741 $props['timestamp'] = wfTimestamp( TS_MW );
742 $this->setProps( $props );
743
744 // Delete thumbnails and refresh the metadata cache
745 $this->purgeThumbnails();
746 $this->saveToCache();
747 wfPurgeSquidServers( array( $this->getURL() ) );
748
749 // Fail now if the file isn't there
750 if ( !$this->fileExists ) {
751 wfDebug( __METHOD__.": File ".$this->getPath()." went missing!\n" );
752 return false;
753 }
754
755 $reupload = false;
756 if ( $timestamp === false ) {
757 $timestamp = $dbw->timestamp();
758 }
759
760 # Test to see if the row exists using INSERT IGNORE
761 # This avoids race conditions by locking the row until the commit, and also
762 # doesn't deadlock. SELECT FOR UPDATE causes a deadlock for every race condition.
763 $dbw->insert( 'image',
764 array(
765 'img_name' => $this->getName(),
766 'img_size'=> $this->size,
767 'img_width' => intval( $this->width ),
768 'img_height' => intval( $this->height ),
769 'img_bits' => $this->bits,
770 'img_media_type' => $this->media_type,
771 'img_major_mime' => $this->major_mime,
772 'img_minor_mime' => $this->minor_mime,
773 'img_timestamp' => $timestamp,
774 'img_description' => $comment,
775 'img_user' => $wgUser->getID(),
776 'img_user_text' => $wgUser->getName(),
777 'img_metadata' => $this->metadata,
778 'img_sha1' => $this->sha1
779 ),
780 __METHOD__,
781 'IGNORE'
782 );
783
784 if( $dbw->affectedRows() == 0 ) {
785 $reupload = true;
786
787 # Collision, this is an update of a file
788 # Insert previous contents into oldimage
789 $dbw->insertSelect( 'oldimage', 'image',
790 array(
791 'oi_name' => 'img_name',
792 'oi_archive_name' => $dbw->addQuotes( $oldver ),
793 'oi_size' => 'img_size',
794 'oi_width' => 'img_width',
795 'oi_height' => 'img_height',
796 'oi_bits' => 'img_bits',
797 'oi_timestamp' => 'img_timestamp',
798 'oi_description' => 'img_description',
799 'oi_user' => 'img_user',
800 'oi_user_text' => 'img_user_text',
801 'oi_metadata' => 'img_metadata',
802 'oi_media_type' => 'img_media_type',
803 'oi_major_mime' => 'img_major_mime',
804 'oi_minor_mime' => 'img_minor_mime',
805 'oi_sha1' => 'img_sha1'
806 ), array( 'img_name' => $this->getName() ), __METHOD__
807 );
808
809 # Update the current image row
810 $dbw->update( 'image',
811 array( /* SET */
812 'img_size' => $this->size,
813 'img_width' => intval( $this->width ),
814 'img_height' => intval( $this->height ),
815 'img_bits' => $this->bits,
816 'img_media_type' => $this->media_type,
817 'img_major_mime' => $this->major_mime,
818 'img_minor_mime' => $this->minor_mime,
819 'img_timestamp' => $timestamp,
820 'img_description' => $comment,
821 'img_user' => $wgUser->getID(),
822 'img_user_text' => $wgUser->getName(),
823 'img_metadata' => $this->metadata,
824 'img_sha1' => $this->sha1
825 ), array( /* WHERE */
826 'img_name' => $this->getName()
827 ), __METHOD__
828 );
829 } else {
830 # This is a new file
831 # Update the image count
832 $site_stats = $dbw->tableName( 'site_stats' );
833 $dbw->query( "UPDATE $site_stats SET ss_images=ss_images+1", __METHOD__ );
834 }
835
836 $descTitle = $this->getTitle();
837 $article = new Article( $descTitle );
838
839 # Add the log entry
840 $log = new LogPage( 'upload' );
841 $action = $reupload ? 'overwrite' : 'upload';
842 $log->addEntry( $action, $descTitle, $comment );
843
844 if( $descTitle->exists() ) {
845 # Create a null revision
846 $nullRevision = Revision::newNullRevision( $dbw, $descTitle->getArticleId(), $log->getRcComment(), false );
847 $nullRevision->insertOn( $dbw );
848 $article->updateRevisionOn( $dbw, $nullRevision );
849
850 # Invalidate the cache for the description page
851 $descTitle->invalidateCache();
852 $descTitle->purgeSquid();
853 } else {
854 // New file; create the description page.
855 // There's already a log entry, so don't make a second RC entry
856 $article->doEdit( $pageText, $comment, EDIT_NEW | EDIT_SUPPRESS_RC );
857 }
858
859 # Hooks, hooks, the magic of hooks...
860 wfRunHooks( 'FileUpload', array( $this ) );
861
862 # Commit the transaction now, in case something goes wrong later
863 # The most important thing is that files don't get lost, especially archives
864 $dbw->immediateCommit();
865
866 # Invalidate cache for all pages using this file
867 $update = new HTMLCacheUpdate( $this->getTitle(), 'imagelinks' );
868 $update->doUpdate();
869
870 return true;
871 }
872
873 /**
874 * Move or copy a file to its public location. If a file exists at the
875 * destination, move it to an archive. Returns the archive name on success
876 * or an empty string if it was a new file, and a wikitext-formatted
877 * WikiError object on failure.
878 *
879 * The archive name should be passed through to recordUpload for database
880 * registration.
881 *
882 * @param string $sourcePath Local filesystem path to the source image
883 * @param integer $flags A bitwise combination of:
884 * File::DELETE_SOURCE Delete the source file, i.e. move
885 * rather than copy
886 * @return FileRepoStatus object. On success, the value member contains the
887 * archive name, or an empty string if it was a new file.
888 */
889 function publish( $srcPath, $flags = 0 ) {
890 $this->lock();
891 $dstRel = $this->getRel();
892 $archiveName = gmdate( 'YmdHis' ) . '!'. $this->getName();
893 $archiveRel = 'archive/' . $this->getHashPath() . $archiveName;
894 $flags = $flags & File::DELETE_SOURCE ? LocalRepo::DELETE_SOURCE : 0;
895 $status = $this->repo->publish( $srcPath, $dstRel, $archiveRel, $flags );
896 if ( $status->value == 'new' ) {
897 $status->value = '';
898 } else {
899 $status->value = $archiveName;
900 }
901 $this->unlock();
902 return $status;
903 }
904
905 /** getLinksTo inherited */
906 /** getExifData inherited */
907 /** isLocal inherited */
908 /** wasDeleted inherited */
909
910 /**
911 * Delete all versions of the file.
912 *
913 * Moves the files into an archive directory (or deletes them)
914 * and removes the database rows.
915 *
916 * Cache purging is done; logging is caller's responsibility.
917 *
918 * @param $reason
919 * @param $suppress
920 * @return FileRepoStatus object.
921 */
922 function delete( $reason, $suppress = false ) {
923 $this->lock();
924 $batch = new LocalFileDeleteBatch( $this, $reason, $suppress );
925 $batch->addCurrent();
926
927 # Get old version relative paths
928 $dbw = $this->repo->getMasterDB();
929 $result = $dbw->select( 'oldimage',
930 array( 'oi_archive_name' ),
931 array( 'oi_name' => $this->getName() ) );
932 while ( $row = $dbw->fetchObject( $result ) ) {
933 $batch->addOld( $row->oi_archive_name );
934 }
935 $status = $batch->execute();
936
937 if ( $status->ok ) {
938 // Update site_stats
939 $site_stats = $dbw->tableName( 'site_stats' );
940 $dbw->query( "UPDATE $site_stats SET ss_images=ss_images-1", __METHOD__ );
941 $this->purgeEverything();
942 }
943
944 $this->unlock();
945 return $status;
946 }
947
948 /**
949 * Delete an old version of the file.
950 *
951 * Moves the file into an archive directory (or deletes it)
952 * and removes the database row.
953 *
954 * Cache purging is done; logging is caller's responsibility.
955 *
956 * @param $reason
957 * @param $suppress
958 * @throws MWException or FSException on database or filestore failure
959 * @return FileRepoStatus object.
960 */
961 function deleteOld( $archiveName, $reason, $suppress=false ) {
962 $this->lock();
963 $batch = new LocalFileDeleteBatch( $this, $reason, $suppress );
964 $batch->addOld( $archiveName );
965 $status = $batch->execute();
966 $this->unlock();
967 if ( $status->ok ) {
968 $this->purgeDescription();
969 $this->purgeHistory();
970 }
971 return $status;
972 }
973
974 /**
975 * Restore all or specified deleted revisions to the given file.
976 * Permissions and logging are left to the caller.
977 *
978 * May throw database exceptions on error.
979 *
980 * @param $versions set of record ids of deleted items to restore,
981 * or empty to restore all revisions.
982 * @param $unuppress
983 * @return FileRepoStatus
984 */
985 function restore( $versions = array(), $unsuppress = false ) {
986 $batch = new LocalFileRestoreBatch( $this, $unsuppress );
987 if ( !$versions ) {
988 $batch->addAll();
989 } else {
990 $batch->addIds( $versions );
991 }
992 $status = $batch->execute();
993 if ( !$status->ok ) {
994 return $status;
995 }
996
997 $cleanupStatus = $batch->cleanup();
998 $cleanupStatus->successCount = 0;
999 $cleanupStatus->failCount = 0;
1000 $status->merge( $cleanupStatus );
1001 return $status;
1002 }
1003
1004 /** isMultipage inherited */
1005 /** pageCount inherited */
1006 /** scaleHeight inherited */
1007 /** getImageSize inherited */
1008
1009 /**
1010 * Get the URL of the file description page.
1011 */
1012 function getDescriptionUrl() {
1013 return $this->title->getLocalUrl();
1014 }
1015
1016 /**
1017 * Get the HTML text of the description page
1018 * This is not used by ImagePage for local files, since (among other things)
1019 * it skips the parser cache.
1020 */
1021 function getDescriptionText() {
1022 global $wgParser;
1023 $revision = Revision::newFromTitle( $this->title );
1024 if ( !$revision ) return false;
1025 $text = $revision->getText();
1026 if ( !$text ) return false;
1027 $html = $wgParser->parse( $text, new ParserOptions );
1028 return $html;
1029 }
1030
1031 function getDescription() {
1032 $this->load();
1033 return $this->description;
1034 }
1035
1036 function getTimestamp() {
1037 $this->load();
1038 return $this->timestamp;
1039 }
1040
1041 function getSha1() {
1042 $this->load();
1043 // Initialise now if necessary
1044 if ( $this->sha1 == '' && $this->fileExists ) {
1045 $this->sha1 = File::sha1Base36( $this->getPath() );
1046 if ( strval( $this->sha1 ) != '' ) {
1047 $dbw = $this->repo->getMasterDB();
1048 $dbw->update( 'image',
1049 array( 'img_sha1' => $this->sha1 ),
1050 array( 'img_name' => $this->getName() ),
1051 __METHOD__ );
1052 $this->saveToCache();
1053 }
1054 }
1055
1056 return $this->sha1;
1057 }
1058
1059 /**
1060 * Start a transaction and lock the image for update
1061 * Increments a reference counter if the lock is already held
1062 * @return boolean True if the image exists, false otherwise
1063 */
1064 function lock() {
1065 $dbw = $this->repo->getMasterDB();
1066 if ( !$this->locked ) {
1067 $dbw->begin();
1068 $this->locked++;
1069 }
1070 return $dbw->selectField( 'image', '1', array( 'img_name' => $this->getName() ), __METHOD__ );
1071 }
1072
1073 /**
1074 * Decrement the lock reference count. If the reference count is reduced to zero, commits
1075 * the transaction and thereby releases the image lock.
1076 */
1077 function unlock() {
1078 if ( $this->locked ) {
1079 --$this->locked;
1080 if ( !$this->locked ) {
1081 $dbw = $this->repo->getMasterDB();
1082 $dbw->commit();
1083 }
1084 }
1085 }
1086
1087 /**
1088 * Roll back the DB transaction and mark the image unlocked
1089 */
1090 function unlockAndRollback() {
1091 $this->locked = false;
1092 $dbw = $this->repo->getMasterDB();
1093 $dbw->rollback();
1094 }
1095 } // LocalFile class
1096
1097 #------------------------------------------------------------------------------
1098
1099 /**
1100 * Backwards compatibility class
1101 */
1102 class Image extends LocalFile {
1103 function __construct( $title ) {
1104 $repo = RepoGroup::singleton()->getLocalRepo();
1105 parent::__construct( $title, $repo );
1106 }
1107
1108 /**
1109 * Wrapper for wfFindFile(), for backwards-compatibility only
1110 * Do not use in core code.
1111 * @deprecated
1112 */
1113 static function newFromTitle( $title, $time = false ) {
1114 $img = wfFindFile( $title, $time );
1115 if ( !$img ) {
1116 $img = wfLocalFile( $title );
1117 }
1118 return $img;
1119 }
1120
1121 /**
1122 * Wrapper for wfFindFile(), for backwards-compatibility only.
1123 * Do not use in core code.
1124 *
1125 * @param string $name name of the image, used to create a title object using Title::makeTitleSafe
1126 * @return image object or null if invalid title
1127 * @deprecated
1128 */
1129 static function newFromName( $name ) {
1130 $title = Title::makeTitleSafe( NS_IMAGE, $name );
1131 if ( is_object( $title ) ) {
1132 $img = wfFindFile( $title );
1133 if ( !$img ) {
1134 $img = wfLocalFile( $title );
1135 }
1136 return $img;
1137 } else {
1138 return NULL;
1139 }
1140 }
1141
1142 /**
1143 * Return the URL of an image, provided its name.
1144 *
1145 * Backwards-compatibility for extensions.
1146 * Note that fromSharedDirectory will only use the shared path for files
1147 * that actually exist there now, and will return local paths otherwise.
1148 *
1149 * @param string $name Name of the image, without the leading "Image:"
1150 * @param boolean $fromSharedDirectory Should this be in $wgSharedUploadPath?
1151 * @return string URL of $name image
1152 * @deprecated
1153 */
1154 static function imageUrl( $name, $fromSharedDirectory = false ) {
1155 $image = null;
1156 if( $fromSharedDirectory ) {
1157 $image = wfFindFile( $name );
1158 }
1159 if( !$image ) {
1160 $image = wfLocalFile( $name );
1161 }
1162 return $image->getUrl();
1163 }
1164 }
1165
1166 #------------------------------------------------------------------------------
1167
1168 /**
1169 * Helper class for file deletion
1170 */
1171 class LocalFileDeleteBatch {
1172 var $file, $reason, $srcRels = array(), $archiveUrls = array(), $deletionBatch, $suppress;
1173 var $status;
1174
1175 function __construct( File $file, $reason = '', $suppress = false ) {
1176 $this->file = $file;
1177 $this->reason = $reason;
1178 $this->suppress = $suppress;
1179 $this->status = $file->repo->newGood();
1180 }
1181
1182 function addCurrent() {
1183 $this->srcRels['.'] = $this->file->getRel();
1184 }
1185
1186 function addOld( $oldName ) {
1187 $this->srcRels[$oldName] = $this->file->getArchiveRel( $oldName );
1188 $this->archiveUrls[] = $this->file->getArchiveUrl( $oldName );
1189 }
1190
1191 function getOldRels() {
1192 if ( !isset( $this->srcRels['.'] ) ) {
1193 $oldRels =& $this->srcRels;
1194 $deleteCurrent = false;
1195 } else {
1196 $oldRels = $this->srcRels;
1197 unset( $oldRels['.'] );
1198 $deleteCurrent = true;
1199 }
1200 return array( $oldRels, $deleteCurrent );
1201 }
1202
1203 /*protected*/ function getHashes() {
1204 $hashes = array();
1205 list( $oldRels, $deleteCurrent ) = $this->getOldRels();
1206 if ( $deleteCurrent ) {
1207 $hashes['.'] = $this->file->getSha1();
1208 }
1209 if ( count( $oldRels ) ) {
1210 $dbw = $this->file->repo->getMasterDB();
1211 $res = $dbw->select( 'oldimage', array( 'oi_archive_name', 'oi_sha1' ),
1212 'oi_archive_name IN(' . $dbw->makeList( array_keys( $oldRels ) ) . ')',
1213 __METHOD__ );
1214 while ( $row = $dbw->fetchObject( $res ) ) {
1215 if ( rtrim( $row->oi_sha1, "\0" ) === '' ) {
1216 // Get the hash from the file
1217 $oldUrl = $this->file->getArchiveVirtualUrl( $row->oi_archive_name );
1218 $props = $this->file->repo->getFileProps( $oldUrl );
1219 if ( $props['fileExists'] ) {
1220 // Upgrade the oldimage row
1221 $dbw->update( 'oldimage',
1222 array( 'oi_sha1' => $props['sha1'] ),
1223 array( 'oi_name' => $this->file->getName(), 'oi_archive_name' => $row->oi_archive_name ),
1224 __METHOD__ );
1225 $hashes[$row->oi_archive_name] = $props['sha1'];
1226 } else {
1227 $hashes[$row->oi_archive_name] = false;
1228 }
1229 } else {
1230 $hashes[$row->oi_archive_name] = $row->oi_sha1;
1231 }
1232 }
1233 }
1234 $missing = array_diff_key( $this->srcRels, $hashes );
1235 foreach ( $missing as $name => $rel ) {
1236 $this->status->error( 'filedelete-old-unregistered', $name );
1237 }
1238 foreach ( $hashes as $name => $hash ) {
1239 if ( !$hash ) {
1240 $this->status->error( 'filedelete-missing', $this->srcRels[$name] );
1241 unset( $hashes[$name] );
1242 }
1243 }
1244
1245 return $hashes;
1246 }
1247
1248 function doDBInserts() {
1249 global $wgUser;
1250 $dbw = $this->file->repo->getMasterDB();
1251 $encTimestamp = $dbw->addQuotes( $dbw->timestamp() );
1252 $encUserId = $dbw->addQuotes( $wgUser->getId() );
1253 $encReason = $dbw->addQuotes( $this->reason );
1254 $encGroup = $dbw->addQuotes( 'deleted' );
1255 $ext = $this->file->getExtension();
1256 $dotExt = $ext === '' ? '' : ".$ext";
1257 $encExt = $dbw->addQuotes( $dotExt );
1258 list( $oldRels, $deleteCurrent ) = $this->getOldRels();
1259
1260 // Bitfields to further suppress the content
1261 if ( $this->suppress ) {
1262 $bitfield = 0;
1263 // This should be 15...
1264 $bitfield |= Revision::DELETED_TEXT;
1265 $bitfield |= Revision::DELETED_COMMENT;
1266 $bitfield |= Revision::DELETED_USER;
1267 $bitfield |= Revision::DELETED_RESTRICTED;
1268 } else {
1269 $bitfield = 'oi_deleted';
1270 }
1271
1272 if ( $deleteCurrent ) {
1273 $concat = $dbw->buildConcat( array( "img_sha1", $encExt ) );
1274 $where = array( 'img_name' => $this->file->getName() );
1275 $dbw->insertSelect( 'filearchive', 'image',
1276 array(
1277 'fa_storage_group' => $encGroup,
1278 'fa_storage_key' => "CASE WHEN img_sha1='' THEN '' ELSE $concat END",
1279 'fa_deleted_user' => $encUserId,
1280 'fa_deleted_timestamp' => $encTimestamp,
1281 'fa_deleted_reason' => $encReason,
1282 'fa_deleted' => $this->suppress ? $bitfield : 0,
1283
1284 'fa_name' => 'img_name',
1285 'fa_archive_name' => 'NULL',
1286 'fa_size' => 'img_size',
1287 'fa_width' => 'img_width',
1288 'fa_height' => 'img_height',
1289 'fa_metadata' => 'img_metadata',
1290 'fa_bits' => 'img_bits',
1291 'fa_media_type' => 'img_media_type',
1292 'fa_major_mime' => 'img_major_mime',
1293 'fa_minor_mime' => 'img_minor_mime',
1294 'fa_description' => 'img_description',
1295 'fa_user' => 'img_user',
1296 'fa_user_text' => 'img_user_text',
1297 'fa_timestamp' => 'img_timestamp'
1298 ), $where, __METHOD__ );
1299 }
1300
1301 if ( count( $oldRels ) ) {
1302 $concat = $dbw->buildConcat( array( "oi_sha1", $encExt ) );
1303 $where = array(
1304 'oi_name' => $this->file->getName(),
1305 'oi_archive_name IN (' . $dbw->makeList( array_keys( $oldRels ) ) . ')' );
1306 $dbw->insertSelect( 'filearchive', 'oldimage',
1307 array(
1308 'fa_storage_group' => $encGroup,
1309 'fa_storage_key' => "CASE WHEN oi_sha1='' THEN '' ELSE $concat END",
1310 'fa_deleted_user' => $encUserId,
1311 'fa_deleted_timestamp' => $encTimestamp,
1312 'fa_deleted_reason' => $encReason,
1313 'fa_deleted' => $this->suppress ? $bitfield : 'oi_deleted',
1314
1315 'fa_name' => 'oi_name',
1316 'fa_archive_name' => 'oi_archive_name',
1317 'fa_size' => 'oi_size',
1318 'fa_width' => 'oi_width',
1319 'fa_height' => 'oi_height',
1320 'fa_metadata' => 'oi_metadata',
1321 'fa_bits' => 'oi_bits',
1322 'fa_media_type' => 'oi_media_type',
1323 'fa_major_mime' => 'oi_major_mime',
1324 'fa_minor_mime' => 'oi_minor_mime',
1325 'fa_description' => 'oi_description',
1326 'fa_user' => 'oi_user',
1327 'fa_user_text' => 'oi_user_text',
1328 'fa_timestamp' => 'oi_timestamp',
1329 'fa_deleted' => $bitfield
1330 ), $where, __METHOD__ );
1331 }
1332 }
1333
1334 function doDBDeletes() {
1335 $dbw = $this->file->repo->getMasterDB();
1336 list( $oldRels, $deleteCurrent ) = $this->getOldRels();
1337 if ( count( $oldRels ) ) {
1338 $dbw->delete( 'oldimage',
1339 array(
1340 'oi_name' => $this->file->getName(),
1341 'oi_archive_name IN (' . $dbw->makeList( array_keys( $oldRels ) ) . ')'
1342 ), __METHOD__ );
1343 }
1344 if ( $deleteCurrent ) {
1345 $dbw->delete( 'image', array( 'img_name' => $this->file->getName() ), __METHOD__ );
1346 }
1347 }
1348
1349 /**
1350 * Run the transaction
1351 */
1352 function execute() {
1353 global $wgUser, $wgUseSquid;
1354 wfProfileIn( __METHOD__ );
1355
1356 $this->file->lock();
1357 // Leave private files alone
1358 $privateFiles = array();
1359 list( $oldRels, $deleteCurrent ) = $this->getOldRels();
1360 $dbw = $this->file->repo->getMasterDB();
1361 if( !empty( $oldRels ) ) {
1362 $res = $dbw->select( 'oldimage',
1363 array( 'oi_archive_name', 'oi_sha1' ),
1364 array( 'oi_name' => $this->file->getName(),
1365 'oi_archive_name IN (' . $dbw->makeList( array_keys($oldRels) ) . ')',
1366 'oi_deleted & ' . File::DELETED_FILE => File::DELETED_FILE ),
1367 __METHOD__ );
1368 while( $row = $dbw->fetchObject( $res ) ) {
1369 $title = $this->file->getTitle();
1370 $privateFiles[$row->oi_archive_name] = 1;
1371 }
1372 }
1373 // Prepare deletion batch
1374 $hashes = $this->getHashes();
1375 $this->deletionBatch = array();
1376 $ext = $this->file->getExtension();
1377 $dotExt = $ext === '' ? '' : ".$ext";
1378 foreach ( $this->srcRels as $name => $srcRel ) {
1379 // Skip files that have no hash (missing source).
1380 // Keep private files where they are.
1381 if ( isset($hashes[$name]) && !array_key_exists($name,$privateFiles) ) {
1382 $hash = $hashes[$name];
1383 $key = $hash . $dotExt;
1384 $dstRel = $this->file->repo->getDeletedHashPath( $key ) . $key;
1385 $this->deletionBatch[$name] = array( $srcRel, $dstRel );
1386 }
1387 }
1388
1389 // Lock the filearchive rows so that the files don't get deleted by a cleanup operation
1390 // We acquire this lock by running the inserts now, before the file operations.
1391 //
1392 // This potentially has poor lock contention characteristics -- an alternative
1393 // scheme would be to insert stub filearchive entries with no fa_name and commit
1394 // them in a separate transaction, then run the file ops, then update the fa_name fields.
1395 $this->doDBInserts();
1396
1397 // Execute the file deletion batch
1398 $status = $this->file->repo->deleteBatch( $this->deletionBatch );
1399 if ( !$status->isGood() ) {
1400 $this->status->merge( $status );
1401 }
1402
1403 if ( !$this->status->ok ) {
1404 // Critical file deletion error
1405 // Roll back inserts, release lock and abort
1406 // TODO: delete the defunct filearchive rows if we are using a non-transactional DB
1407 $this->file->unlockAndRollback();
1408 return $this->status;
1409 }
1410
1411 // Purge squid
1412 if ( $wgUseSquid ) {
1413 $urls = array();
1414 foreach ( $this->srcRels as $srcRel ) {
1415 $urlRel = str_replace( '%2F', '/', rawurlencode( $srcRel ) );
1416 $urls[] = $this->file->repo->getZoneUrl( 'public' ) . '/' . $urlRel;
1417 }
1418 SquidUpdate::purge( $urls );
1419 }
1420
1421 // Delete image/oldimage rows
1422 $this->doDBDeletes();
1423
1424 // Commit and return
1425 $this->file->unlock();
1426 wfProfileOut( __METHOD__ );
1427 return $this->status;
1428 }
1429 }
1430
1431 #------------------------------------------------------------------------------
1432
1433 /**
1434 * Helper class for file undeletion
1435 */
1436 class LocalFileRestoreBatch {
1437 var $file, $cleanupBatch, $ids, $all, $unsuppress = false;
1438
1439 function __construct( File $file, $unsuppress = false ) {
1440 $this->file = $file;
1441 $this->cleanupBatch = $this->ids = array();
1442 $this->ids = array();
1443 $this->unsuppress = $unsuppress;
1444 }
1445
1446 /**
1447 * Add a file by ID
1448 */
1449 function addId( $fa_id ) {
1450 $this->ids[] = $fa_id;
1451 }
1452
1453 /**
1454 * Add a whole lot of files by ID
1455 */
1456 function addIds( $ids ) {
1457 $this->ids = array_merge( $this->ids, $ids );
1458 }
1459
1460 /**
1461 * Add all revisions of the file
1462 */
1463 function addAll() {
1464 $this->all = true;
1465 }
1466
1467 /**
1468 * Run the transaction, except the cleanup batch.
1469 * The cleanup batch should be run in a separate transaction, because it locks different
1470 * rows and there's no need to keep the image row locked while it's acquiring those locks
1471 * The caller may have its own transaction open.
1472 * So we save the batch and let the caller call cleanup()
1473 */
1474 function execute() {
1475 global $wgUser, $wgLang;
1476 if ( !$this->all && !$this->ids ) {
1477 // Do nothing
1478 return $this->file->repo->newGood();
1479 }
1480
1481 $exists = $this->file->lock();
1482 $dbw = $this->file->repo->getMasterDB();
1483 $status = $this->file->repo->newGood();
1484
1485 // Fetch all or selected archived revisions for the file,
1486 // sorted from the most recent to the oldest.
1487 $conditions = array( 'fa_name' => $this->file->getName() );
1488 if( !$this->all ) {
1489 $conditions[] = 'fa_id IN (' . $dbw->makeList( $this->ids ) . ')';
1490 }
1491
1492 $result = $dbw->select( 'filearchive', '*',
1493 $conditions,
1494 __METHOD__,
1495 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
1496
1497 $idsPresent = array();
1498 $storeBatch = array();
1499 $insertBatch = array();
1500 $insertCurrent = false;
1501 $deleteIds = array();
1502 $first = true;
1503 $archiveNames = array();
1504 while( $row = $dbw->fetchObject( $result ) ) {
1505 $idsPresent[] = $row->fa_id;
1506
1507 if ( $row->fa_name != $this->file->getName() ) {
1508 $status->error( 'undelete-filename-mismatch', $wgLang->timeanddate( $row->fa_timestamp ) );
1509 $status->failCount++;
1510 continue;
1511 }
1512 if ( $row->fa_storage_key == '' ) {
1513 // Revision was missing pre-deletion
1514 $status->error( 'undelete-bad-store-key', $wgLang->timeanddate( $row->fa_timestamp ) );
1515 $status->failCount++;
1516 continue;
1517 }
1518
1519 $deletedRel = $this->file->repo->getDeletedHashPath( $row->fa_storage_key ) . $row->fa_storage_key;
1520 $deletedUrl = $this->file->repo->getVirtualUrl() . '/deleted/' . $deletedRel;
1521
1522 $sha1 = substr( $row->fa_storage_key, 0, strcspn( $row->fa_storage_key, '.' ) );
1523 # Fix leading zero
1524 if ( strlen( $sha1 ) == 32 && $sha1[0] == '0' ) {
1525 $sha1 = substr( $sha1, 1 );
1526 }
1527
1528 if( is_null( $row->fa_major_mime ) || $row->fa_major_mime == 'unknown'
1529 || is_null( $row->fa_minor_mime ) || $row->fa_minor_mime == 'unknown'
1530 || is_null( $row->fa_media_type ) || $row->fa_media_type == 'UNKNOWN'
1531 || is_null( $row->fa_metadata ) ) {
1532 // Refresh our metadata
1533 // Required for a new current revision; nice for older ones too. :)
1534 $props = RepoGroup::singleton()->getFileProps( $deletedUrl );
1535 } else {
1536 $props = array(
1537 'minor_mime' => $row->fa_minor_mime,
1538 'major_mime' => $row->fa_major_mime,
1539 'media_type' => $row->fa_media_type,
1540 'metadata' => $row->fa_metadata );
1541 }
1542
1543 if ( $first && !$exists ) {
1544 // The live (current) version cannot be hidden!
1545 if( !$this->unsuppress && $row->fa_deleted ) {
1546 $this->file->unlock();
1547 return $status;
1548 }
1549 // This revision will be published as the new current version
1550 $destRel = $this->file->getRel();
1551 $insertCurrent = array(
1552 'img_name' => $row->fa_name,
1553 'img_size' => $row->fa_size,
1554 'img_width' => $row->fa_width,
1555 'img_height' => $row->fa_height,
1556 'img_metadata' => $props['metadata'],
1557 'img_bits' => $row->fa_bits,
1558 'img_media_type' => $props['media_type'],
1559 'img_major_mime' => $props['major_mime'],
1560 'img_minor_mime' => $props['minor_mime'],
1561 'img_description' => $row->fa_description,
1562 'img_user' => $row->fa_user,
1563 'img_user_text' => $row->fa_user_text,
1564 'img_timestamp' => $row->fa_timestamp,
1565 'img_sha1' => $sha1);
1566 } else {
1567 $archiveName = $row->fa_archive_name;
1568 if( $archiveName == '' ) {
1569 // This was originally a current version; we
1570 // have to devise a new archive name for it.
1571 // Format is <timestamp of archiving>!<name>
1572 $timestamp = wfTimestamp( TS_UNIX, $row->fa_deleted_timestamp );
1573 do {
1574 $archiveName = wfTimestamp( TS_MW, $timestamp ) . '!' . $row->fa_name;
1575 $timestamp++;
1576 } while ( isset( $archiveNames[$archiveName] ) );
1577 }
1578 $archiveNames[$archiveName] = true;
1579 $destRel = $this->file->getArchiveRel( $archiveName );
1580 $insertBatch[] = array(
1581 'oi_name' => $row->fa_name,
1582 'oi_archive_name' => $archiveName,
1583 'oi_size' => $row->fa_size,
1584 'oi_width' => $row->fa_width,
1585 'oi_height' => $row->fa_height,
1586 'oi_bits' => $row->fa_bits,
1587 'oi_description' => $row->fa_description,
1588 'oi_user' => $row->fa_user,
1589 'oi_user_text' => $row->fa_user_text,
1590 'oi_timestamp' => $row->fa_timestamp,
1591 'oi_metadata' => $props['metadata'],
1592 'oi_media_type' => $props['media_type'],
1593 'oi_major_mime' => $props['major_mime'],
1594 'oi_minor_mime' => $props['minor_mime'],
1595 'oi_deleted' => $this->unsuppress ? 0 : $row->fa_deleted,
1596 'oi_sha1' => $sha1 );
1597 }
1598
1599 $deleteIds[] = $row->fa_id;
1600 if( !$this->unsuppress && $row->fa_deleted & File::DELETED_FILE ) {
1601 // private files can stay where they are
1602 } else {
1603 $storeBatch[] = array( $deletedUrl, 'public', $destRel );
1604 $this->cleanupBatch[] = $row->fa_storage_key;
1605 }
1606 $first = false;
1607 }
1608 unset( $result );
1609
1610 // Add a warning to the status object for missing IDs
1611 $missingIds = array_diff( $this->ids, $idsPresent );
1612 foreach ( $missingIds as $id ) {
1613 $status->error( 'undelete-missing-filearchive', $id );
1614 }
1615
1616 // Run the store batch
1617 // Use the OVERWRITE_SAME flag to smooth over a common error
1618 $storeStatus = $this->file->repo->storeBatch( $storeBatch, FileRepo::OVERWRITE_SAME );
1619 $status->merge( $storeStatus );
1620
1621 if ( !$status->ok ) {
1622 // Store batch returned a critical error -- this usually means nothing was stored
1623 // Stop now and return an error
1624 $this->file->unlock();
1625 return $status;
1626 }
1627
1628 // Run the DB updates
1629 // Because we have locked the image row, key conflicts should be rare.
1630 // If they do occur, we can roll back the transaction at this time with
1631 // no data loss, but leaving unregistered files scattered throughout the
1632 // public zone.
1633 // This is not ideal, which is why it's important to lock the image row.
1634 if ( $insertCurrent ) {
1635 $dbw->insert( 'image', $insertCurrent, __METHOD__ );
1636 }
1637 if ( $insertBatch ) {
1638 $dbw->insert( 'oldimage', $insertBatch, __METHOD__ );
1639 }
1640 if ( $deleteIds ) {
1641 $dbw->delete( 'filearchive',
1642 array( 'fa_id IN (' . $dbw->makeList( $deleteIds ) . ')' ),
1643 __METHOD__ );
1644 }
1645
1646 if( $status->successCount > 0 ) {
1647 if( !$exists ) {
1648 wfDebug( __METHOD__." restored {$status->successCount} items, creating a new current\n" );
1649
1650 // Update site_stats
1651 $site_stats = $dbw->tableName( 'site_stats' );
1652 $dbw->query( "UPDATE $site_stats SET ss_images=ss_images+1", __METHOD__ );
1653
1654 $this->file->purgeEverything();
1655 } else {
1656 wfDebug( __METHOD__." restored {$status->successCount} as archived versions\n" );
1657 $this->file->purgeDescription();
1658 $this->file->purgeHistory();
1659 }
1660 }
1661 $this->file->unlock();
1662 return $status;
1663 }
1664
1665 /**
1666 * Delete unused files in the deleted zone.
1667 * This should be called from outside the transaction in which execute() was called.
1668 */
1669 function cleanup() {
1670 if ( !$this->cleanupBatch ) {
1671 return $this->file->repo->newGood();
1672 }
1673 $status = $this->file->repo->cleanupDeletedBatch( $this->cleanupBatch );
1674 return $status;
1675 }
1676 }