Revert r64853 (add $wgLogAutocreatedAccounts to enable/disable account autocreation...
[mediawiki.git] / includes / job / UploadFromUrlJob.php
blob3f9152450504bfbff6cd40ddfa3d855cef3ca240
1 <?php
2 /**
3 * Job for asynchronous upload-by-url.
5 * @file
6 * @ingroup JobQueue
7 */
9 /**
10 * Job for asynchronous upload-by-url.
12 * This job is in fact an interface to UploadFromUrl, which is designed such
13 * that it does not require any globals. If it does, fix it elsewhere, do not
14 * add globals in here.
16 * @ingroup JobQueue
18 class UploadFromUrlJob extends Job {
19 const SESSION_KEYNAME = 'wsUploadFromUrlJobData';
21 /**
22 * @var UploadFromUrl
24 public $upload;
26 /**
27 * @var User
29 protected $user;
31 public function __construct( $title, $params, $id = 0 ) {
32 parent::__construct( 'uploadFromUrl', $title, $params, $id );
35 public function run() {
36 # Initialize this object and the upload object
37 $this->upload = new UploadFromUrl();
38 $this->upload->initialize(
39 $this->title->getText(),
40 $this->params['url'],
41 false
43 $this->user = User::newFromName( $this->params['userName'] );
45 # Fetch the file
46 $status = $this->upload->fetchFile();
47 if ( !$status->isOk() ) {
48 $this->leaveMessage( $status );
49 return true;
52 # Verify upload
53 $result = $this->upload->verifyUpload();
54 if ( $result['status'] != UploadBase::OK ) {
55 $status = $this->upload->convertVerifyErrorToStatus( $result );
56 $this->leaveMessage( $status );
57 return true;
60 # Check warnings
61 if ( !$this->params['ignoreWarnings'] ) {
62 $warnings = $this->upload->checkWarnings();
63 if ( $warnings ) {
64 wfSetupSession( $this->params['sessionId'] );
66 if ( $this->params['leaveMessage'] ) {
67 $this->user->leaveUserMessage(
68 wfMsg( 'upload-warning-subj' ),
69 wfMsg( 'upload-warning-msg',
70 $this->params['sessionKey'],
71 $this->params['url'] )
73 } else {
74 $this->storeResultInSession( 'Warning',
75 'warnings', $warnings );
78 # Stash the upload in the session
79 $this->upload->stashSession( $this->params['sessionKey'] );
80 session_write_close();
82 return true;
86 # Perform the upload
87 $status = $this->upload->performUpload(
88 $this->params['comment'],
89 $this->params['pageText'],
90 $this->params['watch'],
91 $this->user
93 $this->leaveMessage( $status );
94 return true;
98 /**
99 * Leave a message on the user talk page or in the session according to
100 * $params['leaveMessage'].
102 * @param $status Status
104 protected function leaveMessage( $status ) {
105 if ( $this->params['leaveMessage'] ) {
106 if ( $status->isGood() ) {
107 $this->user->leaveUserMessage( wfMsg( 'upload-success-subj' ),
108 wfMsg( 'upload-success-msg',
109 $this->upload->getTitle()->getText(),
110 $this->params['url']
111 ) );
112 } else {
113 $this->user->leaveUserMessage( wfMsg( 'upload-failure-subj' ),
114 wfMsg( 'upload-failure-msg',
115 $status->getWikiText(),
116 $this->params['url']
117 ) );
119 } else {
120 wfSetupSession( $this->params['sessionId'] );
121 if ( $status->isOk() ) {
122 $this->storeResultInSession( 'Success',
123 'filename', $this->upload->getLocalFile()->getName() );
124 } else {
125 $this->storeResultInSession( 'Failure',
126 'errors', $status->getErrorsArray() );
128 session_write_close();
133 * Store a result in the session data. Note that the caller is responsible
134 * for appropriate session_start and session_write_close calls.
136 * @param $result String: the result (Success|Warning|Failure)
137 * @param $dataKey String: the key of the extra data
138 * @param $dataValue Mixed: the extra data itself
140 protected function storeResultInSession( $result, $dataKey, $dataValue ) {
141 $session =& self::getSessionData( $this->params['sessionKey'] );
142 $session['result'] = $result;
143 $session[$dataKey] = $dataValue;
147 * Initialize the session data. Sets the intial result to queued.
149 public function initializeSessionData() {
150 $session =& self::getSessionData( $this->params['sessionKey'] );
151 $$session['result'] = 'Queued';
154 public static function &getSessionData( $key ) {
155 if ( !isset( $_SESSION[self::SESSION_KEYNAME][$key] ) ) {
156 $_SESSION[self::SESSION_KEYNAME][$key] = array();
158 return $_SESSION[self::SESSION_KEYNAME][$key];