(bug 35565) Special:Log/patrol doesn't indicate whether patrolling was automatic
[mediawiki.git] / includes / upload / UploadFromChunks.php
blob11780e43c23e0a0e9ee6dc55f040fde86f92af2d
1 <?php
2 /**
3 * Implements uploading from chunks
5 * @ingroup Upload
6 * @author Michael Dale
7 */
8 class UploadFromChunks extends UploadFromFile {
9 protected $mOffset, $mChunkIndex, $mFileKey, $mVirtualTempPath;
11 /**
12 * Setup local pointers to stash, repo and user ( similar to UploadFromStash )
14 * @param $user User
15 * @param $stash UploadStash
16 * @param $repo FileRepo
18 public function __construct( $user = false, $stash = false, $repo = false ) {
19 // user object. sometimes this won't exist, as when running from cron.
20 $this->user = $user;
22 if( $repo ) {
23 $this->repo = $repo;
24 } else {
25 $this->repo = RepoGroup::singleton()->getLocalRepo();
28 if( $stash ) {
29 $this->stash = $stash;
30 } else {
31 if( $user ) {
32 wfDebug( __METHOD__ . " creating new UploadFromChunks instance for " . $user->getId() . "\n" );
33 } else {
34 wfDebug( __METHOD__ . " creating new UploadFromChunks instance with no user\n" );
36 $this->stash = new UploadStash( $this->repo, $this->user );
39 return true;
41 /**
42 * Calls the parent stashFile and updates the uploadsession table to handle "chunks"
44 * @return UploadStashFile stashed file
46 public function stashFile() {
47 // Stash file is the called on creating a new chunk session:
48 $this->mChunkIndex = 0;
49 $this->mOffset = 0;
50 // Create a local stash target
51 $this->mLocalFile = parent::stashFile();
52 // Update the initial file offset ( based on file size )
53 $this->mOffset = $this->mLocalFile->getSize();
54 $this->mFileKey = $this->mLocalFile->getFileKey();
56 // Output a copy of this first to chunk 0 location:
57 $status = $this->outputChunk( $this->mLocalFile->getPath() );
59 // Update db table to reflect initial "chunk" state
60 $this->updateChunkStatus();
61 return $this->mLocalFile;
64 /**
65 * Continue chunk uploading
66 */
67 public function continueChunks( $name, $key, $webRequestUpload ) {
68 $this->mFileKey = $key;
69 $this->mUpload = $webRequestUpload;
70 // Get the chunk status form the db:
71 $this->getChunkStatus();
73 $metadata = $this->stash->getMetadata( $key );
74 $this->initializePathInfo( $name,
75 $this->getRealPath( $metadata['us_path'] ),
76 $metadata['us_size'],
77 false
81 /**
82 * Append the final chunk and ready file for parent::performUpload()
83 * @return FileRepoStatus
85 public function concatenateChunks() {
86 wfDebug( __METHOD__ . " concatenate {$this->mChunkIndex} chunks:" .
87 $this->getOffset() . ' inx:' . $this->getChunkIndex() . "\n" );
89 // Concatenate all the chunks to mVirtualTempPath
90 $fileList = Array();
91 // The first chunk is stored at the mVirtualTempPath path so we start on "chunk 1"
92 for( $i = 0; $i <= $this->getChunkIndex(); $i++ ){
93 $fileList[] = $this->getVirtualChunkLocation( $i );
96 // Get the file extension from the last chunk
97 $ext = FileBackend::extensionFromPath( $this->mVirtualTempPath );
98 // Get a 0-byte temp file to perform the concatenation at
99 $tmpFile = TempFSFile::factory( 'chunkedupload_', $ext );
100 $tmpPath = $tmpFile
101 ? $tmpFile->getPath()
102 : false; // fail in concatenate()
103 // Concatenate the chunks at the temp file
104 $status = $this->repo->concatenate( $fileList, $tmpPath, FileRepo::DELETE_SOURCE );
105 if( !$status->isOk() ){
106 return $status;
108 // Update the mTempPath and mLocalFile
109 // ( for FileUpload or normal Stash to take over )
110 $this->mTempPath = $tmpPath; // file system path
111 $this->mLocalFile = parent::stashFile();
113 return $status;
117 * Perform the upload, then remove the temp copy afterward
118 * @param $comment string
119 * @param $pageText string
120 * @param $watch bool
121 * @param $user User
122 * @return Status
124 public function performUpload( $comment, $pageText, $watch, $user ) {
125 $rv = parent::performUpload( $comment, $pageText, $watch, $user );
126 return $rv;
130 * Returns the virtual chunk location:
131 * @param $index
132 * @return string
134 function getVirtualChunkLocation( $index ){
135 return $this->repo->getVirtualUrl( 'temp' ) .
136 '/' .
137 $this->repo->getHashPath(
138 $this->getChunkFileKey( $index )
139 ) .
140 $this->getChunkFileKey( $index );
144 * Add a chunk to the temporary directory
146 * @param $chunkPath string path to temporary chunk file
147 * @param $chunkSize int size of the current chunk
148 * @param $offset int offset of current chunk ( mutch match database chunk offset )
149 * @return Status
151 public function addChunk( $chunkPath, $chunkSize, $offset ) {
152 // Get the offset before we add the chunk to the file system
153 $preAppendOffset = $this->getOffset();
155 if ( $preAppendOffset + $chunkSize > $this->getMaxUploadSize()) {
156 $status = Status::newFatal( 'file-too-large' );
157 } else {
158 // Make sure the client is uploading the correct chunk with a matching offset.
159 if ( $preAppendOffset == $offset ) {
160 // Update local chunk index for the current chunk
161 $this->mChunkIndex++;
162 $status = $this->outputChunk( $chunkPath );
163 if( $status->isGood() ){
164 // Update local offset:
165 $this->mOffset = $preAppendOffset + $chunkSize;
166 // Update chunk table status db
167 $this->updateChunkStatus();
169 } else {
170 $status = Status::newFatal( 'invalid-chunk-offset' );
173 return $status;
177 * Update the chunk db table with the current status:
179 private function updateChunkStatus(){
180 wfDebug( __METHOD__ . " update chunk status for {$this->mFileKey} offset:" .
181 $this->getOffset() . ' inx:' . $this->getChunkIndex() . "\n" );
183 $dbw = $this->repo->getMasterDb();
184 $dbw->update(
185 'uploadstash',
186 array(
187 'us_status' => 'chunks',
188 'us_chunk_inx' => $this->getChunkIndex(),
189 'us_size' => $this->getOffset()
191 array( 'us_key' => $this->mFileKey ),
192 __METHOD__
196 * Get the chunk db state and populate update relevant local values
198 private function getChunkStatus(){
199 // get Master db to avoid race conditions.
200 // Otherwise, if chunk upload time < replag there will be spurious errors
201 $dbw = $this->repo->getMasterDb();
202 $row = $dbw->selectRow(
203 'uploadstash',
204 array(
205 'us_chunk_inx',
206 'us_size',
207 'us_path',
209 array( 'us_key' => $this->mFileKey ),
210 __METHOD__
212 // Handle result:
213 if ( $row ) {
214 $this->mChunkIndex = $row->us_chunk_inx;
215 $this->mOffset = $row->us_size;
216 $this->mVirtualTempPath = $row->us_path;
220 * Get the current Chunk index
221 * @return Integer index of the current chunk
223 private function getChunkIndex(){
224 if( $this->mChunkIndex !== null ){
225 return $this->mChunkIndex;
227 return 0;
231 * Gets the current offset in fromt the stashedupload table
232 * @return Integer current byte offset of the chunk file set
234 private function getOffset(){
235 if ( $this->mOffset !== null ){
236 return $this->mOffset;
238 return 0;
242 * Output the chunk to disk
244 * @param $chunkPath string
245 * @return FileRepoStatus
247 private function outputChunk( $chunkPath ){
248 // Key is fileKey + chunk index
249 $fileKey = $this->getChunkFileKey();
251 // Store the chunk per its indexed fileKey:
252 $hashPath = $this->repo->getHashPath( $fileKey );
253 $storeStatus = $this->repo->store( $chunkPath, 'temp', "$hashPath$fileKey" );
255 // Check for error in stashing the chunk:
256 if ( ! $storeStatus->isOK() ) {
257 $error = $storeStatus->getErrorsArray();
258 $error = reset( $error );
259 if ( ! count( $error ) ) {
260 $error = $storeStatus->getWarningsArray();
261 $error = reset( $error );
262 if ( ! count( $error ) ) {
263 $error = array( 'unknown', 'no error recorded' );
266 throw new UploadChunkFileException( "error storing file in '$chunkPath': " . implode( '; ', $error ) );
268 return $storeStatus;
270 private function getChunkFileKey( $index = null ){
271 if( $index === null ){
272 $index = $this->getChunkIndex();
274 return $this->mFileKey . '.' . $index ;
278 class UploadChunkZeroLengthFileException extends MWException {};
279 class UploadChunkFileException extends MWException {};