Merge "Update documentation of Status"
[lhc/web/wiklou.git] / includes / filerepo / file / OldLocalFile.php
1 <?php
2 /**
3 * Old file in the oldimage table.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup FileAbstraction
22 */
23
24 /**
25 * Class to represent a file in the oldimage table
26 *
27 * @ingroup FileAbstraction
28 */
29 class OldLocalFile extends LocalFile {
30 var $requestedTime, $archive_name;
31
32 const CACHE_VERSION = 1;
33 const MAX_CACHE_ROWS = 20;
34
35 /**
36 * @param $title Title
37 * @param $repo FileRepo
38 * @param $time null
39 * @return OldLocalFile
40 * @throws MWException
41 */
42 static function newFromTitle( $title, $repo, $time = null ) {
43 # The null default value is only here to avoid an E_STRICT
44 if ( $time === null ) {
45 throw new MWException( __METHOD__ . ' got null for $time parameter' );
46 }
47
48 return new self( $title, $repo, $time, null );
49 }
50
51 /**
52 * @param $title Title
53 * @param $repo FileRepo
54 * @param $archiveName
55 * @return OldLocalFile
56 */
57 static function newFromArchiveName( $title, $repo, $archiveName ) {
58 return new self( $title, $repo, null, $archiveName );
59 }
60
61 /**
62 * @param $row
63 * @param $repo FileRepo
64 * @return OldLocalFile
65 */
66 static function newFromRow( $row, $repo ) {
67 $title = Title::makeTitle( NS_FILE, $row->oi_name );
68 $file = new self( $title, $repo, null, $row->oi_archive_name );
69 $file->loadFromRow( $row, 'oi_' );
70
71 return $file;
72 }
73
74 /**
75 * Create a OldLocalFile from a SHA-1 key
76 * Do not call this except from inside a repo class.
77 *
78 * @param string $sha1 base-36 SHA-1
79 * @param $repo LocalRepo
80 * @param string|bool $timestamp MW_timestamp (optional)
81 *
82 * @return bool|OldLocalFile
83 */
84 static function newFromKey( $sha1, $repo, $timestamp = false ) {
85 $dbr = $repo->getSlaveDB();
86
87 $conds = array( 'oi_sha1' => $sha1 );
88 if ( $timestamp ) {
89 $conds['oi_timestamp'] = $dbr->timestamp( $timestamp );
90 }
91
92 $row = $dbr->selectRow( 'oldimage', self::selectFields(), $conds, __METHOD__ );
93 if ( $row ) {
94 return self::newFromRow( $row, $repo );
95 } else {
96 return false;
97 }
98 }
99
100 /**
101 * Fields in the oldimage table
102 * @return array
103 */
104 static function selectFields() {
105 return array(
106 'oi_name',
107 'oi_archive_name',
108 'oi_size',
109 'oi_width',
110 'oi_height',
111 'oi_metadata',
112 'oi_bits',
113 'oi_media_type',
114 'oi_major_mime',
115 'oi_minor_mime',
116 'oi_description',
117 'oi_user',
118 'oi_user_text',
119 'oi_timestamp',
120 'oi_deleted',
121 'oi_sha1',
122 );
123 }
124
125 /**
126 * @param $title Title
127 * @param $repo FileRepo
128 * @param string $time timestamp or null to load by archive name
129 * @param string $archiveName archive name or null to load by timestamp
130 * @throws MWException
131 */
132 function __construct( $title, $repo, $time, $archiveName ) {
133 parent::__construct( $title, $repo );
134 $this->requestedTime = $time;
135 $this->archive_name = $archiveName;
136 if ( is_null( $time ) && is_null( $archiveName ) ) {
137 throw new MWException( __METHOD__ . ': must specify at least one of $time or $archiveName' );
138 }
139 }
140
141 /**
142 * @return bool
143 */
144 function getCacheKey() {
145 return false;
146 }
147
148 /**
149 * @return String
150 */
151 function getArchiveName() {
152 if ( !isset( $this->archive_name ) ) {
153 $this->load();
154 }
155
156 return $this->archive_name;
157 }
158
159 /**
160 * @return bool
161 */
162 function isOld() {
163 return true;
164 }
165
166 /**
167 * @return bool
168 */
169 function isVisible() {
170 return $this->exists() && !$this->isDeleted( File::DELETED_FILE );
171 }
172
173 function loadFromDB() {
174 wfProfileIn( __METHOD__ );
175
176 $this->dataLoaded = true;
177 $dbr = $this->repo->getSlaveDB();
178 $conds = array( 'oi_name' => $this->getName() );
179 if ( is_null( $this->requestedTime ) ) {
180 $conds['oi_archive_name'] = $this->archive_name;
181 } else {
182 $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime );
183 }
184 $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
185 $conds, __METHOD__, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
186 if ( $row ) {
187 $this->loadFromRow( $row, 'oi_' );
188 } else {
189 $this->fileExists = false;
190 }
191
192 wfProfileOut( __METHOD__ );
193 }
194
195 /**
196 * Load lazy file metadata from the DB
197 */
198 protected function loadExtraFromDB() {
199 wfProfileIn( __METHOD__ );
200
201 $this->extraDataLoaded = true;
202 $dbr = $this->repo->getSlaveDB();
203 $conds = array( 'oi_name' => $this->getName() );
204 if ( is_null( $this->requestedTime ) ) {
205 $conds['oi_archive_name'] = $this->archive_name;
206 } else {
207 $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime );
208 }
209 // In theory the file could have just been renamed/deleted...oh well
210 $row = $dbr->selectRow( 'oldimage', $this->getLazyCacheFields( 'oi_' ),
211 $conds, __METHOD__, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
212
213 if ( !$row ) { // fallback to master
214 $dbr = $this->repo->getMasterDB();
215 $row = $dbr->selectRow( 'oldimage', $this->getLazyCacheFields( 'oi_' ),
216 $conds, __METHOD__, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
217 }
218
219 if ( $row ) {
220 foreach ( $this->unprefixRow( $row, 'oi_' ) as $name => $value ) {
221 $this->$name = $value;
222 }
223 } else {
224 wfProfileOut( __METHOD__ );
225 throw new MWException( "Could not find data for image '{$this->archive_name}'." );
226 }
227
228 wfProfileOut( __METHOD__ );
229 }
230
231 /**
232 * @param $prefix string
233 * @return array
234 */
235 function getCacheFields( $prefix = 'img_' ) {
236 $fields = parent::getCacheFields( $prefix );
237 $fields[] = $prefix . 'archive_name';
238 $fields[] = $prefix . 'deleted';
239
240 return $fields;
241 }
242
243 /**
244 * @return string
245 */
246 function getRel() {
247 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
248 }
249
250 /**
251 * @return string
252 */
253 function getUrlRel() {
254 return 'archive/' . $this->getHashPath() . rawurlencode( $this->getArchiveName() );
255 }
256
257 function upgradeRow() {
258 wfProfileIn( __METHOD__ );
259 $this->loadFromFile();
260
261 # Don't destroy file info of missing files
262 if ( !$this->fileExists ) {
263 wfDebug( __METHOD__ . ": file does not exist, aborting\n" );
264 wfProfileOut( __METHOD__ );
265
266 return;
267 }
268
269 $dbw = $this->repo->getMasterDB();
270 list( $major, $minor ) = self::splitMime( $this->mime );
271
272 wfDebug( __METHOD__ . ': upgrading ' . $this->archive_name . " to the current schema\n" );
273 $dbw->update( 'oldimage',
274 array(
275 'oi_size' => $this->size, // sanity
276 'oi_width' => $this->width,
277 'oi_height' => $this->height,
278 'oi_bits' => $this->bits,
279 'oi_media_type' => $this->media_type,
280 'oi_major_mime' => $major,
281 'oi_minor_mime' => $minor,
282 'oi_metadata' => $this->metadata,
283 'oi_sha1' => $this->sha1,
284 ), array(
285 'oi_name' => $this->getName(),
286 'oi_archive_name' => $this->archive_name ),
287 __METHOD__
288 );
289 wfProfileOut( __METHOD__ );
290 }
291
292 /**
293 * @param $field Integer: one of DELETED_* bitfield constants
294 * for file or revision rows
295 * @return bool
296 */
297 function isDeleted( $field ) {
298 $this->load();
299
300 return ( $this->deleted & $field ) == $field;
301 }
302
303 /**
304 * Returns bitfield value
305 * @return int
306 */
307 function getVisibility() {
308 $this->load();
309
310 return (int)$this->deleted;
311 }
312
313 /**
314 * Determine if the current user is allowed to view a particular
315 * field of this image file, if it's marked as deleted.
316 *
317 * @param $field Integer
318 * @param $user User object to check, or null to use $wgUser
319 * @return bool
320 */
321 function userCan( $field, User $user = null ) {
322 $this->load();
323
324 return Revision::userCanBitfield( $this->deleted, $field, $user );
325 }
326
327 /**
328 * Upload a file directly into archive. Generally for Special:Import.
329 *
330 * @param string $srcPath File system path of the source file
331 * @param string $archiveName Full archive name of the file, in the form
332 * $timestamp!$filename, where $filename must match $this->getName()
333 *
334 * @param $timestamp string
335 * @param $comment string
336 * @param $user
337 * @param $flags int
338 * @return FileRepoStatus
339 */
340 function uploadOld( $srcPath, $archiveName, $timestamp, $comment, $user, $flags = 0 ) {
341 $this->lock();
342
343 $dstRel = 'archive/' . $this->getHashPath() . $archiveName;
344 $status = $this->publishTo( $srcPath, $dstRel,
345 $flags & File::DELETE_SOURCE ? FileRepo::DELETE_SOURCE : 0
346 );
347
348 if ( $status->isGood() ) {
349 if ( !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) ) {
350 $status->fatal( 'filenotfound', $srcPath );
351 }
352 }
353
354 $this->unlock();
355
356 return $status;
357 }
358
359 /**
360 * Record a file upload in the oldimage table, without adding log entries.
361 *
362 * @param string $srcPath File system path to the source file
363 * @param string $archiveName The archive name of the file
364 * @param $timestamp string
365 * @param string $comment Upload comment
366 * @param $user User User who did this upload
367 * @return bool
368 */
369 function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
370 $dbw = $this->repo->getMasterDB();
371 $dbw->begin( __METHOD__ );
372
373 $dstPath = $this->repo->getZonePath( 'public' ) . '/' . $this->getRel();
374 $props = $this->repo->getFileProps( $dstPath );
375 if ( !$props['fileExists'] ) {
376 return false;
377 }
378
379 $dbw->insert( 'oldimage',
380 array(
381 'oi_name' => $this->getName(),
382 'oi_archive_name' => $archiveName,
383 'oi_size' => $props['size'],
384 'oi_width' => intval( $props['width'] ),
385 'oi_height' => intval( $props['height'] ),
386 'oi_bits' => $props['bits'],
387 'oi_timestamp' => $dbw->timestamp( $timestamp ),
388 'oi_description' => $comment,
389 'oi_user' => $user->getId(),
390 'oi_user_text' => $user->getName(),
391 'oi_metadata' => $props['metadata'],
392 'oi_media_type' => $props['media_type'],
393 'oi_major_mime' => $props['major_mime'],
394 'oi_minor_mime' => $props['minor_mime'],
395 'oi_sha1' => $props['sha1'],
396 ), __METHOD__
397 );
398
399 $dbw->commit( __METHOD__ );
400
401 return true;
402 }
403 }