MediaWiki master
UploadFromStash.php
Go to the documentation of this file.
1<?php
28
37 protected $mFileKey;
41 protected $mSourceType;
42
44 private $stash;
45
47 private $repo;
48
54 public function __construct( ?UserIdentity $user = null, $stash = false, $repo = false ) {
55 if ( $repo ) {
56 $this->repo = $repo;
57 } else {
58 $this->repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
59 }
60
61 if ( $stash ) {
62 $this->stash = $stash;
63 } else {
64 if ( $user ) {
65 wfDebug( __METHOD__ . " creating new UploadStash instance for " . $user->getId() );
66 } else {
67 wfDebug( __METHOD__ . " creating new UploadStash instance with no user" );
68 }
69
70 $this->stash = new UploadStash( $this->repo, $user );
71 }
72 }
73
78 public static function isValidKey( $key ) {
79 // this is checked in more detail in UploadStash
80 return (bool)preg_match( UploadStash::KEY_FORMAT_REGEX, $key );
81 }
82
87 public static function isValidRequest( $request ) {
88 // this passes wpSessionKey to getText() as a default when wpFileKey isn't set.
89 // wpSessionKey has no default which guarantees failure if both are missing
90 // (though that should have been caught earlier)
91 return self::isValidKey( $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) ) );
92 }
93
99 public function initialize( $key, $name = 'upload_file', $initTempFile = true ) {
106 $metadata = $this->stash->getMetadata( $key );
107 $tempPath = $initTempFile ? $this->getRealPath( $metadata['us_path'] ) : null;
108 if ( $tempPath === false ) {
109 throw new UploadStashBadPathException( wfMessage( 'uploadstash-bad-path' ) );
110 }
111 $this->initializePathInfo( $name,
112 $tempPath,
113 $metadata['us_size'],
114 false
115 );
116
117 $this->mFileKey = $key;
118 $this->mVirtualTempPath = $metadata['us_path'];
119 $this->mFileProps = $this->stash->getFileProps( $key );
120 $this->mSourceType = $metadata['us_source_type'];
121 }
122
126 public function initializeFromRequest( &$request ) {
127 // sends wpSessionKey as a default when wpFileKey is missing
128 $fileKey = $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) );
129
130 // chooses one of wpDestFile, wpUploadFile, filename in that order.
131 $desiredDestName = $request->getText(
132 'wpDestFile',
133 $request->getText( 'wpUploadFile', $request->getText( 'filename' ) )
134 );
135
136 $this->initialize( $fileKey, $desiredDestName );
137 }
138
142 public function getSourceType() {
143 return $this->mSourceType;
144 }
145
150 public function getTempFileSha1Base36() {
151 // phan doesn't like us accessing this directly since in
152 // parent class this can be null, however we always set this in
153 // this class so it is safe. Add a check to keep phan happy.
154 if ( !is_array( $this->mFileProps ) ) {
155 throw new LogicException( "mFileProps should never be null" );
156 } else {
157 return $this->mFileProps['sha1'];
158 }
159 }
160
165 public function unsaveUploadedFile() {
166 return $this->stash->removeFile( $this->mFileKey );
167 }
168
172 public function postProcessUpload() {
173 parent::postProcessUpload();
174 $this->unsaveUploadedFile();
175 }
176}
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.
Base class for file repositories.
Definition FileRepo.php:68
Service locator for MediaWiki core services.
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.
getRealPath( $srcPath)
initializePathInfo( $name, $tempPath, $fileSize, $removeTempFile=false)
Implements uploading from previously stored file.
__construct(?UserIdentity $user=null, $stash=false, $repo=false)
getTempFileSha1Base36()
Get the base 36 SHA1 of the file.
static isValidKey( $key)
static isValidRequest( $request)
initializeFromRequest(&$request)
unsaveUploadedFile()
Remove a temporarily kept file stashed by saveTempUploadedFile().
postProcessUpload()
Remove the database record after a successful upload.
initialize( $key, $name='upload_file', $initTempFile=true)
UploadStash is intended to accomplish a few things:
Interface for objects representing user identity.