3 * Old file in the oldimage table
6 * @ingroup FileAbstraction
10 * Class to represent a file in the oldimage table
12 * @ingroup FileAbstraction
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
22 if ( $time === null ) {
23 throw new MWException( __METHOD__
.' got null for $time parameter' );
25 return new self( $title, $repo, $time, null );
28 static function newFromArchiveName( $title, $repo, $archiveName ) {
29 return new self( $title, $repo, null, $archiveName );
32 static function newFromRow( $row, $repo ) {
33 $title = Title
::makeTitle( NS_FILE
, $row->oi_name
);
34 $file = new self( $title, $repo, null, $row->oi_archive_name
);
35 $file->loadFromRow( $row, 'oi_' );
40 * Create a OldLocalFile from a SHA-1 key
41 * Do not call this except from inside a repo class.
43 * @param $sha1 string base-36 SHA-1
44 * @param $repo LocalRepo
45 * @param string|bool $timestamp MW_timestamp (optional)
47 * @return bool|OldLocalFile
49 static function newFromKey( $sha1, $repo, $timestamp = false ) {
50 $dbr = $repo->getSlaveDB();
52 $conds = array( 'oi_sha1' => $sha1 );
54 $conds['oi_timestamp'] = $dbr->timestamp( $timestamp );
57 $row = $dbr->selectRow( 'oldimage', self
::selectFields(), $conds, __METHOD__
);
59 return self
::newFromRow( $row, $repo );
66 * Fields in the oldimage table
69 static function selectFields() {
92 * @param $repo FileRepo
93 * @param $time String: timestamp or null to load by archive name
94 * @param $archiveName String: archive name or null to load by timestamp
96 function __construct( $title, $repo, $time, $archiveName ) {
97 parent
::__construct( $title, $repo );
98 $this->requestedTime
= $time;
99 $this->archive_name
= $archiveName;
100 if ( is_null( $time ) && is_null( $archiveName ) ) {
101 throw new MWException( __METHOD__
.': must specify at least one of $time or $archiveName' );
105 function getCacheKey() {
109 function getArchiveName() {
110 if ( !isset( $this->archive_name
) ) {
113 return $this->archive_name
;
120 function isVisible() {
121 return $this->exists() && !$this->isDeleted(File
::DELETED_FILE
);
124 function loadFromDB() {
125 wfProfileIn( __METHOD__
);
126 $this->dataLoaded
= true;
127 $dbr = $this->repo
->getSlaveDB();
128 $conds = array( 'oi_name' => $this->getName() );
129 if ( is_null( $this->requestedTime
) ) {
130 $conds['oi_archive_name'] = $this->archive_name
;
132 $conds[] = 'oi_timestamp = ' . $dbr->addQuotes( $dbr->timestamp( $this->requestedTime
) );
134 $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
135 $conds, __METHOD__
, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
137 $this->loadFromRow( $row, 'oi_' );
139 $this->fileExists
= false;
141 wfProfileOut( __METHOD__
);
144 function getCacheFields( $prefix = 'img_' ) {
145 $fields = parent
::getCacheFields( $prefix );
146 $fields[] = $prefix . 'archive_name';
147 $fields[] = $prefix . 'deleted';
152 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
155 function getUrlRel() {
156 return 'archive/' . $this->getHashPath() . rawurlencode( $this->getArchiveName() );
159 function upgradeRow() {
160 wfProfileIn( __METHOD__
);
161 $this->loadFromFile();
163 # Don't destroy file info of missing files
164 if ( !$this->fileExists
) {
165 wfDebug( __METHOD__
.": file does not exist, aborting\n" );
166 wfProfileOut( __METHOD__
);
170 $dbw = $this->repo
->getMasterDB();
171 list( $major, $minor ) = self
::splitMime( $this->mime
);
173 wfDebug(__METHOD__
.': upgrading '.$this->archive_name
." to the current schema\n");
174 $dbw->update( 'oldimage',
176 'oi_width' => $this->width
,
177 'oi_height' => $this->height
,
178 'oi_bits' => $this->bits
,
179 'oi_media_type' => $this->media_type
,
180 'oi_major_mime' => $major,
181 'oi_minor_mime' => $minor,
182 'oi_metadata' => $this->metadata
,
183 'oi_sha1' => $this->sha1
,
185 'oi_name' => $this->getName(),
186 'oi_archive_name' => $this->archive_name
),
189 wfProfileOut( __METHOD__
);
193 * @param $field Integer: one of DELETED_* bitfield constants
194 * for file or revision rows
197 function isDeleted( $field ) {
199 return ($this->deleted
& $field) == $field;
203 * Returns bitfield value
206 function getVisibility() {
208 return (int)$this->deleted
;
212 * Determine if the current user is allowed to view a particular
213 * field of this image file, if it's marked as deleted.
215 * @param $field Integer
216 * @param $user User object to check, or null to use $wgUser
219 function userCan( $field, User
$user = null ) {
221 return Revision
::userCanBitfield( $this->deleted
, $field, $user );
225 * Upload a file directly into archive. Generally for Special:Import.
227 * @param $srcPath string File system path of the source file
228 * @param $archiveName string Full archive name of the file, in the form
229 * $timestamp!$filename, where $filename must match $this->getName()
231 * @return FileRepoStatus
233 function uploadOld( $srcPath, $archiveName, $timestamp, $comment, $user, $flags = 0 ) {
236 $dstRel = 'archive/' . $this->getHashPath() . $archiveName;
237 $status = $this->publishTo( $srcPath, $dstRel,
238 $flags & File
::DELETE_SOURCE ? FileRepo
::DELETE_SOURCE
: 0
241 if ( $status->isGood() ) {
242 if ( !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) ) {
243 $status->fatal( 'filenotfound', $srcPath );
253 * Record a file upload in the oldimage table, without adding log entries.
255 * @param $srcPath string File system path to the source file
256 * @param $archiveName string The archive name of the file
257 * @param $comment string Upload comment
258 * @param $user User User who did this upload
261 function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
262 $dbw = $this->repo
->getMasterDB();
263 $dbw->begin( __METHOD__
);
265 $dstPath = $this->repo
->getZonePath( 'public' ) . '/' . $this->getRel();
266 $props = $this->repo
->getFileProps( $dstPath );
267 if ( !$props['fileExists'] ) {
271 $dbw->insert( 'oldimage',
273 'oi_name' => $this->getName(),
274 'oi_archive_name' => $archiveName,
275 'oi_size' => $props['size'],
276 'oi_width' => intval( $props['width'] ),
277 'oi_height' => intval( $props['height'] ),
278 'oi_bits' => $props['bits'],
279 'oi_timestamp' => $dbw->timestamp( $timestamp ),
280 'oi_description' => $comment,
281 'oi_user' => $user->getId(),
282 'oi_user_text' => $user->getName(),
283 'oi_metadata' => $props['metadata'],
284 'oi_media_type' => $props['media_type'],
285 'oi_major_mime' => $props['major_mime'],
286 'oi_minor_mime' => $props['minor_mime'],
287 'oi_sha1' => $props['sha1'],
291 $dbw->commit( __METHOD__
);