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