Some HipHop fixes:
[lhc/web/wiklou.git] / includes / filerepo / OldLocalFile.php
1 <?php
2 /**
3 * Old file in the oldimage table
4 *
5 * @file
6 * @ingroup FileRepo
7 */
8
9 /**
10 * Class to represent a file in the oldimage table
11 *
12 * @ingroup FileRepo
13 */
14 class OldLocalFile extends LocalFile {
15 var $requestedTime, $archive_name;
16
17 const CACHE_VERSION = 1;
18 const MAX_CACHE_ROWS = 20;
19
20 static function newFromTitle( $title, $repo, $time = null ) {
21 # The null default value is only here to avoid an E_STRICT
22 if( $time === null )
23 throw new MWException( __METHOD__.' got null for $time parameter' );
24 return new self( $title, $repo, $time, null );
25 }
26
27 static function newFromArchiveName( $title, $repo, $archiveName ) {
28 return new self( $title, $repo, null, $archiveName );
29 }
30
31 static function newFromRow( $row, $repo ) {
32 $title = Title::makeTitle( NS_FILE, $row->oi_name );
33 $file = new self( $title, $repo, null, $row->oi_archive_name );
34 $file->loadFromRow( $row, 'oi_' );
35 return $file;
36 }
37
38 /**
39 * @param $sha1
40 * @param $repo LocalRepo
41 * @param bool $timestamp
42 * @return bool|OldLocalFile
43 */
44 static function newFromKey( $sha1, $repo, $timestamp = false ) {
45 $conds = array( 'oi_sha1' => $sha1 );
46 if( $timestamp ) {
47 $conds['oi_timestamp'] = $timestamp;
48 }
49 $dbr = $repo->getSlaveDB();
50 $row = $dbr->selectRow( 'oldimage', self::selectFields(), $conds, __METHOD__ );
51 if( $row ) {
52 return self::newFromRow( $row, $repo );
53 } else {
54 return false;
55 }
56 }
57
58 /**
59 * Fields in the oldimage table
60 */
61 static function selectFields() {
62 return array(
63 'oi_name',
64 'oi_archive_name',
65 'oi_size',
66 'oi_width',
67 'oi_height',
68 'oi_metadata',
69 'oi_bits',
70 'oi_media_type',
71 'oi_major_mime',
72 'oi_minor_mime',
73 'oi_description',
74 'oi_user',
75 'oi_user_text',
76 'oi_timestamp',
77 'oi_deleted',
78 'oi_sha1',
79 );
80 }
81
82 /**
83 * @param $title Title
84 * @param $repo FileRepo
85 * @param $time String: timestamp or null to load by archive name
86 * @param $archiveName String: archive name or null to load by timestamp
87 */
88 function __construct( $title, $repo, $time, $archiveName ) {
89 parent::__construct( $title, $repo );
90 $this->requestedTime = $time;
91 $this->archive_name = $archiveName;
92 if ( is_null( $time ) && is_null( $archiveName ) ) {
93 throw new MWException( __METHOD__.': must specify at least one of $time or $archiveName' );
94 }
95 }
96
97 function getCacheKey() {
98 return false;
99 }
100
101 function getArchiveName() {
102 if ( !isset( $this->archive_name ) ) {
103 $this->load();
104 }
105 return $this->archive_name;
106 }
107
108 function isOld() {
109 return true;
110 }
111
112 function isVisible() {
113 return $this->exists() && !$this->isDeleted(File::DELETED_FILE);
114 }
115
116 function loadFromDB() {
117 wfProfileIn( __METHOD__ );
118 $this->dataLoaded = true;
119 $dbr = $this->repo->getSlaveDB();
120 $conds = array( 'oi_name' => $this->getName() );
121 if ( is_null( $this->requestedTime ) ) {
122 $conds['oi_archive_name'] = $this->archive_name;
123 } else {
124 $conds[] = 'oi_timestamp = ' . $dbr->addQuotes( $dbr->timestamp( $this->requestedTime ) );
125 }
126 $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
127 $conds, __METHOD__, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
128 if ( $row ) {
129 $this->loadFromRow( $row, 'oi_' );
130 } else {
131 $this->fileExists = false;
132 }
133 wfProfileOut( __METHOD__ );
134 }
135
136 function getCacheFields( $prefix = 'img_' ) {
137 $fields = parent::getCacheFields( $prefix );
138 $fields[] = $prefix . 'archive_name';
139 $fields[] = $prefix . 'deleted';
140 return $fields;
141 }
142
143 function getRel() {
144 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
145 }
146
147 function getUrlRel() {
148 return 'archive/' . $this->getHashPath() . rawurlencode( $this->getArchiveName() );
149 }
150
151 function upgradeRow() {
152 wfProfileIn( __METHOD__ );
153 $this->loadFromFile();
154
155 # Don't destroy file info of missing files
156 if ( !$this->fileExists ) {
157 wfDebug( __METHOD__.": file does not exist, aborting\n" );
158 wfProfileOut( __METHOD__ );
159 return;
160 }
161
162 $dbw = $this->repo->getMasterDB();
163 list( $major, $minor ) = self::splitMime( $this->mime );
164
165 wfDebug(__METHOD__.': upgrading '.$this->archive_name." to the current schema\n");
166 $dbw->update( 'oldimage',
167 array(
168 'oi_width' => $this->width,
169 'oi_height' => $this->height,
170 'oi_bits' => $this->bits,
171 'oi_media_type' => $this->media_type,
172 'oi_major_mime' => $major,
173 'oi_minor_mime' => $minor,
174 'oi_metadata' => $this->metadata,
175 'oi_sha1' => $this->sha1,
176 ), array(
177 'oi_name' => $this->getName(),
178 'oi_archive_name' => $this->archive_name ),
179 __METHOD__
180 );
181 wfProfileOut( __METHOD__ );
182 }
183
184 /**
185 * @param $field Integer: one of DELETED_* bitfield constants
186 * for file or revision rows
187 * @return bool
188 */
189 function isDeleted( $field ) {
190 $this->load();
191 return ($this->deleted & $field) == $field;
192 }
193
194 /**
195 * Returns bitfield value
196 * @return int
197 */
198 function getVisibility() {
199 $this->load();
200 return (int)$this->deleted;
201 }
202
203 /**
204 * Determine if the current user is allowed to view a particular
205 * field of this image file, if it's marked as deleted.
206 *
207 * @param $field Integer
208 * @return bool
209 */
210 function userCan( $field ) {
211 $this->load();
212 return Revision::userCanBitfield( $this->deleted, $field );
213 }
214
215 /**
216 * Upload a file directly into archive. Generally for Special:Import.
217 *
218 * @param $srcPath string File system path of the source file
219 * @param $archiveName string Full archive name of the file, in the form
220 * $timestamp!$filename, where $filename must match $this->getName()
221 */
222 function uploadOld( $srcPath, $archiveName, $timestamp, $comment, $user, $flags = 0 ) {
223 $this->lock();
224 $status = $this->publish( $srcPath,
225 $flags & File::DELETE_SOURCE ? FileRepo::DELETE_SOURCE : 0,
226 $archiveName );
227
228 if ( $status->isGood() ) {
229 if ( !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) ) {
230 $status->fatal( 'filenotfound', $srcPath );
231 }
232 }
233
234 $this->unlock();
235
236 return $status;
237 }
238
239 /**
240 * Record a file upload in the oldimage table, without adding log entries.
241 *
242 * @param $srcPath string File system path to the source file
243 * @param $archiveName string The archive name of the file
244 * @param $comment string Upload comment
245 * @param $user User User who did this upload
246 * @return bool
247 */
248 function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
249 $dbw = $this->repo->getMasterDB();
250 $dbw->begin();
251
252 $dstPath = $this->repo->getZonePath( 'public' ) . '/' . $this->getRel();
253 $props = self::getPropsFromPath( $dstPath );
254 if ( !$props['fileExists'] ) {
255 return false;
256 }
257
258 $dbw->insert( 'oldimage',
259 array(
260 'oi_name' => $this->getName(),
261 'oi_archive_name' => $archiveName,
262 'oi_size' => $props['size'],
263 'oi_width' => intval( $props['width'] ),
264 'oi_height' => intval( $props['height'] ),
265 'oi_bits' => $props['bits'],
266 'oi_timestamp' => $dbw->timestamp( $timestamp ),
267 'oi_description' => $comment,
268 'oi_user' => $user->getId(),
269 'oi_user_text' => $user->getName(),
270 'oi_metadata' => $props['metadata'],
271 'oi_media_type' => $props['media_type'],
272 'oi_major_mime' => $props['major_mime'],
273 'oi_minor_mime' => $props['minor_mime'],
274 'oi_sha1' => $props['sha1'],
275 ), __METHOD__
276 );
277
278 $dbw->commit();
279
280 return true;
281 }
282
283 }