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 
134  public static function getQueryInfo( array $options = [] ) {
135  $commentQuery = MediaWikiServices::getInstance()->getCommentStore()->getJoin( 'oi_description' );
136  $ret = [
137  'tables' => [
138  'oldimage',
139  'oldimage_actor' => 'actor'
140  ] + $commentQuery['tables'],
141  'fields' => [
142  'oi_name',
143  'oi_archive_name',
144  'oi_size',
145  'oi_width',
146  'oi_height',
147  'oi_bits',
148  'oi_media_type',
149  'oi_major_mime',
150  'oi_minor_mime',
151  'oi_timestamp',
152  'oi_deleted',
153  'oi_sha1',
154  'oi_actor',
155  'oi_user' => 'oldimage_actor.actor_user',
156  'oi_user_text' => 'oldimage_actor.actor_name'
157  ] + $commentQuery['fields'],
158  'joins' => [
159  'oldimage_actor' => [ 'JOIN', 'actor_id=oi_actor' ]
160  ] + $commentQuery['joins'],
161  ];
162 
163  if ( in_array( 'omit-nonlazy', $options, true ) ) {
164  // Internal use only for getting only the lazy fields
165  $ret['fields'] = [];
166  }
167  if ( !in_array( 'omit-lazy', $options, true ) ) {
168  // Note: Keep this in sync with self::getLazyCacheFields()
169  $ret['fields'][] = 'oi_metadata';
170  }
171 
172  return $ret;
173  }
174 
184  public function __construct( $title, $repo, $time, $archiveName ) {
185  parent::__construct( $title, $repo );
186  $this->requestedTime = $time;
187  $this->archive_name = $archiveName;
188  if ( $time === null && $archiveName === null ) {
189  throw new MWException( __METHOD__ . ': must specify at least one of $time or $archiveName' );
190  }
191  }
192 
193  public function loadFromRow( $row, $prefix = 'img_' ) {
194  $this->archive_name = $row->{"{$prefix}archive_name"};
195  $this->deleted = $row->{"{$prefix}deleted"};
196  $row = clone $row;
197  unset( $row->{"{$prefix}archive_name"} );
198  unset( $row->{"{$prefix}deleted"} );
199  parent::loadFromRow( $row, $prefix );
200  }
201 
206  protected function getCacheKey() {
207  return false;
208  }
209 
214  public function getArchiveName() {
215  if ( !isset( $this->archive_name ) ) {
216  $this->load();
217  }
218 
219  return $this->archive_name;
220  }
221 
225  public function isOld() {
226  return true;
227  }
228 
232  public function isVisible() {
233  return $this->exists() && !$this->isDeleted( File::DELETED_FILE );
234  }
235 
240  protected function loadFromDB( $flags = 0 ) {
241  $this->dataLoaded = true;
242 
243  $dbr = ( $flags & self::READ_LATEST )
244  ? $this->repo->getPrimaryDB()
245  : $this->repo->getReplicaDB();
246 
247  $conds = [ 'oi_name' => $this->getName() ];
248  if ( $this->requestedTime === null ) {
249  $conds['oi_archive_name'] = $this->archive_name;
250  } else {
251  $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime );
252  }
253  $fileQuery = static::getQueryInfo();
254  $row = $dbr->selectRow(
255  $fileQuery['tables'],
256  $fileQuery['fields'],
257  $conds,
258  __METHOD__,
259  [ 'ORDER BY' => 'oi_timestamp DESC' ],
260  $fileQuery['joins']
261  );
262  if ( $row ) {
263  $this->loadFromRow( $row, 'oi_' );
264  } else {
265  $this->fileExists = false;
266  }
267  }
268 
273  protected function loadExtraFromDB() {
274  $this->extraDataLoaded = true;
275  $dbr = $this->repo->getReplicaDB();
276  $conds = [ 'oi_name' => $this->getName() ];
277  if ( $this->requestedTime === null ) {
278  $conds['oi_archive_name'] = $this->archive_name;
279  } else {
280  $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime );
281  }
282  $fileQuery = static::getQueryInfo( [ 'omit-nonlazy' ] );
283  // In theory the file could have just been renamed/deleted...oh well
284  $row = $dbr->selectRow(
285  $fileQuery['tables'],
286  $fileQuery['fields'],
287  $conds,
288  __METHOD__,
289  [ 'ORDER BY' => 'oi_timestamp DESC' ],
290  $fileQuery['joins']
291  );
292 
293  if ( !$row ) { // fallback to primary DB
294  $dbr = $this->repo->getPrimaryDB();
295  $row = $dbr->selectRow(
296  $fileQuery['tables'],
297  $fileQuery['fields'],
298  $conds,
299  __METHOD__,
300  [ 'ORDER BY' => 'oi_timestamp DESC' ],
301  $fileQuery['joins']
302  );
303  }
304 
305  if ( $row ) {
306  foreach ( $this->unprefixRow( $row, 'oi_' ) as $name => $value ) {
307  $this->$name = $value;
308  }
309  } else {
310  throw new MWException( "Could not find data for image '{$this->archive_name}'." );
311  }
312  }
313 
318  protected function getCacheFields( $prefix = 'img_' ) {
319  $fields = parent::getCacheFields( $prefix );
320  $fields[] = $prefix . 'archive_name';
321  $fields[] = $prefix . 'deleted';
322 
323  return $fields;
324  }
325 
330  public function getRel() {
331  return $this->getArchiveRel( $this->getArchiveName() );
332  }
333 
338  public function getUrlRel() {
339  return $this->getArchiveRel( rawurlencode( $this->getArchiveName() ) );
340  }
341 
345  public function upgradeRow() {
346  $this->loadFromFile();
347 
348  # Don't destroy file info of missing files
349  if ( !$this->fileExists ) {
350  wfDebug( __METHOD__ . ": file does not exist, aborting" );
351 
352  return;
353  }
354 
355  $dbw = $this->repo->getPrimaryDB();
356  list( $major, $minor ) = self::splitMime( $this->mime );
357 
358  wfDebug( __METHOD__ . ': upgrading ' . $this->archive_name . " to the current schema" );
359  $dbw->update( 'oldimage',
360  [
361  'oi_size' => $this->size,
362  'oi_width' => $this->width,
363  'oi_height' => $this->height,
364  'oi_bits' => $this->bits,
365  'oi_media_type' => $this->media_type,
366  'oi_major_mime' => $major,
367  'oi_minor_mime' => $minor,
368  'oi_metadata' => $this->getMetadataForDb( $dbw ),
369  'oi_sha1' => $this->sha1,
370  ], [
371  'oi_name' => $this->getName(),
372  'oi_archive_name' => $this->archive_name ],
373  __METHOD__
374  );
375  }
376 
377  protected function reserializeMetadata() {
378  // TODO: implement this and make it possible to hit it from refreshImageMetadata.php
379  // It can be hit from action=purge but that's not very useful if the
380  // goal is to reserialize the whole oldimage table.
381  }
382 
388  public function isDeleted( $field ) {
389  $this->load();
390 
391  return ( $this->deleted & $field ) == $field;
392  }
393 
398  public function getVisibility() {
399  $this->load();
400 
401  return (int)$this->deleted;
402  }
403 
412  public function userCan( $field, Authority $performer ) {
413  $this->load();
414 
415  return RevisionRecord::userCanBitfield(
416  $this->deleted,
417  $field,
418  $performer
419  );
420  }
421 
431  public function uploadOld( $srcPath, $timestamp, $comment, UserIdentity $user ) {
432  $archiveName = $this->getArchiveName();
433  $dstRel = $this->getArchiveRel( $archiveName );
434  $status = $this->publishTo( $srcPath, $dstRel );
435 
436  if ( $status->isGood() &&
437  !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user )
438  ) {
439  $status->fatal( 'filenotfound', $srcPath );
440  }
441 
442  return $status;
443  }
444 
456  protected function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
457  $dbw = $this->repo->getPrimaryDB();
458 
459  $services = MediaWikiServices::getInstance();
460  $mwProps = new MWFileProps( $services->getMimeAnalyzer() );
461  $props = $mwProps->getPropsFromPath( $srcPath, true );
462  if ( !$props['fileExists'] ) {
463  return false;
464  }
465  $this->setProps( $props );
466 
467  $dbw->startAtomic( __METHOD__ );
468  $commentFields = $services->getCommentStore()
469  ->insert( $dbw, 'oi_description', $comment );
470  $actorId = $services->getActorNormalization()
471  ->acquireActorId( $user, $dbw );
472  $dbw->insert( 'oldimage',
473  [
474  'oi_name' => $this->getName(),
475  'oi_archive_name' => $archiveName,
476  'oi_size' => $props['size'],
477  'oi_width' => intval( $props['width'] ),
478  'oi_height' => intval( $props['height'] ),
479  'oi_bits' => $props['bits'],
480  'oi_actor' => $actorId,
481  'oi_timestamp' => $dbw->timestamp( $timestamp ),
482  'oi_metadata' => $this->getMetadataForDb( $dbw ),
483  'oi_media_type' => $props['media_type'],
484  'oi_major_mime' => $props['major_mime'],
485  'oi_minor_mime' => $props['minor_mime'],
486  'oi_sha1' => $props['sha1'],
487  ] + $commentFields, __METHOD__
488  );
489  $dbw->endAtomic( __METHOD__ );
490 
491  return true;
492  }
493 
501  public function exists() {
502  $archiveName = $this->getArchiveName();
503  if ( $archiveName === '' || !is_string( $archiveName ) ) {
504  return false;
505  }
506  return parent::exists();
507  }
508 }
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 bool $repo
Some member variables can be lazy-initialised using __get().
Definition: File.php:115
const DELETED_FILE
Definition: File.php:72
Title string bool $title
Definition: File.php:118
Local file in the wiki's own database.
Definition: LocalFile.php:60
string $timestamp
Upload timestamp.
Definition: LocalFile.php:151
string $sha1
SHA-1 base 36 content hash.
Definition: LocalFile.php:124
MediaWiki exception.
Definition: MWException.php:29
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:638
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