b096ce0cf0573279849c6f4ebe668133c86e3c59
[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 __construct( $info ) {
14 parent::__construct( $info );
15
16 # Initialize simpleCleanPairs, to make errors less misleading
17 global $IP;
18 $this->simpleCleanPairs = array(
19 $this->directory => '$wgUploadDirectory',
20 wfTempDir() => '{wfTempDir()}',
21 $IP => '$IP',
22 );
23 if ( $this->deletedDir ) {
24 $this->simpleCleanPairs[$this->deletedDir] = '{$wgFileStore[\'deleted\'][\'directory\']}';
25 }
26 }
27
28 function getSlaveDB() {
29 return wfGetDB( DB_SLAVE );
30 }
31
32 function getMasterDB() {
33 return wfGetDB( DB_MASTER );
34 }
35
36 function getMemcKey( $key ) {
37 return wfWikiID( $this->getSlaveDB() ) . ":{$key}";
38 }
39
40 function newFileFromRow( $row ) {
41 if ( isset( $row->img_name ) ) {
42 return call_user_func( $this->fileFromRowFactory, $row, $this );
43 } elseif ( isset( $row->oi_name ) ) {
44 return call_user_func( $this->oldFileFromRowFactory, $row, $this );
45 } else {
46 throw new MWException( __METHOD__.': invalid row' );
47 }
48 }
49
50 function newFromArchiveName( $title, $archiveName ) {
51 return OldLocalFile::newFromArchiveName( $title, $this, $archiveName );
52 }
53
54 /**
55 * Delete files in the deleted directory if they are not referenced in the
56 * filearchive table. This needs to be done in the repo because it needs to
57 * interleave database locks with file operations, which is potentially a
58 * remote operation.
59 * @return FileRepoStatus
60 */
61 function cleanupDeletedBatch( $storageKeys ) {
62 $root = $this->getZonePath( 'deleted' );
63 $dbw = $this->getMasterDB();
64 $status = $this->newGood();
65 $storageKeys = array_unique($storageKeys);
66 foreach ( $storageKeys as $key ) {
67 $hashPath = $this->getDeletedHashPath( $key );
68 $path = "$root/$hashPath$key";
69 $dbw->begin();
70 $inuse = $dbw->selectField( 'filearchive', '1',
71 array( 'fa_storage_group' => 'deleted', 'fa_storage_key' => $key ),
72 __METHOD__, array( 'FOR UPDATE' ) );
73 if( !$inuse ) {
74 $sha1 = substr( $key, 0, strcspn( $key, '.' ) );
75 $ext = substr( $key, strcspn($key,'.') + 1 );
76 $ext = File::normalizeExtension($ext);
77 $inuse = $dbw->selectField( 'oldimage', '1',
78 array( 'oi_sha1' => $sha1,
79 "oi_archive_name LIKE '%.{$ext}'",
80 'oi_deleted & '.File::DELETED_FILE => File::DELETED_FILE ),
81 __METHOD__, array( 'FOR UPDATE' ) );
82 }
83 if ( !$inuse ) {
84 wfDebug( __METHOD__ . ": deleting $key\n" );
85 if ( !@unlink( $path ) ) {
86 $status->error( 'undelete-cleanup-error', $path );
87 $status->failCount++;
88 }
89 } else {
90 wfDebug( __METHOD__ . ": $key still in use\n" );
91 $status->successCount++;
92 }
93 $dbw->commit();
94 }
95 return $status;
96 }
97
98 /**
99 * Function link Title::getArticleID().
100 * We can't say Title object, what database it should use, so we duplicate that function here.
101 */
102 protected function getArticleID( $title ) {
103 if( !$title instanceof Title ) {
104 return 0;
105 }
106 $dbr = $this->getSlaveDB();
107 $id = $dbr->selectField(
108 'page', // Table
109 'page_id', //Field
110 array( //Conditions
111 'page_namespace' => $title->getNamespace(),
112 'page_title' => $title->getDbKey(),
113 ),
114 __METHOD__ //Function name
115 );
116 return $id;
117 }
118
119 function checkRedirect( $title ) {
120 global $wgMemc;
121
122 if( is_string( $title ) ) {
123 $title = Title::newFromTitle( $title );
124 }
125 if( $title instanceof Title && $title->getNamespace() == NS_MEDIA ) {
126 $title = Title::makeTitle( NS_IMAGE, $title->getText() );
127 }
128
129 $memcKey = $this->getMemcKey( "image_redirect:" . md5( $title->getPrefixedDBkey() ) );
130 $cachedValue = $wgMemc->get( $memcKey );
131 if( $cachedValue ) {
132 return Title::newFromDbKey( $cachedValue );
133 } elseif( $cachedValue == ' ' ) { # FIXME: ugly hack, but BagOStuff caching seems to be weird and return false if !cachedValue, not only if it doesn't exist
134 return false;
135 }
136
137 $id = $this->getArticleID( $title );
138 if( !$id ) {
139 $wgMemc->set( $memcKey, " ", 9000 );
140 return false;
141 }
142 $dbr = $this->getSlaveDB();
143 $row = $dbr->selectRow(
144 'redirect',
145 array( 'rd_title', 'rd_namespace' ),
146 array( 'rd_from' => $id ),
147 __METHOD__
148 );
149
150 if( $row ) $targetTitle = Title::makeTitle( $row->rd_namespace, $row->rd_title );
151 $wgMemc->set( $memcKey, ($row ? $targetTitle->getPrefixedDBkey() : " "), 9000 );
152 if( !$row ) {
153 return false;
154 }
155 return $targetTitle;
156 }
157
158 function invalidateImageRedirect( $title ) {
159 global $wgMemc;
160 $memcKey = $this->getMemcKey( "image_redirect:" . md5( $title->getPrefixedDBkey() ) );
161 $wgMemc->delete( $memcKey );
162 }
163
164 function findBySha1( $hash ) {
165 $dbr = $this->getSlaveDB();
166 $res = $dbr->select(
167 'image',
168 LocalFile::selectFields(),
169 array( 'img_sha1' => $hash )
170 );
171
172 $result = array();
173 while ( $row = $res->fetchObject() )
174 $result[] = $this->newFileFromRow( $row );
175 $res->free();
176 return $result;
177 }
178
179 /*
180 * Find many files using one query
181 */
182 function findFiles( $titles, $flags ) {
183 // FIXME: Comply with $flags
184 // FIXME: Only accepts a $titles array where the keys are the sanitized
185 // file names.
186
187 if ( count( $titles ) == 0 ) return array();
188
189 $dbr = $this->getSlaveDB();
190 $res = $dbr->select(
191 'image',
192 LocalFile::selectFields(),
193 array( 'img_name' => array_keys( $titles ) )
194 );
195
196 $result = array();
197 while ( $row = $res->fetchObject() ) {
198 $result[$row->img_name] = $this->newFileFromRow( $row );
199 }
200 $res->free();
201 return $result;
202 }
203 }