MediaWiki  REL1_23
OldLocalFile.php
Go to the documentation of this file.
00001 <?php
00029 class OldLocalFile extends LocalFile {
00031     protected $requestedTime;
00032 
00034     protected $archive_name;
00035 
00036     const CACHE_VERSION = 1;
00037     const MAX_CACHE_ROWS = 20;
00038 
00046     static function newFromTitle( $title, $repo, $time = null ) {
00047         # The null default value is only here to avoid an E_STRICT
00048         if ( $time === null ) {
00049             throw new MWException( __METHOD__ . ' got null for $time parameter' );
00050         }
00051 
00052         return new self( $title, $repo, $time, null );
00053     }
00054 
00061     static function newFromArchiveName( $title, $repo, $archiveName ) {
00062         return new self( $title, $repo, null, $archiveName );
00063     }
00064 
00070     static function newFromRow( $row, $repo ) {
00071         $title = Title::makeTitle( NS_FILE, $row->oi_name );
00072         $file = new self( $title, $repo, null, $row->oi_archive_name );
00073         $file->loadFromRow( $row, 'oi_' );
00074 
00075         return $file;
00076     }
00077 
00088     static function newFromKey( $sha1, $repo, $timestamp = false ) {
00089         $dbr = $repo->getSlaveDB();
00090 
00091         $conds = array( 'oi_sha1' => $sha1 );
00092         if ( $timestamp ) {
00093             $conds['oi_timestamp'] = $dbr->timestamp( $timestamp );
00094         }
00095 
00096         $row = $dbr->selectRow( 'oldimage', self::selectFields(), $conds, __METHOD__ );
00097         if ( $row ) {
00098             return self::newFromRow( $row, $repo );
00099         } else {
00100             return false;
00101         }
00102     }
00103 
00108     static function selectFields() {
00109         return array(
00110             'oi_name',
00111             'oi_archive_name',
00112             'oi_size',
00113             'oi_width',
00114             'oi_height',
00115             'oi_metadata',
00116             'oi_bits',
00117             'oi_media_type',
00118             'oi_major_mime',
00119             'oi_minor_mime',
00120             'oi_description',
00121             'oi_user',
00122             'oi_user_text',
00123             'oi_timestamp',
00124             'oi_deleted',
00125             'oi_sha1',
00126         );
00127     }
00128 
00136     function __construct( $title, $repo, $time, $archiveName ) {
00137         parent::__construct( $title, $repo );
00138         $this->requestedTime = $time;
00139         $this->archive_name = $archiveName;
00140         if ( is_null( $time ) && is_null( $archiveName ) ) {
00141             throw new MWException( __METHOD__ . ': must specify at least one of $time or $archiveName' );
00142         }
00143     }
00144 
00148     function getCacheKey() {
00149         return false;
00150     }
00151 
00155     function getArchiveName() {
00156         if ( !isset( $this->archive_name ) ) {
00157             $this->load();
00158         }
00159 
00160         return $this->archive_name;
00161     }
00162 
00166     function isOld() {
00167         return true;
00168     }
00169 
00173     function isVisible() {
00174         return $this->exists() && !$this->isDeleted( File::DELETED_FILE );
00175     }
00176 
00177     function loadFromDB() {
00178         wfProfileIn( __METHOD__ );
00179 
00180         $this->dataLoaded = true;
00181         $dbr = $this->repo->getSlaveDB();
00182         $conds = array( 'oi_name' => $this->getName() );
00183         if ( is_null( $this->requestedTime ) ) {
00184             $conds['oi_archive_name'] = $this->archive_name;
00185         } else {
00186             $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime );
00187         }
00188         $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
00189             $conds, __METHOD__, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
00190         if ( $row ) {
00191             $this->loadFromRow( $row, 'oi_' );
00192         } else {
00193             $this->fileExists = false;
00194         }
00195 
00196         wfProfileOut( __METHOD__ );
00197     }
00198 
00202     protected function loadExtraFromDB() {
00203         wfProfileIn( __METHOD__ );
00204 
00205         $this->extraDataLoaded = true;
00206         $dbr = $this->repo->getSlaveDB();
00207         $conds = array( 'oi_name' => $this->getName() );
00208         if ( is_null( $this->requestedTime ) ) {
00209             $conds['oi_archive_name'] = $this->archive_name;
00210         } else {
00211             $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime );
00212         }
00213         // In theory the file could have just been renamed/deleted...oh well
00214         $row = $dbr->selectRow( 'oldimage', $this->getLazyCacheFields( 'oi_' ),
00215             $conds, __METHOD__, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
00216 
00217         if ( !$row ) { // fallback to master
00218             $dbr = $this->repo->getMasterDB();
00219             $row = $dbr->selectRow( 'oldimage', $this->getLazyCacheFields( 'oi_' ),
00220                 $conds, __METHOD__, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
00221         }
00222 
00223         if ( $row ) {
00224             foreach ( $this->unprefixRow( $row, 'oi_' ) as $name => $value ) {
00225                 $this->$name = $value;
00226             }
00227         } else {
00228             wfProfileOut( __METHOD__ );
00229             throw new MWException( "Could not find data for image '{$this->archive_name}'." );
00230         }
00231 
00232         wfProfileOut( __METHOD__ );
00233     }
00234 
00239     function getCacheFields( $prefix = 'img_' ) {
00240         $fields = parent::getCacheFields( $prefix );
00241         $fields[] = $prefix . 'archive_name';
00242         $fields[] = $prefix . 'deleted';
00243 
00244         return $fields;
00245     }
00246 
00250     function getRel() {
00251         return 'archive/' . $this->getHashPath() . $this->getArchiveName();
00252     }
00253 
00257     function getUrlRel() {
00258         return 'archive/' . $this->getHashPath() . rawurlencode( $this->getArchiveName() );
00259     }
00260 
00261     function upgradeRow() {
00262         wfProfileIn( __METHOD__ );
00263         $this->loadFromFile();
00264 
00265         # Don't destroy file info of missing files
00266         if ( !$this->fileExists ) {
00267             wfDebug( __METHOD__ . ": file does not exist, aborting\n" );
00268             wfProfileOut( __METHOD__ );
00269 
00270             return;
00271         }
00272 
00273         $dbw = $this->repo->getMasterDB();
00274         list( $major, $minor ) = self::splitMime( $this->mime );
00275 
00276         wfDebug( __METHOD__ . ': upgrading ' . $this->archive_name . " to the current schema\n" );
00277         $dbw->update( 'oldimage',
00278             array(
00279                 'oi_size' => $this->size, // sanity
00280                 'oi_width' => $this->width,
00281                 'oi_height' => $this->height,
00282                 'oi_bits' => $this->bits,
00283                 'oi_media_type' => $this->media_type,
00284                 'oi_major_mime' => $major,
00285                 'oi_minor_mime' => $minor,
00286                 'oi_metadata' => $this->metadata,
00287                 'oi_sha1' => $this->sha1,
00288             ), array(
00289                 'oi_name' => $this->getName(),
00290                 'oi_archive_name' => $this->archive_name ),
00291             __METHOD__
00292         );
00293         wfProfileOut( __METHOD__ );
00294     }
00295 
00301     function isDeleted( $field ) {
00302         $this->load();
00303 
00304         return ( $this->deleted & $field ) == $field;
00305     }
00306 
00311     function getVisibility() {
00312         $this->load();
00313 
00314         return (int)$this->deleted;
00315     }
00316 
00325     function userCan( $field, User $user = null ) {
00326         $this->load();
00327 
00328         return Revision::userCanBitfield( $this->deleted, $field, $user );
00329     }
00330 
00343     function uploadOld( $srcPath, $archiveName, $timestamp, $comment, $user, $flags = 0 ) {
00344         $this->lock();
00345 
00346         $dstRel = 'archive/' . $this->getHashPath() . $archiveName;
00347         $status = $this->publishTo( $srcPath, $dstRel,
00348             $flags & File::DELETE_SOURCE ? FileRepo::DELETE_SOURCE : 0
00349         );
00350 
00351         if ( $status->isGood() ) {
00352             if ( !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) ) {
00353                 $status->fatal( 'filenotfound', $srcPath );
00354             }
00355         }
00356 
00357         $this->unlock();
00358 
00359         return $status;
00360     }
00361 
00372     function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
00373         $dbw = $this->repo->getMasterDB();
00374         $dbw->begin( __METHOD__ );
00375 
00376         $dstPath = $this->repo->getZonePath( 'public' ) . '/' . $this->getRel();
00377         $props = $this->repo->getFileProps( $dstPath );
00378         if ( !$props['fileExists'] ) {
00379             return false;
00380         }
00381 
00382         $dbw->insert( 'oldimage',
00383             array(
00384                 'oi_name' => $this->getName(),
00385                 'oi_archive_name' => $archiveName,
00386                 'oi_size' => $props['size'],
00387                 'oi_width' => intval( $props['width'] ),
00388                 'oi_height' => intval( $props['height'] ),
00389                 'oi_bits' => $props['bits'],
00390                 'oi_timestamp' => $dbw->timestamp( $timestamp ),
00391                 'oi_description' => $comment,
00392                 'oi_user' => $user->getId(),
00393                 'oi_user_text' => $user->getName(),
00394                 'oi_metadata' => $props['metadata'],
00395                 'oi_media_type' => $props['media_type'],
00396                 'oi_major_mime' => $props['major_mime'],
00397                 'oi_minor_mime' => $props['minor_mime'],
00398                 'oi_sha1' => $props['sha1'],
00399             ), __METHOD__
00400         );
00401 
00402         $dbw->commit( __METHOD__ );
00403 
00404         return true;
00405     }
00406 }