(bug 19195) Make user IDs more readily available with the API
[lhc/web/wiklou.git] / includes / job / UploadFromUrlJob.php
1 <?php
2 /**
3 * Job for asynchronous upload-by-url.
4 *
5 * @file
6 * @ingroup JobQueue
7 */
8
9 /**
10 * Job for asynchronous upload-by-url.
11 *
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.
15 *
16 * @ingroup JobQueue
17 */
18 class UploadFromUrlJob extends Job {
19 const SESSION_KEYNAME = 'wsUploadFromUrlJobData';
20
21 /**
22 * @var UploadFromUrl
23 */
24 public $upload;
25
26 /**
27 * @var User
28 */
29 protected $user;
30
31 public function __construct( $title, $params, $id = 0 ) {
32 parent::__construct( 'uploadFromUrl', $title, $params, $id );
33 }
34
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
42 );
43 $this->user = User::newFromName( $this->params['userName'] );
44
45 # Fetch the file
46 $status = $this->upload->fetchFile();
47 if ( !$status->isOk() ) {
48 $this->leaveMessage( $status );
49 return true;
50 }
51
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;
58 }
59
60 # Check warnings
61 if ( !$this->params['ignoreWarnings'] ) {
62 $warnings = $this->upload->checkWarnings();
63 if ( $warnings ) {
64
65 # Stash the upload
66 $key = $this->upload->stashFile();
67
68 if ( $this->params['leaveMessage'] ) {
69 $this->user->leaveUserMessage(
70 wfMsg( 'upload-warning-subj' ),
71 wfMsg( 'upload-warning-msg',
72 $key,
73 $this->params['url'] )
74 );
75 } else {
76 wfSetupSession( $this->params['sessionId'] );
77 $this->storeResultInSession( 'Warning',
78 'warnings', $warnings );
79 session_write_close();
80 }
81
82 return true;
83 }
84 }
85
86 # Perform the upload
87 $status = $this->upload->performUpload(
88 $this->params['comment'],
89 $this->params['pageText'],
90 $this->params['watch'],
91 $this->user
92 );
93 $this->leaveMessage( $status );
94 return true;
95
96 }
97
98 /**
99 * Leave a message on the user talk page or in the session according to
100 * $params['leaveMessage'].
101 *
102 * @param $status Status
103 */
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 ) );
118 }
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() );
127 }
128 session_write_close();
129 }
130 }
131
132 /**
133 * Store a result in the session data. Note that the caller is responsible
134 * for appropriate session_start and session_write_close calls.
135 *
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
139 */
140 protected function storeResultInSession( $result, $dataKey, $dataValue ) {
141 $session =& self::getSessionData( $this->params['sessionKey'] );
142 $session['result'] = $result;
143 $session[$dataKey] = $dataValue;
144 }
145
146 /**
147 * Initialize the session data. Sets the intial result to queued.
148 */
149 public function initializeSessionData() {
150 $session =& self::getSessionData( $this->params['sessionKey'] );
151 $$session['result'] = 'Queued';
152 }
153
154 /**
155 * @param $key
156 * @return mixed
157 */
158 public static function &getSessionData( $key ) {
159 if ( !isset( $_SESSION[self::SESSION_KEYNAME][$key] ) ) {
160 $_SESSION[self::SESSION_KEYNAME][$key] = array();
161 }
162 return $_SESSION[self::SESSION_KEYNAME][$key];
163 }
164 }