| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
use MediaWiki\MediaWikiServices; |
| 25 |
use MediaWiki\Request\WebRequest; |
| 26 |
use MediaWiki\User\UserIdentity; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class UploadFromStash extends UploadBase { |
| 35 |
|
| 36 |
protected $mFileKey; |
| 37 |
|
| 38 |
protected $mVirtualTempPath; |
| 39 |
|
| 40 |
protected $mSourceType; |
| 41 |
|
| 42 |
|
| 43 |
private $stash; |
| 44 |
|
| 45 |
|
| 46 |
private $repo; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public function __construct( ?UserIdentity $user = null, $stash = false, $repo = false ) { |
| 54 |
if ( $repo ) { |
| 55 |
$this->repo = $repo; |
| 56 |
} else { |
| 57 |
$this->repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo(); |
| 58 |
} |
| 59 |
|
| 60 |
if ( $stash ) { |
| 61 |
$this->stash = $stash; |
| 62 |
} else { |
| 63 |
if ( $user ) { |
| 64 |
wfDebug( __METHOD__ . " creating new UploadStash instance for " . $user->getId() ); |
| 65 |
} else { |
| 66 |
wfDebug( __METHOD__ . " creating new UploadStash instance with no user" ); |
| 67 |
} |
| 68 |
|
| 69 |
$this->stash = new UploadStash( $this->repo, $user ); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public static function isValidKey( $key ) { |
| 78 |
|
| 79 |
return (bool)preg_match( UploadStash::KEY_FORMAT_REGEX, $key ); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public static function isValidRequest( $request ) { |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
return self::isValidKey( $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) ) ); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public function initialize( $key, $name = 'upload_file', $initTempFile = true ) { |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
$metadata = $this->stash->getMetadata( $key ); |
| 106 |
$this->initializePathInfo( $name, |
| 107 |
$initTempFile ? $this->getRealPath( $metadata['us_path'] ) : false, |
| 108 |
$metadata['us_size'], |
| 109 |
false |
| 110 |
); |
| 111 |
|
| 112 |
$this->mFileKey = $key; |
| 113 |
$this->mVirtualTempPath = $metadata['us_path']; |
| 114 |
$this->mFileProps = $this->stash->getFileProps( $key ); |
| 115 |
$this->mSourceType = $metadata['us_source_type']; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public function initializeFromRequest( &$request ) { |
| 122 |
|
| 123 |
$fileKey = $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) ); |
| 124 |
|
| 125 |
|
| 126 |
$desiredDestName = $request->getText( |
| 127 |
'wpDestFile', |
| 128 |
$request->getText( 'wpUploadFile', $request->getText( 'filename' ) ) |
| 129 |
); |
| 130 |
|
| 131 |
$this->initialize( $fileKey, $desiredDestName ); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public function getSourceType() { |
| 138 |
return $this->mSourceType; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public function getTempFileSha1Base36() { |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
if ( !is_array( $this->mFileProps ) ) { |
| 150 |
throw new LogicException( "mFileProps should never be null" ); |
| 151 |
} else { |
| 152 |
return $this->mFileProps['sha1']; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
public function unsaveUploadedFile() { |
| 161 |
return $this->stash->removeFile( $this->mFileKey ); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public function postProcessUpload() { |
| 168 |
parent::postProcessUpload(); |
| 169 |
$this->unsaveUploadedFile(); |
| 170 |
} |
| 171 |
} |