// Parameter handling
$this->mParams = $this->extractRequestParams();
$request = $this->getMain()->getRequest();
+ // Check if async mode is actually supported
+ $this->mParams['async'] = ( $this->mParams['async'] && !wfIsWindows() );
// Add the uploaded file to the params array
$this->mParams['file'] = $request->getFileName( 'file' );
$this->mParams['chunk'] = $request->getFileName( 'chunk' );
// Select an upload module
if ( !$this->selectUploadModule() ) {
- // This is not a true upload, but a status request or similar
- return;
- }
- if ( !isset( $this->mUpload ) ) {
+ return; // not a true upload, but a status request or similar
+ } elseif ( !isset( $this->mUpload ) ) {
$this->dieUsage( 'No upload module set', 'nomodule' );
}
// First check permission to upload
$this->checkPermissions( $user );
- // Fetch the file
+ // Fetch the file (usually a no-op)
$status = $this->mUpload->fetchFile();
if ( !$status->isGood() ) {
$errors = $status->getErrorsArray();
if ( !$this->mUpload->getTitle() ) {
$this->dieUsage( 'Invalid file title supplied', 'internal-error' );
}
+ } elseif ( $this->mParams['async'] ) {
+ // defer verification to background process
} else {
$this->verifyUpload();
}
// Check if the user has the rights to modify or overwrite the requested title
// (This check is irrelevant if stashing is already requested, since the errors
// can always be fixed by changing the title)
- if ( ! $this->mParams['stash'] ) {
+ if ( !$this->mParams['stash'] ) {
$permErrors = $this->mUpload->verifyTitlePermissions( $user );
if ( $permErrors !== true ) {
$this->dieRecoverableError( $permErrors[0], 'filename' );
}
}
+
// Get the result based on the current upload context:
$result = $this->getContextResult();
-
if ( $result['result'] === 'Success' ) {
$result['imageinfo'] = $this->mUpload->getImageInfo( $this->getResult() );
}
// performUpload will return a formatted properly for the API with status
return $this->performUpload( $warnings );
}
+
/**
* Get Stash Result, throws an expetion if the file could not be stashed.
* @param $warnings array Array of Api upload warnings
}
return $result;
}
+
/**
* Get Warnings Result
* @param $warnings array Array of Api upload warnings
}
return $result;
}
+
/**
* Get the result of a chunk upload.
* @param $warnings array Array of Api upload warnings
if( $this->mParams['offset'] + $chunkSize == $this->mParams['filesize'] ) {
if ( $this->mParams['async'] && !wfIsWindows() ) {
$progress = UploadBase::getSessionStatus( $this->mParams['filekey'] );
- if ( $progress && $progress['result'] !== 'Failed' ) {
+ if ( $progress && $progress['result'] === 'Poll' ) {
$this->dieUsage( "Chunk assembly already in progress.", 'stashfailed' );
}
UploadBase::setSessionStatus(
* @throws MWException
* @return String file key
*/
- function performStash() {
+ private function performStash() {
try {
$stashFile = $this->mUpload->stashFile();
* @param $data array Optional extra data to pass to the user
* @throws UsageException
*/
- function dieRecoverableError( $error, $parameter, $data = array() ) {
+ private function dieRecoverableError( $error, $parameter, $data = array() ) {
try {
$data['filekey'] = $this->performStash();
$data['sessionkey'] = $data['filekey'];
'filekey', 'file', 'url', 'statuskey' );
}
+ // Status report for "upload to stash"/"upload from stash"
if ( $this->mParams['filekey'] && $this->mParams['checkstatus'] ) {
$progress = UploadBase::getSessionStatus( $this->mParams['filekey'] );
if ( !$progress ) {
} elseif ( !$progress['status']->isGood() ) {
$this->dieUsage( $progress['status']->getWikiText(), 'stashfailed' );
}
+ if ( isset( $progress['status']->value['verification'] ) ) {
+ $this->checkVerification( $progress['status']->value['verification'] );
+ }
unset( $progress['status'] ); // remove Status object
$this->getResult()->addValue( null, $this->getModuleName(), $progress );
return false;
}
$this->mUpload = new UploadFromStash( $this->getUser() );
-
- $this->mUpload->initialize( $this->mParams['filekey'], $this->mParams['filename'] );
+ // This will not download the temp file in initialize() in async mode.
+ // We still have enough information to call checkWarnings() and such.
+ $this->mUpload->initialize(
+ $this->mParams['filekey'], $this->mParams['filename'], !$this->mParams['async']
+ );
} elseif ( isset( $this->mParams['file'] ) ) {
$this->mUpload = new UploadFromFile();
$this->mUpload->initialize(
* Performs file verification, dies on error.
*/
protected function verifyUpload( ) {
- global $wgFileExtensions;
-
$verification = $this->mUpload->verifyUpload( );
if ( $verification['status'] === UploadBase::OK ) {
return;
+ } else {
+ return $this->checkVerification( $verification );
}
+ }
+
+ /**
+ * Performs file verification, dies on error.
+ */
+ protected function checkVerification( array $verification ) {
+ global $wgFileExtensions;
// TODO: Move them to ApiBase's message map
switch( $verification['status'] ) {
* @return array
*/
protected function performUpload( $warnings ) {
+ global $IP;
+
// Use comment as initial page text by default
if ( is_null( $this->mParams['text'] ) ) {
$this->mParams['text'] = $this->mParams['comment'];
}
// No errors, no warnings: do the upload
- $status = $this->mUpload->performUpload( $this->mParams['comment'],
- $this->mParams['text'], $watch, $this->getUser() );
-
- if ( !$status->isGood() ) {
- $error = $status->getErrorsArray();
-
- if ( count( $error ) == 1 && $error[0][0] == 'async' ) {
- // The upload can not be performed right now, because the user
- // requested so
- return array(
- 'result' => 'Queued',
- 'statuskey' => $error[0][1],
- );
+ if ( $this->mParams['async'] ) {
+ $progress = UploadBase::getSessionStatus( $this->mParams['filekey'] );
+ if ( $progress && $progress['result'] === 'Poll' ) {
+ $this->dieUsage( "Upload from stash already in progress.", 'publishfailed' );
+ }
+ UploadBase::setSessionStatus(
+ $this->mParams['filekey'],
+ array( 'result' => 'Poll', 'stage' => 'queued', 'status' => Status::newGood() )
+ );
+ $retVal = 1;
+ $cmd = wfShellWikiCmd(
+ "$IP/includes/upload/PublishStashedFile.php",
+ array(
+ '--wiki', wfWikiID(),
+ '--filename', $this->mParams['filename'],
+ '--filekey', $this->mParams['filekey'],
+ '--userid', $this->getUser()->getId(),
+ '--comment', $this->mParams['comment'],
+ '--text', $this->mParams['text'],
+ '--watch', $watch,
+ '--sessionid', session_id(),
+ '--quiet'
+ )
+ ) . " < " . wfGetNull() . " > " . wfGetNull() . " 2>&1 &";
+ // Start a process in the background. Enforce the time limits via PHP
+ // since ulimit4.sh seems to often not work for this particular usage.
+ wfShellExec( $cmd, $retVal, array(), array( 'time' => 0, 'memory' => 0 ) );
+ if ( $retVal == 0 ) {
+ $result['result'] = 'Poll';
} else {
- $this->getResult()->setIndexedTagName( $error, 'error' );
+ UploadBase::setSessionStatus( $this->mParams['filekey'], false );
+ $this->dieUsage(
+ "Failed to start PublishStashedFile.php", 'publishfailed' );
+ }
+ } else {
+ $status = $this->mUpload->performUpload( $this->mParams['comment'],
+ $this->mParams['text'], $watch, $this->getUser() );
+
+ if ( !$status->isGood() ) {
+ $error = $status->getErrorsArray();
+
+ if ( count( $error ) == 1 && $error[0][0] == 'async' ) {
+ // The upload can not be performed right now, because the user
+ // requested so
+ return array(
+ 'result' => 'Queued',
+ 'statuskey' => $error[0][1],
+ );
+ } else {
+ $this->getResult()->setIndexedTagName( $error, 'error' );
- $this->dieUsage( 'An internal error occurred', 'internal-error', 0, $error );
+ $this->dieUsage( 'An internal error occurred', 'internal-error', 0, $error );
+ }
}
+ $result['result'] = 'Success';
}
- $file = $this->mUpload->getLocalFile();
-
- $result['result'] = 'Success';
$result['filename'] = $file->getName();
if ( $warnings && count( $warnings ) > 0 ) {
$result['warnings'] = $warnings;
array( 'code' => 'filename-tooshort', 'info' => 'The filename is too short' ),
array( 'code' => 'overwrite', 'info' => 'Overwriting an existing file is not allowed' ),
array( 'code' => 'stashfailed', 'info' => 'Stashing temporary file failed' ),
+ array( 'code' => 'publishfailed', 'info' => 'Publishing of stashed file failed' ),
array( 'code' => 'internal-error', 'info' => 'An internal error occurred' ),
array( 'code' => 'asynccopyuploaddisabled', 'info' => 'Asynchronous copy uploads disabled' ),
array( 'fileexists-forbidden' ),
--- /dev/null
+<?php
+/**
+ * Upload a file from the upload stash into the local file repo.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup Maintenance
+ */
+require_once( __DIR__ . '/../../maintenance/Maintenance.php' );
+set_time_limit( 3600 ); // 1 hour
+
+/**
+ * Upload a file from the upload stash into the local file repo.
+ *
+ * @ingroup Maintenance
+ */
+class PublishStashedFile extends Maintenance {
+ public function __construct() {
+ parent::__construct();
+ $this->mDescription = "Upload stashed file into the local file repo";
+ $this->addOption( 'filename', "Desired file name", true, true );
+ $this->addOption( 'filekey', "Upload stash file key", true, true );
+ $this->addOption( 'userid', "Upload owner user ID", true, true );
+ $this->addOption( 'comment', "Upload comment", true, true );
+ $this->addOption( 'text', "Upload description", true, true );
+ $this->addOption( 'watch', "Whether the uploader should watch the page", true, true );
+ $this->addOption( 'sessionid', "Upload owner session ID", true, true );
+ }
+
+ public function execute() {
+ wfSetupSession( $this->getOption( 'sessionid' ) );
+ try {
+ $user = User::newFromId( $this->getOption( 'userid' ) );
+ if ( !$user ) {
+ throw new MWException( "No user with ID " . $this->getOption( 'userid' ) . "." );
+ }
+
+ UploadBase::setSessionStatus(
+ $this->getOption( 'filekey' ),
+ array( 'result' => 'Poll', 'stage' => 'publish', 'status' => Status::newGood() )
+ );
+
+ $upload = new UploadFromStash( $user );
+ // @TODO: initialize() causes a GET, ideally we could frontload the antivirus
+ // checks and anything else to the stash stage (which includes concatenation and
+ // the local file is thus already there). That way, instead of GET+PUT, there could
+ // just be a COPY operation from the stash to the public zone.
+ $upload->initialize( $this->getOption( 'filekey' ), $this->getOption( 'filename' ) );
+
+ // Check if the local file checks out (this is generally a no-op)
+ $verification = $upload->verifyUpload();
+ if ( $verification['status'] !== UploadBase::OK ) {
+ $status = Status::newFatal( 'verification-error' );
+ $status->value = array( 'verification' => $verification );
+ UploadBase::setSessionStatus(
+ $this->getOption( 'filekey' ),
+ array( 'result' => 'Failure', 'stage' => 'publish', 'status' => $status )
+ );
+ $this->error( "Could not verify upload.\n", 1 ); // die
+ }
+
+ // Upload the stashed file to a permanent location
+ $status = $upload->performUpload(
+ $this->getOption( 'comment' ),
+ $this->getOption( 'text' ),
+ $this->getOption( 'watch' ),
+ $user
+ );
+ if ( !$status->isGood() ) {
+ UploadBase::setSessionStatus(
+ $this->getOption( 'filekey' ),
+ array( 'result' => 'Failure', 'stage' => 'publish', 'status' => $status )
+ );
+ $this->error( $status->getWikiText() . "\n", 1 ); // die
+ }
+
+ // Build the image info array while we have the local reference handy
+ $apiMain = new ApiMain(); // dummy object (XXX)
+ $imageInfo = $upload->getImageInfo( $apiMain->getResult() );
+
+ // Cleanup any temporary local file
+ $upload->cleanupTempFile();
+
+ // Cache the info so the user doesn't have to wait forever to get the final info
+ UploadBase::setSessionStatus(
+ $this->getOption( 'filekey' ),
+ array(
+ 'result' => 'Success',
+ 'stage' => 'publish',
+ 'filename' => $upload->getLocalFile()->getName(),
+ 'imageinfo' => $imageInfo,
+ 'status' => Status::newGood()
+ )
+ );
+ } catch ( MWException $e ) {
+ UploadBase::setSessionStatus(
+ $this->getOption( 'filekey' ),
+ array(
+ 'result' => 'Failure',
+ 'stage' => 'publish',
+ 'status' => Status::newFatal( 'api-error-stashfailed' )
+ )
+ );
+ throw $e;
+ }
+ session_write_close();
+ }
+}
+
+$maintClass = "PublishStashedFile";
+require_once( RUN_MAINTENANCE_IF_MAIN );