Merged filerepo-work branch:
[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 function newFromTitle( $title, $repo, $time ) {
15 return new self( $title, $repo, $time, null );
16 }
17
18 function newFromArchiveName( $title, $repo, $archiveName ) {
19 return new self( $title, $repo, null, $archiveName );
20 }
21
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_' );
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 /**
61 * Try to load file metadata from memcached. Returns true on success.
62 */
63 function loadFromCache() {
64 global $wgMemc;
65 wfProfileIn( __METHOD__ );
66 $this->dataLoaded = false;
67 $key = $this->getCacheKey();
68 if ( !$key ) {
69 return false;
70 }
71 $oldImages = $wgMemc->get( $key );
72
73 if ( isset( $oldImages['version'] ) && $oldImages['version'] == MW_OLDFILE_VERSION ) {
74 unset( $oldImages['version'] );
75 $more = isset( $oldImages['more'] );
76 unset( $oldImages['more'] );
77 $found = false;
78 if ( is_null( $this->requestedTime ) ) {
79 foreach ( $oldImages as $timestamp => $info ) {
80 if ( $info['archive_name'] == $this->archive_name ) {
81 $found = true;
82 break;
83 }
84 }
85 } else {
86 krsort( $oldImages );
87 foreach ( $oldImages as $timestamp => $info ) {
88 if ( $timestamp <= $this->requestedTime ) {
89 $found = true;
90 break;
91 }
92 }
93 }
94 if ( $found ) {
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;
99 }
100 } elseif ( $more ) {
101 wfDebug( "Cache key was truncated, oldimage row might be found in the database\n" );
102 } else {
103 wfDebug( "Image did not exist at the specified time.\n" );
104 $this->fileExists = false;
105 $this->dataLoaded = true;
106 }
107 }
108
109 if ( $this->dataLoaded ) {
110 wfIncrStats( 'image_cache_hit' );
111 } else {
112 wfIncrStats( 'image_cache_miss' );
113 }
114
115 wfProfileOut( __METHOD__ );
116 return $this->dataLoaded;
117 }
118
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
122 global $wgMemc;
123 if ( $wgMemc instanceof FakeMemcachedClient ) {
124 return;
125 }
126 $key = $this->getCacheKey();
127 if ( !$key ) {
128 return;
129 }
130 wfProfileIn( __METHOD__ );
131
132 $dbr = $this->repo->getSlaveDB();
133 $res = $dbr->select( 'oldimage', $this->getCacheFields(),
134 array( 'oi_name' => $this->getName() ), __METHOD__,
135 array(
136 'LIMIT' => self::MAX_CACHE_ROWS + 1,
137 'ORDER BY' => 'oi_timestamp DESC',
138 ));
139 $cache = array( 'version' => self::CACHE_VERSION );
140 $numRows = $dbr->numRows( $res );
141 if ( $numRows > self::MAX_CACHE_ROWS ) {
142 $cache['more'] = true;
143 $numRows--;
144 }
145 for ( $i = 0; $i < $numRows; $i++ ) {
146 $row = $dbr->fetchObject( $res );
147 $this->decodeRow( $row, 'oi_' );
148 $cache[$row->oi_timestamp] = $row;
149 }
150 $dbr->freeResult( $res );
151 $wgMemc->set( $key, $cache, 7*86400 /* 1 week */ );
152 wfProfileOut( __METHOD__ );
153 }
154
155 function loadFromDB() {
156 wfProfileIn( __METHOD__ );
157 $dbr = $this->repo->getSlaveDB();
158 $conds = array( 'oi_name' => $this->getName() );
159 if ( is_null( $this->requestedTimestamp ) ) {
160 $conds['oi_archive_name'] = $this->archive_name;
161 } else {
162 $conds[] = 'oi_timestamp <= ' . $dbr->addQuotes( $this->requestedTimestamp );
163 }
164 $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
165 $conds, __METHOD__, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
166 if ( $row ) {
167 $this->loadFromRow( $row, 'oi_' );
168 } else {
169 $this->fileExists = false;
170 }
171 $this->dataLoaded = true;
172 }
173
174 function getCacheFields( $prefix = 'img_' ) {
175 $fields = parent::getCacheFields( $prefix );
176 $fields[] = $prefix . 'archive_name';
177
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' ) );
181 return $fields;
182 }
183
184 function getRel() {
185 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
186 }
187
188 function getUrlRel() {
189 return 'archive/' . $this->getHashPath() . urlencode( $this->getArchiveName() );
190 }
191
192 function upgradeRow() {
193 wfProfileIn( __METHOD__ );
194
195 $this->loadFromFile();
196
197 $dbw = $this->repo->getMasterDB();
198 list( $major, $minor ) = self::splitMime( $this->mime );
199
200 wfDebug(__METHOD__.': upgrading '.$this->archive_name." to the current schema\n");
201 $dbw->update( 'oldimage',
202 array(
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 ),
211 __METHOD__
212 );
213 wfProfileOut( __METHOD__ );
214 }
215
216 // XXX: Temporary hack before schema update
217 function maybeUpgradeRow() {}
218
219 }
220
221
222 ?>