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