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