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 | protected $mFileKey; |
36 | protected $mVirtualTempPath; |
37 | protected $mSourceType; |
38 | |
39 | /** @var UploadStash */ |
40 | private $stash; |
41 | |
42 | /** @var FileRepo */ |
43 | private $repo; |
44 | |
45 | /** |
46 | * @param UserIdentity|null $user Default: null Sometimes this won't exist, as when running from cron. |
47 | * @param UploadStash|false $stash Default: false |
48 | * @param FileRepo|false $repo Default: false |
49 | */ |
50 | public function __construct( UserIdentity $user = null, $stash = false, $repo = false ) { |
51 | if ( $repo ) { |
52 | $this->repo = $repo; |
53 | } else { |
54 | $this->repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo(); |
55 | } |
56 | |
57 | if ( $stash ) { |
58 | $this->stash = $stash; |
59 | } else { |
60 | if ( $user ) { |
61 | wfDebug( __METHOD__ . " creating new UploadStash instance for " . $user->getId() ); |
62 | } else { |
63 | wfDebug( __METHOD__ . " creating new UploadStash instance with no user" ); |
64 | } |
65 | |
66 | $this->stash = new UploadStash( $this->repo, $user ); |
67 | } |
68 | } |
69 | |
70 | /** |
71 | * @param string $key |
72 | * @return bool |
73 | */ |
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 ); |
77 | } |
78 | |
79 | /** |
80 | * @param WebRequest $request |
81 | * @return bool |
82 | */ |
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' ) ) ); |
88 | } |
89 | |
90 | /** |
91 | * @param string $key |
92 | * @param string $name |
93 | * @param bool $initTempFile |
94 | */ |
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. |
101 | */ |
102 | $metadata = $this->stash->getMetadata( $key ); |
103 | $this->initializePathInfo( $name, |
104 | $initTempFile ? $this->getRealPath( $metadata['us_path'] ) : false, |
105 | $metadata['us_size'], |
106 | false |
107 | ); |
108 | |
109 | $this->mFileKey = $key; |
110 | $this->mVirtualTempPath = $metadata['us_path']; |
111 | $this->mFileProps = $this->stash->getFileProps( $key ); |
112 | $this->mSourceType = $metadata['us_source_type']; |
113 | } |
114 | |
115 | /** |
116 | * @param WebRequest &$request |
117 | */ |
118 | public function initializeFromRequest( &$request ) { |
119 | // sends wpSessionKey as a default when wpFileKey is missing |
120 | $fileKey = $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) ); |
121 | |
122 | // chooses one of wpDestFile, wpUploadFile, filename in that order. |
123 | $desiredDestName = $request->getText( |
124 | 'wpDestFile', |
125 | $request->getText( 'wpUploadFile', $request->getText( 'filename' ) ) |
126 | ); |
127 | |
128 | $this->initialize( $fileKey, $desiredDestName ); |
129 | } |
130 | |
131 | /** |
132 | * @return string |
133 | */ |
134 | public function getSourceType() { |
135 | return $this->mSourceType; |
136 | } |
137 | |
138 | /** |
139 | * Get the base 36 SHA1 of the file |
140 | * @return string |
141 | */ |
142 | public function getTempFileSha1Base36() { |
143 | // phan doesn't like us accessing this directly since in |
144 | // parent class this can be null, however we always set this in |
145 | // this class so it is safe. Add a check to keep phan happy. |
146 | if ( !is_array( $this->mFileProps ) ) { |
147 | throw new LogicException( "mFileProps should never be null" ); |
148 | } else { |
149 | return $this->mFileProps['sha1']; |
150 | } |
151 | } |
152 | |
153 | /** |
154 | * Remove a temporarily kept file stashed by saveTempUploadedFile(). |
155 | * @return bool Success |
156 | */ |
157 | public function unsaveUploadedFile() { |
158 | return $this->stash->removeFile( $this->mFileKey ); |
159 | } |
160 | |
161 | /** |
162 | * Remove the database record after a successful upload. |
163 | */ |
164 | public function postProcessUpload() { |
165 | parent::postProcessUpload(); |
166 | $this->unsaveUploadedFile(); |
167 | } |
168 | } |