Index: branches/wmf/1.18wmf1/extensions/VipsScaler/VipsTest.php |
— | — | @@ -1,7 +1,7 @@ |
2 | | -<?php |
| 2 | + <?php |
3 | 3 | |
4 | 4 | /** |
5 | | - * Extension registration file for Special:VipsTest. The VipsScaler extension |
| 5 | + * Extension registration file for Special:VipsTest. The VipsScaler extension |
6 | 6 | * must be enabled. |
7 | 7 | */ |
8 | 8 | |
— | — | @@ -18,6 +18,8 @@ |
19 | 19 | */ |
20 | 20 | $wgVipsTestExpiry = 3600; |
21 | 21 | |
| 22 | + $dir = dirname( __FILE__ ); |
| 23 | + |
22 | 24 | /** Registration */ |
23 | 25 | $wgAutoloadClasses['SpecialVipsTest'] = "$dir/SpecialVipsTest.php"; |
24 | 26 | $wgExtensionAliasesFiles['VipsTest'] = "$dir/VipsScaler.alias.php"; |
— | — | @@ -25,7 +27,7 @@ |
26 | 28 | $wgGroupPermissions['*']['vipsscaler-test'] = true; |
27 | 29 | $wgSpecialPages['VipsTest'] = 'SpecialVipsTest'; |
28 | 30 | |
29 | | -/** |
| 31 | +/** |
30 | 32 | * Disable VipsScaler for ordinary image scaling so that the test has something |
31 | 33 | * to compare against. |
32 | 34 | */ |
Index: branches/wmf/1.18wmf1/extensions/VipsScaler/SpecialVipsTest.php |
— | — | @@ -79,8 +79,7 @@ |
80 | 80 | if ( $request->getCheck( 'bilinear' ) ) { |
81 | 81 | $vipsUrlOptions['bilinear'] = 1; |
82 | 82 | } |
83 | | - |
84 | | - |
| 83 | + |
85 | 84 | # Generate normal thumbnail |
86 | 85 | $params = array( 'width' => $width ); |
87 | 86 | $thumb = $file->transform( $params ); |
— | — | @@ -97,7 +96,7 @@ |
98 | 97 | |
99 | 98 | # Make url to the vips thumbnail |
100 | 99 | $vipsThumbUrl = $this->getTitle()->getLocalUrl( $vipsUrlOptions ); |
101 | | - |
| 100 | + |
102 | 101 | # HTML for the thumbnails |
103 | 102 | $thumbs = Html::rawElement( 'div', array( 'id' => 'mw-vipstest-thumbnails' ), |
104 | 103 | Html::element( 'img', array( |
— | — | @@ -107,14 +106,14 @@ |
108 | 107 | Html::element( 'img', array( |
109 | 108 | 'src' => $vipsThumbUrl, |
110 | 109 | 'alt' => wfMessage( 'vipsscaler-vips-thumb' ), |
111 | | - ) ) |
| 110 | + ) ) |
112 | 111 | ); |
113 | 112 | |
114 | 113 | # Helper messages shown above the thumbnails rendering |
115 | 114 | $help = wfMessage( 'vipsscaler-thumbs-help' )->parseAsBlock(); |
116 | 115 | |
117 | 116 | # A checkbox to easily alternate between both views: |
118 | | - $checkbox = Xml::checkLabel( |
| 117 | + $checkbox = Xml::checkLabel( |
119 | 118 | wfMessage( 'vipsscaler-thumbs-switch-label' ), |
120 | 119 | 'mw-vipstest-thumbs-switch', |
121 | 120 | 'mw-vipstest-thumbs-switch' |
— | — | @@ -148,7 +147,7 @@ |
149 | 148 | // was correct. So we have to show it again. |
150 | 149 | // See HTMLForm::show() |
151 | 150 | $result = $form->show(); |
152 | | - if( $result === true or $result instanceof Status && $result->isGood() ) { |
| 151 | + if( $result === true || $result instanceof Status && $result->isGood() ) { |
153 | 152 | $form->displayForm( $result ); |
154 | 153 | $this->showThumbnails(); |
155 | 154 | } |
— | — | @@ -188,7 +187,7 @@ |
189 | 188 | 'Bilinear' => array( |
190 | 189 | 'name' => 'bilinear', |
191 | 190 | 'class' => 'HTMLCheckField', |
192 | | - 'label-message' => 'vipsscaler-form-bilinear', |
| 191 | + 'label-message' => 'vipsscaler-form-bilinear', |
193 | 192 | ), |
194 | 193 | ); |
195 | 194 | |
— | — | @@ -202,13 +201,18 @@ |
203 | 202 | return $fields; |
204 | 203 | } |
205 | 204 | |
| 205 | + /** |
| 206 | + * @param $input |
| 207 | + * @param $alldata |
| 208 | + * @return bool|String |
| 209 | + */ |
206 | 210 | public static function validateFileInput( $input, $alldata ) { |
207 | 211 | if ( !trim( $input ) ) { |
208 | | - # Don't show an error if the file is not yet specified, |
| 212 | + # Don't show an error if the file is not yet specified, |
209 | 213 | # because it is annoying |
210 | 214 | return true; |
211 | 215 | } |
212 | | - |
| 216 | + |
213 | 217 | $title = Title::makeTitleSafe( NS_FILE, $input ); |
214 | 218 | if( is_null( $title ) ) { |
215 | 219 | return wfMsg( 'vipsscaler-invalid-file' ); |
— | — | @@ -221,8 +225,14 @@ |
222 | 226 | // Looks sane enough. |
223 | 227 | return true; |
224 | 228 | } |
| 229 | + |
| 230 | + /** |
| 231 | + * @param $input |
| 232 | + * @param $allData |
| 233 | + * @return bool|String |
| 234 | + */ |
225 | 235 | public static function validateWidth( $input, $allData ) { |
226 | | - if ( self::validateFileInput( $allData['File'], $allData ) !== true |
| 236 | + if ( self::validateFileInput( $allData['File'], $allData ) !== true |
227 | 237 | || !trim( $allData['File'] ) ) { |
228 | 238 | # Invalid file, error will already be shown at file field |
229 | 239 | return true; |
— | — | @@ -234,16 +244,23 @@ |
235 | 245 | } |
236 | 246 | return true; |
237 | 247 | } |
| 248 | + |
| 249 | + /** |
| 250 | + * @param $input |
| 251 | + * @param $allData |
| 252 | + * @return bool|String |
| 253 | + */ |
238 | 254 | public static function validateSharpen( $input, $allData ) { |
239 | 255 | if ( $input >= 5.0 || $input < 0.0 ) { |
240 | 256 | return wfMsg( 'vipsscaler-invalid-sharpen' ); |
241 | 257 | } |
242 | 258 | return true; |
243 | | - |
244 | 259 | } |
245 | 260 | |
246 | 261 | /** |
247 | 262 | * Process data submitted by the form. |
| 263 | + * @param $data array |
| 264 | + * @return Status |
248 | 265 | */ |
249 | 266 | public static function processForm( array $data ) { |
250 | 267 | return Status::newGood(); |
— | — | @@ -278,7 +295,6 @@ |
279 | 296 | if ( !$handler->normaliseParams( $file, $params ) ) { |
280 | 297 | return $this->streamError( 500, "VipsScaler: invalid parameters\n" ); |
281 | 298 | } |
282 | | - |
283 | 299 | |
284 | 300 | # Get the thumbnail |
285 | 301 | if ( is_null( $wgVipsThumbnailerHost ) || $request->getBool( 'noproxy' ) ) { |
— | — | @@ -308,7 +324,7 @@ |
309 | 325 | 'dstPath' => $dstPath, |
310 | 326 | 'dstUrl' => $dstUrl, |
311 | 327 | ); |
312 | | - |
| 328 | + |
313 | 329 | $options = array(); |
314 | 330 | if ( $request->getBool( 'bilinear' ) ) { |
315 | 331 | $options['bilinear'] = true; |
— | — | @@ -347,9 +363,22 @@ |
348 | 364 | $url = wfExpandUrl( $request->getRequestURL(), PROTO_INTERNAL ); |
349 | 365 | $url = wfAppendQuery( $url, array( 'noproxy' => '1' ) ); |
350 | 366 | wfDebug( __METHOD__ . ": Getting vips thumb from remote url $url\n" ); |
351 | | - |
352 | | - $options = array( 'method' => 'GET' ); |
353 | 367 | |
| 368 | + $bits = IP::splitHostAndPort( $wgVipsThumbnailerHost ); |
| 369 | + if ( !$bits ) { |
| 370 | + throw new MWException( __METHOD__.': $wgVipsThumbnailerHost is not set to a valid host' ); |
| 371 | + } |
| 372 | + list( $host, $port ) = $bits; |
| 373 | + if ( $port === false ) { |
| 374 | + $port = 80; |
| 375 | + } |
| 376 | + $proxy = IP::combineHostAndPort( $host, $port ); |
| 377 | + |
| 378 | + $options = array( |
| 379 | + 'method' => 'GET', |
| 380 | + 'proxy' => $proxy, |
| 381 | + ); |
| 382 | + |
354 | 383 | $req = MWHttpRequest::factory( $url, $options ); |
355 | 384 | $status = $req->execute(); |
356 | 385 | if ( $status->isOk() ) { |
— | — | @@ -361,18 +390,20 @@ |
362 | 391 | header( "Cache-Control: public, max-age=$wgVipsTestExpiry, s-maxage=$wgVipsTestExpiry" ); |
363 | 392 | header( 'Expires: ' . gmdate( 'r ', time() + $wgVipsTestExpiry ) ); |
364 | 393 | print $req->getContent(); |
| 394 | + } elseif ( $status->hasMessage( 'http-bad-status' ) ) { |
| 395 | + return $this->streamError( 500, $req->getContent() ); |
365 | 396 | } else { |
366 | | - return $this->streamError( 500, $req->getContent() ); |
| 397 | + global $wgOut; |
| 398 | + return $this->streamError( 500, $wgOut->parse( $status->getWikiText() ) ); |
367 | 399 | } |
368 | | - |
369 | 400 | } |
370 | 401 | } |
371 | 402 | |
372 | | - |
373 | 403 | /** |
374 | 404 | * Generates a blank page with given HTTP error code |
375 | 405 | * |
376 | | - * @param $code Integer: HTTP error either 404 or 500 |
| 406 | + * @param $code Integer HTTP error either 404 or 500 |
| 407 | + * @param $error string |
377 | 408 | */ |
378 | 409 | protected function streamError( $code, $error = '' ) { |
379 | 410 | $this->getOutput()->setStatusCode( $code ); |
Index: branches/wmf/1.18wmf1/extensions/VipsScaler/VipsScaler_body.php |
— | — | @@ -38,6 +38,7 @@ |
39 | 39 | * @param File $file |
40 | 40 | * @param array $params |
41 | 41 | * @param MediaTransformOutput $mto |
| 42 | + * @return bool |
42 | 43 | */ |
43 | 44 | public static function onTransform( $handler, $file, &$params, &$mto ) { |
44 | 45 | # Check $wgVipsConditions |
— | — | @@ -46,15 +47,14 @@ |
47 | 48 | wfDebug( "...\n" ); |
48 | 49 | return true; |
49 | 50 | } |
50 | | - |
51 | 51 | return self::doTransform( $handler, $file, $params, $options, $mto ); |
52 | 52 | } |
53 | | - |
| 53 | + |
54 | 54 | /** |
55 | 55 | * Performs a transform with VIPS |
56 | | - * |
| 56 | + * |
57 | 57 | * @see VipsScaler::onTransform |
58 | | - * |
| 58 | + * |
59 | 59 | * @param BitmapHandler $handler |
60 | 60 | * @param File $file |
61 | 61 | * @param array $params |
— | — | @@ -108,10 +108,11 @@ |
109 | 109 | } |
110 | 110 | |
111 | 111 | /** |
112 | | - * @param $handler |
113 | | - * @param $file |
114 | | - * @param $params |
115 | | - * @param $options |
| 112 | + * @param $handler BitmapHandler |
| 113 | + * @param $file File |
| 114 | + * @param $params array |
| 115 | + * @param $options array |
| 116 | + * @return array |
116 | 117 | */ |
117 | 118 | public static function makeCommands( $handler, $file, $params, $options ) { |
118 | 119 | global $wgVipsCommand; |
— | — | @@ -136,10 +137,10 @@ |
137 | 138 | # Calculate shrink factors. Offsetting by a small amount is required |
138 | 139 | # because of rounding down of the target size by VIPS. See 25990#c7 |
139 | 140 | # |
140 | | - # No need to invert source and physical dimensions. They already got |
| 141 | + # No need to invert source and physical dimensions. They already got |
141 | 142 | # switched if needed. |
142 | 143 | # |
143 | | - # Use sprintf() instead of plain string conversion so that we can |
| 144 | + # Use sprintf() instead of plain string conversion so that we can |
144 | 145 | # control the precision |
145 | 146 | $rx = sprintf( "%.18e", $params['srcWidth'] / ($params['physicalWidth'] + 0.125) ); |
146 | 147 | $ry = sprintf( "%.18e", $params['srcHeight'] / ($params['physicalHeight'] + 0.125) ); |
— | — | @@ -178,7 +179,7 @@ |
179 | 180 | } |
180 | 181 | |
181 | 182 | if ( !empty( $options['convolution'] ) ) { |
182 | | - $commands[] = new VipsConvolution( $wgVipsCommand, |
| 183 | + $commands[] = new VipsConvolution( $wgVipsCommand, |
183 | 184 | array( 'im_convf', $options['convolution'] ) ); |
184 | 185 | } |
185 | 186 | |
— | — | @@ -352,6 +353,7 @@ |
353 | 354 | $this->vips = $vips; |
354 | 355 | $this->args = $args; |
355 | 356 | } |
| 357 | + |
356 | 358 | /** |
357 | 359 | * Set the input and output file of this command |
358 | 360 | * |
— | — | @@ -374,6 +376,7 @@ |
375 | 377 | $this->output = $output; |
376 | 378 | } |
377 | 379 | } |
| 380 | + |
378 | 381 | /** |
379 | 382 | * Returns the output filename |
380 | 383 | * @return string |
— | — | @@ -381,6 +384,7 @@ |
382 | 385 | public function getOutput() { |
383 | 386 | return $this->output; |
384 | 387 | } |
| 388 | + |
385 | 389 | /** |
386 | 390 | * Return the output of the command |
387 | 391 | * @return string |
— | — | @@ -396,9 +400,13 @@ |
397 | 401 | */ |
398 | 402 | public function execute() { |
399 | 403 | # Build and escape the command string |
400 | | - $cmd = wfEscapeShellArg( $this->vips, |
401 | | - array_shift( $this->args ), |
402 | | - $this->input, $this->output ); |
| 404 | + $cmd = |
| 405 | + 'IM_CONCURRENCY=1 ' . |
| 406 | + wfEscapeShellArg( |
| 407 | + $this->vips, |
| 408 | + array_shift( $this->args ), |
| 409 | + $this->input, $this->output |
| 410 | + ); |
403 | 411 | |
404 | 412 | foreach ( $this->args as $arg ) { |
405 | 413 | $cmd .= ' ' . wfEscapeShellArg( $arg ); |
— | — | @@ -445,26 +453,30 @@ |
446 | 454 | * matrix file as its last argument |
447 | 455 | */ |
448 | 456 | class VipsConvolution extends VipsCommand { |
| 457 | + |
| 458 | + /** |
| 459 | + * @return int |
| 460 | + */ |
449 | 461 | public function execute() { |
450 | 462 | # Convert a 2D array into a space/newline separated matrix |
451 | 463 | $convolutionMatrix = array_pop( $this->args ); |
452 | 464 | $convolutionString = ''; |
453 | | - foreach ( $convolutionMatrix as $i=>$row ) { |
| 465 | + foreach ( $convolutionMatrix as $row ) { |
454 | 466 | $convolutionString .= implode( ' ', $row ) . "\n"; |
455 | 467 | } |
456 | 468 | # Save the matrix in a tempfile |
457 | 469 | $convolutionFile = self::makeTemp( 'conv' ); |
458 | 470 | file_put_contents( $convolutionFile, $convolutionString ); |
459 | 471 | array_push( $this->args, $convolutionFile ); |
460 | | - |
| 472 | + |
461 | 473 | wfDebug( __METHOD__ . ": Convolving image [\n" . $convolutionString . "] \n" ); |
462 | | - |
| 474 | + |
463 | 475 | # Call the parent to actually execute the command |
464 | 476 | $retval = parent::execute(); |
465 | | - |
| 477 | + |
466 | 478 | # Remove the temporary matrix file |
467 | 479 | unlink( $convolutionFile ); |
468 | | - |
| 480 | + |
469 | 481 | return $retval; |
470 | 482 | } |
471 | 483 | } |
Property changes on: branches/wmf/1.18wmf1/extensions/VipsScaler |
___________________________________________________________________ |
Modified: svn:mergeinfo |
472 | 484 | Merged /trunk/extensions/VipsScaler:r104604-104623 |