MediaWiki REL1_35
OldLocalFile.php
Go to the documentation of this file.
1<?php
26
33class OldLocalFile extends LocalFile {
35 protected $requestedTime;
36
38 protected $archive_name;
39
40 public const CACHE_VERSION = 1;
41
50 public static function newFromTitle( $title, $repo, $time = null ) {
51 # The null default value is only here to avoid an E_STRICT
52 if ( $time === null ) {
53 throw new MWException( __METHOD__ . ' got null for $time parameter' );
54 }
55
56 return new static( $title, $repo, $time, null );
57 }
58
67 public static function newFromArchiveName( $title, $repo, $archiveName ) {
68 return new static( $title, $repo, null, $archiveName );
69 }
70
78 public static function newFromRow( $row, $repo ) {
79 $title = Title::makeTitle( NS_FILE, $row->oi_name );
80 $file = new static( $title, $repo, null, $row->oi_archive_name );
81 $file->loadFromRow( $row, 'oi_' );
82
83 return $file;
84 }
85
98 public static function newFromKey( $sha1, $repo, $timestamp = false ) {
99 $dbr = $repo->getReplicaDB();
100
101 $conds = [ 'oi_sha1' => $sha1 ];
102 if ( $timestamp ) {
103 $conds['oi_timestamp'] = $dbr->timestamp( $timestamp );
104 }
105
106 $fileQuery = static::getQueryInfo();
107 $row = $dbr->selectRow(
108 $fileQuery['tables'], $fileQuery['fields'], $conds, __METHOD__, [], $fileQuery['joins']
109 );
110 if ( $row ) {
111 return static::newFromRow( $row, $repo );
112 } else {
113 return false;
114 }
115 }
116
130 public static function getQueryInfo( array $options = [] ) {
131 $commentQuery = MediaWikiServices::getInstance()->getCommentStore()->getJoin( 'oi_description' );
132 $actorQuery = ActorMigration::newMigration()->getJoin( 'oi_user' );
133 $ret = [
134 'tables' => [ 'oldimage' ] + $commentQuery['tables'] + $actorQuery['tables'],
135 'fields' => [
136 'oi_name',
137 'oi_archive_name',
138 'oi_size',
139 'oi_width',
140 'oi_height',
141 'oi_bits',
142 'oi_media_type',
143 'oi_major_mime',
144 'oi_minor_mime',
145 'oi_timestamp',
146 'oi_deleted',
147 'oi_sha1',
148 ] + $commentQuery['fields'] + $actorQuery['fields'],
149 'joins' => $commentQuery['joins'] + $actorQuery['joins'],
150 ];
151
152 if ( in_array( 'omit-nonlazy', $options, true ) ) {
153 // Internal use only for getting only the lazy fields
154 $ret['fields'] = [];
155 }
156 if ( !in_array( 'omit-lazy', $options, true ) ) {
157 // Note: Keep this in sync with self::getLazyCacheFields()
158 $ret['fields'][] = 'oi_metadata';
159 }
160
161 return $ret;
162 }
163
173 public function __construct( $title, $repo, $time, $archiveName ) {
174 parent::__construct( $title, $repo );
175 $this->requestedTime = $time;
176 $this->archive_name = $archiveName;
177 if ( $time === null && $archiveName === null ) {
178 throw new MWException( __METHOD__ . ': must specify at least one of $time or $archiveName' );
179 }
180 }
181
186 protected function getCacheKey() {
187 return false;
188 }
189
194 public function getArchiveName() {
195 if ( !isset( $this->archive_name ) ) {
196 $this->load();
197 }
198
199 return $this->archive_name;
200 }
201
205 public function isOld() {
206 return true;
207 }
208
212 public function isVisible() {
213 return $this->exists() && !$this->isDeleted( File::DELETED_FILE );
214 }
215
220 protected function loadFromDB( $flags = 0 ) {
221 $this->dataLoaded = true;
222
223 $dbr = ( $flags & self::READ_LATEST )
224 ? $this->repo->getMasterDB()
225 : $this->repo->getReplicaDB();
226
227 $conds = [ 'oi_name' => $this->getName() ];
228 if ( $this->requestedTime === null ) {
229 $conds['oi_archive_name'] = $this->archive_name;
230 } else {
231 $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime );
232 }
233 $fileQuery = static::getQueryInfo();
234 $row = $dbr->selectRow(
235 $fileQuery['tables'],
236 $fileQuery['fields'],
237 $conds,
238 __METHOD__,
239 [ 'ORDER BY' => 'oi_timestamp DESC' ],
240 $fileQuery['joins']
241 );
242 if ( $row ) {
243 $this->loadFromRow( $row, 'oi_' );
244 } else {
245 $this->fileExists = false;
246 }
247 }
248
253 protected function loadExtraFromDB() {
254 $this->extraDataLoaded = true;
255 $dbr = $this->repo->getReplicaDB();
256 $conds = [ 'oi_name' => $this->getName() ];
257 if ( $this->requestedTime === null ) {
258 $conds['oi_archive_name'] = $this->archive_name;
259 } else {
260 $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime );
261 }
262 $fileQuery = static::getQueryInfo( [ 'omit-nonlazy' ] );
263 // In theory the file could have just been renamed/deleted...oh well
264 $row = $dbr->selectRow(
265 $fileQuery['tables'],
266 $fileQuery['fields'],
267 $conds,
268 __METHOD__,
269 [ 'ORDER BY' => 'oi_timestamp DESC' ],
270 $fileQuery['joins']
271 );
272
273 if ( !$row ) { // fallback to master
274 $dbr = $this->repo->getMasterDB();
275 $row = $dbr->selectRow(
276 $fileQuery['tables'],
277 $fileQuery['fields'],
278 $conds,
279 __METHOD__,
280 [ 'ORDER BY' => 'oi_timestamp DESC' ],
281 $fileQuery['joins']
282 );
283 }
284
285 if ( $row ) {
286 foreach ( $this->unprefixRow( $row, 'oi_' ) as $name => $value ) {
287 $this->$name = $value;
288 }
289 } else {
290 throw new MWException( "Could not find data for image '{$this->archive_name}'." );
291 }
292 }
293
298 protected function getCacheFields( $prefix = 'img_' ) {
299 $fields = parent::getCacheFields( $prefix );
300 $fields[] = $prefix . 'archive_name';
301 $fields[] = $prefix . 'deleted';
302
303 return $fields;
304 }
305
310 public function getRel() {
311 return $this->getArchiveRel( $this->getArchiveName() );
312 }
313
318 public function getUrlRel() {
319 return $this->getArchiveRel( rawurlencode( $this->getArchiveName() ) );
320 }
321
325 public function upgradeRow() {
326 $this->loadFromFile();
327
328 # Don't destroy file info of missing files
329 if ( !$this->fileExists ) {
330 wfDebug( __METHOD__ . ": file does not exist, aborting" );
331
332 return;
333 }
334
335 $dbw = $this->repo->getMasterDB();
336 list( $major, $minor ) = self::splitMime( $this->mime );
337
338 wfDebug( __METHOD__ . ': upgrading ' . $this->archive_name . " to the current schema" );
339 $dbw->update( 'oldimage',
340 [
341 'oi_size' => $this->size, // sanity
342 'oi_width' => $this->width,
343 'oi_height' => $this->height,
344 'oi_bits' => $this->bits,
345 'oi_media_type' => $this->media_type,
346 'oi_major_mime' => $major,
347 'oi_minor_mime' => $minor,
348 'oi_metadata' => $this->metadata,
349 'oi_sha1' => $this->sha1,
350 ], [
351 'oi_name' => $this->getName(),
352 'oi_archive_name' => $this->archive_name ],
353 __METHOD__
354 );
355 }
356
362 public function isDeleted( $field ) {
363 $this->load();
364
365 return ( $this->deleted & $field ) == $field;
366 }
367
372 public function getVisibility() {
373 $this->load();
374
375 return (int)$this->deleted;
376 }
377
386 public function userCan( $field, User $user = null ) {
387 $this->load();
388
389 if ( !$user ) {
390 wfDeprecated( __METHOD__ . ' without passing a $user parameter', '1.35' );
391 global $wgUser;
392 $user = $wgUser;
393 }
394 return RevisionRecord::userCanBitfield(
395 $this->deleted,
396 $field,
397 $user
398 );
399 }
400
410 public function uploadOld( $srcPath, $timestamp, $comment, $user ) {
411 $this->lock();
412
413 $archiveName = $this->getArchiveName();
414 $dstRel = $this->getArchiveRel( $archiveName );
415 $status = $this->publishTo( $srcPath, $dstRel );
416
417 if ( $status->isGood() &&
418 !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user )
419 ) {
420 $status->fatal( 'filenotfound', $srcPath );
421 }
422
423 $this->unlock();
424
425 return $status;
426 }
427
439 protected function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
440 $dbw = $this->repo->getMasterDB();
441
442 $dstPath = $this->repo->getZonePath( 'public' ) . '/' . $this->getRel();
443 $props = $this->repo->getFileProps( $dstPath );
444 if ( !$props['fileExists'] ) {
445 return false;
446 }
447
448 $commentFields = MediaWikiServices::getInstance()->getCommentStore()
449 ->insert( $dbw, 'oi_description', $comment );
450 $actorFields = ActorMigration::newMigration()->getInsertValues( $dbw, 'oi_user', $user );
451 $dbw->insert( 'oldimage',
452 [
453 'oi_name' => $this->getName(),
454 'oi_archive_name' => $archiveName,
455 'oi_size' => $props['size'],
456 'oi_width' => intval( $props['width'] ),
457 'oi_height' => intval( $props['height'] ),
458 'oi_bits' => $props['bits'],
459 'oi_timestamp' => $dbw->timestamp( $timestamp ),
460 'oi_metadata' => $props['metadata'],
461 'oi_media_type' => $props['media_type'],
462 'oi_major_mime' => $props['major_mime'],
463 'oi_minor_mime' => $props['minor_mime'],
464 'oi_sha1' => $props['sha1'],
465 ] + $commentFields + $actorFields, __METHOD__
466 );
467
468 return true;
469 }
470
478 public function exists() {
479 $archiveName = $this->getArchiveName();
480 if ( $archiveName === '' || !is_string( $archiveName ) ) {
481 return false;
482 }
483 return parent::exists();
484 }
485}
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
getName()
Return the name of this file.
Definition File.php:315
string $name
The name of a file from its title object.
Definition File.php:137
FileRepo LocalRepo ForeignAPIRepo bool $repo
Some member variables can be lazy-initialised using __get().
Definition File.php:110
const DELETED_FILE
Definition File.php:67
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:290
Title string bool $title
Definition File.php:113
getArchiveRel( $suffix=false)
Get the path of an archived file relative to the public zone root Stable to override.
Definition File.php:1629
Class to represent a local file in the wiki's own database.
Definition LocalFile.php:59
lock()
Start an atomic DB section and lock the image for update or increments a reference counter if the loc...
loadFromRow( $row, $prefix='img_')
Load file metadata from a DB result row Stable to override.
load( $flags=0)
Load file metadata from cache or DB, unless already loaded Stable to override.
loadFromFile()
Load metadata from the file itself.
string $timestamp
Upload timestamp.
publishTo( $src, $dstRel, $flags=0, array $options=[])
Move or copy a file to a specified location.
unlock()
Decrement the lock reference count and end the atomic section if it reaches zero.
User $user
Uploader.
string $sha1
SHA-1 base 36 content hash.
Definition LocalFile.php:89
unprefixRow( $row, $prefix='img_')
int $deleted
Bitfield akin to rev_deleted.
Definition LocalFile.php:98
MediaWiki exception.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Page revision base class.
Class to represent a file in the oldimage table.
static newFromArchiveName( $title, $repo, $archiveName)
Stable to override.
upgradeRow()
Stable to override.
recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user)
Record a file upload in the oldimage table, without adding log entries.
getCacheKey()
Stable to override.
static newFromRow( $row, $repo)
Stable to override.
uploadOld( $srcPath, $timestamp, $comment, $user)
Upload a file directly into archive.
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)
Stable to call.
loadFromDB( $flags=0)
Stable to override.
static newFromKey( $sha1, $repo, $timestamp=false)
Create a OldLocalFile from a SHA-1 key Do not call this except from inside a repo class.
getVisibility()
Returns bitfield value.
static newFromTitle( $title, $repo, $time=null)
Stable to override.
getArchiveName()
Stable to override.
string $archive_name
Archive name.
userCan( $field, User $user=null)
Determine if the current user is allowed to view a particular field of this image file,...
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.Stable to override string[...
string int $requestedTime
Timestamp.
isDeleted( $field)
loadExtraFromDB()
Load lazy file metadata from the DB Stable to override.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:60
const NS_FILE
Definition Defines.php:76
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42