Merge "(bug 43211) Remove unneeded noprint classes after CSS change."
[lhc/web/wiklou.git] / includes / filerepo / file / OldLocalFile.php
1 <?php
2 /**
3 * Old file in the oldimage table.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup FileAbstraction
22 */
23
24 /**
25 * Class to represent a file in the oldimage table
26 *
27 * @ingroup FileAbstraction
28 */
29 class OldLocalFile extends LocalFile {
30 var $requestedTime, $archive_name;
31
32 const CACHE_VERSION = 1;
33 const MAX_CACHE_ROWS = 20;
34
35 /**
36 * @param $title Title
37 * @param $repo FileRepo
38 * @param $time null
39 * @return OldLocalFile
40 * @throws MWException
41 */
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' );
46 }
47 return new self( $title, $repo, $time, null );
48 }
49
50 /**
51 * @param $title Title
52 * @param $repo FileRepo
53 * @param $archiveName
54 * @return OldLocalFile
55 */
56 static function newFromArchiveName( $title, $repo, $archiveName ) {
57 return new self( $title, $repo, null, $archiveName );
58 }
59
60 /**
61 * @param $row
62 * @param $repo FileRepo
63 * @return OldLocalFile
64 */
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_' );
69 return $file;
70 }
71
72 /**
73 * Create a OldLocalFile from a SHA-1 key
74 * Do not call this except from inside a repo class.
75 *
76 * @param $sha1 string base-36 SHA-1
77 * @param $repo LocalRepo
78 * @param string|bool $timestamp MW_timestamp (optional)
79 *
80 * @return bool|OldLocalFile
81 */
82 static function newFromKey( $sha1, $repo, $timestamp = false ) {
83 $dbr = $repo->getSlaveDB();
84
85 $conds = array( 'oi_sha1' => $sha1 );
86 if ( $timestamp ) {
87 $conds['oi_timestamp'] = $dbr->timestamp( $timestamp );
88 }
89
90 $row = $dbr->selectRow( 'oldimage', self::selectFields(), $conds, __METHOD__ );
91 if ( $row ) {
92 return self::newFromRow( $row, $repo );
93 } else {
94 return false;
95 }
96 }
97
98 /**
99 * Fields in the oldimage table
100 * @return array
101 */
102 static function selectFields() {
103 return array(
104 'oi_name',
105 'oi_archive_name',
106 'oi_size',
107 'oi_width',
108 'oi_height',
109 'oi_metadata',
110 'oi_bits',
111 'oi_media_type',
112 'oi_major_mime',
113 'oi_minor_mime',
114 'oi_description',
115 'oi_user',
116 'oi_user_text',
117 'oi_timestamp',
118 'oi_deleted',
119 'oi_sha1',
120 );
121 }
122
123 /**
124 * @param $title Title
125 * @param $repo FileRepo
126 * @param $time String: timestamp or null to load by archive name
127 * @param $archiveName String: archive name or null to load by timestamp
128 * @throws MWException
129 */
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' );
136 }
137 }
138
139 /**
140 * @return bool
141 */
142 function getCacheKey() {
143 return false;
144 }
145
146 /**
147 * @return String
148 */
149 function getArchiveName() {
150 if ( !isset( $this->archive_name ) ) {
151 $this->load();
152 }
153 return $this->archive_name;
154 }
155
156 /**
157 * @return bool
158 */
159 function isOld() {
160 return true;
161 }
162
163 /**
164 * @return bool
165 */
166 function isVisible() {
167 return $this->exists() && !$this->isDeleted( File::DELETED_FILE );
168 }
169
170 function loadFromDB() {
171 wfProfileIn( __METHOD__ );
172
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;
178 } else {
179 $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime );
180 }
181 $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
182 $conds, __METHOD__, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
183 if ( $row ) {
184 $this->loadFromRow( $row, 'oi_' );
185 } else {
186 $this->fileExists = false;
187 }
188
189 wfProfileOut( __METHOD__ );
190 }
191
192 /**
193 * Load lazy file metadata from the DB
194 */
195 protected function loadExtraFromDB() {
196 wfProfileIn( __METHOD__ );
197
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;
203 } else {
204 $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime );
205 }
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' ) );
209
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' ) );
214 }
215
216 if ( $row ) {
217 foreach ( $this->unprefixRow( $row, 'oi_' ) as $name => $value ) {
218 $this->$name = $value;
219 }
220 } else {
221 throw new MWException( "Could not find data for image '{$this->archive_name}'." );
222 }
223
224 wfProfileOut( __METHOD__ );
225 }
226
227 /**
228 * @param $prefix string
229 * @return array
230 */
231 function getCacheFields( $prefix = 'img_' ) {
232 $fields = parent::getCacheFields( $prefix );
233 $fields[] = $prefix . 'archive_name';
234 $fields[] = $prefix . 'deleted';
235 return $fields;
236 }
237
238 /**
239 * @return string
240 */
241 function getRel() {
242 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
243 }
244
245 /**
246 * @return string
247 */
248 function getUrlRel() {
249 return 'archive/' . $this->getHashPath() . rawurlencode( $this->getArchiveName() );
250 }
251
252 function upgradeRow() {
253 wfProfileIn( __METHOD__ );
254 $this->loadFromFile();
255
256 # Don't destroy file info of missing files
257 if ( !$this->fileExists ) {
258 wfDebug( __METHOD__ . ": file does not exist, aborting\n" );
259 wfProfileOut( __METHOD__ );
260 return;
261 }
262
263 $dbw = $this->repo->getMasterDB();
264 list( $major, $minor ) = self::splitMime( $this->mime );
265
266 wfDebug( __METHOD__ . ': upgrading ' . $this->archive_name . " to the current schema\n" );
267 $dbw->update( 'oldimage',
268 array(
269 'oi_size' => $this->size, // sanity
270 'oi_width' => $this->width,
271 'oi_height' => $this->height,
272 'oi_bits' => $this->bits,
273 'oi_media_type' => $this->media_type,
274 'oi_major_mime' => $major,
275 'oi_minor_mime' => $minor,
276 'oi_metadata' => $this->metadata,
277 'oi_sha1' => $this->sha1,
278 ), array(
279 'oi_name' => $this->getName(),
280 'oi_archive_name' => $this->archive_name ),
281 __METHOD__
282 );
283 wfProfileOut( __METHOD__ );
284 }
285
286 /**
287 * @param $field Integer: one of DELETED_* bitfield constants
288 * for file or revision rows
289 * @return bool
290 */
291 function isDeleted( $field ) {
292 $this->load();
293 return ($this->deleted & $field) == $field;
294 }
295
296 /**
297 * Returns bitfield value
298 * @return int
299 */
300 function getVisibility() {
301 $this->load();
302 return (int)$this->deleted;
303 }
304
305 /**
306 * Determine if the current user is allowed to view a particular
307 * field of this image file, if it's marked as deleted.
308 *
309 * @param $field Integer
310 * @param $user User object to check, or null to use $wgUser
311 * @return bool
312 */
313 function userCan( $field, User $user = null ) {
314 $this->load();
315 return Revision::userCanBitfield( $this->deleted, $field, $user );
316 }
317
318 /**
319 * Upload a file directly into archive. Generally for Special:Import.
320 *
321 * @param $srcPath string File system path of the source file
322 * @param $archiveName string Full archive name of the file, in the form
323 * $timestamp!$filename, where $filename must match $this->getName()
324 *
325 * @param $timestamp string
326 * @param $comment string
327 * @param $user
328 * @param $flags int
329 * @return FileRepoStatus
330 */
331 function uploadOld( $srcPath, $archiveName, $timestamp, $comment, $user, $flags = 0 ) {
332 $this->lock();
333
334 $dstRel = 'archive/' . $this->getHashPath() . $archiveName;
335 $status = $this->publishTo( $srcPath, $dstRel,
336 $flags & File::DELETE_SOURCE ? FileRepo::DELETE_SOURCE : 0
337 );
338
339 if ( $status->isGood() ) {
340 if ( !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) ) {
341 $status->fatal( 'filenotfound', $srcPath );
342 }
343 }
344
345 $this->unlock();
346
347 return $status;
348 }
349
350 /**
351 * Record a file upload in the oldimage table, without adding log entries.
352 *
353 * @param $srcPath string File system path to the source file
354 * @param $archiveName string The archive name of the file
355 * @param $timestamp string
356 * @param $comment string Upload comment
357 * @param $user User User who did this upload
358 * @return bool
359 */
360 function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
361 $dbw = $this->repo->getMasterDB();
362 $dbw->begin( __METHOD__ );
363
364 $dstPath = $this->repo->getZonePath( 'public' ) . '/' . $this->getRel();
365 $props = $this->repo->getFileProps( $dstPath );
366 if ( !$props['fileExists'] ) {
367 return false;
368 }
369
370 $dbw->insert( 'oldimage',
371 array(
372 'oi_name' => $this->getName(),
373 'oi_archive_name' => $archiveName,
374 'oi_size' => $props['size'],
375 'oi_width' => intval( $props['width'] ),
376 'oi_height' => intval( $props['height'] ),
377 'oi_bits' => $props['bits'],
378 'oi_timestamp' => $dbw->timestamp( $timestamp ),
379 'oi_description' => $comment,
380 'oi_user' => $user->getId(),
381 'oi_user_text' => $user->getName(),
382 'oi_metadata' => $props['metadata'],
383 'oi_media_type' => $props['media_type'],
384 'oi_major_mime' => $props['major_mime'],
385 'oi_minor_mime' => $props['minor_mime'],
386 'oi_sha1' => $props['sha1'],
387 ), __METHOD__
388 );
389
390 $dbw->commit( __METHOD__ );
391
392 return true;
393 }
394
395 }