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 );
50 * @param FileRepo $repo
51 * @param string $time Timestamp or null to load by archive name
52 * @param string $archiveName Archive name or null to load by timestamp
54 function __construct( $title, $repo, $time, $archiveName ) {
55 parent
::__construct( $title, $repo );
56 $this->requestedTime
= $time;
57 $this->archive_name
= $archiveName;
58 if ( is_null( $time ) && is_null( $archiveName ) ) {
59 throw new MWException( __METHOD__
.': must specify at least one of $time or $archiveName' );
63 function getCacheKey() {
67 function getArchiveName() {
68 if ( !isset( $this->archive_name
) ) {
71 return $this->archive_name
;
78 function isVisible() {
79 return $this->exists() && !$this->isDeleted(File
::DELETED_FILE
);
82 function loadFromDB() {
83 wfProfileIn( __METHOD__
);
84 $this->dataLoaded
= true;
85 $dbr = $this->repo
->getSlaveDB();
86 $conds = array( 'oi_name' => $this->getName() );
87 if ( is_null( $this->requestedTime
) ) {
88 $conds['oi_archive_name'] = $this->archive_name
;
90 $conds[] = 'oi_timestamp = ' . $dbr->addQuotes( $dbr->timestamp( $this->requestedTime
) );
92 $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
93 $conds, __METHOD__
, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
95 $this->loadFromRow( $row, 'oi_' );
97 $this->fileExists
= false;
99 wfProfileOut( __METHOD__
);
102 function getCacheFields( $prefix = 'img_' ) {
103 $fields = parent
::getCacheFields( $prefix );
104 $fields[] = $prefix . 'archive_name';
105 $fields[] = $prefix . 'deleted';
110 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
113 function getUrlRel() {
114 return 'archive/' . $this->getHashPath() . urlencode( $this->getArchiveName() );
117 function upgradeRow() {
118 wfProfileIn( __METHOD__
);
119 $this->loadFromFile();
121 # Don't destroy file info of missing files
122 if ( !$this->fileExists
) {
123 wfDebug( __METHOD__
.": file does not exist, aborting\n" );
124 wfProfileOut( __METHOD__
);
128 $dbw = $this->repo
->getMasterDB();
129 list( $major, $minor ) = self
::splitMime( $this->mime
);
131 wfDebug(__METHOD__
.': upgrading '.$this->archive_name
." to the current schema\n");
132 $dbw->update( 'oldimage',
134 'oi_width' => $this->width
,
135 'oi_height' => $this->height
,
136 'oi_bits' => $this->bits
,
137 'oi_media_type' => $this->media_type
,
138 'oi_major_mime' => $major,
139 'oi_minor_mime' => $minor,
140 'oi_metadata' => $this->metadata
,
141 'oi_sha1' => $this->sha1
,
143 'oi_name' => $this->getName(),
144 'oi_archive_name' => $this->archive_name
),
147 wfProfileOut( __METHOD__
);
151 * int $field one of DELETED_* bitfield constants
152 * for file or revision rows
155 function isDeleted( $field ) {
156 return ($this->deleted
& $field) == $field;
160 * Determine if the current user is allowed to view a particular
161 * field of this FileStore image file, if it's marked as deleted.
165 function userCan( $field ) {
166 if( isset($this->deleted
) && ($this->deleted
& $field) == $field ) {
168 $permission = ( $this->deleted
& File
::DELETED_RESTRICTED
) == File
::DELETED_RESTRICTED
171 wfDebug( "Checking for $permission due to $field match on $this->mDeleted\n" );
172 return $wgUser->isAllowed( $permission );