Merge "Reduced some master queries by adding flags to Revision functions."
[lhc/web/wiklou.git] / includes / upload / UploadFromStash.php
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 /**
25 * Implements uploading from previously stored file.
26 *
27 * @ingroup Upload
28 * @author Bryan Tong Minh
29 */
30 class UploadFromStash extends UploadBase {
31 protected $mFileKey, $mVirtualTempPath, $mFileProps, $mSourceType;
32
33 // an instance of UploadStash
34 private $stash;
35
36 //LocalFile repo
37 private $repo;
38
39 /**
40 * @param $user User
41 * @param $stash UploadStash
42 * @param $repo FileRepo
43 */
44 public function __construct( $user = false, $stash = false, $repo = false ) {
45 // user object. sometimes this won't exist, as when running from cron.
46 $this->user = $user;
47
48 if( $repo ) {
49 $this->repo = $repo;
50 } else {
51 $this->repo = RepoGroup::singleton()->getLocalRepo();
52 }
53
54 if( $stash ) {
55 $this->stash = $stash;
56 } else {
57 if( $user ) {
58 wfDebug( __METHOD__ . " creating new UploadStash instance for " . $user->getId() . "\n" );
59 } else {
60 wfDebug( __METHOD__ . " creating new UploadStash instance with no user\n" );
61 }
62
63 $this->stash = new UploadStash( $this->repo, $this->user );
64 }
65
66 return true;
67 }
68
69 /**
70 * @param $key string
71 * @return bool
72 */
73 public static function isValidKey( $key ) {
74 // this is checked in more detail in UploadStash
75 return (bool)preg_match( UploadStash::KEY_FORMAT_REGEX, $key );
76 }
77
78 /**
79 * @param $request WebRequest
80 *
81 * @return Boolean
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 $key string
92 * @param $name string
93 */
94 public function initialize( $key, $name = 'upload_file' ) {
95 /**
96 * Confirming a temporarily stashed upload.
97 * We don't want path names to be forged, so we keep
98 * them in the session on the server and just give
99 * an opaque key to the user agent.
100 */
101 $metadata = $this->stash->getMetadata( $key );
102 $this->initializePathInfo( $name,
103 $this->getRealPath ( $metadata['us_path'] ),
104 $metadata['us_size'],
105 false
106 );
107
108 $this->mFileKey = $key;
109 $this->mVirtualTempPath = $metadata['us_path'];
110 $this->mFileProps = $this->stash->getFileProps( $key );
111 $this->mSourceType = $metadata['us_source_type'];
112 }
113
114 /**
115 * @param $request WebRequest
116 */
117 public function initializeFromRequest( &$request ) {
118 // sends wpSessionKey as a default when wpFileKey is missing
119 $fileKey = $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) );
120
121 // chooses one of wpDestFile, wpUploadFile, filename in that order.
122 $desiredDestName = $request->getText( 'wpDestFile', $request->getText( 'wpUploadFile', $request->getText( 'filename' ) ) );
123
124 $this->initialize( $fileKey, $desiredDestName );
125 }
126
127 /**
128 * @return string
129 */
130 public function getSourceType() {
131 return $this->mSourceType;
132 }
133
134 /**
135 * File has been previously verified so no need to do so again.
136 *
137 * @return bool
138 */
139 protected function verifyFile() {
140 return true;
141 }
142
143 /**
144 * Stash the file.
145 *
146 * @return UploadStashFile
147 */
148 public function stashFile() {
149 // replace mLocalFile with an instance of UploadStashFile, which adds some methods
150 // that are useful for stashed files.
151 $this->mLocalFile = parent::stashFile();
152 return $this->mLocalFile;
153 }
154
155 /**
156 * This should return the key instead of the UploadStashFile instance, for backward compatibility.
157 * @return String
158 */
159 public function stashSession() {
160 return $this->stashFile()->getFileKey();
161 }
162
163 /**
164 * Remove a temporarily kept file stashed by saveTempUploadedFile().
165 * @return bool success
166 */
167 public function unsaveUploadedFile() {
168 return $this->stash->removeFile( $this->mFileKey );
169 }
170
171 /**
172 * Perform the upload, then remove the database record afterward.
173 * @param $comment string
174 * @param $pageText string
175 * @param $watch bool
176 * @param $user User
177 * @return Status
178 */
179 public function performUpload( $comment, $pageText, $watch, $user ) {
180 $rv = parent::performUpload( $comment, $pageText, $watch, $user );
181 $this->unsaveUploadedFile();
182 return $rv;
183 }
184 }