Merge "resources: Remove the deprecated 'jquery.badge' module"
[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 string Timestamp */
31 protected $requestedTime;
32
33 /** @var string Archive name */
34 protected $archive_name;
35
36 const CACHE_VERSION = 1;
37 const MAX_CACHE_ROWS = 20;
38
39 /**
40 * @param Title $title
41 * @param FileRepo $repo
42 * @param null|int $time Timestamp or null
43 * @return OldLocalFile
44 * @throws MWException
45 */
46 static function newFromTitle( $title, $repo, $time = null ) {
47 # The null default value is only here to avoid an E_STRICT
48 if ( $time === null ) {
49 throw new MWException( __METHOD__ . ' got null for $time parameter' );
50 }
51
52 return new self( $title, $repo, $time, null );
53 }
54
55 /**
56 * @param Title $title
57 * @param FileRepo $repo
58 * @param string $archiveName
59 * @return OldLocalFile
60 */
61 static function newFromArchiveName( $title, $repo, $archiveName ) {
62 return new self( $title, $repo, null, $archiveName );
63 }
64
65 /**
66 * @param stdClass $row
67 * @param FileRepo $repo
68 * @return OldLocalFile
69 */
70 static function newFromRow( $row, $repo ) {
71 $title = Title::makeTitle( NS_FILE, $row->oi_name );
72 $file = new self( $title, $repo, null, $row->oi_archive_name );
73 $file->loadFromRow( $row, 'oi_' );
74
75 return $file;
76 }
77
78 /**
79 * Create a OldLocalFile from a SHA-1 key
80 * Do not call this except from inside a repo class.
81 *
82 * @param string $sha1 Base-36 SHA-1
83 * @param LocalRepo $repo
84 * @param string|bool $timestamp MW_timestamp (optional)
85 *
86 * @return bool|OldLocalFile
87 */
88 static function newFromKey( $sha1, $repo, $timestamp = false ) {
89 $dbr = $repo->getReplicaDB();
90
91 $conds = [ 'oi_sha1' => $sha1 ];
92 if ( $timestamp ) {
93 $conds['oi_timestamp'] = $dbr->timestamp( $timestamp );
94 }
95
96 $fileQuery = self::getQueryInfo();
97 $row = $dbr->selectRow(
98 $fileQuery['tables'], $fileQuery['fields'], $conds, __METHOD__, [], $fileQuery['joins']
99 );
100 if ( $row ) {
101 return self::newFromRow( $row, $repo );
102 } else {
103 return false;
104 }
105 }
106
107 /**
108 * Fields in the oldimage table
109 * @deprecated since 1.31, use self::getQueryInfo() instead.
110 * @return array
111 */
112 static function selectFields() {
113 return [
114 'oi_name',
115 'oi_archive_name',
116 'oi_size',
117 'oi_width',
118 'oi_height',
119 'oi_metadata',
120 'oi_bits',
121 'oi_media_type',
122 'oi_major_mime',
123 'oi_minor_mime',
124 'oi_user',
125 'oi_user_text',
126 'oi_timestamp',
127 'oi_deleted',
128 'oi_sha1',
129 ] + CommentStore::newKey( 'oi_description' )->getFields();
130 }
131
132 /**
133 * Return the tables, fields, and join conditions to be selected to create
134 * a new oldlocalfile object.
135 * @since 1.31
136 * @param string[] $options
137 * - omit-lazy: Omit fields that are lazily cached.
138 * @return array With three keys:
139 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
140 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
141 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
142 */
143 public static function getQueryInfo( array $options = [] ) {
144 $commentQuery = CommentStore::newKey( 'oi_description' )->getJoin();
145 $ret = [
146 'tables' => [ 'oldimage' ] + $commentQuery['tables'],
147 'fields' => [
148 'oi_name',
149 'oi_archive_name',
150 'oi_size',
151 'oi_width',
152 'oi_height',
153 'oi_bits',
154 'oi_media_type',
155 'oi_major_mime',
156 'oi_minor_mime',
157 'oi_user',
158 'oi_user_text',
159 'oi_timestamp',
160 'oi_deleted',
161 'oi_sha1',
162 ] + $commentQuery['fields'],
163 'joins' => $commentQuery['joins'],
164 ];
165
166 if ( in_array( 'omit-nonlazy', $options, true ) ) {
167 // Internal use only for getting only the lazy fields
168 $ret['fields'] = [];
169 }
170 if ( !in_array( 'omit-lazy', $options, true ) ) {
171 // Note: Keep this in sync with self::getLazyCacheFields()
172 $ret['fields'][] = 'oi_metadata';
173 }
174
175 return $ret;
176 }
177
178 /**
179 * @param Title $title
180 * @param FileRepo $repo
181 * @param string $time Timestamp or null to load by archive name
182 * @param string $archiveName Archive name or null to load by timestamp
183 * @throws MWException
184 */
185 function __construct( $title, $repo, $time, $archiveName ) {
186 parent::__construct( $title, $repo );
187 $this->requestedTime = $time;
188 $this->archive_name = $archiveName;
189 if ( is_null( $time ) && is_null( $archiveName ) ) {
190 throw new MWException( __METHOD__ . ': must specify at least one of $time or $archiveName' );
191 }
192 }
193
194 /**
195 * @return bool
196 */
197 function getCacheKey() {
198 return false;
199 }
200
201 /**
202 * @return string
203 */
204 function getArchiveName() {
205 if ( !isset( $this->archive_name ) ) {
206 $this->load();
207 }
208
209 return $this->archive_name;
210 }
211
212 /**
213 * @return bool
214 */
215 function isOld() {
216 return true;
217 }
218
219 /**
220 * @return bool
221 */
222 function isVisible() {
223 return $this->exists() && !$this->isDeleted( File::DELETED_FILE );
224 }
225
226 function loadFromDB( $flags = 0 ) {
227 $this->dataLoaded = true;
228
229 $dbr = ( $flags & self::READ_LATEST )
230 ? $this->repo->getMasterDB()
231 : $this->repo->getReplicaDB();
232
233 $conds = [ 'oi_name' => $this->getName() ];
234 if ( is_null( $this->requestedTime ) ) {
235 $conds['oi_archive_name'] = $this->archive_name;
236 } else {
237 $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime );
238 }
239 $fileQuery = static::getQueryInfo();
240 $row = $dbr->selectRow(
241 $fileQuery['tables'],
242 $fileQuery['fields'],
243 $conds,
244 __METHOD__,
245 [ 'ORDER BY' => 'oi_timestamp DESC' ],
246 $fileQuery['joins']
247 );
248 if ( $row ) {
249 $this->loadFromRow( $row, 'oi_' );
250 } else {
251 $this->fileExists = false;
252 }
253 }
254
255 /**
256 * Load lazy file metadata from the DB
257 */
258 protected function loadExtraFromDB() {
259 $this->extraDataLoaded = true;
260 $dbr = $this->repo->getReplicaDB();
261 $conds = [ 'oi_name' => $this->getName() ];
262 if ( is_null( $this->requestedTime ) ) {
263 $conds['oi_archive_name'] = $this->archive_name;
264 } else {
265 $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime );
266 }
267 $fileQuery = static::getQueryInfo( [ 'omit-nonlazy' ] );
268 // In theory the file could have just been renamed/deleted...oh well
269 $row = $dbr->selectRow(
270 $fileQuery['tables'],
271 $fileQuery['fields'],
272 $conds,
273 __METHOD__,
274 [ 'ORDER BY' => 'oi_timestamp DESC' ],
275 $fileQuery['joins']
276 );
277
278 if ( !$row ) { // fallback to master
279 $dbr = $this->repo->getMasterDB();
280 $row = $dbr->selectRow(
281 $fileQuery['tables'],
282 $fileQuery['fields'],
283 $conds,
284 __METHOD__,
285 [ 'ORDER BY' => 'oi_timestamp DESC' ],
286 $fileQuery['joins']
287 );
288 }
289
290 if ( $row ) {
291 foreach ( $this->unprefixRow( $row, 'oi_' ) as $name => $value ) {
292 $this->$name = $value;
293 }
294 } else {
295 throw new MWException( "Could not find data for image '{$this->archive_name}'." );
296 }
297 }
298
299 /** @inheritDoc */
300 protected function getCacheFields( $prefix = 'img_' ) {
301 $fields = parent::getCacheFields( $prefix );
302 $fields[] = $prefix . 'archive_name';
303 $fields[] = $prefix . 'deleted';
304
305 return $fields;
306 }
307
308 /**
309 * @return string
310 */
311 function getRel() {
312 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
313 }
314
315 /**
316 * @return string
317 */
318 function getUrlRel() {
319 return 'archive/' . $this->getHashPath() . rawurlencode( $this->getArchiveName() );
320 }
321
322 function upgradeRow() {
323 $this->loadFromFile();
324
325 # Don't destroy file info of missing files
326 if ( !$this->fileExists ) {
327 wfDebug( __METHOD__ . ": file does not exist, aborting\n" );
328
329 return;
330 }
331
332 $dbw = $this->repo->getMasterDB();
333 list( $major, $minor ) = self::splitMime( $this->mime );
334
335 wfDebug( __METHOD__ . ': upgrading ' . $this->archive_name . " to the current schema\n" );
336 $dbw->update( 'oldimage',
337 [
338 'oi_size' => $this->size, // sanity
339 'oi_width' => $this->width,
340 'oi_height' => $this->height,
341 'oi_bits' => $this->bits,
342 'oi_media_type' => $this->media_type,
343 'oi_major_mime' => $major,
344 'oi_minor_mime' => $minor,
345 'oi_metadata' => $this->metadata,
346 'oi_sha1' => $this->sha1,
347 ], [
348 'oi_name' => $this->getName(),
349 'oi_archive_name' => $this->archive_name ],
350 __METHOD__
351 );
352 }
353
354 /**
355 * @param int $field One of DELETED_* bitfield constants for file or
356 * revision rows
357 * @return bool
358 */
359 function isDeleted( $field ) {
360 $this->load();
361
362 return ( $this->deleted & $field ) == $field;
363 }
364
365 /**
366 * Returns bitfield value
367 * @return int
368 */
369 function getVisibility() {
370 $this->load();
371
372 return (int)$this->deleted;
373 }
374
375 /**
376 * Determine if the current user is allowed to view a particular
377 * field of this image file, if it's marked as deleted.
378 *
379 * @param int $field
380 * @param User|null $user User object to check, or null to use $wgUser
381 * @return bool
382 */
383 function userCan( $field, User $user = null ) {
384 $this->load();
385
386 return Revision::userCanBitfield( $this->deleted, $field, $user );
387 }
388
389 /**
390 * Upload a file directly into archive. Generally for Special:Import.
391 *
392 * @param string $srcPath File system path of the source file
393 * @param string $archiveName Full archive name of the file, in the form
394 * $timestamp!$filename, where $filename must match $this->getName()
395 * @param string $timestamp
396 * @param string $comment
397 * @param User $user
398 * @return Status
399 */
400 function uploadOld( $srcPath, $archiveName, $timestamp, $comment, $user ) {
401 $this->lock();
402
403 $dstRel = 'archive/' . $this->getHashPath() . $archiveName;
404 $status = $this->publishTo( $srcPath, $dstRel );
405
406 if ( $status->isGood() ) {
407 if ( !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) ) {
408 $status->fatal( 'filenotfound', $srcPath );
409 }
410 }
411
412 $this->unlock();
413
414 return $status;
415 }
416
417 /**
418 * Record a file upload in the oldimage table, without adding log entries.
419 *
420 * @param string $srcPath File system path to the source file
421 * @param string $archiveName The archive name of the file
422 * @param string $timestamp
423 * @param string $comment Upload comment
424 * @param User $user User who did this upload
425 * @return bool
426 */
427 protected function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
428 $dbw = $this->repo->getMasterDB();
429
430 $dstPath = $this->repo->getZonePath( 'public' ) . '/' . $this->getRel();
431 $props = $this->repo->getFileProps( $dstPath );
432 if ( !$props['fileExists'] ) {
433 return false;
434 }
435
436 $commentFields = CommentStore::newKey( 'oi_description' )->insert( $dbw, $comment );
437 $dbw->insert( 'oldimage',
438 [
439 'oi_name' => $this->getName(),
440 'oi_archive_name' => $archiveName,
441 'oi_size' => $props['size'],
442 'oi_width' => intval( $props['width'] ),
443 'oi_height' => intval( $props['height'] ),
444 'oi_bits' => $props['bits'],
445 'oi_timestamp' => $dbw->timestamp( $timestamp ),
446 'oi_user' => $user->getId(),
447 'oi_user_text' => $user->getName(),
448 'oi_metadata' => $props['metadata'],
449 'oi_media_type' => $props['media_type'],
450 'oi_major_mime' => $props['major_mime'],
451 'oi_minor_mime' => $props['minor_mime'],
452 'oi_sha1' => $props['sha1'],
453 ] + $commentFields, __METHOD__
454 );
455
456 return true;
457 }
458
459 /**
460 * If archive name is an empty string, then file does not "exist"
461 *
462 * This is the case for a couple files on Wikimedia servers where
463 * the old version is "lost".
464 * @return bool
465 */
466 public function exists() {
467 $archiveName = $this->getArchiveName();
468 if ( $archiveName === '' || !is_string( $archiveName ) ) {
469 return false;
470 }
471 return parent::exists();
472 }
473 }