More rev_deleted merging
[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 $storageKeys = array_unique($storageKeys);
44 foreach ( $storageKeys as $key ) {
45 $hashPath = $this->getDeletedHashPath( $key );
46 $path = "$root/$hashPath$key";
47 $dbw->begin();
48 $inuse = $dbw->selectField( 'filearchive', '1',
49 array( 'fa_storage_group' => 'deleted', 'fa_storage_key' => $key ),
50 __METHOD__, array( 'FOR UPDATE' ) );
51 if( !$inuse ) {
52 $sha1 = substr( $key, 0, strcspn( $key, '.' ) );
53 $inuse = $dbw->selectField( 'oldimage', '1',
54 array( 'oi_sha1' => $sha1,
55 'oi_deleted & '.File::DELETED_FILE => File::DELETED_FILE ),
56 __METHOD__, array( 'FOR UPDATE' ) );
57 }
58 if ( !$inuse ) {
59 wfDebug( __METHOD__ . ": deleting $key\n" );
60 if ( !@unlink( $path ) ) {
61 $status->error( 'undelete-cleanup-error', $path );
62 $status->failCount++;
63 }
64 } else {
65 wfDebug( __METHOD__ . ": $key still in use\n" );
66 $status->successCount++;
67 }
68 $dbw->commit();
69 }
70 return $status;
71 }
72
73 /**
74 * Function link Title::getArticleID().
75 * We can't say Title object, what database it should use, so we duplicate that function here.
76 */
77 private function getArticleID( $title ) {
78 if( !$title instanceof Title ) {
79 return 0;
80 }
81 $dbr = $this->getSlaveDB();
82 $id = $dbr->selectField(
83 'page', // Table
84 'page_id', //Field
85 array( //Conditions
86 'page_namespace' => $title->getNamespace(),
87 'page_title' => $title->getDbKey(),
88 ),
89 __METHOD__ //Function name
90 );
91 return $id;
92 }
93
94 function checkRedirect( $title ) {
95 global $wgFileRedirects;
96 if( !$wgFileRedirects ) {
97 return false;
98 }
99
100 if( $title instanceof Title && $title->getNamespace() == NS_MEDIA ) {
101 $title = Title::makeTitle( NS_IMAGE, $title->getText() );
102 }
103
104 $id = $this->getArticleID( $title );
105 if( !$id ) {
106 return false;
107 }
108 $dbr = $this->getSlaveDB();
109 $row = $dbr->selectRow(
110 'redirect',
111 array( 'rd_title', 'rd_namespace' ),
112 array( 'rd_from' => $id ),
113 __METHOD__
114 );
115 if( !$row ) {
116 return false;
117 }
118 return Title::makeTitle( $row->rd_namespace, $row->rd_title );
119 }
120 }