MediaWiki  1.31.0
ArchivedFile.php
Go to the documentation of this file.
1 <?php
29 class ArchivedFile {
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 
87  private $archive_name;
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
176  $dbr = wfGetDB( DB_REPLICA );
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 }
ArchivedFile\loadFromRow
loadFromRow( $row)
Load ArchivedFile object fields from a DB row.
Definition: ArchivedFile.php:300
ArchivedFile\$deleted
int $deleted
Bitfield akin to rev_deleted.
Definition: ArchivedFile.php:76
ArchivedFile\exists
exists()
Definition: ArchivedFile.php:367
ArchivedFile\getID
getID()
Definition: ArchivedFile.php:358
ArchivedFile\$sha1
string $sha1
SHA-1 hash of file content.
Definition: ArchivedFile.php:79
ArchivedFile\getKey
getKey()
Return the FileStore key.
Definition: ArchivedFile.php:377
ArchivedFile\getTimestamp
getTimestamp()
Return upload timestamp.
Definition: ArchivedFile.php:505
Revision\userCanBitfield
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,...
Definition: Revision.php:1205
ArchivedFile\$metadata
string $metadata
Metadata string.
Definition: ArchivedFile.php:55
ArchivedFile\$dataLoaded
bool $dataLoaded
Whether or not all this has been loaded from the database (loadFromXxx)
Definition: ArchivedFile.php:73
ArchivedFile\$group
string $group
FileStore storage group.
Definition: ArchivedFile.php:37
captcha-old.count
count
Definition: captcha-old.py:249
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1968
ArchivedFile\$width
int $width
Width.
Definition: ArchivedFile.php:49
ArchivedFile\isDeleted
isDeleted( $field)
for file or revision rows
Definition: ArchivedFile.php:607
NS_FILE
const NS_FILE
Definition: Defines.php:71
MIGRATION_WRITE_BOTH
const MIGRATION_WRITE_BOTH
Definition: Defines.php:294
ArchivedFile\getQueryInfo
static getQueryInfo()
Return the tables, fields, and join conditions to be selected to create a new archivedfile object.
Definition: ArchivedFile.php:266
User\newFromAnyId
static newFromAnyId( $userId, $userName, $actorId)
Static factory method for creation from an ID, name, and/or actor ID.
Definition: User.php:657
ArchivedFile\$key
string $key
FileStore SHA-1 key.
Definition: ArchivedFile.php:40
ArchivedFile\$bits
int $bits
Size in bytes.
Definition: ArchivedFile.php:46
ArchivedFile\getStorageKey
getStorageKey()
Return the FileStore key (overriding base File class)
Definition: ArchivedFile.php:387
ActorMigration\newMigration
static newMigration()
Static constructor.
Definition: ActorMigration.php:89
LocalRepo\getHashFromKey
static getHashFromKey( $key)
Gets the SHA1 hash from a storage key.
Definition: LocalRepo.php:181
File\normalizeTitle
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
php
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:35
ArchivedFile\$archive_name
string $archive_name
Original base filename.
Definition: ArchivedFile.php:87
$dbr
$dbr
Definition: testCompression.php:50
ArchivedFile\getMetadata
getMetadata()
Get handler-specific metadata.
Definition: ArchivedFile.php:423
ArchivedFile\$user
User null $user
Uploader.
Definition: ArchivedFile.php:67
key
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:25
ArchivedFile\getSize
getSize()
Return the size of the image file, in bytes.
Definition: ArchivedFile.php:433
handler
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:783
ArchivedFile\load
load()
Loads a file object from the filearchive table.
Definition: ArchivedFile.php:150
Title\getDBkey
getDBkey()
Get the main part with underscores.
Definition: Title.php:947
ArchivedFile\getHandler
getHandler()
Get a MediaHandler instance for this file.
Definition: ArchivedFile.php:463
MWException
MediaWiki exception.
Definition: MWException.php:26
ArchivedFile\$id
int $id
Filearchive row ID.
Definition: ArchivedFile.php:31
File\DELETED_COMMENT
const DELETED_COMMENT
Definition: File.php:54
user
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
Definition: distributors.txt:9
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
Definition: GlobalFunctions.php:1111
ArchivedFile\getMimeType
getMimeType()
Returns the MIME type of the file.
Definition: ArchivedFile.php:453
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2800
ArchivedFile\$mime
string $mime
MIME type.
Definition: ArchivedFile.php:58
ArchivedFile\getTitle
getTitle()
Return the associated title object.
Definition: ArchivedFile.php:335
ArchivedFile\pageCount
pageCount()
Returns the number of pages of a multipage document, or false for documents which aren't multipage do...
Definition: ArchivedFile.php:476
ArchivedFile\getVisibility
getVisibility()
Returns the deletion bitfield.
Definition: ArchivedFile.php:595
ArchivedFile\__construct
__construct( $title, $id=0, $key='', $sha1='')
Definition: ArchivedFile.php:102
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:534
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
ArchivedFile\$size
int $size
File size in bytes.
Definition: ArchivedFile.php:43
ArchivedFile\$handler
MediaHandler $handler
Definition: ArchivedFile.php:90
ArchivedFile\userCan
userCan( $field, User $user=null)
Determine if the current user is allowed to view a particular field of this FileStore image file,...
Definition: ArchivedFile.php:620
ArchivedFile\getDescription
getDescription()
Return upload description.
Definition: ArchivedFile.php:553
ArchivedFile\$title
Title $title
Definition: ArchivedFile.php:93
MIGRATION_OLD
const MIGRATION_OLD
Definition: Defines.php:293
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:562
ArchivedFile
Class representing a row of the 'filearchive' table.
Definition: ArchivedFile.php:29
title
title
Definition: parserTests.txt:219
ArchivedFile\$pageCount
int false $pageCount
Number of pages of a multipage document, or false for documents which aren't multipage documents.
Definition: ArchivedFile.php:84
ArchivedFile\getSha1
getSha1()
Get the SHA-1 base 36 hash of the file.
Definition: ArchivedFile.php:517
ArchivedFile\getName
getName()
Return the file name.
Definition: ArchivedFile.php:347
ArchivedFile\getGroup
getGroup()
Return the FileStore storage group.
Definition: ArchivedFile.php:395
ArchivedFile\getWidth
getWidth()
Return the width of the image.
Definition: ArchivedFile.php:403
ArchivedFile\getRawUserText
getRawUserText()
Return the user name of the uploader.
Definition: ArchivedFile.php:576
ArchivedFile\getUser
getUser( $type='text')
Returns ID or name of user who uploaded the file.
Definition: ArchivedFile.php:534
ArchivedFile\$timestamp
string $timestamp
Time of upload.
Definition: ArchivedFile.php:70
Title
Represents a title within MediaWiki.
Definition: Title.php:39
ArchivedFile\getRawDescription
getRawDescription()
Return upload description.
Definition: ArchivedFile.php:585
MediaHandler\getHandler
static getHandler( $type)
Get a MediaHandler for a given MIME type from the instance cache.
Definition: MediaHandler.php:46
ArchivedFile\$description
string $description
Upload description.
Definition: ArchivedFile.php:64
ArchivedFile\getHeight
getHeight()
Return the height of the image.
Definition: ArchivedFile.php:413
width
width
Definition: parserTests.txt:163
name
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
ArchivedFile\$name
string $name
File name.
Definition: ArchivedFile.php:34
ArchivedFile\getBits
getBits()
Return the bits of the image file, in bytes.
Definition: ArchivedFile.php:443
ArchivedFile\selectFields
static selectFields()
Fields in the filearchive table.
Definition: ArchivedFile.php:219
ArchivedFile\$height
int $height
Height.
Definition: ArchivedFile.php:52
CommentStore\getStore
static getStore()
Definition: CommentStore.php:130
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:53
group
no text was provided for refs named< code > blankwithnoreference</code ></span ></li ></ol ></div > ! end ! test with< references/> in group ! wikitext Wikipedia rocks< ref > Proceeds of vol XXI</ref > Wikipedia rocks< ref group=note > Proceeds of vol XXI</ref >< references/>< references group=note/> ! html< p > Wikipedia rocks< sup id="cite_ref-1" class="reference">< a href="#cite_note-1"> &Wikipedia rocks< sup id="cite_ref-2" class="reference">< a href="#cite_note-2"> &</p >< div class="mw-references-wrap">< ol class="references">< li id="cite_note-1">< span class="mw-cite-backlink">< a href="#cite_ref-1"> ↑</a ></span >< span class="reference-text"> Proceeds of vol XXI</span ></li ></ol ></div >< div class="mw-references-wrap">< ol class="references">< li id="cite_note-2">< span class="mw-cite-backlink">< a href="#cite_ref-2"> ↑</a ></span >< span class="reference-text"> Proceeds of vol XXI</span ></li ></ol ></div > ! end ! test with< references/> in group
Definition: citeParserTests.txt:312
ArchivedFile\getRawUser
getRawUser()
Return the user ID of the uploader.
Definition: ArchivedFile.php:567
MediaHandler
Base media handler class.
Definition: MediaHandler.php:30
ArchivedFile\$media_type
string $media_type
Media type.
Definition: ArchivedFile.php:61
ArchivedFile\newFromRow
static newFromRow( $row)
Loads a file object from the filearchive table.
Definition: ArchivedFile.php:207
ArchivedFile\getMediaType
getMediaType()
Return the type of the media in the file.
Definition: ArchivedFile.php:494
$type
$type
Definition: testCompression.php:48
$wgActorTableSchemaMigrationStage
int $wgActorTableSchemaMigrationStage
Actor table schema migration stage.
Definition: DefaultSettings.php:8822