a22da16fb12c22b5c29b1f86be13518d656177a4
3 * Old file 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
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
68 static function selectFields() {
91 * @param $repo FileRepo
92 * @param $time String: timestamp or null to load by archive name
93 * @param $archiveName String: archive name or null to load by timestamp
95 function __construct( $title, $repo, $time, $archiveName ) {
96 parent
::__construct( $title, $repo );
97 $this->requestedTime
= $time;
98 $this->archive_name
= $archiveName;
99 if ( is_null( $time ) && is_null( $archiveName ) ) {
100 throw new MWException( __METHOD__
.': must specify at least one of $time or $archiveName' );
104 function getCacheKey() {
108 function getArchiveName() {
109 if ( !isset( $this->archive_name
) ) {
112 return $this->archive_name
;
119 function isVisible() {
120 return $this->exists() && !$this->isDeleted(File
::DELETED_FILE
);
123 function loadFromDB() {
124 wfProfileIn( __METHOD__
);
125 $this->dataLoaded
= true;
126 $dbr = $this->repo
->getSlaveDB();
127 $conds = array( 'oi_name' => $this->getName() );
128 if ( is_null( $this->requestedTime
) ) {
129 $conds['oi_archive_name'] = $this->archive_name
;
131 $conds[] = 'oi_timestamp = ' . $dbr->addQuotes( $dbr->timestamp( $this->requestedTime
) );
133 $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
134 $conds, __METHOD__
, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
136 $this->loadFromRow( $row, 'oi_' );
138 $this->fileExists
= false;
140 wfProfileOut( __METHOD__
);
143 function getCacheFields( $prefix = 'img_' ) {
144 $fields = parent
::getCacheFields( $prefix );
145 $fields[] = $prefix . 'archive_name';
146 $fields[] = $prefix . 'deleted';
151 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
154 function getUrlRel() {
155 return 'archive/' . $this->getHashPath() . rawurlencode( $this->getArchiveName() );
158 function upgradeRow() {
159 wfProfileIn( __METHOD__
);
160 $this->loadFromFile();
162 # Don't destroy file info of missing files
163 if ( !$this->fileExists
) {
164 wfDebug( __METHOD__
.": file does not exist, aborting\n" );
165 wfProfileOut( __METHOD__
);
169 $dbw = $this->repo
->getMasterDB();
170 list( $major, $minor ) = self
::splitMime( $this->mime
);
172 wfDebug(__METHOD__
.': upgrading '.$this->archive_name
." to the current schema\n");
173 $dbw->update( 'oldimage',
175 'oi_width' => $this->width
,
176 'oi_height' => $this->height
,
177 'oi_bits' => $this->bits
,
178 'oi_media_type' => $this->media_type
,
179 'oi_major_mime' => $major,
180 'oi_minor_mime' => $minor,
181 'oi_metadata' => $this->metadata
,
182 'oi_sha1' => $this->sha1
,
184 'oi_name' => $this->getName(),
185 'oi_archive_name' => $this->archive_name
),
188 wfProfileOut( __METHOD__
);
192 * @param $field Integer: one of DELETED_* bitfield constants
193 * for file or revision rows
196 function isDeleted( $field ) {
198 return ($this->deleted
& $field) == $field;
202 * Returns bitfield value
205 function getVisibility() {
207 return (int)$this->deleted
;
211 * Determine if the current user is allowed to view a particular
212 * field of this image file, if it's marked as deleted.
214 * @param $field Integer
215 * @param $user User object to check, or null to use $wgUser
218 function userCan( $field, User
$user = null ) {
220 return Revision
::userCanBitfield( $this->deleted
, $field, $user );
224 * Upload a file directly into archive. Generally for Special:Import.
226 * @param $srcPath string File system path of the source file
227 * @param $archiveName string Full archive name of the file, in the form
228 * $timestamp!$filename, where $filename must match $this->getName()
230 * @return FileRepoStatus
232 function uploadOld( $srcPath, $archiveName, $timestamp, $comment, $user, $flags = 0 ) {
235 $dstRel = 'archive/' . $this->getHashPath() . $archiveName;
236 $status = $this->publishTo( $srcPath, $dstRel,
237 $flags & File
::DELETE_SOURCE ? FileRepo
::DELETE_SOURCE
: 0
240 if ( $status->isGood() ) {
241 if ( !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) ) {
242 $status->fatal( 'filenotfound', $srcPath );
252 * Record a file upload in the oldimage table, without adding log entries.
254 * @param $srcPath string File system path to the source file
255 * @param $archiveName string The archive name of the file
256 * @param $comment string Upload comment
257 * @param $user User User who did this upload
260 function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
261 $dbw = $this->repo
->getMasterDB();
264 $dstPath = $this->repo
->getZonePath( 'public' ) . '/' . $this->getRel();
265 $props = self
::getPropsFromPath( $dstPath );
266 if ( !$props['fileExists'] ) {
270 $dbw->insert( 'oldimage',
272 'oi_name' => $this->getName(),
273 'oi_archive_name' => $archiveName,
274 'oi_size' => $props['size'],
275 'oi_width' => intval( $props['width'] ),
276 'oi_height' => intval( $props['height'] ),
277 'oi_bits' => $props['bits'],
278 'oi_timestamp' => $dbw->timestamp( $timestamp ),
279 'oi_description' => $comment,
280 'oi_user' => $user->getId(),
281 'oi_user_text' => $user->getName(),
282 'oi_metadata' => $props['metadata'],
283 'oi_media_type' => $props['media_type'],
284 'oi_major_mime' => $props['major_mime'],
285 'oi_minor_mime' => $props['minor_mime'],
286 'oi_sha1' => $props['sha1'],