* Introduced FileRepoStatus -- result class for file repo operations.
[lhc/web/wiklou.git] / includes / filerepo / LocalRepo.php
1 <?php
2 /**
3 * A repository that stores files in the local filesystem and registers them
4 * in the wiki's own database. This is the most commonly used repository class.
5 */
6 class LocalRepo extends FSRepo {
7 var $fileFactory = array( 'LocalFile', 'newFromTitle' );
8 var $oldFileFactory = array( 'OldLocalFile', 'newFromTitle' );
9
10 function getSlaveDB() {
11 return wfGetDB( DB_SLAVE );
12 }
13
14 function getMasterDB() {
15 return wfGetDB( DB_MASTER );
16 }
17
18 function newFileFromRow( $row ) {
19 if ( isset( $row->img_name ) ) {
20 return LocalFile::newFromRow( $row, $this );
21 } elseif ( isset( $row->oi_name ) ) {
22 return OldLocalFile::newFromRow( $row, $this );
23 } else {
24 throw new MWException( __METHOD__.': invalid row' );
25 }
26 }
27
28 function newFromArchiveName( $title, $archiveName ) {
29 return OldLocalFile::newFromArchiveName( $title, $this, $archiveName );
30 }
31
32 /**
33 * Delete files in the deleted directory if they are not referenced in the
34 * filearchive table. This needs to be done in the repo because it needs to
35 * interleave database locks with file operations, which is potentially a
36 * remote operation.
37 * @return FileRepoStatus
38 */
39 function cleanupDeletedBatch( $storageKeys ) {
40 $root = $this->getZonePath( 'deleted' );
41 $dbw = $this->getMasterDB();
42 $status = $this->newGood();
43 foreach ( $storageKeys as $key ) {
44 $hashPath = $this->getDeletedHashPath( $key );
45 $path = "$root/$hashPath$key";
46 $dbw->begin();
47 $inuse = $dbw->select( 'filearchive', '1',
48 array( 'fa_storage_group' => 'deleted', 'fa_storage_key' => $key ),
49 __METHOD__, array( 'FOR UPDATE' ) );
50 if ( !$inuse && !unlink( $path ) ) {
51 $status->error( 'undelete-cleanup-error', $path );
52 $status->failCount++;
53 } else {
54 $status->successCount++;
55 }
56 $dbw->commit();
57 }
58 return $status;
59 }
60 }