* @author Michael Dale
*/
class UploadFromUrl extends UploadBase {
- protected $mAsync, $mUrl;
- protected $mIgnoreWarnings = true;
+ protected $mUrl;
protected $mTempPath, $mTmpHandle;
- protected static $allowedUrls = array();
+ protected static $allowedUrls = [];
/**
* Checks if the user is allowed to use the upload-by-URL feature. If the
public static function isAllowedUrl( $url ) {
if ( !isset( self::$allowedUrls[$url] ) ) {
$allowed = true;
- Hooks::run( 'IsUploadAllowedFromUrl', array( $url, &$allowed ) );
+ Hooks::run( 'IsUploadAllowedFromUrl', [ $url, &$allowed ] );
self::$allowedUrls[$url] = $allowed;
}
*
* @param string $name
* @param string $url
- * @param bool|string $async Whether the download should be performed
- * asynchronous. False for synchronous, async or async-leavemessage for
- * asynchronous download.
* @throws MWException
*/
- public function initialize( $name, $url, $async = false ) {
- global $wgAllowAsyncCopyUploads;
-
+ public function initialize( $name, $url ) {
$this->mUrl = $url;
- $this->mAsync = $wgAllowAsyncCopyUploads ? $async : false;
- if ( $async ) {
- throw new MWException( 'Asynchronous copy uploads are no longer possible as of r81612.' );
- }
- $tempPath = $this->mAsync ? null : $this->makeTemporaryFile();
+ $tempPath = $this->makeTemporaryFile();
# File size and removeTempFile will be filled in later
$this->initializePathInfo( $name, $tempPath, 0, false );
}
}
/**
- * Download the file (if not async)
+ * Download the file
*
- * @param array $httpOptions Array of options for MWHttpRequest. Ignored if async.
+ * @param array $httpOptions Array of options for MWHttpRequest.
* This could be used to override the timeout on the http request.
* @return Status
*/
- public function fetchFile( $httpOptions = array() ) {
+ public function fetchFile( $httpOptions = [] ) {
if ( !Http::isValidURI( $this->mUrl ) ) {
return Status::newFatal( 'http-invalid-url', $this->mUrl );
}
if ( !self::isAllowedUrl( $this->mUrl ) ) {
return Status::newFatal( 'upload-copy-upload-invalid-url' );
}
- if ( !$this->mAsync ) {
- return $this->reallyFetchFile( $httpOptions );
- }
-
- return Status::newGood();
+ return $this->reallyFetchFile( $httpOptions );
}
/**
* @param array $httpOptions Array of options for MWHttpRequest
* @return Status
*/
- protected function reallyFetchFile( $httpOptions = array() ) {
+ protected function reallyFetchFile( $httpOptions = [] ) {
global $wgCopyUploadProxy, $wgCopyUploadTimeout;
if ( $this->mTempPath === false ) {
return Status::newFatal( 'tmp-create-error' );
$this->mRemoveTempFile = true;
$this->mFileSize = 0;
- $options = $httpOptions + array( 'followRedirects' => true );
+ $options = $httpOptions + [ 'followRedirects' => true ];
if ( $wgCopyUploadProxy !== false ) {
$options['proxy'] = $wgCopyUploadProxy;
'<' . implode( ',', array_keys( array_filter( $options ) ) ) . '>'
);
$req = MWHttpRequest::factory( $this->mUrl, $options, __METHOD__ );
- $req->setCallback( array( $this, 'saveTempFileChunk' ) );
+ $req->setCallback( [ $this, 'saveTempFileChunk' ] );
$status = $req->execute();
if ( $this->mTmpHandle ) {
}
wfDebugLog( 'fileupload', $status );
- if ( $status->isOk() ) {
+ if ( $status->isOK() ) {
wfDebugLog( 'fileupload', 'Download by URL completed successfuly.' );
} else {
wfDebugLog(
return $status;
}
-
- /**
- * Wrapper around the parent function in order to defer verifying the
- * upload until the file really has been fetched.
- * @return array|mixed
- */
- public function verifyUpload() {
- if ( $this->mAsync ) {
- return array( 'status' => UploadBase::OK );
- }
-
- return parent::verifyUpload();
- }
-
- /**
- * Wrapper around the parent function in order to defer checking warnings
- * until the file really has been fetched.
- * @return array
- */
- public function checkWarnings() {
- if ( $this->mAsync ) {
- $this->mIgnoreWarnings = false;
-
- return array();
- }
-
- return parent::checkWarnings();
- }
-
- /**
- * Wrapper around the parent function in order to defer checking protection
- * until we are sure that the file can actually be uploaded
- * @param User $user
- * @return bool|mixed
- */
- public function verifyTitlePermissions( $user ) {
- if ( $this->mAsync ) {
- return true;
- }
-
- return parent::verifyTitlePermissions( $user );
- }
-
- /**
- * Wrapper around the parent function in order to defer uploading to the
- * job queue for asynchronous uploads
- * @param string $comment
- * @param string $pageText
- * @param bool $watch
- * @param User $user
- * @return Status
- */
- public function performUpload( $comment, $pageText, $watch, $user ) {
- if ( $this->mAsync ) {
- $sessionKey = $this->insertJob( $comment, $pageText, $watch, $user );
-
- return Status::newFatal( 'async', $sessionKey );
- }
-
- return parent::performUpload( $comment, $pageText, $watch, $user );
- }
-
- /**
- * @param string $comment
- * @param string $pageText
- * @param bool $watch
- * @param User $user
- * @return string
- */
- protected function insertJob( $comment, $pageText, $watch, $user ) {
- $sessionKey = $this->stashSession();
- $job = new UploadFromUrlJob( $this->getTitle(), array(
- 'url' => $this->mUrl,
- 'comment' => $comment,
- 'pageText' => $pageText,
- 'watch' => $watch,
- 'userName' => $user->getName(),
- 'leaveMessage' => $this->mAsync == 'async-leavemessage',
- 'ignoreWarnings' => $this->mIgnoreWarnings,
- 'sessionId' => MediaWiki\Session\SessionManager::getGlobalSession()->getId(),
- 'sessionKey' => $sessionKey,
- ) );
- $job->initializeSessionData();
- JobQueueGroup::singleton()->push( $job );
-
- return $sessionKey;
- }
}