Remove @static from all over the place. That's what the static keyword is for, this...
[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 * @param string $key
90 * @return string
91 */
92 public static function getHashFromKey( $key ) {
93 return strtok( $key, '.' );
94 }
95
96 /**
97 * Checks if there is a redirect named as $title
98 *
99 * @param $title Title of file
100 */
101 function checkRedirect( $title ) {
102 global $wgMemc;
103
104 if( is_string( $title ) ) {
105 $title = Title::newFromText( $title );
106 }
107 if( $title instanceof Title && $title->getNamespace() == NS_MEDIA ) {
108 $title = Title::makeTitle( NS_FILE, $title->getText() );
109 }
110
111 $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
112 if ( $memcKey === false ) {
113 $memcKey = $this->getLocalCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
114 $expiry = 300; // no invalidation, 5 minutes
115 } else {
116 $expiry = 86400; // has invalidation, 1 day
117 }
118 $cachedValue = $wgMemc->get( $memcKey );
119 if ( $cachedValue === ' ' || $cachedValue === '' ) {
120 // Does not exist
121 return false;
122 } elseif ( strval( $cachedValue ) !== '' ) {
123 return Title::newFromText( $cachedValue, NS_FILE );
124 } // else $cachedValue is false or null: cache miss
125
126 $id = $this->getArticleID( $title );
127 if( !$id ) {
128 $wgMemc->set( $memcKey, " ", $expiry );
129 return false;
130 }
131 $dbr = $this->getSlaveDB();
132 $row = $dbr->selectRow(
133 'redirect',
134 array( 'rd_title', 'rd_namespace' ),
135 array( 'rd_from' => $id ),
136 __METHOD__
137 );
138
139 if( $row && $row->rd_namespace == NS_FILE ) {
140 $targetTitle = Title::makeTitle( $row->rd_namespace, $row->rd_title );
141 $wgMemc->set( $memcKey, $targetTitle->getDBkey(), $expiry );
142 return $targetTitle;
143 } else {
144 $wgMemc->set( $memcKey, '', $expiry );
145 return false;
146 }
147 }
148
149
150 /**
151 * Function link Title::getArticleID().
152 * We can't say Title object, what database it should use, so we duplicate that function here.
153 * @param $title Title
154 */
155 protected function getArticleID( $title ) {
156 if( !$title instanceof Title ) {
157 return 0;
158 }
159 $dbr = $this->getSlaveDB();
160 $id = $dbr->selectField(
161 'page', // Table
162 'page_id', //Field
163 array( //Conditions
164 'page_namespace' => $title->getNamespace(),
165 'page_title' => $title->getDBkey(),
166 ),
167 __METHOD__ //Function name
168 );
169 return $id;
170 }
171
172 /**
173 * Get an array or iterator of file objects for files that have a given
174 * SHA-1 content hash.
175 */
176 function findBySha1( $hash ) {
177 $dbr = $this->getSlaveDB();
178 $res = $dbr->select(
179 'image',
180 LocalFile::selectFields(),
181 array( 'img_sha1' => $hash )
182 );
183
184 $result = array();
185 foreach ( $res as $row ) {
186 $result[] = $this->newFileFromRow( $row );
187 }
188 $res->free();
189
190 return $result;
191 }
192
193 /**
194 * Get a connection to the slave DB
195 */
196 function getSlaveDB() {
197 return wfGetDB( DB_SLAVE );
198 }
199
200 /**
201 * Get a connection to the master DB
202 */
203 function getMasterDB() {
204 return wfGetDB( DB_MASTER );
205 }
206
207 /**
208 * Get a key on the primary cache for this repository.
209 * Returns false if the repository's cache is not accessible at this site.
210 * The parameters are the parts of the key, as for wfMemcKey().
211 */
212 function getSharedCacheKey( /*...*/ ) {
213 $args = func_get_args();
214 return call_user_func_array( 'wfMemcKey', $args );
215 }
216
217 /**
218 * Invalidates image redirect cache related to that image
219 *
220 * @param $title Title of page
221 */
222 function invalidateImageRedirect( $title ) {
223 global $wgMemc;
224 $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
225 if ( $memcKey ) {
226 $wgMemc->delete( $memcKey );
227 }
228 }
229 }
230