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
24 use MediaWiki\MediaWikiServices
;
27 * Class to represent a file in the oldimage table
29 * @ingroup FileAbstraction
31 class OldLocalFile
extends LocalFile
{
32 /** @var string|int Timestamp */
33 protected $requestedTime;
35 /** @var string Archive name */
36 protected $archive_name;
38 const CACHE_VERSION
= 1;
39 const MAX_CACHE_ROWS
= 20;
43 * @param FileRepo $repo
44 * @param string|int|null $time
48 static function newFromTitle( $title, $repo, $time = null ) {
49 # The null default value is only here to avoid an E_STRICT
50 if ( $time === null ) {
51 throw new MWException( __METHOD__
. ' got null for $time parameter' );
54 return new static( $title, $repo, $time, null );
59 * @param FileRepo $repo
60 * @param string $archiveName
63 static function newFromArchiveName( $title, $repo, $archiveName ) {
64 return new static( $title, $repo, null, $archiveName );
68 * @param stdClass $row
69 * @param FileRepo $repo
72 static function newFromRow( $row, $repo ) {
73 $title = Title
::makeTitle( NS_FILE
, $row->oi_name
);
74 $file = new static( $title, $repo, null, $row->oi_archive_name
);
75 $file->loadFromRow( $row, 'oi_' );
81 * Create a OldLocalFile from a SHA-1 key
82 * Do not call this except from inside a repo class.
84 * @param string $sha1 Base-36 SHA-1
85 * @param LocalRepo $repo
86 * @param string|bool $timestamp MW_timestamp (optional)
88 * @return bool|OldLocalFile
90 static function newFromKey( $sha1, $repo, $timestamp = false ) {
91 $dbr = $repo->getReplicaDB();
93 $conds = [ 'oi_sha1' => $sha1 ];
95 $conds['oi_timestamp'] = $dbr->timestamp( $timestamp );
98 $fileQuery = static::getQueryInfo();
99 $row = $dbr->selectRow(
100 $fileQuery['tables'], $fileQuery['fields'], $conds, __METHOD__
, [], $fileQuery['joins']
103 return static::newFromRow( $row, $repo );
110 * Return the tables, fields, and join conditions to be selected to create
111 * a new oldlocalfile object.
113 * @param string[] $options
114 * - omit-lazy: Omit fields that are lazily cached.
115 * @return array[] With three keys:
116 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
117 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
118 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
120 public static function getQueryInfo( array $options = [] ) {
121 $commentQuery = MediaWikiServices
::getInstance()->getCommentStore()->getJoin( 'oi_description' );
122 $actorQuery = ActorMigration
::newMigration()->getJoin( 'oi_user' );
124 'tables' => [ 'oldimage' ] +
$commentQuery['tables'] +
$actorQuery['tables'],
138 ] +
$commentQuery['fields'] +
$actorQuery['fields'],
139 'joins' => $commentQuery['joins'] +
$actorQuery['joins'],
142 if ( in_array( 'omit-nonlazy', $options, true ) ) {
143 // Internal use only for getting only the lazy fields
146 if ( !in_array( 'omit-lazy', $options, true ) ) {
147 // Note: Keep this in sync with self::getLazyCacheFields()
148 $ret['fields'][] = 'oi_metadata';
155 * @param Title $title
156 * @param FileRepo $repo
157 * @param string|int|null $time Timestamp or null to load by archive name
158 * @param string|null $archiveName Archive name or null to load by timestamp
159 * @throws MWException
161 function __construct( $title, $repo, $time, $archiveName ) {
162 parent
::__construct( $title, $repo );
163 $this->requestedTime
= $time;
164 $this->archive_name
= $archiveName;
165 if ( is_null( $time ) && is_null( $archiveName ) ) {
166 throw new MWException( __METHOD__
. ': must specify at least one of $time or $archiveName' );
173 function getCacheKey() {
180 function getArchiveName() {
181 if ( !isset( $this->archive_name
) ) {
185 return $this->archive_name
;
198 function isVisible() {
199 return $this->exists() && !$this->isDeleted( File
::DELETED_FILE
);
202 function loadFromDB( $flags = 0 ) {
203 $this->dataLoaded
= true;
205 $dbr = ( $flags & self
::READ_LATEST
)
206 ?
$this->repo
->getMasterDB()
207 : $this->repo
->getReplicaDB();
209 $conds = [ 'oi_name' => $this->getName() ];
210 if ( is_null( $this->requestedTime
) ) {
211 $conds['oi_archive_name'] = $this->archive_name
;
213 $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime
);
215 $fileQuery = static::getQueryInfo();
216 $row = $dbr->selectRow(
217 $fileQuery['tables'],
218 $fileQuery['fields'],
221 [ 'ORDER BY' => 'oi_timestamp DESC' ],
225 $this->loadFromRow( $row, 'oi_' );
227 $this->fileExists
= false;
232 * Load lazy file metadata from the DB
234 protected function loadExtraFromDB() {
235 $this->extraDataLoaded
= true;
236 $dbr = $this->repo
->getReplicaDB();
237 $conds = [ 'oi_name' => $this->getName() ];
238 if ( is_null( $this->requestedTime
) ) {
239 $conds['oi_archive_name'] = $this->archive_name
;
241 $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime
);
243 $fileQuery = static::getQueryInfo( [ 'omit-nonlazy' ] );
244 // In theory the file could have just been renamed/deleted...oh well
245 $row = $dbr->selectRow(
246 $fileQuery['tables'],
247 $fileQuery['fields'],
250 [ 'ORDER BY' => 'oi_timestamp DESC' ],
254 if ( !$row ) { // fallback to master
255 $dbr = $this->repo
->getMasterDB();
256 $row = $dbr->selectRow(
257 $fileQuery['tables'],
258 $fileQuery['fields'],
261 [ 'ORDER BY' => 'oi_timestamp DESC' ],
267 foreach ( $this->unprefixRow( $row, 'oi_' ) as $name => $value ) {
268 $this->$name = $value;
271 throw new MWException( "Could not find data for image '{$this->archive_name}'." );
276 protected function getCacheFields( $prefix = 'img_' ) {
277 $fields = parent
::getCacheFields( $prefix );
278 $fields[] = $prefix . 'archive_name';
279 $fields[] = $prefix . 'deleted';
288 return $this->getArchiveRel( $this->getArchiveName() );
294 function getUrlRel() {
295 return $this->getArchiveRel( rawurlencode( $this->getArchiveName() ) );
298 function upgradeRow() {
299 $this->loadFromFile();
301 # Don't destroy file info of missing files
302 if ( !$this->fileExists
) {
303 wfDebug( __METHOD__
. ": file does not exist, aborting\n" );
308 $dbw = $this->repo
->getMasterDB();
309 list( $major, $minor ) = self
::splitMime( $this->mime
);
311 wfDebug( __METHOD__
. ': upgrading ' . $this->archive_name
. " to the current schema\n" );
312 $dbw->update( 'oldimage',
314 'oi_size' => $this->size
, // sanity
315 'oi_width' => $this->width
,
316 'oi_height' => $this->height
,
317 'oi_bits' => $this->bits
,
318 'oi_media_type' => $this->media_type
,
319 'oi_major_mime' => $major,
320 'oi_minor_mime' => $minor,
321 'oi_metadata' => $this->metadata
,
322 'oi_sha1' => $this->sha1
,
324 'oi_name' => $this->getName(),
325 'oi_archive_name' => $this->archive_name
],
331 * @param int $field One of DELETED_* bitfield constants for file or
335 function isDeleted( $field ) {
338 return ( $this->deleted
& $field ) == $field;
342 * Returns bitfield value
345 function getVisibility() {
348 return (int)$this->deleted
;
352 * Determine if the current user is allowed to view a particular
353 * field of this image file, if it's marked as deleted.
356 * @param User|null $user User object to check, or null to use $wgUser
359 function userCan( $field, User
$user = null ) {
362 return Revision
::userCanBitfield( $this->deleted
, $field, $user );
366 * Upload a file directly into archive. Generally for Special:Import.
368 * @param string $srcPath File system path of the source file
369 * @param string $timestamp
370 * @param string $comment
374 public function uploadOld( $srcPath, $timestamp, $comment, $user ) {
377 $archiveName = $this->getArchiveName();
378 $dstRel = $this->getArchiveRel( $archiveName );
379 $status = $this->publishTo( $srcPath, $dstRel );
381 if ( $status->isGood() &&
382 !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user )
384 $status->fatal( 'filenotfound', $srcPath );
393 * Record a file upload in the oldimage table, without adding log entries.
395 * @param string $srcPath File system path to the source file
396 * @param string $archiveName The archive name of the file
397 * @param string $timestamp
398 * @param string $comment Upload comment
399 * @param User $user User who did this upload
402 protected function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
403 $dbw = $this->repo
->getMasterDB();
405 $dstPath = $this->repo
->getZonePath( 'public' ) . '/' . $this->getRel();
406 $props = $this->repo
->getFileProps( $dstPath );
407 if ( !$props['fileExists'] ) {
411 $commentFields = MediaWikiServices
::getInstance()->getCommentStore()
412 ->insert( $dbw, 'oi_description', $comment );
413 $actorFields = ActorMigration
::newMigration()->getInsertValues( $dbw, 'oi_user', $user );
414 $dbw->insert( 'oldimage',
416 'oi_name' => $this->getName(),
417 'oi_archive_name' => $archiveName,
418 'oi_size' => $props['size'],
419 'oi_width' => intval( $props['width'] ),
420 'oi_height' => intval( $props['height'] ),
421 'oi_bits' => $props['bits'],
422 'oi_timestamp' => $dbw->timestamp( $timestamp ),
423 'oi_metadata' => $props['metadata'],
424 'oi_media_type' => $props['media_type'],
425 'oi_major_mime' => $props['major_mime'],
426 'oi_minor_mime' => $props['minor_mime'],
427 'oi_sha1' => $props['sha1'],
428 ] +
$commentFields +
$actorFields, __METHOD__
435 * If archive name is an empty string, then file does not "exist"
437 * This is the case for a couple files on Wikimedia servers where
438 * the old version is "lost".
441 public function exists() {
442 $archiveName = $this->getArchiveName();
443 if ( $archiveName === '' ||
!is_string( $archiveName ) ) {
446 return parent
::exists();