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