3 * Old file in the in the oldimage table
10 * Class to represent a file in the oldimage table
14 class OldLocalFile
extends LocalFile
{
15 var $requestedTime, $archive_name;
17 const CACHE_VERSION
= 1;
18 const MAX_CACHE_ROWS
= 20;
20 static function newFromTitle( $title, $repo, $time = null ) {
21 # The null default value is only here to avoid an E_STRICT
23 throw new MWException( __METHOD__
.' got null for $time parameter' );
24 return new self( $title, $repo, $time, null );
27 static function newFromArchiveName( $title, $repo, $archiveName ) {
28 return new self( $title, $repo, null, $archiveName );
31 static function newFromRow( $row, $repo ) {
32 $title = Title
::makeTitle( NS_FILE
, $row->oi_name
);
33 $file = new self( $title, $repo, null, $row->oi_archive_name
);
34 $file->loadFromRow( $row, 'oi_' );
41 * @param $repo LocalRepo
42 * @param bool $timestamp
43 * @return bool|OldLocalFile
45 static function newFromKey( $sha1, $repo, $timestamp = false ) {
46 $conds = array( 'oi_sha1' => $sha1 );
48 $conds['oi_timestamp'] = $timestamp;
50 $dbr = $repo->getSlaveDB();
51 $row = $dbr->selectRow( 'oldimage', self
::selectFields(), $conds, __METHOD__
);
53 return self
::newFromRow( $row, $repo );
60 * Fields in the oldimage table
62 static function selectFields() {
85 * @param $repo FileRepo
86 * @param $time String: timestamp or null to load by archive name
87 * @param $archiveName String: archive name or null to load by timestamp
89 function __construct( $title, $repo, $time, $archiveName ) {
90 parent
::__construct( $title, $repo );
91 $this->requestedTime
= $time;
92 $this->archive_name
= $archiveName;
93 if ( is_null( $time ) && is_null( $archiveName ) ) {
94 throw new MWException( __METHOD__
.': must specify at least one of $time or $archiveName' );
98 function getCacheKey() {
102 function getArchiveName() {
103 if ( !isset( $this->archive_name
) ) {
106 return $this->archive_name
;
113 function isVisible() {
114 return $this->exists() && !$this->isDeleted(File
::DELETED_FILE
);
117 function loadFromDB() {
118 wfProfileIn( __METHOD__
);
119 $this->dataLoaded
= true;
120 $dbr = $this->repo
->getSlaveDB();
121 $conds = array( 'oi_name' => $this->getName() );
122 if ( is_null( $this->requestedTime
) ) {
123 $conds['oi_archive_name'] = $this->archive_name
;
125 $conds[] = 'oi_timestamp = ' . $dbr->addQuotes( $dbr->timestamp( $this->requestedTime
) );
127 $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
128 $conds, __METHOD__
, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
130 $this->loadFromRow( $row, 'oi_' );
132 $this->fileExists
= false;
134 wfProfileOut( __METHOD__
);
137 function getCacheFields( $prefix = 'img_' ) {
138 $fields = parent
::getCacheFields( $prefix );
139 $fields[] = $prefix . 'archive_name';
140 $fields[] = $prefix . 'deleted';
145 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
148 function getUrlRel() {
149 return 'archive/' . $this->getHashPath() . rawurlencode( $this->getArchiveName() );
152 function upgradeRow() {
153 wfProfileIn( __METHOD__
);
154 $this->loadFromFile();
156 # Don't destroy file info of missing files
157 if ( !$this->fileExists
) {
158 wfDebug( __METHOD__
.": file does not exist, aborting\n" );
159 wfProfileOut( __METHOD__
);
163 $dbw = $this->repo
->getMasterDB();
164 list( $major, $minor ) = self
::splitMime( $this->mime
);
166 wfDebug(__METHOD__
.': upgrading '.$this->archive_name
." to the current schema\n");
167 $dbw->update( 'oldimage',
169 'oi_width' => $this->width
,
170 'oi_height' => $this->height
,
171 'oi_bits' => $this->bits
,
172 'oi_media_type' => $this->media_type
,
173 'oi_major_mime' => $major,
174 'oi_minor_mime' => $minor,
175 'oi_metadata' => $this->metadata
,
176 'oi_sha1' => $this->sha1
,
178 'oi_name' => $this->getName(),
179 'oi_archive_name' => $this->archive_name
),
182 wfProfileOut( __METHOD__
);
186 * @param $field Integer: one of DELETED_* bitfield constants
187 * for file or revision rows
190 function isDeleted( $field ) {
192 return ($this->deleted
& $field) == $field;
196 * Returns bitfield value
199 function getVisibility() {
201 return (int)$this->deleted
;
205 * Determine if the current user is allowed to view a particular
206 * field of this image file, if it's marked as deleted.
208 * @param $field Integer
211 function userCan( $field ) {
213 return Revision
::userCanBitfield( $this->deleted
, $field );