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
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 MWException( __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()->getDBLoadBalancerFactory()->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
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 MWException( __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 ( !isset( $this->archive_name ) ) {
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 & self::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 MWException( "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
312 wfDebug( __METHOD__ . ': upgrading ' . $this->archive_name . " to the current schema" );
313 $dbw->newUpdateQueryBuilder()
314 ->update( 'oldimage' )
315 ->set( [
316 'oi_size' => $this->size,
317 'oi_width' => $this->width,
318 'oi_height' => $this->height,
319 'oi_bits' => $this->bits,
320 'oi_media_type' => $this->media_type,
321 'oi_major_mime' => $major,
322 'oi_minor_mime' => $minor,
323 'oi_metadata' => $this->getMetadataForDb( $dbw ),
324 'oi_sha1' => $this->sha1,
325 ] )
326 ->where( [
327 'oi_name' => $this->getName(),
328 'oi_archive_name' => $this->archive_name,
329 ] )
330 ->caller( __METHOD__ )->execute();
331 }
332
333 protected function reserializeMetadata() {
334 // TODO: implement this and make it possible to hit it from refreshImageMetadata.php
335 // It can be hit from action=purge but that's not very useful if the
336 // goal is to reserialize the whole oldimage table.
337 }
338
344 public function isDeleted( $field ) {
345 $this->load();
346
347 return ( $this->deleted & $field ) == $field;
348 }
349
354 public function getVisibility() {
355 $this->load();
356
357 return (int)$this->deleted;
358 }
359
368 public function userCan( $field, Authority $performer ) {
369 $this->load();
370
371 return RevisionRecord::userCanBitfield(
372 $this->deleted,
373 $field,
374 $performer
375 );
376 }
377
387 public function uploadOld( $srcPath, $timestamp, $comment, UserIdentity $user ) {
388 $archiveName = $this->getArchiveName();
389 $dstRel = $this->getArchiveRel( $archiveName );
390 $status = $this->publishTo( $srcPath, $dstRel );
391
392 if ( $status->isGood() &&
393 !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user )
394 ) {
395 $status->fatal( 'filenotfound', $srcPath );
396 }
397
398 return $status;
399 }
400
412 protected function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
413 $dbw = $this->repo->getPrimaryDB();
414
415 $services = MediaWikiServices::getInstance();
416 $mwProps = new MWFileProps( $services->getMimeAnalyzer() );
417 $props = $mwProps->getPropsFromPath( $srcPath, true );
418 if ( !$props['fileExists'] ) {
419 return false;
420 }
421 $this->setProps( $props );
422
423 $dbw->startAtomic( __METHOD__ );
424 $commentFields = $services->getCommentStore()
425 ->insert( $dbw, 'oi_description', $comment );
426 $actorId = $services->getActorNormalization()
427 ->acquireActorId( $user, $dbw );
428 $dbw->newInsertQueryBuilder()
429 ->insertInto( 'oldimage' )
430 ->row( [
431 'oi_name' => $this->getName(),
432 'oi_archive_name' => $archiveName,
433 'oi_size' => $props['size'],
434 'oi_width' => intval( $props['width'] ),
435 'oi_height' => intval( $props['height'] ),
436 'oi_bits' => $props['bits'],
437 'oi_actor' => $actorId,
438 'oi_timestamp' => $dbw->timestamp( $timestamp ),
439 'oi_metadata' => $this->getMetadataForDb( $dbw ),
440 'oi_media_type' => $props['media_type'],
441 'oi_major_mime' => $props['major_mime'],
442 'oi_minor_mime' => $props['minor_mime'],
443 'oi_sha1' => $props['sha1'],
444 ] + $commentFields )
445 ->caller( __METHOD__ )->execute();
446 $dbw->endAtomic( __METHOD__ );
447
448 return true;
449 }
450
458 public function exists() {
459 $archiveName = $this->getArchiveName();
460 if ( $archiveName === '' || !is_string( $archiveName ) ) {
461 return false;
462 }
463 return parent::exists();
464 }
465}
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:117
Title string false $title
Definition File.php:120
const DELETED_FILE
Definition File.php:74
Local file in the wiki's own database.
Definition LocalFile.php:67
string $sha1
SHA-1 base 36 content hash.
MediaWiki exception.
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:79
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 the current execution context, such as a web reque...
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...
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42