MediaWiki  1.30.2
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 $user_text;
71 
73  private $timestamp;
74 
76  private $dataLoaded;
77 
79  private $deleted;
80 
82  private $sha1;
83 
87  private $pageCount;
88 
90  private $archive_name;
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
180  $dbr = wfGetDB( DB_REPLICA );
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 
222  static function selectFields() {
223  return [
224  'fa_id',
225  'fa_name',
226  'fa_archive_name',
227  'fa_storage_key',
228  'fa_storage_group',
229  'fa_size',
230  'fa_bits',
231  'fa_width',
232  'fa_height',
233  'fa_metadata',
234  'fa_media_type',
235  'fa_major_mime',
236  'fa_minor_mime',
237  'fa_user',
238  'fa_user_text',
239  'fa_timestamp',
240  'fa_deleted',
241  'fa_deleted_timestamp', /* Used by LocalFileRestoreBatch */
242  'fa_sha1',
243  ] + CommentStore::newKey( 'fa_description' )->getFields();
244  }
245 
252  public function loadFromRow( $row ) {
253  $this->id = intval( $row->fa_id );
254  $this->name = $row->fa_name;
255  $this->archive_name = $row->fa_archive_name;
256  $this->group = $row->fa_storage_group;
257  $this->key = $row->fa_storage_key;
258  $this->size = $row->fa_size;
259  $this->bits = $row->fa_bits;
260  $this->width = $row->fa_width;
261  $this->height = $row->fa_height;
262  $this->metadata = $row->fa_metadata;
263  $this->mime = "$row->fa_major_mime/$row->fa_minor_mime";
264  $this->media_type = $row->fa_media_type;
265  $this->description = CommentStore::newKey( 'fa_description' )
266  // Legacy because $row probably came from self::selectFields()
267  ->getCommentLegacy( wfGetDB( DB_REPLICA ), $row )->text;
268  $this->user = $row->fa_user;
269  $this->user_text = $row->fa_user_text;
270  $this->timestamp = $row->fa_timestamp;
271  $this->deleted = $row->fa_deleted;
272  if ( isset( $row->fa_sha1 ) ) {
273  $this->sha1 = $row->fa_sha1;
274  } else {
275  // old row, populate from key
276  $this->sha1 = LocalRepo::getHashFromKey( $this->key );
277  }
278  if ( !$this->title ) {
279  $this->title = Title::makeTitleSafe( NS_FILE, $row->fa_name );
280  }
281  }
282 
288  public function getTitle() {
289  if ( !$this->title ) {
290  $this->load();
291  }
292  return $this->title;
293  }
294 
300  public function getName() {
301  if ( $this->name === false ) {
302  $this->load();
303  }
304 
305  return $this->name;
306  }
307 
311  public function getID() {
312  $this->load();
313 
314  return $this->id;
315  }
316 
320  public function exists() {
321  $this->load();
322 
323  return $this->exists;
324  }
325 
330  public function getKey() {
331  $this->load();
332 
333  return $this->key;
334  }
335 
340  public function getStorageKey() {
341  return $this->getKey();
342  }
343 
348  public function getGroup() {
349  return $this->group;
350  }
351 
356  public function getWidth() {
357  $this->load();
358 
359  return $this->width;
360  }
361 
366  public function getHeight() {
367  $this->load();
368 
369  return $this->height;
370  }
371 
376  public function getMetadata() {
377  $this->load();
378 
379  return $this->metadata;
380  }
381 
386  public function getSize() {
387  $this->load();
388 
389  return $this->size;
390  }
391 
396  public function getBits() {
397  $this->load();
398 
399  return $this->bits;
400  }
401 
406  public function getMimeType() {
407  $this->load();
408 
409  return $this->mime;
410  }
411 
416  function getHandler() {
417  if ( !isset( $this->handler ) ) {
418  $this->handler = MediaHandler::getHandler( $this->getMimeType() );
419  }
420 
421  return $this->handler;
422  }
423 
429  function pageCount() {
430  if ( !isset( $this->pageCount ) ) {
431  // @FIXME: callers expect File objects
432  if ( $this->getHandler() && $this->handler->isMultiPage( $this ) ) {
433  $this->pageCount = $this->handler->pageCount( $this );
434  } else {
435  $this->pageCount = false;
436  }
437  }
438 
439  return $this->pageCount;
440  }
441 
447  public function getMediaType() {
448  $this->load();
449 
450  return $this->media_type;
451  }
452 
458  public function getTimestamp() {
459  $this->load();
460 
461  return wfTimestamp( TS_MW, $this->timestamp );
462  }
463 
470  function getSha1() {
471  $this->load();
472 
473  return $this->sha1;
474  }
475 
486  public function getUser( $type = 'text' ) {
487  $this->load();
488 
489  if ( $type == 'text' ) {
490  return $this->user_text;
491  } elseif ( $type == 'id' ) {
492  return (int)$this->user;
493  }
494 
495  throw new MWException( "Unknown type '$type'." );
496  }
497 
503  public function getDescription() {
504  $this->load();
505  if ( $this->isDeleted( File::DELETED_COMMENT ) ) {
506  return 0;
507  } else {
508  return $this->description;
509  }
510  }
511 
517  public function getRawUser() {
518  $this->load();
519 
520  return $this->user;
521  }
522 
528  public function getRawUserText() {
529  $this->load();
530 
531  return $this->user_text;
532  }
533 
539  public function getRawDescription() {
540  $this->load();
541 
542  return $this->description;
543  }
544 
549  public function getVisibility() {
550  $this->load();
551 
552  return $this->deleted;
553  }
554 
561  public function isDeleted( $field ) {
562  $this->load();
563 
564  return ( $this->deleted & $field ) == $field;
565  }
566 
574  public function userCan( $field, User $user = null ) {
575  $this->load();
576 
577  $title = $this->getTitle();
578  return Revision::userCanBitfield( $this->deleted, $field, $user, $title ?: null );
579  }
580 }
ArchivedFile\loadFromRow
loadFromRow( $row)
Load ArchivedFile object fields from a DB row.
Definition: ArchivedFile.php:252
ArchivedFile\$deleted
int $deleted
Bitfield akin to rev_deleted.
Definition: ArchivedFile.php:79
ArchivedFile\exists
exists()
Definition: ArchivedFile.php:320
ArchivedFile\getID
getID()
Definition: ArchivedFile.php:311
ArchivedFile\$sha1
string $sha1
SHA-1 hash of file content.
Definition: ArchivedFile.php:82
ArchivedFile\getKey
getKey()
Return the FileStore key.
Definition: ArchivedFile.php:330
ArchivedFile\getTimestamp
getTimestamp()
Return upload timestamp.
Definition: ArchivedFile.php:458
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:1798
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:76
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:2040
ArchivedFile\$width
int $width
Width.
Definition: ArchivedFile.php:49
ArchivedFile\isDeleted
isDeleted( $field)
for file or revision rows
Definition: ArchivedFile.php:561
NS_FILE
const NS_FILE
Definition: Defines.php:71
ArchivedFile\$key
string $key
FileStore SHA-1 key.
Definition: ArchivedFile.php:40
ArchivedFile\$bits
int $bits
Size in bytes.
Definition: ArchivedFile.php:46
CommentStore\newKey
static newKey( $key)
Static constructor for easier chaining.
Definition: CommentStore.php:114
ArchivedFile\getStorageKey
getStorageKey()
Return the FileStore key (overriding base File class)
Definition: ArchivedFile.php:340
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:90
ArchivedFile\getMetadata
getMetadata()
Get handler-specific metadata.
Definition: ArchivedFile.php:376
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
title
to move a page</td >< td > &*You are moving the page across *A non empty talk page already exists under the new or *You uncheck the box below In those you will have to move or merge the page manually if desired</td >< td > be sure to &You are responsible for making sure that links continue to point where they are supposed to go Note that the page will &a page at the new title
Definition: All_system_messages.txt:2696
ArchivedFile\getSize
getSize()
Return the size of the image file, in bytes.
Definition: ArchivedFile.php:386
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:781
ArchivedFile\load
load()
Loads a file object from the filearchive table.
Definition: ArchivedFile.php:154
Title\getDBkey
getDBkey()
Get the main part with underscores.
Definition: Title.php:955
ArchivedFile\getHandler
getHandler()
Get a MediaHandler instance for this file.
Definition: ArchivedFile.php:416
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
ArchivedFile\getMimeType
getMimeType()
Returns the MIME type of the file.
Definition: ArchivedFile.php:406
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2856
ArchivedFile\$mime
string $mime
MIME type.
Definition: ArchivedFile.php:58
ArchivedFile\getTitle
getTitle()
Return the associated title object.
Definition: ArchivedFile.php:288
ArchivedFile\pageCount
pageCount()
Returns the number of pages of a multipage document, or false for documents which aren't multipage do...
Definition: ArchivedFile.php:429
ArchivedFile\getVisibility
getVisibility()
Returns the deletion bitfield.
Definition: ArchivedFile.php:549
ArchivedFile\__construct
__construct( $title, $id=0, $key='', $sha1='')
Definition: ArchivedFile.php:105
ArchivedFile\$user
int $user
User ID of uploader.
Definition: ArchivedFile.php:67
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:529
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:93
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:574
ArchivedFile\$user_text
string $user_text
User name of uploader.
Definition: ArchivedFile.php:70
ArchivedFile\getDescription
getDescription()
Return upload description.
Definition: ArchivedFile.php:503
ArchivedFile\$title
Title $title
Definition: ArchivedFile.php:96
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:557
ArchivedFile
Class representing a row of the 'filearchive' table.
Definition: ArchivedFile.php:29
ArchivedFile\$pageCount
int false $pageCount
Number of pages of a multipage document, or false for documents which aren't multipage documents.
Definition: ArchivedFile.php:87
ArchivedFile\getSha1
getSha1()
Get the SHA-1 base 36 hash of the file.
Definition: ArchivedFile.php:470
ArchivedFile\getName
getName()
Return the file name.
Definition: ArchivedFile.php:300
ArchivedFile\getGroup
getGroup()
Return the FileStore storage group.
Definition: ArchivedFile.php:348
ArchivedFile\getWidth
getWidth()
Return the width of the image.
Definition: ArchivedFile.php:356
ArchivedFile\getRawUserText
getRawUserText()
Return the user name of the uploader.
Definition: ArchivedFile.php:528
ArchivedFile\getUser
getUser( $type='text')
Returns ID or name of user who uploaded the file.
Definition: ArchivedFile.php:486
ArchivedFile\$timestamp
string $timestamp
Time of upload.
Definition: ArchivedFile.php:73
Title
Represents a title within MediaWiki.
Definition: Title.php:39
ArchivedFile\getRawDescription
getRawDescription()
Return upload description.
Definition: ArchivedFile.php:539
$dbr
if(! $regexes) $dbr
Definition: cleanup.php:94
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:366
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:396
ArchivedFile\selectFields
static selectFields()
Fields in the filearchive table.
Definition: ArchivedFile.php:222
ArchivedFile\$height
int $height
Height.
Definition: ArchivedFile.php:52
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:51
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:306
ArchivedFile\getRawUser
getRawUser()
Return the user ID of the uploader.
Definition: ArchivedFile.php:517
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:209
ArchivedFile\getMediaType
getMediaType()
Return the type of the media in the file.
Definition: ArchivedFile.php:447
$type
$type
Definition: testCompression.php:48