MediaWiki master
UploadFromStash.php
Go to the documentation of this file.
1<?php
10namespace MediaWiki\Upload;
11
12use LogicException;
18
27 protected $mFileKey;
31 protected $mSourceType;
32
34 private $stash;
35
37 private $repo;
38
44 public function __construct( ?UserIdentity $user = null, $stash = false, $repo = false ) {
45 if ( $repo ) {
46 $this->repo = $repo;
47 } else {
48 $this->repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
49 }
50
51 if ( $stash ) {
52 $this->stash = $stash;
53 } else {
54 if ( $user ) {
55 wfDebug( __METHOD__ . " creating new UploadStash instance for " . $user->getId() );
56 } else {
57 wfDebug( __METHOD__ . " creating new UploadStash instance with no user" );
58 }
59
60 $this->stash = new UploadStash( $this->repo, $user );
61 }
62 parent::__construct();
63 }
64
69 public static function isValidKey( $key ) {
70 // this is checked in more detail in UploadStash
71 return (bool)preg_match( UploadStash::KEY_FORMAT_REGEX, $key );
72 }
73
78 public static function isValidRequest( $request ) {
79 // this passes wpSessionKey to getText() as a default when wpFileKey isn't set.
80 // wpSessionKey has no default which guarantees failure if both are missing
81 // (though that should have been caught earlier)
82 return self::isValidKey( $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) ) );
83 }
84
90 public function initialize( $key, $name = 'upload_file', $initTempFile = true ) {
97 $metadata = $this->stash->getMetadata( $key );
98 $tempPath = $initTempFile ? $this->getRealPath( $metadata['us_path'] ) : null;
99 if ( $tempPath === false ) {
100 throw new UploadStashBadPathException( wfMessage( 'uploadstash-bad-path' ) );
101 }
102 $this->initializePathInfo( $name,
103 $tempPath,
104 $metadata['us_size'],
105 false
106 );
107
108 $this->mFileKey = $key;
109 $this->mVirtualTempPath = $metadata['us_path'];
110 $this->mFileProps = $this->stash->getFileProps( $key );
111 $this->mSourceType = $metadata['us_source_type'];
112 $this->mStashFile = $this->stash->getFile( $key );
113 }
114
118 public function initializeFromRequest( &$request ) {
119 // sends wpSessionKey as a default when wpFileKey is missing
120 $fileKey = $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) );
121
122 // chooses one of wpDestFile, wpUploadFile, filename in that order.
123 $desiredDestName = $request->getText(
124 'wpDestFile',
125 $request->getText( 'wpUploadFile', $request->getText( 'filename' ) )
126 );
127
128 $this->initialize( $fileKey, $desiredDestName );
129 }
130
134 public function getSourceType() {
135 return $this->mSourceType;
136 }
137
142 public function getTempFileSha1Base36() {
143 // phan doesn't like us accessing this directly since in
144 // parent class this can be null, however we always set this in
145 // this class so it is safe. Add a check to keep phan happy.
146 if ( !is_array( $this->mFileProps ) ) {
147 throw new LogicException( "mFileProps should never be null" );
148 } else {
149 return $this->mFileProps['sha1'];
150 }
151 }
152
157 public function unsaveUploadedFile() {
158 $this->mStashFile = null;
159 return $this->stash->removeFile( $this->mFileKey );
160 }
161
165 public function postProcessUpload() {
166 parent::postProcessUpload();
167 $this->unsaveUploadedFile();
168 }
169}
170
172class_alias( UploadFromStash::class, 'UploadFromStash' );
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Local repository that stores files in the local filesystem and registers them in the wiki's own datab...
Definition LocalRepo.php:45
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form,...
UploadBase and subclasses are the backend of MediaWiki's file uploads.
initializePathInfo( $name, $tempPath, $fileSize, $removeTempFile=false)
Implements uploading from previously stored file.
getTempFileSha1Base36()
Get the base 36 SHA1 of the file.
unsaveUploadedFile()
Remove a temporarily kept file stashed by saveTempUploadedFile().
initialize( $key, $name='upload_file', $initTempFile=true)
postProcessUpload()
Remove the database record after a successful upload.
__construct(?UserIdentity $user=null, $stash=false, $repo=false)
UploadStash is intended to accomplish a few things:
Interface for objects representing user identity.