MediaWiki REL1_31
ArchivedFile.php
Go to the documentation of this file.
1<?php
31 private $id;
32
34 private $name;
35
37 private $group;
38
40 private $key;
41
43 private $size;
44
46 private $bits;
47
49 private $width;
50
52 private $height;
53
55 private $metadata;
56
58 private $mime;
59
61 private $media_type;
62
64 private $description;
65
67 private $user;
68
70 private $timestamp;
71
73 private $dataLoaded;
74
76 private $deleted;
77
79 private $sha1;
80
84 private $pageCount;
85
88
90 protected $handler;
91
93 protected $title; # image title
94
102 function __construct( $title, $id = 0, $key = '', $sha1 = '' ) {
103 $this->id = -1;
104 $this->title = false;
105 $this->name = false;
106 $this->group = 'deleted'; // needed for direct use of constructor
107 $this->key = '';
108 $this->size = 0;
109 $this->bits = 0;
110 $this->width = 0;
111 $this->height = 0;
112 $this->metadata = '';
113 $this->mime = "unknown/unknown";
114 $this->media_type = '';
115 $this->description = '';
116 $this->user = null;
117 $this->timestamp = null;
118 $this->deleted = 0;
119 $this->dataLoaded = false;
120 $this->exists = false;
121 $this->sha1 = '';
122
123 if ( $title instanceof Title ) {
124 $this->title = File::normalizeTitle( $title, 'exception' );
125 $this->name = $title->getDBkey();
126 }
127
128 if ( $id ) {
129 $this->id = $id;
130 }
131
132 if ( $key ) {
133 $this->key = $key;
134 }
135
136 if ( $sha1 ) {
137 $this->sha1 = $sha1;
138 }
139
140 if ( !$id && !$key && !( $title instanceof Title ) && !$sha1 ) {
141 throw new MWException( "No specifications provided to ArchivedFile constructor." );
142 }
143 }
144
150 public function load() {
151 if ( $this->dataLoaded ) {
152 return true;
153 }
154 $conds = [];
155
156 if ( $this->id > 0 ) {
157 $conds['fa_id'] = $this->id;
158 }
159 if ( $this->key ) {
160 $conds['fa_storage_group'] = $this->group;
161 $conds['fa_storage_key'] = $this->key;
162 }
163 if ( $this->title ) {
164 $conds['fa_name'] = $this->title->getDBkey();
165 }
166 if ( $this->sha1 ) {
167 $conds['fa_sha1'] = $this->sha1;
168 }
169
170 if ( !count( $conds ) ) {
171 throw new MWException( "No specific information for retrieving archived file" );
172 }
173
174 if ( !$this->title || $this->title->getNamespace() == NS_FILE ) {
175 $this->dataLoaded = true; // set it here, to have also true on miss
177 $fileQuery = self::getQueryInfo();
178 $row = $dbr->selectRow(
179 $fileQuery['tables'],
180 $fileQuery['fields'],
181 $conds,
182 __METHOD__,
183 [ 'ORDER BY' => 'fa_timestamp DESC' ],
184 $fileQuery['joins']
185 );
186 if ( !$row ) {
187 // this revision does not exist?
188 return null;
189 }
190
191 // initialize fields for filestore image object
192 $this->loadFromRow( $row );
193 } else {
194 throw new MWException( 'This title does not correspond to an image page.' );
195 }
196 $this->exists = true;
197
198 return true;
199 }
200
207 public static function newFromRow( $row ) {
208 $file = new ArchivedFile( Title::makeTitle( NS_FILE, $row->fa_name ) );
209 $file->loadFromRow( $row );
210
211 return $file;
212 }
213
219 static function selectFields() {
221
223 // If code is using this instead of self::getQueryInfo(), there's a
224 // decent chance it's going to try to directly access
225 // $row->fa_user or $row->fa_user_text and we can't give it
226 // useful values here once those aren't being written anymore.
227 throw new BadMethodCallException(
228 'Cannot use ' . __METHOD__ . ' when $wgActorTableSchemaMigrationStage > MIGRATION_WRITE_BOTH'
229 );
230 }
231
232 wfDeprecated( __METHOD__, '1.31' );
233 return [
234 'fa_id',
235 'fa_name',
236 'fa_archive_name',
237 'fa_storage_key',
238 'fa_storage_group',
239 'fa_size',
240 'fa_bits',
241 'fa_width',
242 'fa_height',
243 'fa_metadata',
244 'fa_media_type',
245 'fa_major_mime',
246 'fa_minor_mime',
247 'fa_user',
248 'fa_user_text',
249 'fa_actor' => $wgActorTableSchemaMigrationStage > MIGRATION_OLD ? 'fa_actor' : 'NULL',
250 'fa_timestamp',
251 'fa_deleted',
252 'fa_deleted_timestamp', /* Used by LocalFileRestoreBatch */
253 'fa_sha1',
254 ] + CommentStore::getStore()->getFields( 'fa_description' );
255 }
256
266 public static function getQueryInfo() {
267 $commentQuery = CommentStore::getStore()->getJoin( 'fa_description' );
268 $actorQuery = ActorMigration::newMigration()->getJoin( 'fa_user' );
269 return [
270 'tables' => [ 'filearchive' ] + $commentQuery['tables'] + $actorQuery['tables'],
271 'fields' => [
272 'fa_id',
273 'fa_name',
274 'fa_archive_name',
275 'fa_storage_key',
276 'fa_storage_group',
277 'fa_size',
278 'fa_bits',
279 'fa_width',
280 'fa_height',
281 'fa_metadata',
282 'fa_media_type',
283 'fa_major_mime',
284 'fa_minor_mime',
285 'fa_timestamp',
286 'fa_deleted',
287 'fa_deleted_timestamp', /* Used by LocalFileRestoreBatch */
288 'fa_sha1',
289 ] + $commentQuery['fields'] + $actorQuery['fields'],
290 'joins' => $commentQuery['joins'] + $actorQuery['joins'],
291 ];
292 }
293
300 public function loadFromRow( $row ) {
301 $this->id = intval( $row->fa_id );
302 $this->name = $row->fa_name;
303 $this->archive_name = $row->fa_archive_name;
304 $this->group = $row->fa_storage_group;
305 $this->key = $row->fa_storage_key;
306 $this->size = $row->fa_size;
307 $this->bits = $row->fa_bits;
308 $this->width = $row->fa_width;
309 $this->height = $row->fa_height;
310 $this->metadata = $row->fa_metadata;
311 $this->mime = "$row->fa_major_mime/$row->fa_minor_mime";
312 $this->media_type = $row->fa_media_type;
313 $this->description = CommentStore::getStore()
314 // Legacy because $row may have come from self::selectFields()
315 ->getCommentLegacy( wfGetDB( DB_REPLICA ), 'fa_description', $row )->text;
316 $this->user = User::newFromAnyId( $row->fa_user, $row->fa_user_text, $row->fa_actor );
317 $this->timestamp = $row->fa_timestamp;
318 $this->deleted = $row->fa_deleted;
319 if ( isset( $row->fa_sha1 ) ) {
320 $this->sha1 = $row->fa_sha1;
321 } else {
322 // old row, populate from key
323 $this->sha1 = LocalRepo::getHashFromKey( $this->key );
324 }
325 if ( !$this->title ) {
326 $this->title = Title::makeTitleSafe( NS_FILE, $row->fa_name );
327 }
328 }
329
335 public function getTitle() {
336 if ( !$this->title ) {
337 $this->load();
338 }
339 return $this->title;
340 }
341
347 public function getName() {
348 if ( $this->name === false ) {
349 $this->load();
350 }
351
352 return $this->name;
353 }
354
358 public function getID() {
359 $this->load();
360
361 return $this->id;
362 }
363
367 public function exists() {
368 $this->load();
369
370 return $this->exists;
371 }
372
377 public function getKey() {
378 $this->load();
379
380 return $this->key;
381 }
382
387 public function getStorageKey() {
388 return $this->getKey();
389 }
390
395 public function getGroup() {
396 return $this->group;
397 }
398
403 public function getWidth() {
404 $this->load();
405
406 return $this->width;
407 }
408
413 public function getHeight() {
414 $this->load();
415
416 return $this->height;
417 }
418
423 public function getMetadata() {
424 $this->load();
425
426 return $this->metadata;
427 }
428
433 public function getSize() {
434 $this->load();
435
436 return $this->size;
437 }
438
443 public function getBits() {
444 $this->load();
445
446 return $this->bits;
447 }
448
453 public function getMimeType() {
454 $this->load();
455
456 return $this->mime;
457 }
458
463 function getHandler() {
464 if ( !isset( $this->handler ) ) {
465 $this->handler = MediaHandler::getHandler( $this->getMimeType() );
466 }
467
468 return $this->handler;
469 }
470
476 function pageCount() {
477 if ( !isset( $this->pageCount ) ) {
478 // @FIXME: callers expect File objects
479 if ( $this->getHandler() && $this->handler->isMultiPage( $this ) ) {
480 $this->pageCount = $this->handler->pageCount( $this );
481 } else {
482 $this->pageCount = false;
483 }
484 }
485
486 return $this->pageCount;
487 }
488
494 public function getMediaType() {
495 $this->load();
496
497 return $this->media_type;
498 }
499
505 public function getTimestamp() {
506 $this->load();
507
508 return wfTimestamp( TS_MW, $this->timestamp );
509 }
510
517 function getSha1() {
518 $this->load();
519
520 return $this->sha1;
521 }
522
534 public function getUser( $type = 'text' ) {
535 $this->load();
536
537 if ( $type === 'object' ) {
538 return $this->user;
539 } elseif ( $type === 'text' ) {
540 return $this->user ? $this->user->getName() : '';
541 } elseif ( $type === 'id' ) {
542 return $this->user ? $this->user->getId() : 0;
543 }
544
545 throw new MWException( "Unknown type '$type'." );
546 }
547
553 public function getDescription() {
554 $this->load();
555 if ( $this->isDeleted( File::DELETED_COMMENT ) ) {
556 return 0;
557 } else {
558 return $this->description;
559 }
560 }
561
567 public function getRawUser() {
568 return $this->getUser( 'id' );
569 }
570
576 public function getRawUserText() {
577 return $this->getUser( 'text' );
578 }
579
585 public function getRawDescription() {
586 $this->load();
587
588 return $this->description;
589 }
590
595 public function getVisibility() {
596 $this->load();
597
598 return $this->deleted;
599 }
600
607 public function isDeleted( $field ) {
608 $this->load();
609
610 return ( $this->deleted & $field ) == $field;
611 }
612
620 public function userCan( $field, User $user = null ) {
621 $this->load();
622
623 $title = $this->getTitle();
624 return Revision::userCanBitfield( $this->deleted, $field, $user, $title ?: null );
625 }
626}
int $wgActorTableSchemaMigrationStage
Actor table schema migration stage.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
Class representing a row of the 'filearchive' table.
getTimestamp()
Return upload timestamp.
User null $user
Uploader.
getSize()
Return the size of the image file, in bytes.
MediaHandler $handler
bool $dataLoaded
Whether or not all this has been loaded from the database (loadFromXxx)
isDeleted( $field)
for file or revision rows
userCan( $field, User $user=null)
Determine if the current user is allowed to view a particular field of this FileStore image file,...
getHeight()
Return the height of the image.
int $height
Height.
getBits()
Return the bits of the image file, in bytes.
int $size
File size in bytes.
string $sha1
SHA-1 hash of file content.
string $media_type
Media type.
string $key
FileStore SHA-1 key.
static selectFields()
Fields in the filearchive table.
string $group
FileStore storage group.
string $name
File name.
string $description
Upload description.
getSha1()
Get the SHA-1 base 36 hash of the file.
string $archive_name
Original base filename.
int false $pageCount
Number of pages of a multipage document, or false for documents which aren't multipage documents.
getHandler()
Get a MediaHandler instance for this file.
static getQueryInfo()
Return the tables, fields, and join conditions to be selected to create a new archivedfile object.
loadFromRow( $row)
Load ArchivedFile object fields from a DB row.
getRawUserText()
Return the user name of the uploader.
string $mime
MIME type.
getDescription()
Return upload description.
int $id
Filearchive row ID.
getRawUser()
Return the user ID of the uploader.
__construct( $title, $id=0, $key='', $sha1='')
load()
Loads a file object from the filearchive table.
pageCount()
Returns the number of pages of a multipage document, or false for documents which aren't multipage do...
getGroup()
Return the FileStore storage group.
static newFromRow( $row)
Loads a file object from the filearchive table.
getTitle()
Return the associated title object.
getWidth()
Return the width of the image.
int $deleted
Bitfield akin to rev_deleted.
getStorageKey()
Return the FileStore key (overriding base File class)
getMimeType()
Returns the MIME type of the file.
getUser( $type='text')
Returns ID or name of user who uploaded the file.
getKey()
Return the FileStore key.
string $metadata
Metadata string.
string $timestamp
Time of upload.
getName()
Return the file name.
int $bits
Size in bytes.
getMediaType()
Return the type of the media in the file.
getRawDescription()
Return upload description.
getVisibility()
Returns the deletion bitfield.
getMetadata()
Get handler-specific metadata.
int $width
Width.
static normalizeTitle( $title, $exception=false)
Given a string or Title object return either a valid Title object with namespace NS_FILE or null.
Definition File.php:184
const DELETED_COMMENT
Definition File.php:54
static getHashFromKey( $key)
Gets the SHA1 hash from a storage key.
MediaWiki exception.
Base media handler class.
static getHandler( $type)
Get a MediaHandler for a given MIME type from the instance cache.
Represents a title within MediaWiki.
Definition Title.php:39
getDBkey()
Get the main part with underscores.
Definition Title.php:947
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:53
static newFromAnyId( $userId, $userName, $actorId)
Static factory method for creation from an ID, name, and/or actor ID.
Definition User.php:657
when a variable name is used in a it is silently declared as a new local masking the global
Definition design.txt:95
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at name
Definition design.txt:12
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling but I prefer the flexibility This should also do the output encoding The system allocates a global one in $wgOut Title Represents the title of an and does all the work of translating among various forms such as plain database key
Definition design.txt:26
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such and we might be restricted by PHP settings such as safe mode or open_basedir We cannot assume that the software even has read access anywhere useful Many shared hosts run all users web applications under the same user
Wikitext formatted, in the key only.
const NS_FILE
Definition Defines.php:80
const MIGRATION_WRITE_BOTH
Definition Defines.php:303
const MIGRATION_OLD
Definition Defines.php:302
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check set to true or false to override the $wgMaxImageArea check result gives extension the possibility to transform it themselves set to a MediaTransformOutput the error message to be returned in an array you should do so by altering $wgNamespaceProtection and $wgNamespaceContentModels outside the handler
Definition hooks.txt:930
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
title
width
const DB_REPLICA
Definition defines.php:25