* Fix db->makeList() spacing
[mediawiki.git] / includes / filerepo / LocalRepo.php
blob26651e6aba5aa5f538bea632bf553d3b99af8d91
1 <?php
2 /**
3 * Local repository that stores files in the local filesystem and registers them
4 * in the wiki's own database.
6 * @file
7 * @ingroup FileRepo
8 */
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
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' );
23 /**
24 * @throws MWException
25 * @param $row
26 * @return File
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' );
38 function newFromArchiveName( $title, $archiveName ) {
39 return OldLocalFile::newFromArchiveName( $title, $this, $archiveName );
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
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' ) );
71 if ( !$inuse ) {
72 wfDebug( __METHOD__ . ": deleting $key\n" );
73 if ( !@unlink( $path ) ) {
74 $status->error( 'undelete-cleanup-error', $path );
75 $status->failCount++;
77 } else {
78 wfDebug( __METHOD__ . ": $key still in use\n" );
79 $status->successCount++;
81 $dbw->commit();
83 return $status;
86 /**
87 * Gets the SHA1 hash from a storage key
89 * @param string $key
90 * @return string
92 public static function getHashFromKey( $key ) {
93 return strtok( $key, '.' );
96 /**
97 * Checks if there is a redirect named as $title
99 * @param $title Title of file
101 function checkRedirect( $title ) {
102 global $wgMemc;
104 if( is_string( $title ) ) {
105 $title = Title::newFromText( $title );
107 if( $title instanceof Title && $title->getNamespace() == NS_MEDIA ) {
108 $title = Title::makeTitle( NS_FILE, $title->getText() );
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
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
126 $id = $this->getArticleID( $title );
127 if( !$id ) {
128 $wgMemc->set( $memcKey, " ", $expiry );
129 return false;
131 $dbr = $this->getSlaveDB();
132 $row = $dbr->selectRow(
133 'redirect',
134 array( 'rd_title', 'rd_namespace' ),
135 array( 'rd_from' => $id ),
136 __METHOD__
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;
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
155 protected function getArticleID( $title ) {
156 if( !$title instanceof Title ) {
157 return 0;
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(),
167 __METHOD__ //Function name
169 return $id;
173 * Get an array or iterator of file objects for files that have a given
174 * SHA-1 content hash.
176 function findBySha1( $hash ) {
177 $dbr = $this->getSlaveDB();
178 $res = $dbr->select(
179 'image',
180 LocalFile::selectFields(),
181 array( 'img_sha1' => $hash )
184 $result = array();
185 foreach ( $res as $row ) {
186 $result[] = $this->newFileFromRow( $row );
188 $res->free();
190 return $result;
194 * Get a connection to the slave DB
196 function getSlaveDB() {
197 return wfGetDB( DB_SLAVE );
201 * Get a connection to the master DB
203 function getMasterDB() {
204 return wfGetDB( DB_MASTER );
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().
212 function getSharedCacheKey( /*...*/ ) {
213 $args = func_get_args();
214 return call_user_func_array( 'wfMemcKey', $args );
218 * Invalidates image redirect cache related to that image
220 * @param $title Title of page
222 function invalidateImageRedirect( $title ) {
223 global $wgMemc;
224 $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
225 if ( $memcKey ) {
226 $wgMemc->delete( $memcKey );