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