Split off some of these validation methods into their own class called Validate so...
authorChad Horohoe <demon@users.mediawiki.org>
Fri, 3 Oct 2008 13:32:55 +0000 (13:32 +0000)
committerChad Horohoe <demon@users.mediawiki.org>
Fri, 3 Oct 2008 13:32:55 +0000 (13:32 +0000)
includes/AutoLoader.php
includes/Validate.php [new file with mode: 0644]
includes/specials/SpecialPreferences.php

index c1f9a2e..f9897e2 100644 (file)
@@ -199,6 +199,7 @@ $wgAutoloadLocalClasses = array(
        'UserArrayFromResult' => 'includes/UserArray.php',
        'UserMailer' => 'includes/UserMailer.php',
        'UserRightsProxy' => 'includes/UserRightsProxy.php',
+       'Validate' => 'includes/Validate.php',
        'WatchedItem' => 'includes/WatchedItem.php',
        'WatchlistEditor' => 'includes/WatchlistEditor.php',
        'WebRequest' => 'includes/WebRequest.php',
diff --git a/includes/Validate.php b/includes/Validate.php
new file mode 100644 (file)
index 0000000..9dc3f37
--- /dev/null
@@ -0,0 +1,105 @@
+<?php
+/**
+ * Validate - A bunch of static methods used to validate user input
+ * Split from Special:Preferences in 1.14 for more general use
+ */
+class Validate {
+       
+       /**
+        * Given an inputed integer, determine if it's within a given 
+        * range. If not, return the upper or lower boundary, whichever
+        * was crossed.
+        * @param int $val A user inputted integer
+        * @param int $min The lower limit
+        * @param int $max The upper limit
+        * @return int
+        */
+       function int( &$val, $min=0, $max=0x7fffffff ) {
+               $val = intval($val);
+               $val = min($val, $max);
+               $val = max($val, $min);
+               return $val;
+       }
+
+       /**
+        * Given an inputed float, determine if it's within a given
+        * range. If not, return the upper or lower boundary, whichever
+        * was crossed.
+        * @param float $val User inputed value
+        * @param float $min Lower limit
+        * @param float $max Uppser limit
+        * @return float
+        */
+       public static function float( &$val, $min, $max=0x7fffffff ) {
+               $val = floatval( $val );
+               $val = min( $val, $max );
+               $val = max( $val, $min );
+               return( $val );
+       }
+
+       /**
+        * Like int(), only return null if it's not a string
+        * @see Validate::int()
+        * @param int $val User given integer
+        * @param int $min Lower limit
+        * @param int $max Upper limit
+        * @return mixed [int or null]
+        */
+       public static function intOrNull( &$val, $min=0, $max=0x7fffffff ) {
+               $val = trim($val);
+               if($val === '') {
+                       return null;
+               } else {
+                       return self :: int( $val, $min, $max );
+               }
+       }
+
+       /**
+        * Given a date string, validate to see if it's an acceptable type. If
+        * not, return an acceptable one.
+        * @param string $val User inputed date
+        * @return string
+        */
+       public static function dateFormat( $val ) {
+               global $wgLang, $wgContLang;
+               if ( $val !== false && (
+                       in_array( $val, (array)$wgLang->getDatePreferences() ) ||
+                       in_array( $val, (array)$wgContLang->getDatePreferences() ) ) )
+               {
+                       return $val;
+               } else {
+                       return $wgLang->getDefaultDateFormat();
+               }
+       }
+
+       /**
+        * Used to validate the user inputed timezone before saving it as
+        * 'timecorrection', will return '00:00' if fed bogus data.
+        * Note: It's not a 100% correct implementation timezone-wise, it will
+        * accept stuff like '14:30',
+        * @param string $s the user input
+        * @return string
+        */
+       public static function timeZone( $s ) {
+               if ( $s !== '' ) {
+                       if ( strpos( $s, ':' ) ) {
+                               # HH:MM
+                               $array = explode( ':' , $s );
+                               $hour = intval( $array[0] );
+                               $minute = intval( $array[1] );
+                       } else {
+                               $minute = intval( $s * 60 );
+                               $hour = intval( $minute / 60 );
+                               $minute = abs( $minute ) % 60;
+                       }
+                       # Max is +14:00 and min is -12:00, see:
+                       # http://en.wikipedia.org/wiki/Timezone
+                       $hour = min( $hour, 14 );
+                       $hour = max( $hour, -12 );
+                       $minute = min( $minute, 59 );
+                       $minute = max( $minute, 0 );
+                       $s = sprintf( "%02d:%02d", $hour, $minute );
+               }
+               return $s;
+       }
+}
index fa5f793..ca1e3d3 100644 (file)
@@ -124,84 +124,6 @@ class PreferencesForm {
                        $this->mainPrefsForm( '' );
                }
        }
-       /**
-        * @access private
-        */
-       function validateInt( &$val, $min=0, $max=0x7fffffff ) {
-               $val = intval($val);
-               $val = min($val, $max);
-               $val = max($val, $min);
-               return $val;
-       }
-
-       /**
-        * @access private
-        */
-       function validateFloat( &$val, $min, $max=0x7fffffff ) {
-               $val = floatval( $val );
-               $val = min( $val, $max );
-               $val = max( $val, $min );
-               return( $val );
-       }
-
-       /**
-        * @access private
-        */
-       function validateIntOrNull( &$val, $min=0, $max=0x7fffffff ) {
-               $val = trim($val);
-               if($val === '') {
-                       return null;
-               } else {
-                       return $this->validateInt( $val, $min, $max );
-               }
-       }
-
-       /**
-        * @access private
-        */
-       function validateDate( $val ) {
-               global $wgLang, $wgContLang;
-               if ( $val !== false && (
-                       in_array( $val, (array)$wgLang->getDatePreferences() ) ||
-                       in_array( $val, (array)$wgContLang->getDatePreferences() ) ) )
-               {
-                       return $val;
-               } else {
-                       return $wgLang->getDefaultDateFormat();
-               }
-       }
-
-       /**
-        * Used to validate the user inputed timezone before saving it as
-        * 'timecorrection', will return '00:00' if fed bogus data.
-        * Note: It's not a 100% correct implementation timezone-wise, it will
-        * accept stuff like '14:30',
-        * @access private
-        * @param string $s the user input
-        * @return string
-        */
-       function validateTimeZone( $s ) {
-               if ( $s !== '' ) {
-                       if ( strpos( $s, ':' ) ) {
-                               # HH:MM
-                               $array = explode( ':' , $s );
-                               $hour = intval( $array[0] );
-                               $minute = intval( $array[1] );
-                       } else {
-                               $minute = intval( $s * 60 );
-                               $hour = intval( $minute / 60 );
-                               $minute = abs( $minute ) % 60;
-                       }
-                       # Max is +14:00 and min is -12:00, see:
-                       # http://en.wikipedia.org/wiki/Timezone
-                       $hour = min( $hour, 14 );
-                       $hour = max( $hour, -12 );
-                       $minute = min( $minute, 59 );
-                       $minute = max( $minute, 0 );
-                       $s = sprintf( "%02d:%02d", $hour, $minute );
-               }
-               return $s;
-       }
 
        /**
         * @access private
@@ -273,21 +195,21 @@ class PreferencesForm {
                if( $wgUseTeX ) {
                        $wgUser->setOption( 'math', $this->mMath );
                }
-               $wgUser->setOption( 'date', $this->validateDate( $this->mDate ) );
-               $wgUser->setOption( 'searchlimit', $this->validateIntOrNull( $this->mSearch ) );
-               $wgUser->setOption( 'contextlines', $this->validateIntOrNull( $this->mSearchLines ) );
-               $wgUser->setOption( 'contextchars', $this->validateIntOrNull( $this->mSearchChars ) );
-               $wgUser->setOption( 'rclimit', $this->validateIntOrNull( $this->mRecent ) );
-               $wgUser->setOption( 'rcdays', $this->validateInt($this->mRecentDays, 1, ceil($wgRCMaxAge / (3600*24))));
-               $wgUser->setOption( 'wllimit', $this->validateIntOrNull( $this->mWatchlistEdits, 0, 1000 ) );
-               $wgUser->setOption( 'rows', $this->validateInt( $this->mRows, 4, 1000 ) );
-               $wgUser->setOption( 'cols', $this->validateInt( $this->mCols, 4, 1000 ) );
-               $wgUser->setOption( 'stubthreshold', $this->validateIntOrNull( $this->mStubs ) );
-               $wgUser->setOption( 'timecorrection', $this->validateTimeZone( $this->mHourDiff, -12, 14 ) );
+               $wgUser->setOption( 'date', Validate::dateFormat( $this->mDate ) );
+               $wgUser->setOption( 'searchlimit', Validate::intOrNull( $this->mSearch ) );
+               $wgUser->setOption( 'contextlines', Validate::intOrNull( $this->mSearchLines ) );
+               $wgUser->setOption( 'contextchars', Validate::intOrNull( $this->mSearchChars ) );
+               $wgUser->setOption( 'rclimit', Validate::intOrNull( $this->mRecent ) );
+               $wgUser->setOption( 'rcdays', Validate::int($this->mRecentDays, 1, ceil($wgRCMaxAge / (3600*24))));
+               $wgUser->setOption( 'wllimit', Validate::intOrNull( $this->mWatchlistEdits, 0, 1000 ) );
+               $wgUser->setOption( 'rows', Validate::int( $this->mRows, 4, 1000 ) );
+               $wgUser->setOption( 'cols', Validate::int( $this->mCols, 4, 1000 ) );
+               $wgUser->setOption( 'stubthreshold', Validate::intOrNull( $this->mStubs ) );
+               $wgUser->setOption( 'timecorrection', Validate::timeZone( $this->mHourDiff, -12, 14 ) );
                $wgUser->setOption( 'imagesize', $this->mImageSize );
                $wgUser->setOption( 'thumbsize', $this->mThumbSize );
-               $wgUser->setOption( 'underline', $this->validateInt($this->mUnderline, 0, 2) );
-               $wgUser->setOption( 'watchlistdays', $this->validateFloat( $this->mWatchlistDays, 0, 7 ) );
+               $wgUser->setOption( 'underline', Validate::int($this->mUnderline, 0, 2) );
+               $wgUser->setOption( 'watchlistdays', Validate::float( $this->mWatchlistDays, 0, 7 ) );
                $wgUser->setOption( 'disablesuggest', $this->mDisableMWSuggest );
 
                # Set search namespace options