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
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_' );
40 * @param $repo LocalRepo
41 * @param bool $timestamp
42 * @return bool|OldLocalFile
44 static function newFromKey( $sha1, $repo, $timestamp = false ) {
45 $conds = array( 'oi_sha1' => $sha1 );
47 $conds['oi_timestamp'] = $timestamp;
49 $dbr = $repo->getSlaveDB();
50 $row = $dbr->selectRow( 'oldimage', self
::selectFields(), $conds, __METHOD__
);
52 return self
::newFromRow( $row, $repo );
59 * Fields in the oldimage table
61 static function selectFields() {
84 * @param $repo FileRepo
85 * @param $time String: timestamp or null to load by archive name
86 * @param $archiveName String: archive name or null to load by timestamp
88 function __construct( $title, $repo, $time, $archiveName ) {
89 parent
::__construct( $title, $repo );
90 $this->requestedTime
= $time;
91 $this->archive_name
= $archiveName;
92 if ( is_null( $time ) && is_null( $archiveName ) ) {
93 throw new MWException( __METHOD__
.': must specify at least one of $time or $archiveName' );
97 function getCacheKey() {
101 function getArchiveName() {
102 if ( !isset( $this->archive_name
) ) {
105 return $this->archive_name
;
112 function isVisible() {
113 return $this->exists() && !$this->isDeleted(File
::DELETED_FILE
);
116 function loadFromDB() {
117 wfProfileIn( __METHOD__
);
118 $this->dataLoaded
= true;
119 $dbr = $this->repo
->getSlaveDB();
120 $conds = array( 'oi_name' => $this->getName() );
121 if ( is_null( $this->requestedTime
) ) {
122 $conds['oi_archive_name'] = $this->archive_name
;
124 $conds[] = 'oi_timestamp = ' . $dbr->addQuotes( $dbr->timestamp( $this->requestedTime
) );
126 $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
127 $conds, __METHOD__
, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
129 $this->loadFromRow( $row, 'oi_' );
131 $this->fileExists
= false;
133 wfProfileOut( __METHOD__
);
136 function getCacheFields( $prefix = 'img_' ) {
137 $fields = parent
::getCacheFields( $prefix );
138 $fields[] = $prefix . 'archive_name';
139 $fields[] = $prefix . 'deleted';
144 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
147 function getUrlRel() {
148 return 'archive/' . $this->getHashPath() . rawurlencode( $this->getArchiveName() );
151 function upgradeRow() {
152 wfProfileIn( __METHOD__
);
153 $this->loadFromFile();
155 # Don't destroy file info of missing files
156 if ( !$this->fileExists
) {
157 wfDebug( __METHOD__
.": file does not exist, aborting\n" );
158 wfProfileOut( __METHOD__
);
162 $dbw = $this->repo
->getMasterDB();
163 list( $major, $minor ) = self
::splitMime( $this->mime
);
165 wfDebug(__METHOD__
.': upgrading '.$this->archive_name
." to the current schema\n");
166 $dbw->update( 'oldimage',
168 'oi_width' => $this->width
,
169 'oi_height' => $this->height
,
170 'oi_bits' => $this->bits
,
171 'oi_media_type' => $this->media_type
,
172 'oi_major_mime' => $major,
173 'oi_minor_mime' => $minor,
174 'oi_metadata' => $this->metadata
,
175 'oi_sha1' => $this->sha1
,
177 'oi_name' => $this->getName(),
178 'oi_archive_name' => $this->archive_name
),
181 wfProfileOut( __METHOD__
);
185 * @param $field Integer: one of DELETED_* bitfield constants
186 * for file or revision rows
189 function isDeleted( $field ) {
191 return ($this->deleted
& $field) == $field;
195 * Returns bitfield value
198 function getVisibility() {
200 return (int)$this->deleted
;
204 * Determine if the current user is allowed to view a particular
205 * field of this image file, if it's marked as deleted.
207 * @param $field Integer
210 function userCan( $field ) {
212 return Revision
::userCanBitfield( $this->deleted
, $field );
216 * Upload a file directly into archive. Generally for Special:Import.
218 * @param $srcPath string File system path of the source file
219 * @param $archiveName string Full archive name of the file, in the form
220 * $timestamp!$filename, where $filename must match $this->getName()
222 * @return FileRepoStatus
224 function uploadOld( $srcPath, $archiveName, $timestamp, $comment, $user, $flags = 0 ) {
227 $dstRel = 'archive/' . $this->getHashPath() . $archiveName;
228 $status = $this->publishTo( $srcPath, $dstRel,
229 $flags & File
::DELETE_SOURCE ? FileRepo
::DELETE_SOURCE
: 0
232 if ( $status->isGood() ) {
233 if ( !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) ) {
234 $status->fatal( 'filenotfound', $srcPath );
244 * Record a file upload in the oldimage table, without adding log entries.
246 * @param $srcPath string File system path to the source file
247 * @param $archiveName string The archive name of the file
248 * @param $comment string Upload comment
249 * @param $user User User who did this upload
252 function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
253 $dbw = $this->repo
->getMasterDB();
256 $dstPath = $this->repo
->getZonePath( 'public' ) . '/' . $this->getRel();
257 $props = self
::getPropsFromPath( $dstPath );
258 if ( !$props['fileExists'] ) {
262 $dbw->insert( 'oldimage',
264 'oi_name' => $this->getName(),
265 'oi_archive_name' => $archiveName,
266 'oi_size' => $props['size'],
267 'oi_width' => intval( $props['width'] ),
268 'oi_height' => intval( $props['height'] ),
269 'oi_bits' => $props['bits'],
270 'oi_timestamp' => $dbw->timestamp( $timestamp ),
271 'oi_description' => $comment,
272 'oi_user' => $user->getId(),
273 'oi_user_text' => $user->getName(),
274 'oi_metadata' => $props['metadata'],
275 'oi_media_type' => $props['media_type'],
276 'oi_major_mime' => $props['major_mime'],
277 'oi_minor_mime' => $props['minor_mime'],
278 'oi_sha1' => $props['sha1'],