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 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 null|int $time Timestamp or null
43 * @return OldLocalFile
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
59 * @return OldLocalFile
61 static function newFromArchiveName( $title, $repo, $archiveName ) {
62 return new self( $title, $repo, null, $archiveName );
66 * @param stdClass $row
67 * @param FileRepo $repo
68 * @return OldLocalFile
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 wfDeprecated( __METHOD__
, '1.31' );
130 ] + CommentStore
::newKey( 'oi_description' )->getFields();
134 * Return the tables, fields, and join conditions to be selected to create
135 * a new oldlocalfile object.
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()`
144 public static function getQueryInfo( array $options = [] ) {
145 $commentQuery = CommentStore
::newKey( 'oi_description' )->getJoin();
147 'tables' => [ 'oldimage' ] +
$commentQuery['tables'],
163 ] +
$commentQuery['fields'],
164 'joins' => $commentQuery['joins'],
167 if ( in_array( 'omit-nonlazy', $options, true ) ) {
168 // Internal use only for getting only the lazy fields
171 if ( !in_array( 'omit-lazy', $options, true ) ) {
172 // Note: Keep this in sync with self::getLazyCacheFields()
173 $ret['fields'][] = 'oi_metadata';
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
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' );
198 function getCacheKey() {
205 function getArchiveName() {
206 if ( !isset( $this->archive_name
) ) {
210 return $this->archive_name
;
223 function isVisible() {
224 return $this->exists() && !$this->isDeleted( File
::DELETED_FILE
);
227 function loadFromDB( $flags = 0 ) {
228 $this->dataLoaded
= true;
230 $dbr = ( $flags & self
::READ_LATEST
)
231 ?
$this->repo
->getMasterDB()
232 : $this->repo
->getReplicaDB();
234 $conds = [ 'oi_name' => $this->getName() ];
235 if ( is_null( $this->requestedTime
) ) {
236 $conds['oi_archive_name'] = $this->archive_name
;
238 $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime
);
240 $fileQuery = static::getQueryInfo();
241 $row = $dbr->selectRow(
242 $fileQuery['tables'],
243 $fileQuery['fields'],
246 [ 'ORDER BY' => 'oi_timestamp DESC' ],
250 $this->loadFromRow( $row, 'oi_' );
252 $this->fileExists
= false;
257 * Load lazy file metadata from the DB
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
;
266 $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime
);
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'],
275 [ 'ORDER BY' => 'oi_timestamp DESC' ],
279 if ( !$row ) { // fallback to master
280 $dbr = $this->repo
->getMasterDB();
281 $row = $dbr->selectRow(
282 $fileQuery['tables'],
283 $fileQuery['fields'],
286 [ 'ORDER BY' => 'oi_timestamp DESC' ],
292 foreach ( $this->unprefixRow( $row, 'oi_' ) as $name => $value ) {
293 $this->$name = $value;
296 throw new MWException( "Could not find data for image '{$this->archive_name}'." );
301 protected function getCacheFields( $prefix = 'img_' ) {
302 $fields = parent
::getCacheFields( $prefix );
303 $fields[] = $prefix . 'archive_name';
304 $fields[] = $prefix . 'deleted';
313 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
319 function getUrlRel() {
320 return 'archive/' . $this->getHashPath() . rawurlencode( $this->getArchiveName() );
323 function upgradeRow() {
324 $this->loadFromFile();
326 # Don't destroy file info of missing files
327 if ( !$this->fileExists
) {
328 wfDebug( __METHOD__
. ": file does not exist, aborting\n" );
333 $dbw = $this->repo
->getMasterDB();
334 list( $major, $minor ) = self
::splitMime( $this->mime
);
336 wfDebug( __METHOD__
. ': upgrading ' . $this->archive_name
. " to the current schema\n" );
337 $dbw->update( 'oldimage',
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
,
349 'oi_name' => $this->getName(),
350 'oi_archive_name' => $this->archive_name
],
356 * @param int $field One of DELETED_* bitfield constants for file or
360 function isDeleted( $field ) {
363 return ( $this->deleted
& $field ) == $field;
367 * Returns bitfield value
370 function getVisibility() {
373 return (int)$this->deleted
;
377 * Determine if the current user is allowed to view a particular
378 * field of this image file, if it's marked as deleted.
381 * @param User|null $user User object to check, or null to use $wgUser
384 function userCan( $field, User
$user = null ) {
387 return Revision
::userCanBitfield( $this->deleted
, $field, $user );
391 * Upload a file directly into archive. Generally for Special:Import.
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
401 function uploadOld( $srcPath, $archiveName, $timestamp, $comment, $user ) {
404 $dstRel = 'archive/' . $this->getHashPath() . $archiveName;
405 $status = $this->publishTo( $srcPath, $dstRel );
407 if ( $status->isGood() ) {
408 if ( !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) ) {
409 $status->fatal( 'filenotfound', $srcPath );
419 * Record a file upload in the oldimage table, without adding log entries.
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
428 protected function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
429 $dbw = $this->repo
->getMasterDB();
431 $dstPath = $this->repo
->getZonePath( 'public' ) . '/' . $this->getRel();
432 $props = $this->repo
->getFileProps( $dstPath );
433 if ( !$props['fileExists'] ) {
437 $commentFields = CommentStore
::newKey( 'oi_description' )->insert( $dbw, $comment );
438 $dbw->insert( 'oldimage',
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__
461 * If archive name is an empty string, then file does not "exist"
463 * This is the case for a couple files on Wikimedia servers where
464 * the old version is "lost".
467 public function exists() {
468 $archiveName = $this->getArchiveName();
469 if ( $archiveName === '' ||
!is_string( $archiveName ) ) {
472 return parent
::exists();