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' );
47 return new self( $title, $repo, $time, null );
52 * @param $repo FileRepo
54 * @return OldLocalFile
56 static function newFromArchiveName( $title, $repo, $archiveName ) {
57 return new self( $title, $repo, null, $archiveName );
62 * @param $repo FileRepo
63 * @return OldLocalFile
65 static function newFromRow( $row, $repo ) {
66 $title = Title
::makeTitle( NS_FILE
, $row->oi_name
);
67 $file = new self( $title, $repo, null, $row->oi_archive_name
);
68 $file->loadFromRow( $row, 'oi_' );
73 * Create a OldLocalFile from a SHA-1 key
74 * Do not call this except from inside a repo class.
76 * @param string $sha1 base-36 SHA-1
77 * @param $repo LocalRepo
78 * @param string|bool $timestamp MW_timestamp (optional)
80 * @return bool|OldLocalFile
82 static function newFromKey( $sha1, $repo, $timestamp = false ) {
83 $dbr = $repo->getSlaveDB();
85 $conds = array( 'oi_sha1' => $sha1 );
87 $conds['oi_timestamp'] = $dbr->timestamp( $timestamp );
90 $row = $dbr->selectRow( 'oldimage', self
::selectFields(), $conds, __METHOD__
);
92 return self
::newFromRow( $row, $repo );
99 * Fields in the oldimage table
102 static function selectFields() {
124 * @param $title Title
125 * @param $repo FileRepo
126 * @param string $time timestamp or null to load by archive name
127 * @param string $archiveName archive name or null to load by timestamp
128 * @throws MWException
130 function __construct( $title, $repo, $time, $archiveName ) {
131 parent
::__construct( $title, $repo );
132 $this->requestedTime
= $time;
133 $this->archive_name
= $archiveName;
134 if ( is_null( $time ) && is_null( $archiveName ) ) {
135 throw new MWException( __METHOD__
. ': must specify at least one of $time or $archiveName' );
142 function getCacheKey() {
149 function getArchiveName() {
150 if ( !isset( $this->archive_name
) ) {
153 return $this->archive_name
;
166 function isVisible() {
167 return $this->exists() && !$this->isDeleted( File
::DELETED_FILE
);
170 function loadFromDB() {
171 wfProfileIn( __METHOD__
);
173 $this->dataLoaded
= true;
174 $dbr = $this->repo
->getSlaveDB();
175 $conds = array( 'oi_name' => $this->getName() );
176 if ( is_null( $this->requestedTime
) ) {
177 $conds['oi_archive_name'] = $this->archive_name
;
179 $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime
);
181 $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
182 $conds, __METHOD__
, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
184 $this->loadFromRow( $row, 'oi_' );
186 $this->fileExists
= false;
189 wfProfileOut( __METHOD__
);
193 * Load lazy file metadata from the DB
195 protected function loadExtraFromDB() {
196 wfProfileIn( __METHOD__
);
198 $this->extraDataLoaded
= true;
199 $dbr = $this->repo
->getSlaveDB();
200 $conds = array( 'oi_name' => $this->getName() );
201 if ( is_null( $this->requestedTime
) ) {
202 $conds['oi_archive_name'] = $this->archive_name
;
204 $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime
);
206 // In theory the file could have just been renamed/deleted...oh well
207 $row = $dbr->selectRow( 'oldimage', $this->getLazyCacheFields( 'oi_' ),
208 $conds, __METHOD__
, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
210 if ( !$row ) { // fallback to master
211 $dbr = $this->repo
->getMasterDB();
212 $row = $dbr->selectRow( 'oldimage', $this->getLazyCacheFields( 'oi_' ),
213 $conds, __METHOD__
, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
217 foreach ( $this->unprefixRow( $row, 'oi_' ) as $name => $value ) {
218 $this->$name = $value;
221 wfProfileOut( __METHOD__
);
222 throw new MWException( "Could not find data for image '{$this->archive_name}'." );
225 wfProfileOut( __METHOD__
);
229 * @param $prefix string
232 function getCacheFields( $prefix = 'img_' ) {
233 $fields = parent
::getCacheFields( $prefix );
234 $fields[] = $prefix . 'archive_name';
235 $fields[] = $prefix . 'deleted';
243 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
249 function getUrlRel() {
250 return 'archive/' . $this->getHashPath() . rawurlencode( $this->getArchiveName() );
253 function upgradeRow() {
254 wfProfileIn( __METHOD__
);
255 $this->loadFromFile();
257 # Don't destroy file info of missing files
258 if ( !$this->fileExists
) {
259 wfDebug( __METHOD__
. ": file does not exist, aborting\n" );
260 wfProfileOut( __METHOD__
);
264 $dbw = $this->repo
->getMasterDB();
265 list( $major, $minor ) = self
::splitMime( $this->mime
);
267 wfDebug( __METHOD__
. ': upgrading ' . $this->archive_name
. " to the current schema\n" );
268 $dbw->update( 'oldimage',
270 'oi_size' => $this->size
, // sanity
271 'oi_width' => $this->width
,
272 'oi_height' => $this->height
,
273 'oi_bits' => $this->bits
,
274 'oi_media_type' => $this->media_type
,
275 'oi_major_mime' => $major,
276 'oi_minor_mime' => $minor,
277 'oi_metadata' => $this->metadata
,
278 'oi_sha1' => $this->sha1
,
280 'oi_name' => $this->getName(),
281 'oi_archive_name' => $this->archive_name
),
284 wfProfileOut( __METHOD__
);
288 * @param $field Integer: one of DELETED_* bitfield constants
289 * for file or revision rows
292 function isDeleted( $field ) {
294 return ( $this->deleted
& $field ) == $field;
298 * Returns bitfield value
301 function getVisibility() {
303 return (int)$this->deleted
;
307 * Determine if the current user is allowed to view a particular
308 * field of this image file, if it's marked as deleted.
310 * @param $field Integer
311 * @param $user User object to check, or null to use $wgUser
314 function userCan( $field, User
$user = null ) {
316 return Revision
::userCanBitfield( $this->deleted
, $field, $user );
320 * Upload a file directly into archive. Generally for Special:Import.
322 * @param string $srcPath File system path of the source file
323 * @param string $archiveName Full archive name of the file, in the form
324 * $timestamp!$filename, where $filename must match $this->getName()
326 * @param $timestamp string
327 * @param $comment string
330 * @return FileRepoStatus
332 function uploadOld( $srcPath, $archiveName, $timestamp, $comment, $user, $flags = 0 ) {
335 $dstRel = 'archive/' . $this->getHashPath() . $archiveName;
336 $status = $this->publishTo( $srcPath, $dstRel,
337 $flags & File
::DELETE_SOURCE ? FileRepo
::DELETE_SOURCE
: 0
340 if ( $status->isGood() ) {
341 if ( !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) ) {
342 $status->fatal( 'filenotfound', $srcPath );
352 * Record a file upload in the oldimage table, without adding log entries.
354 * @param string $srcPath File system path to the source file
355 * @param string $archiveName The archive name of the file
356 * @param $timestamp string
357 * @param string $comment Upload comment
358 * @param $user User User who did this upload
361 function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
362 $dbw = $this->repo
->getMasterDB();
363 $dbw->begin( __METHOD__
);
365 $dstPath = $this->repo
->getZonePath( 'public' ) . '/' . $this->getRel();
366 $props = $this->repo
->getFileProps( $dstPath );
367 if ( !$props['fileExists'] ) {
371 $dbw->insert( 'oldimage',
373 'oi_name' => $this->getName(),
374 'oi_archive_name' => $archiveName,
375 'oi_size' => $props['size'],
376 'oi_width' => intval( $props['width'] ),
377 'oi_height' => intval( $props['height'] ),
378 'oi_bits' => $props['bits'],
379 'oi_timestamp' => $dbw->timestamp( $timestamp ),
380 'oi_description' => $comment,
381 'oi_user' => $user->getId(),
382 'oi_user_text' => $user->getName(),
383 'oi_metadata' => $props['metadata'],
384 'oi_media_type' => $props['media_type'],
385 'oi_major_mime' => $props['major_mime'],
386 'oi_minor_mime' => $props['minor_mime'],
387 'oi_sha1' => $props['sha1'],
391 $dbw->commit( __METHOD__
);