4 * Class to represent a file in the oldimage table
8 class OldLocalFile
extends LocalFile
{
9 var $requestedTime, $archive_name;
11 const CACHE_VERSION
= 1;
12 const MAX_CACHE_ROWS
= 20;
14 static function newFromTitle( $title, $repo, $time ) {
15 return new self( $title, $repo, $time, null );
18 static function newFromArchiveName( $title, $repo, $archiveName ) {
19 return new self( $title, $repo, null, $archiveName );
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_' );
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
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' );
44 function getCacheKey() {
45 $hashedName = md5($this->getName());
46 return wfMemcKey( 'oldfile', $hashedName );
49 function getArchiveName() {
50 if ( !isset( $this->archive_name
) ) {
53 return $this->archive_name
;
60 function isVisible() {
61 return $this->exists() && !$this->isDeleted(File
::DELETED_FILE
);
65 * Try to load file metadata from memcached. Returns true on success.
67 function loadFromCache() {
69 wfProfileIn( __METHOD__
);
70 $this->dataLoaded
= false;
71 $key = $this->getCacheKey();
75 $oldImages = $wgMemc->get( $key );
77 if ( isset( $oldImages['version'] ) && $oldImages['version'] == self
::CACHE_VERSION
) {
78 unset( $oldImages['version'] );
79 $more = isset( $oldImages['more'] );
80 unset( $oldImages['more'] );
82 if ( is_null( $this->requestedTime
) ) {
83 foreach ( $oldImages as $timestamp => $info ) {
84 if ( $info['archive_name'] == $this->archive_name
) {
91 foreach ( $oldImages as $timestamp => $info ) {
92 if ( $timestamp <= $this->requestedTime
) {
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;
106 wfDebug( "Cache key was truncated, oldimage row might be found in the database\n" );
108 wfDebug( "Image did not exist at the specified time.\n" );
109 $this->fileExists
= false;
110 $this->dataLoaded
= true;
114 if ( $this->dataLoaded
) {
115 wfIncrStats( 'image_cache_hit' );
117 wfIncrStats( 'image_cache_miss' );
120 wfProfileOut( __METHOD__
);
121 return $this->dataLoaded
;
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
) ) {
129 // This is expensive, so we only do it if $wgMemc is real
131 if ( $wgMemc instanceof FakeMemcachedClient
) {
134 $key = $this->getCacheKey();
138 wfProfileIn( __METHOD__
);
140 $dbr = $this->repo
->getSlaveDB();
141 $res = $dbr->select( 'oldimage', $this->getCacheFields( 'oi_' ),
142 array( 'oi_name' => $this->getName() ), __METHOD__
,
144 'LIMIT' => self
::MAX_CACHE_ROWS +
1,
145 'ORDER BY' => 'oi_timestamp DESC',
147 $cache = array( 'version' => self
::CACHE_VERSION
);
148 $numRows = $dbr->numRows( $res );
149 if ( $numRows > self
::MAX_CACHE_ROWS
) {
150 $cache['more'] = true;
153 for ( $i = 0; $i < $numRows; $i++
) {
154 $row = $dbr->fetchObject( $res );
155 $decoded = $this->decodeRow( $row, 'oi_' );
156 $cache[$row->oi_timestamp
] = $decoded;
158 $dbr->freeResult( $res );
159 $wgMemc->set( $key, $cache, 7*86400 /* 1 week */ );
160 wfProfileOut( __METHOD__
);
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
;
171 $conds[] = 'oi_timestamp <= ' . $dbr->addQuotes( $this->requestedTime
);
173 $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
174 $conds, __METHOD__
, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
176 $this->loadFromRow( $row, 'oi_' );
178 $this->fileExists
= false;
180 wfProfileOut( __METHOD__
);
183 function getCacheFields( $prefix = 'img_' ) {
184 $fields = parent
::getCacheFields( $prefix );
185 $fields[] = $prefix . 'archive_name';
186 $fields[] = $prefix . 'deleted';
192 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
195 function getUrlRel() {
196 return 'archive/' . $this->getHashPath() . urlencode( $this->getArchiveName() );
199 function upgradeRow() {
200 wfProfileIn( __METHOD__
);
201 $this->loadFromFile();
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__
);
210 $dbw = $this->repo
->getMasterDB();
211 list( $major, $minor ) = self
::splitMime( $this->mime
);
213 wfDebug(__METHOD__
.': upgrading '.$this->archive_name
." to the current schema\n");
214 $dbw->update( 'oldimage',
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
,
225 'oi_name' => $this->getName(),
226 'oi_archive_name' => $this->archive_name
),
229 wfProfileOut( __METHOD__
);
233 * int $field one of DELETED_* bitfield constants
234 * for file or revision rows
237 function isDeleted( $field ) {
238 return ($this->deleted
& $field) == $field;
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.
247 function userCan( $field ) {
248 if( isset($this->deleted
) && ($this->deleted
& $field) == $field ) {
250 $permission = ( $this->deleted
& File
::DELETED_RESTRICTED
) == File
::DELETED_RESTRICTED
253 wfDebug( "Checking for $permission due to $field match on $this->mDeleted\n" );
254 return $wgUser->isAllowed( $permission );