MediaWiki  REL1_20
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                 $this->dataLoaded = true;
00173                 $dbr = $this->repo->getSlaveDB();
00174                 $conds = array( 'oi_name' => $this->getName() );
00175                 if ( is_null( $this->requestedTime ) ) {
00176                         $conds['oi_archive_name'] = $this->archive_name;
00177                 } else {
00178                         $conds[] = 'oi_timestamp = ' . $dbr->addQuotes( $dbr->timestamp( $this->requestedTime ) );
00179                 }
00180                 $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
00181                         $conds, __METHOD__, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
00182                 if ( $row ) {
00183                         $this->loadFromRow( $row, 'oi_' );
00184                 } else {
00185                         $this->fileExists = false;
00186                 }
00187                 wfProfileOut( __METHOD__ );
00188         }
00189 
00194         function getCacheFields( $prefix = 'img_' ) {
00195                 $fields = parent::getCacheFields( $prefix );
00196                 $fields[] = $prefix . 'archive_name';
00197                 $fields[] = $prefix . 'deleted';
00198                 return $fields;
00199         }
00200 
00204         function getRel() {
00205                 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
00206         }
00207 
00211         function getUrlRel() {
00212                 return 'archive/' . $this->getHashPath() . rawurlencode( $this->getArchiveName() );
00213         }
00214 
00215         function upgradeRow() {
00216                 wfProfileIn( __METHOD__ );
00217                 $this->loadFromFile();
00218 
00219                 # Don't destroy file info of missing files
00220                 if ( !$this->fileExists ) {
00221                         wfDebug( __METHOD__.": file does not exist, aborting\n" );
00222                         wfProfileOut( __METHOD__ );
00223                         return;
00224                 }
00225 
00226                 $dbw = $this->repo->getMasterDB();
00227                 list( $major, $minor ) = self::splitMime( $this->mime );
00228 
00229                 wfDebug(__METHOD__.': upgrading '.$this->archive_name." to the current schema\n");
00230                 $dbw->update( 'oldimage',
00231                         array(
00232                                 'oi_size'       => $this->size, // sanity
00233                                 'oi_width'      => $this->width,
00234                                 'oi_height'     => $this->height,
00235                                 'oi_bits'       => $this->bits,
00236                                 'oi_media_type' => $this->media_type,
00237                                 'oi_major_mime' => $major,
00238                                 'oi_minor_mime' => $minor,
00239                                 'oi_metadata'   => $this->metadata,
00240                                 'oi_sha1'       => $this->sha1,
00241                         ), array(
00242                                 'oi_name' => $this->getName(),
00243                                 'oi_archive_name' => $this->archive_name ),
00244                         __METHOD__
00245                 );
00246                 wfProfileOut( __METHOD__ );
00247         }
00248 
00254         function isDeleted( $field ) {
00255                 $this->load();
00256                 return ($this->deleted & $field) == $field;
00257         }
00258 
00263         function getVisibility() {
00264                 $this->load();
00265                 return (int)$this->deleted;
00266         }
00267 
00276         function userCan( $field, User $user = null ) {
00277                 $this->load();
00278                 return Revision::userCanBitfield( $this->deleted, $field, $user );
00279         }
00280 
00294         function uploadOld( $srcPath, $archiveName, $timestamp, $comment, $user, $flags = 0 ) {
00295                 $this->lock();
00296 
00297                 $dstRel = 'archive/' . $this->getHashPath() . $archiveName;
00298                 $status = $this->publishTo( $srcPath, $dstRel,
00299                         $flags & File::DELETE_SOURCE ? FileRepo::DELETE_SOURCE : 0
00300                 );
00301 
00302                 if ( $status->isGood() ) {
00303                         if ( !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) ) {
00304                                 $status->fatal( 'filenotfound', $srcPath );
00305                         }
00306                 }
00307 
00308                 $this->unlock();
00309 
00310                 return $status;
00311         }
00312 
00323         function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
00324                 $dbw = $this->repo->getMasterDB();
00325                 $dbw->begin( __METHOD__ );
00326 
00327                 $dstPath = $this->repo->getZonePath( 'public' ) . '/' . $this->getRel();
00328                 $props = $this->repo->getFileProps( $dstPath );
00329                 if ( !$props['fileExists'] ) {
00330                         return false;
00331                 }
00332 
00333                 $dbw->insert( 'oldimage',
00334                         array(
00335                                 'oi_name'         => $this->getName(),
00336                                 'oi_archive_name' => $archiveName,
00337                                 'oi_size'         => $props['size'],
00338                                 'oi_width'        => intval( $props['width'] ),
00339                                 'oi_height'       => intval( $props['height'] ),
00340                                 'oi_bits'         => $props['bits'],
00341                                 'oi_timestamp'    => $dbw->timestamp( $timestamp ),
00342                                 'oi_description'  => $comment,
00343                                 'oi_user'         => $user->getId(),
00344                                 'oi_user_text'    => $user->getName(),
00345                                 'oi_metadata'     => $props['metadata'],
00346                                 'oi_media_type'   => $props['media_type'],
00347                                 'oi_major_mime'   => $props['major_mime'],
00348                                 'oi_minor_mime'   => $props['minor_mime'],
00349                                 'oi_sha1'         => $props['sha1'],
00350                         ), __METHOD__
00351                 );
00352 
00353                 $dbw->commit( __METHOD__ );
00354 
00355                 return true;
00356         }
00357 
00358 }