4906e795feffd7c9b0a7e74f663cb9097dd2c8e8
[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 = null ) {
15 # The null default value is only here to avoid an E_STRICT
16 if( $time === null )
17 throw new MWException( __METHOD__.' got null for $time parameter' );
18 return new self( $title, $repo, $time, null );
19 }
20
21 static function newFromArchiveName( $title, $repo, $archiveName ) {
22 return new self( $title, $repo, null, $archiveName );
23 }
24
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_' );
29 return $file;
30 }
31
32 /**
33 * @param Title $title
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
37 */
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' );
44 }
45 }
46
47 function getCacheKey() {
48 $hashedName = md5($this->getName());
49 return wfMemcKey( 'oldfile', $hashedName );
50 }
51
52 function getArchiveName() {
53 if ( !isset( $this->archive_name ) ) {
54 $this->load();
55 }
56 return $this->archive_name;
57 }
58
59 function isOld() {
60 return true;
61 }
62
63 function isVisible() {
64 return $this->exists() && !$this->isDeleted(File::DELETED_FILE);
65 }
66
67 /**
68 * Try to load file metadata from memcached. Returns true on success.
69 */
70 function loadFromCache() {
71 global $wgMemc;
72 wfProfileIn( __METHOD__ );
73 $this->dataLoaded = false;
74 $key = $this->getCacheKey();
75 if ( !$key ) {
76 return false;
77 }
78 $oldImages = $wgMemc->get( $key );
79
80 if ( isset( $oldImages['version'] ) && $oldImages['version'] == self::CACHE_VERSION ) {
81 unset( $oldImages['version'] );
82 $more = isset( $oldImages['more'] );
83 unset( $oldImages['more'] );
84 $found = false;
85 if ( is_null( $this->requestedTime ) ) {
86 foreach ( $oldImages as $timestamp => $info ) {
87 if ( $info['archive_name'] == $this->archive_name ) {
88 $found = true;
89 break;
90 }
91 }
92 } else {
93 krsort( $oldImages );
94 foreach ( $oldImages as $timestamp => $info ) {
95 if ( $timestamp == $this->requestedTime ) {
96 $found = true;
97 break;
98 }
99 }
100 }
101 if ( $found ) {
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;
107 }
108 } elseif ( $more ) {
109 wfDebug( "Cache key was truncated, oldimage row might be found in the database\n" );
110 } else {
111 wfDebug( "Image did not exist at the specified time.\n" );
112 $this->fileExists = false;
113 $this->dataLoaded = true;
114 }
115 }
116
117 if ( $this->dataLoaded ) {
118 wfIncrStats( 'image_cache_hit' );
119 } else {
120 wfIncrStats( 'image_cache_miss' );
121 }
122
123 wfProfileOut( __METHOD__ );
124 return $this->dataLoaded;
125 }
126
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 ) ) {
130 return;
131 }
132 // This is expensive, so we only do it if $wgMemc is real
133 global $wgMemc;
134 if ( $wgMemc instanceof FakeMemcachedClient ) {
135 return;
136 }
137 $key = $this->getCacheKey();
138 if ( !$key ) {
139 return;
140 }
141 wfProfileIn( __METHOD__ );
142
143 $dbr = $this->repo->getSlaveDB();
144 $res = $dbr->select( 'oldimage', $this->getCacheFields( 'oi_' ),
145 array( 'oi_name' => $this->getName() ), __METHOD__,
146 array(
147 'LIMIT' => self::MAX_CACHE_ROWS + 1,
148 'ORDER BY' => 'oi_timestamp DESC',
149 ));
150 $cache = array( 'version' => self::CACHE_VERSION );
151 $numRows = $dbr->numRows( $res );
152 if ( $numRows > self::MAX_CACHE_ROWS ) {
153 $cache['more'] = true;
154 $numRows--;
155 }
156 for ( $i = 0; $i < $numRows; $i++ ) {
157 $row = $dbr->fetchObject( $res );
158 $decoded = $this->decodeRow( $row, 'oi_' );
159 $cache[$row->oi_timestamp] = $decoded;
160 }
161 $dbr->freeResult( $res );
162 $wgMemc->set( $key, $cache, 7*86400 /* 1 week */ );
163 wfProfileOut( __METHOD__ );
164 }
165
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;
173 } else {
174 $conds[] = 'oi_timestamp = ' . $dbr->addQuotes( $dbr->timestamp( $this->requestedTime ) );
175 }
176 $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
177 $conds, __METHOD__, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
178 if ( $row ) {
179 $this->loadFromRow( $row, 'oi_' );
180 } else {
181 $this->fileExists = false;
182 }
183 wfProfileOut( __METHOD__ );
184 }
185
186 function getCacheFields( $prefix = 'img_' ) {
187 $fields = parent::getCacheFields( $prefix );
188 $fields[] = $prefix . 'archive_name';
189 $fields[] = $prefix . 'deleted';
190 return $fields;
191 }
192
193 function getRel() {
194 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
195 }
196
197 function getUrlRel() {
198 return 'archive/' . $this->getHashPath() . urlencode( $this->getArchiveName() );
199 }
200
201 function upgradeRow() {
202 wfProfileIn( __METHOD__ );
203 $this->loadFromFile();
204
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__ );
209 return;
210 }
211
212 $dbw = $this->repo->getMasterDB();
213 list( $major, $minor ) = self::splitMime( $this->mime );
214
215 wfDebug(__METHOD__.': upgrading '.$this->archive_name." to the current schema\n");
216 $dbw->update( 'oldimage',
217 array(
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,
226 ), array(
227 'oi_name' => $this->getName(),
228 'oi_archive_name' => $this->archive_name ),
229 __METHOD__
230 );
231 wfProfileOut( __METHOD__ );
232 }
233
234 /**
235 * int $field one of DELETED_* bitfield constants
236 * for file or revision rows
237 * @return bool
238 */
239 function isDeleted( $field ) {
240 return ($this->deleted & $field) == $field;
241 }
242
243 /**
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.
246 * @param int $field
247 * @return bool
248 */
249 function userCan( $field ) {
250 if( isset($this->deleted) && ($this->deleted & $field) == $field ) {
251 global $wgUser;
252 $permission = ( $this->deleted & File::DELETED_RESTRICTED ) == File::DELETED_RESTRICTED
253 ? 'hiderevision'
254 : 'deleterevision';
255 wfDebug( "Checking for $permission due to $field match on $this->mDeleted\n" );
256 return $wgUser->isAllowed( $permission );
257 } else {
258 return true;
259 }
260 }
261
262 }
263
264