*Clean up deletion of revisions and remove some gaps
[lhc/web/wiklou.git] / includes / filerepo / OldLocalFile.php
1 <?php
2
3 /**
4 * Class to represent a file in the oldimage table
5 *
6 * @addtogroup FileRepo
7 */
8 class OldLocalFile extends LocalFile {
9 var $requestedTime, $archive_name;
10
11 const CACHE_VERSION = 1;
12 const MAX_CACHE_ROWS = 20;
13
14 static function newFromTitle( $title, $repo, $time ) {
15 return new self( $title, $repo, $time, null );
16 }
17
18 static function newFromArchiveName( $title, $repo, $archiveName ) {
19 return new self( $title, $repo, null, $archiveName );
20 }
21
22 static function newFromRow( $row, $repo ) {
23 $title = Title::makeTitle( NS_IMAGE, $row->oi_name );
24 $file = new self( $title, $repo, null, $row->oi_archive_name );
25 $file->loadFromRow( $row, 'oi_' );
26 return $file;
27 }
28
29 /**
30 * @param Title $title
31 * @param FileRepo $repo
32 * @param string $time Timestamp or null to load by archive name
33 * @param string $archiveName Archive name or null to load by timestamp
34 */
35 function __construct( $title, $repo, $time, $archiveName ) {
36 parent::__construct( $title, $repo );
37 $this->requestedTime = $time;
38 $this->archive_name = $archiveName;
39 if ( is_null( $time ) && is_null( $archiveName ) ) {
40 throw new MWException( __METHOD__.': must specify at least one of $time or $archiveName' );
41 }
42 }
43
44 function getCacheKey() {
45 $hashedName = md5($this->getName());
46 return wfMemcKey( 'oldfile', $hashedName );
47 }
48
49 function getArchiveName() {
50 if ( !isset( $this->archive_name ) ) {
51 $this->load();
52 }
53 return $this->archive_name;
54 }
55
56 function isOld() {
57 return true;
58 }
59
60 function isVisible() {
61 return $this->exists() && !$this->isDeleted(File::DELETED_FILE);
62 }
63
64 /**
65 * Try to load file metadata from memcached. Returns true on success.
66 */
67 function loadFromCache() {
68 global $wgMemc;
69 wfProfileIn( __METHOD__ );
70 $this->dataLoaded = false;
71 $key = $this->getCacheKey();
72 if ( !$key ) {
73 return false;
74 }
75 $oldImages = $wgMemc->get( $key );
76
77 if ( isset( $oldImages['version'] ) && $oldImages['version'] == self::CACHE_VERSION ) {
78 unset( $oldImages['version'] );
79 $more = isset( $oldImages['more'] );
80 unset( $oldImages['more'] );
81 $found = false;
82 if ( is_null( $this->requestedTime ) ) {
83 foreach ( $oldImages as $timestamp => $info ) {
84 if ( $info['archive_name'] == $this->archive_name ) {
85 $found = true;
86 break;
87 }
88 }
89 } else {
90 krsort( $oldImages );
91 foreach ( $oldImages as $timestamp => $info ) {
92 if ( $timestamp <= $this->requestedTime ) {
93 $found = true;
94 break;
95 }
96 }
97 }
98 if ( $found ) {
99 wfDebug( "Pulling file metadata from cache key {$key}[{$timestamp}]\n" );
100 $this->dataLoaded = true;
101 $this->fileExists = true;
102 foreach ( $info as $name => $value ) {
103 $this->$name = $value;
104 }
105 } elseif ( $more ) {
106 wfDebug( "Cache key was truncated, oldimage row might be found in the database\n" );
107 } else {
108 wfDebug( "Image did not exist at the specified time.\n" );
109 $this->fileExists = false;
110 $this->dataLoaded = true;
111 }
112 }
113
114 if ( $this->dataLoaded ) {
115 wfIncrStats( 'image_cache_hit' );
116 } else {
117 wfIncrStats( 'image_cache_miss' );
118 }
119
120 wfProfileOut( __METHOD__ );
121 return $this->dataLoaded;
122 }
123
124 function saveToCache() {
125 // If a timestamp was specified, cache the entire history of the image (up to MAX_CACHE_ROWS).
126 if ( is_null( $this->requestedTime ) ) {
127 return;
128 }
129 // This is expensive, so we only do it if $wgMemc is real
130 global $wgMemc;
131 if ( $wgMemc instanceof FakeMemcachedClient ) {
132 return;
133 }
134 $key = $this->getCacheKey();
135 if ( !$key ) {
136 return;
137 }
138 wfProfileIn( __METHOD__ );
139
140 $dbr = $this->repo->getSlaveDB();
141 $res = $dbr->select( 'oldimage', $this->getCacheFields( 'oi_' ),
142 array( 'oi_name' => $this->getName() ), __METHOD__,
143 array(
144 'LIMIT' => self::MAX_CACHE_ROWS + 1,
145 'ORDER BY' => 'oi_timestamp DESC',
146 ));
147 $cache = array( 'version' => self::CACHE_VERSION );
148 $numRows = $dbr->numRows( $res );
149 if ( $numRows > self::MAX_CACHE_ROWS ) {
150 $cache['more'] = true;
151 $numRows--;
152 }
153 for ( $i = 0; $i < $numRows; $i++ ) {
154 $row = $dbr->fetchObject( $res );
155 $decoded = $this->decodeRow( $row, 'oi_' );
156 $cache[$row->oi_timestamp] = $decoded;
157 }
158 $dbr->freeResult( $res );
159 $wgMemc->set( $key, $cache, 7*86400 /* 1 week */ );
160 wfProfileOut( __METHOD__ );
161 }
162
163 function loadFromDB() {
164 wfProfileIn( __METHOD__ );
165 $this->dataLoaded = true;
166 $dbr = $this->repo->getSlaveDB();
167 $conds = array( 'oi_name' => $this->getName() );
168 if ( is_null( $this->requestedTime ) ) {
169 $conds['oi_archive_name'] = $this->archive_name;
170 } else {
171 $conds[] = 'oi_timestamp <= ' . $dbr->addQuotes( $this->requestedTime );
172 }
173 $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
174 $conds, __METHOD__, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
175 if ( $row ) {
176 $this->loadFromRow( $row, 'oi_' );
177 } else {
178 $this->fileExists = false;
179 }
180 wfProfileOut( __METHOD__ );
181 }
182
183 function getCacheFields( $prefix = 'img_' ) {
184 $fields = parent::getCacheFields( $prefix );
185 $fields[] = $prefix . 'archive_name';
186 $fields[] = $prefix . 'deleted';
187
188 return $fields;
189 }
190
191 function getRel() {
192 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
193 }
194
195 function getUrlRel() {
196 return 'archive/' . $this->getHashPath() . urlencode( $this->getArchiveName() );
197 }
198
199 function upgradeRow() {
200 wfProfileIn( __METHOD__ );
201 $this->loadFromFile();
202
203 # Don't destroy file info of missing files
204 if ( !$this->fileExists ) {
205 wfDebug( __METHOD__.": file does not exist, aborting\n" );
206 wfProfileOut( __METHOD__ );
207 return;
208 }
209
210 $dbw = $this->repo->getMasterDB();
211 list( $major, $minor ) = self::splitMime( $this->mime );
212
213 wfDebug(__METHOD__.': upgrading '.$this->archive_name." to the current schema\n");
214 $dbw->update( 'oldimage',
215 array(
216 'oi_width' => $this->width,
217 'oi_height' => $this->height,
218 'oi_bits' => $this->bits,
219 'oi_media_type' => $this->media_type,
220 'oi_major_mime' => $major,
221 'oi_minor_mime' => $minor,
222 'oi_metadata' => $this->metadata,
223 'oi_sha1' => $this->sha1,
224 ), array(
225 'oi_name' => $this->getName(),
226 'oi_archive_name' => $this->archive_name ),
227 __METHOD__
228 );
229 wfProfileOut( __METHOD__ );
230 }
231
232 /**
233 * int $field one of DELETED_* bitfield constants
234 * for file or revision rows
235 * @return bool
236 */
237 function isDeleted( $field ) {
238 return ($this->deleted & $field) == $field;
239 }
240
241 /**
242 * Determine if the current user is allowed to view a particular
243 * field of this FileStore image file, if it's marked as deleted.
244 * @param int $field
245 * @return bool
246 */
247 function userCan( $field ) {
248 if( isset($this->deleted) && ($this->deleted & $field) == $field ) {
249 global $wgUser;
250 $permission = ( $this->deleted & File::DELETED_RESTRICTED ) == File::DELETED_RESTRICTED
251 ? 'hiderevision'
252 : 'deleterevision';
253 wfDebug( "Checking for $permission due to $field match on $this->mDeleted\n" );
254 return $wgUser->isAllowed( $permission );
255 } else {
256 return true;
257 }
258 }
259
260 }
261
262