00001 <?php
00002
00008 class OldLocalFile extends LocalFile {
00009 var $requestedTime, $archive_name;
00010
00011 const CACHE_VERSION = 1;
00012 const MAX_CACHE_ROWS = 20;
00013
00014 static function newFromTitle( $title, $repo, $time = null ) {
00015 # The null default value is only here to avoid an E_STRICT
00016 if( $time === null )
00017 throw new MWException( __METHOD__.' got null for $time parameter' );
00018 return new self( $title, $repo, $time, null );
00019 }
00020
00021 static function newFromArchiveName( $title, $repo, $archiveName ) {
00022 return new self( $title, $repo, null, $archiveName );
00023 }
00024
00025 static function newFromRow( $row, $repo ) {
00026 $title = Title::makeTitle( NS_FILE, $row->oi_name );
00027 $file = new self( $title, $repo, null, $row->oi_archive_name );
00028 $file->loadFromRow( $row, 'oi_' );
00029 return $file;
00030 }
00031
00032 static function newFromKey( $sha1, $repo, $timestamp = false ) {
00033 # Polymorphic function name to distinguish foreign and local fetches
00034 $fname = get_class( $this ) . '::' . __FUNCTION__;
00035
00036 $conds = array( 'oi_sha1' => $sha1 );
00037 if( $timestamp ) {
00038 $conds['oi_timestamp'] = $timestamp;
00039 }
00040 $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ), $conds, $fname );
00041 if( $row ) {
00042 return self::newFromRow( $row, $repo );
00043 } else {
00044 return false;
00045 }
00046 }
00047
00051 static function selectFields() {
00052 return array(
00053 'oi_name',
00054 'oi_archive_name',
00055 'oi_size',
00056 'oi_width',
00057 'oi_height',
00058 'oi_metadata',
00059 'oi_bits',
00060 'oi_media_type',
00061 'oi_major_mime',
00062 'oi_minor_mime',
00063 'oi_description',
00064 'oi_user',
00065 'oi_user_text',
00066 'oi_timestamp',
00067 'oi_deleted',
00068 'oi_sha1',
00069 );
00070 }
00071
00078 function __construct( $title, $repo, $time, $archiveName ) {
00079 parent::__construct( $title, $repo );
00080 $this->requestedTime = $time;
00081 $this->archive_name = $archiveName;
00082 if ( is_null( $time ) && is_null( $archiveName ) ) {
00083 throw new MWException( __METHOD__.': must specify at least one of $time or $archiveName' );
00084 }
00085 }
00086
00087 function getCacheKey() {
00088 return false;
00089 }
00090
00091 function getArchiveName() {
00092 if ( !isset( $this->archive_name ) ) {
00093 $this->load();
00094 }
00095 return $this->archive_name;
00096 }
00097
00098 function isOld() {
00099 return true;
00100 }
00101
00102 function isVisible() {
00103 return $this->exists() && !$this->isDeleted(File::DELETED_FILE);
00104 }
00105
00106 function loadFromDB() {
00107 wfProfileIn( __METHOD__ );
00108 $this->dataLoaded = true;
00109 $dbr = $this->repo->getSlaveDB();
00110 $conds = array( 'oi_name' => $this->getName() );
00111 if ( is_null( $this->requestedTime ) ) {
00112 $conds['oi_archive_name'] = $this->archive_name;
00113 } else {
00114 $conds[] = 'oi_timestamp = ' . $dbr->addQuotes( $dbr->timestamp( $this->requestedTime ) );
00115 }
00116 $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
00117 $conds, __METHOD__, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
00118 if ( $row ) {
00119 $this->loadFromRow( $row, 'oi_' );
00120 } else {
00121 $this->fileExists = false;
00122 }
00123 wfProfileOut( __METHOD__ );
00124 }
00125
00126 function getCacheFields( $prefix = 'img_' ) {
00127 $fields = parent::getCacheFields( $prefix );
00128 $fields[] = $prefix . 'archive_name';
00129 $fields[] = $prefix . 'deleted';
00130 return $fields;
00131 }
00132
00133 function getRel() {
00134 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
00135 }
00136
00137 function getUrlRel() {
00138 return 'archive/' . $this->getHashPath() . urlencode( $this->getArchiveName() );
00139 }
00140
00141 function upgradeRow() {
00142 wfProfileIn( __METHOD__ );
00143 $this->loadFromFile();
00144
00145 # Don't destroy file info of missing files
00146 if ( !$this->fileExists ) {
00147 wfDebug( __METHOD__.": file does not exist, aborting\n" );
00148 wfProfileOut( __METHOD__ );
00149 return;
00150 }
00151
00152 $dbw = $this->repo->getMasterDB();
00153 list( $major, $minor ) = self::splitMime( $this->mime );
00154
00155 wfDebug(__METHOD__.': upgrading '.$this->archive_name." to the current schema\n");
00156 $dbw->update( 'oldimage',
00157 array(
00158 'oi_width' => $this->width,
00159 'oi_height' => $this->height,
00160 'oi_bits' => $this->bits,
00161 'oi_media_type' => $this->media_type,
00162 'oi_major_mime' => $major,
00163 'oi_minor_mime' => $minor,
00164 'oi_metadata' => $this->metadata,
00165 'oi_sha1' => $this->sha1,
00166 ), array(
00167 'oi_name' => $this->getName(),
00168 'oi_archive_name' => $this->archive_name ),
00169 __METHOD__
00170 );
00171 wfProfileOut( __METHOD__ );
00172 }
00173
00179 function isDeleted( $field ) {
00180 return ($this->deleted & $field) == $field;
00181 }
00182
00189 function userCan( $field ) {
00190 if( isset($this->deleted) && ($this->deleted & $field) == $field ) {
00191 global $wgUser;
00192 $permission = ( $this->deleted & File::DELETED_RESTRICTED ) == File::DELETED_RESTRICTED
00193 ? 'suppressrevision'
00194 : 'deleterevision';
00195 wfDebug( "Checking for $permission due to $field match on $this->mDeleted\n" );
00196 return $wgUser->isAllowed( $permission );
00197 } else {
00198 return true;
00199 }
00200 }
00201 }