Static code analysis housekeeping time... things that could be double-checked are...
[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 static function newFromTitle( $title, $repo, $time ) {
15 return new self( $title, $repo, $time, null );
16 }
17
18 static function newFromArchiveName( $title, $repo, $archiveName ) {
19 return new self( $title, $repo, null, $archiveName );
20 }
21
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_' );
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'] == self::CACHE_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 $this->fileExists = true;
98 foreach ( $info as $name => $value ) {
99 $this->$name = $value;
100 }
101 } elseif ( $more ) {
102 wfDebug( "Cache key was truncated, oldimage row might be found in the database\n" );
103 } else {
104 wfDebug( "Image did not exist at the specified time.\n" );
105 $this->fileExists = false;
106 $this->dataLoaded = true;
107 }
108 }
109
110 if ( $this->dataLoaded ) {
111 wfIncrStats( 'image_cache_hit' );
112 } else {
113 wfIncrStats( 'image_cache_miss' );
114 }
115
116 wfProfileOut( __METHOD__ );
117 return $this->dataLoaded;
118 }
119
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 ) ) {
123 return;
124 }
125 // This is expensive, so we only do it if $wgMemc is real
126 global $wgMemc;
127 if ( $wgMemc instanceof FakeMemcachedClient ) {
128 return;
129 }
130 $key = $this->getCacheKey();
131 if ( !$key ) {
132 return;
133 }
134 wfProfileIn( __METHOD__ );
135
136 $dbr = $this->repo->getSlaveDB();
137 $res = $dbr->select( 'oldimage', $this->getCacheFields( 'oi_' ),
138 array( 'oi_name' => $this->getName() ), __METHOD__,
139 array(
140 'LIMIT' => self::MAX_CACHE_ROWS + 1,
141 'ORDER BY' => 'oi_timestamp DESC',
142 ));
143 $cache = array( 'version' => self::CACHE_VERSION );
144 $numRows = $dbr->numRows( $res );
145 if ( $numRows > self::MAX_CACHE_ROWS ) {
146 $cache['more'] = true;
147 $numRows--;
148 }
149 for ( $i = 0; $i < $numRows; $i++ ) {
150 $row = $dbr->fetchObject( $res );
151 $decoded = $this->decodeRow( $row, 'oi_' );
152 $cache[$row->oi_timestamp] = $decoded;
153 }
154 $dbr->freeResult( $res );
155 $wgMemc->set( $key, $cache, 7*86400 /* 1 week */ );
156 wfProfileOut( __METHOD__ );
157 }
158
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;
166 } else {
167 $conds[] = 'oi_timestamp <= ' . $dbr->addQuotes( $this->requestedTime );
168 }
169 $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
170 $conds, __METHOD__, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
171 if ( $row ) {
172 $this->loadFromRow( $row, 'oi_' );
173 } else {
174 $this->fileExists = false;
175 }
176 wfProfileOut( __METHOD__ );
177 }
178
179 function getCacheFields( $prefix = 'img_' ) {
180 $fields = parent::getCacheFields( $prefix );
181 $fields[] = $prefix . 'archive_name';
182
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' ) );
186 return $fields;
187 }
188
189 function getRel() {
190 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
191 }
192
193 function getUrlRel() {
194 return 'archive/' . $this->getHashPath() . urlencode( $this->getArchiveName() );
195 }
196
197 function upgradeRow() {
198 wfProfileIn( __METHOD__ );
199 $this->loadFromFile();
200
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__ );
205 return;
206 }
207
208 $dbw = $this->repo->getMasterDB();
209 list( $major, $minor ) = self::splitMime( $this->mime );
210
211 wfDebug(__METHOD__.': upgrading '.$this->archive_name." to the current schema\n");
212 $dbw->update( 'oldimage',
213 array(
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,
222 ), array(
223 'oi_name' => $this->getName(),
224 'oi_archive_name' => $this->archive_name ),
225 __METHOD__
226 );
227 wfProfileOut( __METHOD__ );
228 }
229 }
230
231
232