MediaWiki master
UploadFromStash.php
Go to the documentation of this file.
1<?php
10namespace MediaWiki\Upload;
11
12use LogicException;
19
28 protected $mFileKey;
32 protected $mSourceType;
33
35 private $stash;
36
38 private $repo;
39
46 public function __construct( ?UserIdentity $user = null, $stash = false, $repo = false ) {
47 if ( $repo ) {
48 $this->repo = $repo;
49 } else {
50 $this->repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
51 }
52
53 if ( $stash ) {
54 $this->stash = $stash;
55 } else {
56 if ( $user ) {
57 wfDebug( __METHOD__ . " creating new UploadStash instance for " . $user->getId() );
58 } else {
59 wfDebug( __METHOD__ . " creating new UploadStash instance with no user" );
60 }
61
62 $this->stash = new UploadStash( $this->repo, $user );
63 }
64 parent::__construct();
65 }
66
71 public static function isValidKey( $key ) {
72 return (bool)preg_match( UploadStash::KEY_FORMAT_REGEX, $key );
73 }
74
79 public static function isValidRequest( $request ) {
80 // Get the stash key from wpFileKey or wpSessionKey.
81 return self::isValidKey( $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) ) );
82 }
83
90 public function initialize( $key, $name = 'upload_file', $initTempFile = true ) {
91 // Confirm that the file key is valid.
92 // We don't want path names to be forged, so we keep them in the
93 // uploadstash table and just give an opaque key to the user agent.
94 $metadata = $this->stash->getMetadata( $key );
95 $tempPath = $initTempFile ? $this->getRealPath( $metadata['us_path'] ) : null;
96 if ( $tempPath === false ) {
97 throw new UploadStashBadPathException( wfMessage( 'uploadstash-bad-path' ) );
98 }
99 $this->initializePathInfo( $name,
100 $tempPath,
101 $metadata['us_size'],
102 false
103 );
104
105 $this->mFileKey = $key;
106 $this->mVirtualTempPath = $metadata['us_path'];
107 $this->mFileProps = $this->stash->getFileProps( $key );
108 $this->mSourceType = $metadata['us_source_type'];
109 $this->mStashFile = $this->stash->getFile( $key );
110 }
111
115 public function initializeFromRequest( &$request ) {
116 // Get the stash key from wpFileKey or wpSessionKey.
117 // All callers apparently use wpSessionKey.
118 $fileKey = $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) );
119
120 // Get the dest name from wpDestFile, wpUploadFile or filename in that order.
121 $desiredDestName = $request->getText(
122 'wpDestFile',
123 $request->getText( 'wpUploadFile', $request->getText( 'filename' ) )
124 );
125
126 $this->initialize( $fileKey, $desiredDestName );
127 }
128
132 public function getSourceType() {
133 return $this->mSourceType;
134 }
135
140 public function getTempFileSha1Base36() {
141 // phan doesn't like us accessing this directly since in
142 // parent class this can be null, however we always set this in
143 // this class so it is safe. Add a check to keep phan happy.
144 if ( !is_array( $this->mFileProps ) ) {
145 throw new LogicException( "mFileProps should never be null" );
146 } else {
147 return $this->mFileProps['sha1'];
148 }
149 }
150
155 public function unsaveUploadedFile() {
156 $this->mStashFile = null;
157 return $this->stash->removeFile( $this->mFileKey );
158 }
159
163 public function postProcessUpload() {
164 parent::postProcessUpload();
165 $this->unsaveUploadedFile();
166 }
167}
168
170class_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:44
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 provides temporary file storage for users, persisting across multiple requests.
Interface for objects representing user identity.