MediaWiki REL1_28
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 $user_text;
71
73 private $timestamp;
74
76 private $dataLoaded;
77
79 private $deleted;
80
82 private $sha1;
83
87 private $pageCount;
88
91
93 protected $handler;
94
96 protected $title; # image title
97
105 function __construct( $title, $id = 0, $key = '', $sha1 = '' ) {
106 $this->id = -1;
107 $this->title = false;
108 $this->name = false;
109 $this->group = 'deleted'; // needed for direct use of constructor
110 $this->key = '';
111 $this->size = 0;
112 $this->bits = 0;
113 $this->width = 0;
114 $this->height = 0;
115 $this->metadata = '';
116 $this->mime = "unknown/unknown";
117 $this->media_type = '';
118 $this->description = '';
119 $this->user = 0;
120 $this->user_text = '';
121 $this->timestamp = null;
122 $this->deleted = 0;
123 $this->dataLoaded = false;
124 $this->exists = false;
125 $this->sha1 = '';
126
127 if ( $title instanceof Title ) {
128 $this->title = File::normalizeTitle( $title, 'exception' );
129 $this->name = $title->getDBkey();
130 }
131
132 if ( $id ) {
133 $this->id = $id;
134 }
135
136 if ( $key ) {
137 $this->key = $key;
138 }
139
140 if ( $sha1 ) {
141 $this->sha1 = $sha1;
142 }
143
144 if ( !$id && !$key && !( $title instanceof Title ) && !$sha1 ) {
145 throw new MWException( "No specifications provided to ArchivedFile constructor." );
146 }
147 }
148
154 public function load() {
155 if ( $this->dataLoaded ) {
156 return true;
157 }
158 $conds = [];
159
160 if ( $this->id > 0 ) {
161 $conds['fa_id'] = $this->id;
162 }
163 if ( $this->key ) {
164 $conds['fa_storage_group'] = $this->group;
165 $conds['fa_storage_key'] = $this->key;
166 }
167 if ( $this->title ) {
168 $conds['fa_name'] = $this->title->getDBkey();
169 }
170 if ( $this->sha1 ) {
171 $conds['fa_sha1'] = $this->sha1;
172 }
173
174 if ( !count( $conds ) ) {
175 throw new MWException( "No specific information for retrieving archived file" );
176 }
177
178 if ( !$this->title || $this->title->getNamespace() == NS_FILE ) {
179 $this->dataLoaded = true; // set it here, to have also true on miss
181 $row = $dbr->selectRow(
182 'filearchive',
183 self::selectFields(),
184 $conds,
185 __METHOD__,
186 [ 'ORDER BY' => 'fa_timestamp DESC' ]
187 );
188 if ( !$row ) {
189 // this revision does not exist?
190 return null;
191 }
192
193 // initialize fields for filestore image object
194 $this->loadFromRow( $row );
195 } else {
196 throw new MWException( 'This title does not correspond to an image page.' );
197 }
198 $this->exists = true;
199
200 return true;
201 }
202
209 public static function newFromRow( $row ) {
210 $file = new ArchivedFile( Title::makeTitle( NS_FILE, $row->fa_name ) );
211 $file->loadFromRow( $row );
212
213 return $file;
214 }
215
220 static function selectFields() {
221 return [
222 'fa_id',
223 'fa_name',
224 'fa_archive_name',
225 'fa_storage_key',
226 'fa_storage_group',
227 'fa_size',
228 'fa_bits',
229 'fa_width',
230 'fa_height',
231 'fa_metadata',
232 'fa_media_type',
233 'fa_major_mime',
234 'fa_minor_mime',
235 'fa_description',
236 'fa_user',
237 'fa_user_text',
238 'fa_timestamp',
239 'fa_deleted',
240 'fa_deleted_timestamp', /* Used by LocalFileRestoreBatch */
241 'fa_sha1',
242 ];
243 }
244
251 public function loadFromRow( $row ) {
252 $this->id = intval( $row->fa_id );
253 $this->name = $row->fa_name;
254 $this->archive_name = $row->fa_archive_name;
255 $this->group = $row->fa_storage_group;
256 $this->key = $row->fa_storage_key;
257 $this->size = $row->fa_size;
258 $this->bits = $row->fa_bits;
259 $this->width = $row->fa_width;
260 $this->height = $row->fa_height;
261 $this->metadata = $row->fa_metadata;
262 $this->mime = "$row->fa_major_mime/$row->fa_minor_mime";
263 $this->media_type = $row->fa_media_type;
264 $this->description = $row->fa_description;
265 $this->user = $row->fa_user;
266 $this->user_text = $row->fa_user_text;
267 $this->timestamp = $row->fa_timestamp;
268 $this->deleted = $row->fa_deleted;
269 if ( isset( $row->fa_sha1 ) ) {
270 $this->sha1 = $row->fa_sha1;
271 } else {
272 // old row, populate from key
273 $this->sha1 = LocalRepo::getHashFromKey( $this->key );
274 }
275 if ( !$this->title ) {
276 $this->title = Title::makeTitleSafe( NS_FILE, $row->fa_name );
277 }
278 }
279
285 public function getTitle() {
286 if ( !$this->title ) {
287 $this->load();
288 }
289 return $this->title;
290 }
291
297 public function getName() {
298 if ( $this->name === false ) {
299 $this->load();
300 }
301
302 return $this->name;
303 }
304
308 public function getID() {
309 $this->load();
310
311 return $this->id;
312 }
313
317 public function exists() {
318 $this->load();
319
320 return $this->exists;
321 }
322
327 public function getKey() {
328 $this->load();
329
330 return $this->key;
331 }
332
337 public function getStorageKey() {
338 return $this->getKey();
339 }
340
345 public function getGroup() {
346 return $this->group;
347 }
348
353 public function getWidth() {
354 $this->load();
355
356 return $this->width;
357 }
358
363 public function getHeight() {
364 $this->load();
365
366 return $this->height;
367 }
368
373 public function getMetadata() {
374 $this->load();
375
376 return $this->metadata;
377 }
378
383 public function getSize() {
384 $this->load();
385
386 return $this->size;
387 }
388
393 public function getBits() {
394 $this->load();
395
396 return $this->bits;
397 }
398
403 public function getMimeType() {
404 $this->load();
405
406 return $this->mime;
407 }
408
413 function getHandler() {
414 if ( !isset( $this->handler ) ) {
415 $this->handler = MediaHandler::getHandler( $this->getMimeType() );
416 }
417
418 return $this->handler;
419 }
420
426 function pageCount() {
427 if ( !isset( $this->pageCount ) ) {
428 // @FIXME: callers expect File objects
429 if ( $this->getHandler() && $this->handler->isMultiPage( $this ) ) {
430 $this->pageCount = $this->handler->pageCount( $this );
431 } else {
432 $this->pageCount = false;
433 }
434 }
435
436 return $this->pageCount;
437 }
438
444 public function getMediaType() {
445 $this->load();
446
447 return $this->media_type;
448 }
449
455 public function getTimestamp() {
456 $this->load();
457
458 return wfTimestamp( TS_MW, $this->timestamp );
459 }
460
467 function getSha1() {
468 $this->load();
469
470 return $this->sha1;
471 }
472
483 public function getUser( $type = 'text' ) {
484 $this->load();
485
486 if ( $type == 'text' ) {
487 return $this->user_text;
488 } elseif ( $type == 'id' ) {
489 return (int)$this->user;
490 }
491
492 throw new MWException( "Unknown type '$type'." );
493 }
494
501 public function getUserText() {
502 wfDeprecated( __METHOD__, '1.23' );
503 $this->load();
504 if ( $this->isDeleted( File::DELETED_USER ) ) {
505 return 0;
506 } else {
507 return $this->user_text;
508 }
509 }
510
516 public function getDescription() {
517 $this->load();
518 if ( $this->isDeleted( File::DELETED_COMMENT ) ) {
519 return 0;
520 } else {
521 return $this->description;
522 }
523 }
524
530 public function getRawUser() {
531 $this->load();
532
533 return $this->user;
534 }
535
541 public function getRawUserText() {
542 $this->load();
543
544 return $this->user_text;
545 }
546
552 public function getRawDescription() {
553 $this->load();
554
555 return $this->description;
556 }
557
562 public function getVisibility() {
563 $this->load();
564
565 return $this->deleted;
566 }
567
574 public function isDeleted( $field ) {
575 $this->load();
576
577 return ( $this->deleted & $field ) == $field;
578 }
579
587 public function userCan( $field, User $user = null ) {
588 $this->load();
589
590 $title = $this->getTitle();
591 return Revision::userCanBitfield( $this->deleted, $field, $user, $title ?: null );
592 }
593}
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.
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.
int $user
User ID of uploader.
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.
getHandler()
Get a MediaHandler instance for this file.
loadFromRow( $row)
Load ArchivedFile object fields from a DB row.
getRawUserText()
Return the user name of the uploader.
string $mime
MIME type.
string $pageCount
Number of pages of a multipage document, or false for documents which aren't multipage documents.
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.
getUserText()
Return the user name of the uploader.
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.
string $user_text
User name of uploader.
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:183
const DELETED_COMMENT
Definition File.php:53
const DELETED_USER
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.
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,...
Represents a title within MediaWiki.
Definition Title.php:36
getDBkey()
Get the main part with underscores.
Definition Title.php:898
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:48
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:62
namespace are movable Hooks may change this value to override the return value of MWNamespace::isMovable(). 'NewDifferenceEngine' do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached one of or reset my talk my contributions etc etc otherwise the built in rate limiting checks are if enabled allows for interception of redirect as a string mapping parameter names to values & $type
Definition hooks.txt:2568
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:952
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:22
const TS_MW
MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS)
Definition defines.php:11