]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/filerepo/file/OldLocalFile.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.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                 $row = $dbr->selectRow( 'oldimage', self::selectFields(), $conds, __METHOD__ );
97                 if ( $row ) {
98                         return self::newFromRow( $row, $repo );
99                 } else {
100                         return false;
101                 }
102         }
103
104         /**
105          * Fields in the oldimage table
106          * @todo Deprecate this in favor of a method that returns tables and joins
107          *  as well, and use CommentStore::getJoin().
108          * @return array
109          */
110         static function selectFields() {
111                 return [
112                         'oi_name',
113                         'oi_archive_name',
114                         'oi_size',
115                         'oi_width',
116                         'oi_height',
117                         'oi_metadata',
118                         'oi_bits',
119                         'oi_media_type',
120                         'oi_major_mime',
121                         'oi_minor_mime',
122                         'oi_user',
123                         'oi_user_text',
124                         'oi_timestamp',
125                         'oi_deleted',
126                         'oi_sha1',
127                 ] + CommentStore::newKey( 'oi_description' )->getFields();
128         }
129
130         /**
131          * @param Title $title
132          * @param FileRepo $repo
133          * @param string $time Timestamp or null to load by archive name
134          * @param string $archiveName Archive name or null to load by timestamp
135          * @throws MWException
136          */
137         function __construct( $title, $repo, $time, $archiveName ) {
138                 parent::__construct( $title, $repo );
139                 $this->requestedTime = $time;
140                 $this->archive_name = $archiveName;
141                 if ( is_null( $time ) && is_null( $archiveName ) ) {
142                         throw new MWException( __METHOD__ . ': must specify at least one of $time or $archiveName' );
143                 }
144         }
145
146         /**
147          * @return bool
148          */
149         function getCacheKey() {
150                 return false;
151         }
152
153         /**
154          * @return string
155          */
156         function getArchiveName() {
157                 if ( !isset( $this->archive_name ) ) {
158                         $this->load();
159                 }
160
161                 return $this->archive_name;
162         }
163
164         /**
165          * @return bool
166          */
167         function isOld() {
168                 return true;
169         }
170
171         /**
172          * @return bool
173          */
174         function isVisible() {
175                 return $this->exists() && !$this->isDeleted( File::DELETED_FILE );
176         }
177
178         function loadFromDB( $flags = 0 ) {
179                 $this->dataLoaded = true;
180
181                 $dbr = ( $flags & self::READ_LATEST )
182                         ? $this->repo->getMasterDB()
183                         : $this->repo->getReplicaDB();
184
185                 $conds = [ 'oi_name' => $this->getName() ];
186                 if ( is_null( $this->requestedTime ) ) {
187                         $conds['oi_archive_name'] = $this->archive_name;
188                 } else {
189                         $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime );
190                 }
191                 $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
192                         $conds, __METHOD__, [ 'ORDER BY' => 'oi_timestamp DESC' ] );
193                 if ( $row ) {
194                         $this->loadFromRow( $row, 'oi_' );
195                 } else {
196                         $this->fileExists = false;
197                 }
198         }
199
200         /**
201          * Load lazy file metadata from the DB
202          */
203         protected function loadExtraFromDB() {
204                 $this->extraDataLoaded = true;
205                 $dbr = $this->repo->getReplicaDB();
206                 $conds = [ 'oi_name' => $this->getName() ];
207                 if ( is_null( $this->requestedTime ) ) {
208                         $conds['oi_archive_name'] = $this->archive_name;
209                 } else {
210                         $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime );
211                 }
212                 // In theory the file could have just been renamed/deleted...oh well
213                 $row = $dbr->selectRow( 'oldimage', $this->getLazyCacheFields( 'oi_' ),
214                         $conds, __METHOD__, [ 'ORDER BY' => 'oi_timestamp DESC' ] );
215
216                 if ( !$row ) { // fallback to master
217                         $dbr = $this->repo->getMasterDB();
218                         $row = $dbr->selectRow( 'oldimage', $this->getLazyCacheFields( 'oi_' ),
219                                 $conds, __METHOD__, [ 'ORDER BY' => 'oi_timestamp DESC' ] );
220                 }
221
222                 if ( $row ) {
223                         foreach ( $this->unprefixRow( $row, 'oi_' ) as $name => $value ) {
224                                 $this->$name = $value;
225                         }
226                 } else {
227                         throw new MWException( "Could not find data for image '{$this->archive_name}'." );
228                 }
229         }
230
231         /**
232          * @param string $prefix
233          * @return array
234          */
235         function getCacheFields( $prefix = 'img_' ) {
236                 $fields = parent::getCacheFields( $prefix );
237                 $fields[] = $prefix . 'archive_name';
238                 $fields[] = $prefix . 'deleted';
239
240                 return $fields;
241         }
242
243         /**
244          * @return string
245          */
246         function getRel() {
247                 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
248         }
249
250         /**
251          * @return string
252          */
253         function getUrlRel() {
254                 return 'archive/' . $this->getHashPath() . rawurlencode( $this->getArchiveName() );
255         }
256
257         function upgradeRow() {
258                 $this->loadFromFile();
259
260                 # Don't destroy file info of missing files
261                 if ( !$this->fileExists ) {
262                         wfDebug( __METHOD__ . ": file does not exist, aborting\n" );
263
264                         return;
265                 }
266
267                 $dbw = $this->repo->getMasterDB();
268                 list( $major, $minor ) = self::splitMime( $this->mime );
269
270                 wfDebug( __METHOD__ . ': upgrading ' . $this->archive_name . " to the current schema\n" );
271                 $dbw->update( 'oldimage',
272                         [
273                                 'oi_size' => $this->size, // sanity
274                                 'oi_width' => $this->width,
275                                 'oi_height' => $this->height,
276                                 'oi_bits' => $this->bits,
277                                 'oi_media_type' => $this->media_type,
278                                 'oi_major_mime' => $major,
279                                 'oi_minor_mime' => $minor,
280                                 'oi_metadata' => $this->metadata,
281                                 'oi_sha1' => $this->sha1,
282                         ], [
283                                 'oi_name' => $this->getName(),
284                                 'oi_archive_name' => $this->archive_name ],
285                         __METHOD__
286                 );
287         }
288
289         /**
290          * @param int $field One of DELETED_* bitfield constants for file or
291          *   revision rows
292          * @return bool
293          */
294         function isDeleted( $field ) {
295                 $this->load();
296
297                 return ( $this->deleted & $field ) == $field;
298         }
299
300         /**
301          * Returns bitfield value
302          * @return int
303          */
304         function getVisibility() {
305                 $this->load();
306
307                 return (int)$this->deleted;
308         }
309
310         /**
311          * Determine if the current user is allowed to view a particular
312          * field of this image file, if it's marked as deleted.
313          *
314          * @param int $field
315          * @param User|null $user User object to check, or null to use $wgUser
316          * @return bool
317          */
318         function userCan( $field, User $user = null ) {
319                 $this->load();
320
321                 return Revision::userCanBitfield( $this->deleted, $field, $user );
322         }
323
324         /**
325          * Upload a file directly into archive. Generally for Special:Import.
326          *
327          * @param string $srcPath File system path of the source file
328          * @param string $archiveName Full archive name of the file, in the form
329          *   $timestamp!$filename, where $filename must match $this->getName()
330          * @param string $timestamp
331          * @param string $comment
332          * @param User $user
333          * @return Status
334          */
335         function uploadOld( $srcPath, $archiveName, $timestamp, $comment, $user ) {
336                 $this->lock();
337
338                 $dstRel = 'archive/' . $this->getHashPath() . $archiveName;
339                 $status = $this->publishTo( $srcPath, $dstRel );
340
341                 if ( $status->isGood() ) {
342                         if ( !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) ) {
343                                 $status->fatal( 'filenotfound', $srcPath );
344                         }
345                 }
346
347                 $this->unlock();
348
349                 return $status;
350         }
351
352         /**
353          * Record a file upload in the oldimage table, without adding log entries.
354          *
355          * @param string $srcPath File system path to the source file
356          * @param string $archiveName The archive name of the file
357          * @param string $timestamp
358          * @param string $comment Upload comment
359          * @param User $user User who did this upload
360          * @return bool
361          */
362         protected function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
363                 $dbw = $this->repo->getMasterDB();
364
365                 $dstPath = $this->repo->getZonePath( 'public' ) . '/' . $this->getRel();
366                 $props = $this->repo->getFileProps( $dstPath );
367                 if ( !$props['fileExists'] ) {
368                         return false;
369                 }
370
371                 $commentFields = CommentStore::newKey( 'oi_description' )->insert( $dbw, $comment );
372                 $dbw->insert( 'oldimage',
373                         [
374                                 'oi_name' => $this->getName(),
375                                 'oi_archive_name' => $archiveName,
376                                 'oi_size' => $props['size'],
377                                 'oi_width' => intval( $props['width'] ),
378                                 'oi_height' => intval( $props['height'] ),
379                                 'oi_bits' => $props['bits'],
380                                 'oi_timestamp' => $dbw->timestamp( $timestamp ),
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'],
388                         ] + $commentFields, __METHOD__
389                 );
390
391                 return true;
392         }
393
394         /**
395          * If archive name is an empty string, then file does not "exist"
396          *
397          * This is the case for a couple files on Wikimedia servers where
398          * the old version is "lost".
399          * @return bool
400          */
401         public function exists() {
402                 $archiveName = $this->getArchiveName();
403                 if ( $archiveName === '' || !is_string( $archiveName ) ) {
404                         return false;
405                 }
406                 return parent::exists();
407         }
408 }