MediaWiki  REL1_22
ArchivedFile.php
Go to the documentation of this file.
00001 <?php
00029 class ArchivedFile {
00033     var $id, # filearchive row ID
00034         $name, # image name
00035         $group, # FileStore storage group
00036         $key, # FileStore sha1 key
00037         $size, # file dimensions
00038         $bits,  # size in bytes
00039         $width, # width
00040         $height, # height
00041         $metadata, # metadata string
00042         $mime, # mime type
00043         $media_type, # media type
00044         $description, # upload description
00045         $user, # user ID of uploader
00046         $user_text, # user name of uploader
00047         $timestamp, # time of upload
00048         $dataLoaded, # Whether or not all this has been loaded from the database (loadFromXxx)
00049         $deleted, # Bitfield akin to rev_deleted
00050         $sha1, # sha1 hash of file content
00051         $pageCount,
00052         $archive_name;
00053 
00057     var $handler;
00061     var $title; # image title
00062 
00071     function __construct( $title, $id = 0, $key = '' ) {
00072         $this->id = -1;
00073         $this->title = false;
00074         $this->name = false;
00075         $this->group = 'deleted'; // needed for direct use of constructor
00076         $this->key = '';
00077         $this->size = 0;
00078         $this->bits = 0;
00079         $this->width = 0;
00080         $this->height = 0;
00081         $this->metadata = '';
00082         $this->mime = "unknown/unknown";
00083         $this->media_type = '';
00084         $this->description = '';
00085         $this->user = 0;
00086         $this->user_text = '';
00087         $this->timestamp = null;
00088         $this->deleted = 0;
00089         $this->dataLoaded = false;
00090         $this->exists = false;
00091         $this->sha1 = '';
00092 
00093         if ( $title instanceof Title ) {
00094             $this->title = File::normalizeTitle( $title, 'exception' );
00095             $this->name = $title->getDBkey();
00096         }
00097 
00098         if ( $id ) {
00099             $this->id = $id;
00100         }
00101 
00102         if ( $key ) {
00103             $this->key = $key;
00104         }
00105 
00106         if ( !$id && !$key && !( $title instanceof Title ) ) {
00107             throw new MWException( "No specifications provided to ArchivedFile constructor." );
00108         }
00109     }
00110 
00116     public function load() {
00117         if ( $this->dataLoaded ) {
00118             return true;
00119         }
00120         $conds = array();
00121 
00122         if ( $this->id > 0 ) {
00123             $conds['fa_id'] = $this->id;
00124         }
00125         if ( $this->key ) {
00126             $conds['fa_storage_group'] = $this->group;
00127             $conds['fa_storage_key'] = $this->key;
00128         }
00129         if ( $this->title ) {
00130             $conds['fa_name'] = $this->title->getDBkey();
00131         }
00132 
00133         if ( !count( $conds ) ) {
00134             throw new MWException( "No specific information for retrieving archived file" );
00135         }
00136 
00137         if ( !$this->title || $this->title->getNamespace() == NS_FILE ) {
00138             $this->dataLoaded = true; // set it here, to have also true on miss
00139             $dbr = wfGetDB( DB_SLAVE );
00140             $row = $dbr->selectRow(
00141                 'filearchive',
00142                 self::selectFields(),
00143                 $conds,
00144                 __METHOD__,
00145                 array( 'ORDER BY' => 'fa_timestamp DESC' )
00146             );
00147             if ( !$row ) {
00148                 // this revision does not exist?
00149                 return null;
00150             }
00151 
00152             // initialize fields for filestore image object
00153             $this->loadFromRow( $row );
00154         } else {
00155             throw new MWException( 'This title does not correspond to an image page.' );
00156         }
00157         $this->exists = true;
00158 
00159         return true;
00160     }
00161 
00169     public static function newFromRow( $row ) {
00170         $file = new ArchivedFile( Title::makeTitle( NS_FILE, $row->fa_name ) );
00171         $file->loadFromRow( $row );
00172         return $file;
00173     }
00174 
00179     static function selectFields() {
00180         return array(
00181             'fa_id',
00182             'fa_name',
00183             'fa_archive_name',
00184             'fa_storage_key',
00185             'fa_storage_group',
00186             'fa_size',
00187             'fa_bits',
00188             'fa_width',
00189             'fa_height',
00190             'fa_metadata',
00191             'fa_media_type',
00192             'fa_major_mime',
00193             'fa_minor_mime',
00194             'fa_description',
00195             'fa_user',
00196             'fa_user_text',
00197             'fa_timestamp',
00198             'fa_deleted',
00199             'fa_deleted_timestamp', /* Used by LocalFileRestoreBatch */
00200             'fa_sha1',
00201         );
00202     }
00203 
00210     public function loadFromRow( $row ) {
00211         $this->id = intval( $row->fa_id );
00212         $this->name = $row->fa_name;
00213         $this->archive_name = $row->fa_archive_name;
00214         $this->group = $row->fa_storage_group;
00215         $this->key = $row->fa_storage_key;
00216         $this->size = $row->fa_size;
00217         $this->bits = $row->fa_bits;
00218         $this->width = $row->fa_width;
00219         $this->height = $row->fa_height;
00220         $this->metadata = $row->fa_metadata;
00221         $this->mime = "$row->fa_major_mime/$row->fa_minor_mime";
00222         $this->media_type = $row->fa_media_type;
00223         $this->description = $row->fa_description;
00224         $this->user = $row->fa_user;
00225         $this->user_text = $row->fa_user_text;
00226         $this->timestamp = $row->fa_timestamp;
00227         $this->deleted = $row->fa_deleted;
00228         if ( isset( $row->fa_sha1 ) ) {
00229             $this->sha1 = $row->fa_sha1;
00230         } else {
00231             // old row, populate from key
00232             $this->sha1 = LocalRepo::getHashFromKey( $this->key );
00233         }
00234     }
00235 
00241     public function getTitle() {
00242         return $this->title;
00243     }
00244 
00250     public function getName() {
00251         return $this->name;
00252     }
00253 
00257     public function getID() {
00258         $this->load();
00259         return $this->id;
00260     }
00261 
00265     public function exists() {
00266         $this->load();
00267         return $this->exists;
00268     }
00269 
00274     public function getKey() {
00275         $this->load();
00276         return $this->key;
00277     }
00278 
00283     public function getStorageKey() {
00284         return $this->getKey();
00285     }
00286 
00291     public function getGroup() {
00292         return $this->group;
00293     }
00294 
00299     public function getWidth() {
00300         $this->load();
00301         return $this->width;
00302     }
00303 
00308     public function getHeight() {
00309         $this->load();
00310         return $this->height;
00311     }
00312 
00317     public function getMetadata() {
00318         $this->load();
00319         return $this->metadata;
00320     }
00321 
00326     public function getSize() {
00327         $this->load();
00328         return $this->size;
00329     }
00330 
00335     public function getBits() {
00336         $this->load();
00337         return $this->bits;
00338     }
00339 
00344     public function getMimeType() {
00345         $this->load();
00346         return $this->mime;
00347     }
00348 
00353     function getHandler() {
00354         if ( !isset( $this->handler ) ) {
00355             $this->handler = MediaHandler::getHandler( $this->getMimeType() );
00356         }
00357         return $this->handler;
00358     }
00359 
00364     function pageCount() {
00365         if ( !isset( $this->pageCount ) ) {
00366             if ( $this->getHandler() && $this->handler->isMultiPage( $this ) ) {
00367                 $this->pageCount = $this->handler->pageCount( $this );
00368             } else {
00369                 $this->pageCount = false;
00370             }
00371         }
00372         return $this->pageCount;
00373     }
00374 
00380     public function getMediaType() {
00381         $this->load();
00382         return $this->media_type;
00383     }
00384 
00390     public function getTimestamp() {
00391         $this->load();
00392         return wfTimestamp( TS_MW, $this->timestamp );
00393     }
00394 
00401     function getSha1() {
00402         $this->load();
00403         return $this->sha1;
00404     }
00405 
00411     public function getUser() {
00412         $this->load();
00413         if ( $this->isDeleted( File::DELETED_USER ) ) {
00414             return 0;
00415         } else {
00416             return $this->user;
00417         }
00418     }
00419 
00425     public function getUserText() {
00426         $this->load();
00427         if ( $this->isDeleted( File::DELETED_USER ) ) {
00428             return 0;
00429         } else {
00430             return $this->user_text;
00431         }
00432     }
00433 
00439     public function getDescription() {
00440         $this->load();
00441         if ( $this->isDeleted( File::DELETED_COMMENT ) ) {
00442             return 0;
00443         } else {
00444             return $this->description;
00445         }
00446     }
00447 
00453     public function getRawUser() {
00454         $this->load();
00455         return $this->user;
00456     }
00457 
00463     public function getRawUserText() {
00464         $this->load();
00465         return $this->user_text;
00466     }
00467 
00473     public function getRawDescription() {
00474         $this->load();
00475         return $this->description;
00476     }
00477 
00482     public function getVisibility() {
00483         $this->load();
00484         return $this->deleted;
00485     }
00486 
00493     public function isDeleted( $field ) {
00494         $this->load();
00495         return ( $this->deleted & $field ) == $field;
00496     }
00497 
00505     public function userCan( $field, User $user = null ) {
00506         $this->load();
00507         return Revision::userCanBitfield( $this->deleted, $field, $user );
00508     }
00509 }