MediaWiki  master
OldLocalFile.php
Go to the documentation of this file.
1 <?php
25 
32 class OldLocalFile extends LocalFile {
34  protected $requestedTime;
35 
37  protected $archive_name;
38 
39  public const CACHE_VERSION = 1;
40 
49  public static function newFromTitle( $title, $repo, $time = null ) {
50  # The null default value is only here to avoid an E_STRICT
51  if ( $time === null ) {
52  throw new MWException( __METHOD__ . ' got null for $time parameter' );
53  }
54 
55  return new static( $title, $repo, $time, null );
56  }
57 
66  public static function newFromArchiveName( $title, $repo, $archiveName ) {
67  return new static( $title, $repo, null, $archiveName );
68  }
69 
77  public static function newFromRow( $row, $repo ) {
78  $title = Title::makeTitle( NS_FILE, $row->oi_name );
79  $file = new static( $title, $repo, null, $row->oi_archive_name );
80  $file->loadFromRow( $row, 'oi_' );
81 
82  return $file;
83  }
84 
97  public static function newFromKey( $sha1, $repo, $timestamp = false ) {
98  $dbr = $repo->getReplicaDB();
99 
100  $conds = [ 'oi_sha1' => $sha1 ];
101  if ( $timestamp ) {
102  $conds['oi_timestamp'] = $dbr->timestamp( $timestamp );
103  }
104 
105  $fileQuery = static::getQueryInfo();
106  $row = $dbr->selectRow(
107  $fileQuery['tables'], $fileQuery['fields'], $conds, __METHOD__, [], $fileQuery['joins']
108  );
109  if ( $row ) {
110  return static::newFromRow( $row, $repo );
111  } else {
112  return false;
113  }
114  }
115 
135  public static function getQueryInfo( array $options = [] ) {
136  $commentQuery = MediaWikiServices::getInstance()->getCommentStore()->getJoin( 'oi_description' );
137  $ret = [
138  'tables' => [
139  'oldimage',
140  'oldimage_actor' => 'actor'
141  ] + $commentQuery['tables'],
142  'fields' => [
143  'oi_name',
144  'oi_archive_name',
145  'oi_size',
146  'oi_width',
147  'oi_height',
148  'oi_bits',
149  'oi_media_type',
150  'oi_major_mime',
151  'oi_minor_mime',
152  'oi_timestamp',
153  'oi_deleted',
154  'oi_sha1',
155  'oi_actor',
156  'oi_user' => 'oldimage_actor.actor_user',
157  'oi_user_text' => 'oldimage_actor.actor_name'
158  ] + $commentQuery['fields'],
159  'joins' => [
160  'oldimage_actor' => [ 'JOIN', 'actor_id=oi_actor' ]
161  ] + $commentQuery['joins'],
162  ];
163 
164  if ( in_array( 'omit-nonlazy', $options, true ) ) {
165  // Internal use only for getting only the lazy fields
166  $ret['fields'] = [];
167  }
168  if ( !in_array( 'omit-lazy', $options, true ) ) {
169  // Note: Keep this in sync with self::getLazyCacheFields()
170  $ret['fields'][] = 'oi_metadata';
171  }
172 
173  return $ret;
174  }
175 
185  public function __construct( $title, $repo, $time, $archiveName ) {
186  parent::__construct( $title, $repo );
187  $this->requestedTime = $time;
188  $this->archive_name = $archiveName;
189  if ( $time === null && $archiveName === null ) {
190  throw new MWException( __METHOD__ . ': must specify at least one of $time or $archiveName' );
191  }
192  }
193 
194  public function loadFromRow( $row, $prefix = 'img_' ) {
195  $this->archive_name = $row->{"{$prefix}archive_name"};
196  $this->deleted = $row->{"{$prefix}deleted"};
197  $row = clone $row;
198  unset( $row->{"{$prefix}archive_name"} );
199  unset( $row->{"{$prefix}deleted"} );
200  parent::loadFromRow( $row, $prefix );
201  }
202 
207  protected function getCacheKey() {
208  return false;
209  }
210 
215  public function getArchiveName() {
216  if ( !isset( $this->archive_name ) ) {
217  $this->load();
218  }
219 
220  return $this->archive_name;
221  }
222 
226  public function isOld() {
227  return true;
228  }
229 
233  public function isVisible() {
234  return $this->exists() && !$this->isDeleted( File::DELETED_FILE );
235  }
236 
241  protected function loadFromDB( $flags = 0 ) {
242  $this->dataLoaded = true;
243 
244  $dbr = ( $flags & self::READ_LATEST )
245  ? $this->repo->getPrimaryDB()
246  : $this->repo->getReplicaDB();
247 
248  $conds = [ 'oi_name' => $this->getName() ];
249  if ( $this->requestedTime === null ) {
250  $conds['oi_archive_name'] = $this->archive_name;
251  } else {
252  $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime );
253  }
254  $fileQuery = static::getQueryInfo();
255  $row = $dbr->selectRow(
256  $fileQuery['tables'],
257  $fileQuery['fields'],
258  $conds,
259  __METHOD__,
260  [ 'ORDER BY' => 'oi_timestamp DESC' ],
261  $fileQuery['joins']
262  );
263  if ( $row ) {
264  $this->loadFromRow( $row, 'oi_' );
265  } else {
266  $this->fileExists = false;
267  }
268  }
269 
274  protected function loadExtraFromDB() {
275  $this->extraDataLoaded = true;
276  $dbr = $this->repo->getReplicaDB();
277  $conds = [ 'oi_name' => $this->getName() ];
278  if ( $this->requestedTime === null ) {
279  $conds['oi_archive_name'] = $this->archive_name;
280  } else {
281  $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime );
282  }
283  $fileQuery = static::getQueryInfo( [ 'omit-nonlazy' ] );
284  // In theory the file could have just been renamed/deleted...oh well
285  $row = $dbr->selectRow(
286  $fileQuery['tables'],
287  $fileQuery['fields'],
288  $conds,
289  __METHOD__,
290  [ 'ORDER BY' => 'oi_timestamp DESC' ],
291  $fileQuery['joins']
292  );
293 
294  if ( !$row ) { // fallback to primary DB
295  $dbr = $this->repo->getPrimaryDB();
296  $row = $dbr->selectRow(
297  $fileQuery['tables'],
298  $fileQuery['fields'],
299  $conds,
300  __METHOD__,
301  [ 'ORDER BY' => 'oi_timestamp DESC' ],
302  $fileQuery['joins']
303  );
304  }
305 
306  if ( $row ) {
307  foreach ( $this->unprefixRow( $row, 'oi_' ) as $name => $value ) {
308  $this->$name = $value;
309  }
310  } else {
311  throw new MWException( "Could not find data for image '{$this->archive_name}'." );
312  }
313  }
314 
319  protected function getCacheFields( $prefix = 'img_' ) {
320  $fields = parent::getCacheFields( $prefix );
321  $fields[] = $prefix . 'archive_name';
322  $fields[] = $prefix . 'deleted';
323 
324  return $fields;
325  }
326 
331  public function getRel() {
332  return $this->getArchiveRel( $this->getArchiveName() );
333  }
334 
339  public function getUrlRel() {
340  return $this->getArchiveRel( rawurlencode( $this->getArchiveName() ) );
341  }
342 
346  public function upgradeRow() {
347  $this->loadFromFile();
348 
349  # Don't destroy file info of missing files
350  if ( !$this->fileExists ) {
351  wfDebug( __METHOD__ . ": file does not exist, aborting" );
352 
353  return;
354  }
355 
356  $dbw = $this->repo->getPrimaryDB();
357  [ $major, $minor ] = self::splitMime( $this->mime );
358 
359  wfDebug( __METHOD__ . ': upgrading ' . $this->archive_name . " to the current schema" );
360  $dbw->update( 'oldimage',
361  [
362  'oi_size' => $this->size,
363  'oi_width' => $this->width,
364  'oi_height' => $this->height,
365  'oi_bits' => $this->bits,
366  'oi_media_type' => $this->media_type,
367  'oi_major_mime' => $major,
368  'oi_minor_mime' => $minor,
369  'oi_metadata' => $this->getMetadataForDb( $dbw ),
370  'oi_sha1' => $this->sha1,
371  ], [
372  'oi_name' => $this->getName(),
373  'oi_archive_name' => $this->archive_name ],
374  __METHOD__
375  );
376  }
377 
378  protected function reserializeMetadata() {
379  // TODO: implement this and make it possible to hit it from refreshImageMetadata.php
380  // It can be hit from action=purge but that's not very useful if the
381  // goal is to reserialize the whole oldimage table.
382  }
383 
389  public function isDeleted( $field ) {
390  $this->load();
391 
392  return ( $this->deleted & $field ) == $field;
393  }
394 
399  public function getVisibility() {
400  $this->load();
401 
402  return (int)$this->deleted;
403  }
404 
413  public function userCan( $field, Authority $performer ) {
414  $this->load();
415 
416  return RevisionRecord::userCanBitfield(
417  $this->deleted,
418  $field,
419  $performer
420  );
421  }
422 
432  public function uploadOld( $srcPath, $timestamp, $comment, UserIdentity $user ) {
433  $archiveName = $this->getArchiveName();
434  $dstRel = $this->getArchiveRel( $archiveName );
435  $status = $this->publishTo( $srcPath, $dstRel );
436 
437  if ( $status->isGood() &&
438  !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user )
439  ) {
440  $status->fatal( 'filenotfound', $srcPath );
441  }
442 
443  return $status;
444  }
445 
457  protected function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
458  $dbw = $this->repo->getPrimaryDB();
459 
460  $services = MediaWikiServices::getInstance();
461  $mwProps = new MWFileProps( $services->getMimeAnalyzer() );
462  $props = $mwProps->getPropsFromPath( $srcPath, true );
463  if ( !$props['fileExists'] ) {
464  return false;
465  }
466  $this->setProps( $props );
467 
468  $dbw->startAtomic( __METHOD__ );
469  $commentFields = $services->getCommentStore()
470  ->insert( $dbw, 'oi_description', $comment );
471  $actorId = $services->getActorNormalization()
472  ->acquireActorId( $user, $dbw );
473  $dbw->insert( 'oldimage',
474  [
475  'oi_name' => $this->getName(),
476  'oi_archive_name' => $archiveName,
477  'oi_size' => $props['size'],
478  'oi_width' => intval( $props['width'] ),
479  'oi_height' => intval( $props['height'] ),
480  'oi_bits' => $props['bits'],
481  'oi_actor' => $actorId,
482  'oi_timestamp' => $dbw->timestamp( $timestamp ),
483  'oi_metadata' => $this->getMetadataForDb( $dbw ),
484  'oi_media_type' => $props['media_type'],
485  'oi_major_mime' => $props['major_mime'],
486  'oi_minor_mime' => $props['minor_mime'],
487  'oi_sha1' => $props['sha1'],
488  ] + $commentFields, __METHOD__
489  );
490  $dbw->endAtomic( __METHOD__ );
491 
492  return true;
493  }
494 
502  public function exists() {
503  $archiveName = $this->getArchiveName();
504  if ( $archiveName === '' || !is_string( $archiveName ) ) {
505  return false;
506  }
507  return parent::exists();
508  }
509 }
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:114
Title string false $title
Definition: File.php:117
const DELETED_FILE
Definition: File.php:71
Local file in the wiki's own database.
Definition: LocalFile.php:60
string $sha1
SHA-1 base 36 content hash.
Definition: LocalFile.php:124
MediaWiki exception.
Definition: MWException.php:30
MimeMagic helper wrapper.
Definition: MWFileProps.php:28
Service locator for MediaWiki core services.
Page revision base class.
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.Stability: stableto overri...
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.
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:641
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.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42