follow-up r61355 a bit more, still have several bits to fix (e.g. json handling)
[lhc/web/wiklou.git] / includes / upload / UploadFromChunks.php
1 <?php
2 /**
3 * @file
4 * @ingroup upload
5 *
6 * First, destination checks are made, and, if ignorewarnings is not
7 * checked, errors / warning is returned.
8 *
9 * 1. We return the uploadUrl.
10 * 2. We then accept chunk uploads from the client.
11 * 3. Return chunk id on each POSTED chunk.
12 * 4. Once the client posts "done=1", the files are concatenated together.
13 *
14 * (More info at: http://firefogg.org/dev/chunk_post.html)
15 */
16 class UploadFromChunks extends UploadBase {
17
18 const INIT = 1;
19 const CHUNK = 2;
20 const DONE = 3;
21
22 protected $chunkMode; // INIT, CHUNK, DONE
23 protected $sessionKey;
24 protected $comment;
25 protected $fileSize = 0;
26 protected $repoPath;
27 protected $pageText;
28 protected $watch;
29
30 public $status;
31
32 // Parent class requires this function even though it is only
33 // used from SpecialUpload.php and we don't do chunked uploading
34 // from SpecialUpload -- best to raise an exception for
35 // now.
36 public function initializeFromRequest( &$request ) {
37 throw new MWException( 'not implemented' );
38 }
39
40 public function initialize( $done, $filename, $sessionKey, $path,
41 $fileSize, $sessionData )
42 {
43 $this->status = new Status;
44
45 $this->initFromSessionKey( $sessionKey, $sessionData );
46
47 if ( !$this->sessionKey && !$done ) {
48 // session key not set, init the chunk upload system:
49 $this->chunkMode = self::INIT;
50 $this->mDesiredDestName = $filename;
51 } else if ( $this->sessionKey && !$done ) {
52 $this->chunkMode = self::CHUNK;
53 } else if ( $this->sessionKey && $done ) {
54 $this->chunkMode = self::DONE;
55 }
56 if ( $this->chunkMode == self::CHUNK || $this->chunkMode == self::DONE ) {
57 $this->mTempPath = $path;
58 $this->fileSize += $fileSize;
59 }
60 }
61
62 /**
63 * Set session information for chunked uploads and allocate a unique key.
64 * @param $comment string
65 * @param $pageText string
66 * @param $watch boolean
67 *
68 * @returns string the session key for this chunked upload
69 */
70 protected function setupChunkSession( $comment, $pageText, $watch ) {
71 $this->sessionKey = $this->getSessionKey();
72 $_SESSION['wsUploadData'][$this->sessionKey] = array(
73 'comment' => $comment,
74 'pageText' => $pageText,
75 'watch' => $watch,
76 'mFilteredName' => $this->mFilteredName,
77 'repoPath' => null,
78 'mDesiredDestName' => $this->mDesiredDestName,
79 'version' => self::SESSION_VERSION,
80 );
81 return $this->sessionKey;
82 }
83
84 /**
85 * Initialize a continuation of a chunked upload from a session key
86 * @param $sessionKey string
87 * @param $request WebRequest
88 *
89 * @returns void
90 */
91 protected function initFromSessionKey( $sessionKey, $sessionData ) {
92 // testing against null because we don't want to cause obscure
93 // bugs when $sessionKey is full of "0"
94 if ( $sessionKey === null ) {
95 return;
96 }
97 $this->sessionKey = $sessionKey;
98
99 if ( isset( $sessionData[$this->sessionKey]['version'] )
100 && $sessionData[$this->sessionKey]['version'] == self::SESSION_VERSION )
101 {
102 $this->comment = $sessionData[$this->sessionKey]['comment'];
103 $this->pageText = $sessionData[$this->sessionKey]['pageText'];
104 $this->watch = $sessionData[$this->sessionKey]['watch'];
105 $this->mFilteredName = $sessionData[$this->sessionKey]['mFilteredName'];
106 $this->repoPath = $sessionData[$this->sessionKey]['repoPath'];
107 $this->mDesiredDestName = $sessionData[$this->sessionKey]['mDesiredDestName'];
108 } else {
109 $this->status = Status::newFatal( 'invalid-session-key' );
110 }
111 }
112
113 /**
114 * Handle a chunk of the upload. Overrides the parent method
115 * because Chunked Uploading clients (i.e. Firefogg) require
116 * specific API responses.
117 * @see UploadBase::performUpload
118 */
119 public function performUpload( $comment, $pageText, $watch, $user ) {
120 wfDebug( "\n\n\performUpload(chunked): comment:" . $comment . ' pageText: ' . $pageText . ' watch:' . $watch );
121 global $wgUser, $wgOut;
122
123 if ( $this->chunkMode == self::INIT ) {
124 // firefogg expects a specific result per:
125 // http://www.firefogg.org/dev/chunk_post.html
126
127 // it's okay to return the token here because
128 // a) the user must have requested the token to get here and
129 // b) should only happen over POST
130 // c) we need the token to validate chunks are coming from a non-xss request
131 $token = urlencode( $wgUser->editToken() );
132 echo FormatJson::encode( array(
133 'uploadUrl' => wfExpandUrl( wfScript( 'api' ) ) . "?action=upload&" .
134 "token={$token}&format=json&enablechunks=true&chunksessionkey=" .
135 $this->setupChunkSession( $comment, $pageText, $watch ) ) );
136 $wgOut->disable();
137 } else if ( $this->chunkMode == self::CHUNK ) {
138 $status = $this->appendChunk();
139 if ( !$status->isOK() ) {
140 return $status;
141 }
142 // return success:
143 // firefogg expects a specific result
144 // http://www.firefogg.org/dev/chunk_post.html
145 echo FormatJson::encode(
146 array( 'result' => 1, 'filesize' => $this->fileSize )
147 );
148 $wgOut->disable();
149 } else if ( $this->chunkMode == self::DONE ) {
150 if ( !$comment )
151 $comment = $this->comment;
152
153 if ( !$pageText )
154 $pageText = $this->pageText;
155
156 if ( !$watch )
157 $watch = $this->watch;
158
159 $status = parent::performUpload( $comment, $pageText, $watch, $user );
160 if ( !$status->isGood() ) {
161 return $status;
162 }
163 $file = $this->getLocalFile();
164
165 // firefogg expects a specific result
166 // http://www.firefogg.org/dev/chunk_post.html
167 echo FormatJson::encode( array(
168 'result' => 1,
169 'done' => 1,
170 'resultUrl' => $file->getDescriptionUrl() )
171 );
172 $wgOut->disable();
173 }
174
175 return Status::newGood();
176 }
177
178 /**
179 * Append a chunk to the Repo file
180 *
181 * @param string $srcPath Path to file to append from
182 * @param string $toAppendPath Path to file to append to
183 * @return Status Status
184 */
185 protected function appendToUploadFile( $srcPath, $toAppendPath ) {
186 $repo = RepoGroup::singleton()->getLocalRepo();
187 $status = $repo->append( $srcPath, $toAppendPath );
188 return $status;
189 }
190
191 /**
192 * Append a chunk to the temporary file.
193 *
194 * @return void
195 */
196 protected function appendChunk() {
197 global $wgMaxUploadSize;
198
199 if ( !$this->repoPath ) {
200 $this->status = $this->saveTempUploadedFile( $this->mDesiredDestName, $this->mTempPath );
201
202 if ( $status->isOK() ) {
203 $this->repoPath = $status->value;
204 $_SESSION['wsUploadData'][$this->sessionKey]['repoPath'] = $this->repoPath;
205 }
206 return $status;
207 }
208 if ( $this->getRealPath( $this->repoPath ) ) {
209 $this->status = $this->appendToUploadFile( $this->repoPath, $this->mTempPath );
210 } else {
211 $this->status = Status::newFatal( 'filenotfound', $this->repoPath );
212 }
213 if ( $this->fileSize > $wgMaxUploadSize )
214 $this->status = Status::newFatal( 'largefileserver' );
215 }
216
217 public function verifyUpload() {
218 if ( $this->chunkMode != self::DONE ) {
219 return array('status' => UploadBase::OK);
220 }
221 return parent::verifyUpload();
222 }
223
224 public function checkWarnings() {
225 if ( $this->chunkMode != self::DONE ) {
226 return null;
227 }
228 return parent::checkWarnings();
229 }
230
231 public function getImageInfo( $result ) {
232 if ( $this->chunkMode != self::DONE ) {
233 return null;
234 }
235 return parent::getImageInfo( $result );
236 }
237 }