Merge "(bug 19195) Make user IDs more readily available with the API"
[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 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 * @ingroup FileRepo
23 */
24
25 /**
26 * A repository that stores files in the local filesystem and registers them
27 * in the wiki's own database. This is the most commonly used repository class.
28 *
29 * @ingroup FileRepo
30 */
31 class LocalRepo extends FileRepo {
32 var $fileFactory = array( 'LocalFile' , 'newFromTitle' );
33 var $fileFactoryKey = array( 'LocalFile' , 'newFromKey' );
34 var $fileFromRowFactory = array( 'LocalFile' , 'newFromRow' );
35 var $oldFileFactory = array( 'OldLocalFile', 'newFromTitle' );
36 var $oldFileFactoryKey = array( 'OldLocalFile', 'newFromKey' );
37 var $oldFileFromRowFactory = array( 'OldLocalFile', 'newFromRow' );
38
39 /**
40 * @throws MWException
41 * @param $row
42 * @return File
43 */
44 function newFileFromRow( $row ) {
45 if ( isset( $row->img_name ) ) {
46 return call_user_func( $this->fileFromRowFactory, $row, $this );
47 } elseif ( isset( $row->oi_name ) ) {
48 return call_user_func( $this->oldFileFromRowFactory, $row, $this );
49 } else {
50 throw new MWException( __METHOD__.': invalid row' );
51 }
52 }
53
54 /**
55 * @param $title
56 * @param $archiveName
57 * @return OldLocalFile
58 */
59 function newFromArchiveName( $title, $archiveName ) {
60 return OldLocalFile::newFromArchiveName( $title, $this, $archiveName );
61 }
62
63 /**
64 * Delete files in the deleted directory if they are not referenced in the
65 * filearchive table. This needs to be done in the repo because it needs to
66 * interleave database locks with file operations, which is potentially a
67 * remote operation.
68 *
69 * @param $storageKeys array
70 *
71 * @return FileRepoStatus
72 */
73 function cleanupDeletedBatch( array $storageKeys ) {
74 $backend = $this->backend; // convenience
75 $root = $this->getZonePath( 'deleted' );
76 $dbw = $this->getMasterDB();
77 $status = $this->newGood();
78 $storageKeys = array_unique( $storageKeys );
79 foreach ( $storageKeys as $key ) {
80 $hashPath = $this->getDeletedHashPath( $key );
81 $path = "$root/$hashPath$key";
82 $dbw->begin( __METHOD__ );
83 // Check for usage in deleted/hidden files and pre-emptively
84 // lock the key to avoid any future use until we are finished.
85 $deleted = $this->deletedFileHasKey( $key, 'lock' );
86 $hidden = $this->hiddenFileHasKey( $key, 'lock' );
87 if ( !$deleted && !$hidden ) { // not in use now
88 wfDebug( __METHOD__ . ": deleting $key\n" );
89 $op = array( 'op' => 'delete', 'src' => $path );
90 if ( !$backend->doOperation( $op )->isOK() ) {
91 $status->error( 'undelete-cleanup-error', $path );
92 $status->failCount++;
93 }
94 } else {
95 wfDebug( __METHOD__ . ": $key still in use\n" );
96 $status->successCount++;
97 }
98 $dbw->commit( __METHOD__ );
99 }
100 return $status;
101 }
102
103 /**
104 * Check if a deleted (filearchive) file has this sha1 key
105 *
106 * @param $key String File storage key (base-36 sha1 key with file extension)
107 * @param $lock String|null Use "lock" to lock the row via FOR UPDATE
108 * @return bool File with this key is in use
109 */
110 protected function deletedFileHasKey( $key, $lock = null ) {
111 $options = ( $lock === 'lock' ) ? array( 'FOR UPDATE' ) : array();
112
113 $dbw = $this->getMasterDB();
114 return (bool)$dbw->selectField( 'filearchive', '1',
115 array( 'fa_storage_group' => 'deleted', 'fa_storage_key' => $key ),
116 __METHOD__, $options
117 );
118 }
119
120 /**
121 * Check if a hidden (revision delete) file has this sha1 key
122 *
123 * @param $key String File storage key (base-36 sha1 key with file extension)
124 * @param $lock String|null Use "lock" to lock the row via FOR UPDATE
125 * @return bool File with this key is in use
126 */
127 protected function hiddenFileHasKey( $key, $lock = null ) {
128 $options = ( $lock === 'lock' ) ? array( 'FOR UPDATE' ) : array();
129
130 $sha1 = self::getHashFromKey( $key );
131 $ext = File::normalizeExtension( substr( $key, strcspn( $key, '.' ) + 1 ) );
132
133 $dbw = $this->getMasterDB();
134 return (bool)$dbw->selectField( 'oldimage', '1',
135 array( 'oi_sha1' => $sha1,
136 'oi_archive_name ' . $dbw->buildLike( $dbw->anyString(), ".$ext" ),
137 $dbw->bitAnd( 'oi_deleted', File::DELETED_FILE ) => File::DELETED_FILE ),
138 __METHOD__, $options
139 );
140 }
141
142 /**
143 * Gets the SHA1 hash from a storage key
144 *
145 * @param string $key
146 * @return string
147 */
148 public static function getHashFromKey( $key ) {
149 return strtok( $key, '.' );
150 }
151
152 /**
153 * Checks if there is a redirect named as $title
154 *
155 * @param $title Title of file
156 * @return bool
157 */
158 function checkRedirect( Title $title ) {
159 global $wgMemc;
160
161 $title = File::normalizeTitle( $title, 'exception' );
162
163 $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
164 if ( $memcKey === false ) {
165 $memcKey = $this->getLocalCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
166 $expiry = 300; // no invalidation, 5 minutes
167 } else {
168 $expiry = 86400; // has invalidation, 1 day
169 }
170 $cachedValue = $wgMemc->get( $memcKey );
171 if ( $cachedValue === ' ' || $cachedValue === '' ) {
172 // Does not exist
173 return false;
174 } elseif ( strval( $cachedValue ) !== '' ) {
175 return Title::newFromText( $cachedValue, NS_FILE );
176 } // else $cachedValue is false or null: cache miss
177
178 $id = $this->getArticleID( $title );
179 if( !$id ) {
180 $wgMemc->set( $memcKey, " ", $expiry );
181 return false;
182 }
183 $dbr = $this->getSlaveDB();
184 $row = $dbr->selectRow(
185 'redirect',
186 array( 'rd_title', 'rd_namespace' ),
187 array( 'rd_from' => $id ),
188 __METHOD__
189 );
190
191 if( $row && $row->rd_namespace == NS_FILE ) {
192 $targetTitle = Title::makeTitle( $row->rd_namespace, $row->rd_title );
193 $wgMemc->set( $memcKey, $targetTitle->getDBkey(), $expiry );
194 return $targetTitle;
195 } else {
196 $wgMemc->set( $memcKey, '', $expiry );
197 return false;
198 }
199 }
200
201
202 /**
203 * Function link Title::getArticleID().
204 * We can't say Title object, what database it should use, so we duplicate that function here.
205 *
206 * @param $title Title
207 * @return bool|int|mixed
208 */
209 protected function getArticleID( $title ) {
210 if( !$title instanceof Title ) {
211 return 0;
212 }
213 $dbr = $this->getSlaveDB();
214 $id = $dbr->selectField(
215 'page', // Table
216 'page_id', //Field
217 array( //Conditions
218 'page_namespace' => $title->getNamespace(),
219 'page_title' => $title->getDBkey(),
220 ),
221 __METHOD__ //Function name
222 );
223 return $id;
224 }
225
226 /**
227 * Get an array or iterator of file objects for files that have a given
228 * SHA-1 content hash.
229 *
230 * @param $hash String a sha1 hash to look for
231 * @return Array
232 */
233 function findBySha1( $hash ) {
234 $dbr = $this->getSlaveDB();
235 $res = $dbr->select(
236 'image',
237 LocalFile::selectFields(),
238 array( 'img_sha1' => $hash )
239 );
240
241 $result = array();
242 foreach ( $res as $row ) {
243 $result[] = $this->newFileFromRow( $row );
244 }
245 $res->free();
246
247 return $result;
248 }
249
250 /**
251 * Get a connection to the slave DB
252 * @return DatabaseBase
253 */
254 function getSlaveDB() {
255 return wfGetDB( DB_SLAVE );
256 }
257
258 /**
259 * Get a connection to the master DB
260 * @return DatabaseBase
261 */
262 function getMasterDB() {
263 return wfGetDB( DB_MASTER );
264 }
265
266 /**
267 * Get a key on the primary cache for this repository.
268 * Returns false if the repository's cache is not accessible at this site.
269 * The parameters are the parts of the key, as for wfMemcKey().
270 *
271 * @return string
272 */
273 function getSharedCacheKey( /*...*/ ) {
274 $args = func_get_args();
275 return call_user_func_array( 'wfMemcKey', $args );
276 }
277
278 /**
279 * Invalidates image redirect cache related to that image
280 *
281 * @param $title Title of page
282 * @return void
283 */
284 function invalidateImageRedirect( Title $title ) {
285 global $wgMemc;
286 $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
287 if ( $memcKey ) {
288 $wgMemc->delete( $memcKey );
289 }
290 }
291 }
292