Easier to use addWikiText() in wrapWikiMsg()
[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 public $upload;
22 protected $user;
23
24 public function __construct( $title, $params, $id = 0 ) {
25 parent::__construct( 'uploadFromUrl', $title, $params, $id );
26 }
27
28 public function run() {
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 wfSetupSession( $this->params['sessionId'] );
58
59 if ( $this->params['leaveMessage'] ) {
60 $this->user->leaveUserMessage(
61 wfMsg( 'upload-warning-subj' ),
62 wfMsg( 'upload-warning-msg',
63 $this->params['sessionKey'],
64 $this->params['url'] )
65 );
66 } else {
67 $this->storeResultInSession( 'Warning',
68 'warnings', $warnings );
69 }
70
71 # Stash the upload in the session
72 $this->upload->stashSession( $this->params['sessionKey'] );
73 session_write_close();
74
75 return true;
76 }
77 }
78
79 # Perform the upload
80 $status = $this->upload->performUpload(
81 $this->params['comment'],
82 $this->params['pageText'],
83 $this->params['watch'],
84 $this->user
85 );
86 $this->leaveMessage( $status );
87 return true;
88
89 }
90
91 /**
92 * Leave a message on the user talk page or in the session according to
93 * $params['leaveMessage'].
94 *
95 * @param $status Status
96 */
97 protected function leaveMessage( $status ) {
98 if ( $this->params['leaveMessage'] ) {
99 if ( $status->isGood() ) {
100 $this->user->leaveUserMessage( wfMsg( 'upload-success-subj' ),
101 wfMsg( 'upload-success-msg',
102 $this->upload->getTitle()->getText(),
103 $this->params['url']
104 ) );
105 } else {
106 $this->user->leaveUserMessage( wfMsg( 'upload-failure-subj' ),
107 wfMsg( 'upload-failure-msg',
108 $status->getWikiText(),
109 $this->params['url']
110 ) );
111 }
112 } else {
113 wfSetupSession( $this->params['sessionId'] );
114 if ( $status->isOk() ) {
115 $this->storeResultInSession( 'Success',
116 'filename', $this->upload->getLocalFile()->getName() );
117 } else {
118 $this->storeResultInSession( 'Failure',
119 'errors', $status->getErrorsArray() );
120 }
121 session_write_close();
122 }
123 }
124
125 /**
126 * Store a result in the session data. Note that the caller is responsible
127 * for appropriate session_start and session_write_close calls.
128 *
129 * @param $result String: the result (Success|Warning|Failure)
130 * @param $dataKey String: the key of the extra data
131 * @param $dataValue Mixed: the extra data itself
132 */
133 protected function storeResultInSession( $result, $dataKey, $dataValue ) {
134 $session =& self::getSessionData( $this->params['sessionKey'] );
135 $session['result'] = $result;
136 $session[$dataKey] = $dataValue;
137 }
138
139 /**
140 * Initialize the session data. Sets the intial result to queued.
141 */
142 public function initializeSessionData() {
143 $session =& self::getSessionData( $this->params['sessionKey'] );
144 $$session['result'] = 'Queued';
145 }
146
147 public static function &getSessionData( $key ) {
148 if ( !isset( $_SESSION[self::SESSION_KEYNAME][$key] ) ) {
149 $_SESSION[self::SESSION_KEYNAME][$key] = array();
150 }
151 return $_SESSION[self::SESSION_KEYNAME][$key];
152 }
153 }