MediaWiki  REL1_24
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( $flags = 0 ) {
00178         wfProfileIn( __METHOD__ );
00179 
00180         $this->dataLoaded = true;
00181 
00182         $dbr = $this->repo->getSlaveDB();
00183         $conds = array( 'oi_name' => $this->getName() );
00184         if ( is_null( $this->requestedTime ) ) {
00185             $conds['oi_archive_name'] = $this->archive_name;
00186         } else {
00187             $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime );
00188         }
00189         $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
00190             $conds, __METHOD__, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
00191         if ( $row ) {
00192             $this->loadFromRow( $row, 'oi_' );
00193         } else {
00194             $this->fileExists = false;
00195         }
00196 
00197         wfProfileOut( __METHOD__ );
00198     }
00199 
00203     protected function loadExtraFromDB() {
00204         wfProfileIn( __METHOD__ );
00205 
00206         $this->extraDataLoaded = true;
00207         $dbr = $this->repo->getSlaveDB();
00208         $conds = array( 'oi_name' => $this->getName() );
00209         if ( is_null( $this->requestedTime ) ) {
00210             $conds['oi_archive_name'] = $this->archive_name;
00211         } else {
00212             $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime );
00213         }
00214         // In theory the file could have just been renamed/deleted...oh well
00215         $row = $dbr->selectRow( 'oldimage', $this->getLazyCacheFields( 'oi_' ),
00216             $conds, __METHOD__, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
00217 
00218         if ( !$row ) { // fallback to master
00219             $dbr = $this->repo->getMasterDB();
00220             $row = $dbr->selectRow( 'oldimage', $this->getLazyCacheFields( 'oi_' ),
00221                 $conds, __METHOD__, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
00222         }
00223 
00224         if ( $row ) {
00225             foreach ( $this->unprefixRow( $row, 'oi_' ) as $name => $value ) {
00226                 $this->$name = $value;
00227             }
00228         } else {
00229             wfProfileOut( __METHOD__ );
00230             throw new MWException( "Could not find data for image '{$this->archive_name}'." );
00231         }
00232 
00233         wfProfileOut( __METHOD__ );
00234     }
00235 
00240     function getCacheFields( $prefix = 'img_' ) {
00241         $fields = parent::getCacheFields( $prefix );
00242         $fields[] = $prefix . 'archive_name';
00243         $fields[] = $prefix . 'deleted';
00244 
00245         return $fields;
00246     }
00247 
00251     function getRel() {
00252         return 'archive/' . $this->getHashPath() . $this->getArchiveName();
00253     }
00254 
00258     function getUrlRel() {
00259         return 'archive/' . $this->getHashPath() . rawurlencode( $this->getArchiveName() );
00260     }
00261 
00262     function upgradeRow() {
00263         wfProfileIn( __METHOD__ );
00264         $this->loadFromFile();
00265 
00266         # Don't destroy file info of missing files
00267         if ( !$this->fileExists ) {
00268             wfDebug( __METHOD__ . ": file does not exist, aborting\n" );
00269             wfProfileOut( __METHOD__ );
00270 
00271             return;
00272         }
00273 
00274         $dbw = $this->repo->getMasterDB();
00275         list( $major, $minor ) = self::splitMime( $this->mime );
00276 
00277         wfDebug( __METHOD__ . ': upgrading ' . $this->archive_name . " to the current schema\n" );
00278         $dbw->update( 'oldimage',
00279             array(
00280                 'oi_size' => $this->size, // sanity
00281                 'oi_width' => $this->width,
00282                 'oi_height' => $this->height,
00283                 'oi_bits' => $this->bits,
00284                 'oi_media_type' => $this->media_type,
00285                 'oi_major_mime' => $major,
00286                 'oi_minor_mime' => $minor,
00287                 'oi_metadata' => $this->metadata,
00288                 'oi_sha1' => $this->sha1,
00289             ), array(
00290                 'oi_name' => $this->getName(),
00291                 'oi_archive_name' => $this->archive_name ),
00292             __METHOD__
00293         );
00294         wfProfileOut( __METHOD__ );
00295     }
00296 
00302     function isDeleted( $field ) {
00303         $this->load();
00304 
00305         return ( $this->deleted & $field ) == $field;
00306     }
00307 
00312     function getVisibility() {
00313         $this->load();
00314 
00315         return (int)$this->deleted;
00316     }
00317 
00326     function userCan( $field, User $user = null ) {
00327         $this->load();
00328 
00329         return Revision::userCanBitfield( $this->deleted, $field, $user );
00330     }
00331 
00344     function uploadOld( $srcPath, $archiveName, $timestamp, $comment, $user, $flags = 0 ) {
00345         $this->lock();
00346 
00347         $dstRel = 'archive/' . $this->getHashPath() . $archiveName;
00348         $status = $this->publishTo( $srcPath, $dstRel,
00349             $flags & File::DELETE_SOURCE ? FileRepo::DELETE_SOURCE : 0
00350         );
00351 
00352         if ( $status->isGood() ) {
00353             if ( !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) ) {
00354                 $status->fatal( 'filenotfound', $srcPath );
00355             }
00356         }
00357 
00358         $this->unlock();
00359 
00360         return $status;
00361     }
00362 
00373     function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
00374         $dbw = $this->repo->getMasterDB();
00375         $dbw->begin( __METHOD__ );
00376 
00377         $dstPath = $this->repo->getZonePath( 'public' ) . '/' . $this->getRel();
00378         $props = $this->repo->getFileProps( $dstPath );
00379         if ( !$props['fileExists'] ) {
00380             return false;
00381         }
00382 
00383         $dbw->insert( 'oldimage',
00384             array(
00385                 'oi_name' => $this->getName(),
00386                 'oi_archive_name' => $archiveName,
00387                 'oi_size' => $props['size'],
00388                 'oi_width' => intval( $props['width'] ),
00389                 'oi_height' => intval( $props['height'] ),
00390                 'oi_bits' => $props['bits'],
00391                 'oi_timestamp' => $dbw->timestamp( $timestamp ),
00392                 'oi_description' => $comment,
00393                 'oi_user' => $user->getId(),
00394                 'oi_user_text' => $user->getName(),
00395                 'oi_metadata' => $props['metadata'],
00396                 'oi_media_type' => $props['media_type'],
00397                 'oi_major_mime' => $props['major_mime'],
00398                 'oi_minor_mime' => $props['minor_mime'],
00399                 'oi_sha1' => $props['sha1'],
00400             ), __METHOD__
00401         );
00402 
00403         $dbw->commit( __METHOD__ );
00404 
00405         return true;
00406     }
00407 
00414     public function exists() {
00415         $archiveName = $this->getArchiveName();
00416         if ( $archiveName === '' || !is_string( $archiveName ) ) {
00417             return false;
00418         }
00419         return parent::exists();
00420     }
00421 }