MediaWiki  master
OldLocalFile.php
Go to the documentation of this file.
1 <?php
26 
32 class OldLocalFile extends LocalFile {
34  protected $requestedTime;
35 
37  protected $archive_name;
38 
39  public const CACHE_VERSION = 1;
40 
48  public static function newFromTitle( $title, $repo, $time = null ) {
49  # The null default value is only here to avoid an E_STRICT
50  if ( $time === null ) {
51  throw new MWException( __METHOD__ . ' got null for $time parameter' );
52  }
53 
54  return new static( $title, $repo, $time, null );
55  }
56 
63  public static function newFromArchiveName( $title, $repo, $archiveName ) {
64  return new static( $title, $repo, null, $archiveName );
65  }
66 
72  public static function newFromRow( $row, $repo ) {
73  $title = Title::makeTitle( NS_FILE, $row->oi_name );
74  $file = new static( $title, $repo, null, $row->oi_archive_name );
75  $file->loadFromRow( $row, 'oi_' );
76 
77  return $file;
78  }
79 
90  public static function newFromKey( $sha1, $repo, $timestamp = false ) {
91  $dbr = $repo->getReplicaDB();
92 
93  $conds = [ 'oi_sha1' => $sha1 ];
94  if ( $timestamp ) {
95  $conds['oi_timestamp'] = $dbr->timestamp( $timestamp );
96  }
97 
98  $fileQuery = static::getQueryInfo();
99  $row = $dbr->selectRow(
100  $fileQuery['tables'], $fileQuery['fields'], $conds, __METHOD__, [], $fileQuery['joins']
101  );
102  if ( $row ) {
103  return static::newFromRow( $row, $repo );
104  } else {
105  return false;
106  }
107  }
108 
120  public static function getQueryInfo( array $options = [] ) {
121  $commentQuery = MediaWikiServices::getInstance()->getCommentStore()->getJoin( 'oi_description' );
122  $actorQuery = ActorMigration::newMigration()->getJoin( 'oi_user' );
123  $ret = [
124  'tables' => [ 'oldimage' ] + $commentQuery['tables'] + $actorQuery['tables'],
125  'fields' => [
126  'oi_name',
127  'oi_archive_name',
128  'oi_size',
129  'oi_width',
130  'oi_height',
131  'oi_bits',
132  'oi_media_type',
133  'oi_major_mime',
134  'oi_minor_mime',
135  'oi_timestamp',
136  'oi_deleted',
137  'oi_sha1',
138  ] + $commentQuery['fields'] + $actorQuery['fields'],
139  'joins' => $commentQuery['joins'] + $actorQuery['joins'],
140  ];
141 
142  if ( in_array( 'omit-nonlazy', $options, true ) ) {
143  // Internal use only for getting only the lazy fields
144  $ret['fields'] = [];
145  }
146  if ( !in_array( 'omit-lazy', $options, true ) ) {
147  // Note: Keep this in sync with self::getLazyCacheFields()
148  $ret['fields'][] = 'oi_metadata';
149  }
150 
151  return $ret;
152  }
153 
161  public function __construct( $title, $repo, $time, $archiveName ) {
162  parent::__construct( $title, $repo );
163  $this->requestedTime = $time;
164  $this->archive_name = $archiveName;
165  if ( $time === null && $archiveName === null ) {
166  throw new MWException( __METHOD__ . ': must specify at least one of $time or $archiveName' );
167  }
168  }
169 
173  protected function getCacheKey() {
174  return false;
175  }
176 
180  public function getArchiveName() {
181  if ( !isset( $this->archive_name ) ) {
182  $this->load();
183  }
184 
185  return $this->archive_name;
186  }
187 
191  public function isOld() {
192  return true;
193  }
194 
198  public function isVisible() {
199  return $this->exists() && !$this->isDeleted( File::DELETED_FILE );
200  }
201 
202  protected function loadFromDB( $flags = 0 ) {
203  $this->dataLoaded = true;
204 
205  $dbr = ( $flags & self::READ_LATEST )
206  ? $this->repo->getMasterDB()
207  : $this->repo->getReplicaDB();
208 
209  $conds = [ 'oi_name' => $this->getName() ];
210  if ( $this->requestedTime === null ) {
211  $conds['oi_archive_name'] = $this->archive_name;
212  } else {
213  $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime );
214  }
215  $fileQuery = static::getQueryInfo();
216  $row = $dbr->selectRow(
217  $fileQuery['tables'],
218  $fileQuery['fields'],
219  $conds,
220  __METHOD__,
221  [ 'ORDER BY' => 'oi_timestamp DESC' ],
222  $fileQuery['joins']
223  );
224  if ( $row ) {
225  $this->loadFromRow( $row, 'oi_' );
226  } else {
227  $this->fileExists = false;
228  }
229  }
230 
234  protected function loadExtraFromDB() {
235  $this->extraDataLoaded = true;
236  $dbr = $this->repo->getReplicaDB();
237  $conds = [ 'oi_name' => $this->getName() ];
238  if ( $this->requestedTime === null ) {
239  $conds['oi_archive_name'] = $this->archive_name;
240  } else {
241  $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime );
242  }
243  $fileQuery = static::getQueryInfo( [ 'omit-nonlazy' ] );
244  // In theory the file could have just been renamed/deleted...oh well
245  $row = $dbr->selectRow(
246  $fileQuery['tables'],
247  $fileQuery['fields'],
248  $conds,
249  __METHOD__,
250  [ 'ORDER BY' => 'oi_timestamp DESC' ],
251  $fileQuery['joins']
252  );
253 
254  if ( !$row ) { // fallback to master
255  $dbr = $this->repo->getMasterDB();
256  $row = $dbr->selectRow(
257  $fileQuery['tables'],
258  $fileQuery['fields'],
259  $conds,
260  __METHOD__,
261  [ 'ORDER BY' => 'oi_timestamp DESC' ],
262  $fileQuery['joins']
263  );
264  }
265 
266  if ( $row ) {
267  foreach ( $this->unprefixRow( $row, 'oi_' ) as $name => $value ) {
268  $this->$name = $value;
269  }
270  } else {
271  throw new MWException( "Could not find data for image '{$this->archive_name}'." );
272  }
273  }
274 
276  protected function getCacheFields( $prefix = 'img_' ) {
277  $fields = parent::getCacheFields( $prefix );
278  $fields[] = $prefix . 'archive_name';
279  $fields[] = $prefix . 'deleted';
280 
281  return $fields;
282  }
283 
287  public function getRel() {
288  return $this->getArchiveRel( $this->getArchiveName() );
289  }
290 
294  public function getUrlRel() {
295  return $this->getArchiveRel( rawurlencode( $this->getArchiveName() ) );
296  }
297 
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\n" );
304 
305  return;
306  }
307 
308  $dbw = $this->repo->getMasterDB();
309  list( $major, $minor ) = self::splitMime( $this->mime );
310 
311  wfDebug( __METHOD__ . ': upgrading ' . $this->archive_name . " to the current schema\n" );
312  $dbw->update( 'oldimage',
313  [
314  'oi_size' => $this->size, // sanity
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->metadata,
322  'oi_sha1' => $this->sha1,
323  ], [
324  'oi_name' => $this->getName(),
325  'oi_archive_name' => $this->archive_name ],
326  __METHOD__
327  );
328  }
329 
335  public function isDeleted( $field ) {
336  $this->load();
337 
338  return ( $this->deleted & $field ) == $field;
339  }
340 
345  public function getVisibility() {
346  $this->load();
347 
348  return (int)$this->deleted;
349  }
350 
359  public function userCan( $field, User $user = null ) {
360  $this->load();
361 
362  if ( !$user ) {
363  wfDeprecated( __METHOD__ . ' without passing a $user parameter', '1.35' );
364  global $wgUser;
365  $user = $wgUser;
366  }
367  return RevisionRecord::userCanBitfield(
368  $this->deleted,
369  $field,
370  $user
371  );
372  }
373 
383  public function uploadOld( $srcPath, $timestamp, $comment, $user ) {
384  $this->lock();
385 
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  $this->unlock();
397 
398  return $status;
399  }
400 
411  protected function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
412  $dbw = $this->repo->getMasterDB();
413 
414  $dstPath = $this->repo->getZonePath( 'public' ) . '/' . $this->getRel();
415  $props = $this->repo->getFileProps( $dstPath );
416  if ( !$props['fileExists'] ) {
417  return false;
418  }
419 
420  $commentFields = MediaWikiServices::getInstance()->getCommentStore()
421  ->insert( $dbw, 'oi_description', $comment );
422  $actorFields = ActorMigration::newMigration()->getInsertValues( $dbw, 'oi_user', $user );
423  $dbw->insert( 'oldimage',
424  [
425  'oi_name' => $this->getName(),
426  'oi_archive_name' => $archiveName,
427  'oi_size' => $props['size'],
428  'oi_width' => intval( $props['width'] ),
429  'oi_height' => intval( $props['height'] ),
430  'oi_bits' => $props['bits'],
431  'oi_timestamp' => $dbw->timestamp( $timestamp ),
432  'oi_metadata' => $props['metadata'],
433  'oi_media_type' => $props['media_type'],
434  'oi_major_mime' => $props['major_mime'],
435  'oi_minor_mime' => $props['minor_mime'],
436  'oi_sha1' => $props['sha1'],
437  ] + $commentFields + $actorFields, __METHOD__
438  );
439 
440  return true;
441  }
442 
450  public function exists() {
451  $archiveName = $this->getArchiveName();
452  if ( $archiveName === '' || !is_string( $archiveName ) ) {
453  return false;
454  }
455  return parent::exists();
456  }
457 }
LocalFile\unprefixRow
unprefixRow( $row, $prefix='img_')
Definition: LocalFile.php:539
OldLocalFile\isOld
isOld()
Definition: OldLocalFile.php:191
File\$repo
FileRepo LocalRepo ForeignAPIRepo bool $repo
Some member variables can be lazy-initialised using __get().
Definition: File.php:106
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:46
OldLocalFile\CACHE_VERSION
const CACHE_VERSION
Definition: OldLocalFile.php:39
OldLocalFile\newFromKey
static newFromKey( $sha1, $repo, $timestamp=false)
Create a OldLocalFile from a SHA-1 key Do not call this except from inside a repo class.
Definition: OldLocalFile.php:90
LocalFile\unlock
unlock()
Decrement the lock reference count and end the atomic section if it reaches zero.
Definition: LocalFile.php:2360
OldLocalFile\isDeleted
isDeleted( $field)
Definition: OldLocalFile.php:335
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:144
OldLocalFile\getQueryInfo
static getQueryInfo(array $options=[])
Return the tables, fields, and join conditions to be selected to create a new oldlocalfile object.
Definition: OldLocalFile.php:120
OldLocalFile\getVisibility
getVisibility()
Returns bitfield value.
Definition: OldLocalFile.php:345
NS_FILE
const NS_FILE
Definition: Defines.php:75
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
OldLocalFile\exists
exists()
If archive name is an empty string, then file does not "exist".
Definition: OldLocalFile.php:450
LocalFile\$sha1
string $sha1
SHA-1 base 36 content hash.
Definition: LocalFile.php:86
File\splitMime
static splitMime( $mime)
Split an internet media type into its two components; if not a two-part name, set the minor type to '...
Definition: File.php:283
OldLocalFile\recordOldUpload
recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user)
Record a file upload in the oldimage table, without adding log entries.
Definition: OldLocalFile.php:411
File\getArchiveRel
getArchiveRel( $suffix=false)
Get the path of an archived file relative to the public zone root.
Definition: File.php:1592
ActorMigration\newMigration
static newMigration()
Static constructor.
Definition: ActorMigration.php:139
$dbr
$dbr
Definition: testCompression.php:54
LocalFile\publishTo
publishTo( $src, $dstRel, $flags=0, array $options=[])
Move or copy a file to a specified location.
Definition: LocalFile.php:1851
MWException
MediaWiki exception.
Definition: MWException.php:26
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
Definition: GlobalFunctions.php:1030
OldLocalFile\$archive_name
string $archive_name
Archive name.
Definition: OldLocalFile.php:37
OldLocalFile\upgradeRow
upgradeRow()
Fix assorted version-related problems with the image row by reloading it from the file.
Definition: OldLocalFile.php:298
LocalFile\loadFromRow
loadFromRow( $row, $prefix='img_')
Load file metadata from a DB result row.
Definition: LocalFile.php:610
LocalFile\$deleted
int $deleted
Bitfield akin to rev_deleted.
Definition: LocalFile.php:95
OldLocalFile\loadExtraFromDB
loadExtraFromDB()
Load lazy file metadata from the DB.
Definition: OldLocalFile.php:234
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:595
OldLocalFile\__construct
__construct( $title, $repo, $time, $archiveName)
Definition: OldLocalFile.php:161
OldLocalFile\uploadOld
uploadOld( $srcPath, $timestamp, $comment, $user)
Upload a file directly into archive.
Definition: OldLocalFile.php:383
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:913
LocalFile
Class to represent a local file in the wiki's own database.
Definition: LocalFile.php:56
OldLocalFile\$requestedTime
string int $requestedTime
Timestamp.
Definition: OldLocalFile.php:34
LocalFile\lock
lock()
Start an atomic DB section and lock the image for update or increments a reference counter if the loc...
Definition: LocalFile.php:2315
LocalFile\load
load( $flags=0)
Load file metadata from cache or DB, unless already loaded.
Definition: LocalFile.php:627
File\$title
Title string bool $title
Definition: File.php:109
LocalFile\$user
User $user
Uploader.
Definition: LocalFile.php:116
File\getName
getName()
Return the name of this file.
Definition: File.php:307
LocalFile\$timestamp
string $timestamp
Upload timestamp.
Definition: LocalFile.php:113
OldLocalFile\isVisible
isVisible()
Definition: OldLocalFile.php:198
OldLocalFile\newFromRow
static newFromRow( $row, $repo)
Definition: OldLocalFile.php:72
OldLocalFile\getCacheFields
getCacheFields( $prefix='img_')
Returns the list of object properties that are included as-is in the cache.Must be the empty string s...
Definition: OldLocalFile.php:276
OldLocalFile\getRel
getRel()
Definition: OldLocalFile.php:287
OldLocalFile\getUrlRel
getUrlRel()
Definition: OldLocalFile.php:294
OldLocalFile\newFromTitle
static newFromTitle( $title, $repo, $time=null)
Definition: OldLocalFile.php:48
LocalFile\loadFromFile
loadFromFile()
Load metadata from the file itself.
Definition: LocalFile.php:381
OldLocalFile\userCan
userCan( $field, User $user=null)
Determine if the current user is allowed to view a particular field of this image file,...
Definition: OldLocalFile.php:359
File\$name
string $name
The name of a file from its title object.
Definition: File.php:133
File\DELETED_FILE
const DELETED_FILE
Definition: File.php:63
OldLocalFile\getCacheKey
getCacheKey()
Definition: OldLocalFile.php:173
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:54
OldLocalFile\getArchiveName
getArchiveName()
Definition: OldLocalFile.php:180
OldLocalFile
Class to represent a file in the oldimage table.
Definition: OldLocalFile.php:32
OldLocalFile\newFromArchiveName
static newFromArchiveName( $title, $repo, $archiveName)
Definition: OldLocalFile.php:63
OldLocalFile\loadFromDB
loadFromDB( $flags=0)
Load file metadata from the DB.
Definition: OldLocalFile.php:202