9b297db1d90fd75614a71bdc4e8838c2916eaa98
[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
14 public function __construct( $title, $params, $id = 0 ) {
15 parent::__construct( 'uploadFromUrl', $title, $params, $id );
16 }
17
18 public function run() {
19 # Initialize this object and the upload object
20 $upload = new UploadFromUrl();
21 $upload->initialize(
22 $this->title->getText(),
23 $this->params['url'],
24 false
25 );
26 $this->user = User::newFromName( $this->params['userName'] );
27
28 # Fetch the file
29 $status = $upload->fetchFile();
30 if ( !$status->isOk() ) {
31 $this->leaveMessage( $status );
32 return;
33 }
34
35 # Check warnings
36 if ( !$this->params['ignoreWarnings'] ) {
37 $warnings = $this->checkWarnings();
38 if ( $warnings ) {
39 if ( $this->params['leaveMessage'] ) {
40 $this->user->leaveUserMessage(
41 wfMsg( 'upload-warning-subj' ),
42 wfMsg( 'upload-warning-msg', $this->params['sessionKey'] )
43 );
44 } else {
45 $this->storeResultInSession( 'Warning',
46 'warnings', $warnings );
47 }
48 return;
49 }
50 }
51
52 # Perform the upload
53 $status = $upload->performUpload(
54 $this->params['comment'],
55 $this->params['pageText'],
56 $this->params['watch']
57 );
58 $this->leaveMessage( $status );
59 }
60
61 /**
62 * Leave a message on the user talk page or in the session according to
63 * $params['leaveMessage'].
64 *
65 * @param $status Status
66 */
67 protected function leaveMessage( $status ) {
68 if ( $this->params['leaveMessage'] ) {
69 if ( $status->isGood() ) {
70 $file = $this->getLocalFile();
71
72 $this->user->leaveUserMessage( wfMsg( 'successfulupload' ),
73 wfMsg( 'upload-success-msg', $file->getDescriptionUrl() ) );
74 } else {
75 $this->user->leaveUserMessage( wfMsg( 'upload-failure-subj' ),
76 wfMsg( 'upload-failure-msg', $status->getWikiText() ) );
77 }
78 } else {
79 if ( $status->isOk() ) {
80 $this->storeResultInSession( 'Success',
81 'filename', $this->getLocalFile()->getName() );
82 } else {
83 $this->storeResultInSession( 'Failure',
84 'errors', $status->getErrorsArray() );
85 }
86
87 }
88 }
89
90 /**
91 * Store a result in the session data
92 *
93 * @param $result string The result (Success|Warning|Failure)
94 * @param $dataKey string The key of the extra data
95 * @param $dataKey mixed The extra data itself
96 */
97 protected function storeResultInSession( $result, $dataKey, $dataValue ) {
98 $session &= $_SESSION[UploadBase::getSessionKeyname()][$this->params['sessionKey']];
99 $session['result'] = $result;
100 $session[$dataKey] = $dataValue;
101 }
102 }