MediaWiki master
UploadFromStash.php
Go to the documentation of this file.
1<?php
27
36 protected $mFileKey;
40 protected $mSourceType;
41
43 private $stash;
44
46 private $repo;
47
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
77 public static function isValidKey( $key ) {
78 // this is checked in more detail in UploadStash
79 return (bool)preg_match( UploadStash::KEY_FORMAT_REGEX, $key );
80 }
81
86 public static function isValidRequest( $request ) {
87 // this passes wpSessionKey to getText() as a default when wpFileKey isn't set.
88 // wpSessionKey has no default which guarantees failure if both are missing
89 // (though that should have been caught earlier)
90 return self::isValidKey( $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) ) );
91 }
92
98 public function initialize( $key, $name = 'upload_file', $initTempFile = true ) {
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
121 public function initializeFromRequest( &$request ) {
122 // sends wpSessionKey as a default when wpFileKey is missing
123 $fileKey = $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) );
124
125 // chooses one of wpDestFile, wpUploadFile, filename in that order.
126 $desiredDestName = $request->getText(
127 'wpDestFile',
128 $request->getText( 'wpUploadFile', $request->getText( 'filename' ) )
129 );
130
131 $this->initialize( $fileKey, $desiredDestName );
132 }
133
137 public function getSourceType() {
138 return $this->mSourceType;
139 }
140
145 public function getTempFileSha1Base36() {
146 // phan doesn't like us accessing this directly since in
147 // parent class this can be null, however we always set this in
148 // this class so it is safe. Add a check to keep phan happy.
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
160 public function unsaveUploadedFile() {
161 return $this->stash->removeFile( $this->mFileKey );
162 }
163
167 public function postProcessUpload() {
168 parent::postProcessUpload();
169 $this->unsaveUploadedFile();
170 }
171}
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Base class for file repositories.
Definition FileRepo.php:57
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.