* Introduced FileRepoStatus -- result class for file repo operations.
[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 // Cache the entire history of the image (up to MAX_CACHE_ROWS).
122 // This is expensive, so we only do it if $wgMemc is real
123 global $wgMemc;
124 if ( $wgMemc instanceof FakeMemcachedClient ) {
125 return;
126 }
127 $key = $this->getCacheKey();
128 if ( !$key ) {
129 return;
130 }
131 wfProfileIn( __METHOD__ );
132
133 $dbr = $this->repo->getSlaveDB();
134 $res = $dbr->select( 'oldimage', $this->getCacheFields( 'oi_' ),
135 array( 'oi_name' => $this->getName() ), __METHOD__,
136 array(
137 'LIMIT' => self::MAX_CACHE_ROWS + 1,
138 'ORDER BY' => 'oi_timestamp DESC',
139 ));
140 $cache = array( 'version' => self::CACHE_VERSION );
141 $numRows = $dbr->numRows( $res );
142 if ( $numRows > self::MAX_CACHE_ROWS ) {
143 $cache['more'] = true;
144 $numRows--;
145 }
146 for ( $i = 0; $i < $numRows; $i++ ) {
147 $row = $dbr->fetchObject( $res );
148 $decoded = $this->decodeRow( $row, 'oi_' );
149 $cache[$row->oi_timestamp] = $decoded;
150 }
151 $dbr->freeResult( $res );
152 $wgMemc->set( $key, $cache, 7*86400 /* 1 week */ );
153 wfProfileOut( __METHOD__ );
154 }
155
156 function loadFromDB() {
157 wfProfileIn( __METHOD__ );
158 $dbr = $this->repo->getSlaveDB();
159 $conds = array( 'oi_name' => $this->getName() );
160 if ( is_null( $this->requestedTime ) ) {
161 $conds['oi_archive_name'] = $this->archive_name;
162 } else {
163 $conds[] = 'oi_timestamp <= ' . $dbr->addQuotes( $this->requestedTime );
164 }
165 $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
166 $conds, __METHOD__, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
167 if ( $row ) {
168 $this->loadFromRow( $row, 'oi_' );
169 } else {
170 $this->fileExists = false;
171 }
172 $this->dataLoaded = true;
173 wfProfileOut( __METHOD__ );
174 }
175
176 function getCacheFields( $prefix = 'img_' ) {
177 $fields = parent::getCacheFields( $prefix );
178 $fields[] = $prefix . 'archive_name';
179
180 // XXX: Temporary hack before schema update
181 $fields = array_diff( $fields, array(
182 'oi_media_type', 'oi_major_mime', 'oi_minor_mime', 'oi_metadata' ) );
183 return $fields;
184 }
185
186 function getRel() {
187 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
188 }
189
190 function getUrlRel() {
191 return 'archive/' . $this->getHashPath() . urlencode( $this->getArchiveName() );
192 }
193
194 function upgradeRow() {
195 wfProfileIn( __METHOD__ );
196
197 $this->loadFromFile();
198
199 $dbw = $this->repo->getMasterDB();
200 list( $major, $minor ) = self::splitMime( $this->mime );
201
202 wfDebug(__METHOD__.': upgrading '.$this->archive_name." to the current schema\n");
203 $dbw->update( 'oldimage',
204 array(
205 'oi_width' => $this->width,
206 'oi_height' => $this->height,
207 'oi_bits' => $this->bits,
208 #'oi_media_type' => $this->media_type,
209 #'oi_major_mime' => $major,
210 #'oi_minor_mime' => $minor,
211 #'oi_metadata' => $this->metadata,
212 'oi_sha1' => $this->sha1,
213 ), array(
214 'oi_name' => $this->getName(),
215 'oi_archive_name' => $this->archive_name ),
216 __METHOD__
217 );
218 wfProfileOut( __METHOD__ );
219 }
220 }
221
222
223