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;
35 static function newFromTitle( $title, $repo, $time = null ) {
36 # The null default value is only here to avoid an E_STRICT
37 if ( $time === null ) {
38 throw new MWException( __METHOD__
.' got null for $time parameter' );
40 return new self( $title, $repo, $time, null );
43 static function newFromArchiveName( $title, $repo, $archiveName ) {
44 return new self( $title, $repo, null, $archiveName );
47 static function newFromRow( $row, $repo ) {
48 $title = Title
::makeTitle( NS_FILE
, $row->oi_name
);
49 $file = new self( $title, $repo, null, $row->oi_archive_name
);
50 $file->loadFromRow( $row, 'oi_' );
55 * Create a OldLocalFile from a SHA-1 key
56 * Do not call this except from inside a repo class.
58 * @param $sha1 string base-36 SHA-1
59 * @param $repo LocalRepo
60 * @param string|bool $timestamp MW_timestamp (optional)
62 * @return bool|OldLocalFile
64 static function newFromKey( $sha1, $repo, $timestamp = false ) {
65 $dbr = $repo->getSlaveDB();
67 $conds = array( 'oi_sha1' => $sha1 );
69 $conds['oi_timestamp'] = $dbr->timestamp( $timestamp );
72 $row = $dbr->selectRow( 'oldimage', self
::selectFields(), $conds, __METHOD__
);
74 return self
::newFromRow( $row, $repo );
81 * Fields in the oldimage table
84 static function selectFields() {
106 * @param $title Title
107 * @param $repo FileRepo
108 * @param $time String: timestamp or null to load by archive name
109 * @param $archiveName String: archive name or null to load by timestamp
111 function __construct( $title, $repo, $time, $archiveName ) {
112 parent
::__construct( $title, $repo );
113 $this->requestedTime
= $time;
114 $this->archive_name
= $archiveName;
115 if ( is_null( $time ) && is_null( $archiveName ) ) {
116 throw new MWException( __METHOD__
.': must specify at least one of $time or $archiveName' );
120 function getCacheKey() {
124 function getArchiveName() {
125 if ( !isset( $this->archive_name
) ) {
128 return $this->archive_name
;
135 function isVisible() {
136 return $this->exists() && !$this->isDeleted(File
::DELETED_FILE
);
139 function loadFromDB() {
140 wfProfileIn( __METHOD__
);
141 $this->dataLoaded
= true;
142 $dbr = $this->repo
->getSlaveDB();
143 $conds = array( 'oi_name' => $this->getName() );
144 if ( is_null( $this->requestedTime
) ) {
145 $conds['oi_archive_name'] = $this->archive_name
;
147 $conds[] = 'oi_timestamp = ' . $dbr->addQuotes( $dbr->timestamp( $this->requestedTime
) );
149 $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
150 $conds, __METHOD__
, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
152 $this->loadFromRow( $row, 'oi_' );
154 $this->fileExists
= false;
156 wfProfileOut( __METHOD__
);
159 function getCacheFields( $prefix = 'img_' ) {
160 $fields = parent
::getCacheFields( $prefix );
161 $fields[] = $prefix . 'archive_name';
162 $fields[] = $prefix . 'deleted';
167 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
170 function getUrlRel() {
171 return 'archive/' . $this->getHashPath() . rawurlencode( $this->getArchiveName() );
174 function upgradeRow() {
175 wfProfileIn( __METHOD__
);
176 $this->loadFromFile();
178 # Don't destroy file info of missing files
179 if ( !$this->fileExists
) {
180 wfDebug( __METHOD__
.": file does not exist, aborting\n" );
181 wfProfileOut( __METHOD__
);
185 $dbw = $this->repo
->getMasterDB();
186 list( $major, $minor ) = self
::splitMime( $this->mime
);
188 wfDebug(__METHOD__
.': upgrading '.$this->archive_name
." to the current schema\n");
189 $dbw->update( 'oldimage',
191 'oi_width' => $this->width
,
192 'oi_height' => $this->height
,
193 'oi_bits' => $this->bits
,
194 'oi_media_type' => $this->media_type
,
195 'oi_major_mime' => $major,
196 'oi_minor_mime' => $minor,
197 'oi_metadata' => $this->metadata
,
198 'oi_sha1' => $this->sha1
,
200 'oi_name' => $this->getName(),
201 'oi_archive_name' => $this->archive_name
),
204 wfProfileOut( __METHOD__
);
208 * @param $field Integer: one of DELETED_* bitfield constants
209 * for file or revision rows
212 function isDeleted( $field ) {
214 return ($this->deleted
& $field) == $field;
218 * Returns bitfield value
221 function getVisibility() {
223 return (int)$this->deleted
;
227 * Determine if the current user is allowed to view a particular
228 * field of this image file, if it's marked as deleted.
230 * @param $field Integer
231 * @param $user User object to check, or null to use $wgUser
234 function userCan( $field, User
$user = null ) {
236 return Revision
::userCanBitfield( $this->deleted
, $field, $user );
240 * Upload a file directly into archive. Generally for Special:Import.
242 * @param $srcPath string File system path of the source file
243 * @param $archiveName string Full archive name of the file, in the form
244 * $timestamp!$filename, where $filename must match $this->getName()
246 * @return FileRepoStatus
248 function uploadOld( $srcPath, $archiveName, $timestamp, $comment, $user, $flags = 0 ) {
251 $dstRel = 'archive/' . $this->getHashPath() . $archiveName;
252 $status = $this->publishTo( $srcPath, $dstRel,
253 $flags & File
::DELETE_SOURCE ? FileRepo
::DELETE_SOURCE
: 0
256 if ( $status->isGood() ) {
257 if ( !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) ) {
258 $status->fatal( 'filenotfound', $srcPath );
268 * Record a file upload in the oldimage table, without adding log entries.
270 * @param $srcPath string File system path to the source file
271 * @param $archiveName string The archive name of the file
272 * @param $comment string Upload comment
273 * @param $user User User who did this upload
276 function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
277 $dbw = $this->repo
->getMasterDB();
278 $dbw->begin( __METHOD__
);
280 $dstPath = $this->repo
->getZonePath( 'public' ) . '/' . $this->getRel();
281 $props = $this->repo
->getFileProps( $dstPath );
282 if ( !$props['fileExists'] ) {
286 $dbw->insert( 'oldimage',
288 'oi_name' => $this->getName(),
289 'oi_archive_name' => $archiveName,
290 'oi_size' => $props['size'],
291 'oi_width' => intval( $props['width'] ),
292 'oi_height' => intval( $props['height'] ),
293 'oi_bits' => $props['bits'],
294 'oi_timestamp' => $dbw->timestamp( $timestamp ),
295 'oi_description' => $comment,
296 'oi_user' => $user->getId(),
297 'oi_user_text' => $user->getName(),
298 'oi_metadata' => $props['metadata'],
299 'oi_media_type' => $props['media_type'],
300 'oi_major_mime' => $props['major_mime'],
301 'oi_minor_mime' => $props['minor_mime'],
302 'oi_sha1' => $props['sha1'],
306 $dbw->commit( __METHOD__
);