2d249d7a04b3482c5dae43e68c48eecb05e8d054
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_FILE
, $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() {
74 * @param FileRepo $repo
75 * @param string $time Timestamp or null to load by archive name
76 * @param string $archiveName Archive name or null to load by timestamp
78 function __construct( $title, $repo, $time, $archiveName ) {
79 parent
::__construct( $title, $repo );
80 $this->requestedTime
= $time;
81 $this->archive_name
= $archiveName;
82 if ( is_null( $time ) && is_null( $archiveName ) ) {
83 throw new MWException( __METHOD__
.': must specify at least one of $time or $archiveName' );
87 function getCacheKey() {
91 function getArchiveName() {
92 if ( !isset( $this->archive_name
) ) {
95 return $this->archive_name
;
102 function isVisible() {
103 return $this->exists() && !$this->isDeleted(File
::DELETED_FILE
);
106 function loadFromDB() {
107 wfProfileIn( __METHOD__
);
108 $this->dataLoaded
= true;
109 $dbr = $this->repo
->getSlaveDB();
110 $conds = array( 'oi_name' => $this->getName() );
111 if ( is_null( $this->requestedTime
) ) {
112 $conds['oi_archive_name'] = $this->archive_name
;
114 $conds[] = 'oi_timestamp = ' . $dbr->addQuotes( $dbr->timestamp( $this->requestedTime
) );
116 $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
117 $conds, __METHOD__
, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
119 $this->loadFromRow( $row, 'oi_' );
121 $this->fileExists
= false;
123 wfProfileOut( __METHOD__
);
126 function getCacheFields( $prefix = 'img_' ) {
127 $fields = parent
::getCacheFields( $prefix );
128 $fields[] = $prefix . 'archive_name';
129 $fields[] = $prefix . 'deleted';
134 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
137 function getUrlRel() {
138 return 'archive/' . $this->getHashPath() . urlencode( $this->getArchiveName() );
141 function upgradeRow() {
142 wfProfileIn( __METHOD__
);
143 $this->loadFromFile();
145 # Don't destroy file info of missing files
146 if ( !$this->fileExists
) {
147 wfDebug( __METHOD__
.": file does not exist, aborting\n" );
148 wfProfileOut( __METHOD__
);
152 $dbw = $this->repo
->getMasterDB();
153 list( $major, $minor ) = self
::splitMime( $this->mime
);
155 wfDebug(__METHOD__
.': upgrading '.$this->archive_name
." to the current schema\n");
156 $dbw->update( 'oldimage',
158 'oi_width' => $this->width
,
159 'oi_height' => $this->height
,
160 'oi_bits' => $this->bits
,
161 'oi_media_type' => $this->media_type
,
162 'oi_major_mime' => $major,
163 'oi_minor_mime' => $minor,
164 'oi_metadata' => $this->metadata
,
165 'oi_sha1' => $this->sha1
,
167 'oi_name' => $this->getName(),
168 'oi_archive_name' => $this->archive_name
),
171 wfProfileOut( __METHOD__
);
175 * int $field one of DELETED_* bitfield constants
176 * for file or revision rows
179 function isDeleted( $field ) {
180 return ($this->deleted
& $field) == $field;
184 * Returns bitfield value
187 function getVisibility() {
188 return (int)$this->deleted
;
192 * Determine if the current user is allowed to view a particular
193 * field of this image file, if it's marked as deleted.
197 function userCan( $field ) {
198 if( isset($this->deleted
) && ($this->deleted
& $field) == $field ) {
200 $permission = ( $this->deleted
& File
::DELETED_RESTRICTED
) == File
::DELETED_RESTRICTED
203 wfDebug( "Checking for $permission due to $field match on $this->mDeleted\n" );
204 return $wgUser->isAllowed( $permission );