6833a3880ed91d378e771d066ca977f13b267d9b
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() {
129 ] + CommentStore
::newKey( 'oi_description' )->getFields();
133 * Return the tables, fields, and join conditions to be selected to create
134 * a new oldlocalfile object.
136 * @param string[] $options
137 * - omit-lazy: Omit fields that are lazily cached.
138 * @return array With three keys:
139 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
140 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
141 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
143 public static function getQueryInfo( array $options = [] ) {
144 $commentQuery = CommentStore
::newKey( 'oi_description' )->getJoin();
146 'tables' => [ 'oldimage' ] +
$commentQuery['tables'],
162 ] +
$commentQuery['fields'],
163 'joins' => $commentQuery['joins'],
166 if ( in_array( 'omit-nonlazy', $options, true ) ) {
167 // Internal use only for getting only the lazy fields
170 if ( !in_array( 'omit-lazy', $options, true ) ) {
171 // Note: Keep this in sync with self::getLazyCacheFields()
172 $ret['fields'][] = 'oi_metadata';
179 * @param Title $title
180 * @param FileRepo $repo
181 * @param string $time Timestamp or null to load by archive name
182 * @param string $archiveName Archive name or null to load by timestamp
183 * @throws MWException
185 function __construct( $title, $repo, $time, $archiveName ) {
186 parent
::__construct( $title, $repo );
187 $this->requestedTime
= $time;
188 $this->archive_name
= $archiveName;
189 if ( is_null( $time ) && is_null( $archiveName ) ) {
190 throw new MWException( __METHOD__
. ': must specify at least one of $time or $archiveName' );
197 function getCacheKey() {
204 function getArchiveName() {
205 if ( !isset( $this->archive_name
) ) {
209 return $this->archive_name
;
222 function isVisible() {
223 return $this->exists() && !$this->isDeleted( File
::DELETED_FILE
);
226 function loadFromDB( $flags = 0 ) {
227 $this->dataLoaded
= true;
229 $dbr = ( $flags & self
::READ_LATEST
)
230 ?
$this->repo
->getMasterDB()
231 : $this->repo
->getReplicaDB();
233 $conds = [ 'oi_name' => $this->getName() ];
234 if ( is_null( $this->requestedTime
) ) {
235 $conds['oi_archive_name'] = $this->archive_name
;
237 $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime
);
239 $fileQuery = static::getQueryInfo();
240 $row = $dbr->selectRow(
241 $fileQuery['tables'],
242 $fileQuery['fields'],
245 [ 'ORDER BY' => 'oi_timestamp DESC' ],
249 $this->loadFromRow( $row, 'oi_' );
251 $this->fileExists
= false;
256 * Load lazy file metadata from the DB
258 protected function loadExtraFromDB() {
259 $this->extraDataLoaded
= true;
260 $dbr = $this->repo
->getReplicaDB();
261 $conds = [ 'oi_name' => $this->getName() ];
262 if ( is_null( $this->requestedTime
) ) {
263 $conds['oi_archive_name'] = $this->archive_name
;
265 $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime
);
267 $fileQuery = static::getQueryInfo( [ 'omit-nonlazy' ] );
268 // In theory the file could have just been renamed/deleted...oh well
269 $row = $dbr->selectRow(
270 $fileQuery['tables'],
271 $fileQuery['fields'],
274 [ 'ORDER BY' => 'oi_timestamp DESC' ],
278 if ( !$row ) { // fallback to master
279 $dbr = $this->repo
->getMasterDB();
280 $row = $dbr->selectRow(
281 $fileQuery['tables'],
282 $fileQuery['fields'],
285 [ 'ORDER BY' => 'oi_timestamp DESC' ],
291 foreach ( $this->unprefixRow( $row, 'oi_' ) as $name => $value ) {
292 $this->$name = $value;
295 throw new MWException( "Could not find data for image '{$this->archive_name}'." );
300 protected function getCacheFields( $prefix = 'img_' ) {
301 $fields = parent
::getCacheFields( $prefix );
302 $fields[] = $prefix . 'archive_name';
303 $fields[] = $prefix . 'deleted';
312 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
318 function getUrlRel() {
319 return 'archive/' . $this->getHashPath() . rawurlencode( $this->getArchiveName() );
322 function upgradeRow() {
323 $this->loadFromFile();
325 # Don't destroy file info of missing files
326 if ( !$this->fileExists
) {
327 wfDebug( __METHOD__
. ": file does not exist, aborting\n" );
332 $dbw = $this->repo
->getMasterDB();
333 list( $major, $minor ) = self
::splitMime( $this->mime
);
335 wfDebug( __METHOD__
. ': upgrading ' . $this->archive_name
. " to the current schema\n" );
336 $dbw->update( 'oldimage',
338 'oi_size' => $this->size
, // sanity
339 'oi_width' => $this->width
,
340 'oi_height' => $this->height
,
341 'oi_bits' => $this->bits
,
342 'oi_media_type' => $this->media_type
,
343 'oi_major_mime' => $major,
344 'oi_minor_mime' => $minor,
345 'oi_metadata' => $this->metadata
,
346 'oi_sha1' => $this->sha1
,
348 'oi_name' => $this->getName(),
349 'oi_archive_name' => $this->archive_name
],
355 * @param int $field One of DELETED_* bitfield constants for file or
359 function isDeleted( $field ) {
362 return ( $this->deleted
& $field ) == $field;
366 * Returns bitfield value
369 function getVisibility() {
372 return (int)$this->deleted
;
376 * Determine if the current user is allowed to view a particular
377 * field of this image file, if it's marked as deleted.
380 * @param User|null $user User object to check, or null to use $wgUser
383 function userCan( $field, User
$user = null ) {
386 return Revision
::userCanBitfield( $this->deleted
, $field, $user );
390 * Upload a file directly into archive. Generally for Special:Import.
392 * @param string $srcPath File system path of the source file
393 * @param string $archiveName Full archive name of the file, in the form
394 * $timestamp!$filename, where $filename must match $this->getName()
395 * @param string $timestamp
396 * @param string $comment
400 function uploadOld( $srcPath, $archiveName, $timestamp, $comment, $user ) {
403 $dstRel = 'archive/' . $this->getHashPath() . $archiveName;
404 $status = $this->publishTo( $srcPath, $dstRel );
406 if ( $status->isGood() ) {
407 if ( !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) ) {
408 $status->fatal( 'filenotfound', $srcPath );
418 * Record a file upload in the oldimage table, without adding log entries.
420 * @param string $srcPath File system path to the source file
421 * @param string $archiveName The archive name of the file
422 * @param string $timestamp
423 * @param string $comment Upload comment
424 * @param User $user User who did this upload
427 protected function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
428 $dbw = $this->repo
->getMasterDB();
430 $dstPath = $this->repo
->getZonePath( 'public' ) . '/' . $this->getRel();
431 $props = $this->repo
->getFileProps( $dstPath );
432 if ( !$props['fileExists'] ) {
436 $commentFields = CommentStore
::newKey( 'oi_description' )->insert( $dbw, $comment );
437 $dbw->insert( 'oldimage',
439 'oi_name' => $this->getName(),
440 'oi_archive_name' => $archiveName,
441 'oi_size' => $props['size'],
442 'oi_width' => intval( $props['width'] ),
443 'oi_height' => intval( $props['height'] ),
444 'oi_bits' => $props['bits'],
445 'oi_timestamp' => $dbw->timestamp( $timestamp ),
446 'oi_user' => $user->getId(),
447 'oi_user_text' => $user->getName(),
448 'oi_metadata' => $props['metadata'],
449 'oi_media_type' => $props['media_type'],
450 'oi_major_mime' => $props['major_mime'],
451 'oi_minor_mime' => $props['minor_mime'],
452 'oi_sha1' => $props['sha1'],
453 ] +
$commentFields, __METHOD__
460 * If archive name is an empty string, then file does not "exist"
462 * This is the case for a couple files on Wikimedia servers where
463 * the old version is "lost".
466 public function exists() {
467 $archiveName = $this->getArchiveName();
468 if ( $archiveName === '' ||
!is_string( $archiveName ) ) {
471 return parent
::exists();