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
;
61 * Try to load file metadata from memcached. Returns true on success.
63 function loadFromCache() {
65 wfProfileIn( __METHOD__
);
66 $this->dataLoaded
= false;
67 $key = $this->getCacheKey();
71 $oldImages = $wgMemc->get( $key );
73 if ( isset( $oldImages['version'] ) && $oldImages['version'] == self
::CACHE_VERSION
) {
74 unset( $oldImages['version'] );
75 $more = isset( $oldImages['more'] );
76 unset( $oldImages['more'] );
78 if ( is_null( $this->requestedTime
) ) {
79 foreach ( $oldImages as $timestamp => $info ) {
80 if ( $info['archive_name'] == $this->archive_name
) {
87 foreach ( $oldImages as $timestamp => $info ) {
88 if ( $timestamp <= $this->requestedTime
) {
95 wfDebug( "Pulling file metadata from cache key {$key}[{$timestamp}]\n" );
96 $this->dataLoaded
= true;
97 $this->fileExists
= true;
98 foreach ( $info as $name => $value ) {
99 $this->$name = $value;
102 wfDebug( "Cache key was truncated, oldimage row might be found in the database\n" );
104 wfDebug( "Image did not exist at the specified time.\n" );
105 $this->fileExists
= false;
106 $this->dataLoaded
= true;
110 if ( $this->dataLoaded
) {
111 wfIncrStats( 'image_cache_hit' );
113 wfIncrStats( 'image_cache_miss' );
116 wfProfileOut( __METHOD__
);
117 return $this->dataLoaded
;
120 function saveToCache() {
121 // If a timestamp was specified, cache the entire history of the image (up to MAX_CACHE_ROWS).
122 if ( is_null( $this->requestedTime
) ) {
125 // This is expensive, so we only do it if $wgMemc is real
127 if ( $wgMemc instanceof FakeMemcachedClient
) {
130 $key = $this->getCacheKey();
134 wfProfileIn( __METHOD__
);
136 $dbr = $this->repo
->getSlaveDB();
137 $res = $dbr->select( 'oldimage', $this->getCacheFields( 'oi_' ),
138 array( 'oi_name' => $this->getName() ), __METHOD__
,
140 'LIMIT' => self
::MAX_CACHE_ROWS +
1,
141 'ORDER BY' => 'oi_timestamp DESC',
143 $cache = array( 'version' => self
::CACHE_VERSION
);
144 $numRows = $dbr->numRows( $res );
145 if ( $numRows > self
::MAX_CACHE_ROWS
) {
146 $cache['more'] = true;
149 for ( $i = 0; $i < $numRows; $i++
) {
150 $row = $dbr->fetchObject( $res );
151 $decoded = $this->decodeRow( $row, 'oi_' );
152 $cache[$row->oi_timestamp
] = $decoded;
154 $dbr->freeResult( $res );
155 $wgMemc->set( $key, $cache, 7*86400 /* 1 week */ );
156 wfProfileOut( __METHOD__
);
159 function loadFromDB() {
160 wfProfileIn( __METHOD__
);
161 $this->dataLoaded
= true;
162 $dbr = $this->repo
->getSlaveDB();
163 $conds = array( 'oi_name' => $this->getName() );
164 if ( is_null( $this->requestedTime
) ) {
165 $conds['oi_archive_name'] = $this->archive_name
;
167 $conds[] = 'oi_timestamp <= ' . $dbr->addQuotes( $this->requestedTime
);
169 $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
170 $conds, __METHOD__
, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
172 $this->loadFromRow( $row, 'oi_' );
174 $this->fileExists
= false;
176 wfProfileOut( __METHOD__
);
179 function getCacheFields( $prefix = 'img_' ) {
180 $fields = parent
::getCacheFields( $prefix );
181 $fields[] = $prefix . 'archive_name';
183 // XXX: Temporary hack before schema update
184 //$fields = array_diff( $fields, array(
185 // 'oi_media_type', 'oi_major_mime', 'oi_minor_mime', 'oi_metadata' ) );
190 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
193 function getUrlRel() {
194 return 'archive/' . $this->getHashPath() . urlencode( $this->getArchiveName() );
197 function upgradeRow() {
198 wfProfileIn( __METHOD__
);
199 $this->loadFromFile();
201 # Don't destroy file info of missing files
202 if ( !$this->fileExists
) {
203 wfDebug( __METHOD__
.": file does not exist, aborting\n" );
204 wfProfileOut( __METHOD__
);
208 $dbw = $this->repo
->getMasterDB();
209 list( $major, $minor ) = self
::splitMime( $this->mime
);
211 wfDebug(__METHOD__
.': upgrading '.$this->archive_name
." to the current schema\n");
212 $dbw->update( 'oldimage',
214 'oi_width' => $this->width
,
215 'oi_height' => $this->height
,
216 'oi_bits' => $this->bits
,
217 'oi_media_type' => $this->media_type
,
218 'oi_major_mime' => $major,
219 'oi_minor_mime' => $minor,
220 'oi_metadata' => $this->metadata
,
221 'oi_sha1' => $this->sha1
,
223 'oi_name' => $this->getName(),
224 'oi_archive_name' => $this->archive_name
),
227 wfProfileOut( __METHOD__
);