4906e795feffd7c9b0a7e74f663cb9097dd2c8e8
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 = null ) {
15 # The null default value is only here to avoid an E_STRICT
17 throw new MWException( __METHOD__
.' got null for $time parameter' );
18 return new self( $title, $repo, $time, null );
21 static function newFromArchiveName( $title, $repo, $archiveName ) {
22 return new self( $title, $repo, null, $archiveName );
25 static function newFromRow( $row, $repo ) {
26 $title = Title
::makeTitle( NS_IMAGE
, $row->oi_name
);
27 $file = new self( $title, $repo, null, $row->oi_archive_name
);
28 $file->loadFromRow( $row, 'oi_' );
34 * @param FileRepo $repo
35 * @param string $time Timestamp or null to load by archive name
36 * @param string $archiveName Archive name or null to load by timestamp
38 function __construct( $title, $repo, $time, $archiveName ) {
39 parent
::__construct( $title, $repo );
40 $this->requestedTime
= $time;
41 $this->archive_name
= $archiveName;
42 if ( is_null( $time ) && is_null( $archiveName ) ) {
43 throw new MWException( __METHOD__
.': must specify at least one of $time or $archiveName' );
47 function getCacheKey() {
48 $hashedName = md5($this->getName());
49 return wfMemcKey( 'oldfile', $hashedName );
52 function getArchiveName() {
53 if ( !isset( $this->archive_name
) ) {
56 return $this->archive_name
;
63 function isVisible() {
64 return $this->exists() && !$this->isDeleted(File
::DELETED_FILE
);
68 * Try to load file metadata from memcached. Returns true on success.
70 function loadFromCache() {
72 wfProfileIn( __METHOD__
);
73 $this->dataLoaded
= false;
74 $key = $this->getCacheKey();
78 $oldImages = $wgMemc->get( $key );
80 if ( isset( $oldImages['version'] ) && $oldImages['version'] == self
::CACHE_VERSION
) {
81 unset( $oldImages['version'] );
82 $more = isset( $oldImages['more'] );
83 unset( $oldImages['more'] );
85 if ( is_null( $this->requestedTime
) ) {
86 foreach ( $oldImages as $timestamp => $info ) {
87 if ( $info['archive_name'] == $this->archive_name
) {
94 foreach ( $oldImages as $timestamp => $info ) {
95 if ( $timestamp == $this->requestedTime
) {
102 wfDebug( "Pulling file metadata from cache key {$key}[{$timestamp}]\n" );
103 $this->dataLoaded
= true;
104 $this->fileExists
= true;
105 foreach ( $info as $name => $value ) {
106 $this->$name = $value;
109 wfDebug( "Cache key was truncated, oldimage row might be found in the database\n" );
111 wfDebug( "Image did not exist at the specified time.\n" );
112 $this->fileExists
= false;
113 $this->dataLoaded
= true;
117 if ( $this->dataLoaded
) {
118 wfIncrStats( 'image_cache_hit' );
120 wfIncrStats( 'image_cache_miss' );
123 wfProfileOut( __METHOD__
);
124 return $this->dataLoaded
;
127 function saveToCache() {
128 // If a timestamp was specified, cache the entire history of the image (up to MAX_CACHE_ROWS).
129 if ( is_null( $this->requestedTime
) ) {
132 // This is expensive, so we only do it if $wgMemc is real
134 if ( $wgMemc instanceof FakeMemcachedClient
) {
137 $key = $this->getCacheKey();
141 wfProfileIn( __METHOD__
);
143 $dbr = $this->repo
->getSlaveDB();
144 $res = $dbr->select( 'oldimage', $this->getCacheFields( 'oi_' ),
145 array( 'oi_name' => $this->getName() ), __METHOD__
,
147 'LIMIT' => self
::MAX_CACHE_ROWS +
1,
148 'ORDER BY' => 'oi_timestamp DESC',
150 $cache = array( 'version' => self
::CACHE_VERSION
);
151 $numRows = $dbr->numRows( $res );
152 if ( $numRows > self
::MAX_CACHE_ROWS
) {
153 $cache['more'] = true;
156 for ( $i = 0; $i < $numRows; $i++
) {
157 $row = $dbr->fetchObject( $res );
158 $decoded = $this->decodeRow( $row, 'oi_' );
159 $cache[$row->oi_timestamp
] = $decoded;
161 $dbr->freeResult( $res );
162 $wgMemc->set( $key, $cache, 7*86400 /* 1 week */ );
163 wfProfileOut( __METHOD__
);
166 function loadFromDB() {
167 wfProfileIn( __METHOD__
);
168 $this->dataLoaded
= true;
169 $dbr = $this->repo
->getSlaveDB();
170 $conds = array( 'oi_name' => $this->getName() );
171 if ( is_null( $this->requestedTime
) ) {
172 $conds['oi_archive_name'] = $this->archive_name
;
174 $conds[] = 'oi_timestamp = ' . $dbr->addQuotes( $dbr->timestamp( $this->requestedTime
) );
176 $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
177 $conds, __METHOD__
, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
179 $this->loadFromRow( $row, 'oi_' );
181 $this->fileExists
= false;
183 wfProfileOut( __METHOD__
);
186 function getCacheFields( $prefix = 'img_' ) {
187 $fields = parent
::getCacheFields( $prefix );
188 $fields[] = $prefix . 'archive_name';
189 $fields[] = $prefix . 'deleted';
194 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
197 function getUrlRel() {
198 return 'archive/' . $this->getHashPath() . urlencode( $this->getArchiveName() );
201 function upgradeRow() {
202 wfProfileIn( __METHOD__
);
203 $this->loadFromFile();
205 # Don't destroy file info of missing files
206 if ( !$this->fileExists
) {
207 wfDebug( __METHOD__
.": file does not exist, aborting\n" );
208 wfProfileOut( __METHOD__
);
212 $dbw = $this->repo
->getMasterDB();
213 list( $major, $minor ) = self
::splitMime( $this->mime
);
215 wfDebug(__METHOD__
.': upgrading '.$this->archive_name
." to the current schema\n");
216 $dbw->update( 'oldimage',
218 'oi_width' => $this->width
,
219 'oi_height' => $this->height
,
220 'oi_bits' => $this->bits
,
221 'oi_media_type' => $this->media_type
,
222 'oi_major_mime' => $major,
223 'oi_minor_mime' => $minor,
224 'oi_metadata' => $this->metadata
,
225 'oi_sha1' => $this->sha1
,
227 'oi_name' => $this->getName(),
228 'oi_archive_name' => $this->archive_name
),
231 wfProfileOut( __METHOD__
);
235 * int $field one of DELETED_* bitfield constants
236 * for file or revision rows
239 function isDeleted( $field ) {
240 return ($this->deleted
& $field) == $field;
244 * Determine if the current user is allowed to view a particular
245 * field of this FileStore image file, if it's marked as deleted.
249 function userCan( $field ) {
250 if( isset($this->deleted
) && ($this->deleted
& $field) == $field ) {
252 $permission = ( $this->deleted
& File
::DELETED_RESTRICTED
) == File
::DELETED_RESTRICTED
255 wfDebug( "Checking for $permission due to $field match on $this->mDeleted\n" );
256 return $wgUser->isAllowed( $permission );