// Parameter handling
$this->mParams = $this->extractRequestParams();
$request = $this->getMain()->getRequest();
+ // Check if async mode is actually supported
+ $this->mParams['async'] = ( $this->mParams['async'] && !wfIsWindows() );
+ $this->mParams['async'] = false; // XXX: disabled per bug 44080
// 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() );
}
* Get an uplaod result based on upload context
* @return array
*/
- private function getContextResult(){
+ private function getContextResult() {
$warnings = $this->getApiWarnings();
if ( $warnings && !$this->mParams['ignorewarnings'] ) {
// Get warnings formated in result array format
// 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 array
*/
- private function getStashResult( $warnings ){
+ private function getStashResult( $warnings ) {
$result = array ();
// Some uploads can request they be stashed, so as not to publish them immediately.
// In this case, a failure to stash ought to be fatal
}
return $result;
}
+
/**
* Get Warnings Result
* @param $warnings array Array of Api upload warnings
* @return array
*/
- private function getWarningsResult( $warnings ){
+ private function getWarningsResult( $warnings ) {
$result = array();
$result['result'] = 'Warning';
$result['warnings'] = $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(
$cmd = wfShellWikiCmd(
"$IP/includes/upload/AssembleUploadChunks.php",
array(
+ '--wiki', wfWikiID(),
'--filename', $this->mParams['filename'],
'--filekey', $this->mParams['filekey'],
'--userid', $this->getUser()->getId(),
'--quiet'
)
) . " < " . wfGetNull() . " > " . wfGetNull() . " 2>&1 &";
- wfShellExec( $cmd, $retVal ); // start a process in the background
+ // 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 {
* @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;
if ( $this->mParams['chunk'] ) {
// Chunk upload
$this->mUpload = new UploadFromChunks();
- if( isset( $this->mParams['filekey'] ) ){
+ if( isset( $this->mParams['filekey'] ) ) {
// handle new chunk
$this->mUpload->continueChunks(
$this->mParams['filename'],
}
$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() );
- $this->dieUsage( 'An internal error occurred', 'internal-error', 0, $error );
+ 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 );
+ }
}
+ $result['result'] = 'Success';
}
- $file = $this->mUpload->getLocalFile();
-
- $result['result'] = 'Success';
$result['filename'] = $file->getName();
if ( $warnings && count( $warnings ) > 0 ) {
$result['warnings'] = $warnings;
'offset' => 'Offset of chunk in bytes',
'filesize' => 'Filesize of entire upload',
- 'async', 'Make potentially large file operations asynchronous when possible',
+ 'async' => 'Make potentially large file operations asynchronous when possible',
'asyncdownload' => 'Make fetching a URL asynchronous',
'leavemessage' => 'If asyncdownload is used, leave a message on the user talk page if finished',
'statuskey' => 'Fetch the upload status for this file key (upload by URL)',
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' ),
public function getHelpUrls() {
return 'https://www.mediawiki.org/wiki/API:Upload';
}
-
- public function getVersion() {
- return __CLASS__ . ': $Id$';
- }
}