MediaWiki REL1_34
OldLocalFile.php
Go to the documentation of this file.
1<?php
25
31class OldLocalFile extends LocalFile {
33 protected $requestedTime;
34
36 protected $archive_name;
37
38 const CACHE_VERSION = 1;
39 const MAX_CACHE_ROWS = 20;
40
48 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 static function newFromArchiveName( $title, $repo, $archiveName ) {
64 return new static( $title, $repo, null, $archiveName );
65 }
66
72 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 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 function __construct( $title, $repo, $time, $archiveName ) {
162 parent::__construct( $title, $repo );
163 $this->requestedTime = $time;
164 $this->archive_name = $archiveName;
165 if ( is_null( $time ) && is_null( $archiveName ) ) {
166 throw new MWException( __METHOD__ . ': must specify at least one of $time or $archiveName' );
167 }
168 }
169
173 function getCacheKey() {
174 return false;
175 }
176
180 function getArchiveName() {
181 if ( !isset( $this->archive_name ) ) {
182 $this->load();
183 }
184
185 return $this->archive_name;
186 }
187
191 function isOld() {
192 return true;
193 }
194
198 function isVisible() {
199 return $this->exists() && !$this->isDeleted( File::DELETED_FILE );
200 }
201
202 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 ( is_null( $this->requestedTime ) ) {
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 ( is_null( $this->requestedTime ) ) {
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 function getRel() {
288 return $this->getArchiveRel( $this->getArchiveName() );
289 }
290
294 function getUrlRel() {
295 return $this->getArchiveRel( rawurlencode( $this->getArchiveName() ) );
296 }
297
298 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 function isDeleted( $field ) {
336 $this->load();
337
338 return ( $this->deleted & $field ) == $field;
339 }
340
345 function getVisibility() {
346 $this->load();
347
348 return (int)$this->deleted;
349 }
350
359 function userCan( $field, User $user = null ) {
360 $this->load();
361
362 return Revision::userCanBitfield( $this->deleted, $field, $user );
363 }
364
374 public function uploadOld( $srcPath, $timestamp, $comment, $user ) {
375 $this->lock();
376
377 $archiveName = $this->getArchiveName();
378 $dstRel = $this->getArchiveRel( $archiveName );
379 $status = $this->publishTo( $srcPath, $dstRel );
380
381 if ( $status->isGood() &&
382 !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user )
383 ) {
384 $status->fatal( 'filenotfound', $srcPath );
385 }
386
387 $this->unlock();
388
389 return $status;
390 }
391
402 protected function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
403 $dbw = $this->repo->getMasterDB();
404
405 $dstPath = $this->repo->getZonePath( 'public' ) . '/' . $this->getRel();
406 $props = $this->repo->getFileProps( $dstPath );
407 if ( !$props['fileExists'] ) {
408 return false;
409 }
410
411 $commentFields = MediaWikiServices::getInstance()->getCommentStore()
412 ->insert( $dbw, 'oi_description', $comment );
413 $actorFields = ActorMigration::newMigration()->getInsertValues( $dbw, 'oi_user', $user );
414 $dbw->insert( 'oldimage',
415 [
416 'oi_name' => $this->getName(),
417 'oi_archive_name' => $archiveName,
418 'oi_size' => $props['size'],
419 'oi_width' => intval( $props['width'] ),
420 'oi_height' => intval( $props['height'] ),
421 'oi_bits' => $props['bits'],
422 'oi_timestamp' => $dbw->timestamp( $timestamp ),
423 'oi_metadata' => $props['metadata'],
424 'oi_media_type' => $props['media_type'],
425 'oi_major_mime' => $props['major_mime'],
426 'oi_minor_mime' => $props['minor_mime'],
427 'oi_sha1' => $props['sha1'],
428 ] + $commentFields + $actorFields, __METHOD__
429 );
430
431 return true;
432 }
433
441 public function exists() {
442 $archiveName = $this->getArchiveName();
443 if ( $archiveName === '' || !is_string( $archiveName ) ) {
444 return false;
445 }
446 return parent::exists();
447 }
448}
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
getName()
Return the name of this file.
Definition File.php:307
string $name
The name of a file from its title object.
Definition File.php:133
FileRepo LocalRepo ForeignAPIRepo bool $repo
Some member variables can be lazy-initialised using __get().
Definition File.php:106
const DELETED_FILE
Definition File.php:63
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
Title string bool $title
Definition File.php:109
getArchiveRel( $suffix=false)
Get the path of an archived file relative to the public zone root.
Definition File.php:1552
Class to represent a local file in the wiki's own database.
Definition LocalFile.php:56
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.
load( $flags=0)
Load file metadata from cache or DB, unless already loaded.
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:86
unprefixRow( $row, $prefix='img_')
int $deleted
Bitfield akin to rev_deleted.
Definition LocalFile.php:95
MediaWiki exception.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Class to represent a file in the oldimage table.
const MAX_CACHE_ROWS
static newFromArchiveName( $title, $repo, $archiveName)
upgradeRow()
Fix assorted version-related problems with the image row by reloading it from the file.
recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user)
Record a file upload in the oldimage table, without adding log entries.
static newFromRow( $row, $repo)
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)
loadFromDB( $flags=0)
Load file metadata from the DB.
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)
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.string[] 1....
string int $requestedTime
Timestamp.
isDeleted( $field)
loadExtraFromDB()
Load lazy file metadata from the DB.
static userCanBitfield( $bitfield, $field, User $user=null, Title $title=null)
Determine if the current user is allowed to view a particular field of this revision,...
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:51
const NS_FILE
Definition Defines.php:75
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42