MediaWiki  REL1_19
OldLocalFile.php
Go to the documentation of this file.
00001 <?php
00014 class OldLocalFile extends LocalFile {
00015         var $requestedTime, $archive_name;
00016 
00017         const CACHE_VERSION = 1;
00018         const MAX_CACHE_ROWS = 20;
00019 
00020         static function newFromTitle( $title, $repo, $time = null ) {
00021                 # The null default value is only here to avoid an E_STRICT
00022                 if ( $time === null ) {
00023                         throw new MWException( __METHOD__.' got null for $time parameter' );
00024                 }
00025                 return new self( $title, $repo, $time, null );
00026         }
00027 
00028         static function newFromArchiveName( $title, $repo, $archiveName ) {
00029                 return new self( $title, $repo, null, $archiveName );
00030         }
00031 
00032         static function newFromRow( $row, $repo ) {
00033                 $title = Title::makeTitle( NS_FILE, $row->oi_name );
00034                 $file = new self( $title, $repo, null, $row->oi_archive_name );
00035                 $file->loadFromRow( $row, 'oi_' );
00036                 return $file;
00037         }
00038 
00049         static function newFromKey( $sha1, $repo, $timestamp = false ) {
00050                 $dbr = $repo->getSlaveDB();
00051 
00052                 $conds = array( 'oi_sha1' => $sha1 );
00053                 if ( $timestamp ) {
00054                         $conds['oi_timestamp'] = $dbr->timestamp( $timestamp );
00055                 }
00056 
00057                 $row = $dbr->selectRow( 'oldimage', self::selectFields(), $conds, __METHOD__ );
00058                 if ( $row ) {
00059                         return self::newFromRow( $row, $repo );
00060                 } else {
00061                         return false;
00062                 }
00063         }
00064         
00068         static function selectFields() {
00069                 return array(
00070                         'oi_name',
00071                         'oi_archive_name',
00072                         'oi_size',
00073                         'oi_width',
00074                         'oi_height',
00075                         'oi_metadata',
00076                         'oi_bits',
00077                         'oi_media_type',
00078                         'oi_major_mime',
00079                         'oi_minor_mime',
00080                         'oi_description',
00081                         'oi_user',
00082                         'oi_user_text',
00083                         'oi_timestamp',
00084                         'oi_deleted',
00085                         'oi_sha1',
00086                 );
00087         }
00088 
00095         function __construct( $title, $repo, $time, $archiveName ) {
00096                 parent::__construct( $title, $repo );
00097                 $this->requestedTime = $time;
00098                 $this->archive_name = $archiveName;
00099                 if ( is_null( $time ) && is_null( $archiveName ) ) {
00100                         throw new MWException( __METHOD__.': must specify at least one of $time or $archiveName' );
00101                 }
00102         }
00103 
00104         function getCacheKey() {
00105                 return false;
00106         }
00107 
00108         function getArchiveName() {
00109                 if ( !isset( $this->archive_name ) ) {
00110                         $this->load();
00111                 }
00112                 return $this->archive_name;
00113         }
00114 
00115         function isOld() {
00116                 return true;
00117         }
00118 
00119         function isVisible() {
00120                 return $this->exists() && !$this->isDeleted(File::DELETED_FILE);
00121         }
00122 
00123         function loadFromDB() {
00124                 wfProfileIn( __METHOD__ );
00125                 $this->dataLoaded = true;
00126                 $dbr = $this->repo->getSlaveDB();
00127                 $conds = array( 'oi_name' => $this->getName() );
00128                 if ( is_null( $this->requestedTime ) ) {
00129                         $conds['oi_archive_name'] = $this->archive_name;
00130                 } else {
00131                         $conds[] = 'oi_timestamp = ' . $dbr->addQuotes( $dbr->timestamp( $this->requestedTime ) );
00132                 }
00133                 $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
00134                         $conds, __METHOD__, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
00135                 if ( $row ) {
00136                         $this->loadFromRow( $row, 'oi_' );
00137                 } else {
00138                         $this->fileExists = false;
00139                 }
00140                 wfProfileOut( __METHOD__ );
00141         }
00142 
00143         function getCacheFields( $prefix = 'img_' ) {
00144                 $fields = parent::getCacheFields( $prefix );
00145                 $fields[] = $prefix . 'archive_name';
00146                 $fields[] = $prefix . 'deleted';
00147                 return $fields;
00148         }
00149 
00150         function getRel() {
00151                 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
00152         }
00153 
00154         function getUrlRel() {
00155                 return 'archive/' . $this->getHashPath() . rawurlencode( $this->getArchiveName() );
00156         }
00157 
00158         function upgradeRow() {
00159                 wfProfileIn( __METHOD__ );
00160                 $this->loadFromFile();
00161 
00162                 # Don't destroy file info of missing files
00163                 if ( !$this->fileExists ) {
00164                         wfDebug( __METHOD__.": file does not exist, aborting\n" );
00165                         wfProfileOut( __METHOD__ );
00166                         return;
00167                 }
00168 
00169                 $dbw = $this->repo->getMasterDB();
00170                 list( $major, $minor ) = self::splitMime( $this->mime );
00171 
00172                 wfDebug(__METHOD__.': upgrading '.$this->archive_name." to the current schema\n");
00173                 $dbw->update( 'oldimage',
00174                         array(
00175                                 'oi_width' => $this->width,
00176                                 'oi_height' => $this->height,
00177                                 'oi_bits' => $this->bits,
00178                                 'oi_media_type' => $this->media_type,
00179                                 'oi_major_mime' => $major,
00180                                 'oi_minor_mime' => $minor,
00181                                 'oi_metadata' => $this->metadata,
00182                                 'oi_sha1' => $this->sha1,
00183                         ), array(
00184                                 'oi_name' => $this->getName(),
00185                                 'oi_archive_name' => $this->archive_name ),
00186                         __METHOD__
00187                 );
00188                 wfProfileOut( __METHOD__ );
00189         }
00190 
00196         function isDeleted( $field ) {
00197                 $this->load();
00198                 return ($this->deleted & $field) == $field;
00199         }
00200 
00205         function getVisibility() {
00206                 $this->load();
00207                 return (int)$this->deleted;
00208         }
00209 
00218         function userCan( $field, User $user = null ) {
00219                 $this->load();
00220                 return Revision::userCanBitfield( $this->deleted, $field, $user );
00221         }
00222         
00232         function uploadOld( $srcPath, $archiveName, $timestamp, $comment, $user, $flags = 0 ) {
00233                 $this->lock();
00234                 
00235                 $dstRel = 'archive/' . $this->getHashPath() . $archiveName;
00236                 $status = $this->publishTo( $srcPath, $dstRel,
00237                         $flags & File::DELETE_SOURCE ? FileRepo::DELETE_SOURCE : 0
00238                 );
00239                 
00240                 if ( $status->isGood() ) {
00241                         if ( !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) ) {
00242                                 $status->fatal( 'filenotfound', $srcPath );
00243                         }
00244                 }
00245                 
00246                 $this->unlock();
00247                 
00248                 return $status;
00249         }
00250         
00260         function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
00261                 $dbw = $this->repo->getMasterDB();
00262                 $dbw->begin();
00263 
00264                 $dstPath = $this->repo->getZonePath( 'public' ) . '/' . $this->getRel();
00265                 $props = $this->repo->getFileProps( $dstPath );
00266                 if ( !$props['fileExists'] ) {
00267                         return false;
00268                 }
00269 
00270                 $dbw->insert( 'oldimage',
00271                         array(
00272                                 'oi_name'         => $this->getName(),
00273                                 'oi_archive_name' => $archiveName,
00274                                 'oi_size'         => $props['size'],
00275                                 'oi_width'        => intval( $props['width'] ),
00276                                 'oi_height'       => intval( $props['height'] ),
00277                                 'oi_bits'         => $props['bits'],
00278                                 'oi_timestamp'    => $dbw->timestamp( $timestamp ),
00279                                 'oi_description'  => $comment,
00280                                 'oi_user'         => $user->getId(),
00281                                 'oi_user_text'    => $user->getName(),
00282                                 'oi_metadata'     => $props['metadata'],
00283                                 'oi_media_type'   => $props['media_type'],
00284                                 'oi_major_mime'   => $props['major_mime'],
00285                                 'oi_minor_mime'   => $props['minor_mime'],
00286                                 'oi_sha1'         => $props['sha1'],
00287                         ), __METHOD__
00288                 );
00289 
00290                 $dbw->commit();
00291 
00292                 return true;
00293         }
00294         
00295 }