(bug 30192) Thumbnails of archived images don't get deleted. Patch by Russ and Sam...
[lhc/web/wiklou.git] / includes / filerepo / LocalRepo.php
1 <?php
2 /**
3 * Local repository that stores files in the local filesystem and registers them
4 * in the wiki's own database.
5 *
6 * @file
7 * @ingroup FileRepo
8 */
9
10 /**
11 * A repository that stores files in the local filesystem and registers them
12 * in the wiki's own database. This is the most commonly used repository class.
13 * @ingroup FileRepo
14 */
15 class LocalRepo extends FSRepo {
16 var $fileFactory = array( 'LocalFile', 'newFromTitle' );
17 var $fileFactoryKey = array( 'LocalFile', 'newFromKey' );
18 var $oldFileFactory = array( 'OldLocalFile', 'newFromTitle' );
19 var $oldFileFactoryKey = array( 'OldLocalFile', 'newFromKey' );
20 var $fileFromRowFactory = array( 'LocalFile', 'newFromRow' );
21 var $oldFileFromRowFactory = array( 'OldLocalFile', 'newFromRow' );
22
23 /**
24 * @throws MWException
25 * @param $row
26 * @return File
27 */
28 function newFileFromRow( $row ) {
29 if ( isset( $row->img_name ) ) {
30 return call_user_func( $this->fileFromRowFactory, $row, $this );
31 } elseif ( isset( $row->oi_name ) ) {
32 return call_user_func( $this->oldFileFromRowFactory, $row, $this );
33 } else {
34 throw new MWException( __METHOD__.': invalid row' );
35 }
36 }
37
38 /**
39 * @param $title
40 * @param $archiveName
41 * @return OldLocalFile
42 */
43 function newFromArchiveName( $title, $archiveName ) {
44 return OldLocalFile::newFromArchiveName( $title, $this, $archiveName );
45 }
46
47 /**
48 * Delete files in the deleted directory if they are not referenced in the
49 * filearchive table. This needs to be done in the repo because it needs to
50 * interleave database locks with file operations, which is potentially a
51 * remote operation.
52 *
53 * @param $storageKeys array
54 *
55 * @return FileRepoStatus
56 */
57 function cleanupDeletedBatch( $storageKeys ) {
58 $root = $this->getZonePath( 'deleted' );
59 $dbw = $this->getMasterDB();
60 $status = $this->newGood();
61 $storageKeys = array_unique( $storageKeys );
62 foreach ( $storageKeys as $key ) {
63 $hashPath = $this->getDeletedHashPath( $key );
64 $path = "$root/$hashPath$key";
65 $dbw->begin();
66 $inuse = $dbw->selectField( 'filearchive', '1',
67 array( 'fa_storage_group' => 'deleted', 'fa_storage_key' => $key ),
68 __METHOD__, array( 'FOR UPDATE' ) );
69 if( !$inuse ) {
70 $sha1 = self::getHashFromKey( $key );
71 $ext = substr( $key, strcspn( $key, '.' ) + 1 );
72 $ext = File::normalizeExtension($ext);
73 $inuse = $dbw->selectField( 'oldimage', '1',
74 array( 'oi_sha1' => $sha1,
75 'oi_archive_name ' . $dbw->buildLike( $dbw->anyString(), ".$ext" ),
76 $dbw->bitAnd('oi_deleted', File::DELETED_FILE) => File::DELETED_FILE ),
77 __METHOD__, array( 'FOR UPDATE' ) );
78 }
79 if ( !$inuse ) {
80 wfDebug( __METHOD__ . ": deleting $key\n" );
81 wfSuppressWarnings();
82 $unlink = unlink( $path );
83 wfRestoreWarnings();
84 if ( !$unlink ) {
85 $status->error( 'undelete-cleanup-error', $path );
86 $status->failCount++;
87 }
88 } else {
89 wfDebug( __METHOD__ . ": $key still in use\n" );
90 $status->successCount++;
91 }
92 $dbw->commit();
93 }
94 return $status;
95 }
96
97 /**
98 * Gets the SHA1 hash from a storage key
99 *
100 * @param string $key
101 * @return string
102 */
103 public static function getHashFromKey( $key ) {
104 return strtok( $key, '.' );
105 }
106
107 /**
108 * Checks if there is a redirect named as $title
109 *
110 * @param $title Title of file
111 */
112 function checkRedirect( $title ) {
113 global $wgMemc;
114
115 if( is_string( $title ) ) {
116 $title = Title::newFromText( $title );
117 }
118 if( $title instanceof Title && $title->getNamespace() == NS_MEDIA ) {
119 $title = Title::makeTitle( NS_FILE, $title->getText() );
120 }
121
122 $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
123 if ( $memcKey === false ) {
124 $memcKey = $this->getLocalCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
125 $expiry = 300; // no invalidation, 5 minutes
126 } else {
127 $expiry = 86400; // has invalidation, 1 day
128 }
129 $cachedValue = $wgMemc->get( $memcKey );
130 if ( $cachedValue === ' ' || $cachedValue === '' ) {
131 // Does not exist
132 return false;
133 } elseif ( strval( $cachedValue ) !== '' ) {
134 return Title::newFromText( $cachedValue, NS_FILE );
135 } // else $cachedValue is false or null: cache miss
136
137 $id = $this->getArticleID( $title );
138 if( !$id ) {
139 $wgMemc->set( $memcKey, " ", $expiry );
140 return false;
141 }
142 $dbr = $this->getSlaveDB();
143 $row = $dbr->selectRow(
144 'redirect',
145 array( 'rd_title', 'rd_namespace' ),
146 array( 'rd_from' => $id ),
147 __METHOD__
148 );
149
150 if( $row && $row->rd_namespace == NS_FILE ) {
151 $targetTitle = Title::makeTitle( $row->rd_namespace, $row->rd_title );
152 $wgMemc->set( $memcKey, $targetTitle->getDBkey(), $expiry );
153 return $targetTitle;
154 } else {
155 $wgMemc->set( $memcKey, '', $expiry );
156 return false;
157 }
158 }
159
160
161 /**
162 * Function link Title::getArticleID().
163 * We can't say Title object, what database it should use, so we duplicate that function here.
164 * @param $title Title
165 */
166 protected function getArticleID( $title ) {
167 if( !$title instanceof Title ) {
168 return 0;
169 }
170 $dbr = $this->getSlaveDB();
171 $id = $dbr->selectField(
172 'page', // Table
173 'page_id', //Field
174 array( //Conditions
175 'page_namespace' => $title->getNamespace(),
176 'page_title' => $title->getDBkey(),
177 ),
178 __METHOD__ //Function name
179 );
180 return $id;
181 }
182
183 /**
184 * Get an array or iterator of file objects for files that have a given
185 * SHA-1 content hash.
186 */
187 function findBySha1( $hash ) {
188 $dbr = $this->getSlaveDB();
189 $res = $dbr->select(
190 'image',
191 LocalFile::selectFields(),
192 array( 'img_sha1' => $hash )
193 );
194
195 $result = array();
196 foreach ( $res as $row ) {
197 $result[] = $this->newFileFromRow( $row );
198 }
199 $res->free();
200
201 return $result;
202 }
203
204 /**
205 * Get a connection to the slave DB
206 */
207 function getSlaveDB() {
208 return wfGetDB( DB_SLAVE );
209 }
210
211 /**
212 * Get a connection to the master DB
213 */
214 function getMasterDB() {
215 return wfGetDB( DB_MASTER );
216 }
217
218 /**
219 * Get a key on the primary cache for this repository.
220 * Returns false if the repository's cache is not accessible at this site.
221 * The parameters are the parts of the key, as for wfMemcKey().
222 */
223 function getSharedCacheKey( /*...*/ ) {
224 $args = func_get_args();
225 return call_user_func_array( 'wfMemcKey', $args );
226 }
227
228 /**
229 * Invalidates image redirect cache related to that image
230 *
231 * @param $title Title of page
232 */
233 function invalidateImageRedirect( $title ) {
234 global $wgMemc;
235 $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
236 if ( $memcKey ) {
237 $wgMemc->delete( $memcKey );
238 }
239 }
240 }
241