4 * Class to represent a file in the oldimage table
8 class OldLocalFile
extends LocalFile
{
9 var $requestedTime, $archive_name;
11 const CACHE_VERSION
= 1;
12 const MAX_CACHE_ROWS
= 20;
14 static function newFromTitle( $title, $repo, $time = null ) {
15 # The null default value is only here to avoid an E_STRICT
17 throw new MWException( __METHOD__
.' got null for $time parameter' );
18 return new self( $title, $repo, $time, null );
21 static function newFromArchiveName( $title, $repo, $archiveName ) {
22 return new self( $title, $repo, null, $archiveName );
25 static function newFromRow( $row, $repo ) {
26 $title = Title
::makeTitle( NS_IMAGE
, $row->oi_name
);
27 $file = new self( $title, $repo, null, $row->oi_archive_name
);
28 $file->loadFromRow( $row, 'oi_' );
32 static function newFromKey( $sha1, $repo, $timestamp = false ) {
33 # Polymorphic function name to distinguish foreign and local fetches
34 $fname = get_class( $this ) . '::' . __FUNCTION__
;
36 $conds = array( 'oi_sha1' => $sha1 );
38 $conds['oi_timestamp'] = $timestamp;
40 $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ), $conds, $fname );
42 return self
::newFromRow( $row, $repo );
49 * Fields in the oldimage table
51 static function selectFields() {
73 * @param FileRepo $repo
74 * @param string $time Timestamp or null to load by archive name
75 * @param string $archiveName Archive name or null to load by timestamp
77 function __construct( $title, $repo, $time, $archiveName ) {
78 parent
::__construct( $title, $repo );
79 $this->requestedTime
= $time;
80 $this->archive_name
= $archiveName;
81 if ( is_null( $time ) && is_null( $archiveName ) ) {
82 throw new MWException( __METHOD__
.': must specify at least one of $time or $archiveName' );
86 function getCacheKey() {
90 function getArchiveName() {
91 if ( !isset( $this->archive_name
) ) {
94 return $this->archive_name
;
101 function isVisible() {
102 return $this->exists() && !$this->isDeleted(File
::DELETED_FILE
);
105 function loadFromDB() {
106 wfProfileIn( __METHOD__
);
107 $this->dataLoaded
= true;
108 $dbr = $this->repo
->getSlaveDB();
109 $conds = array( 'oi_name' => $this->getName() );
110 if ( is_null( $this->requestedTime
) ) {
111 $conds['oi_archive_name'] = $this->archive_name
;
113 $conds[] = 'oi_timestamp = ' . $dbr->addQuotes( $dbr->timestamp( $this->requestedTime
) );
115 $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
116 $conds, __METHOD__
, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
118 $this->loadFromRow( $row, 'oi_' );
120 $this->fileExists
= false;
122 wfProfileOut( __METHOD__
);
125 function getCacheFields( $prefix = 'img_' ) {
126 $fields = parent
::getCacheFields( $prefix );
127 $fields[] = $prefix . 'archive_name';
128 $fields[] = $prefix . 'deleted';
133 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
136 function getUrlRel() {
137 return 'archive/' . $this->getHashPath() . urlencode( $this->getArchiveName() );
140 function upgradeRow() {
141 wfProfileIn( __METHOD__
);
142 $this->loadFromFile();
144 # Don't destroy file info of missing files
145 if ( !$this->fileExists
) {
146 wfDebug( __METHOD__
.": file does not exist, aborting\n" );
147 wfProfileOut( __METHOD__
);
151 $dbw = $this->repo
->getMasterDB();
152 list( $major, $minor ) = self
::splitMime( $this->mime
);
154 wfDebug(__METHOD__
.': upgrading '.$this->archive_name
." to the current schema\n");
155 $dbw->update( 'oldimage',
157 'oi_width' => $this->width
,
158 'oi_height' => $this->height
,
159 'oi_bits' => $this->bits
,
160 'oi_media_type' => $this->media_type
,
161 'oi_major_mime' => $major,
162 'oi_minor_mime' => $minor,
163 'oi_metadata' => $this->metadata
,
164 'oi_sha1' => $this->sha1
,
166 'oi_name' => $this->getName(),
167 'oi_archive_name' => $this->archive_name
),
170 wfProfileOut( __METHOD__
);
174 * int $field one of DELETED_* bitfield constants
175 * for file or revision rows
178 function isDeleted( $field ) {
179 return ($this->deleted
& $field) == $field;
183 * Determine if the current user is allowed to view a particular
184 * field of this FileStore image file, if it's marked as deleted.
188 function userCan( $field ) {
189 if( isset($this->deleted
) && ($this->deleted
& $field) == $field ) {
191 $permission = ( $this->deleted
& File
::DELETED_RESTRICTED
) == File
::DELETED_RESTRICTED
194 wfDebug( "Checking for $permission due to $field match on $this->mDeleted\n" );
195 return $wgUser->isAllowed( $permission );