d5225b1113524c13ab27058ec75f69d42070a293
[lhc/web/wiklou.git] / includes / job / UploadFromUrlJob.php
1 <?php
2
3 /**
4 * Job for asynchronous upload-by-url.
5 *
6 * This job is in fact an interface to UploadFromUrl, which is designed such
7 * that it does not require any globals. If it does, fix it elsewhere, do not
8 * add globals in here.
9 *
10 * @ingroup JobQueue
11 */
12 class UploadFromUrlJob extends Job {
13 public $upload;
14 protected $user;
15
16 public function __construct( $title, $params, $id = 0 ) {
17 parent::__construct( 'uploadFromUrl', $title, $params, $id );
18 }
19
20 public function run() {
21 # Initialize this object and the upload object
22 $this->upload = new UploadFromUrl();
23 $this->upload->initialize(
24 $this->title->getText(),
25 $this->params['url'],
26 false
27 );
28 $this->user = User::newFromName( $this->params['userName'] );
29
30 # Fetch the file
31 $status = $this->upload->fetchFile();
32 if ( !$status->isOk() ) {
33 $this->leaveMessage( $status );
34 return;
35 }
36
37 # Verify upload
38 $result = $this->upload->verifyUpload();
39 if ( $result['status'] != UploadBase::OK ) {
40 $status = $this->upload->convertVerifyErrorToStatus( $result );
41 $this->leaveMessage( $status );
42 return;
43 }
44
45 # Check warnings
46 if ( !$this->params['ignoreWarnings'] ) {
47 $warnings = $this->upload->checkWarnings();
48 if ( $warnings ) {
49 if ( $this->params['leaveMessage'] ) {
50 $this->user->leaveUserMessage(
51 wfMsg( 'upload-warning-subj' ),
52 wfMsg( 'upload-warning-msg',
53 $this->params['sessionKey'],
54 $this->params['url'] )
55 );
56 } else {
57 $this->storeResultInSession( 'Warning',
58 'warnings', $warnings );
59 }
60
61 // FIXME: stash in session
62 return;
63 }
64 }
65
66 # Perform the upload
67 $status = $this->upload->performUpload(
68 $this->params['comment'],
69 $this->params['pageText'],
70 $this->params['watch'],
71 $this->user
72 );
73 $this->leaveMessage( $status );
74 }
75
76 /**
77 * Leave a message on the user talk page or in the session according to
78 * $params['leaveMessage'].
79 *
80 * @param $status Status
81 */
82 protected function leaveMessage( $status ) {
83 if ( $this->params['leaveMessage'] ) {
84 if ( $status->isGood() ) {
85 $this->user->leaveUserMessage( wfMsg( 'upload-success-subj' ),
86 wfMsg( 'upload-success-msg',
87 $this->upload->getTitle()->getText(),
88 $this->params['url']
89 ) );
90 } else {
91 $this->user->leaveUserMessage( wfMsg( 'upload-failure-subj' ),
92 wfMsg( 'upload-failure-msg',
93 $status->getWikiText(),
94 $this->params['url']
95 ) );
96 }
97 } else {
98 if ( $status->isOk() ) {
99 $this->storeResultInSession( 'Success',
100 'filename', $this->getLocalFile()->getName() );
101 } else {
102 $this->storeResultInSession( 'Failure',
103 'errors', $status->getErrorsArray() );
104 }
105
106 }
107 }
108
109 /**
110 * Store a result in the session data
111 * THIS IS BROKEN. $_SESSION does not exist when using runJobs.php
112 *
113 * @param $result string The result (Success|Warning|Failure)
114 * @param $dataKey string The key of the extra data
115 * @param $dataKey mixed The extra data itself
116 */
117 protected function storeResultInSession( $result, $dataKey, $dataValue ) {
118 $session &= $_SESSION[UploadBase::getSessionKeyname()][$this->params['sessionKey']];
119 $session['result'] = $result;
120 $session[$dataKey] = $dataValue;
121 }
122 }