Index: branches/new-upload/phase3/includes/HttpFunctions.php |
— | — | @@ -6,192 +6,200 @@ |
7 | 7 | |
8 | 8 | |
9 | 9 | class Http { |
10 | | - const SYNC_DOWNLOAD = 1; //syncronys upload (in a single request) |
| 10 | + const SYNC_DOWNLOAD = 1; //syncronys upload (in a single request) |
11 | 11 | const ASYNC_DOWNLOAD = 2; //asynchronous upload we should spawn out another process and monitor progress if possible) |
12 | 12 | |
13 | | - var $body = ''; |
| 13 | + var $body = ''; |
| 14 | + public static function request( $url, $opts = array() ) { |
| 15 | + $req = new HttpRequest( $url, $opts ); |
| 16 | + $status = $req->doRequest(); |
| 17 | + if( $status->isOK() ){ |
| 18 | + return $status->value; |
| 19 | + }else{ |
| 20 | + return false; |
| 21 | + } |
| 22 | + } |
14 | 23 | /** |
15 | 24 | * Simple wrapper for Http::request( 'GET' ) |
16 | 25 | */ |
17 | | - public static function get( $url, $opts = array() ) { |
18 | | - $opt['method'] = 'GET'; |
19 | | - $req = new HttpRequest($url, $opts ); |
20 | | - return $req->doRequest(); |
21 | | - } |
| 26 | + public static function get( $url, $opts = array() ) { |
| 27 | + $opt['method'] = 'GET'; |
| 28 | + return Http::request($url, $opts); |
| 29 | + } |
22 | 30 | /** |
23 | 31 | * Simple wrapper for Http::request( 'POST' ) |
24 | 32 | */ |
25 | 33 | public static function post( $url, $opts = array() ) { |
26 | 34 | $opts['method']='POST'; |
27 | | - $req = new HttpRequest( $url, $opts ); |
28 | | - return $req->doRequest(); |
| 35 | + return Http::request($url, $opts); |
29 | 36 | } |
| 37 | + |
30 | 38 | public static function doDownload( $url, $target_file_path , $dl_mode = self::SYNC_DOWNLOAD , $redirectCount=0){ |
31 | | - global $wgPhpCliPath, $wgMaxUploadSize, $wgMaxRedirects; |
32 | | - //do a quick check to HEAD to insure the file size is not > $wgMaxUploadSize |
33 | | - $head = get_headers($url, 1); |
34 | | - |
| 39 | + global $wgPhpCliPath, $wgMaxUploadSize, $wgMaxRedirects; |
| 40 | + //do a quick check to HEAD to insure the file size is not > $wgMaxUploadSize |
| 41 | + $head = get_headers($url, 1); |
| 42 | + |
35 | 43 | //check for redirects: |
36 | 44 | if( isset( $head['Location'] ) && strrpos($head[0], '302')!==false ){ |
37 | 45 | if($redirectCount < $wgMaxRedirects){ |
38 | | - if( UploadFromUrl::isValidURI( $head['Location'] )){ |
| 46 | + if( UploadFromUrl::isValidURI( $head['Location'] )){ |
39 | 47 | return self::doDownload ( $head['Location'], $target_file_path , $dl_mode, $redirectCount++); |
40 | 48 | }else{ |
41 | | - return Status::newFatal('upload-proto-error'); |
42 | | - } |
| 49 | + return Status::newFatal('upload-proto-error'); |
| 50 | + } |
43 | 51 | }else{ |
44 | | - return Status::newFatal('upload-too-many-redirects'); |
| 52 | + return Status::newFatal('upload-too-many-redirects'); |
45 | 53 | } |
46 | | - } |
47 | | - //we did not get a 200 ok response: |
| 54 | + } |
| 55 | + //we did not get a 200 ok response: |
48 | 56 | if( strrpos($head[0], '200 OK') === false){ |
49 | | - return Status::newFatal( 'upload-http-error', htmlspecialchars($head[0]) ); |
| 57 | + return Status::newFatal( 'upload-http-error', htmlspecialchars($head[0]) ); |
50 | 58 | } |
51 | | - |
52 | | - |
53 | | - $content_length = (isset($head['Content-Length']))?$head['Content-Length']:null; |
| 59 | + |
| 60 | + |
| 61 | + $content_length = (isset($head['Content-Length']))?$head['Content-Length']:null; |
54 | 62 | if($content_length){ |
55 | | - if($content_length > $wgMaxUploadSize){ |
56 | | - return Status::newFatal('requested file length ' . $content_length . ' is greater than $wgMaxUploadSize: ' . $wgMaxUploadSize); |
57 | | - } |
58 | | - } |
59 | | - |
60 | | - //check if we can find phpCliPath (for doing a background shell request to php to do the download: |
61 | | - if( $wgPhpCliPath && wfShellExecEnabled() && $dl_mode == self::ASYNC_DOWNLOAD){ |
| 63 | + if($content_length > $wgMaxUploadSize){ |
| 64 | + return Status::newFatal('requested file length ' . $content_length . ' is greater than $wgMaxUploadSize: ' . $wgMaxUploadSize); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + //check if we can find phpCliPath (for doing a background shell request to php to do the download: |
| 69 | + if( $wgPhpCliPath && wfShellExecEnabled() && $dl_mode == self::ASYNC_DOWNLOAD){ |
62 | 70 | wfDebug("\ASYNC_DOWNLOAD\n"); |
63 | | - //setup session and shell call: |
64 | | - return self::initBackgroundDownload( $url, $target_file_path, $content_length ); |
| 71 | + //setup session and shell call: |
| 72 | + return self::initBackgroundDownload( $url, $target_file_path, $content_length ); |
65 | 73 | }else if( $dl_mode== self::SYNC_DOWNLOAD ){ |
66 | 74 | wfDebug("\nSYNC_DOWNLOAD\n"); |
67 | 75 | //SYNC_DOWNLOAD download as much as we can in the time we have to execute |
68 | 76 | $opts['method']='GET'; |
69 | 77 | $opts['target_file_path'] = $target_file_path; |
70 | | - $req = new HttpRequest($url, $opts ); |
71 | | - return $req->doRequest(); |
72 | | - } |
| 78 | + $req = new HttpRequest($url, $opts ); |
| 79 | + return $req->doRequest(); |
| 80 | + } |
73 | 81 | } |
74 | 82 | /** |
75 | 83 | * a non blocking request (generally an exit point in the application) |
76 | | - * should write to a file location and give updates |
77 | | - * |
| 84 | + * should write to a file location and give updates |
| 85 | + * |
78 | 86 | */ |
79 | 87 | private function initBackgroundDownload( $url, $target_file_path, $content_length = null ){ |
80 | 88 | global $wgMaxUploadSize, $IP, $wgPhpCliPath; |
81 | | - $status = Status::newGood(); |
82 | | - |
83 | | - //generate a session id with all the details for the download (pid, target_file_path ) |
84 | | - $upload_session_key = self::getUploadSessionKey(); |
| 89 | + $status = Status::newGood(); |
| 90 | + |
| 91 | + //generate a session id with all the details for the download (pid, target_file_path ) |
| 92 | + $upload_session_key = self::getUploadSessionKey(); |
85 | 93 | $session_id = session_id(); |
86 | | - |
87 | | - //store the url and target path: |
| 94 | + |
| 95 | + //store the url and target path: |
88 | 96 | $_SESSION[ 'wsDownload' ][$upload_session_key]['url'] = $url; |
89 | | - $_SESSION[ 'wsDownload' ][$upload_session_key]['target_file_path'] = $target_file_path; |
90 | | - |
| 97 | + $_SESSION[ 'wsDownload' ][$upload_session_key]['target_file_path'] = $target_file_path; |
| 98 | + |
91 | 99 | if($content_length) |
92 | 100 | $_SESSION[ 'wsDownload' ][$upload_session_key]['content_length'] = $content_length; |
93 | 101 | |
94 | 102 | //set initial loaded bytes: |
95 | 103 | $_SESSION[ 'wsDownload' ][$upload_session_key]['loaded'] = 0; |
96 | | - |
97 | | - |
98 | | - //run the background download request: |
99 | | - $cmd = $wgPhpCliPath . ' ' . $IP . "/maintenance/http_session_download.php --sid {$session_id} --usk {$upload_session_key}"; |
100 | | - $pid = wfShellBackgroundExec($cmd , $retval); |
101 | | - //the pid is not of much use since we won't be visiting this same apache any-time soon. |
| 104 | + |
| 105 | + |
| 106 | + //run the background download request: |
| 107 | + $cmd = $wgPhpCliPath . ' ' . $IP . "/maintenance/http_session_download.php --sid {$session_id} --usk {$upload_session_key}"; |
| 108 | + $pid = wfShellBackgroundExec($cmd , $retval); |
| 109 | + //the pid is not of much use since we won't be visiting this same apache any-time soon. |
102 | 110 | if(!$pid) |
103 | 111 | return Status::newFatal('could not run background shell exec'); |
104 | | - |
105 | | - //update the status value with the $upload_session_key (for the user to check on the status of the upload) |
| 112 | + |
| 113 | + //update the status value with the $upload_session_key (for the user to check on the status of the upload) |
106 | 114 | $status->value = $upload_session_key; |
107 | | - |
| 115 | + |
108 | 116 | //return good status |
109 | | - return $status; |
110 | | - } |
| 117 | + return $status; |
| 118 | + } |
111 | 119 | function getUploadSessionKey(){ |
112 | 120 | $key = mt_rand( 0, 0x7fffffff ); |
113 | 121 | $_SESSION['wsUploadData'][$key] = array(); |
114 | 122 | return $key; |
115 | | - } |
| 123 | + } |
116 | 124 | /** |
117 | | - * used to run a session based download. Is initiated via the shell. |
| 125 | + * used to run a session based download. Is initiated via the shell. |
118 | 126 | * |
119 | 127 | * @param string $session_id // the session id to grab download details from |
120 | | - * @param string $upload_session_key //the key of the given upload session |
| 128 | + * @param string $upload_session_key //the key of the given upload session |
121 | 129 | * (a given client could have started a few http uploads at once) |
122 | 130 | */ |
123 | 131 | public static function doSessionIdDownload( $session_id, $upload_session_key ){ |
124 | | - global $wgUser, $wgEnableWriteAPI, $wgAsyncHTTPTimeout; |
| 132 | + global $wgUser, $wgEnableWriteAPI, $wgAsyncHTTPTimeout; |
125 | 133 | wfDebug("\n\ndoSessionIdDownload\n\n"); |
126 | 134 | //set session to the provided key: |
127 | 135 | session_id($session_id); |
128 | | - //start the session |
| 136 | + //start the session |
129 | 137 | if( session_start() === false){ |
130 | | - wfDebug( __METHOD__ . ' could not start session'); |
| 138 | + wfDebug( __METHOD__ . ' could not start session'); |
131 | 139 | } |
132 | | - //get all the vars we need from session_id |
| 140 | + //get all the vars we need from session_id |
133 | 141 | if(!isset($_SESSION[ 'wsDownload' ][$upload_session_key])){ |
134 | 142 | wfDebug( __METHOD__ .' Error:could not find upload session'); |
135 | 143 | exit(); |
136 | 144 | } |
137 | 145 | //setup the global user from the session key we just inherited |
138 | 146 | $wgUser = User::newFromSession(); |
139 | | - |
140 | | - //grab the session data to setup the request: |
| 147 | + |
| 148 | + //grab the session data to setup the request: |
141 | 149 | $sd =& $_SESSION[ 'wsDownload' ][$upload_session_key]; |
142 | 150 | //close down the session so we can other http queries can get session updates: |
143 | | - session_write_close(); |
144 | | - |
| 151 | + session_write_close(); |
| 152 | + |
145 | 153 | $req = new HttpRequest( $sd['url'], array( |
146 | | - 'target_file_path' => $sd['target_file_path'], |
147 | | - 'upload_session_key'=> $upload_session_key, |
| 154 | + 'target_file_path' => $sd['target_file_path'], |
| 155 | + 'upload_session_key'=> $upload_session_key, |
148 | 156 | 'timeout' => $wgAsyncHTTPTimeout |
149 | | - ) ); |
150 | | - //run the actual request .. (this can take some time) |
| 157 | + ) ); |
| 158 | + //run the actual request .. (this can take some time) |
151 | 159 | wfDebug("do Request: " . $sd['url'] . ' tf: ' . $sd['target_file_path'] ); |
152 | | - $status = $req->doRequest(); |
| 160 | + $status = $req->doRequest(); |
153 | 161 | //wfDebug("done with req status is: ". $status->isOK(). ' '.$status->getWikiText(). "\n"); |
154 | 162 | |
155 | | - //start up the session again: |
| 163 | + //start up the session again: |
156 | 164 | if( session_start() === false){ |
157 | | - wfDebug( __METHOD__ . ' ERROR:: Could not start session'); |
158 | | - } |
159 | | - //grab the updated session data pointer |
160 | | - $sd =& $_SESSION[ 'wsDownload' ][$upload_session_key]; |
161 | | - //if error update status: |
| 165 | + wfDebug( __METHOD__ . ' ERROR:: Could not start session'); |
| 166 | + } |
| 167 | + //grab the updated session data pointer |
| 168 | + $sd =& $_SESSION[ 'wsDownload' ][$upload_session_key]; |
| 169 | + //if error update status: |
162 | 170 | if( !$status->isOK() ){ |
163 | | - $sd['apiUploadResult']= ApiFormatJson::getJsonEncode( |
| 171 | + $sd['apiUploadResult']= ApiFormatJson::getJsonEncode( |
164 | 172 | array( 'error' => $status->getWikiText() ) |
165 | 173 | ); |
166 | | - } |
167 | | - //if status oky process upload using fauxReq to api: |
168 | | - if( $status->isOK() ){ |
| 174 | + } |
| 175 | + //if status oky process upload using fauxReq to api: |
| 176 | + if( $status->isOK() ){ |
169 | 177 | //setup the faxRequest |
170 | | - $fauxReqData = $sd['mParams']; |
171 | | - $fauxReqData['action'] = 'upload'; |
| 178 | + $fauxReqData = $sd['mParams']; |
| 179 | + $fauxReqData['action'] = 'upload'; |
172 | 180 | $fauxReqData['format'] = 'json'; |
173 | | - $fauxReqData['internalhttpsession'] = $upload_session_key; |
174 | | - //evil but no other clean way about it: |
175 | | - |
176 | | - $faxReq = new FauxRequest($fauxReqData, true); |
| 181 | + $fauxReqData['internalhttpsession'] = $upload_session_key; |
| 182 | + //evil but no other clean way about it: |
| 183 | + |
| 184 | + $faxReq = new FauxRequest($fauxReqData, true); |
177 | 185 | $processor = new ApiMain($faxReq, $wgEnableWriteAPI); |
178 | | - |
179 | | - //init the mUpload var for the $processor |
180 | | - $processor->execute(); |
181 | | - $processor->getResult()->cleanUpUTF8(); |
| 186 | + |
| 187 | + //init the mUpload var for the $processor |
| 188 | + $processor->execute(); |
| 189 | + $processor->getResult()->cleanUpUTF8(); |
182 | 190 | $printer = $processor->createPrinterByName('json'); |
183 | | - $printer->initPrinter(false); |
| 191 | + $printer->initPrinter(false); |
184 | 192 | ob_start(); |
185 | 193 | $printer->execute(); |
186 | | - $apiUploadResult = ob_get_clean(); |
187 | | - |
| 194 | + $apiUploadResult = ob_get_clean(); |
| 195 | + |
188 | 196 | wfDebug("\n\n got api result:: $apiUploadResult \n" ); |
189 | | - //the status updates runner will grab the result form the session: |
190 | | - $sd['apiUploadResult'] = $apiUploadResult; |
| 197 | + //the status updates runner will grab the result form the session: |
| 198 | + $sd['apiUploadResult'] = $apiUploadResult; |
191 | 199 | } |
192 | | - //close the session: |
193 | | - session_write_close(); |
| 200 | + //close the session: |
| 201 | + session_write_close(); |
194 | 202 | } |
195 | | - |
| 203 | + |
196 | 204 | /** |
197 | 205 | * Check if the URL can be served by localhost |
198 | 206 | * @param $url string Full url to check |
— | — | @@ -225,7 +233,7 @@ |
226 | 234 | } |
227 | 235 | return false; |
228 | 236 | } |
229 | | - |
| 237 | + |
230 | 238 | /** |
231 | 239 | * Return a standard user-agent we can use for external requests. |
232 | 240 | */ |
— | — | @@ -241,45 +249,45 @@ |
242 | 250 | global $wgSyncHTTPTimeout; |
243 | 251 | $this->url = $url; |
244 | 252 | //set the timeout to default sync timeout (unless the timeout option is provided) |
245 | | - $this->timeout = (isset($opt['timeout']))?$opt['timeout']:$wgSyncHTTPTimeout; |
246 | | - $this->method = (isset($opt['method']))?$opt['method']:'GET'; |
| 253 | + $this->timeout = (isset($opt['timeout']))?$opt['timeout']:$wgSyncHTTPTimeout; |
| 254 | + $this->method = (isset($opt['method']))?$opt['method']:'GET'; |
247 | 255 | $this->target_file_path = (isset($opt['target_file_path']))?$opt['target_file_path']:false; |
248 | | - $this->upload_session_key = (isset($opt['upload_session_key']))?$opt['upload_session_key']:false; |
| 256 | + $this->upload_session_key = (isset($opt['upload_session_key']))?$opt['upload_session_key']:false; |
249 | 257 | } |
250 | 258 | /** |
251 | 259 | * Get the contents of a file by HTTP |
252 | | - * @param $url string Full URL to act on |
| 260 | + * @param $url string Full URL to act on |
253 | 261 | * @param $Opt associative array Optional array of options: |
254 | | - * 'method' => 'GET', 'POST' etc. |
| 262 | + * 'method' => 'GET', 'POST' etc. |
255 | 263 | * 'target_file_path' => if curl should output to a target file |
256 | 264 | * 'adapter' => 'curl', 'soket' |
257 | 265 | */ |
258 | | - public function doRequest() { |
| 266 | + public function doRequest() { |
259 | 267 | # Use curl if available |
260 | | - if ( function_exists( 'curl_init' ) ) { |
| 268 | + if ( function_exists( 'curl_init' ) ) { |
261 | 269 | return $this->doCurlReq(); |
262 | | - }else{ |
| 270 | + }else{ |
263 | 271 | return $this->doPhpReq(); |
264 | 272 | } |
265 | 273 | } |
266 | 274 | private function doCurlReq(){ |
267 | 275 | global $wgHTTPProxy, $wgTitle; |
268 | | - |
269 | | - $status = Status::newGood(); |
270 | | - $c = curl_init( $this->url ); |
271 | | - |
272 | | - //proxy setup: |
| 276 | + |
| 277 | + $status = Status::newGood(); |
| 278 | + $c = curl_init( $this->url ); |
| 279 | + |
| 280 | + //proxy setup: |
273 | 281 | if ( Http::isLocalURL( $this->url ) ) { |
274 | 282 | curl_setopt( $c, CURLOPT_PROXY, 'localhost:80' ); |
275 | 283 | } else if ($wgHTTPProxy) { |
276 | 284 | curl_setopt($c, CURLOPT_PROXY, $wgHTTPProxy); |
277 | 285 | } |
278 | | - |
279 | | - curl_setopt( $c, CURLOPT_TIMEOUT, $this->timeout ); |
280 | | - |
281 | | - |
| 286 | + |
| 287 | + curl_setopt( $c, CURLOPT_TIMEOUT, $this->timeout ); |
| 288 | + |
| 289 | + |
282 | 290 | curl_setopt( $c, CURLOPT_USERAGENT, Http::userAgent() ); |
283 | | - |
| 291 | + |
284 | 292 | if ( $this->method == 'POST' ) { |
285 | 293 | curl_setopt( $c, CURLOPT_POST, true ); |
286 | 294 | curl_setopt( $c, CURLOPT_POSTFIELDS, '' ); |
— | — | @@ -295,21 +303,21 @@ |
296 | 304 | if ( is_object( $wgTitle ) ) { |
297 | 305 | curl_setopt( $c, CURLOPT_REFERER, $wgTitle->getFullURL() ); |
298 | 306 | } |
299 | | - |
300 | | - //set the write back function (if we are writing to a file) |
301 | | - if( $this->target_file_path ){ |
302 | | - $cwrite = new simpleFileWriter( $this->target_file_path, $this->upload_session_key ); |
| 307 | + |
| 308 | + //set the write back function (if we are writing to a file) |
| 309 | + if( $this->target_file_path ){ |
| 310 | + $cwrite = new simpleFileWriter( $this->target_file_path, $this->upload_session_key ); |
303 | 311 | if(!$cwrite->status->isOK()){ |
304 | | - wfDebug("ERROR in setting up simpleFileWriter\n"); |
305 | | - $status = $cwrite->status; |
| 312 | + wfDebug("ERROR in setting up simpleFileWriter\n"); |
| 313 | + $status = $cwrite->status; |
306 | 314 | } |
307 | 315 | curl_setopt( $c, CURLOPT_WRITEFUNCTION, array($cwrite, 'callbackWriteBody') ); |
308 | 316 | } |
309 | 317 | |
310 | | - //start output grabber: |
| 318 | + //start output grabber: |
311 | 319 | if(!$this->target_file_path) |
312 | | - ob_start(); |
313 | | - |
| 320 | + ob_start(); |
| 321 | + |
314 | 322 | //run the actual curl_exec: |
315 | 323 | try { |
316 | 324 | if (false === curl_exec($c)) { |
— | — | @@ -319,20 +327,20 @@ |
320 | 328 | } |
321 | 329 | } catch (Exception $e) { |
322 | 330 | //do something with curl exec error? |
323 | | - } |
324 | | - //if direct request output the results to the stats value: |
325 | | - if( !$this->target_file_path && $status->isOK() ){ |
| 331 | + } |
| 332 | + //if direct request output the results to the stats value: |
| 333 | + if( !$this->target_file_path && $status->isOK() ){ |
326 | 334 | $status->value = ob_get_contents(); |
327 | 335 | ob_end_clean(); |
328 | | - } |
| 336 | + } |
329 | 337 | //if we wrote to a target file close up or return error |
330 | 338 | if( $this->target_file_path ){ |
331 | | - $cwrite->close(); |
| 339 | + $cwrite->close(); |
332 | 340 | if( ! $cwrite->status->isOK() ){ |
333 | | - return $cwrite->status; |
| 341 | + return $cwrite->status; |
334 | 342 | } |
335 | 343 | } |
336 | | - |
| 344 | + |
337 | 345 | # Don't return the text of error messages, return false on error |
338 | 346 | $retcode = curl_getinfo( $c, CURLINFO_HTTP_CODE ); |
339 | 347 | if ( $retcode != 200 ) { |
— | — | @@ -346,13 +354,13 @@ |
347 | 355 | wfDebug( __METHOD__ . ": CURL error code $errno: $errstr\n" ); |
348 | 356 | $status = Status::newFatal( " CURL error code $errno: $errstr\n" ); |
349 | 357 | } |
350 | | - curl_close( $c ); |
351 | | - |
352 | | - //return the result obj |
353 | | - return $status; |
| 358 | + curl_close( $c ); |
| 359 | + |
| 360 | + //return the result obj |
| 361 | + return $status; |
354 | 362 | } |
355 | 363 | public function doPhpReq(){ |
356 | | - #$use file_get_contents... |
| 364 | + #$use file_get_contents... |
357 | 365 | # This doesn't have local fetch capabilities... |
358 | 366 | |
359 | 367 | $headers = array( "User-Agent: " . self :: userAgent() ); |
— | — | @@ -375,15 +383,15 @@ |
376 | 384 | } |
377 | 385 | } |
378 | 386 | /** |
379 | | - * a simpleFileWriter with session id updates |
380 | | - * |
| 387 | + * a simpleFileWriter with session id updates |
| 388 | + * |
381 | 389 | */ |
382 | 390 | class simpleFileWriter{ |
383 | 391 | var $target_file_path; |
384 | | - var $status = null; |
385 | | - var $session_id = null; |
386 | | - var $session_update_interval = 0; //how offten to update the session while downloading |
387 | | - |
| 392 | + var $status = null; |
| 393 | + var $session_id = null; |
| 394 | + var $session_update_interval = 0; //how offten to update the session while downloading |
| 395 | + |
388 | 396 | function simpleFileWriter($target_file_path, $upload_session_key){ |
389 | 397 | $this->target_file_path = $target_file_path; |
390 | 398 | $this->upload_session_key = $upload_session_key; |
— | — | @@ -391,32 +399,32 @@ |
392 | 400 | //open the file: |
393 | 401 | $this->fp = fopen( $this->target_file_path, 'w'); |
394 | 402 | if( $this->fp === false ){ |
395 | | - $this->status = Status::newFatal('HTTP::could-not-open-file-for-writing'); |
| 403 | + $this->status = Status::newFatal('HTTP::could-not-open-file-for-writing'); |
396 | 404 | } |
397 | 405 | //true start time |
398 | 406 | $this->prevTime = time(); |
399 | 407 | } |
400 | 408 | public function callbackWriteBody($ch, $data_packet){ |
401 | | - global $wgMaxUploadSize; |
402 | | - |
| 409 | + global $wgMaxUploadSize; |
| 410 | + |
403 | 411 | //write out the content |
404 | 412 | if( fwrite($this->fp, $data_packet) === false){ |
405 | 413 | wfDebug(__METHOD__ ." ::could-not-write-to-file\n"); |
406 | 414 | $this->status = Status::newFatal('HTTP::could-not-write-to-file'); |
407 | 415 | return 0; |
408 | | - } |
409 | | - |
410 | | - //check file size: |
| 416 | + } |
| 417 | + |
| 418 | + //check file size: |
411 | 419 | clearstatcache(); |
412 | 420 | $this->current_fsize = filesize( $this->target_file_path); |
413 | | - |
| 421 | + |
414 | 422 | if( $this->current_fsize > $wgMaxUploadSize){ |
415 | 423 | wfDebug( __METHOD__ . " ::http download too large\n"); |
416 | | - $this->status = Status::newFatal('HTTP::file-has-grown-beyond-upload-limit-killing: downloaded more than ' . |
417 | | - Language::formatSize($wgMaxUploadSize) . ' '); |
| 424 | + $this->status = Status::newFatal('HTTP::file-has-grown-beyond-upload-limit-killing: downloaded more than ' . |
| 425 | + Language::formatSize($wgMaxUploadSize) . ' '); |
418 | 426 | return 0; |
419 | | - } |
420 | | - |
| 427 | + } |
| 428 | + |
421 | 429 | //if more than session_update_interval second have passed update_session_progress |
422 | 430 | if($this->upload_session_key && ( (time() - $this->prevTime) > $this->session_update_interval )) { |
423 | 431 | $this->prevTime = time(); |
— | — | @@ -426,35 +434,35 @@ |
427 | 435 | wfDebug( __METHOD__ . ' update session failed or was canceled'); |
428 | 436 | return 0; |
429 | 437 | } |
430 | | - } |
431 | | - return strlen( $data_packet ); |
| 438 | + } |
| 439 | + return strlen( $data_packet ); |
432 | 440 | } |
433 | 441 | public function update_session_progress(){ |
434 | 442 | $status = Status::newGood(); |
435 | | - //start the session |
| 443 | + //start the session |
436 | 444 | if( session_start() === false){ |
437 | 445 | wfDebug( __METHOD__ . ' could not start session'); |
438 | | - exit(0); |
439 | | - } |
| 446 | + exit(0); |
| 447 | + } |
440 | 448 | $sd =& $_SESSION[ 'wsDownload' ][ $this->upload_session_key ]; |
441 | | - //check if the user canceled the request: |
| 449 | + //check if the user canceled the request: |
442 | 450 | if( $sd['user_cancel'] == true ){ |
443 | 451 | //kill the download |
444 | | - return Status::newFatal('user-canceled-request'); |
445 | | - } |
446 | | - //update the progress bytes download so far: |
| 452 | + return Status::newFatal('user-canceled-request'); |
| 453 | + } |
| 454 | + //update the progress bytes download so far: |
447 | 455 | $sd['loaded'] = $this->current_fsize; |
448 | 456 | wfDebug('set session loaded amount to: ' . $sd['loaded'] . "\n"); |
449 | 457 | //close down the session so we can other http queries can get session updates: |
450 | | - session_write_close(); |
451 | | - return $status; |
| 458 | + session_write_close(); |
| 459 | + return $status; |
452 | 460 | } |
453 | | - public function close(){ |
454 | | - //do a final session update: |
| 461 | + public function close(){ |
| 462 | + //do a final session update: |
455 | 463 | $this->update_session_progress(); |
456 | | - //close up the file handle: |
| 464 | + //close up the file handle: |
457 | 465 | if(false === fclose( $this->fp )){ |
458 | 466 | $this->status = Status::newFatal('HTTP::could-not-close-file'); |
459 | | - } |
| 467 | + } |
460 | 468 | } |
461 | 469 | } |
\ No newline at end of file |
Index: branches/new-upload/phase3/includes/Import.php |
— | — | @@ -90,15 +90,15 @@ |
91 | 91 | function setSize( $size ) { |
92 | 92 | $this->size = intval( $size ); |
93 | 93 | } |
94 | | - |
| 94 | + |
95 | 95 | function setType( $type ) { |
96 | 96 | $this->type = $type; |
97 | 97 | } |
98 | | - |
| 98 | + |
99 | 99 | function setAction( $action ) { |
100 | 100 | $this->action = $action; |
101 | 101 | } |
102 | | - |
| 102 | + |
103 | 103 | function setParams( $params ) { |
104 | 104 | $this->params = $params; |
105 | 105 | } |
— | — | @@ -142,15 +142,15 @@ |
143 | 143 | function getSize() { |
144 | 144 | return $this->size; |
145 | 145 | } |
146 | | - |
| 146 | + |
147 | 147 | function getType() { |
148 | 148 | return $this->type; |
149 | 149 | } |
150 | | - |
| 150 | + |
151 | 151 | function getAction() { |
152 | 152 | return $this->action; |
153 | 153 | } |
154 | | - |
| 154 | + |
155 | 155 | function getParams() { |
156 | 156 | return $this->params; |
157 | 157 | } |
— | — | @@ -209,7 +209,7 @@ |
210 | 210 | ) ); |
211 | 211 | $revId = $revision->insertOn( $dbw ); |
212 | 212 | $changed = $article->updateIfNewerOn( $dbw, $revision ); |
213 | | - |
| 213 | + |
214 | 214 | # To be on the safe side... |
215 | 215 | $tempTitle = $GLOBALS['wgTitle']; |
216 | 216 | $GLOBALS['wgTitle'] = $this->title; |
— | — | @@ -237,12 +237,12 @@ |
238 | 238 | |
239 | 239 | return true; |
240 | 240 | } |
241 | | - |
| 241 | + |
242 | 242 | function importLogItem() { |
243 | 243 | $dbw = wfGetDB( DB_MASTER ); |
244 | 244 | # FIXME: this will not record autoblocks |
245 | 245 | if( !$this->getTitle() ) { |
246 | | - wfDebug( __METHOD__ . ": skipping invalid {$this->type}/{$this->action} log time, timestamp " . |
| 246 | + wfDebug( __METHOD__ . ": skipping invalid {$this->type}/{$this->action} log time, timestamp " . |
247 | 247 | $this->timestamp . "\n" ); |
248 | 248 | return; |
249 | 249 | } |
— | — | @@ -261,7 +261,7 @@ |
262 | 262 | ); |
263 | 263 | // FIXME: this could fail slightly for multiple matches :P |
264 | 264 | if( $prior ) { |
265 | | - wfDebug( __METHOD__ . ": skipping existing item for Log:{$this->type}/{$this->action}, timestamp " . |
| 265 | + wfDebug( __METHOD__ . ": skipping existing item for Log:{$this->type}/{$this->action}, timestamp " . |
266 | 266 | $this->timestamp . "\n" ); |
267 | 267 | return false; |
268 | 268 | } |
— | — | @@ -355,7 +355,7 @@ |
356 | 356 | // @fixme! |
357 | 357 | $src = $this->getSrc(); |
358 | 358 | $status = Http::get( $src ); |
359 | | - if( !$status->isOK() ){ |
| 359 | + if( !$status->isOK() ){ |
360 | 360 | if( !$data ) { |
361 | 361 | wfDebug( "IMPORT: couldn't fetch source $src\n" ); |
362 | 362 | fclose( $f ); |
— | — | @@ -415,7 +415,7 @@ |
416 | 416 | return($name); |
417 | 417 | } |
418 | 418 | } |
419 | | - |
| 419 | + |
420 | 420 | # -------------- |
421 | 421 | |
422 | 422 | function doImport() { |
— | — | @@ -516,7 +516,7 @@ |
517 | 517 | $this->mUploadCallback = $callback; |
518 | 518 | return $previous; |
519 | 519 | } |
520 | | - |
| 520 | + |
521 | 521 | /** |
522 | 522 | * Sets the action to perform as each log item reached. |
523 | 523 | * @param $callback callback |
— | — | @@ -552,7 +552,7 @@ |
553 | 553 | $dbw = wfGetDB( DB_MASTER ); |
554 | 554 | return $dbw->deadlockLoop( array( $revision, 'importOldRevision' ) ); |
555 | 555 | } |
556 | | - |
| 556 | + |
557 | 557 | /** |
558 | 558 | * Default per-revision callback, performs the import. |
559 | 559 | * @param $revision WikiRevision |
— | — | @@ -892,7 +892,7 @@ |
893 | 893 | } |
894 | 894 | } |
895 | 895 | } |
896 | | - |
| 896 | + |
897 | 897 | function in_logitem( $parser, $name, $attribs ) { |
898 | 898 | $name = $this->stripXmlNamespace($name); |
899 | 899 | $this->debug( "in_logitem $name" ); |
— | — | @@ -1105,8 +1105,7 @@ |
1106 | 1106 | # quicker and sorts out user-agent problems which might |
1107 | 1107 | # otherwise prevent importing from large sites, such |
1108 | 1108 | # as the Wikimedia cluster, etc. |
1109 | | - $req = new HttpRequest( $url, array( 'method' => $method ) ); |
1110 | | - $data = $req->doRequest(); |
| 1109 | + $data = Http::request( $url, array( 'method' => $method ) ); |
1111 | 1110 | if( $data !== false ) { |
1112 | 1111 | $file = tmpfile(); |
1113 | 1112 | fwrite( $file, $data ); |