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 function newFromTitle( $title, $repo, $time ) {
15 return new self( $title, $repo, $time, null );
18 function newFromArchiveName( $title, $repo, $archiveName ) {
19 return new self( $title, $repo, null, $archiveName );
22 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'] == MW_OLDFILE_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 foreach ( $cachedValues as $name => $value ) {
98 $this->$name = $value;
101 wfDebug( "Cache key was truncated, oldimage row might be found in the database\n" );
103 wfDebug( "Image did not exist at the specified time.\n" );
104 $this->fileExists
= false;
105 $this->dataLoaded
= true;
109 if ( $this->dataLoaded
) {
110 wfIncrStats( 'image_cache_hit' );
112 wfIncrStats( 'image_cache_miss' );
115 wfProfileOut( __METHOD__
);
116 return $this->dataLoaded
;
119 function saveToCache() {
120 // Cache the entire history of the image (up to MAX_CACHE_ROWS).
121 // This is expensive, so we only do it if $wgMemc is real
123 if ( $wgMemc instanceof FakeMemcachedClient
) {
126 $key = $this->getCacheKey();
130 wfProfileIn( __METHOD__
);
132 $dbr = $this->repo
->getSlaveDB();
133 $res = $dbr->select( 'oldimage', $this->getCacheFields(),
134 array( 'oi_name' => $this->getName() ), __METHOD__
,
136 'LIMIT' => self
::MAX_CACHE_ROWS +
1,
137 'ORDER BY' => 'oi_timestamp DESC',
139 $cache = array( 'version' => self
::CACHE_VERSION
);
140 $numRows = $dbr->numRows( $res );
141 if ( $numRows > self
::MAX_CACHE_ROWS
) {
142 $cache['more'] = true;
145 for ( $i = 0; $i < $numRows; $i++
) {
146 $row = $dbr->fetchObject( $res );
147 $this->decodeRow( $row, 'oi_' );
148 $cache[$row->oi_timestamp
] = $row;
150 $dbr->freeResult( $res );
151 $wgMemc->set( $key, $cache, 7*86400 /* 1 week */ );
152 wfProfileOut( __METHOD__
);
155 function loadFromDB() {
156 wfProfileIn( __METHOD__
);
157 $dbr = $this->repo
->getSlaveDB();
158 $conds = array( 'oi_name' => $this->getName() );
159 if ( is_null( $this->requestedTime
) ) {
160 $conds['oi_archive_name'] = $this->archive_name
;
162 $conds[] = 'oi_timestamp <= ' . $dbr->addQuotes( $this->requestedTime
);
164 $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
165 $conds, __METHOD__
, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
167 $this->loadFromRow( $row, 'oi_' );
169 $this->fileExists
= false;
171 $this->dataLoaded
= true;
174 function getCacheFields( $prefix = 'img_' ) {
175 $fields = parent
::getCacheFields( $prefix );
176 $fields[] = $prefix . 'archive_name';
178 // XXX: Temporary hack before schema update
179 $fields = array_diff( $fields, array(
180 'oi_media_type', 'oi_major_mime', 'oi_minor_mime', 'oi_metadata' ) );
185 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
188 function getUrlRel() {
189 return 'archive/' . $this->getHashPath() . urlencode( $this->getArchiveName() );
192 function upgradeRow() {
193 wfProfileIn( __METHOD__
);
195 $this->loadFromFile();
197 $dbw = $this->repo
->getMasterDB();
198 list( $major, $minor ) = self
::splitMime( $this->mime
);
200 wfDebug(__METHOD__
.': upgrading '.$this->archive_name
." to the current schema\n");
201 $dbw->update( 'oldimage',
203 'oi_width' => $this->width
,
204 'oi_height' => $this->height
,
205 'oi_bits' => $this->bits
,
206 #'oi_media_type' => $this->media_type,
207 #'oi_major_mime' => $major,
208 #'oi_minor_mime' => $minor,
209 #'oi_metadata' => $this->metadata,
210 ), array( 'oi_name' => $this->getName(), 'oi_timestamp' => $this->requestedTime
),
213 wfProfileOut( __METHOD__
);
216 // XXX: Temporary hack before schema update
217 function maybeUpgradeRow() {}