3 * Job for asynchronous upload-by-url.
10 * Job for asynchronous upload-by-url.
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.
18 class UploadFromUrlJob
extends Job
{
19 const SESSION_KEYNAME
= 'wsUploadFromUrlJobData';
24 public function __construct( $title, $params, $id = 0 ) {
25 parent
::__construct( 'uploadFromUrl', $title, $params, $id );
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(),
36 $this->user
= User
::newFromName( $this->params
['userName'] );
39 $status = $this->upload
->fetchFile();
40 if ( !$status->isOk() ) {
41 $this->leaveMessage( $status );
46 $result = $this->upload
->verifyUpload();
47 if ( $result['status'] != UploadBase
::OK
) {
48 $status = $this->upload
->convertVerifyErrorToStatus( $result );
49 $this->leaveMessage( $status );
54 if ( !$this->params
['ignoreWarnings'] ) {
55 $warnings = $this->upload
->checkWarnings();
57 wfSetupSession( $this->params
['sessionId'] );
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'] )
67 $this->storeResultInSession( 'Warning',
68 'warnings', $warnings );
71 # Stash the upload in the session
72 $this->upload
->stashSession( $this->params
['sessionKey'] );
73 session_write_close();
80 $status = $this->upload
->performUpload(
81 $this->params
['comment'],
82 $this->params
['pageText'],
83 $this->params
['watch'],
86 $this->leaveMessage( $status );
92 * Leave a message on the user talk page or in the session according to
93 * $params['leaveMessage'].
95 * @param $status Status
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(),
106 $this->user
->leaveUserMessage( wfMsg( 'upload-failure-subj' ),
107 wfMsg( 'upload-failure-msg',
108 $status->getWikiText(),
113 wfSetupSession( $this->params
['sessionId'] );
114 if ( $status->isOk() ) {
115 $this->storeResultInSession( 'Success',
116 'filename', $this->upload
->getLocalFile()->getName() );
118 $this->storeResultInSession( 'Failure',
119 'errors', $status->getErrorsArray() );
121 session_write_close();
126 * Store a result in the session data. Note that the caller is responsible
127 * for appropriate session_start and session_write_close calls.
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
133 protected function storeResultInSession( $result, $dataKey, $dataValue ) {
134 $session =& self
::getSessionData( $this->params
['sessionKey'] );
135 $session['result'] = $result;
136 $session[$dataKey] = $dataValue;
140 * Initialize the session data. Sets the intial result to queued.
142 public function initializeSessionData() {
143 $session =& self
::getSessionData( $this->params
['sessionKey'] );
144 $
$session['result'] = 'Queued';
147 public static function &getSessionData( $key ) {
148 if ( !isset( $_SESSION[self
::SESSION_KEYNAME
][$key] ) ) {
149 $_SESSION[self
::SESSION_KEYNAME
][$key] = array();
151 return $_SESSION[self
::SESSION_KEYNAME
][$key];