(bug 10323) Special:Undelete should have "inverse selection" button
[mediawiki.git] / includes / Validate.php
blob7c598cfbdaba17c3f4afabd7243f0601607b4618
1 <?php
2 /**
3 * Validate - A bunch of static methods used to validate user input
4 * Split from Special:Preferences in 1.14 for more general use
5 */
6 class Validate {
8 /**
9 * Given an inputed integer, determine if it's within a given
10 * range. If not, return the upper or lower boundary, whichever
11 * was crossed.
12 * @param int $val A user inputted integer
13 * @param int $min The lower limit
14 * @param int $max The upper limit
15 * @return int
17 public static function int( &$val, $min=0, $max=0x7fffffff ) {
18 $val = intval($val);
19 $val = min($val, $max);
20 $val = max($val, $min);
21 return $val;
24 /**
25 * Given an inputed float, determine if it's within a given
26 * range. If not, return the upper or lower boundary, whichever
27 * was crossed.
28 * @param float $val User inputed value
29 * @param float $min Lower limit
30 * @param float $max Uppser limit
31 * @return float
33 public static function float( &$val, $min, $max=0x7fffffff ) {
34 $val = floatval( $val );
35 $val = min( $val, $max );
36 $val = max( $val, $min );
37 return( $val );
40 /**
41 * Like int(), only return null if it's not a string
42 * @see Validate::int()
43 * @param int $val User given integer
44 * @param int $min Lower limit
45 * @param int $max Upper limit
46 * @return mixed [int or null]
48 public static function intOrNull( &$val, $min=0, $max=0x7fffffff ) {
49 $val = trim($val);
50 if($val === '') {
51 return null;
52 } else {
53 return self :: int( $val, $min, $max );
57 /**
58 * Given a date string, validate to see if it's an acceptable type. If
59 * not, return an acceptable one.
60 * @param string $val User inputed date
61 * @return string
63 public static function dateFormat( $val ) {
64 global $wgLang, $wgContLang;
65 if ( $val !== false && (
66 in_array( $val, (array)$wgLang->getDatePreferences() ) ||
67 in_array( $val, (array)$wgContLang->getDatePreferences() ) ) )
69 return $val;
70 } else {
71 return $wgLang->getDefaultDateFormat();
75 /**
76 * Used to validate the user inputed timezone before saving it as
77 * 'timecorrection', will return '00:00' if fed bogus data.
78 * Note: It's not a 100% correct implementation timezone-wise, it will
79 * accept stuff like '14:30',
80 * @param string $s the user input
81 * @return string
83 public static function timeZone( $s ) {
84 if ( $s !== '' ) {
85 if ( strpos( $s, ':' ) ) {
86 # HH:MM
87 $array = explode( ':' , $s );
88 $hour = intval( $array[0] );
89 $minute = intval( $array[1] );
90 } else {
91 $minute = intval( $s * 60 );
92 $hour = intval( $minute / 60 );
93 $minute = abs( $minute ) % 60;
95 # Max is +14:00 and min is -12:00, see:
96 # http://en.wikipedia.org/wiki/Timezone
97 $hour = min( $hour, 14 );
98 $hour = max( $hour, -12 );
99 $minute = min( $minute, 59 );
100 $minute = max( $minute, 0 );
101 $s = sprintf( "%02d:%02d", $hour, $minute );
103 return $s;