(bug 28348) Don't call wfTimestamp() twice really fast in recordUpload2() and expect...
[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 function newFromArchiveName( $title, $archiveName ) {
39 return OldLocalFile::newFromArchiveName( $title, $this, $archiveName );
40 }
41
42 /**
43 * Delete files in the deleted directory if they are not referenced in the
44 * filearchive table. This needs to be done in the repo because it needs to
45 * interleave database locks with file operations, which is potentially a
46 * remote operation.
47 * @return FileRepoStatus
48 */
49 function cleanupDeletedBatch( $storageKeys ) {
50 $root = $this->getZonePath( 'deleted' );
51 $dbw = $this->getMasterDB();
52 $status = $this->newGood();
53 $storageKeys = array_unique($storageKeys);
54 foreach ( $storageKeys as $key ) {
55 $hashPath = $this->getDeletedHashPath( $key );
56 $path = "$root/$hashPath$key";
57 $dbw->begin();
58 $inuse = $dbw->selectField( 'filearchive', '1',
59 array( 'fa_storage_group' => 'deleted', 'fa_storage_key' => $key ),
60 __METHOD__, array( 'FOR UPDATE' ) );
61 if( !$inuse ) {
62 $sha1 = self::getHashFromKey( $key );
63 $ext = substr( $key, strcspn( $key, '.' ) + 1 );
64 $ext = File::normalizeExtension($ext);
65 $inuse = $dbw->selectField( 'oldimage', '1',
66 array( 'oi_sha1' => $sha1,
67 'oi_archive_name ' . $dbw->buildLike( $dbw->anyString(), ".$ext" ),
68 $dbw->bitAnd('oi_deleted', File::DELETED_FILE) => File::DELETED_FILE ),
69 __METHOD__, array( 'FOR UPDATE' ) );
70 }
71 if ( !$inuse ) {
72 wfDebug( __METHOD__ . ": deleting $key\n" );
73 if ( !@unlink( $path ) ) {
74 $status->error( 'undelete-cleanup-error', $path );
75 $status->failCount++;
76 }
77 } else {
78 wfDebug( __METHOD__ . ": $key still in use\n" );
79 $status->successCount++;
80 }
81 $dbw->commit();
82 }
83 return $status;
84 }
85
86 /**
87 * Gets the SHA1 hash from a storage key
88 *
89 * @static
90 * @param string $key
91 * @return string
92 */
93 public static function getHashFromKey( $key ) {
94 return strtok( $key, '.' );
95 }
96
97 /**
98 * Checks if there is a redirect named as $title
99 *
100 * @param $title Title of file
101 */
102 function checkRedirect( $title ) {
103 global $wgMemc;
104
105 if( is_string( $title ) ) {
106 $title = Title::newFromText( $title );
107 }
108 if( $title instanceof Title && $title->getNamespace() == NS_MEDIA ) {
109 $title = Title::makeTitle( NS_FILE, $title->getText() );
110 }
111
112 $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
113 if ( $memcKey === false ) {
114 $memcKey = $this->getLocalCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
115 $expiry = 300; // no invalidation, 5 minutes
116 } else {
117 $expiry = 86400; // has invalidation, 1 day
118 }
119 $cachedValue = $wgMemc->get( $memcKey );
120 if ( $cachedValue === ' ' || $cachedValue === '' ) {
121 // Does not exist
122 return false;
123 } elseif ( strval( $cachedValue ) !== '' ) {
124 return Title::newFromText( $cachedValue, NS_FILE );
125 } // else $cachedValue is false or null: cache miss
126
127 $id = $this->getArticleID( $title );
128 if( !$id ) {
129 $wgMemc->set( $memcKey, " ", $expiry );
130 return false;
131 }
132 $dbr = $this->getSlaveDB();
133 $row = $dbr->selectRow(
134 'redirect',
135 array( 'rd_title', 'rd_namespace' ),
136 array( 'rd_from' => $id ),
137 __METHOD__
138 );
139
140 if( $row && $row->rd_namespace == NS_FILE ) {
141 $targetTitle = Title::makeTitle( $row->rd_namespace, $row->rd_title );
142 $wgMemc->set( $memcKey, $targetTitle->getDBkey(), $expiry );
143 return $targetTitle;
144 } else {
145 $wgMemc->set( $memcKey, '', $expiry );
146 return false;
147 }
148 }
149
150
151 /**
152 * Function link Title::getArticleID().
153 * We can't say Title object, what database it should use, so we duplicate that function here.
154 * @param $title Title
155 */
156 protected function getArticleID( $title ) {
157 if( !$title instanceof Title ) {
158 return 0;
159 }
160 $dbr = $this->getSlaveDB();
161 $id = $dbr->selectField(
162 'page', // Table
163 'page_id', //Field
164 array( //Conditions
165 'page_namespace' => $title->getNamespace(),
166 'page_title' => $title->getDBkey(),
167 ),
168 __METHOD__ //Function name
169 );
170 return $id;
171 }
172
173 /**
174 * Get an array or iterator of file objects for files that have a given
175 * SHA-1 content hash.
176 */
177 function findBySha1( $hash ) {
178 $dbr = $this->getSlaveDB();
179 $res = $dbr->select(
180 'image',
181 LocalFile::selectFields(),
182 array( 'img_sha1' => $hash )
183 );
184
185 $result = array();
186 foreach ( $res as $row ) {
187 $result[] = $this->newFileFromRow( $row );
188 }
189 $res->free();
190
191 return $result;
192 }
193
194 /**
195 * Get a connection to the slave DB
196 */
197 function getSlaveDB() {
198 return wfGetDB( DB_SLAVE );
199 }
200
201 /**
202 * Get a connection to the master DB
203 */
204 function getMasterDB() {
205 return wfGetDB( DB_MASTER );
206 }
207
208 /**
209 * Get a key on the primary cache for this repository.
210 * Returns false if the repository's cache is not accessible at this site.
211 * The parameters are the parts of the key, as for wfMemcKey().
212 */
213 function getSharedCacheKey( /*...*/ ) {
214 $args = func_get_args();
215 return call_user_func_array( 'wfMemcKey', $args );
216 }
217
218 /**
219 * Invalidates image redirect cache related to that image
220 *
221 * @param $title Title of page
222 */
223 function invalidateImageRedirect( $title ) {
224 global $wgMemc;
225 $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
226 if ( $memcKey ) {
227 $wgMemc->delete( $memcKey );
228 }
229 }
230 }
231