Merge "Fix sessionfailure i18n message during authentication"
[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 string Timestamp */
31 protected $requestedTime;
32
33 /** @var string Archive name */
34 protected $archive_name;
35
36 const CACHE_VERSION = 1;
37 const MAX_CACHE_ROWS = 20;
38
39 /**
40 * @param Title $title
41 * @param FileRepo $repo
42 * @param null|int $time Timestamp or null
43 * @return OldLocalFile
44 * @throws MWException
45 */
46 static function newFromTitle( $title, $repo, $time = null ) {
47 # The null default value is only here to avoid an E_STRICT
48 if ( $time === null ) {
49 throw new MWException( __METHOD__ . ' got null for $time parameter' );
50 }
51
52 return new self( $title, $repo, $time, null );
53 }
54
55 /**
56 * @param Title $title
57 * @param FileRepo $repo
58 * @param string $archiveName
59 * @return OldLocalFile
60 */
61 static function newFromArchiveName( $title, $repo, $archiveName ) {
62 return new self( $title, $repo, null, $archiveName );
63 }
64
65 /**
66 * @param stdClass $row
67 * @param FileRepo $repo
68 * @return OldLocalFile
69 */
70 static function newFromRow( $row, $repo ) {
71 $title = Title::makeTitle( NS_FILE, $row->oi_name );
72 $file = new self( $title, $repo, null, $row->oi_archive_name );
73 $file->loadFromRow( $row, 'oi_' );
74
75 return $file;
76 }
77
78 /**
79 * Create a OldLocalFile from a SHA-1 key
80 * Do not call this except from inside a repo class.
81 *
82 * @param string $sha1 Base-36 SHA-1
83 * @param LocalRepo $repo
84 * @param string|bool $timestamp MW_timestamp (optional)
85 *
86 * @return bool|OldLocalFile
87 */
88 static function newFromKey( $sha1, $repo, $timestamp = false ) {
89 $dbr = $repo->getReplicaDB();
90
91 $conds = [ 'oi_sha1' => $sha1 ];
92 if ( $timestamp ) {
93 $conds['oi_timestamp'] = $dbr->timestamp( $timestamp );
94 }
95
96 $fileQuery = self::getQueryInfo();
97 $row = $dbr->selectRow(
98 $fileQuery['tables'], $fileQuery['fields'], $conds, __METHOD__, [], $fileQuery['joins']
99 );
100 if ( $row ) {
101 return self::newFromRow( $row, $repo );
102 } else {
103 return false;
104 }
105 }
106
107 /**
108 * Fields in the oldimage table
109 * @deprecated since 1.31, use self::getQueryInfo() instead.
110 * @return array
111 */
112 static function selectFields() {
113 wfDeprecated( __METHOD__, '1.31' );
114 return [
115 'oi_name',
116 'oi_archive_name',
117 'oi_size',
118 'oi_width',
119 'oi_height',
120 'oi_metadata',
121 'oi_bits',
122 'oi_media_type',
123 'oi_major_mime',
124 'oi_minor_mime',
125 'oi_user',
126 'oi_user_text',
127 'oi_timestamp',
128 'oi_deleted',
129 'oi_sha1',
130 ] + CommentStore::getStore()->getFields( 'oi_description' );
131 }
132
133 /**
134 * Return the tables, fields, and join conditions to be selected to create
135 * a new oldlocalfile object.
136 * @since 1.31
137 * @param string[] $options
138 * - omit-lazy: Omit fields that are lazily cached.
139 * @return array With three keys:
140 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
141 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
142 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
143 */
144 public static function getQueryInfo( array $options = [] ) {
145 $commentQuery = CommentStore::getStore()->getJoin( 'oi_description' );
146 $ret = [
147 'tables' => [ 'oldimage' ] + $commentQuery['tables'],
148 'fields' => [
149 'oi_name',
150 'oi_archive_name',
151 'oi_size',
152 'oi_width',
153 'oi_height',
154 'oi_bits',
155 'oi_media_type',
156 'oi_major_mime',
157 'oi_minor_mime',
158 'oi_user',
159 'oi_user_text',
160 'oi_timestamp',
161 'oi_deleted',
162 'oi_sha1',
163 ] + $commentQuery['fields'],
164 'joins' => $commentQuery['joins'],
165 ];
166
167 if ( in_array( 'omit-nonlazy', $options, true ) ) {
168 // Internal use only for getting only the lazy fields
169 $ret['fields'] = [];
170 }
171 if ( !in_array( 'omit-lazy', $options, true ) ) {
172 // Note: Keep this in sync with self::getLazyCacheFields()
173 $ret['fields'][] = 'oi_metadata';
174 }
175
176 return $ret;
177 }
178
179 /**
180 * @param Title $title
181 * @param FileRepo $repo
182 * @param string $time Timestamp or null to load by archive name
183 * @param string $archiveName Archive name or null to load by timestamp
184 * @throws MWException
185 */
186 function __construct( $title, $repo, $time, $archiveName ) {
187 parent::__construct( $title, $repo );
188 $this->requestedTime = $time;
189 $this->archive_name = $archiveName;
190 if ( is_null( $time ) && is_null( $archiveName ) ) {
191 throw new MWException( __METHOD__ . ': must specify at least one of $time or $archiveName' );
192 }
193 }
194
195 /**
196 * @return bool
197 */
198 function getCacheKey() {
199 return false;
200 }
201
202 /**
203 * @return string
204 */
205 function getArchiveName() {
206 if ( !isset( $this->archive_name ) ) {
207 $this->load();
208 }
209
210 return $this->archive_name;
211 }
212
213 /**
214 * @return bool
215 */
216 function isOld() {
217 return true;
218 }
219
220 /**
221 * @return bool
222 */
223 function isVisible() {
224 return $this->exists() && !$this->isDeleted( File::DELETED_FILE );
225 }
226
227 function loadFromDB( $flags = 0 ) {
228 $this->dataLoaded = true;
229
230 $dbr = ( $flags & self::READ_LATEST )
231 ? $this->repo->getMasterDB()
232 : $this->repo->getReplicaDB();
233
234 $conds = [ 'oi_name' => $this->getName() ];
235 if ( is_null( $this->requestedTime ) ) {
236 $conds['oi_archive_name'] = $this->archive_name;
237 } else {
238 $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime );
239 }
240 $fileQuery = static::getQueryInfo();
241 $row = $dbr->selectRow(
242 $fileQuery['tables'],
243 $fileQuery['fields'],
244 $conds,
245 __METHOD__,
246 [ 'ORDER BY' => 'oi_timestamp DESC' ],
247 $fileQuery['joins']
248 );
249 if ( $row ) {
250 $this->loadFromRow( $row, 'oi_' );
251 } else {
252 $this->fileExists = false;
253 }
254 }
255
256 /**
257 * Load lazy file metadata from the DB
258 */
259 protected function loadExtraFromDB() {
260 $this->extraDataLoaded = true;
261 $dbr = $this->repo->getReplicaDB();
262 $conds = [ 'oi_name' => $this->getName() ];
263 if ( is_null( $this->requestedTime ) ) {
264 $conds['oi_archive_name'] = $this->archive_name;
265 } else {
266 $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime );
267 }
268 $fileQuery = static::getQueryInfo( [ 'omit-nonlazy' ] );
269 // In theory the file could have just been renamed/deleted...oh well
270 $row = $dbr->selectRow(
271 $fileQuery['tables'],
272 $fileQuery['fields'],
273 $conds,
274 __METHOD__,
275 [ 'ORDER BY' => 'oi_timestamp DESC' ],
276 $fileQuery['joins']
277 );
278
279 if ( !$row ) { // fallback to master
280 $dbr = $this->repo->getMasterDB();
281 $row = $dbr->selectRow(
282 $fileQuery['tables'],
283 $fileQuery['fields'],
284 $conds,
285 __METHOD__,
286 [ 'ORDER BY' => 'oi_timestamp DESC' ],
287 $fileQuery['joins']
288 );
289 }
290
291 if ( $row ) {
292 foreach ( $this->unprefixRow( $row, 'oi_' ) as $name => $value ) {
293 $this->$name = $value;
294 }
295 } else {
296 throw new MWException( "Could not find data for image '{$this->archive_name}'." );
297 }
298 }
299
300 /** @inheritDoc */
301 protected function getCacheFields( $prefix = 'img_' ) {
302 $fields = parent::getCacheFields( $prefix );
303 $fields[] = $prefix . 'archive_name';
304 $fields[] = $prefix . 'deleted';
305
306 return $fields;
307 }
308
309 /**
310 * @return string
311 */
312 function getRel() {
313 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
314 }
315
316 /**
317 * @return string
318 */
319 function getUrlRel() {
320 return 'archive/' . $this->getHashPath() . rawurlencode( $this->getArchiveName() );
321 }
322
323 function upgradeRow() {
324 $this->loadFromFile();
325
326 # Don't destroy file info of missing files
327 if ( !$this->fileExists ) {
328 wfDebug( __METHOD__ . ": file does not exist, aborting\n" );
329
330 return;
331 }
332
333 $dbw = $this->repo->getMasterDB();
334 list( $major, $minor ) = self::splitMime( $this->mime );
335
336 wfDebug( __METHOD__ . ': upgrading ' . $this->archive_name . " to the current schema\n" );
337 $dbw->update( 'oldimage',
338 [
339 'oi_size' => $this->size, // sanity
340 'oi_width' => $this->width,
341 'oi_height' => $this->height,
342 'oi_bits' => $this->bits,
343 'oi_media_type' => $this->media_type,
344 'oi_major_mime' => $major,
345 'oi_minor_mime' => $minor,
346 'oi_metadata' => $this->metadata,
347 'oi_sha1' => $this->sha1,
348 ], [
349 'oi_name' => $this->getName(),
350 'oi_archive_name' => $this->archive_name ],
351 __METHOD__
352 );
353 }
354
355 /**
356 * @param int $field One of DELETED_* bitfield constants for file or
357 * revision rows
358 * @return bool
359 */
360 function isDeleted( $field ) {
361 $this->load();
362
363 return ( $this->deleted & $field ) == $field;
364 }
365
366 /**
367 * Returns bitfield value
368 * @return int
369 */
370 function getVisibility() {
371 $this->load();
372
373 return (int)$this->deleted;
374 }
375
376 /**
377 * Determine if the current user is allowed to view a particular
378 * field of this image file, if it's marked as deleted.
379 *
380 * @param int $field
381 * @param User|null $user User object to check, or null to use $wgUser
382 * @return bool
383 */
384 function userCan( $field, User $user = null ) {
385 $this->load();
386
387 return Revision::userCanBitfield( $this->deleted, $field, $user );
388 }
389
390 /**
391 * Upload a file directly into archive. Generally for Special:Import.
392 *
393 * @param string $srcPath File system path of the source file
394 * @param string $archiveName Full archive name of the file, in the form
395 * $timestamp!$filename, where $filename must match $this->getName()
396 * @param string $timestamp
397 * @param string $comment
398 * @param User $user
399 * @return Status
400 */
401 function uploadOld( $srcPath, $archiveName, $timestamp, $comment, $user ) {
402 $this->lock();
403
404 $dstRel = 'archive/' . $this->getHashPath() . $archiveName;
405 $status = $this->publishTo( $srcPath, $dstRel );
406
407 if ( $status->isGood() ) {
408 if ( !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) ) {
409 $status->fatal( 'filenotfound', $srcPath );
410 }
411 }
412
413 $this->unlock();
414
415 return $status;
416 }
417
418 /**
419 * Record a file upload in the oldimage table, without adding log entries.
420 *
421 * @param string $srcPath File system path to the source file
422 * @param string $archiveName The archive name of the file
423 * @param string $timestamp
424 * @param string $comment Upload comment
425 * @param User $user User who did this upload
426 * @return bool
427 */
428 protected function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
429 $dbw = $this->repo->getMasterDB();
430
431 $dstPath = $this->repo->getZonePath( 'public' ) . '/' . $this->getRel();
432 $props = $this->repo->getFileProps( $dstPath );
433 if ( !$props['fileExists'] ) {
434 return false;
435 }
436
437 $commentFields = CommentStore::getStore()->insert( $dbw, 'oi_description', $comment );
438 $dbw->insert( 'oldimage',
439 [
440 'oi_name' => $this->getName(),
441 'oi_archive_name' => $archiveName,
442 'oi_size' => $props['size'],
443 'oi_width' => intval( $props['width'] ),
444 'oi_height' => intval( $props['height'] ),
445 'oi_bits' => $props['bits'],
446 'oi_timestamp' => $dbw->timestamp( $timestamp ),
447 'oi_user' => $user->getId(),
448 'oi_user_text' => $user->getName(),
449 'oi_metadata' => $props['metadata'],
450 'oi_media_type' => $props['media_type'],
451 'oi_major_mime' => $props['major_mime'],
452 'oi_minor_mime' => $props['minor_mime'],
453 'oi_sha1' => $props['sha1'],
454 ] + $commentFields, __METHOD__
455 );
456
457 return true;
458 }
459
460 /**
461 * If archive name is an empty string, then file does not "exist"
462 *
463 * This is the case for a couple files on Wikimedia servers where
464 * the old version is "lost".
465 * @return bool
466 */
467 public function exists() {
468 $archiveName = $this->getArchiveName();
469 if ( $archiveName === '' || !is_string( $archiveName ) ) {
470 return false;
471 }
472 return parent::exists();
473 }
474 }