Index: trunk/phase3/includes/HttpFunctions.php |
— | — | @@ -1,83 +1,86 @@ |
2 | 2 | <?php |
3 | 3 | /** |
4 | 4 | * HTTP handling class |
5 | | - * |
| 5 | + * @defgroup HTTP HTTP |
| 6 | + * @file |
| 7 | + * @ingroup HTTP |
6 | 8 | */ |
7 | 9 | |
8 | | - |
9 | 10 | class Http { |
10 | | - const SYNC_DOWNLOAD = 1; //syncronys upload (in a single request) |
11 | | - const ASYNC_DOWNLOAD = 2; //asynchronous upload we should spawn out another process and monitor progress if possible) |
| 11 | + const SYNC_DOWNLOAD = 1; // syncronys upload (in a single request) |
| 12 | + const ASYNC_DOWNLOAD = 2; // asynchronous upload we should spawn out another process and monitor progress if possible) |
12 | 13 | |
13 | 14 | var $body = ''; |
14 | 15 | 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 | | - } |
| 16 | + $req = new HttpRequest( $url, $opts ); |
| 17 | + $status = $req->doRequest(); |
| 18 | + if( $status->isOK() ){ |
| 19 | + return $status->value; |
| 20 | + } else { |
| 21 | + return false; |
| 22 | + } |
| 23 | + } |
| 24 | + |
23 | 25 | /** |
24 | 26 | * Simple wrapper for Http::request( 'GET' ) |
25 | 27 | */ |
26 | 28 | public static function get( $url, $opts = array() ) { |
27 | 29 | $opt['method'] = 'GET'; |
28 | | - return Http::request($url, $opts); |
| 30 | + return Http::request( $url, $opts ); |
29 | 31 | } |
| 32 | + |
30 | 33 | /** |
31 | 34 | * Simple wrapper for Http::request( 'POST' ) |
32 | 35 | */ |
33 | 36 | public static function post( $url, $opts = array() ) { |
34 | | - $opts['method']='POST'; |
35 | | - return Http::request($url, $opts); |
| 37 | + $opts['method'] = 'POST'; |
| 38 | + return Http::request( $url, $opts ); |
36 | 39 | } |
37 | 40 | |
38 | | - public static function doDownload( $url, $target_file_path , $dl_mode = self::SYNC_DOWNLOAD , $redirectCount=0){ |
| 41 | + public static function doDownload( $url, $target_file_path , $dl_mode = self::SYNC_DOWNLOAD , $redirectCount = 0 ){ |
39 | 42 | 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); |
| 43 | + // do a quick check to HEAD to insure the file size is not > $wgMaxUploadSize |
| 44 | + $head = get_headers( $url, 1 ); |
42 | 45 | |
43 | | - //check for redirects: |
44 | | - if( isset( $head['Location'] ) && strrpos($head[0], '302')!==false ){ |
45 | | - if($redirectCount < $wgMaxRedirects){ |
46 | | - if( UploadFromUrl::isValidURI( $head['Location'] )){ |
47 | | - return self::doDownload ( $head['Location'], $target_file_path , $dl_mode, $redirectCount++); |
48 | | - }else{ |
49 | | - return Status::newFatal('upload-proto-error'); |
| 46 | + // check for redirects: |
| 47 | + if( isset( $head['Location'] ) && strrpos( $head[0], '302' ) !== false ){ |
| 48 | + if( $redirectCount < $wgMaxRedirects ){ |
| 49 | + if( UploadFromUrl::isValidURI( $head['Location'] ) ){ |
| 50 | + return self::doDownload( $head['Location'], $target_file_path , $dl_mode, $redirectCount++ ); |
| 51 | + } else { |
| 52 | + return Status::newFatal( 'upload-proto-error' ); |
50 | 53 | } |
51 | | - }else{ |
52 | | - return Status::newFatal('upload-too-many-redirects'); |
| 54 | + } else { |
| 55 | + return Status::newFatal( 'upload-too-many-redirects' ); |
53 | 56 | } |
54 | 57 | } |
55 | | - //we did not get a 200 ok response: |
56 | | - if( strrpos($head[0], '200 OK') === false){ |
57 | | - return Status::newFatal( 'upload-http-error', htmlspecialchars($head[0]) ); |
| 58 | + // we did not get a 200 ok response: |
| 59 | + if( strrpos( $head[0], '200 OK' ) === false ){ |
| 60 | + return Status::newFatal( 'upload-http-error', htmlspecialchars( $head[0] ) ); |
58 | 61 | } |
59 | 62 | |
60 | | - |
61 | | - $content_length = (isset($head['Content-Length']))?$head['Content-Length']:null; |
62 | | - if($content_length){ |
63 | | - if($content_length > $wgMaxUploadSize){ |
64 | | - return Status::newFatal('requested file length ' . $content_length . ' is greater than $wgMaxUploadSize: ' . $wgMaxUploadSize); |
| 63 | + $content_length = ( isset( $head['Content-Length'] ) ) ? $head['Content-Length'] : null; |
| 64 | + if( $content_length ){ |
| 65 | + if( $content_length > $wgMaxUploadSize ){ |
| 66 | + return Status::newFatal( 'requested file length ' . $content_length . ' is greater than $wgMaxUploadSize: ' . $wgMaxUploadSize ); |
65 | 67 | } |
66 | 68 | } |
67 | 69 | |
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){ |
70 | | - wfDebug("\ASYNC_DOWNLOAD\n"); |
71 | | - //setup session and shell call: |
| 70 | + // check if we can find phpCliPath (for doing a background shell request to php to do the download: |
| 71 | + if( $wgPhpCliPath && wfShellExecEnabled() && $dl_mode == self::ASYNC_DOWNLOAD ){ |
| 72 | + wfDebug( __METHOD__ . "\ASYNC_DOWNLOAD\n" ); |
| 73 | + // setup session and shell call: |
72 | 74 | return self::initBackgroundDownload( $url, $target_file_path, $content_length ); |
73 | | - }else if( $dl_mode== self::SYNC_DOWNLOAD ){ |
74 | | - wfDebug("\nSYNC_DOWNLOAD\n"); |
75 | | - //SYNC_DOWNLOAD download as much as we can in the time we have to execute |
76 | | - $opts['method']='GET'; |
| 75 | + } else if( $dl_mode == self::SYNC_DOWNLOAD ){ |
| 76 | + wfDebug( __METHOD__ . "\nSYNC_DOWNLOAD\n" ); |
| 77 | + // SYNC_DOWNLOAD download as much as we can in the time we have to execute |
| 78 | + $opts['method'] = 'GET'; |
77 | 79 | $opts['target_file_path'] = $target_file_path; |
78 | | - $req = new HttpRequest($url, $opts ); |
| 80 | + $req = new HttpRequest( $url, $opts ); |
79 | 81 | return $req->doRequest(); |
80 | 82 | } |
81 | 83 | } |
| 84 | + |
82 | 85 | /** |
83 | 86 | * a non blocking request (generally an exit point in the application) |
84 | 87 | * should write to a file location and give updates |
— | — | @@ -87,66 +90,67 @@ |
88 | 91 | global $wgMaxUploadSize, $IP, $wgPhpCliPath; |
89 | 92 | $status = Status::newGood(); |
90 | 93 | |
91 | | - //generate a session id with all the details for the download (pid, target_file_path ) |
| 94 | + // generate a session id with all the details for the download (pid, target_file_path ) |
92 | 95 | $upload_session_key = self::getUploadSessionKey(); |
93 | 96 | $session_id = session_id(); |
94 | 97 | |
95 | | - //store the url and target path: |
96 | | - $_SESSION[ 'wsDownload' ][$upload_session_key]['url'] = $url; |
97 | | - $_SESSION[ 'wsDownload' ][$upload_session_key]['target_file_path'] = $target_file_path; |
| 98 | + // store the url and target path: |
| 99 | + $_SESSION['wsDownload'][$upload_session_key]['url'] = $url; |
| 100 | + $_SESSION['wsDownload'][$upload_session_key]['target_file_path'] = $target_file_path; |
98 | 101 | |
99 | | - if($content_length) |
100 | | - $_SESSION[ 'wsDownload' ][$upload_session_key]['content_length'] = $content_length; |
| 102 | + if( $content_length ) |
| 103 | + $_SESSION['wsDownload'][$upload_session_key]['content_length'] = $content_length; |
101 | 104 | |
102 | | - //set initial loaded bytes: |
103 | | - $_SESSION[ 'wsDownload' ][$upload_session_key]['loaded'] = 0; |
| 105 | + // set initial loaded bytes: |
| 106 | + $_SESSION['wsDownload'][$upload_session_key]['loaded'] = 0; |
104 | 107 | |
105 | | - |
106 | | - //run the background download request: |
| 108 | + // run the background download request: |
107 | 109 | $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. |
110 | | - if(!$pid) |
111 | | - return Status::newFatal('could not run background shell exec'); |
| 110 | + $pid = wfShellBackgroundExec( $cmd, $retval ); |
| 111 | + // the pid is not of much use since we won't be visiting this same apache any-time soon. |
| 112 | + if( !$pid ) |
| 113 | + return Status::newFatal( 'could not run background shell exec' ); |
112 | 114 | |
113 | | - //update the status value with the $upload_session_key (for the user to check on the status of the upload) |
| 115 | + // update the status value with the $upload_session_key (for the user to check on the status of the upload) |
114 | 116 | $status->value = $upload_session_key; |
115 | 117 | |
116 | | - //return good status |
| 118 | + // return good status |
117 | 119 | return $status; |
118 | 120 | } |
| 121 | + |
119 | 122 | function getUploadSessionKey(){ |
120 | 123 | $key = mt_rand( 0, 0x7fffffff ); |
121 | 124 | $_SESSION['wsUploadData'][$key] = array(); |
122 | 125 | return $key; |
123 | 126 | } |
| 127 | + |
124 | 128 | /** |
125 | 129 | * used to run a session based download. Is initiated via the shell. |
126 | 130 | * |
127 | | - * @param string $session_id // the session id to grab download details from |
128 | | - * @param string $upload_session_key //the key of the given upload session |
| 131 | + * @param $session_id String: the session id to grab download details from |
| 132 | + * @param $upload_session_key String: the key of the given upload session |
129 | 133 | * (a given client could have started a few http uploads at once) |
130 | 134 | */ |
131 | 135 | public static function doSessionIdDownload( $session_id, $upload_session_key ){ |
132 | 136 | global $wgUser, $wgEnableWriteAPI, $wgAsyncHTTPTimeout; |
133 | | - wfDebug("\n\ndoSessionIdDownload\n\n"); |
134 | | - //set session to the provided key: |
135 | | - session_id($session_id); |
136 | | - //start the session |
137 | | - if( session_start() === false){ |
138 | | - wfDebug( __METHOD__ . ' could not start session'); |
| 137 | + wfDebug( __METHOD__ . "\n\ndoSessionIdDownload\n\n" ); |
| 138 | + // set session to the provided key: |
| 139 | + session_id( $session_id ); |
| 140 | + // start the session |
| 141 | + if( session_start() === false ){ |
| 142 | + wfDebug( __METHOD__ . ' could not start session' ); |
139 | 143 | } |
140 | 144 | //get all the vars we need from session_id |
141 | 145 | if(!isset($_SESSION[ 'wsDownload' ][$upload_session_key])){ |
142 | 146 | wfDebug( __METHOD__ .' Error:could not find upload session'); |
143 | 147 | exit(); |
144 | 148 | } |
145 | | - //setup the global user from the session key we just inherited |
| 149 | + // setup the global user from the session key we just inherited |
146 | 150 | $wgUser = User::newFromSession(); |
147 | 151 | |
148 | | - //grab the session data to setup the request: |
149 | | - $sd =& $_SESSION[ 'wsDownload' ][$upload_session_key]; |
150 | | - //close down the session so we can other http queries can get session updates: |
| 152 | + // grab the session data to setup the request: |
| 153 | + $sd =& $_SESSION['wsDownload'][$upload_session_key]; |
| 154 | + // close down the session so we can other http queries can get session updates: |
151 | 155 | session_write_close(); |
152 | 156 | |
153 | 157 | $req = new HttpRequest( $sd['url'], array( |
— | — | @@ -154,49 +158,49 @@ |
155 | 159 | 'upload_session_key'=> $upload_session_key, |
156 | 160 | 'timeout' => $wgAsyncHTTPTimeout |
157 | 161 | ) ); |
158 | | - //run the actual request .. (this can take some time) |
159 | | - wfDebug("do Request: " . $sd['url'] . ' tf: ' . $sd['target_file_path'] ); |
| 162 | + // run the actual request .. (this can take some time) |
| 163 | + wfDebug( __METHOD__ . "do Request: " . $sd['url'] . ' tf: ' . $sd['target_file_path'] ); |
160 | 164 | $status = $req->doRequest(); |
161 | 165 | //wfDebug("done with req status is: ". $status->isOK(). ' '.$status->getWikiText(). "\n"); |
162 | 166 | |
163 | | - //start up the session again: |
164 | | - if( session_start() === false){ |
| 167 | + // start up the session again: |
| 168 | + if( session_start() === false ){ |
165 | 169 | wfDebug( __METHOD__ . ' ERROR:: Could not start session'); |
166 | 170 | } |
167 | | - //grab the updated session data pointer |
168 | | - $sd =& $_SESSION[ 'wsDownload' ][$upload_session_key]; |
169 | | - //if error update status: |
| 171 | + // grab the updated session data pointer |
| 172 | + $sd =& $_SESSION['wsDownload'][$upload_session_key]; |
| 173 | + // if error update status: |
170 | 174 | if( !$status->isOK() ){ |
171 | | - $sd['apiUploadResult']= ApiFormatJson::getJsonEncode( |
172 | | - array( 'error' => $status->getWikiText() ) |
173 | | - ); |
| 175 | + $sd['apiUploadResult'] = ApiFormatJson::getJsonEncode( |
| 176 | + array( 'error' => $status->getWikiText() ) |
| 177 | + ); |
174 | 178 | } |
175 | | - //if status oky process upload using fauxReq to api: |
| 179 | + // if status okay process upload using fauxReq to api: |
176 | 180 | if( $status->isOK() ){ |
177 | | - //setup the faxRequest |
| 181 | + // setup the FauxRequest |
178 | 182 | $fauxReqData = $sd['mParams']; |
179 | 183 | $fauxReqData['action'] = 'upload'; |
180 | 184 | $fauxReqData['format'] = 'json'; |
181 | 185 | $fauxReqData['internalhttpsession'] = $upload_session_key; |
182 | | - //evil but no other clean way about it: |
183 | 186 | |
184 | | - $faxReq = new FauxRequest($fauxReqData, true); |
185 | | - $processor = new ApiMain($faxReq, $wgEnableWriteAPI); |
| 187 | + // evil but no other clean way about it: |
| 188 | + $faxReq = new FauxRequest( $fauxReqData, true ); |
| 189 | + $processor = new ApiMain( $faxReq, $wgEnableWriteAPI ); |
186 | 190 | |
187 | 191 | //init the mUpload var for the $processor |
188 | 192 | $processor->execute(); |
189 | 193 | $processor->getResult()->cleanUpUTF8(); |
190 | | - $printer = $processor->createPrinterByName('json'); |
191 | | - $printer->initPrinter(false); |
| 194 | + $printer = $processor->createPrinterByName( 'json' ); |
| 195 | + $printer->initPrinter( false ); |
192 | 196 | ob_start(); |
193 | 197 | $printer->execute(); |
194 | 198 | $apiUploadResult = ob_get_clean(); |
195 | 199 | |
196 | | - wfDebug("\n\n got api result:: $apiUploadResult \n" ); |
197 | | - //the status updates runner will grab the result form the session: |
| 200 | + wfDebug( __METHOD__ . "\n\n got api result:: $apiUploadResult \n" ); |
| 201 | + // the status updates runner will grab the result form the session: |
198 | 202 | $sd['apiUploadResult'] = $apiUploadResult; |
199 | 203 | } |
200 | | - //close the session: |
| 204 | + // close the session: |
201 | 205 | session_write_close(); |
202 | 206 | } |
203 | 207 | |
— | — | @@ -245,16 +249,18 @@ |
246 | 250 | class HttpRequest{ |
247 | 251 | var $target_file_path; |
248 | 252 | var $upload_session_key; |
249 | | - function __construct($url, $opt){ |
| 253 | + |
| 254 | + function __construct( $url, $opt ){ |
250 | 255 | global $wgSyncHTTPTimeout; |
251 | 256 | $this->url = $url; |
252 | | - //set the timeout to default sync timeout (unless the timeout option is provided) |
253 | | - $this->timeout = (isset($opt['timeout']))?$opt['timeout']:$wgSyncHTTPTimeout; |
254 | | - $this->method = (isset($opt['method']))?$opt['method']:'GET'; |
255 | | - $this->target_file_path = (isset($opt['target_file_path']))?$opt['target_file_path']:false; |
256 | | - $this->upload_session_key = (isset($opt['upload_session_key']))?$opt['upload_session_key']:false; |
| 257 | + // set the timeout to default sync timeout (unless the timeout option is provided) |
| 258 | + $this->timeout = ( isset( $opt['timeout'] ) ) ? $opt['timeout'] : $wgSyncHTTPTimeout; |
| 259 | + $this->method = ( isset( $opt['method'] ) ) ? $opt['method'] : 'GET'; |
| 260 | + $this->target_file_path = ( isset( $opt['target_file_path'] ) ) ? $opt['target_file_path'] : false; |
| 261 | + $this->upload_session_key = ( isset( $opt['upload_session_key'] ) ) ? $opt['upload_session_key'] : false; |
257 | 262 | } |
258 | | -/** |
| 263 | + |
| 264 | + /** |
259 | 265 | * Get the contents of a file by HTTP |
260 | 266 | * @param $url string Full URL to act on |
261 | 267 | * @param $Opt associative array Optional array of options: |
— | — | @@ -262,36 +268,35 @@ |
263 | 269 | * 'target_file_path' => if curl should output to a target file |
264 | 270 | * 'adapter' => 'curl', 'soket' |
265 | 271 | */ |
266 | | - public function doRequest() { |
| 272 | + public function doRequest() { |
267 | 273 | # Use curl if available |
268 | 274 | if ( function_exists( 'curl_init' ) ) { |
269 | 275 | return $this->doCurlReq(); |
270 | | - }else{ |
| 276 | + } else { |
271 | 277 | return $this->doPhpReq(); |
272 | 278 | } |
273 | | - } |
274 | | - private function doCurlReq(){ |
| 279 | + } |
| 280 | + |
| 281 | + private function doCurlReq(){ |
275 | 282 | global $wgHTTPProxy, $wgTitle; |
276 | 283 | |
277 | 284 | $status = Status::newGood(); |
278 | 285 | $c = curl_init( $this->url ); |
279 | 286 | |
280 | | - //proxy setup: |
| 287 | + // proxy setup: |
281 | 288 | if ( Http::isLocalURL( $this->url ) ) { |
282 | 289 | curl_setopt( $c, CURLOPT_PROXY, 'localhost:80' ); |
283 | | - } else if ($wgHTTPProxy) { |
284 | | - curl_setopt($c, CURLOPT_PROXY, $wgHTTPProxy); |
| 290 | + } else if ( $wgHTTPProxy ) { |
| 291 | + curl_setopt( $c, CURLOPT_PROXY, $wgHTTPProxy ); |
285 | 292 | } |
286 | 293 | |
287 | 294 | curl_setopt( $c, CURLOPT_TIMEOUT, $this->timeout ); |
288 | | - |
289 | | - |
290 | 295 | curl_setopt( $c, CURLOPT_USERAGENT, Http::userAgent() ); |
291 | 296 | |
292 | 297 | if ( $this->method == 'POST' ) { |
293 | 298 | curl_setopt( $c, CURLOPT_POST, true ); |
294 | 299 | curl_setopt( $c, CURLOPT_POSTFIELDS, '' ); |
295 | | - }else{ |
| 300 | + } else { |
296 | 301 | curl_setopt( $c, CURLOPT_CUSTOMREQUEST, $this->method ); |
297 | 302 | } |
298 | 303 | |
— | — | @@ -304,39 +309,39 @@ |
305 | 310 | curl_setopt( $c, CURLOPT_REFERER, $wgTitle->getFullURL() ); |
306 | 311 | } |
307 | 312 | |
308 | | - //set the write back function (if we are writing to a file) |
| 313 | + // set the write back function (if we are writing to a file) |
309 | 314 | if( $this->target_file_path ){ |
310 | 315 | $cwrite = new simpleFileWriter( $this->target_file_path, $this->upload_session_key ); |
311 | | - if(!$cwrite->status->isOK()){ |
312 | | - wfDebug("ERROR in setting up simpleFileWriter\n"); |
| 316 | + if( !$cwrite->status->isOK() ){ |
| 317 | + wfDebug( __METHOD__ . "ERROR in setting up simpleFileWriter\n" ); |
313 | 318 | $status = $cwrite->status; |
314 | 319 | } |
315 | | - curl_setopt( $c, CURLOPT_WRITEFUNCTION, array($cwrite, 'callbackWriteBody') ); |
| 320 | + curl_setopt( $c, CURLOPT_WRITEFUNCTION, array( $cwrite, 'callbackWriteBody' ) ); |
316 | 321 | } |
317 | 322 | |
318 | | - //start output grabber: |
319 | | - if(!$this->target_file_path) |
| 323 | + // start output grabber: |
| 324 | + if( !$this->target_file_path ) |
320 | 325 | ob_start(); |
321 | 326 | |
322 | 327 | //run the actual curl_exec: |
323 | 328 | try { |
324 | | - if (false === curl_exec($c)) { |
325 | | - $error_txt ='Error sending request: #' . curl_errno($c) .' '. curl_error($c); |
326 | | - wfDebug($error_txt . "\n"); |
327 | | - $status = Status::newFatal( $error_txt); |
328 | | - } |
329 | | - } catch (Exception $e) { |
330 | | - //do something with curl exec error? |
331 | | - } |
332 | | - //if direct request output the results to the stats value: |
| 329 | + if ( false === curl_exec( $c ) ) { |
| 330 | + $error_txt ='Error sending request: #' . curl_errno( $c ) .' '. curl_error( $c ); |
| 331 | + wfDebug( __METHOD__ . $error_txt . "\n" ); |
| 332 | + $status = Status::newFatal( $error_txt ); |
| 333 | + } |
| 334 | + } catch ( Exception $e ) { |
| 335 | + // do something with curl exec error? |
| 336 | + } |
| 337 | + // if direct request output the results to the stats value: |
333 | 338 | if( !$this->target_file_path && $status->isOK() ){ |
334 | | - $status->value = ob_get_contents(); |
| 339 | + $status->value = ob_get_contents(); |
335 | 340 | ob_end_clean(); |
336 | 341 | } |
337 | | - //if we wrote to a target file close up or return error |
| 342 | + // if we wrote to a target file close up or return error |
338 | 343 | if( $this->target_file_path ){ |
339 | 344 | $cwrite->close(); |
340 | | - if( ! $cwrite->status->isOK() ){ |
| 345 | + if( !$cwrite->status->isOK() ){ |
341 | 346 | return $cwrite->status; |
342 | 347 | } |
343 | 348 | } |
— | — | @@ -356,9 +361,10 @@ |
357 | 362 | } |
358 | 363 | curl_close( $c ); |
359 | 364 | |
360 | | - //return the result obj |
| 365 | + // return the result obj |
361 | 366 | return $status; |
362 | 367 | } |
| 368 | + |
363 | 369 | public function doPhpReq(){ |
364 | 370 | #$use file_get_contents... |
365 | 371 | # This doesn't have local fetch capabilities... |
— | — | @@ -376,57 +382,59 @@ |
377 | 383 | $ctx = stream_context_create( $opts ); |
378 | 384 | |
379 | 385 | $status->value = file_get_contents( $url, false, $ctx ); |
380 | | - if(!$status->value){ |
381 | | - $status->error('file_get_contents-failed'); |
| 386 | + if( !$status->value ){ |
| 387 | + $status->error( 'file_get_contents-failed' ); |
382 | 388 | } |
383 | 389 | return $status; |
384 | 390 | } |
| 391 | + |
385 | 392 | } |
| 393 | + |
386 | 394 | /** |
387 | 395 | * a simpleFileWriter with session id updates |
388 | | - * |
389 | 396 | */ |
390 | | -class simpleFileWriter{ |
| 397 | +class simpleFileWriter { |
391 | 398 | var $target_file_path; |
392 | 399 | var $status = null; |
393 | 400 | var $session_id = null; |
394 | | - var $session_update_interval = 0; //how offten to update the session while downloading |
| 401 | + var $session_update_interval = 0; // how often to update the session while downloading |
395 | 402 | |
396 | | - function simpleFileWriter($target_file_path, $upload_session_key){ |
| 403 | + function simpleFileWriter( $target_file_path, $upload_session_key ){ |
397 | 404 | $this->target_file_path = $target_file_path; |
398 | 405 | $this->upload_session_key = $upload_session_key; |
399 | 406 | $this->status = Status::newGood(); |
400 | | - //open the file: |
401 | | - $this->fp = fopen( $this->target_file_path, 'w'); |
| 407 | + // open the file: |
| 408 | + $this->fp = fopen( $this->target_file_path, 'w' ); |
402 | 409 | if( $this->fp === false ){ |
403 | | - $this->status = Status::newFatal('HTTP::could-not-open-file-for-writing'); |
| 410 | + $this->status = Status::newFatal( 'HTTP::could-not-open-file-for-writing' ); |
404 | 411 | } |
405 | | - //true start time |
| 412 | + // true start time |
406 | 413 | $this->prevTime = time(); |
407 | 414 | } |
| 415 | + |
408 | 416 | public function callbackWriteBody($ch, $data_packet){ |
409 | 417 | global $wgMaxUploadSize; |
410 | 418 | |
411 | | - //write out the content |
412 | | - if( fwrite($this->fp, $data_packet) === false){ |
413 | | - wfDebug(__METHOD__ ." ::could-not-write-to-file\n"); |
414 | | - $this->status = Status::newFatal('HTTP::could-not-write-to-file'); |
| 419 | + // write out the content |
| 420 | + if( fwrite( $this->fp, $data_packet ) === false ){ |
| 421 | + wfDebug( __METHOD__ ." ::could-not-write-to-file\n" ); |
| 422 | + $this->status = Status::newFatal( 'HTTP::could-not-write-to-file' ); |
415 | 423 | return 0; |
416 | 424 | } |
417 | 425 | |
418 | | - //check file size: |
| 426 | + // check file size: |
419 | 427 | clearstatcache(); |
420 | | - $this->current_fsize = filesize( $this->target_file_path); |
| 428 | + $this->current_fsize = filesize( $this->target_file_path ); |
421 | 429 | |
422 | | - if( $this->current_fsize > $wgMaxUploadSize){ |
423 | | - wfDebug( __METHOD__ . " ::http download too large\n"); |
424 | | - $this->status = Status::newFatal('HTTP::file-has-grown-beyond-upload-limit-killing: downloaded more than ' . |
425 | | - Language::formatSize($wgMaxUploadSize) . ' '); |
| 430 | + if( $this->current_fsize > $wgMaxUploadSize ){ |
| 431 | + wfDebug( __METHOD__ . " ::http download too large\n" ); |
| 432 | + $this->status = Status::newFatal( 'HTTP::file-has-grown-beyond-upload-limit-killing: downloaded more than ' . |
| 433 | + Language::formatSize( $wgMaxUploadSize ) . ' ' ); |
426 | 434 | return 0; |
427 | 435 | } |
428 | 436 | |
429 | | - //if more than session_update_interval second have passed update_session_progress |
430 | | - if($this->upload_session_key && ( (time() - $this->prevTime) > $this->session_update_interval )) { |
| 437 | + // if more than session_update_interval second have passed update_session_progress |
| 438 | + if( $this->upload_session_key && ( ( time() - $this->prevTime ) > $this->session_update_interval ) ) { |
431 | 439 | $this->prevTime = time(); |
432 | 440 | $session_status = $this->update_session_progress(); |
433 | 441 | if( !$session_status->isOK() ){ |
— | — | @@ -437,32 +445,35 @@ |
438 | 446 | } |
439 | 447 | return strlen( $data_packet ); |
440 | 448 | } |
| 449 | + |
441 | 450 | public function update_session_progress(){ |
442 | 451 | $status = Status::newGood(); |
443 | | - //start the session |
| 452 | + // start the session |
444 | 453 | if( session_start() === false){ |
445 | | - wfDebug( __METHOD__ . ' could not start session'); |
446 | | - exit(0); |
| 454 | + wfDebug( __METHOD__ . ' could not start session' ); |
| 455 | + exit( 0 ); |
447 | 456 | } |
448 | | - $sd =& $_SESSION[ 'wsDownload' ][ $this->upload_session_key ]; |
449 | | - //check if the user canceled the request: |
| 457 | + $sd =& $_SESSION['wsDownload'][$this->upload_session_key]; |
| 458 | + // check if the user canceled the request: |
450 | 459 | if( $sd['user_cancel'] == true ){ |
451 | | - //kill the download |
452 | | - return Status::newFatal('user-canceled-request'); |
| 460 | + // kill the download |
| 461 | + return Status::newFatal( 'user-canceled-request' ); |
453 | 462 | } |
454 | | - //update the progress bytes download so far: |
| 463 | + // update the progress bytes download so far: |
455 | 464 | $sd['loaded'] = $this->current_fsize; |
456 | | - wfDebug('set session loaded amount to: ' . $sd['loaded'] . "\n"); |
457 | | - //close down the session so we can other http queries can get session updates: |
| 465 | + wfDebug( __METHOD__ . ': set session loaded amount to: ' . $sd['loaded'] . "\n"); |
| 466 | + // close down the session so we can other http queries can get session updates: |
458 | 467 | session_write_close(); |
459 | 468 | return $status; |
460 | 469 | } |
| 470 | + |
461 | 471 | public function close(){ |
462 | | - //do a final session update: |
| 472 | + // do a final session update: |
463 | 473 | $this->update_session_progress(); |
464 | | - //close up the file handle: |
465 | | - if(false === fclose( $this->fp )){ |
466 | | - $this->status = Status::newFatal('HTTP::could-not-close-file'); |
| 474 | + // close up the file handle: |
| 475 | + if( false === fclose( $this->fp ) ){ |
| 476 | + $this->status = Status::newFatal( 'HTTP::could-not-close-file' ); |
467 | 477 | } |
468 | 478 | } |
| 479 | + |
469 | 480 | } |
\ No newline at end of file |
Index: trunk/phase3/includes/OutputPage.php |
— | — | @@ -15,7 +15,7 @@ |
16 | 16 | var $mCategoryLinks = array(), $mLanguageLinks = array(); |
17 | 17 | |
18 | 18 | var $mScriptLoaderClassList = array(); |
19 | | - //the most recent id of any script that is grouped in the script request |
| 19 | + // the most recent id of any script that is grouped in the script request |
20 | 20 | var $mLatestScriptRevID = 0; |
21 | 21 | |
22 | 22 | var $mScripts = '', $mLinkColours, $mPageLinkTitle = '', $mHeadItems = array(); |
— | — | @@ -93,8 +93,7 @@ |
94 | 94 | function addKeyword( $text ) { |
95 | 95 | if( is_array( $text )) { |
96 | 96 | $this->mKeywords = array_merge( $this->mKeywords, $text ); |
97 | | - } |
98 | | - else { |
| 97 | + } else { |
99 | 98 | array_push( $this->mKeywords, $text ); |
100 | 99 | } |
101 | 100 | } |
— | — | @@ -116,26 +115,27 @@ |
117 | 116 | if( substr( $file, 0, 1 ) == '/' ) { |
118 | 117 | $path = $file; |
119 | 118 | } else { |
120 | | - $path = "{$wgStylePath}/common/{$file}"; |
| 119 | + $path = "{$wgStylePath}/common/{$file}"; |
121 | 120 | } |
| 121 | + |
122 | 122 | if( $wgEnableScriptLoader ){ |
123 | | - if( strpos($path, $wgScript) !== false ){ |
124 | | - $reqPath = str_replace($wgScript.'?', '', $path); |
125 | | - $reqArgs = split('&', $reqPath); |
| 123 | + if( strpos( $path, $wgScript ) !== false ){ |
| 124 | + $reqPath = str_replace( $wgScript . '?', '', $path ); |
| 125 | + $reqArgs = split( '&', $reqPath ); |
126 | 126 | $reqSet = array(); |
127 | 127 | |
128 | | - foreach($reqArgs as $arg){ |
129 | | - list($key, $var) = split('=', $arg); |
130 | | - $reqSet[$key]= $var; |
| 128 | + foreach( $reqArgs as $arg ){ |
| 129 | + list( $key, $var ) = split( '=', $arg ); |
| 130 | + $reqSet[$key] = $var; |
131 | 131 | } |
132 | 132 | |
133 | | - if( isset( $reqSet['title'] ) && $reqSet != '' ) { |
134 | | - //extract any extra param (for now just skin) |
135 | | - $ext_param = ( isset( $reqSet['useskin'] ) && $reqSet['useskin'] != '') ? '|useskin=' . ucfirst( $reqSet['useskin'] ) : ''; |
| 133 | + if( isset( $reqSet['title'] ) && $reqSet != '' ) { |
| 134 | + // extract any extra param (for now just skin) |
| 135 | + $ext_param = ( isset( $reqSet['useskin'] ) && $reqSet['useskin'] != '' ) ? '|useskin=' . ucfirst( $reqSet['useskin'] ) : ''; |
136 | 136 | $this->mScriptLoaderClassList[] = 'WT:' . $reqSet['title'] . $ext_param ; |
137 | | - //add the title revision to the key |
| 137 | + // add the title revision to the key |
138 | 138 | $t = Title::newFromText( $reqSet['title'] ); |
139 | | - //if there is no title (don't worry we just use the $wgStyleVersion var (which should be updated on relevant commits) |
| 139 | + // if there is no title (don't worry we just use the $wgStyleVersion var (which should be updated on relevant commits) |
140 | 140 | if( $t->exists() ){ |
141 | 141 | if( $t->getLatestRevID() > $this->mLatestScriptRevID ) |
142 | 142 | $this->mLatestScriptRevID = $t->getLatestRevID(); |
— | — | @@ -143,16 +143,17 @@ |
144 | 144 | return true; |
145 | 145 | } |
146 | 146 | } |
147 | | - //check for class from path: |
| 147 | + |
| 148 | + // check for class from path: |
148 | 149 | $js_class = $this->getJsClassFromPath( $path ); |
149 | 150 | if( $js_class ){ |
150 | | - //add to the class list: |
| 151 | + // add to the class list: |
151 | 152 | $this->mScriptLoaderClassList[] = $js_class; |
152 | 153 | return true; |
153 | 154 | } |
154 | 155 | } |
155 | | - //die(); |
156 | | - //if the script loader did not find a way to add the script than add using AddScript |
| 156 | + |
| 157 | + // if the script loader did not find a way to add the script than add using addScript |
157 | 158 | $this->addScript( |
158 | 159 | Xml::element( 'script', |
159 | 160 | array( |
— | — | @@ -163,49 +164,57 @@ |
164 | 165 | ) |
165 | 166 | ); |
166 | 167 | } |
167 | | - /* |
| 168 | + |
| 169 | + /** |
168 | 170 | * This is the core script that is included on every page |
169 | 171 | * (they are requested separately to improve caching across |
170 | 172 | * different page load types (edit, upload, view, etc) |
171 | 173 | */ |
172 | 174 | function addCoreScripts2Top(){ |
173 | | - global $wgEnableScriptLoader, $wgStyleVersion,$wgJSAutoloadLocalClasses, $wgJsMimeType, $wgScriptPath, $wgEnableJS2system ; |
174 | | - //@@todo we should depricate wikibits in favor of mv_embed and native jQuery functions |
| 175 | + global $wgEnableScriptLoader, $wgStyleVersion, $wgJSAutoloadLocalClasses, $wgJsMimeType, $wgScriptPath, $wgEnableJS2system; |
| 176 | + //@@todo we should deprecate wikibits in favor of mv_embed and native jQuery functions |
175 | 177 | |
176 | 178 | if( $wgEnableJS2system ){ |
177 | | - $core_classes = array('window.jQuery', 'mv_embed', 'wikibits'); |
178 | | - }else{ |
179 | | - $core_classes = array('wikibits'); |
| 179 | + $core_classes = array( 'window.jQuery', 'mv_embed', 'wikibits' ); |
| 180 | + } else { |
| 181 | + $core_classes = array( 'wikibits' ); |
180 | 182 | } |
| 183 | + |
181 | 184 | if( $wgEnableScriptLoader ){ |
182 | 185 | $this->mScripts = $this->getScriptLoaderJs( $core_classes ) . $this->mScripts; |
183 | | - }else{ |
| 186 | + } else { |
184 | 187 | $so = ''; |
185 | | - foreach($core_classes as $s){ |
186 | | - if(isset( $wgJSAutoloadLocalClasses[$s] )){ |
187 | | - $so.= Xml::element( 'script', array( |
188 | | - 'type' => $wgJsMimeType, |
189 | | - 'src' => "{$wgScriptPath}/{$wgJSAutoloadLocalClasses[$s]}?" . $this->getURIDparam() |
190 | | - ), |
191 | | - '', false |
192 | | - ); |
| 188 | + foreach( $core_classes as $s ){ |
| 189 | + if( isset( $wgJSAutoloadLocalClasses[$s] ) ){ |
| 190 | + $so.= Xml::element( 'script', array( |
| 191 | + 'type' => $wgJsMimeType, |
| 192 | + 'src' => "{$wgScriptPath}/{$wgJSAutoloadLocalClasses[$s]}?" . $this->getURIDparam() |
| 193 | + ), |
| 194 | + '', false |
| 195 | + ); |
193 | 196 | } |
194 | 197 | } |
195 | 198 | $this->mScripts = $so . $this->mScripts; |
196 | 199 | } |
197 | 200 | } |
| 201 | + |
| 202 | + /** |
| 203 | + * @param $js_class String: name of JavaScript class |
| 204 | + * @return Boolean: false if class wasn't found, true on success |
| 205 | + */ |
198 | 206 | function addScriptClass( $js_class ){ |
199 | 207 | global $wgJSAutoloadClasses, $wgJSAutoloadLocalClasses, $wgJsMimeType, |
200 | 208 | $wgEnableScriptLoader, $wgStyleVersion, $wgScriptPath; |
201 | | - if(isset($wgJSAutoloadClasses[ $js_class ] ) || isset( $wgJSAutoloadLocalClasses[$js_class]) ){ |
202 | | - if($wgEnableScriptLoader){ |
203 | | - if( ! in_array( $js_class, $this->mScriptLoaderClassList ) ){ |
| 209 | + |
| 210 | + if( isset( $wgJSAutoloadClasses[$js_class] ) || isset( $wgJSAutoloadLocalClasses[$js_class] ) ){ |
| 211 | + if( $wgEnableScriptLoader ){ |
| 212 | + if( !in_array( $js_class, $this->mScriptLoaderClassList ) ){ |
204 | 213 | $this->mScriptLoaderClassList[] = $js_class; |
205 | 214 | } |
206 | | - }else{ |
207 | | - //do a normal load of without the script-loader: |
| 215 | + } else { |
| 216 | + // do a normal load of without the script-loader: |
208 | 217 | $path = $wgScriptPath . '/'; |
209 | | - $path.= isset($wgJSAutoloadClasses[ $js_class ] )?$wgJSAutoloadClasses[ $js_class ]: |
| 218 | + $path.= isset( $wgJSAutoloadClasses[$js_class] ) ? $wgJSAutoloadClasses[$js_class]: |
210 | 219 | $wgJSAutoloadLocalClasses[$js_class]; |
211 | 220 | $this->addScript( |
212 | 221 | Xml::element( 'script', |
— | — | @@ -219,25 +228,26 @@ |
220 | 229 | } |
221 | 230 | return true; |
222 | 231 | } |
223 | | - wfDebug( __METHOD__ . " could not find js_class: " . $js_class ); |
224 | | - return false; //could not find the class |
| 232 | + wfDebug( __METHOD__ . ' could not find js_class: ' . $js_class ); |
| 233 | + return false; // could not find the class |
225 | 234 | } |
| 235 | + |
226 | 236 | /** |
227 | 237 | * gets the scriptLoader javascript include |
228 | | - * |
| 238 | + * @param $forcClassAry Boolean: false by default |
229 | 239 | */ |
230 | | - function getScriptLoaderJs( $forceClassAry=false ){ |
| 240 | + function getScriptLoaderJs( $forceClassAry = false ){ |
231 | 241 | global $wgScriptPath, $wgJsMimeType, $wgStyleVersion, $wgRequest, $wgDebugJavaScript; |
232 | 242 | |
233 | | - if(!$forceClassAry){ |
234 | | - $class_list = implode(',', $this->mScriptLoaderClassList ); |
235 | | - }else{ |
236 | | - $class_list = implode(',', $forceClassAry ); |
| 243 | + if( !$forceClassAry ){ |
| 244 | + $class_list = implode( ',', $this->mScriptLoaderClassList ); |
| 245 | + } else { |
| 246 | + $class_list = implode( ',', $forceClassAry ); |
237 | 247 | } |
238 | 248 | |
239 | 249 | $debug_param = ( $wgDebugJavaScript || |
240 | | - $wgRequest->getVal('debug')=='true' || |
241 | | - $wgRequest->getVal('debug')=='1' ) |
| 250 | + $wgRequest->getVal( 'debug' ) == 'true' || |
| 251 | + $wgRequest->getVal( 'debug' ) == '1' ) |
242 | 252 | ? '&debug=true' : ''; |
243 | 253 | |
244 | 254 | //@@todo intelligent unique id generation based on svn version of file (rather than just grabbing the $wgStyleVersion var) |
— | — | @@ -254,25 +264,28 @@ |
255 | 265 | '', false |
256 | 266 | ); |
257 | 267 | } |
| 268 | + |
258 | 269 | function getURIDparam(){ |
259 | | - global $wgDebugJavaScript,$wgStyleVersion; |
| 270 | + global $wgDebugJavaScript, $wgStyleVersion; |
260 | 271 | if( $wgDebugJavaScript ){ |
261 | | - return "urid=". time(); |
262 | | - }else{ |
| 272 | + return 'urid=' . time(); |
| 273 | + } else { |
263 | 274 | return "urid={$wgStyleVersion}_{$this->mLatestScriptRevID}"; |
264 | 275 | } |
265 | 276 | } |
| 277 | + |
266 | 278 | function getJsClassFromPath( $path ){ |
267 | 279 | global $wgJSAutoloadClasses, $wgJSAutoloadLocalClasses, $wgScriptPath; |
268 | 280 | |
269 | 281 | $scriptLoaderPaths = array_merge( $wgJSAutoloadClasses, $wgJSAutoloadLocalClasses ); |
270 | 282 | foreach( $scriptLoaderPaths as $js_class => $js_path ){ |
271 | 283 | $js_path = "{$wgScriptPath}/{$js_path}"; |
272 | | - if($path == $js_path) |
| 284 | + if( $path == $js_path ) |
273 | 285 | return $js_class; |
274 | 286 | } |
275 | 287 | return false; |
276 | 288 | } |
| 289 | + |
277 | 290 | /** |
278 | 291 | * Add a self-contained script tag with the given contents |
279 | 292 | * @param string $script JavaScript text, no <script> tags |
— | — | @@ -285,9 +298,9 @@ |
286 | 299 | function getScript() { |
287 | 300 | global $wgEnableScriptLoader; |
288 | 301 | if( $wgEnableScriptLoader ){ |
289 | | - //include $this->mScripts (for anything that we could not package into the scriptloader |
290 | | - return $this->mScripts ."\n". $this->getScriptLoaderJs() . $this->getHeadItems(); |
291 | | - }else{ |
| 302 | + //include $this->mScripts (for anything that we could not package into the scriptloader |
| 303 | + return $this->mScripts . "\n" . $this->getScriptLoaderJs() . $this->getHeadItems(); |
| 304 | + } else { |
292 | 305 | return $this->mScripts . $this->getHeadItems(); |
293 | 306 | } |
294 | 307 | } |
— | — | @@ -1117,7 +1130,7 @@ |
1118 | 1131 | |
1119 | 1132 | $sk = $wgUser->getSkin(); |
1120 | 1133 | |
1121 | | - //add our core scripts to output |
| 1134 | + // add our core scripts to output |
1122 | 1135 | $this->addCoreScripts2Top(); |
1123 | 1136 | |
1124 | 1137 | if ( $wgUseAjax ) { |
— | — | @@ -1915,9 +1928,15 @@ |
1916 | 1929 | $options['dir'] = $dir; |
1917 | 1930 | $this->styles[$style] = $options; |
1918 | 1931 | } |
| 1932 | + |
| 1933 | + /** |
| 1934 | + * Adds inline CSS styles |
| 1935 | + * @param $style_css Mixed: inline CSS |
| 1936 | + */ |
1919 | 1937 | public function addInlineStyle( $style_css ){ |
1920 | 1938 | $this->mScripts .= "<style type=\"text/css\">$style_css</style>"; |
1921 | 1939 | } |
| 1940 | + |
1922 | 1941 | /** |
1923 | 1942 | * Build a set of <link>s for the stylesheets specified in the $this->styles array. |
1924 | 1943 | * These will be applied to various media & IE conditionals. |
Index: trunk/phase3/includes/AutoLoader.php |
— | — | @@ -580,12 +580,12 @@ |
581 | 581 | |
582 | 582 | ); |
583 | 583 | |
584 | | -//autoloader for javascript files (path is from the mediawiki folder |
| 584 | +// Autoloader for JavaScript files (path is from the MediaWiki folder) |
585 | 585 | global $wgJSAutoloadLocalClasses; |
586 | 586 | $wgJSAutoloadLocalClasses = array( |
587 | | - 'ajax' => 'skins/common/ajax.js', |
588 | | - 'ajaxwatch' => 'skins/common/ajaxwatch.js', |
589 | | - 'allmessages' => 'skins/common/allmessages.js', |
| 587 | + 'ajax' => 'skins/common/ajax.js', |
| 588 | + 'ajaxwatch' => 'skins/common/ajaxwatch.js', |
| 589 | + 'allmessages' => 'skins/common/allmessages.js', |
590 | 590 | 'block' => 'skins/common/block.js', |
591 | 591 | 'changepassword' => 'skins/common/changepassword.js', |
592 | 592 | 'diff' => 'skins/common/diff.js', |
— | — | @@ -599,13 +599,13 @@ |
600 | 600 | 'preview' => 'skins/common/preview.js', |
601 | 601 | 'protect' => 'skins/common/protect.js', |
602 | 602 | 'rightclickedit' => 'skins/common/rightclickedit.js', |
603 | | - 'sticky' => 'skins/common/sticky.js', |
| 603 | + 'sticky' => 'skins/common/sticky.js', |
604 | 604 | 'upload' => 'skins/common/upload.js', |
605 | 605 | 'wikibits' => 'skins/common/wikibits.js', |
606 | 606 | |
607 | | - //phase 2 javascript: |
| 607 | + // phase 2 javascript: |
608 | 608 | 'uploadPage' => 'js2/uploadPage.js', |
609 | | - 'editPage' => 'js2/editPage.js', |
| 609 | + 'editPage' => 'js2/editPage.js', |
610 | 610 | ); |
611 | 611 | |
612 | 612 | //Include the js2 autoLoadClasses |
Index: trunk/phase3/includes/api/ApiUpload.php |
— | — | @@ -22,35 +22,33 @@ |
23 | 23 | * http://www.gnu.org/copyleft/gpl.html |
24 | 24 | */ |
25 | 25 | |
26 | | -if (!defined('MEDIAWIKI')) { |
| 26 | +if ( !defined( 'MEDIAWIKI' ) ) { |
27 | 27 | // Eclipse helper - will be ignored in production |
28 | | - require_once ("ApiBase.php"); |
| 28 | + require_once("ApiBase.php"); |
29 | 29 | } |
30 | 30 | |
31 | | - |
32 | 31 | /** |
33 | 32 | * @ingroup API |
34 | 33 | */ |
35 | 34 | class ApiUpload extends ApiBase { |
36 | 35 | var $mUpload = null; |
37 | 36 | |
38 | | - public function __construct($main, $action) { |
39 | | - parent :: __construct($main, $action); |
| 37 | + public function __construct( $main, $action ) { |
| 38 | + parent::__construct( $main, $action ); |
40 | 39 | } |
41 | 40 | |
42 | 41 | public function execute() { |
43 | 42 | global $wgUser; |
44 | 43 | |
45 | | - |
46 | 44 | $this->getMain()->isWriteMode(); |
47 | 45 | $this->mParams = $this->extractRequestParams(); |
48 | 46 | $request = $this->getMain()->getRequest(); |
49 | 47 | |
50 | | - //do token checks: |
51 | | - if(is_null($this->mParams['token'])) |
52 | | - $this->dieUsageMsg(array('missingparam', 'token')); |
53 | | - if(!$wgUser->matchEditToken($this->mParams['token'])) |
54 | | - $this->dieUsageMsg(array('sessionfailure')); |
| 48 | + // do token checks: |
| 49 | + if( is_null( $this->mParams['token'] ) ) |
| 50 | + $this->dieUsageMsg( array( 'missingparam', 'token' ) ); |
| 51 | + if( !$wgUser->matchEditToken( $this->mParams['token'] ) ) |
| 52 | + $this->dieUsageMsg( array( 'sessionfailure' ) ); |
55 | 53 | |
56 | 54 | |
57 | 55 | // Add the uploaded file to the params array |
— | — | @@ -60,24 +58,24 @@ |
61 | 59 | if( !UploadBase::isEnabled() ) |
62 | 60 | $this->dieUsageMsg( array( 'uploaddisabled' ) ); |
63 | 61 | |
64 | | - wfDebug("running require param\n"); |
| 62 | + wfDebug( __METHOD__ . "running require param\n" ); |
65 | 63 | // One and only one of the following parameters is needed |
66 | 64 | $this->requireOnlyOneParameter( $this->mParams, |
67 | 65 | 'sessionkey', 'file', 'url', 'enablechunks' ); |
68 | 66 | |
69 | 67 | if( $this->mParams['enablechunks'] ){ |
70 | | - //chunks upload enabled |
| 68 | + // chunks upload enabled |
71 | 69 | $this->mUpload = new UploadFromChunks(); |
72 | 70 | $this->mUpload->initializeFromParams( $this->mParams, $request ); |
73 | 71 | |
74 | 72 | //if getAPIresult did not exit report the status error: |
75 | | - if( isset( $this->mUpload->status[ 'error' ] ) ) |
76 | | - $this->dieUsageMsg( $this->mUpload->status[ 'error' ] ); |
| 73 | + if( isset( $this->mUpload->status['error'] ) ) |
| 74 | + $this->dieUsageMsg( $this->mUpload->status['error'] ); |
77 | 75 | |
78 | | - }else if( $this->mParams['internalhttpsession'] ){ |
79 | | - $sd = & $_SESSION['wsDownload'][ $this->mParams['internalhttpsession'] ]; |
| 76 | + } else if( $this->mParams['internalhttpsession'] ){ |
| 77 | + $sd = & $_SESSION['wsDownload'][$this->mParams['internalhttpsession']]; |
80 | 78 | |
81 | | - //get the params from the init session: |
| 79 | + // get the params from the init session: |
82 | 80 | $this->mUpload = new UploadFromFile(); |
83 | 81 | |
84 | 82 | $this->mUpload->initialize( $this->mParams['filename'], |
— | — | @@ -88,32 +86,32 @@ |
89 | 87 | if( !isset( $this->mUpload ) ) |
90 | 88 | $this->dieUsage( 'No upload module set', 'nomodule' ); |
91 | 89 | |
92 | | - }else if( $this->mParams['httpstatus'] && $this->mParams['sessionkey']){ |
93 | | - //return the status of the given upload session_key: |
94 | | - if(!isset($_SESSION['wsDownload'][ $this->mParams['sessionkey'] ])){ |
| 90 | + } else if( $this->mParams['httpstatus'] && $this->mParams['sessionkey'] ){ |
| 91 | + // return the status of the given upload session_key: |
| 92 | + if( !isset( $_SESSION['wsDownload'][ $this->mParams['sessionkey'] ] ) ){ |
95 | 93 | return $this->getResult()->addValue( null, $this->getModuleName(), |
96 | 94 | array( 'error' => 'invalid-session-key' |
97 | 95 | )); |
98 | 96 | } |
99 | | - $sd = & $_SESSION['wsDownload'][ $this->mParams['sessionkey'] ]; |
100 | | - //keep passing down the upload sessionkey |
| 97 | + $sd = & $_SESSION['wsDownload'][$this->mParams['sessionkey']]; |
| 98 | + // keep passing down the upload sessionkey |
101 | 99 | $statusResult = array( |
102 | 100 | 'upload_session_key' => $this->mParams['sessionkey'] |
103 | 101 | ); |
104 | 102 | |
105 | | - //put values into the final apiResult if available |
106 | | - if( isset($sd['apiUploadResult'])) $statusResult['apiUploadResult'] = $sd['apiUploadResult']; |
107 | | - if( isset($sd['loaded']) ) $statusResult['loaded'] = $sd['loaded']; |
108 | | - if( isset($sd['content_length']) ) $statusResult['content_length'] = $sd['content_length']; |
| 103 | + // put values into the final apiResult if available |
| 104 | + if( isset( $sd['apiUploadResult'] ) ) $statusResult['apiUploadResult'] = $sd['apiUploadResult']; |
| 105 | + if( isset( $sd['loaded'] ) ) $statusResult['loaded'] = $sd['loaded']; |
| 106 | + if( isset( $sd['content_length'] ) ) $statusResult['content_length'] = $sd['content_length']; |
109 | 107 | |
110 | 108 | return $this->getResult()->addValue( null, $this->getModuleName(), |
111 | 109 | $statusResult |
112 | 110 | ); |
113 | | - }else if( $this->mParams['sessionkey'] ) { |
| 111 | + } else if( $this->mParams['sessionkey'] ) { |
114 | 112 | // Stashed upload |
115 | 113 | $this->mUpload = new UploadFromStash(); |
116 | 114 | $this->mUpload->initialize( $this->mParams['filename'], $_SESSION['wsUploadData'][$this->mParams['sessionkey']] ); |
117 | | - }else{ |
| 115 | + } else { |
118 | 116 | // Upload from url or file |
119 | 117 | // Parameter filename is required |
120 | 118 | if( !isset( $this->mParams['filename'] ) ) |
— | — | @@ -130,7 +128,7 @@ |
131 | 129 | } elseif( isset( $this->mParams['url'] ) ) { |
132 | 130 | |
133 | 131 | $this->mUpload = new UploadFromUrl(); |
134 | | - $this->mUpload->initialize( $this->mParams['filename'], $this->mParams['url'], $this->mParams['asyncdownload']); |
| 132 | + $this->mUpload->initialize( $this->mParams['filename'], $this->mParams['url'], $this->mParams['asyncdownload'] ); |
135 | 133 | |
136 | 134 | $status = $this->mUpload->fetchFile(); |
137 | 135 | if( !$status->isOK() ){ |
— | — | @@ -139,13 +137,13 @@ |
140 | 138 | //check if we doing a async request set session info and return the upload_session_key) |
141 | 139 | if( $this->mUpload->isAsync() ){ |
142 | 140 | $upload_session_key = $status->value; |
143 | | - //update the session with anything with the params we will need to finish up the upload later on: |
144 | | - if(!isset($_SESSION['wsDownload'][$upload_session_key])) |
| 141 | + // update the session with anything with the params we will need to finish up the upload later on: |
| 142 | + if( !isset( $_SESSION['wsDownload'][$upload_session_key] ) ) |
145 | 143 | $_SESSION['wsDownload'][$upload_session_key] = array(); |
146 | 144 | |
147 | 145 | $sd =& $_SESSION['wsDownload'][$upload_session_key]; |
148 | 146 | |
149 | | - //copy mParams for finishing up after: |
| 147 | + // copy mParams for finishing up after: |
150 | 148 | $sd['mParams'] = $this->mParams; |
151 | 149 | |
152 | 150 | return $this->getResult()->addValue( null, $this->getModuleName(), |
— | — | @@ -162,12 +160,12 @@ |
163 | 161 | //finish up the exec command: |
164 | 162 | $this->doExecUpload(); |
165 | 163 | } |
| 164 | + |
166 | 165 | function doExecUpload(){ |
167 | 166 | global $wgUser; |
168 | | - //Check whether the user has the appropriate permissions to upload anyway |
| 167 | + // Check whether the user has the appropriate permissions to upload anyway |
169 | 168 | $permission = $this->mUpload->isAllowed( $wgUser ); |
170 | 169 | |
171 | | - |
172 | 170 | if( $permission !== true ) { |
173 | 171 | if( !$wgUser->isLoggedIn() ) |
174 | 172 | $this->dieUsageMsg( array( 'mustbeloggedin', 'upload' ) ); |
— | — | @@ -180,6 +178,7 @@ |
181 | 179 | $this->mUpload->cleanupTempFile(); |
182 | 180 | $this->getResult()->addValue( null, $this->getModuleName(), $result ); |
183 | 181 | } |
| 182 | + |
184 | 183 | private function performUpload() { |
185 | 184 | global $wgUser; |
186 | 185 | $result = array(); |
— | — | @@ -235,6 +234,7 @@ |
236 | 235 | } |
237 | 236 | return $result; |
238 | 237 | } |
| 238 | + |
239 | 239 | if( !$this->mParams['ignorewarnings'] ) { |
240 | 240 | $warnings = $this->mUpload->checkWarnings(); |
241 | 241 | if( $warnings ) { |
— | — | @@ -251,7 +251,8 @@ |
252 | 252 | return $result; |
253 | 253 | } |
254 | 254 | } |
255 | | - //do the upload |
| 255 | + |
| 256 | + // do the upload |
256 | 257 | $status = $this->mUpload->performUpload( $this->mParams['comment'], |
257 | 258 | $this->mParams['comment'], $this->mParams['watch'], $wgUser ); |
258 | 259 | |
— | — | @@ -267,17 +268,16 @@ |
268 | 269 | $result['result'] = 'Success'; |
269 | 270 | $result['filename'] = $file->getName(); |
270 | 271 | |
271 | | - |
272 | 272 | // Append imageinfo to the result |
273 | 273 | |
274 | | - //might be a cleaner way to call this: |
| 274 | + // might be a cleaner way to call this: |
275 | 275 | $imParam = ApiQueryImageInfo::getAllowedParams(); |
276 | | - $imProp = $imParam['prop'][ApiBase :: PARAM_TYPE]; |
| 276 | + $imProp = $imParam['prop'][ApiBase::PARAM_TYPE]; |
277 | 277 | $result['imageinfo'] = ApiQueryImageInfo::getInfo( $file, |
278 | 278 | array_flip( $imProp ), |
279 | 279 | $this->getResult() ); |
280 | 280 | |
281 | | - wfDebug("\n\n return result: " . print_r($result, true)); |
| 281 | + wfDebug( "\n\n return result: " . print_r( $result, true ) ); |
282 | 282 | |
283 | 283 | return $result; |
284 | 284 | } |
— | — | @@ -287,35 +287,35 @@ |
288 | 288 | } |
289 | 289 | |
290 | 290 | public function getAllowedParams() { |
291 | | - return array ( |
| 291 | + return array( |
292 | 292 | 'filename' => null, |
293 | 293 | 'file' => null, |
294 | 294 | 'chunk' => null, |
295 | 295 | 'url' => null, |
296 | | - 'token' => null, |
| 296 | + 'token' => null, |
297 | 297 | 'enablechunks' => null, |
298 | 298 | 'comment' => array( |
299 | | - ApiBase :: PARAM_DFLT => '' |
| 299 | + ApiBase::PARAM_DFLT => '' |
300 | 300 | ), |
301 | | - 'asyncdownload'=>false, |
| 301 | + 'asyncdownload' => false, |
302 | 302 | 'watch' => false, |
303 | 303 | 'ignorewarnings' => false, |
304 | | - 'done' => false, |
| 304 | + 'done' => false, |
305 | 305 | 'sessionkey' => null, |
306 | 306 | 'httpstatus' => null, |
307 | | - 'chunksessionkey'=> null, |
308 | | - 'internalhttpsession'=> null, |
| 307 | + 'chunksessionkey' => null, |
| 308 | + 'internalhttpsession' => null, |
309 | 309 | ); |
310 | 310 | } |
311 | 311 | |
312 | 312 | public function getParamDescription() { |
313 | | - return array ( |
| 313 | + return array( |
314 | 314 | 'filename' => 'Target filename', |
315 | 315 | 'file' => 'File contents', |
316 | 316 | 'chunk'=> 'Chunk File Contents', |
317 | 317 | 'url' => 'Url to upload from', |
318 | 318 | 'comment' => 'Upload comment or initial page text', |
319 | | - 'token' => 'Edit token. You can get one of these through prop=info (this helps avoid remote ajax upload requests with your credentials)', |
| 319 | + 'token' => 'Edit token. You can get one of these through prop=info (this helps avoid remote ajax upload requests with your credentials)', |
320 | 320 | 'enablechunks' => 'Boolean If we are in chunk mode; accepts many small file POSTs', |
321 | 321 | 'asyncdownload' => 'If we should download the url asyncrously usefull for large http downloads (returns a upload session key to get status updates in subquent calls)', |
322 | 322 | 'watch' => 'Watch the page', |
— | — | @@ -330,12 +330,12 @@ |
331 | 331 | |
332 | 332 | public function getDescription() { |
333 | 333 | return array( |
334 | | - 'Upload an File' |
| 334 | + 'Upload a file' |
335 | 335 | ); |
336 | 336 | } |
337 | 337 | |
338 | 338 | protected function getExamples() { |
339 | | - return array ( |
| 339 | + return array( |
340 | 340 | 'api.php?action=upload&filename=Wiki.png&url=http%3A//upload.wikimedia.org/wikipedia/en/b/bc/Wiki.png&ignorewarnings' |
341 | 341 | ); |
342 | 342 | } |
Index: trunk/phase3/mwScriptLoader.php |
— | — | @@ -1,38 +1,33 @@ |
2 | 2 | <?php |
3 | | -/* |
4 | | - * mvwScriptLoader.php |
5 | | -* Script Loading Library for MediaWiki |
6 | | -* |
7 | | -* @author Michael Dale mdale@wikimedia.org |
8 | | -* @date feb, 2009 |
9 | | -* |
10 | | -* This program is free software; you can redistribute it and/or modify |
11 | | -* it under the terms of the GNU General Public License as published by |
12 | | -* the Free Software Foundation; either version 2 of the License, or |
13 | | -* (at your option) any later version. |
14 | | -* |
15 | | -* This program is distributed in the hope that it will be useful, |
16 | | -* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | | -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | | -* GNU General Public License for more details. |
19 | | -* |
20 | | -* You should have received a copy of the GNU General Public License along |
21 | | -* with this program; if not, write to the Free Software Foundation, Inc., |
22 | | -* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
23 | | -* http://www.gnu.org/copyleft/gpl.html |
24 | | -*/ |
25 | | - |
26 | | -/* |
27 | | - * mvwScriptLoader: |
| 3 | +/** |
| 4 | + * mvwScriptLoader.php |
| 5 | + * Script Loading Library for MediaWiki |
28 | 6 | * |
29 | | - * some documentation about script-loader: |
30 | | - * http://www.mediawiki.org/wiki/ScriptLoader |
| 7 | + * @file |
| 8 | + * @author Michael Dale mdale@wikimedia.org |
| 9 | + * @date feb, 2009 |
| 10 | + * @link http://www.mediawiki.org/wiki/ScriptLoader Documentation |
| 11 | + * |
| 12 | + * This program is free software; you can redistribute it and/or modify |
| 13 | + * it under the terms of the GNU General Public License as published by |
| 14 | + * the Free Software Foundation; either version 2 of the License, or |
| 15 | + * (at your option) any later version. |
| 16 | + * |
| 17 | + * This program is distributed in the hope that it will be useful, |
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | + * GNU General Public License for more details. |
| 21 | + * |
| 22 | + * You should have received a copy of the GNU General Public License along |
| 23 | + * with this program; if not, write to the Free Software Foundation, Inc., |
| 24 | + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | + * http://www.gnu.org/copyleft/gpl.html |
31 | 26 | */ |
32 | 27 | |
33 | | -//include WebStart.php |
| 28 | +// include WebStart.php |
34 | 29 | require_once('includes/WebStart.php'); |
35 | 30 | |
36 | | -wfProfileIn('mvwScriptLoader.php'); |
| 31 | +wfProfileIn( 'mvwScriptLoader.php' ); |
37 | 32 | |
38 | 33 | if( isset( $_SERVER['SCRIPT_URL'] ) ) { |
39 | 34 | $url = $_SERVER['SCRIPT_URL']; |
— | — | @@ -40,27 +35,26 @@ |
41 | 36 | $url = $_SERVER['PHP_SELF']; |
42 | 37 | } |
43 | 38 | |
44 | | -if( strpos($url, "mwScriptLoader$wgScriptExtension") === false){ |
| 39 | +if( strpos( $url, "mwScriptLoader$wgScriptExtension" ) === false ){ |
45 | 40 | wfHttpError( 403, 'Forbidden', |
46 | 41 | 'mvwScriptLoader must be accessed through the primary script entry point.' ); |
47 | | - return ; |
| 42 | + return; |
48 | 43 | } |
49 | | -//Verify the script loader is on: |
50 | | -if (!$wgEnableScriptLoader) { |
| 44 | +// Verify the script loader is on: |
| 45 | +if ( !$wgEnableScriptLoader ) { |
51 | 46 | echo '/*ScriptLoader is not enabled for this site. To enable add the following line to your LocalSettings.php'; |
52 | 47 | echo '<pre><b>$wgEnableScriptLoader=true;</b></pre>*/'; |
53 | 48 | echo 'alert(\'Script loader is disabled\');'; |
54 | | - die(1); |
| 49 | + die( 1 ); |
55 | 50 | } |
56 | 51 | |
57 | | -//load the mwEmbed language file: |
| 52 | +// load the mwEmbed language file: |
58 | 53 | $wgExtensionMessagesFiles['mwEmbed'] = "{$IP}/js2/mwEmbed/php/languages/mwEmbed.i18n.php"; |
59 | | -//enable the msgs before we go on: |
| 54 | +// enable the msgs before we go on: |
60 | 55 | wfLoadExtensionMessages( 'mwEmbed' ); |
61 | 56 | |
62 | | -//run jsScriptLoader action: |
| 57 | +// run jsScriptLoader action: |
63 | 58 | $myScriptLoader = new jsScriptLoader(); |
64 | 59 | $myScriptLoader->doScriptLoader(); |
65 | 60 | |
66 | | -wfProfileOut(); |
67 | | -?> |
\ No newline at end of file |
| 61 | +wfProfileOut( 'mvwScriptLoader.php' ); |
\ No newline at end of file |