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  const CACHE_VERSION = 1;
40  const MAX_CACHE_ROWS = 20;
41 
49  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 
64  static function newFromArchiveName( $title, $repo, $archiveName ) {
65  return new static( $title, $repo, null, $archiveName );
66  }
67 
73  static function newFromRow( $row, $repo ) {
74  $title = Title::makeTitle( NS_FILE, $row->oi_name );
75  $file = new static( $title, $repo, null, $row->oi_archive_name );
76  $file->loadFromRow( $row, 'oi_' );
77 
78  return $file;
79  }
80 
91  static function newFromKey( $sha1, $repo, $timestamp = false ) {
92  $dbr = $repo->getReplicaDB();
93 
94  $conds = [ 'oi_sha1' => $sha1 ];
95  if ( $timestamp ) {
96  $conds['oi_timestamp'] = $dbr->timestamp( $timestamp );
97  }
98 
99  $fileQuery = static::getQueryInfo();
100  $row = $dbr->selectRow(
101  $fileQuery['tables'], $fileQuery['fields'], $conds, __METHOD__, [], $fileQuery['joins']
102  );
103  if ( $row ) {
104  return static::newFromRow( $row, $repo );
105  } else {
106  return false;
107  }
108  }
109 
121  public static function getQueryInfo( array $options = [] ) {
122  $commentQuery = MediaWikiServices::getInstance()->getCommentStore()->getJoin( 'oi_description' );
123  $actorQuery = ActorMigration::newMigration()->getJoin( 'oi_user' );
124  $ret = [
125  'tables' => [ 'oldimage' ] + $commentQuery['tables'] + $actorQuery['tables'],
126  'fields' => [
127  'oi_name',
128  'oi_archive_name',
129  'oi_size',
130  'oi_width',
131  'oi_height',
132  'oi_bits',
133  'oi_media_type',
134  'oi_major_mime',
135  'oi_minor_mime',
136  'oi_timestamp',
137  'oi_deleted',
138  'oi_sha1',
139  ] + $commentQuery['fields'] + $actorQuery['fields'],
140  'joins' => $commentQuery['joins'] + $actorQuery['joins'],
141  ];
142 
143  if ( in_array( 'omit-nonlazy', $options, true ) ) {
144  // Internal use only for getting only the lazy fields
145  $ret['fields'] = [];
146  }
147  if ( !in_array( 'omit-lazy', $options, true ) ) {
148  // Note: Keep this in sync with self::getLazyCacheFields()
149  $ret['fields'][] = 'oi_metadata';
150  }
151 
152  return $ret;
153  }
154 
162  function __construct( $title, $repo, $time, $archiveName ) {
163  parent::__construct( $title, $repo );
164  $this->requestedTime = $time;
165  $this->archive_name = $archiveName;
166  if ( $time === null && $archiveName === null ) {
167  throw new MWException( __METHOD__ . ': must specify at least one of $time or $archiveName' );
168  }
169  }
170 
174  function getCacheKey() {
175  return false;
176  }
177 
181  function getArchiveName() {
182  if ( !isset( $this->archive_name ) ) {
183  $this->load();
184  }
185 
186  return $this->archive_name;
187  }
188 
192  function isOld() {
193  return true;
194  }
195 
199  function isVisible() {
200  return $this->exists() && !$this->isDeleted( File::DELETED_FILE );
201  }
202 
203  function loadFromDB( $flags = 0 ) {
204  $this->dataLoaded = true;
205 
206  $dbr = ( $flags & self::READ_LATEST )
207  ? $this->repo->getMasterDB()
208  : $this->repo->getReplicaDB();
209 
210  $conds = [ 'oi_name' => $this->getName() ];
211  if ( $this->requestedTime === null ) {
212  $conds['oi_archive_name'] = $this->archive_name;
213  } else {
214  $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime );
215  }
216  $fileQuery = static::getQueryInfo();
217  $row = $dbr->selectRow(
218  $fileQuery['tables'],
219  $fileQuery['fields'],
220  $conds,
221  __METHOD__,
222  [ 'ORDER BY' => 'oi_timestamp DESC' ],
223  $fileQuery['joins']
224  );
225  if ( $row ) {
226  $this->loadFromRow( $row, 'oi_' );
227  } else {
228  $this->fileExists = false;
229  }
230  }
231 
235  protected function loadExtraFromDB() {
236  $this->extraDataLoaded = true;
237  $dbr = $this->repo->getReplicaDB();
238  $conds = [ 'oi_name' => $this->getName() ];
239  if ( $this->requestedTime === null ) {
240  $conds['oi_archive_name'] = $this->archive_name;
241  } else {
242  $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime );
243  }
244  $fileQuery = static::getQueryInfo( [ 'omit-nonlazy' ] );
245  // In theory the file could have just been renamed/deleted...oh well
246  $row = $dbr->selectRow(
247  $fileQuery['tables'],
248  $fileQuery['fields'],
249  $conds,
250  __METHOD__,
251  [ 'ORDER BY' => 'oi_timestamp DESC' ],
252  $fileQuery['joins']
253  );
254 
255  if ( !$row ) { // fallback to master
256  $dbr = $this->repo->getMasterDB();
257  $row = $dbr->selectRow(
258  $fileQuery['tables'],
259  $fileQuery['fields'],
260  $conds,
261  __METHOD__,
262  [ 'ORDER BY' => 'oi_timestamp DESC' ],
263  $fileQuery['joins']
264  );
265  }
266 
267  if ( $row ) {
268  foreach ( $this->unprefixRow( $row, 'oi_' ) as $name => $value ) {
269  $this->$name = $value;
270  }
271  } else {
272  throw new MWException( "Could not find data for image '{$this->archive_name}'." );
273  }
274  }
275 
277  protected function getCacheFields( $prefix = 'img_' ) {
278  $fields = parent::getCacheFields( $prefix );
279  $fields[] = $prefix . 'archive_name';
280  $fields[] = $prefix . 'deleted';
281 
282  return $fields;
283  }
284 
288  function getRel() {
289  return $this->getArchiveRel( $this->getArchiveName() );
290  }
291 
295  function getUrlRel() {
296  return $this->getArchiveRel( rawurlencode( $this->getArchiveName() ) );
297  }
298 
299  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\n" );
305 
306  return;
307  }
308 
309  $dbw = $this->repo->getMasterDB();
310  list( $major, $minor ) = self::splitMime( $this->mime );
311 
312  wfDebug( __METHOD__ . ': upgrading ' . $this->archive_name . " to the current schema\n" );
313  $dbw->update( 'oldimage',
314  [
315  'oi_size' => $this->size, // sanity
316  'oi_width' => $this->width,
317  'oi_height' => $this->height,
318  'oi_bits' => $this->bits,
319  'oi_media_type' => $this->media_type,
320  'oi_major_mime' => $major,
321  'oi_minor_mime' => $minor,
322  'oi_metadata' => $this->metadata,
323  'oi_sha1' => $this->sha1,
324  ], [
325  'oi_name' => $this->getName(),
326  'oi_archive_name' => $this->archive_name ],
327  __METHOD__
328  );
329  }
330 
336  function isDeleted( $field ) {
337  $this->load();
338 
339  return ( $this->deleted & $field ) == $field;
340  }
341 
346  function getVisibility() {
347  $this->load();
348 
349  return (int)$this->deleted;
350  }
351 
360  function userCan( $field, User $user = null ) {
361  $this->load();
362 
363  if ( !$user ) {
364  // TODO check callers and hard deprecate
365  global $wgUser;
366  $user = $wgUser;
367  }
368  return RevisionRecord::userCanBitfield(
369  $this->deleted,
370  $field,
371  $user
372  );
373  }
374 
384  public function uploadOld( $srcPath, $timestamp, $comment, $user ) {
385  $this->lock();
386 
387  $archiveName = $this->getArchiveName();
388  $dstRel = $this->getArchiveRel( $archiveName );
389  $status = $this->publishTo( $srcPath, $dstRel );
390 
391  if ( $status->isGood() &&
392  !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user )
393  ) {
394  $status->fatal( 'filenotfound', $srcPath );
395  }
396 
397  $this->unlock();
398 
399  return $status;
400  }
401 
412  protected function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
413  $dbw = $this->repo->getMasterDB();
414 
415  $dstPath = $this->repo->getZonePath( 'public' ) . '/' . $this->getRel();
416  $props = $this->repo->getFileProps( $dstPath );
417  if ( !$props['fileExists'] ) {
418  return false;
419  }
420 
421  $commentFields = MediaWikiServices::getInstance()->getCommentStore()
422  ->insert( $dbw, 'oi_description', $comment );
423  $actorFields = ActorMigration::newMigration()->getInsertValues( $dbw, 'oi_user', $user );
424  $dbw->insert( 'oldimage',
425  [
426  'oi_name' => $this->getName(),
427  'oi_archive_name' => $archiveName,
428  'oi_size' => $props['size'],
429  'oi_width' => intval( $props['width'] ),
430  'oi_height' => intval( $props['height'] ),
431  'oi_bits' => $props['bits'],
432  'oi_timestamp' => $dbw->timestamp( $timestamp ),
433  'oi_metadata' => $props['metadata'],
434  'oi_media_type' => $props['media_type'],
435  'oi_major_mime' => $props['major_mime'],
436  'oi_minor_mime' => $props['minor_mime'],
437  'oi_sha1' => $props['sha1'],
438  ] + $commentFields + $actorFields, __METHOD__
439  );
440 
441  return true;
442  }
443 
451  public function exists() {
452  $archiveName = $this->getArchiveName();
453  if ( $archiveName === '' || !is_string( $archiveName ) ) {
454  return false;
455  }
456  return parent::exists();
457  }
458 }
LocalFile\unprefixRow
unprefixRow( $row, $prefix='img_')
Definition: LocalFile.php:532
OldLocalFile\isOld
isOld()
Definition: OldLocalFile.php:192
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:91
LocalFile\unlock
unlock()
Decrement the lock reference count and end the atomic section if it reaches zero.
Definition: LocalFile.php:2261
OldLocalFile\isDeleted
isDeleted( $field)
Definition: OldLocalFile.php:336
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:134
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:121
OldLocalFile\getVisibility
getVisibility()
Returns bitfield value.
Definition: OldLocalFile.php:346
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:451
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:412
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:52
LocalFile\publishTo
publishTo( $src, $dstRel, $flags=0, array $options=[])
Move or copy a file to a specified location.
Definition: LocalFile.php:1807
MWException
MediaWiki exception.
Definition: MWException.php:26
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:299
LocalFile\loadFromRow
loadFromRow( $row, $prefix='img_')
Load file metadata from a DB result row.
Definition: LocalFile.php:603
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:235
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:602
OldLocalFile\__construct
__construct( $title, $repo, $time, $archiveName)
Definition: OldLocalFile.php:162
OldLocalFile\uploadOld
uploadOld( $srcPath, $timestamp, $comment, $user)
Upload a file directly into archive.
Definition: OldLocalFile.php:384
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:2216
LocalFile\load
load( $flags=0)
Load file metadata from cache or DB, unless already loaded.
Definition: LocalFile.php:620
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:199
OldLocalFile\newFromRow
static newFromRow( $row, $repo)
Definition: OldLocalFile.php:73
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:277
OldLocalFile\getRel
getRel()
Definition: OldLocalFile.php:288
OldLocalFile\getUrlRel
getUrlRel()
Definition: OldLocalFile.php:295
OldLocalFile\newFromTitle
static newFromTitle( $title, $repo, $time=null)
Definition: OldLocalFile.php:49
LocalFile\loadFromFile
loadFromFile()
Load metadata from the file itself.
Definition: LocalFile.php:374
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:360
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:174
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:52
OldLocalFile\getArchiveName
getArchiveName()
Definition: OldLocalFile.php:181
OldLocalFile
Class to represent a file in the oldimage table.
Definition: OldLocalFile.php:32
OldLocalFile\newFromArchiveName
static newFromArchiveName( $title, $repo, $archiveName)
Definition: OldLocalFile.php:64
OldLocalFile\loadFromDB
loadFromDB( $flags=0)
Load file metadata from the DB.
Definition: OldLocalFile.php:203
OldLocalFile\MAX_CACHE_ROWS
const MAX_CACHE_ROWS
Definition: OldLocalFile.php:40