MediaWiki 1.43.1
OldLocalFile.php
Go to the documentation of this file.
1<?php
31
38class OldLocalFile extends LocalFile {
40 protected $requestedTime;
41
43 protected $archive_name;
44
45 public const CACHE_VERSION = 1;
46
54 public static function newFromTitle( $title, $repo, $time = null ) {
55 # The null default value is only here to avoid an E_STRICT
56 if ( $time === null ) {
57 throw new InvalidArgumentException( __METHOD__ . ' got null for $time parameter' );
58 }
59
60 return new static( $title, $repo, $time, null );
61 }
62
71 public static function newFromArchiveName( $title, $repo, $archiveName ) {
72 return new static( $title, $repo, null, $archiveName );
73 }
74
82 public static function newFromRow( $row, $repo ) {
83 $title = Title::makeTitle( NS_FILE, $row->oi_name );
84 $file = new static( $title, $repo, null, $row->oi_archive_name );
85 $file->loadFromRow( $row, 'oi_' );
86
87 return $file;
88 }
89
102 public static function newFromKey( $sha1, $repo, $timestamp = false ) {
103 $dbr = $repo->getReplicaDB();
104 $queryBuilder = FileSelectQueryBuilder::newForOldFile( $dbr );
105
106 $queryBuilder->where( [ 'oi_sha1' => $sha1 ] );
107 if ( $timestamp ) {
108 $queryBuilder->andWhere( [ 'oi_timestamp' => $dbr->timestamp( $timestamp ) ] );
109 }
110
111 $row = $queryBuilder->caller( __METHOD__ )->fetchRow();
112 if ( $row ) {
113 return static::newFromRow( $row, $repo );
114 } else {
115 return false;
116 }
117 }
118
139 public static function getQueryInfo( array $options = [] ) {
140 $dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase();
141 $queryInfo = FileSelectQueryBuilder::newForOldFile( $dbr, $options )->getQueryInfo();
142 return [
143 'tables' => $queryInfo['tables'],
144 'fields' => $queryInfo['fields'],
145 'joins' => $queryInfo['join_conds'],
146 ];
147 }
148
157 public function __construct( $title, $repo, $time, $archiveName ) {
158 parent::__construct( $title, $repo );
159 $this->requestedTime = $time;
160 $this->archive_name = $archiveName;
161 if ( $time === null && $archiveName === null ) {
162 throw new LogicException( __METHOD__ . ': must specify at least one of $time or $archiveName' );
163 }
164 }
165
166 public function loadFromRow( $row, $prefix = 'img_' ) {
167 $this->archive_name = $row->{"{$prefix}archive_name"};
168 $this->deleted = $row->{"{$prefix}deleted"};
169 $row = clone $row;
170 unset( $row->{"{$prefix}archive_name"} );
171 unset( $row->{"{$prefix}deleted"} );
172 parent::loadFromRow( $row, $prefix );
173 }
174
179 protected function getCacheKey() {
180 return false;
181 }
182
187 public function getArchiveName() {
188 if ( !isset( $this->archive_name ) ) {
189 $this->load();
190 }
191
192 return $this->archive_name;
193 }
194
198 public function isOld() {
199 return true;
200 }
201
205 public function isVisible() {
206 return $this->exists() && !$this->isDeleted( File::DELETED_FILE );
207 }
208
213 protected function loadFromDB( $flags = 0 ) {
214 $this->dataLoaded = true;
215
216 $dbr = ( $flags & IDBAccessObject::READ_LATEST )
217 ? $this->repo->getPrimaryDB()
218 : $this->repo->getReplicaDB();
219 $queryBuilder = $this->buildQueryBuilderForLoad( $dbr, [] );
220 $row = $queryBuilder->caller( __METHOD__ )->fetchRow();
221 if ( $row ) {
222 $this->loadFromRow( $row, 'oi_' );
223 } else {
224 $this->fileExists = false;
225 }
226 }
227
232 protected function loadExtraFromDB() {
233 $this->extraDataLoaded = true;
234 $dbr = $this->repo->getReplicaDB();
235 $queryBuilder = $this->buildQueryBuilderForLoad( $dbr );
236
237 // In theory the file could have just been renamed/deleted...oh well
238 $row = $queryBuilder->caller( __METHOD__ )->fetchRow();
239
240 if ( !$row ) { // fallback to primary DB
241 $dbr = $this->repo->getPrimaryDB();
242 $queryBuilder = $this->buildQueryBuilderForLoad( $dbr );
243 $row = $queryBuilder->caller( __METHOD__ )->fetchRow();
244 }
245
246 if ( $row ) {
247 foreach ( $this->unprefixRow( $row, 'oi_' ) as $name => $value ) {
248 $this->$name = $value;
249 }
250 } else {
251 throw new RuntimeException( "Could not find data for image '{$this->archive_name}'." );
252 }
253 }
254
255 private function buildQueryBuilderForLoad( IReadableDatabase $dbr, $options = [ 'omit-nonlazy' ] ) {
256 $queryBuilder = FileSelectQueryBuilder::newForOldFile( $dbr, $options );
257 $queryBuilder->where( [ 'oi_name' => $this->getName() ] )
258 ->orderBy( 'oi_timestamp', SelectQueryBuilder::SORT_DESC );
259 if ( $this->requestedTime === null ) {
260 $queryBuilder->andWhere( [ 'oi_archive_name' => $this->archive_name ] );
261 } else {
262 $queryBuilder->andWhere( [ 'oi_timestamp' => $dbr->timestamp( $this->requestedTime ) ] );
263 }
264 return $queryBuilder;
265 }
266
271 protected function getCacheFields( $prefix = 'img_' ) {
272 $fields = parent::getCacheFields( $prefix );
273 $fields[] = $prefix . 'archive_name';
274 $fields[] = $prefix . 'deleted';
275
276 return $fields;
277 }
278
283 public function getRel() {
284 return $this->getArchiveRel( $this->getArchiveName() );
285 }
286
291 public function getUrlRel() {
292 return $this->getArchiveRel( rawurlencode( $this->getArchiveName() ) );
293 }
294
298 public function upgradeRow() {
299 $this->loadFromFile();
300
301 # Don't destroy file info of missing files
302 if ( !$this->fileExists ) {
303 wfDebug( __METHOD__ . ": file does not exist, aborting" );
304
305 return;
306 }
307
308 $dbw = $this->repo->getPrimaryDB();
309 [ $major, $minor ] = self::splitMime( $this->mime );
310
311 wfDebug( __METHOD__ . ': upgrading ' . $this->archive_name . " to the current schema" );
312 $dbw->newUpdateQueryBuilder()
313 ->update( 'oldimage' )
314 ->set( [
315 'oi_size' => $this->size,
316 'oi_width' => $this->width,
317 'oi_height' => $this->height,
318 'oi_bits' => $this->bits,
319 'oi_media_type' => $this->media_type,
320 'oi_major_mime' => $major,
321 'oi_minor_mime' => $minor,
322 'oi_metadata' => $this->getMetadataForDb( $dbw ),
323 'oi_sha1' => $this->sha1,
324 ] )
325 ->where( [
326 'oi_name' => $this->getName(),
327 'oi_archive_name' => $this->archive_name,
328 ] )
329 ->caller( __METHOD__ )->execute();
330 }
331
332 protected function reserializeMetadata() {
333 // TODO: implement this and make it possible to hit it from refreshImageMetadata.php
334 // It can be hit from action=purge but that's not very useful if the
335 // goal is to reserialize the whole oldimage table.
336 }
337
343 public function isDeleted( $field ) {
344 $this->load();
345
346 return ( $this->deleted & $field ) == $field;
347 }
348
353 public function getVisibility() {
354 $this->load();
355
356 return (int)$this->deleted;
357 }
358
367 public function userCan( $field, Authority $performer ) {
368 $this->load();
369
370 return RevisionRecord::userCanBitfield(
371 $this->deleted,
372 $field,
373 $performer
374 );
375 }
376
386 public function uploadOld( $srcPath, $timestamp, $comment, UserIdentity $user ) {
387 $archiveName = $this->getArchiveName();
388 $dstRel = $this->getArchiveRel( $archiveName );
389 $status = $this->publishTo( $srcPath, $dstRel );
390
391 if ( $status->isGood() &&
392 !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user )
393 ) {
394 $status->fatal( 'filenotfound', $srcPath );
395 }
396
397 return $status;
398 }
399
411 protected function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
412 $dbw = $this->repo->getPrimaryDB();
413
414 $services = MediaWikiServices::getInstance();
415 $mwProps = new MWFileProps( $services->getMimeAnalyzer() );
416 $props = $mwProps->getPropsFromPath( $srcPath, true );
417 if ( !$props['fileExists'] ) {
418 return false;
419 }
420 $this->setProps( $props );
421
422 $dbw->startAtomic( __METHOD__ );
423 $commentFields = $services->getCommentStore()
424 ->insert( $dbw, 'oi_description', $comment );
425 $actorId = $services->getActorNormalization()
426 ->acquireActorId( $user, $dbw );
427 $dbw->newInsertQueryBuilder()
428 ->insertInto( 'oldimage' )
429 ->row( [
430 'oi_name' => $this->getName(),
431 'oi_archive_name' => $archiveName,
432 'oi_size' => $props['size'],
433 'oi_width' => intval( $props['width'] ),
434 'oi_height' => intval( $props['height'] ),
435 'oi_bits' => $props['bits'],
436 'oi_actor' => $actorId,
437 'oi_timestamp' => $dbw->timestamp( $timestamp ),
438 'oi_metadata' => $this->getMetadataForDb( $dbw ),
439 'oi_media_type' => $props['media_type'],
440 'oi_major_mime' => $props['major_mime'],
441 'oi_minor_mime' => $props['minor_mime'],
442 'oi_sha1' => $props['sha1'],
443 ] + $commentFields )
444 ->caller( __METHOD__ )->execute();
445 $dbw->endAtomic( __METHOD__ );
446
447 return true;
448 }
449
457 public function exists() {
458 $archiveName = $this->getArchiveName();
459 if ( $archiveName === '' || !is_string( $archiveName ) ) {
460 return false;
461 }
462 return parent::exists();
463 }
464}
const NS_FILE
Definition Defines.php:71
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.
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.
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)
string $archive_name
Archive name.
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...