3 * Old file in the oldimage table.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup FileAbstraction
25 * Class to represent a file in the oldimage table
27 * @ingroup FileAbstraction
29 class OldLocalFile
extends LocalFile
{
30 var $requestedTime, $archive_name;
32 const CACHE_VERSION
= 1;
33 const MAX_CACHE_ROWS
= 20;
37 * @param $repo FileRepo
39 * @return OldLocalFile
42 static function newFromTitle( $title, $repo, $time = null ) {
43 # The null default value is only here to avoid an E_STRICT
44 if ( $time === null ) {
45 throw new MWException( __METHOD__
. ' got null for $time parameter' );
48 return new self( $title, $repo, $time, null );
53 * @param $repo FileRepo
55 * @return OldLocalFile
57 static function newFromArchiveName( $title, $repo, $archiveName ) {
58 return new self( $title, $repo, null, $archiveName );
63 * @param $repo FileRepo
64 * @return OldLocalFile
66 static function newFromRow( $row, $repo ) {
67 $title = Title
::makeTitle( NS_FILE
, $row->oi_name
);
68 $file = new self( $title, $repo, null, $row->oi_archive_name
);
69 $file->loadFromRow( $row, 'oi_' );
75 * Create a OldLocalFile from a SHA-1 key
76 * Do not call this except from inside a repo class.
78 * @param string $sha1 base-36 SHA-1
79 * @param $repo LocalRepo
80 * @param string|bool $timestamp MW_timestamp (optional)
82 * @return bool|OldLocalFile
84 static function newFromKey( $sha1, $repo, $timestamp = false ) {
85 $dbr = $repo->getSlaveDB();
87 $conds = array( 'oi_sha1' => $sha1 );
89 $conds['oi_timestamp'] = $dbr->timestamp( $timestamp );
92 $row = $dbr->selectRow( 'oldimage', self
::selectFields(), $conds, __METHOD__
);
94 return self
::newFromRow( $row, $repo );
101 * Fields in the oldimage table
104 static function selectFields() {
126 * @param $title Title
127 * @param $repo FileRepo
128 * @param string $time timestamp or null to load by archive name
129 * @param string $archiveName archive name or null to load by timestamp
130 * @throws MWException
132 function __construct( $title, $repo, $time, $archiveName ) {
133 parent
::__construct( $title, $repo );
134 $this->requestedTime
= $time;
135 $this->archive_name
= $archiveName;
136 if ( is_null( $time ) && is_null( $archiveName ) ) {
137 throw new MWException( __METHOD__
. ': must specify at least one of $time or $archiveName' );
144 function getCacheKey() {
151 function getArchiveName() {
152 if ( !isset( $this->archive_name
) ) {
156 return $this->archive_name
;
169 function isVisible() {
170 return $this->exists() && !$this->isDeleted( File
::DELETED_FILE
);
173 function loadFromDB() {
174 wfProfileIn( __METHOD__
);
176 $this->dataLoaded
= true;
177 $dbr = $this->repo
->getSlaveDB();
178 $conds = array( 'oi_name' => $this->getName() );
179 if ( is_null( $this->requestedTime
) ) {
180 $conds['oi_archive_name'] = $this->archive_name
;
182 $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime
);
184 $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
185 $conds, __METHOD__
, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
187 $this->loadFromRow( $row, 'oi_' );
189 $this->fileExists
= false;
192 wfProfileOut( __METHOD__
);
196 * Load lazy file metadata from the DB
198 protected function loadExtraFromDB() {
199 wfProfileIn( __METHOD__
);
201 $this->extraDataLoaded
= true;
202 $dbr = $this->repo
->getSlaveDB();
203 $conds = array( 'oi_name' => $this->getName() );
204 if ( is_null( $this->requestedTime
) ) {
205 $conds['oi_archive_name'] = $this->archive_name
;
207 $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime
);
209 // In theory the file could have just been renamed/deleted...oh well
210 $row = $dbr->selectRow( 'oldimage', $this->getLazyCacheFields( 'oi_' ),
211 $conds, __METHOD__
, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
213 if ( !$row ) { // fallback to master
214 $dbr = $this->repo
->getMasterDB();
215 $row = $dbr->selectRow( 'oldimage', $this->getLazyCacheFields( 'oi_' ),
216 $conds, __METHOD__
, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
220 foreach ( $this->unprefixRow( $row, 'oi_' ) as $name => $value ) {
221 $this->$name = $value;
224 wfProfileOut( __METHOD__
);
225 throw new MWException( "Could not find data for image '{$this->archive_name}'." );
228 wfProfileOut( __METHOD__
);
232 * @param $prefix string
235 function getCacheFields( $prefix = 'img_' ) {
236 $fields = parent
::getCacheFields( $prefix );
237 $fields[] = $prefix . 'archive_name';
238 $fields[] = $prefix . 'deleted';
247 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
253 function getUrlRel() {
254 return 'archive/' . $this->getHashPath() . rawurlencode( $this->getArchiveName() );
257 function upgradeRow() {
258 wfProfileIn( __METHOD__
);
259 $this->loadFromFile();
261 # Don't destroy file info of missing files
262 if ( !$this->fileExists
) {
263 wfDebug( __METHOD__
. ": file does not exist, aborting\n" );
264 wfProfileOut( __METHOD__
);
269 $dbw = $this->repo
->getMasterDB();
270 list( $major, $minor ) = self
::splitMime( $this->mime
);
272 wfDebug( __METHOD__
. ': upgrading ' . $this->archive_name
. " to the current schema\n" );
273 $dbw->update( 'oldimage',
275 'oi_size' => $this->size
, // sanity
276 'oi_width' => $this->width
,
277 'oi_height' => $this->height
,
278 'oi_bits' => $this->bits
,
279 'oi_media_type' => $this->media_type
,
280 'oi_major_mime' => $major,
281 'oi_minor_mime' => $minor,
282 'oi_metadata' => $this->metadata
,
283 'oi_sha1' => $this->sha1
,
285 'oi_name' => $this->getName(),
286 'oi_archive_name' => $this->archive_name
),
289 wfProfileOut( __METHOD__
);
293 * @param $field Integer: one of DELETED_* bitfield constants
294 * for file or revision rows
297 function isDeleted( $field ) {
300 return ( $this->deleted
& $field ) == $field;
304 * Returns bitfield value
307 function getVisibility() {
310 return (int)$this->deleted
;
314 * Determine if the current user is allowed to view a particular
315 * field of this image file, if it's marked as deleted.
317 * @param $field Integer
318 * @param $user User object to check, or null to use $wgUser
321 function userCan( $field, User
$user = null ) {
324 return Revision
::userCanBitfield( $this->deleted
, $field, $user );
328 * Upload a file directly into archive. Generally for Special:Import.
330 * @param string $srcPath File system path of the source file
331 * @param string $archiveName Full archive name of the file, in the form
332 * $timestamp!$filename, where $filename must match $this->getName()
334 * @param $timestamp string
335 * @param $comment string
338 * @return FileRepoStatus
340 function uploadOld( $srcPath, $archiveName, $timestamp, $comment, $user, $flags = 0 ) {
343 $dstRel = 'archive/' . $this->getHashPath() . $archiveName;
344 $status = $this->publishTo( $srcPath, $dstRel,
345 $flags & File
::DELETE_SOURCE ? FileRepo
::DELETE_SOURCE
: 0
348 if ( $status->isGood() ) {
349 if ( !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) ) {
350 $status->fatal( 'filenotfound', $srcPath );
360 * Record a file upload in the oldimage table, without adding log entries.
362 * @param string $srcPath File system path to the source file
363 * @param string $archiveName The archive name of the file
364 * @param $timestamp string
365 * @param string $comment Upload comment
366 * @param $user User User who did this upload
369 function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
370 $dbw = $this->repo
->getMasterDB();
371 $dbw->begin( __METHOD__
);
373 $dstPath = $this->repo
->getZonePath( 'public' ) . '/' . $this->getRel();
374 $props = $this->repo
->getFileProps( $dstPath );
375 if ( !$props['fileExists'] ) {
379 $dbw->insert( 'oldimage',
381 'oi_name' => $this->getName(),
382 'oi_archive_name' => $archiveName,
383 'oi_size' => $props['size'],
384 'oi_width' => intval( $props['width'] ),
385 'oi_height' => intval( $props['height'] ),
386 'oi_bits' => $props['bits'],
387 'oi_timestamp' => $dbw->timestamp( $timestamp ),
388 'oi_description' => $comment,
389 'oi_user' => $user->getId(),
390 'oi_user_text' => $user->getName(),
391 'oi_metadata' => $props['metadata'],
392 'oi_media_type' => $props['media_type'],
393 'oi_major_mime' => $props['major_mime'],
394 'oi_minor_mime' => $props['minor_mime'],
395 'oi_sha1' => $props['sha1'],
399 $dbw->commit( __METHOD__
);