MediaWiki REL1_37
OldLocalFile.php
Go to the documentation of this file.
1<?php
28
35class OldLocalFile extends LocalFile {
37 protected $requestedTime;
38
40 protected $archive_name;
41
42 public const CACHE_VERSION = 1;
43
52 public static function newFromTitle( $title, $repo, $time = null ) {
53 # The null default value is only here to avoid an E_STRICT
54 if ( $time === null ) {
55 throw new MWException( __METHOD__ . ' got null for $time parameter' );
56 }
57
58 return new static( $title, $repo, $time, null );
59 }
60
69 public static function newFromArchiveName( $title, $repo, $archiveName ) {
70 return new static( $title, $repo, null, $archiveName );
71 }
72
80 public static function newFromRow( $row, $repo ) {
81 $title = Title::makeTitle( NS_FILE, $row->oi_name );
82 $file = new static( $title, $repo, null, $row->oi_archive_name );
83 $file->loadFromRow( $row, 'oi_' );
84
85 return $file;
86 }
87
100 public static function newFromKey( $sha1, $repo, $timestamp = false ) {
101 $dbr = $repo->getReplicaDB();
102
103 $conds = [ 'oi_sha1' => $sha1 ];
104 if ( $timestamp ) {
105 $conds['oi_timestamp'] = $dbr->timestamp( $timestamp );
106 }
107
108 $fileQuery = static::getQueryInfo();
109 $row = $dbr->selectRow(
110 $fileQuery['tables'], $fileQuery['fields'], $conds, __METHOD__, [], $fileQuery['joins']
111 );
112 if ( $row ) {
113 return static::newFromRow( $row, $repo );
114 } else {
115 return false;
116 }
117 }
118
137 public static function getQueryInfo( array $options = [] ) {
138 $commentQuery = MediaWikiServices::getInstance()->getCommentStore()->getJoin( 'oi_description' );
139 $ret = [
140 'tables' => [
141 'oldimage',
142 'oldimage_actor' => 'actor'
143 ] + $commentQuery['tables'],
144 'fields' => [
145 'oi_name',
146 'oi_archive_name',
147 'oi_size',
148 'oi_width',
149 'oi_height',
150 'oi_bits',
151 'oi_media_type',
152 'oi_major_mime',
153 'oi_minor_mime',
154 'oi_timestamp',
155 'oi_deleted',
156 'oi_sha1',
157 'oi_actor',
158 'oi_user' => 'oldimage_actor.actor_user',
159 'oi_user_text' => 'oldimage_actor.actor_name'
160 ] + $commentQuery['fields'],
161 'joins' => [
162 'oldimage_actor' => [ 'JOIN', 'actor_id=oi_actor' ]
163 ] + $commentQuery['joins'],
164 ];
165
166 if ( in_array( 'omit-nonlazy', $options, true ) ) {
167 // Internal use only for getting only the lazy fields
168 $ret['fields'] = [];
169 }
170 if ( !in_array( 'omit-lazy', $options, true ) ) {
171 // Note: Keep this in sync with self::getLazyCacheFields()
172 $ret['fields'][] = 'oi_metadata';
173 }
174
175 return $ret;
176 }
177
187 public function __construct( $title, $repo, $time, $archiveName ) {
188 parent::__construct( $title, $repo );
189 $this->requestedTime = $time;
190 $this->archive_name = $archiveName;
191 if ( $time === null && $archiveName === null ) {
192 throw new MWException( __METHOD__ . ': must specify at least one of $time or $archiveName' );
193 }
194 }
195
196 public function loadFromRow( $row, $prefix = 'img_' ) {
197 $this->archive_name = $row->{"{$prefix}archive_name"};
198 $this->deleted = $row->{"{$prefix}deleted"};
199 $row = clone $row;
200 unset( $row->{"{$prefix}archive_name"} );
201 unset( $row->{"{$prefix}deleted"} );
202 parent::loadFromRow( $row, $prefix );
203 }
204
209 protected function getCacheKey() {
210 return false;
211 }
212
217 public function getArchiveName() {
218 if ( !isset( $this->archive_name ) ) {
219 $this->load();
220 }
221
222 return $this->archive_name;
223 }
224
228 public function isOld() {
229 return true;
230 }
231
235 public function isVisible() {
236 return $this->exists() && !$this->isDeleted( File::DELETED_FILE );
237 }
238
243 protected function loadFromDB( $flags = 0 ) {
244 $this->dataLoaded = true;
245
246 $dbr = ( $flags & self::READ_LATEST )
247 ? $this->repo->getPrimaryDB()
248 : $this->repo->getReplicaDB();
249
250 $conds = [ 'oi_name' => $this->getName() ];
251 if ( $this->requestedTime === null ) {
252 $conds['oi_archive_name'] = $this->archive_name;
253 } else {
254 $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime );
255 }
256 $fileQuery = static::getQueryInfo();
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 if ( $row ) {
266 $this->loadFromRow( $row, 'oi_' );
267 } else {
268 $this->fileExists = false;
269 }
270 }
271
276 protected function loadExtraFromDB() {
277 $this->extraDataLoaded = true;
278 $dbr = $this->repo->getReplicaDB();
279 $conds = [ 'oi_name' => $this->getName() ];
280 if ( $this->requestedTime === null ) {
281 $conds['oi_archive_name'] = $this->archive_name;
282 } else {
283 $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime );
284 }
285 $fileQuery = static::getQueryInfo( [ 'omit-nonlazy' ] );
286 // In theory the file could have just been renamed/deleted...oh well
287 $row = $dbr->selectRow(
288 $fileQuery['tables'],
289 $fileQuery['fields'],
290 $conds,
291 __METHOD__,
292 [ 'ORDER BY' => 'oi_timestamp DESC' ],
293 $fileQuery['joins']
294 );
295
296 if ( !$row ) { // fallback to primary DB
297 $dbr = $this->repo->getPrimaryDB();
298 $row = $dbr->selectRow(
299 $fileQuery['tables'],
300 $fileQuery['fields'],
301 $conds,
302 __METHOD__,
303 [ 'ORDER BY' => 'oi_timestamp DESC' ],
304 $fileQuery['joins']
305 );
306 }
307
308 if ( $row ) {
309 foreach ( $this->unprefixRow( $row, 'oi_' ) as $name => $value ) {
310 $this->$name = $value;
311 }
312 } else {
313 throw new MWException( "Could not find data for image '{$this->archive_name}'." );
314 }
315 }
316
321 protected function getCacheFields( $prefix = 'img_' ) {
322 $fields = parent::getCacheFields( $prefix );
323 $fields[] = $prefix . 'archive_name';
324 $fields[] = $prefix . 'deleted';
325
326 return $fields;
327 }
328
333 public function getRel() {
334 return $this->getArchiveRel( $this->getArchiveName() );
335 }
336
341 public function getUrlRel() {
342 return $this->getArchiveRel( rawurlencode( $this->getArchiveName() ) );
343 }
344
348 public function upgradeRow() {
349 $this->loadFromFile();
350
351 # Don't destroy file info of missing files
352 if ( !$this->fileExists ) {
353 wfDebug( __METHOD__ . ": file does not exist, aborting" );
354
355 return;
356 }
357
358 $dbw = $this->repo->getPrimaryDB();
359 list( $major, $minor ) = self::splitMime( $this->mime );
360
361 wfDebug( __METHOD__ . ': upgrading ' . $this->archive_name . " to the current schema" );
362 $dbw->update( 'oldimage',
363 [
364 'oi_size' => $this->size, // sanity
365 'oi_width' => $this->width,
366 'oi_height' => $this->height,
367 'oi_bits' => $this->bits,
368 'oi_media_type' => $this->media_type,
369 'oi_major_mime' => $major,
370 'oi_minor_mime' => $minor,
371 'oi_metadata' => $this->getMetadataForDb( $dbw ),
372 'oi_sha1' => $this->sha1,
373 ], [
374 'oi_name' => $this->getName(),
375 'oi_archive_name' => $this->archive_name ],
376 __METHOD__
377 );
378 }
379
380 protected function reserializeMetadata() {
381 // TODO: implement this and make it possible to hit it from refreshImageMetadata.php
382 // It can be hit from action=purge but that's not very useful if the
383 // goal is to reserialize the whole oldimage table.
384 }
385
391 public function isDeleted( $field ) {
392 $this->load();
393
394 return ( $this->deleted & $field ) == $field;
395 }
396
401 public function getVisibility() {
402 $this->load();
403
404 return (int)$this->deleted;
405 }
406
415 public function userCan( $field, Authority $performer ) {
416 $this->load();
417
418 return RevisionRecord::userCanBitfield(
419 $this->deleted,
420 $field,
421 $performer
422 );
423 }
424
434 public function uploadOld( $srcPath, $timestamp, $comment, UserIdentity $user ) {
435 $this->lock();
436
437 $archiveName = $this->getArchiveName();
438 $dstRel = $this->getArchiveRel( $archiveName );
439 $status = $this->publishTo( $srcPath, $dstRel );
440
441 if ( $status->isGood() &&
442 !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user )
443 ) {
444 $status->fatal( 'filenotfound', $srcPath );
445 }
446
447 $this->unlock();
448
449 return $status;
450 }
451
463 protected function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
464 $dbw = $this->repo->getPrimaryDB();
465
466 $services = MediaWikiServices::getInstance();
467 $mwProps = new MWFileProps( $services->getMimeAnalyzer() );
468 $props = $mwProps->getPropsFromPath( $srcPath, true );
469 if ( !$props['fileExists'] ) {
470 return false;
471 }
472 $this->setProps( $props );
473
474 $commentFields = $services->getCommentStore()
475 ->insert( $dbw, 'oi_description', $comment );
476 $actorId = $services->getActorNormalization()
477 ->acquireActorId( $user, $dbw );
478 $dbw->insert( 'oldimage',
479 [
480 'oi_name' => $this->getName(),
481 'oi_archive_name' => $archiveName,
482 'oi_size' => $props['size'],
483 'oi_width' => intval( $props['width'] ),
484 'oi_height' => intval( $props['height'] ),
485 'oi_bits' => $props['bits'],
486 'oi_actor' => $actorId,
487 'oi_timestamp' => $dbw->timestamp( $timestamp ),
488 'oi_metadata' => $this->getMetadataForDb( $dbw ),
489 'oi_media_type' => $props['media_type'],
490 'oi_major_mime' => $props['major_mime'],
491 'oi_minor_mime' => $props['minor_mime'],
492 'oi_sha1' => $props['sha1'],
493 ] + $commentFields, __METHOD__
494 );
495
496 return true;
497 }
498
506 public function exists() {
507 $archiveName = $this->getArchiveName();
508 if ( $archiveName === '' || !is_string( $archiveName ) ) {
509 return false;
510 }
511 return parent::exists();
512 }
513}
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:113
const DELETED_FILE
Definition File.php:70
Title string bool $title
Definition File.php:116
Class to represent a local file in the wiki's own database.
Definition LocalFile.php:63
string $timestamp
Upload timestamp.
string $sha1
SHA-1 base 36 content hash.
MediaWiki exception.
MimeMagic helper wrapper.
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)
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.to override string[] 1....
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.
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