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