MediaWiki  REL1_21
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                         throw new MWException( "Could not find data for image '{$this->archive_name}'." );
00222                 }
00223 
00224                 wfProfileOut( __METHOD__ );
00225         }
00226 
00231         function getCacheFields( $prefix = 'img_' ) {
00232                 $fields = parent::getCacheFields( $prefix );
00233                 $fields[] = $prefix . 'archive_name';
00234                 $fields[] = $prefix . 'deleted';
00235                 return $fields;
00236         }
00237 
00241         function getRel() {
00242                 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
00243         }
00244 
00248         function getUrlRel() {
00249                 return 'archive/' . $this->getHashPath() . rawurlencode( $this->getArchiveName() );
00250         }
00251 
00252         function upgradeRow() {
00253                 wfProfileIn( __METHOD__ );
00254                 $this->loadFromFile();
00255 
00256                 # Don't destroy file info of missing files
00257                 if ( !$this->fileExists ) {
00258                         wfDebug( __METHOD__ . ": file does not exist, aborting\n" );
00259                         wfProfileOut( __METHOD__ );
00260                         return;
00261                 }
00262 
00263                 $dbw = $this->repo->getMasterDB();
00264                 list( $major, $minor ) = self::splitMime( $this->mime );
00265 
00266                 wfDebug( __METHOD__ . ': upgrading ' . $this->archive_name . " to the current schema\n" );
00267                 $dbw->update( 'oldimage',
00268                         array(
00269                                 'oi_size'       => $this->size, // sanity
00270                                 'oi_width'      => $this->width,
00271                                 'oi_height'     => $this->height,
00272                                 'oi_bits'       => $this->bits,
00273                                 'oi_media_type' => $this->media_type,
00274                                 'oi_major_mime' => $major,
00275                                 'oi_minor_mime' => $minor,
00276                                 'oi_metadata'   => $this->metadata,
00277                                 'oi_sha1'       => $this->sha1,
00278                         ), array(
00279                                 'oi_name' => $this->getName(),
00280                                 'oi_archive_name' => $this->archive_name ),
00281                         __METHOD__
00282                 );
00283                 wfProfileOut( __METHOD__ );
00284         }
00285 
00291         function isDeleted( $field ) {
00292                 $this->load();
00293                 return ($this->deleted & $field) == $field;
00294         }
00295 
00300         function getVisibility() {
00301                 $this->load();
00302                 return (int)$this->deleted;
00303         }
00304 
00313         function userCan( $field, User $user = null ) {
00314                 $this->load();
00315                 return Revision::userCanBitfield( $this->deleted, $field, $user );
00316         }
00317 
00331         function uploadOld( $srcPath, $archiveName, $timestamp, $comment, $user, $flags = 0 ) {
00332                 $this->lock();
00333 
00334                 $dstRel = 'archive/' . $this->getHashPath() . $archiveName;
00335                 $status = $this->publishTo( $srcPath, $dstRel,
00336                         $flags & File::DELETE_SOURCE ? FileRepo::DELETE_SOURCE : 0
00337                 );
00338 
00339                 if ( $status->isGood() ) {
00340                         if ( !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) ) {
00341                                 $status->fatal( 'filenotfound', $srcPath );
00342                         }
00343                 }
00344 
00345                 $this->unlock();
00346 
00347                 return $status;
00348         }
00349 
00360         function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
00361                 $dbw = $this->repo->getMasterDB();
00362                 $dbw->begin( __METHOD__ );
00363 
00364                 $dstPath = $this->repo->getZonePath( 'public' ) . '/' . $this->getRel();
00365                 $props = $this->repo->getFileProps( $dstPath );
00366                 if ( !$props['fileExists'] ) {
00367                         return false;
00368                 }
00369 
00370                 $dbw->insert( 'oldimage',
00371                         array(
00372                                 'oi_name'         => $this->getName(),
00373                                 'oi_archive_name' => $archiveName,
00374                                 'oi_size'         => $props['size'],
00375                                 'oi_width'        => intval( $props['width'] ),
00376                                 'oi_height'       => intval( $props['height'] ),
00377                                 'oi_bits'         => $props['bits'],
00378                                 'oi_timestamp'    => $dbw->timestamp( $timestamp ),
00379                                 'oi_description'  => $comment,
00380                                 'oi_user'         => $user->getId(),
00381                                 'oi_user_text'    => $user->getName(),
00382                                 'oi_metadata'     => $props['metadata'],
00383                                 'oi_media_type'   => $props['media_type'],
00384                                 'oi_major_mime'   => $props['major_mime'],
00385                                 'oi_minor_mime'   => $props['minor_mime'],
00386                                 'oi_sha1'         => $props['sha1'],
00387                         ), __METHOD__
00388                 );
00389 
00390                 $dbw->commit( __METHOD__ );
00391 
00392                 return true;
00393         }
00394 
00395 }