MediaWiki  REL1_24
ArchivedFile.php
Go to the documentation of this file.
00001 <?php
00029 class ArchivedFile {
00031     private $id;
00032 
00034     private $name;
00035 
00037     private $group;
00038 
00040     private $key;
00041 
00043     private $size;
00044 
00046     private $bits;
00047 
00049     private $width;
00050 
00052     private $height;
00053 
00055     private $metadata;
00056 
00058     private $mime;
00059 
00061     private $media_type;
00062 
00064     private $description;
00065 
00067     private $user;
00068 
00070     private $user_text;
00071 
00073     private $timestamp;
00074 
00076     private $dataLoaded;
00077 
00079     private $deleted;
00080 
00082     private $sha1;
00083 
00087     private $pageCount;
00088 
00090     private $archive_name;
00091 
00093     protected $handler;
00094 
00096     protected $title; # image title
00097 
00104     function __construct( $title, $id = 0, $key = '' ) {
00105         $this->id = -1;
00106         $this->title = false;
00107         $this->name = false;
00108         $this->group = 'deleted'; // needed for direct use of constructor
00109         $this->key = '';
00110         $this->size = 0;
00111         $this->bits = 0;
00112         $this->width = 0;
00113         $this->height = 0;
00114         $this->metadata = '';
00115         $this->mime = "unknown/unknown";
00116         $this->media_type = '';
00117         $this->description = '';
00118         $this->user = 0;
00119         $this->user_text = '';
00120         $this->timestamp = null;
00121         $this->deleted = 0;
00122         $this->dataLoaded = false;
00123         $this->exists = false;
00124         $this->sha1 = '';
00125 
00126         if ( $title instanceof Title ) {
00127             $this->title = File::normalizeTitle( $title, 'exception' );
00128             $this->name = $title->getDBkey();
00129         }
00130 
00131         if ( $id ) {
00132             $this->id = $id;
00133         }
00134 
00135         if ( $key ) {
00136             $this->key = $key;
00137         }
00138 
00139         if ( !$id && !$key && !( $title instanceof Title ) ) {
00140             throw new MWException( "No specifications provided to ArchivedFile constructor." );
00141         }
00142     }
00143 
00149     public function load() {
00150         if ( $this->dataLoaded ) {
00151             return true;
00152         }
00153         $conds = array();
00154 
00155         if ( $this->id > 0 ) {
00156             $conds['fa_id'] = $this->id;
00157         }
00158         if ( $this->key ) {
00159             $conds['fa_storage_group'] = $this->group;
00160             $conds['fa_storage_key'] = $this->key;
00161         }
00162         if ( $this->title ) {
00163             $conds['fa_name'] = $this->title->getDBkey();
00164         }
00165 
00166         if ( !count( $conds ) ) {
00167             throw new MWException( "No specific information for retrieving archived file" );
00168         }
00169 
00170         if ( !$this->title || $this->title->getNamespace() == NS_FILE ) {
00171             $this->dataLoaded = true; // set it here, to have also true on miss
00172             $dbr = wfGetDB( DB_SLAVE );
00173             $row = $dbr->selectRow(
00174                 'filearchive',
00175                 self::selectFields(),
00176                 $conds,
00177                 __METHOD__,
00178                 array( 'ORDER BY' => 'fa_timestamp DESC' )
00179             );
00180             if ( !$row ) {
00181                 // this revision does not exist?
00182                 return null;
00183             }
00184 
00185             // initialize fields for filestore image object
00186             $this->loadFromRow( $row );
00187         } else {
00188             throw new MWException( 'This title does not correspond to an image page.' );
00189         }
00190         $this->exists = true;
00191 
00192         return true;
00193     }
00194 
00201     public static function newFromRow( $row ) {
00202         $file = new ArchivedFile( Title::makeTitle( NS_FILE, $row->fa_name ) );
00203         $file->loadFromRow( $row );
00204 
00205         return $file;
00206     }
00207 
00212     static function selectFields() {
00213         return array(
00214             'fa_id',
00215             'fa_name',
00216             'fa_archive_name',
00217             'fa_storage_key',
00218             'fa_storage_group',
00219             'fa_size',
00220             'fa_bits',
00221             'fa_width',
00222             'fa_height',
00223             'fa_metadata',
00224             'fa_media_type',
00225             'fa_major_mime',
00226             'fa_minor_mime',
00227             'fa_description',
00228             'fa_user',
00229             'fa_user_text',
00230             'fa_timestamp',
00231             'fa_deleted',
00232             'fa_deleted_timestamp', /* Used by LocalFileRestoreBatch */
00233             'fa_sha1',
00234         );
00235     }
00236 
00243     public function loadFromRow( $row ) {
00244         $this->id = intval( $row->fa_id );
00245         $this->name = $row->fa_name;
00246         $this->archive_name = $row->fa_archive_name;
00247         $this->group = $row->fa_storage_group;
00248         $this->key = $row->fa_storage_key;
00249         $this->size = $row->fa_size;
00250         $this->bits = $row->fa_bits;
00251         $this->width = $row->fa_width;
00252         $this->height = $row->fa_height;
00253         $this->metadata = $row->fa_metadata;
00254         $this->mime = "$row->fa_major_mime/$row->fa_minor_mime";
00255         $this->media_type = $row->fa_media_type;
00256         $this->description = $row->fa_description;
00257         $this->user = $row->fa_user;
00258         $this->user_text = $row->fa_user_text;
00259         $this->timestamp = $row->fa_timestamp;
00260         $this->deleted = $row->fa_deleted;
00261         if ( isset( $row->fa_sha1 ) ) {
00262             $this->sha1 = $row->fa_sha1;
00263         } else {
00264             // old row, populate from key
00265             $this->sha1 = LocalRepo::getHashFromKey( $this->key );
00266         }
00267         if ( !$this->title ) {
00268             $this->title = Title::makeTitleSafe( NS_FILE, $row->fa_name );
00269         }
00270     }
00271 
00277     public function getTitle() {
00278         if ( !$this->title ) {
00279             $this->load();
00280         }
00281         return $this->title;
00282     }
00283 
00289     public function getName() {
00290         if ( $this->name === false ) {
00291             $this->load();
00292         }
00293 
00294         return $this->name;
00295     }
00296 
00300     public function getID() {
00301         $this->load();
00302 
00303         return $this->id;
00304     }
00305 
00309     public function exists() {
00310         $this->load();
00311 
00312         return $this->exists;
00313     }
00314 
00319     public function getKey() {
00320         $this->load();
00321 
00322         return $this->key;
00323     }
00324 
00329     public function getStorageKey() {
00330         return $this->getKey();
00331     }
00332 
00337     public function getGroup() {
00338         return $this->group;
00339     }
00340 
00345     public function getWidth() {
00346         $this->load();
00347 
00348         return $this->width;
00349     }
00350 
00355     public function getHeight() {
00356         $this->load();
00357 
00358         return $this->height;
00359     }
00360 
00365     public function getMetadata() {
00366         $this->load();
00367 
00368         return $this->metadata;
00369     }
00370 
00375     public function getSize() {
00376         $this->load();
00377 
00378         return $this->size;
00379     }
00380 
00385     public function getBits() {
00386         $this->load();
00387 
00388         return $this->bits;
00389     }
00390 
00395     public function getMimeType() {
00396         $this->load();
00397 
00398         return $this->mime;
00399     }
00400 
00405     function getHandler() {
00406         if ( !isset( $this->handler ) ) {
00407             $this->handler = MediaHandler::getHandler( $this->getMimeType() );
00408         }
00409 
00410         return $this->handler;
00411     }
00412 
00418     function pageCount() {
00419         if ( !isset( $this->pageCount ) ) {
00420             if ( $this->getHandler() && $this->handler->isMultiPage( $this ) ) {
00421                 $this->pageCount = $this->handler->pageCount( $this );
00422             } else {
00423                 $this->pageCount = false;
00424             }
00425         }
00426 
00427         return $this->pageCount;
00428     }
00429 
00435     public function getMediaType() {
00436         $this->load();
00437 
00438         return $this->media_type;
00439     }
00440 
00446     public function getTimestamp() {
00447         $this->load();
00448 
00449         return wfTimestamp( TS_MW, $this->timestamp );
00450     }
00451 
00458     function getSha1() {
00459         $this->load();
00460 
00461         return $this->sha1;
00462     }
00463 
00474     public function getUser( $type = 'text' ) {
00475         $this->load();
00476 
00477         if ( $type == 'text' ) {
00478             return $this->user_text;
00479         } elseif ( $type == 'id' ) {
00480             return $this->user;
00481         }
00482 
00483         throw new MWException( "Unknown type '$type'." );
00484     }
00485 
00492     public function getUserText() {
00493         wfDeprecated( __METHOD__, '1.23' );
00494         $this->load();
00495         if ( $this->isDeleted( File::DELETED_USER ) ) {
00496             return 0;
00497         } else {
00498             return $this->user_text;
00499         }
00500     }
00501 
00507     public function getDescription() {
00508         $this->load();
00509         if ( $this->isDeleted( File::DELETED_COMMENT ) ) {
00510             return 0;
00511         } else {
00512             return $this->description;
00513         }
00514     }
00515 
00521     public function getRawUser() {
00522         $this->load();
00523 
00524         return $this->user;
00525     }
00526 
00532     public function getRawUserText() {
00533         $this->load();
00534 
00535         return $this->user_text;
00536     }
00537 
00543     public function getRawDescription() {
00544         $this->load();
00545 
00546         return $this->description;
00547     }
00548 
00553     public function getVisibility() {
00554         $this->load();
00555 
00556         return $this->deleted;
00557     }
00558 
00565     public function isDeleted( $field ) {
00566         $this->load();
00567 
00568         return ( $this->deleted & $field ) == $field;
00569     }
00570 
00578     public function userCan( $field, User $user = null ) {
00579         $this->load();
00580 
00581         $title = $this->getTitle();
00582         return Revision::userCanBitfield( $this->deleted, $field, $user, $title ? : null );
00583     }
00584 }