MediaWiki master
OldLocalFile.php
Go to the documentation of this file.
1<?php
32
39class OldLocalFile extends LocalFile {
41 protected $requestedTime;
42
44 protected $archive_name;
45
46 public const CACHE_VERSION = 1;
47
55 public static function newFromTitle( $title, $repo, $time = null ) {
56 # The null default value is only here to avoid an E_STRICT
57 if ( $time === null ) {
58 throw new InvalidArgumentException( __METHOD__ . ' got null for $time parameter' );
59 }
60
61 return new static( $title, $repo, $time, null );
62 }
63
72 public static function newFromArchiveName( $title, $repo, $archiveName ) {
73 return new static( $title, $repo, null, $archiveName );
74 }
75
83 public static function newFromRow( $row, $repo ) {
84 $title = Title::makeTitle( NS_FILE, $row->oi_name );
85 $file = new static( $title, $repo, null, $row->oi_archive_name );
86 $file->loadFromRow( $row, 'oi_' );
87
88 return $file;
89 }
90
103 public static function newFromKey( $sha1, $repo, $timestamp = false ) {
104 $dbr = $repo->getReplicaDB();
105 $queryBuilder = FileSelectQueryBuilder::newForOldFile( $dbr );
106
107 $queryBuilder->where( [ 'oi_sha1' => $sha1 ] );
108 if ( $timestamp ) {
109 $queryBuilder->andWhere( [ 'oi_timestamp' => $dbr->timestamp( $timestamp ) ] );
110 }
111
112 $row = $queryBuilder->caller( __METHOD__ )->fetchRow();
113 if ( $row ) {
114 return static::newFromRow( $row, $repo );
115 } else {
116 return false;
117 }
118 }
119
140 public static function getQueryInfo( array $options = [] ) {
141 $dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase();
142 $queryInfo = FileSelectQueryBuilder::newForOldFile( $dbr, $options )->getQueryInfo();
143 return [
144 'tables' => $queryInfo['tables'],
145 'fields' => $queryInfo['fields'],
146 'joins' => $queryInfo['join_conds'],
147 ];
148 }
149
158 public function __construct( $title, $repo, $time, $archiveName ) {
159 parent::__construct( $title, $repo );
160 $this->requestedTime = $time;
161 $this->archive_name = $archiveName;
162 if ( $time === null && $archiveName === null ) {
163 throw new LogicException( __METHOD__ . ': must specify at least one of $time or $archiveName' );
164 }
165 }
166
167 public function loadFromRow( $row, $prefix = 'img_' ) {
168 $this->archive_name = $row->{"{$prefix}archive_name"};
169 $this->deleted = $row->{"{$prefix}deleted"};
170 $row = clone $row;
171 unset( $row->{"{$prefix}archive_name"} );
172 unset( $row->{"{$prefix}deleted"} );
173 parent::loadFromRow( $row, $prefix );
174 }
175
180 protected function getCacheKey() {
181 return false;
182 }
183
188 public function getArchiveName() {
189 if ( $this->archive_name === null ) {
190 $this->load();
191 }
192
193 return $this->archive_name;
194 }
195
199 public function isOld() {
200 return true;
201 }
202
206 public function isVisible() {
207 return $this->exists() && !$this->isDeleted( File::DELETED_FILE );
208 }
209
214 protected function loadFromDB( $flags = 0 ) {
215 $this->dataLoaded = true;
216
217 $dbr = ( $flags & IDBAccessObject::READ_LATEST )
218 ? $this->repo->getPrimaryDB()
219 : $this->repo->getReplicaDB();
220 $queryBuilder = $this->buildQueryBuilderForLoad( $dbr, [] );
221 $row = $queryBuilder->caller( __METHOD__ )->fetchRow();
222 if ( $row ) {
223 $this->loadFromRow( $row, 'oi_' );
224 } else {
225 $this->fileExists = false;
226 }
227 }
228
233 protected function loadExtraFromDB() {
234 $this->extraDataLoaded = true;
235 $dbr = $this->repo->getReplicaDB();
236 $queryBuilder = $this->buildQueryBuilderForLoad( $dbr );
237
238 // In theory the file could have just been renamed/deleted...oh well
239 $row = $queryBuilder->caller( __METHOD__ )->fetchRow();
240
241 if ( !$row ) { // fallback to primary DB
242 $dbr = $this->repo->getPrimaryDB();
243 $queryBuilder = $this->buildQueryBuilderForLoad( $dbr );
244 $row = $queryBuilder->caller( __METHOD__ )->fetchRow();
245 }
246
247 if ( $row ) {
248 foreach ( $this->unprefixRow( $row, 'oi_' ) as $name => $value ) {
249 $this->$name = $value;
250 }
251 } else {
252 throw new RuntimeException( "Could not find data for image '{$this->archive_name}'." );
253 }
254 }
255
256 private function buildQueryBuilderForLoad( IReadableDatabase $dbr, $options = [ 'omit-nonlazy' ] ) {
257 $queryBuilder = FileSelectQueryBuilder::newForOldFile( $dbr, $options );
258 $queryBuilder->where( [ 'oi_name' => $this->getName() ] )
259 ->orderBy( 'oi_timestamp', SelectQueryBuilder::SORT_DESC );
260 if ( $this->requestedTime === null ) {
261 $queryBuilder->andWhere( [ 'oi_archive_name' => $this->archive_name ] );
262 } else {
263 $queryBuilder->andWhere( [ 'oi_timestamp' => $dbr->timestamp( $this->requestedTime ) ] );
264 }
265 return $queryBuilder;
266 }
267
272 protected function getCacheFields( $prefix = 'img_' ) {
273 $fields = parent::getCacheFields( $prefix );
274 $fields[] = $prefix . 'archive_name';
275 $fields[] = $prefix . 'deleted';
276
277 return $fields;
278 }
279
284 public function getRel() {
285 return $this->getArchiveRel( $this->getArchiveName() );
286 }
287
292 public function getUrlRel() {
293 return $this->getArchiveRel( rawurlencode( $this->getArchiveName() ) );
294 }
295
299 public function upgradeRow() {
300 $this->loadFromFile();
301
302 # Don't destroy file info of missing files
303 if ( !$this->fileExists ) {
304 wfDebug( __METHOD__ . ": file does not exist, aborting" );
305
306 return;
307 }
308
309 $dbw = $this->repo->getPrimaryDB();
310 [ $major, $minor ] = self::splitMime( $this->mime );
311 $metadata = $this->getMetadataForDb( $dbw );
312
313 wfDebug( __METHOD__ . ': upgrading ' . $this->archive_name . " to the current schema" );
314 $dbw->newUpdateQueryBuilder()
315 ->update( 'oldimage' )
316 ->set( [
317 'oi_size' => $this->size,
318 'oi_width' => $this->width,
319 'oi_height' => $this->height,
320 'oi_bits' => $this->bits,
321 'oi_media_type' => $this->media_type,
322 'oi_major_mime' => $major,
323 'oi_minor_mime' => $minor,
324 'oi_metadata' => $metadata,
325 'oi_sha1' => $this->sha1,
326 ] )
327 ->where( [
328 'oi_name' => $this->getName(),
329 'oi_archive_name' => $this->archive_name,
330 ] )
331 ->caller( __METHOD__ )->execute();
332
333 $migrationStage = MediaWikiServices::getInstance()->getMainConfig()->get(
334 MainConfigNames::FileSchemaMigrationStage
335 );
336 if ( $migrationStage & SCHEMA_COMPAT_WRITE_NEW ) {
337 $dbw->newUpdateQueryBuilder()
338 ->update( 'filerevision' )
339 ->set( [
340 'fr_size' => $this->size,
341 'fr_width' => $this->width,
342 'fr_height' => $this->height,
343 'fr_bits' => $this->bits,
344 'fr_metadata' => $metadata,
345 'fr_sha1' => $this->sha1,
346 ] )
347 ->where( [
348 'fr_file' => $this->acquireFileIdFromName(),
349 'fr_archive_name' => $this->archive_name,
350 ] )
351 ->caller( __METHOD__ )->execute();
352 }
353 }
354
355 protected function reserializeMetadata() {
356 // TODO: implement this and make it possible to hit it from refreshImageMetadata.php
357 // It can be hit from action=purge but that's not very useful if the
358 // goal is to reserialize the whole oldimage table.
359 }
360
366 public function isDeleted( $field ) {
367 $this->load();
368
369 return ( $this->deleted & $field ) == $field;
370 }
371
376 public function getVisibility() {
377 $this->load();
378
379 return (int)$this->deleted;
380 }
381
390 public function userCan( $field, Authority $performer ) {
391 $this->load();
392
393 return RevisionRecord::userCanBitfield(
394 $this->deleted,
395 $field,
396 $performer
397 );
398 }
399
409 public function uploadOld( $srcPath, $timestamp, $comment, UserIdentity $user ) {
410 $archiveName = $this->getArchiveName();
411 $dstRel = $this->getArchiveRel( $archiveName );
412 $status = $this->publishTo( $srcPath, $dstRel );
413
414 if ( $status->isGood() &&
415 !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user )
416 ) {
417 $status->fatal( 'filenotfound', $srcPath );
418 }
419
420 return $status;
421 }
422
434 protected function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
435 $dbw = $this->repo->getPrimaryDB();
436
437 $services = MediaWikiServices::getInstance();
438 $mwProps = new MWFileProps( $services->getMimeAnalyzer() );
439 $props = $mwProps->getPropsFromPath( $srcPath, true );
440 if ( !$props['fileExists'] ) {
441 return false;
442 }
443 $this->setProps( $props );
444
445 $dbw->startAtomic( __METHOD__ );
446
447 $commentFields = $services->getCommentStore()
448 ->insert( $dbw, 'oi_description', $comment );
449 $actorId = $services->getActorNormalization()
450 ->acquireActorId( $user, $dbw );
451 $dbw->newInsertQueryBuilder()
452 ->insertInto( 'oldimage' )
453 ->row( [
454 'oi_name' => $this->getName(),
455 'oi_archive_name' => $archiveName,
456 'oi_size' => $props['size'],
457 'oi_width' => intval( $props['width'] ),
458 'oi_height' => intval( $props['height'] ),
459 'oi_bits' => $props['bits'],
460 'oi_actor' => $actorId,
461 'oi_timestamp' => $dbw->timestamp( $timestamp ),
462 'oi_metadata' => $this->getMetadataForDb( $dbw ),
463 'oi_media_type' => $props['media_type'],
464 'oi_major_mime' => $props['major_mime'],
465 'oi_minor_mime' => $props['minor_mime'],
466 'oi_sha1' => $props['sha1'],
467 ] + $commentFields )
468 ->caller( __METHOD__ )->execute();
469
470 $migrationStage = MediaWikiServices::getInstance()->getMainConfig()->get(
471 MainConfigNames::FileSchemaMigrationStage
472 );
473 if ( $migrationStage & SCHEMA_COMPAT_WRITE_NEW ) {
474 $commentFields = $services->getCommentStore()
475 ->insert( $dbw, 'fr_description', $comment );
476 $dbw->newInsertQueryBuilder()
477 ->insertInto( 'filerevision' )
478 ->ignore()
479 ->row( [
480 'fr_file' => $this->acquireFileIdFromName(),
481 'fr_size' => $this->size,
482 'fr_width' => intval( $this->width ),
483 'fr_height' => intval( $this->height ),
484 'fr_bits' => $this->bits,
485 'fr_actor' => $actorId,
486 'fr_deleted' => 0,
487 'fr_timestamp' => $dbw->timestamp( $timestamp ),
488 'fr_metadata' => $this->getMetadataForDb( $dbw ),
489 'fr_sha1' => $this->sha1
490 ] + $commentFields )
491 ->caller( __METHOD__ )->execute();
492 }
493
494 $dbw->endAtomic( __METHOD__ );
495
496 return true;
497 }
498
506 public function exists() {
507 $archiveName = $this->getArchiveName();
508 if ( $archiveName === '' || !is_string( $archiveName ) ) {
509 return false;
510 }
511 return parent::exists();
512 }
513}
const NS_FILE
Definition Defines.php:71
const SCHEMA_COMPAT_WRITE_NEW
Definition Defines.php:286
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
FileRepo LocalRepo ForeignAPIRepo false $repo
Some member variables can be lazy-initialised using __get().
Definition File.php:126
Title string false $title
Definition File.php:129
const DELETED_FILE
Definition File.php:83
Local file in the wiki's own database.
Definition LocalFile.php:75
string $sha1
SHA-1 base 36 content hash.
MimeMagic helper wrapper.
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
Page revision base class.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
Represents a title within MediaWiki.
Definition Title.php:78
Old file in the oldimage table.
static newFromArchiveName( $title, $repo, $archiveName)
recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user)
Record a file upload in the oldimage table, without adding log entries.
string null $archive_name
Archive name.
uploadOld( $srcPath, $timestamp, $comment, UserIdentity $user)
Upload a file directly into archive.
static newFromRow( $row, $repo)
static getQueryInfo(array $options=[])
Return the tables, fields, and join conditions to be selected to create a new oldlocalfile object.
const CACHE_VERSION
__construct( $title, $repo, $time, $archiveName)
loadFromDB( $flags=0)
reserializeMetadata()
Write the metadata back to the database with the current serialization format.
static newFromKey( $sha1, $repo, $timestamp=false)
Create a OldLocalFile from a SHA-1 key Do not call this except from inside a repo class.
loadFromRow( $row, $prefix='img_')
Load file metadata from a DB result row.
getVisibility()
Returns bitfield value.
static newFromTitle( $title, $repo, $time=null)
exists()
If archive name is an empty string, then file does not "exist".
getCacheFields( $prefix='img_')
Returns the list of object properties that are included as-is in the cache.to override string[] 1....
userCan( $field, Authority $performer)
Determine if the current user is allowed to view a particular field of this image file,...
string int $requestedTime
Timestamp.
isDeleted( $field)
loadExtraFromDB()
Load lazy file metadata from the DB.
Build SELECT queries with a fluent interface.
This interface represents the authority associated with the current execution context,...
Definition Authority.php:37
Interface for objects representing user identity.
Interface for database access objects.
A database connection without write operations.
timestamp( $ts=0)
Convert a timestamp in one of the formats accepted by ConvertibleTimestamp to the format used for ins...