MediaWiki  REL1_22
OldLocalFile.php
Go to the documentation of this file.
00001 <?php
00029 class OldLocalFile extends LocalFile {
00030     var $requestedTime, $archive_name;
00031 
00032     const CACHE_VERSION = 1;
00033     const MAX_CACHE_ROWS = 20;
00034 
00042     static function newFromTitle( $title, $repo, $time = null ) {
00043         # The null default value is only here to avoid an E_STRICT
00044         if ( $time === null ) {
00045             throw new MWException( __METHOD__ . ' got null for $time parameter' );
00046         }
00047         return new self( $title, $repo, $time, null );
00048     }
00049 
00056     static function newFromArchiveName( $title, $repo, $archiveName ) {
00057         return new self( $title, $repo, null, $archiveName );
00058     }
00059 
00065     static function newFromRow( $row, $repo ) {
00066         $title = Title::makeTitle( NS_FILE, $row->oi_name );
00067         $file = new self( $title, $repo, null, $row->oi_archive_name );
00068         $file->loadFromRow( $row, 'oi_' );
00069         return $file;
00070     }
00071 
00082     static function newFromKey( $sha1, $repo, $timestamp = false ) {
00083         $dbr = $repo->getSlaveDB();
00084 
00085         $conds = array( 'oi_sha1' => $sha1 );
00086         if ( $timestamp ) {
00087             $conds['oi_timestamp'] = $dbr->timestamp( $timestamp );
00088         }
00089 
00090         $row = $dbr->selectRow( 'oldimage', self::selectFields(), $conds, __METHOD__ );
00091         if ( $row ) {
00092             return self::newFromRow( $row, $repo );
00093         } else {
00094             return false;
00095         }
00096     }
00097 
00102     static function selectFields() {
00103         return array(
00104             'oi_name',
00105             'oi_archive_name',
00106             'oi_size',
00107             'oi_width',
00108             'oi_height',
00109             'oi_metadata',
00110             'oi_bits',
00111             'oi_media_type',
00112             'oi_major_mime',
00113             'oi_minor_mime',
00114             'oi_description',
00115             'oi_user',
00116             'oi_user_text',
00117             'oi_timestamp',
00118             'oi_deleted',
00119             'oi_sha1',
00120         );
00121     }
00122 
00130     function __construct( $title, $repo, $time, $archiveName ) {
00131         parent::__construct( $title, $repo );
00132         $this->requestedTime = $time;
00133         $this->archive_name = $archiveName;
00134         if ( is_null( $time ) && is_null( $archiveName ) ) {
00135             throw new MWException( __METHOD__ . ': must specify at least one of $time or $archiveName' );
00136         }
00137     }
00138 
00142     function getCacheKey() {
00143         return false;
00144     }
00145 
00149     function getArchiveName() {
00150         if ( !isset( $this->archive_name ) ) {
00151             $this->load();
00152         }
00153         return $this->archive_name;
00154     }
00155 
00159     function isOld() {
00160         return true;
00161     }
00162 
00166     function isVisible() {
00167         return $this->exists() && !$this->isDeleted( File::DELETED_FILE );
00168     }
00169 
00170     function loadFromDB() {
00171         wfProfileIn( __METHOD__ );
00172 
00173         $this->dataLoaded = true;
00174         $dbr = $this->repo->getSlaveDB();
00175         $conds = array( 'oi_name' => $this->getName() );
00176         if ( is_null( $this->requestedTime ) ) {
00177             $conds['oi_archive_name'] = $this->archive_name;
00178         } else {
00179             $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime );
00180         }
00181         $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
00182             $conds, __METHOD__, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
00183         if ( $row ) {
00184             $this->loadFromRow( $row, 'oi_' );
00185         } else {
00186             $this->fileExists = false;
00187         }
00188 
00189         wfProfileOut( __METHOD__ );
00190     }
00191 
00195     protected function loadExtraFromDB() {
00196         wfProfileIn( __METHOD__ );
00197 
00198         $this->extraDataLoaded = true;
00199         $dbr = $this->repo->getSlaveDB();
00200         $conds = array( 'oi_name' => $this->getName() );
00201         if ( is_null( $this->requestedTime ) ) {
00202             $conds['oi_archive_name'] = $this->archive_name;
00203         } else {
00204             $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime );
00205         }
00206         // In theory the file could have just been renamed/deleted...oh well
00207         $row = $dbr->selectRow( 'oldimage', $this->getLazyCacheFields( 'oi_' ),
00208             $conds, __METHOD__, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
00209 
00210         if ( !$row ) { // fallback to master
00211             $dbr = $this->repo->getMasterDB();
00212             $row = $dbr->selectRow( 'oldimage', $this->getLazyCacheFields( 'oi_' ),
00213                 $conds, __METHOD__, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
00214         }
00215 
00216         if ( $row ) {
00217             foreach ( $this->unprefixRow( $row, 'oi_' ) as $name => $value ) {
00218                 $this->$name = $value;
00219             }
00220         } else {
00221             wfProfileOut( __METHOD__ );
00222             throw new MWException( "Could not find data for image '{$this->archive_name}'." );
00223         }
00224 
00225         wfProfileOut( __METHOD__ );
00226     }
00227 
00232     function getCacheFields( $prefix = 'img_' ) {
00233         $fields = parent::getCacheFields( $prefix );
00234         $fields[] = $prefix . 'archive_name';
00235         $fields[] = $prefix . 'deleted';
00236         return $fields;
00237     }
00238 
00242     function getRel() {
00243         return 'archive/' . $this->getHashPath() . $this->getArchiveName();
00244     }
00245 
00249     function getUrlRel() {
00250         return 'archive/' . $this->getHashPath() . rawurlencode( $this->getArchiveName() );
00251     }
00252 
00253     function upgradeRow() {
00254         wfProfileIn( __METHOD__ );
00255         $this->loadFromFile();
00256 
00257         # Don't destroy file info of missing files
00258         if ( !$this->fileExists ) {
00259             wfDebug( __METHOD__ . ": file does not exist, aborting\n" );
00260             wfProfileOut( __METHOD__ );
00261             return;
00262         }
00263 
00264         $dbw = $this->repo->getMasterDB();
00265         list( $major, $minor ) = self::splitMime( $this->mime );
00266 
00267         wfDebug( __METHOD__ . ': upgrading ' . $this->archive_name . " to the current schema\n" );
00268         $dbw->update( 'oldimage',
00269             array(
00270                 'oi_size'       => $this->size, // sanity
00271                 'oi_width'      => $this->width,
00272                 'oi_height'     => $this->height,
00273                 'oi_bits'       => $this->bits,
00274                 'oi_media_type' => $this->media_type,
00275                 'oi_major_mime' => $major,
00276                 'oi_minor_mime' => $minor,
00277                 'oi_metadata'   => $this->metadata,
00278                 'oi_sha1'       => $this->sha1,
00279             ), array(
00280                 'oi_name' => $this->getName(),
00281                 'oi_archive_name' => $this->archive_name ),
00282             __METHOD__
00283         );
00284         wfProfileOut( __METHOD__ );
00285     }
00286 
00292     function isDeleted( $field ) {
00293         $this->load();
00294         return ( $this->deleted & $field ) == $field;
00295     }
00296 
00301     function getVisibility() {
00302         $this->load();
00303         return (int)$this->deleted;
00304     }
00305 
00314     function userCan( $field, User $user = null ) {
00315         $this->load();
00316         return Revision::userCanBitfield( $this->deleted, $field, $user );
00317     }
00318 
00332     function uploadOld( $srcPath, $archiveName, $timestamp, $comment, $user, $flags = 0 ) {
00333         $this->lock();
00334 
00335         $dstRel = 'archive/' . $this->getHashPath() . $archiveName;
00336         $status = $this->publishTo( $srcPath, $dstRel,
00337             $flags & File::DELETE_SOURCE ? FileRepo::DELETE_SOURCE : 0
00338         );
00339 
00340         if ( $status->isGood() ) {
00341             if ( !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) ) {
00342                 $status->fatal( 'filenotfound', $srcPath );
00343             }
00344         }
00345 
00346         $this->unlock();
00347 
00348         return $status;
00349     }
00350 
00361     function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
00362         $dbw = $this->repo->getMasterDB();
00363         $dbw->begin( __METHOD__ );
00364 
00365         $dstPath = $this->repo->getZonePath( 'public' ) . '/' . $this->getRel();
00366         $props = $this->repo->getFileProps( $dstPath );
00367         if ( !$props['fileExists'] ) {
00368             return false;
00369         }
00370 
00371         $dbw->insert( 'oldimage',
00372             array(
00373                 'oi_name'         => $this->getName(),
00374                 'oi_archive_name' => $archiveName,
00375                 'oi_size'         => $props['size'],
00376                 'oi_width'        => intval( $props['width'] ),
00377                 'oi_height'       => intval( $props['height'] ),
00378                 'oi_bits'         => $props['bits'],
00379                 'oi_timestamp'    => $dbw->timestamp( $timestamp ),
00380                 'oi_description'  => $comment,
00381                 'oi_user'         => $user->getId(),
00382                 'oi_user_text'    => $user->getName(),
00383                 'oi_metadata'     => $props['metadata'],
00384                 'oi_media_type'   => $props['media_type'],
00385                 'oi_major_mime'   => $props['major_mime'],
00386                 'oi_minor_mime'   => $props['minor_mime'],
00387                 'oi_sha1'         => $props['sha1'],
00388             ), __METHOD__
00389         );
00390 
00391         $dbw->commit( __METHOD__ );
00392 
00393         return true;
00394     }
00395 
00396 }