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