Make Skin::formatDebugHTML()'s formatting work when memory usage is greather that 10M
[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 // Check for usage in deleted/hidden files and pre-emptively
67 // lock the key to avoid any future use until we are finished.
68 $deleted = $this->deletedFileHasKey( $key, 'lock' );
69 $hidden = $this->hiddenFileHasKey( $key, 'lock' );
70 if ( !$deleted && !$hidden ) { // not in use now
71 wfDebug( __METHOD__ . ": deleting $key\n" );
72 wfSuppressWarnings();
73 $unlink = unlink( $path );
74 wfRestoreWarnings();
75 if ( !$unlink ) {
76 $status->error( 'undelete-cleanup-error', $path );
77 $status->failCount++;
78 }
79 } else {
80 wfDebug( __METHOD__ . ": $key still in use\n" );
81 $status->successCount++;
82 }
83 $dbw->commit();
84 }
85 return $status;
86 }
87
88 /**
89 * Check if a deleted (filearchive) file has this sha1 key
90 * @param $key String File storage key (base-36 sha1 key with file extension)
91 * @param $lock String|null Use "lock" to lock the row via FOR UPDATE
92 * @return bool File with this key is in use
93 */
94 protected function deletedFileHasKey( $key, $lock = null ) {
95 $options = ( $lock === 'lock' ) ? array( 'FOR UPDATE' ) : array();
96
97 $dbw = $this->getMasterDB();
98 return (bool)$dbw->selectField( 'filearchive', '1',
99 array( 'fa_storage_group' => 'deleted', 'fa_storage_key' => $key ),
100 __METHOD__, $options
101 );
102 }
103
104 /**
105 * Check if a hidden (revision delete) file has this sha1 key
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 hiddenFileHasKey( $key, $lock = null ) {
111 $options = ( $lock === 'lock' ) ? array( 'FOR UPDATE' ) : array();
112
113 $sha1 = self::getHashFromKey( $key );
114 $ext = File::normalizeExtension( substr( $key, strcspn( $key, '.' ) + 1 ) );
115
116 $dbw = $this->getMasterDB();
117 return (bool)$dbw->selectField( 'oldimage', '1',
118 array( 'oi_sha1' => $sha1,
119 'oi_archive_name ' . $dbw->buildLike( $dbw->anyString(), ".$ext" ),
120 $dbw->bitAnd( 'oi_deleted', File::DELETED_FILE ) => File::DELETED_FILE ),
121 __METHOD__, array( 'FOR UPDATE' )
122 );
123 }
124
125 /**
126 * Gets the SHA1 hash from a storage key
127 *
128 * @param string $key
129 * @return string
130 */
131 public static function getHashFromKey( $key ) {
132 return strtok( $key, '.' );
133 }
134
135 /**
136 * Checks if there is a redirect named as $title
137 *
138 * @param $title Title of file
139 * @return bool
140 */
141 function checkRedirect( Title $title ) {
142 global $wgMemc;
143
144 $title = File::normalizeTitle( $title, 'exception' );
145
146 $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
147 if ( $memcKey === false ) {
148 $memcKey = $this->getLocalCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
149 $expiry = 300; // no invalidation, 5 minutes
150 } else {
151 $expiry = 86400; // has invalidation, 1 day
152 }
153 $cachedValue = $wgMemc->get( $memcKey );
154 if ( $cachedValue === ' ' || $cachedValue === '' ) {
155 // Does not exist
156 return false;
157 } elseif ( strval( $cachedValue ) !== '' ) {
158 return Title::newFromText( $cachedValue, NS_FILE );
159 } // else $cachedValue is false or null: cache miss
160
161 $id = $this->getArticleID( $title );
162 if( !$id ) {
163 $wgMemc->set( $memcKey, " ", $expiry );
164 return false;
165 }
166 $dbr = $this->getSlaveDB();
167 $row = $dbr->selectRow(
168 'redirect',
169 array( 'rd_title', 'rd_namespace' ),
170 array( 'rd_from' => $id ),
171 __METHOD__
172 );
173
174 if( $row && $row->rd_namespace == NS_FILE ) {
175 $targetTitle = Title::makeTitle( $row->rd_namespace, $row->rd_title );
176 $wgMemc->set( $memcKey, $targetTitle->getDBkey(), $expiry );
177 return $targetTitle;
178 } else {
179 $wgMemc->set( $memcKey, '', $expiry );
180 return false;
181 }
182 }
183
184
185 /**
186 * Function link Title::getArticleID().
187 * We can't say Title object, what database it should use, so we duplicate that function here.
188 * @param $title Title
189 */
190 protected function getArticleID( $title ) {
191 if( !$title instanceof Title ) {
192 return 0;
193 }
194 $dbr = $this->getSlaveDB();
195 $id = $dbr->selectField(
196 'page', // Table
197 'page_id', //Field
198 array( //Conditions
199 'page_namespace' => $title->getNamespace(),
200 'page_title' => $title->getDBkey(),
201 ),
202 __METHOD__ //Function name
203 );
204 return $id;
205 }
206
207 /**
208 * Get an array or iterator of file objects for files that have a given
209 * SHA-1 content hash.
210 * @return Array
211 */
212 function findBySha1( $hash ) {
213 $dbr = $this->getSlaveDB();
214 $res = $dbr->select(
215 'image',
216 LocalFile::selectFields(),
217 array( 'img_sha1' => $hash )
218 );
219
220 $result = array();
221 foreach ( $res as $row ) {
222 $result[] = $this->newFileFromRow( $row );
223 }
224 $res->free();
225
226 return $result;
227 }
228
229 /**
230 * Get a connection to the slave DB
231 */
232 function getSlaveDB() {
233 return wfGetDB( DB_SLAVE );
234 }
235
236 /**
237 * Get a connection to the master DB
238 */
239 function getMasterDB() {
240 return wfGetDB( DB_MASTER );
241 }
242
243 /**
244 * Get a key on the primary cache for this repository.
245 * Returns false if the repository's cache is not accessible at this site.
246 * The parameters are the parts of the key, as for wfMemcKey().
247 * @return string
248 */
249 function getSharedCacheKey( /*...*/ ) {
250 $args = func_get_args();
251 return call_user_func_array( 'wfMemcKey', $args );
252 }
253
254 /**
255 * Invalidates image redirect cache related to that image
256 *
257 * @param $title Title of page
258 * @return void
259 */
260 function invalidateImageRedirect( Title $title ) {
261 global $wgMemc;
262 $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
263 if ( $memcKey ) {
264 $wgMemc->delete( $memcKey );
265 }
266 }
267 }
268