Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 34 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
UploadFromStash | |
0.00% |
0 / 34 |
|
0.00% |
0 / 9 |
210 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
isValidKey | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isValidRequest | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
initialize | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
6 | |||
initializeFromRequest | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
getSourceType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getTempFileSha1Base36 | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
unsaveUploadedFile | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
postProcessUpload | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * Backend for uploading files from previously stored file. |
4 | * |
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. |
9 | * |
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. |
14 | * |
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 |
19 | * |
20 | * @file |
21 | * @ingroup Upload |
22 | */ |
23 | |
24 | use MediaWiki\MediaWikiServices; |
25 | use MediaWiki\Request\WebRequest; |
26 | use MediaWiki\User\UserIdentity; |
27 | |
28 | /** |
29 | * Implements uploading from previously stored file. |
30 | * |
31 | * @ingroup Upload |
32 | * @author Bryan Tong Minh |
33 | */ |
34 | class UploadFromStash extends UploadBase { |
35 | /** @var string */ |
36 | protected $mFileKey; |
37 | /** @var string */ |
38 | protected $mVirtualTempPath; |
39 | /** @var string */ |
40 | protected $mSourceType; |
41 | |
42 | /** @var UploadStash */ |
43 | private $stash; |
44 | |
45 | /** @var FileRepo */ |
46 | private $repo; |
47 | |
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 |
52 | */ |
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 | |
73 | /** |
74 | * @param string $key |
75 | * @return bool |
76 | */ |
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 | |
82 | /** |
83 | * @param WebRequest $request |
84 | * @return bool |
85 | */ |
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 | |
93 | /** |
94 | * @param string $key |
95 | * @param string $name |
96 | * @param bool $initTempFile |
97 | */ |
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. |
104 | */ |
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 | |
118 | /** |
119 | * @param WebRequest &$request |
120 | */ |
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 | |
134 | /** |
135 | * @return string |
136 | */ |
137 | public function getSourceType() { |
138 | return $this->mSourceType; |
139 | } |
140 | |
141 | /** |
142 | * Get the base 36 SHA1 of the file |
143 | * @return string |
144 | */ |
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 | |
156 | /** |
157 | * Remove a temporarily kept file stashed by saveTempUploadedFile(). |
158 | * @return bool Success |
159 | */ |
160 | public function unsaveUploadedFile() { |
161 | return $this->stash->removeFile( $this->mFileKey ); |
162 | } |
163 | |
164 | /** |
165 | * Remove the database record after a successful upload. |
166 | */ |
167 | public function postProcessUpload() { |
168 | parent::postProcessUpload(); |
169 | $this->unsaveUploadedFile(); |
170 | } |
171 | } |