Update Codex from v1.20.0 to v1.20.1
[mediawiki.git] / includes / upload / UploadFromStash.php
bloba0ccaec31c74d5cdeb581766b676e109a862bfe8
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 use MediaWiki\MediaWikiServices;
25 use MediaWiki\Request\WebRequest;
26 use MediaWiki\User\UserIdentity;
28 /**
29 * Implements uploading from previously stored file.
31 * @ingroup Upload
32 * @author Bryan Tong Minh
34 class UploadFromStash extends UploadBase {
35 /** @var string */
36 protected $mFileKey;
37 /** @var string */
38 protected $mVirtualTempPath;
39 /** @var string */
40 protected $mSourceType;
42 /** @var UploadStash */
43 private $stash;
45 /** @var FileRepo */
46 private $repo;
48 /**
49 * @param UserIdentity|null $user Default: null Sometimes this won't exist, as when running from cron.
50 * @param UploadStash|false $stash Default: false
51 * @param FileRepo|false $repo Default: false
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();
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" );
69 $this->stash = new UploadStash( $this->repo, $user );
73 /**
74 * @param string $key
75 * @return bool
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 );
82 /**
83 * @param WebRequest $request
84 * @return bool
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' ) ) );
93 /**
94 * @param string $key
95 * @param string $name
96 * @param bool $initTempFile
98 public function initialize( $key, $name = 'upload_file', $initTempFile = true ) {
99 /**
100 * Confirming a temporarily stashed upload.
101 * We don't want path names to be forged, so we keep
102 * them in the session on the server and just give
103 * an opaque key to the user agent.
105 $metadata = $this->stash->getMetadata( $key );
106 $this->initializePathInfo( $name,
107 $initTempFile ? $this->getRealPath( $metadata['us_path'] ) : false,
108 $metadata['us_size'],
109 false
112 $this->mFileKey = $key;
113 $this->mVirtualTempPath = $metadata['us_path'];
114 $this->mFileProps = $this->stash->getFileProps( $key );
115 $this->mSourceType = $metadata['us_source_type'];
119 * @param WebRequest &$request
121 public function initializeFromRequest( &$request ) {
122 // sends wpSessionKey as a default when wpFileKey is missing
123 $fileKey = $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) );
125 // chooses one of wpDestFile, wpUploadFile, filename in that order.
126 $desiredDestName = $request->getText(
127 'wpDestFile',
128 $request->getText( 'wpUploadFile', $request->getText( 'filename' ) )
131 $this->initialize( $fileKey, $desiredDestName );
135 * @return string
137 public function getSourceType() {
138 return $this->mSourceType;
142 * Get the base 36 SHA1 of the file
143 * @return string
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'];
157 * Remove a temporarily kept file stashed by saveTempUploadedFile().
158 * @return bool Success
160 public function unsaveUploadedFile() {
161 return $this->stash->removeFile( $this->mFileKey );
165 * Remove the database record after a successful upload.
167 public function postProcessUpload() {
168 parent::postProcessUpload();
169 $this->unsaveUploadedFile();