Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
12.00% |
3 / 25 |
|
16.67% |
1 / 6 |
CRAP | |
0.00% |
0 / 1 |
| UploadFromFile | |
12.50% |
3 / 24 |
|
16.67% |
1 / 6 |
92.06 | |
0.00% |
0 / 1 |
| initializeFromRequest | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| initialize | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| isValidRequest | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getSourceType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| skipStashFileAttempt | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| verifyUpload | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Backend for regular file upload. |
| 4 | * |
| 5 | * @license GPL-2.0-or-later |
| 6 | * @file |
| 7 | * @ingroup Upload |
| 8 | */ |
| 9 | |
| 10 | namespace MediaWiki\Upload; |
| 11 | |
| 12 | use MediaWiki\Request\WebRequest; |
| 13 | use MediaWiki\Request\WebRequestUpload; |
| 14 | |
| 15 | /** |
| 16 | * Implements regular file uploads |
| 17 | * |
| 18 | * @ingroup Upload |
| 19 | * @author Bryan Tong Minh |
| 20 | */ |
| 21 | class UploadFromFile extends UploadBase { |
| 22 | /** |
| 23 | * @var WebRequestUpload |
| 24 | */ |
| 25 | protected $mUpload = null; |
| 26 | |
| 27 | /** |
| 28 | * @param WebRequest &$request |
| 29 | */ |
| 30 | public function initializeFromRequest( &$request ) { |
| 31 | $upload = $request->getUpload( 'wpUploadFile' ); |
| 32 | $desiredDestName = $request->getText( 'wpDestFile' ); |
| 33 | if ( !$desiredDestName ) { |
| 34 | $desiredDestName = $upload->getName(); |
| 35 | } |
| 36 | |
| 37 | // @phan-suppress-next-line PhanTypeMismatchArgumentNullable getName only null on failure |
| 38 | $this->initialize( $desiredDestName, $upload ); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Initialize from a filename and a MediaWiki\Request\WebRequestUpload |
| 43 | * @param string $name |
| 44 | * @param WebRequestUpload $webRequestUpload |
| 45 | */ |
| 46 | public function initialize( $name, $webRequestUpload ) { |
| 47 | $this->mUpload = $webRequestUpload; |
| 48 | $this->initializePathInfo( $name, |
| 49 | $this->mUpload->getTempName(), $this->mUpload->getSize() ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @param WebRequest $request |
| 54 | * @return bool |
| 55 | */ |
| 56 | public static function isValidRequest( $request ) { |
| 57 | # Allow all requests, even if no file is present, so that an error |
| 58 | # because a post_max_size or upload_max_filesize overflow |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @return string |
| 64 | */ |
| 65 | public function getSourceType() { |
| 66 | return 'file'; |
| 67 | } |
| 68 | |
| 69 | public function skipStashFileAttempt(): bool { |
| 70 | $tempName = $this->mUpload?->getTempName(); |
| 71 | if ( $tempName !== null ) { |
| 72 | return !file_exists( $tempName ); |
| 73 | } |
| 74 | return parent::skipStashFileAttempt(); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @return array |
| 79 | */ |
| 80 | public function verifyUpload() { |
| 81 | # Check for a post_max_size or upload_max_size overflow, so that a |
| 82 | # proper error can be shown to the user |
| 83 | if ( $this->mTempPath === null || $this->isEmptyFile() ) { |
| 84 | if ( $this->mUpload->isIniSizeOverflow() ) { |
| 85 | return [ |
| 86 | 'status' => UploadBase::FILE_TOO_LARGE, |
| 87 | 'max' => min( |
| 88 | self::getMaxUploadSize( $this->getSourceType() ), |
| 89 | self::getMaxPhpUploadSize() |
| 90 | ), |
| 91 | ]; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return parent::verifyUpload(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** @deprecated class alias since 1.46 */ |
| 100 | class_alias( UploadFromFile::class, 'UploadFromFile' ); |