3 * Old file in the oldimage table.
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.
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.
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
21 * @ingroup FileAbstraction
25 * Class to represent a file in the oldimage table
27 * @ingroup FileAbstraction
29 class OldLocalFile
extends LocalFile
{
30 /** @var string|int Timestamp */
31 protected $requestedTime;
33 /** @var string Archive name */
34 protected $archive_name;
36 const CACHE_VERSION
= 1;
37 const MAX_CACHE_ROWS
= 20;
41 * @param FileRepo $repo
42 * @param string|int $time
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' );
52 return new self( $title, $repo, $time, null );
57 * @param FileRepo $repo
58 * @param string $archiveName
61 static function newFromArchiveName( $title, $repo, $archiveName ) {
62 return new self( $title, $repo, null, $archiveName );
66 * @param stdClass $row
67 * @param FileRepo $repo
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_' );
79 * Create a OldLocalFile from a SHA-1 key
80 * Do not call this except from inside a repo class.
82 * @param string $sha1 Base-36 SHA-1
83 * @param LocalRepo $repo
84 * @param string|bool $timestamp MW_timestamp (optional)
86 * @return bool|OldLocalFile
88 static function newFromKey( $sha1, $repo, $timestamp = false ) {
89 $dbr = $repo->getReplicaDB();
91 $conds = [ 'oi_sha1' => $sha1 ];
93 $conds['oi_timestamp'] = $dbr->timestamp( $timestamp );
96 $fileQuery = self
::getQueryInfo();
97 $row = $dbr->selectRow(
98 $fileQuery['tables'], $fileQuery['fields'], $conds, __METHOD__
, [], $fileQuery['joins']
101 return self
::newFromRow( $row, $repo );
108 * Fields in the oldimage table
109 * @deprecated since 1.31, use self::getQueryInfo() instead.
112 static function selectFields() {
113 global $wgActorTableSchemaMigrationStage;
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'
139 'oi_actor' => $wgActorTableSchemaMigrationStage > MIGRATION_OLD ?
'oi_actor' : null,
143 ] + CommentStore
::getStore()->getFields( 'oi_description' );
147 * Return the tables, fields, and join conditions to be selected to create
148 * a new oldlocalfile object.
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()`
157 public static function getQueryInfo( array $options = [] ) {
158 $commentQuery = CommentStore
::getStore()->getJoin( 'oi_description' );
159 $actorQuery = ActorMigration
::newMigration()->getJoin( 'oi_user' );
161 'tables' => [ 'oldimage' ] +
$commentQuery['tables'] +
$actorQuery['tables'],
175 ] +
$commentQuery['fields'] +
$actorQuery['fields'],
176 'joins' => $commentQuery['joins'] +
$actorQuery['joins'],
179 if ( in_array( 'omit-nonlazy', $options, true ) ) {
180 // Internal use only for getting only the lazy fields
183 if ( !in_array( 'omit-lazy', $options, true ) ) {
184 // Note: Keep this in sync with self::getLazyCacheFields()
185 $ret['fields'][] = 'oi_metadata';
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
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' );
210 function getCacheKey() {
217 function getArchiveName() {
218 if ( !isset( $this->archive_name
) ) {
222 return $this->archive_name
;
235 function isVisible() {
236 return $this->exists() && !$this->isDeleted( File
::DELETED_FILE
);
239 function loadFromDB( $flags = 0 ) {
240 $this->dataLoaded
= true;
242 $dbr = ( $flags & self
::READ_LATEST
)
243 ?
$this->repo
->getMasterDB()
244 : $this->repo
->getReplicaDB();
246 $conds = [ 'oi_name' => $this->getName() ];
247 if ( is_null( $this->requestedTime
) ) {
248 $conds['oi_archive_name'] = $this->archive_name
;
250 $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime
);
252 $fileQuery = static::getQueryInfo();
253 $row = $dbr->selectRow(
254 $fileQuery['tables'],
255 $fileQuery['fields'],
258 [ 'ORDER BY' => 'oi_timestamp DESC' ],
262 $this->loadFromRow( $row, 'oi_' );
264 $this->fileExists
= false;
269 * Load lazy file metadata from the DB
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
;
278 $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime
);
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'],
287 [ 'ORDER BY' => 'oi_timestamp DESC' ],
291 if ( !$row ) { // fallback to master
292 $dbr = $this->repo
->getMasterDB();
293 $row = $dbr->selectRow(
294 $fileQuery['tables'],
295 $fileQuery['fields'],
298 [ 'ORDER BY' => 'oi_timestamp DESC' ],
304 foreach ( $this->unprefixRow( $row, 'oi_' ) as $name => $value ) {
305 $this->$name = $value;
308 throw new MWException( "Could not find data for image '{$this->archive_name}'." );
313 protected function getCacheFields( $prefix = 'img_' ) {
314 $fields = parent
::getCacheFields( $prefix );
315 $fields[] = $prefix . 'archive_name';
316 $fields[] = $prefix . 'deleted';
325 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
331 function getUrlRel() {
332 return 'archive/' . $this->getHashPath() . rawurlencode( $this->getArchiveName() );
335 function upgradeRow() {
336 $this->loadFromFile();
338 # Don't destroy file info of missing files
339 if ( !$this->fileExists
) {
340 wfDebug( __METHOD__
. ": file does not exist, aborting\n" );
345 $dbw = $this->repo
->getMasterDB();
346 list( $major, $minor ) = self
::splitMime( $this->mime
);
348 wfDebug( __METHOD__
. ': upgrading ' . $this->archive_name
. " to the current schema\n" );
349 $dbw->update( 'oldimage',
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
,
361 'oi_name' => $this->getName(),
362 'oi_archive_name' => $this->archive_name
],
368 * @param int $field One of DELETED_* bitfield constants for file or
372 function isDeleted( $field ) {
375 return ( $this->deleted
& $field ) == $field;
379 * Returns bitfield value
382 function getVisibility() {
385 return (int)$this->deleted
;
389 * Determine if the current user is allowed to view a particular
390 * field of this image file, if it's marked as deleted.
393 * @param User|null $user User object to check, or null to use $wgUser
396 function userCan( $field, User
$user = null ) {
399 return Revision
::userCanBitfield( $this->deleted
, $field, $user );
403 * Upload a file directly into archive. Generally for Special:Import.
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
413 function uploadOld( $srcPath, $archiveName, $timestamp, $comment, $user ) {
416 $dstRel = 'archive/' . $this->getHashPath() . $archiveName;
417 $status = $this->publishTo( $srcPath, $dstRel );
419 if ( $status->isGood() ) {
420 if ( !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) ) {
421 $status->fatal( 'filenotfound', $srcPath );
431 * Record a file upload in the oldimage table, without adding log entries.
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
440 protected function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
441 $dbw = $this->repo
->getMasterDB();
443 $dstPath = $this->repo
->getZonePath( 'public' ) . '/' . $this->getRel();
444 $props = $this->repo
->getFileProps( $dstPath );
445 if ( !$props['fileExists'] ) {
449 $commentFields = CommentStore
::getStore()->insert( $dbw, 'oi_description', $comment );
450 $actorFields = ActorMigration
::newMigration()->getInsertValues( $dbw, 'oi_user', $user );
451 $dbw->insert( 'oldimage',
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__
472 * If archive name is an empty string, then file does not "exist"
474 * This is the case for a couple files on Wikimedia servers where
475 * the old version is "lost".
478 public function exists() {
479 $archiveName = $this->getArchiveName();
480 if ( $archiveName === '' ||
!is_string( $archiveName ) ) {
483 return parent
::exists();