r102867 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r102866‎ | r102867 | r102868 >
Date:18:39, 12 November 2011
Author:btongminh
Status:resolved (Comments)
Tags:
Comment:
Add more validation of input, fix sharpening validation
Modified paths:
  • /trunk/extensions/VipsScaler/SpecialVipsTest.php (modified) (history)
  • /trunk/extensions/VipsScaler/VipsScaler.i18n.php (modified) (history)

Diff [purge]

Index: trunk/extensions/VipsScaler/SpecialVipsTest.php
@@ -76,7 +76,7 @@
7777 return;
7878 }
7979 $vipsUrlOptions = array( 'thumb' => $file->getName(), 'width' => $width );
80 - if ( $request->getBool( 'sharpen' ) ) {
 80+ if ( $request->getInt( 'sharpen' ) ) {
8181 $vipsUrlOptions['sharpen'] = $request->getVal( 'sharpen' );
8282 }
8383 if ( $request->getCheck( 'bilinear' ) ) {
@@ -143,6 +143,14 @@
144144 */
145145 protected function getFormFields() {
146146 $fields = array(
 147+ 'File' => array(
 148+ 'name' => 'file',
 149+ 'class' => 'HTMLTextField',
 150+ 'required' => true,
 151+ 'size' => '80',
 152+ 'label-message' => 'vipsscaler-form-file',
 153+ 'validation-callback' => array( __CLASS__, 'validateFileInput' ),
 154+ ),
147155 'Width' => array(
148156 'name' => 'width',
149157 'class' => 'HTMLIntField',
@@ -150,21 +158,15 @@
151159 'size' => '5',
152160 'required' => true,
153161 'label-message' => 'vipsscaler-form-width',
 162+ 'validation-callback' => array( __CLASS__, 'validateWidth' ),
154163 ),
155 - 'File' => array(
156 - 'name' => 'file',
157 - 'class' => 'HTMLTextField',
158 - 'required' => true,
159 - 'size' => '80',
160 - 'label-message' => 'vipsscaler-form-file',
161 - 'validation-callback' => array( __CLASS__, 'validateFileInput' ),
162 - ),
163164 'SharpenRadius' => array(
164165 'name' => 'sharpen',
165166 'class' => 'HTMLFloatField',
166167 'default' => '0.0',
167168 'size' => '5',
168169 'label-message' => 'vipsscaler-form-sharpen-radius',
 170+ 'validation-callback' => array( __CLASS__, 'validateSharpen' ),
169171 ),
170172 'Bilinear' => array(
171173 'name' => 'bilinear',
@@ -176,6 +178,12 @@
177179 }
178180
179181 public static function validateFileInput( $input, $alldata ) {
 182+ if ( !trim( $input ) ) {
 183+ # Don't show an error if the file is not yet specified,
 184+ # because it is annoying
 185+ return true;
 186+ }
 187+
180188 $title = Title::makeTitleSafe( NS_FILE, $input );
181189 if( is_null( $title ) ) {
182190 return wfMsg( 'vipsscaler-invalid-file' );
@@ -188,6 +196,25 @@
189197 // Looks sane enough.
190198 return true;
191199 }
 200+ public static function validateWidth( $input, $allData ) {
 201+ if ( self::validateFileInput( $allData['File'], $allData ) !== true ) {
 202+ # Invalid file, error will already be shown at file field
 203+ return true;
 204+ }
 205+ $title = Title::makeTitleSafe( NS_FILE, $allData['File'] );
 206+ $file = wfFindFile( $title );
 207+ if ( $input <= 0 || $input >= $file->getWidth() ) {
 208+ return wfMsg( 'vipsscaler-invalid-width's );
 209+ }
 210+ return true;
 211+ }
 212+ public static function validateSharpen( $input, $allData ) {
 213+ if ( $input >= 5.0 || $input < 0.0 ) {
 214+ return wfMsg( 'vipsscaler-invalid-sharpen' );
 215+ }
 216+ return true;
 217+
 218+ }
192219
193220 /**
194221 * Process data submitted by the form.
@@ -261,7 +288,7 @@
262289 $options['bilinear'] = true;
263290 wfDebug( __METHOD__ . ": using bilinear scaling\n" );
264291 }
265 - if ( $request->getBool( 'sharpen' ) && $request->getInt( 'sharpen' ) < 5 ) {
 292+ if ( $request->getInt( 'sharpen' ) && $request->getInt( 'sharpen' ) < 5 ) {
266293 # Limit sharpen sigma to 5, otherwise we have to write huge convolution matrices
267294 $options['sharpen'] = array( 'sigma' => floatval( $request->getVal( 'sharpen' ) ) );
268295 wfDebug( __METHOD__ . ": sharpening with radius {$options['sharpen']}\n" );
Index: trunk/extensions/VipsScaler/VipsScaler.i18n.php
@@ -13,7 +13,8 @@
1414
1515 'vipsscaler-desc' => 'Create thumbnails using VIPS',
1616 'vipsscaler-invalid-file' => 'Could not process requested file. Check that it exists on this wiki.',
17 - 'vipsscaler-invalid-width' => 'You must specify a width (integer > 0).',
 17+ 'vipsscaler-invalid-width' => 'Thumbnail width should be larger than zero and not larger than file width.',
 18+ 'vipsscaler-invalid-sharpen' => 'Sharpening amount should be a number larger than zero and smaller than five.',
1819 'vipsscaler-thumb-error' => 'VIPS could not generate a thumbnail with given parameters.',
1920
2021 # Vipscaler test form:

Follow-up revisions

RevisionCommit summaryAuthorDate
r102868Follow-up r102867, fix syntax error. My ctrl key is a bit broken, so ctrl+s d...btongminh18:41, 12 November 2011
r102869Follow-up r102867, don't try to generate thumbnails if validation failedbtongminh18:45, 12 November 2011
r102870Follow-up r102867, another validation fixbtongminh19:53, 12 November 2011

Comments

#Comment by Tim Starling (talk | contribs)   08:33, 18 November 2011
-			if ( $request->getBool( 'sharpen' ) && $request->getInt( 'sharpen' ) < 5 ) {
+			if ( $request->getInt( 'sharpen' ) && $request->getInt( 'sharpen' ) < 5 ) {

This is broken when sharpen is less than 1, e.g. 0.8 is converted to 0 and then to false. Fixed in r103572.

Status & tagging log