Merge "Revert "Adding ability of jQuery badge to display the number zero if requested.""
[lhc/web/wiklou.git] / includes / upload / AssembleUploadChunks.php
1 <?php
2 /**
3 * Assemble the segments of a chunked upload.
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 Maintenance
22 */
23 require_once( __DIR__ . '/../../maintenance/Maintenance.php' );
24
25 /**
26 * Assemble the segments of a chunked upload.
27 *
28 * @ingroup Maintenance
29 */
30 class AssembleUploadChunks extends Maintenance {
31 public function __construct() {
32 parent::__construct();
33 $this->mDescription = "Re-assemble the segments of a chunked upload into a single file";
34 $this->addOption( 'filename', "Desired file name", true, true );
35 $this->addOption( 'filekey', "Upload stash file key", true, true );
36 $this->addOption( 'userid', "Upload owner user ID", true, true );
37 $this->addOption( 'sessionid', "Upload owner session ID", true, true );
38 }
39
40 public function execute() {
41 $e = null;
42 wfDebug( "Started assembly for file {$this->getOption( 'filename' )}\n" );
43 wfSetupSession( $this->getOption( 'sessionid' ) );
44 try {
45 $user = User::newFromId( $this->getOption( 'userid' ) );
46 if ( !$user ) {
47 throw new MWException( "No user with ID " . $this->getOption( 'userid' ) . "." );
48 }
49
50 UploadBase::setSessionStatus(
51 $this->getOption( 'filekey' ),
52 array( 'result' => 'Poll', 'stage' => 'assembling', 'status' => Status::newGood() )
53 );
54
55 $upload = new UploadFromChunks( $user );
56 $upload->continueChunks(
57 $this->getOption( 'filename' ),
58 $this->getOption( 'filekey' ),
59 // @TODO: set User?
60 RequestContext::getMain()->getRequest() // dummy request
61 );
62
63 // Combine all of the chunks into a local file and upload that to a new stash file
64 $status = $upload->concatenateChunks();
65 if ( !$status->isGood() ) {
66 UploadBase::setSessionStatus(
67 $this->getOption( 'filekey' ),
68 array( 'result' => 'Failure', 'stage' => 'assembling', 'status' => $status )
69 );
70 session_write_close();
71 $this->error( $status->getWikiText() . "\n", 1 ); // die
72 }
73
74 // We have a new filekey for the fully concatenated file
75 $newFileKey = $upload->getLocalFile()->getFileKey();
76
77 // Remove the old stash file row and first chunk file
78 $upload->stash->removeFileNoAuth( $this->getOption( 'filekey' ) );
79
80 // Build the image info array while we have the local reference handy
81 $apiMain = new ApiMain(); // dummy object (XXX)
82 $imageInfo = $upload->getImageInfo( $apiMain->getResult() );
83
84 // Cleanup any temporary local file
85 $upload->cleanupTempFile();
86
87 // Cache the info so the user doesn't have to wait forever to get the final info
88 UploadBase::setSessionStatus(
89 $this->getOption( 'filekey' ),
90 array(
91 'result' => 'Success',
92 'stage' => 'assembling',
93 'filekey' => $newFileKey,
94 'imageinfo' => $imageInfo,
95 'status' => Status::newGood()
96 )
97 );
98 } catch ( MWException $e ) {
99 UploadBase::setSessionStatus(
100 $this->getOption( 'filekey' ),
101 array(
102 'result' => 'Failure',
103 'stage' => 'assembling',
104 'status' => Status::newFatal( 'api-error-stashfailed' )
105 )
106 );
107 }
108 session_write_close();
109 if ( $e ) {
110 throw $e;
111 }
112 wfDebug( "Finished assembly for file {$this->getOption( 'filename' )}\n" );
113 }
114 }
115
116 $maintClass = "AssembleUploadChunks";
117 require_once( RUN_MAINTENANCE_IF_MAIN );