Remove silly 'm' prefix from StubObject/DeprecatedGlobal members
[mediawiki.git] / includes / upload / UploadFromStash.php
blobb4e815fc87609556123799e778ff55086a5dacef
1 <?php
2 /**
3 * Backend for uploading files from previously stored file.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Upload
24 /**
25 * Implements uploading from previously stored file.
27 * @ingroup Upload
28 * @author Bryan Tong Minh
30 class UploadFromStash extends UploadBase {
31 protected $mFileKey;
32 protected $mVirtualTempPath;
33 protected $mFileProps;
34 protected $mSourceType;
36 // an instance of UploadStash
37 private $stash;
39 //LocalFile repo
40 private $repo;
42 /**
43 * @param User|bool $user Default: false
44 * @param UploadStash|bool $stash Default: false
45 * @param FileRepo|bool $repo Default: false
47 public function __construct( $user = false, $stash = false, $repo = false ) {
48 // user object. sometimes this won't exist, as when running from cron.
49 $this->user = $user;
51 if ( $repo ) {
52 $this->repo = $repo;
53 } else {
54 $this->repo = RepoGroup::singleton()->getLocalRepo();
57 if ( $stash ) {
58 $this->stash = $stash;
59 } else {
60 if ( $user ) {
61 wfDebug( __METHOD__ . " creating new UploadStash instance for " . $user->getId() . "\n" );
62 } else {
63 wfDebug( __METHOD__ . " creating new UploadStash instance with no user\n" );
66 $this->stash = new UploadStash( $this->repo, $this->user );
70 /**
71 * @param string $key
72 * @return bool
74 public static function isValidKey( $key ) {
75 // this is checked in more detail in UploadStash
76 return (bool)preg_match( UploadStash::KEY_FORMAT_REGEX, $key );
79 /**
80 * @param WebRequest $request
81 * @return bool
83 public static function isValidRequest( $request ) {
84 // this passes wpSessionKey to getText() as a default when wpFileKey isn't set.
85 // wpSessionKey has no default which guarantees failure if both are missing
86 // (though that should have been caught earlier)
87 return self::isValidKey( $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) ) );
90 /**
91 * @param string $key
92 * @param string $name
93 * @param bool $initTempFile
95 public function initialize( $key, $name = 'upload_file', $initTempFile = true ) {
96 /**
97 * Confirming a temporarily stashed upload.
98 * We don't want path names to be forged, so we keep
99 * them in the session on the server and just give
100 * an opaque key to the user agent.
102 $metadata = $this->stash->getMetadata( $key );
103 $this->initializePathInfo( $name,
104 $initTempFile ? $this->getRealPath( $metadata['us_path'] ) : false,
105 $metadata['us_size'],
106 false
109 $this->mFileKey = $key;
110 $this->mVirtualTempPath = $metadata['us_path'];
111 $this->mFileProps = $this->stash->getFileProps( $key );
112 $this->mSourceType = $metadata['us_source_type'];
116 * @param WebRequest $request
118 public function initializeFromRequest( &$request ) {
119 // sends wpSessionKey as a default when wpFileKey is missing
120 $fileKey = $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) );
122 // chooses one of wpDestFile, wpUploadFile, filename in that order.
123 $desiredDestName = $request->getText(
124 'wpDestFile',
125 $request->getText( 'wpUploadFile', $request->getText( 'filename' ) )
128 $this->initialize( $fileKey, $desiredDestName );
132 * @return string
134 public function getSourceType() {
135 return $this->mSourceType;
139 * Get the base 36 SHA1 of the file
140 * @return string
142 public function getTempFileSha1Base36() {
143 return $this->mFileProps['sha1'];
147 * protected function verifyFile() inherited
151 * Stash the file.
153 * @param User $user
154 * @return UploadStashFile
156 public function stashFile( User $user = null ) {
157 // replace mLocalFile with an instance of UploadStashFile, which adds some methods
158 // that are useful for stashed files.
159 $this->mLocalFile = parent::stashFile( $user );
161 return $this->mLocalFile;
165 * This should return the key instead of the UploadStashFile instance, for backward compatibility.
166 * @return string
168 public function stashSession() {
169 return $this->stashFile()->getFileKey();
173 * Remove a temporarily kept file stashed by saveTempUploadedFile().
174 * @return bool Success
176 public function unsaveUploadedFile() {
177 return $this->stash->removeFile( $this->mFileKey );
181 * Perform the upload, then remove the database record afterward.
182 * @param string $comment
183 * @param string $pageText
184 * @param bool $watch
185 * @param User $user
186 * @return Status
188 public function performUpload( $comment, $pageText, $watch, $user ) {
189 $rv = parent::performUpload( $comment, $pageText, $watch, $user );
190 $this->unsaveUploadedFile();
192 return $rv;