enhance filerepo doc structure
[lhc/web/wiklou.git] / includes / filerepo / file / OldLocalFile.php
1 <?php
2 /**
3 * Old file in the oldimage table
4 *
5 * @file
6 * @ingroup FileAbstraction
7 */
8
9 /**
10 * Class to represent a file in the oldimage table
11 *
12 * @ingroup FileAbstraction
13 */
14 class OldLocalFile extends LocalFile {
15 var $requestedTime, $archive_name;
16
17 const CACHE_VERSION = 1;
18 const MAX_CACHE_ROWS = 20;
19
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' );
24 }
25 return new self( $title, $repo, $time, null );
26 }
27
28 static function newFromArchiveName( $title, $repo, $archiveName ) {
29 return new self( $title, $repo, null, $archiveName );
30 }
31
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_' );
36 return $file;
37 }
38
39 /**
40 * Create a OldLocalFile from a SHA-1 key
41 * Do not call this except from inside a repo class.
42 *
43 * @param $sha1 string base-36 SHA-1
44 * @param $repo LocalRepo
45 * @param string|bool $timestamp MW_timestamp (optional)
46 *
47 * @return bool|OldLocalFile
48 */
49 static function newFromKey( $sha1, $repo, $timestamp = false ) {
50 $dbr = $repo->getSlaveDB();
51
52 $conds = array( 'oi_sha1' => $sha1 );
53 if ( $timestamp ) {
54 $conds['oi_timestamp'] = $dbr->timestamp( $timestamp );
55 }
56
57 $row = $dbr->selectRow( 'oldimage', self::selectFields(), $conds, __METHOD__ );
58 if ( $row ) {
59 return self::newFromRow( $row, $repo );
60 } else {
61 return false;
62 }
63 }
64
65 /**
66 * Fields in the oldimage table
67 */
68 static function selectFields() {
69 return array(
70 'oi_name',
71 'oi_archive_name',
72 'oi_size',
73 'oi_width',
74 'oi_height',
75 'oi_metadata',
76 'oi_bits',
77 'oi_media_type',
78 'oi_major_mime',
79 'oi_minor_mime',
80 'oi_description',
81 'oi_user',
82 'oi_user_text',
83 'oi_timestamp',
84 'oi_deleted',
85 'oi_sha1',
86 );
87 }
88
89 /**
90 * @param $title Title
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
94 */
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' );
101 }
102 }
103
104 function getCacheKey() {
105 return false;
106 }
107
108 function getArchiveName() {
109 if ( !isset( $this->archive_name ) ) {
110 $this->load();
111 }
112 return $this->archive_name;
113 }
114
115 function isOld() {
116 return true;
117 }
118
119 function isVisible() {
120 return $this->exists() && !$this->isDeleted(File::DELETED_FILE);
121 }
122
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;
130 } else {
131 $conds[] = 'oi_timestamp = ' . $dbr->addQuotes( $dbr->timestamp( $this->requestedTime ) );
132 }
133 $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
134 $conds, __METHOD__, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
135 if ( $row ) {
136 $this->loadFromRow( $row, 'oi_' );
137 } else {
138 $this->fileExists = false;
139 }
140 wfProfileOut( __METHOD__ );
141 }
142
143 function getCacheFields( $prefix = 'img_' ) {
144 $fields = parent::getCacheFields( $prefix );
145 $fields[] = $prefix . 'archive_name';
146 $fields[] = $prefix . 'deleted';
147 return $fields;
148 }
149
150 function getRel() {
151 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
152 }
153
154 function getUrlRel() {
155 return 'archive/' . $this->getHashPath() . rawurlencode( $this->getArchiveName() );
156 }
157
158 function upgradeRow() {
159 wfProfileIn( __METHOD__ );
160 $this->loadFromFile();
161
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__ );
166 return;
167 }
168
169 $dbw = $this->repo->getMasterDB();
170 list( $major, $minor ) = self::splitMime( $this->mime );
171
172 wfDebug(__METHOD__.': upgrading '.$this->archive_name." to the current schema\n");
173 $dbw->update( 'oldimage',
174 array(
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,
183 ), array(
184 'oi_name' => $this->getName(),
185 'oi_archive_name' => $this->archive_name ),
186 __METHOD__
187 );
188 wfProfileOut( __METHOD__ );
189 }
190
191 /**
192 * @param $field Integer: one of DELETED_* bitfield constants
193 * for file or revision rows
194 * @return bool
195 */
196 function isDeleted( $field ) {
197 $this->load();
198 return ($this->deleted & $field) == $field;
199 }
200
201 /**
202 * Returns bitfield value
203 * @return int
204 */
205 function getVisibility() {
206 $this->load();
207 return (int)$this->deleted;
208 }
209
210 /**
211 * Determine if the current user is allowed to view a particular
212 * field of this image file, if it's marked as deleted.
213 *
214 * @param $field Integer
215 * @param $user User object to check, or null to use $wgUser
216 * @return bool
217 */
218 function userCan( $field, User $user = null ) {
219 $this->load();
220 return Revision::userCanBitfield( $this->deleted, $field, $user );
221 }
222
223 /**
224 * Upload a file directly into archive. Generally for Special:Import.
225 *
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()
229 *
230 * @return FileRepoStatus
231 */
232 function uploadOld( $srcPath, $archiveName, $timestamp, $comment, $user, $flags = 0 ) {
233 $this->lock();
234
235 $dstRel = 'archive/' . $this->getHashPath() . $archiveName;
236 $status = $this->publishTo( $srcPath, $dstRel,
237 $flags & File::DELETE_SOURCE ? FileRepo::DELETE_SOURCE : 0
238 );
239
240 if ( $status->isGood() ) {
241 if ( !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) ) {
242 $status->fatal( 'filenotfound', $srcPath );
243 }
244 }
245
246 $this->unlock();
247
248 return $status;
249 }
250
251 /**
252 * Record a file upload in the oldimage table, without adding log entries.
253 *
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
258 * @return bool
259 */
260 function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
261 $dbw = $this->repo->getMasterDB();
262 $dbw->begin();
263
264 $dstPath = $this->repo->getZonePath( 'public' ) . '/' . $this->getRel();
265 $props = $this->repo->getFileProps( $dstPath );
266 if ( !$props['fileExists'] ) {
267 return false;
268 }
269
270 $dbw->insert( 'oldimage',
271 array(
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'],
287 ), __METHOD__
288 );
289
290 $dbw->commit();
291
292 return true;
293 }
294
295 }