Index: trunk/phase3/includes/api/ApiBase.php |
— | — | @@ -52,6 +52,7 @@ |
53 | 53 | const PARAM_ALLOW_DUPLICATES = 6; // Boolean, do we allow the same value to be set more than once when ISMULTI=true |
54 | 54 | const PARAM_DEPRECATED = 7; // Boolean, is the parameter deprecated (will show a warning) |
55 | 55 | const PARAM_REQUIRED = 8; // Boolean, is the parameter required? |
| 56 | + const PARAM_RANGE_ENFORCE = 9; // Boolean, if MIN/MAX are set, enforce (die) these? Only applies if TYPE='integer' Use with extreme caution |
56 | 57 | |
57 | 58 | const LIMIT_BIG1 = 500; // Fast query, std user limit |
58 | 59 | const LIMIT_BIG2 = 5000; // Fast query, bot/sysop limit |
— | — | @@ -676,16 +677,21 @@ |
677 | 678 | |
678 | 679 | break; |
679 | 680 | case 'integer': // Force everything using intval() and optionally validate limits |
| 681 | + $value = is_array( $value ) ? array_map( 'intval', $value ) : intval( $value ); |
680 | 682 | |
681 | | - $value = is_array( $value ) ? array_map( 'intval', $value ) : intval( $value ); |
682 | 683 | $min = isset ( $paramSettings[self::PARAM_MIN] ) ? $paramSettings[self::PARAM_MIN] : null; |
683 | 684 | $max = isset ( $paramSettings[self::PARAM_MAX] ) ? $paramSettings[self::PARAM_MAX] : null; |
| 685 | + $enforceLimits = isset ( $paramSettings[self::PARAM_RANGE_ENFORCE] ) |
| 686 | + ? $paramSettings[self::PARAM_RANGE_ENFORCE] : false; |
684 | 687 | |
685 | 688 | if ( !is_null( $min ) || !is_null( $max ) ) { |
686 | | - $value = is_array( $value ) ? $value : array( $value ); |
687 | | - foreach ( $value as &$v ) { |
688 | | - $this->validateLimit( $paramName, $v, $min, $max ); |
689 | | - } |
| 689 | + if ( is_array( $value ) ) { |
| 690 | + foreach ( $value as &$v ) { |
| 691 | + $this->validateLimit( $paramName, $v, $min, $max, $enforceLimits ); |
| 692 | + } |
| 693 | + } else { |
| 694 | + $this->validateLimit( $paramName, $value, $min, $max, $enforceLimits ); |
| 695 | + } |
690 | 696 | } |
691 | 697 | break; |
692 | 698 | case 'limit': |
— | — | @@ -820,10 +826,13 @@ |
821 | 827 | * @param $min int Minimum value |
822 | 828 | * @param $max int Maximum value for users |
823 | 829 | * @param $botMax int Maximum value for sysops/bots |
| 830 | + * @param $enforceLimits Boolean Whether to enforce (die) if value is outside limits |
824 | 831 | */ |
825 | | - function validateLimit( $paramName, &$value, $min, $max, $botMax = null ) { |
| 832 | + function validateLimit( $paramName, &$value, $min, $max, $botMax = null, $enforceLimits = false ) { |
826 | 833 | if ( !is_null( $min ) && $value < $min ) { |
827 | | - $this->setWarning( $this->encodeParamName( $paramName ) . " may not be less than $min (set to $value)" ); |
| 834 | + |
| 835 | + $msg = $this->encodeParamName( $paramName ) . " may not be less than $min (set to $value)"; |
| 836 | + $this->warnOrDie( $msg, $enforceLimits ); |
828 | 837 | $value = $min; |
829 | 838 | } |
830 | 839 | |
— | — | @@ -837,17 +846,33 @@ |
838 | 847 | if ( !is_null( $max ) && $value > $max ) { |
839 | 848 | if ( !is_null( $botMax ) && $this->getMain()->canApiHighLimits() ) { |
840 | 849 | if ( $value > $botMax ) { |
841 | | - $this->setWarning( $this->encodeParamName( $paramName ) . " may not be over $botMax (set to $value) for bots or sysops" ); |
| 850 | + $msg = $this->encodeParamName( $paramName ) . " may not be over $botMax (set to $value) for bots or sysops"; |
| 851 | + $this->warnOrDie( $msg, $enforceLimits ); |
842 | 852 | $value = $botMax; |
843 | 853 | } |
844 | 854 | } else { |
845 | | - $this->setWarning( $this->encodeParamName( $paramName ) . " may not be over $max (set to $value) for users" ); |
| 855 | + $msg = $this->encodeParamName( $paramName ) . " may not be over $max (set to $value) for users"; |
| 856 | + $this->warnOrDie( $msg, $enforceLimits ); |
846 | 857 | $value = $max; |
847 | 858 | } |
848 | 859 | } |
849 | 860 | } |
850 | 861 | |
851 | 862 | /** |
| 863 | + * Adds a warning to the output, else dies |
| 864 | + * |
| 865 | + * @param $msg String Message to show as a warning, or error message if dying |
| 866 | + * @param $enforceLimits Boolean Whether this is an enforce (die) |
| 867 | + */ |
| 868 | + private function warnOrDie( $msg, $enforceLimits = false ) { |
| 869 | + if ( $enforceLimits ) { |
| 870 | + $this->dieUsageMsg( $msg ); |
| 871 | + } else { |
| 872 | + $this->setWarning( $msg ); |
| 873 | + } |
| 874 | + } |
| 875 | + |
| 876 | + /** |
852 | 877 | * Truncate an array to a certain length. |
853 | 878 | * @param $arr array Array to truncate |
854 | 879 | * @param $limit int Maximum length |