MediaWiki  REL1_19
ArchivedFile.php
Go to the documentation of this file.
00001 <?php
00014 class ArchivedFile {
00018         var $id, # filearchive row ID
00019                 $name, # image name
00020                 $group, # FileStore storage group
00021                 $key, # FileStore sha1 key
00022                 $size, # file dimensions
00023                 $bits,  # size in bytes
00024                 $width, # width
00025                 $height, # height
00026                 $metadata, # metadata string
00027                 $mime, # mime type
00028                 $media_type, # media type
00029                 $description, # upload description
00030                 $user, # user ID of uploader
00031                 $user_text, # user name of uploader
00032                 $timestamp, # time of upload
00033                 $dataLoaded, # Whether or not all this has been loaded from the database (loadFromXxx)
00034                 $deleted, # Bitfield akin to rev_deleted
00035                 $pageCount,
00036                 $archive_name;
00037 
00041         var $handler;
00045         var $title; # image title
00046 
00055         function __construct( $title, $id=0, $key='' ) {
00056                 $this->id = -1;
00057                 $this->title = false;
00058                 $this->name = false;
00059                 $this->group = 'deleted'; // needed for direct use of constructor
00060                 $this->key = '';
00061                 $this->size = 0;
00062                 $this->bits = 0;
00063                 $this->width = 0;
00064                 $this->height = 0;
00065                 $this->metadata = '';
00066                 $this->mime = "unknown/unknown";
00067                 $this->media_type = '';
00068                 $this->description = '';
00069                 $this->user = 0;
00070                 $this->user_text = '';
00071                 $this->timestamp = null;
00072                 $this->deleted = 0;
00073                 $this->dataLoaded = false;
00074                 $this->exists = false;
00075 
00076                 if( $title instanceof Title ) {
00077                         $this->title = File::normalizeTitle( $title, 'exception' );
00078                         $this->name = $title->getDBkey();
00079                 }
00080 
00081                 if ($id) {
00082                         $this->id = $id;
00083                 }
00084 
00085                 if ($key) {
00086                         $this->key = $key;
00087                 }
00088 
00089                 if ( !$id && !$key && !( $title instanceof Title ) ) {
00090                         throw new MWException( "No specifications provided to ArchivedFile constructor." );
00091                 }
00092         }
00093 
00098         public function load() {
00099                 if ( $this->dataLoaded ) {
00100                         return true;
00101                 }
00102                 $conds = array();
00103 
00104                 if( $this->id > 0 ) {
00105                         $conds['fa_id'] = $this->id;
00106                 }
00107                 if( $this->key ) {
00108                         $conds['fa_storage_group'] = $this->group;
00109                         $conds['fa_storage_key'] = $this->key;
00110                 }
00111                 if( $this->title ) {
00112                         $conds['fa_name'] = $this->title->getDBkey();
00113                 }
00114 
00115                 if( !count($conds)) {
00116                         throw new MWException( "No specific information for retrieving archived file" );
00117                 }
00118 
00119                 if( !$this->title || $this->title->getNamespace() == NS_FILE ) {
00120                         $dbr = wfGetDB( DB_SLAVE );
00121                         $res = $dbr->select( 'filearchive',
00122                                 array(
00123                                         'fa_id',
00124                                         'fa_name',
00125                                         'fa_archive_name',
00126                                         'fa_storage_key',
00127                                         'fa_storage_group',
00128                                         'fa_size',
00129                                         'fa_bits',
00130                                         'fa_width',
00131                                         'fa_height',
00132                                         'fa_metadata',
00133                                         'fa_media_type',
00134                                         'fa_major_mime',
00135                                         'fa_minor_mime',
00136                                         'fa_description',
00137                                         'fa_user',
00138                                         'fa_user_text',
00139                                         'fa_timestamp',
00140                                         'fa_deleted' ),
00141                                 $conds,
00142                                 __METHOD__,
00143                                 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
00144                         if ( $res == false || $dbr->numRows( $res ) == 0 ) {
00145                         // this revision does not exist?
00146                                 return;
00147                         }
00148                         $ret = $dbr->resultObject( $res );
00149                         $row = $ret->fetchObject();
00150 
00151                         // initialize fields for filestore image object
00152                         $this->id = intval($row->fa_id);
00153                         $this->name = $row->fa_name;
00154                         $this->archive_name = $row->fa_archive_name;
00155                         $this->group = $row->fa_storage_group;
00156                         $this->key = $row->fa_storage_key;
00157                         $this->size = $row->fa_size;
00158                         $this->bits = $row->fa_bits;
00159                         $this->width = $row->fa_width;
00160                         $this->height = $row->fa_height;
00161                         $this->metadata = $row->fa_metadata;
00162                         $this->mime = "$row->fa_major_mime/$row->fa_minor_mime";
00163                         $this->media_type = $row->fa_media_type;
00164                         $this->description = $row->fa_description;
00165                         $this->user = $row->fa_user;
00166                         $this->user_text = $row->fa_user_text;
00167                         $this->timestamp = $row->fa_timestamp;
00168                         $this->deleted = $row->fa_deleted;
00169                 } else {
00170                         throw new MWException( 'This title does not correspond to an image page.' );
00171                 }
00172                 $this->dataLoaded = true;
00173                 $this->exists = true;
00174 
00175                 return true;
00176         }
00177 
00185         public static function newFromRow( $row ) {
00186                 $file = new ArchivedFile( Title::makeTitle( NS_FILE, $row->fa_name ) );
00187 
00188                 $file->id = intval($row->fa_id);
00189                 $file->name = $row->fa_name;
00190                 $file->archive_name = $row->fa_archive_name;
00191                 $file->group = $row->fa_storage_group;
00192                 $file->key = $row->fa_storage_key;
00193                 $file->size = $row->fa_size;
00194                 $file->bits = $row->fa_bits;
00195                 $file->width = $row->fa_width;
00196                 $file->height = $row->fa_height;
00197                 $file->metadata = $row->fa_metadata;
00198                 $file->mime = "$row->fa_major_mime/$row->fa_minor_mime";
00199                 $file->media_type = $row->fa_media_type;
00200                 $file->description = $row->fa_description;
00201                 $file->user = $row->fa_user;
00202                 $file->user_text = $row->fa_user_text;
00203                 $file->timestamp = $row->fa_timestamp;
00204                 $file->deleted = $row->fa_deleted;
00205 
00206                 return $file;
00207         }
00208 
00214         public function getTitle() {
00215                 return $this->title;
00216         }
00217 
00223         public function getName() {
00224                 return $this->name;
00225         }
00226 
00230         public function getID() {
00231                 $this->load();
00232                 return $this->id;
00233         }
00234 
00238         public function exists() {
00239                 $this->load();
00240                 return $this->exists;
00241         }
00242 
00247         public function getKey() {
00248                 $this->load();
00249                 return $this->key;
00250         }
00251 
00256         public function getStorageKey() {
00257                 return $this->getKey();
00258         }
00259 
00264         public function getGroup() {
00265                 return $this->group;
00266         }
00267 
00272         public function getWidth() {
00273                 $this->load();
00274                 return $this->width;
00275         }
00276 
00281         public function getHeight() {
00282                 $this->load();
00283                 return $this->height;
00284         }
00285 
00290         public function getMetadata() {
00291                 $this->load();
00292                 return $this->metadata;
00293         }
00294 
00299         public function getSize() {
00300                 $this->load();
00301                 return $this->size;
00302         }
00303 
00308         public function getBits() {
00309                 $this->load();
00310                 return $this->bits;
00311         }
00312 
00317         public function getMimeType() {
00318                 $this->load();
00319                 return $this->mime;
00320         }
00321 
00326         function getHandler() {
00327                 if ( !isset( $this->handler ) ) {
00328                         $this->handler = MediaHandler::getHandler( $this->getMimeType() );
00329                 }
00330                 return $this->handler;
00331         }
00332 
00337         function pageCount() {
00338                 if ( !isset( $this->pageCount ) ) {
00339                         if ( $this->getHandler() && $this->handler->isMultiPage( $this ) ) {
00340                                 $this->pageCount = $this->handler->pageCount( $this );
00341                         } else {
00342                                 $this->pageCount = false;
00343                         }
00344                 }
00345                 return $this->pageCount;
00346         }
00347 
00353         public function getMediaType() {
00354                 $this->load();
00355                 return $this->media_type;
00356         }
00357 
00363         public function getTimestamp() {
00364                 $this->load();
00365                 return wfTimestamp( TS_MW, $this->timestamp );
00366         }
00367 
00373         public function getUser() {
00374                 $this->load();
00375                 if( $this->isDeleted( File::DELETED_USER ) ) {
00376                         return 0;
00377                 } else {
00378                         return $this->user;
00379                 }
00380         }
00381 
00387         public function getUserText() {
00388                 $this->load();
00389                 if( $this->isDeleted( File::DELETED_USER ) ) {
00390                         return 0;
00391                 } else {
00392                         return $this->user_text;
00393                 }
00394         }
00395 
00401         public function getDescription() {
00402                 $this->load();
00403                 if( $this->isDeleted( File::DELETED_COMMENT ) ) {
00404                         return 0;
00405                 } else {
00406                         return $this->description;
00407                 }
00408         }
00409 
00415         public function getRawUser() {
00416                 $this->load();
00417                 return $this->user;
00418         }
00419 
00425         public function getRawUserText() {
00426                 $this->load();
00427                 return $this->user_text;
00428         }
00429 
00435         public function getRawDescription() {
00436                 $this->load();
00437                 return $this->description;
00438         }
00439 
00444         public function getVisibility() {
00445                 $this->load();
00446                 return $this->deleted;
00447         }
00448 
00455         public function isDeleted( $field ) {
00456                 $this->load();
00457                 return ($this->deleted & $field) == $field;
00458         }
00459 
00467         public function userCan( $field, User $user = null ) {
00468                 $this->load();
00469                 return Revision::userCanBitfield( $this->deleted, $field, $user );
00470         }
00471 }