Fix UploadFromUrl test cases, UploadFromUrlJob::run return value, and disable broken...
[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 # Until we find a way to store data in sessions, set leaveMessage to
22 # true unconditionally
23 $this->params['leaveMessage'] = true;
24 # Similar for ignorewarnings. This is not really a good fallback, but
25 # there is no easy way to get a wikitext formatted warning message to
26 # show to the user
27 $this->params['ignoreWarnings'] = true;
28
29 # Initialize this object and the upload object
30 $this->upload = new UploadFromUrl();
31 $this->upload->initialize(
32 $this->title->getText(),
33 $this->params['url'],
34 false
35 );
36 $this->user = User::newFromName( $this->params['userName'] );
37
38 # Fetch the file
39 $status = $this->upload->fetchFile();
40 if ( !$status->isOk() ) {
41 $this->leaveMessage( $status );
42 return true;
43 }
44
45 # Verify upload
46 $result = $this->upload->verifyUpload();
47 if ( $result['status'] != UploadBase::OK ) {
48 $status = $this->upload->convertVerifyErrorToStatus( $result );
49 $this->leaveMessage( $status );
50 return true;
51 }
52
53 # Check warnings
54 if ( !$this->params['ignoreWarnings'] ) {
55 $warnings = $this->upload->checkWarnings();
56 if ( $warnings ) {
57 if ( $this->params['leaveMessage'] ) {
58 $this->user->leaveUserMessage(
59 wfMsg( 'upload-warning-subj' ),
60 wfMsg( 'upload-warning-msg',
61 $this->params['sessionKey'],
62 $this->params['url'] )
63 );
64 } else {
65 $this->storeResultInSession( 'Warning',
66 'warnings', $warnings );
67 }
68
69 // FIXME: stash in session
70 return true;
71 }
72 }
73
74 # Perform the upload
75 $status = $this->upload->performUpload(
76 $this->params['comment'],
77 $this->params['pageText'],
78 $this->params['watch'],
79 $this->user
80 );
81 $this->leaveMessage( $status );
82 return true;
83
84 }
85
86 /**
87 * Leave a message on the user talk page or in the session according to
88 * $params['leaveMessage'].
89 *
90 * @param $status Status
91 */
92 protected function leaveMessage( $status ) {
93 if ( $this->params['leaveMessage'] ) {
94 if ( $status->isGood() ) {
95 $this->user->leaveUserMessage( wfMsg( 'upload-success-subj' ),
96 wfMsg( 'upload-success-msg',
97 $this->upload->getTitle()->getText(),
98 $this->params['url']
99 ) );
100 } else {
101 $this->user->leaveUserMessage( wfMsg( 'upload-failure-subj' ),
102 wfMsg( 'upload-failure-msg',
103 $status->getWikiText(),
104 $this->params['url']
105 ) );
106 }
107 } else {
108 if ( $status->isOk() ) {
109 $this->storeResultInSession( 'Success',
110 'filename', $this->getLocalFile()->getName() );
111 } else {
112 $this->storeResultInSession( 'Failure',
113 'errors', $status->getErrorsArray() );
114 }
115
116 }
117 }
118
119 /**
120 * Store a result in the session data
121 * THIS IS BROKEN. $_SESSION does not exist when using runJobs.php
122 *
123 * @param $result string The result (Success|Warning|Failure)
124 * @param $dataKey string The key of the extra data
125 * @param $dataKey mixed The extra data itself
126 */
127 protected function storeResultInSession( $result, $dataKey, $dataValue ) {
128 $session &= $_SESSION[UploadBase::getSessionKeyname()][$this->params['sessionKey']];
129 $session['result'] = $result;
130 $session[$dataKey] = $dataValue;
131 }
132 }