* API: pageSet now supports pageids, revised revisions listings, lots of examples.
[lhc/web/wiklou.git] / includes / api / ApiBase.php
index d0aab37..c145b1d 100644 (file)
@@ -70,8 +70,7 @@ abstract class ApiBase {
                // Main module has getResult() method overriden
                // Safety - avoid infinite loop:
                if ($this->isMain())
-                       ApiBase :: dieDebug(__METHOD__ .
-                       ' base method was called on main module. ');
+                       ApiBase :: dieDebug(__METHOD__, 'base method was called on main module. ');
                return $this->getMain()->getResult();
        }
 
@@ -189,9 +188,9 @@ abstract class ApiBase {
                        $multi = false;
                        $type = gettype($paramSettings);
                } else {
-                       $default = isset ($paramSettings[self::PARAM_DFLT]) ? $paramSettings[self::PARAM_DFLT] : null;
-                       $multi = isset ($paramSettings[self::PARAM_ISMULTI]) ? $paramSettings[self::PARAM_ISMULTI] : false;
-                       $type = isset ($paramSettings[self::PARAM_TYPE]) ? $paramSettings[self::PARAM_TYPE] : null;
+                       $default = isset ($paramSettings[self :: PARAM_DFLT]) ? $paramSettings[self :: PARAM_DFLT] : null;
+                       $multi = isset ($paramSettings[self :: PARAM_ISMULTI]) ? $paramSettings[self :: PARAM_ISMULTI] : false;
+                       $type = isset ($paramSettings[self :: PARAM_TYPE]) ? $paramSettings[self :: PARAM_TYPE] : null;
 
                        // When type is not given, and no choices, the type is the same as $default
                        if (!isset ($type)) {
@@ -205,7 +204,7 @@ abstract class ApiBase {
                if ($type == 'boolean') {
                        if (isset ($default) && $default !== false) {
                                // Having a default value of anything other than 'false' is pointless
-                               ApiBase :: dieDebug("Boolean param $paramName's default is set to '$default'");
+                               ApiBase :: dieDebug(__METHOD__, "Boolean param $paramName's default is set to '$default'");
                        }
 
                        $value = $wgRequest->getCheck($paramName);
@@ -228,25 +227,26 @@ abstract class ApiBase {
                                        $value = is_array($value) ? array_map('intval', $value) : intval($value);
                                        break;
                                case 'limit' :
-                                       if (!isset ($paramSettings[self::PARAM_MAX1]) || !isset ($paramSettings[self::PARAM_MAX2]))
-                                               ApiBase :: dieDebug("MAX1 or MAX2 are not defined for the limit $paramName");
+                                       if (!isset ($paramSettings[self :: PARAM_MAX1]) || !isset ($paramSettings[self :: PARAM_MAX2]))
+                                               ApiBase :: dieDebug(__METHOD__, "MAX1 or MAX2 are not defined for the limit $paramName");
                                        if ($multi)
-                                               ApiBase :: dieDebug("Multi-values not supported for $paramName");
-                                       $min = isset ($paramSettings[self::PARAM_MIN]) ? $paramSettings[self::PARAM_MIN] : 0;
+                                               ApiBase :: dieDebug(__METHOD__, "Multi-values not supported for $paramName");
+                                       $min = isset ($paramSettings[self :: PARAM_MIN]) ? $paramSettings[self :: PARAM_MIN] : 0;
                                        $value = intval($value);
-                                       $this->validateLimit($paramName, $value, $min, $paramSettings[self::PARAM_MAX1], $paramSettings[self::PARAM_MAX2]);
+                                       $this->validateLimit($paramName, $value, $min, $paramSettings[self :: PARAM_MAX1], $paramSettings[self :: PARAM_MAX2]);
                                        break;
                                case 'boolean' :
                                        if ($multi)
-                                               ApiBase :: dieDebug("Multi-values not supported for $paramName");
+                                               ApiBase :: dieDebug(__METHOD__, "Multi-values not supported for $paramName");
                                        break;
                                case 'timestamp' :
                                        if ($multi)
-                                               ApiBase :: dieDebug("Multi-values not supported for $paramName");
-                                       $value = $this->prepareTimestamp($value); // Adds quotes around timestamp                                                       
+                                               ApiBase :: dieDebug(__METHOD__, "Multi-values not supported for $paramName");
+                                       if (!preg_match('/^[0-9]{14}$/', $value))
+                                               $this->dieUsage("Invalid value '$value' for timestamp parameter $paramName", "badtimestamp_{$valueName}");
                                        break;
                                default :
-                                       ApiBase :: dieDebug("Param $paramName's type is unknown - $type");
+                                       ApiBase :: dieDebug(__METHOD__, "Param $paramName's type is unknown - $type");
 
                        }
                }
@@ -280,17 +280,6 @@ abstract class ApiBase {
                return $allowMultiple ? $valuesList : $valuesList[0];
        }
 
-       /**
-       * Validate the proper format of the timestamp string (14 digits), and add quotes to it.
-       */
-       function prepareTimestamp($value) {
-               if (preg_match('/^[0-9]{14}$/', $value)) {
-                       return $this->db->addQuotes($value);
-               } else {
-                       $this->dieUsage('Incorrect timestamp format', 'badtimestamp');
-               }
-       }
-
        /**
        * Validate the value against the minimum and user/bot maximum limits. Prints usage info on failure.
        */
@@ -305,10 +294,9 @@ abstract class ApiBase {
                        if ($value > $botMax) {
                                $this->dieUsage("$varname may not be over $botMax (set to $value) for bots", $varname);
                        }
-               } else {
-                       if ($value > $max) {
-                               $this->dieUsage("$varname may not be over $max (set to $value) for users", $varname);
-                       }
+               }
+               elseif ($value > $max) {
+                       $this->dieUsage("$varname may not be over $max (set to $value) for users", $varname);
                }
        }
 
@@ -322,8 +310,8 @@ abstract class ApiBase {
        /**
         * Internal code errors should be reported with this method
         */
-       protected static function dieDebug($message) {
-               wfDebugDieBacktrace("Internal error: $message");
+       protected static function dieDebug($method, $message) {
+               wfDebugDieBacktrace("Internal error in $method: $message");
        }
 
        /**
@@ -336,7 +324,7 @@ abstract class ApiBase {
         */
        public function profileIn() {
                if ($this->mTimeIn !== 0)
-                       ApiBase :: dieDebug(__METHOD__ . ' called twice without calling profileOut()');
+                       ApiBase :: dieDebug(__METHOD__, 'called twice without calling profileOut()');
                $this->mTimeIn = microtime(true);
        }
 
@@ -345,9 +333,9 @@ abstract class ApiBase {
         */
        public function profileOut() {
                if ($this->mTimeIn === 0)
-                       ApiBase :: dieDebug(__METHOD__ . ' called without calling profileIn() first');
+                       ApiBase :: dieDebug(__METHOD__, 'called without calling profileIn() first');
                if ($this->mDBTimeIn !== 0)
-                       ApiBase :: dieDebug(__METHOD__ . ' must be called after database profiling is done with profileDBOut()');
+                       ApiBase :: dieDebug(__METHOD__, 'must be called after database profiling is done with profileDBOut()');
 
                $this->mModuleTime += microtime(true) - $this->mTimeIn;
                $this->mTimeIn = 0;
@@ -358,7 +346,7 @@ abstract class ApiBase {
         */
        public function getProfileTime() {
                if ($this->mTimeIn !== 0)
-                       ApiBase :: dieDebug(__METHOD__ . ' called without calling profileOut() first');
+                       ApiBase :: dieDebug(__METHOD__, 'called without calling profileOut() first');
                return $this->mModuleTime;
        }
 
@@ -372,9 +360,9 @@ abstract class ApiBase {
         */
        public function profileDBIn() {
                if ($this->mTimeIn === 0)
-                       ApiBase :: dieDebug(__METHOD__ . ' must be called while profiling the entire module with profileIn()');
+                       ApiBase :: dieDebug(__METHOD__, 'must be called while profiling the entire module with profileIn()');
                if ($this->mDBTimeIn !== 0)
-                       ApiBase :: dieDebug(__METHOD__ . ' called twice without calling profileDBOut()');
+                       ApiBase :: dieDebug(__METHOD__, 'called twice without calling profileDBOut()');
                $this->mDBTimeIn = microtime(true);
        }
 
@@ -383,9 +371,9 @@ abstract class ApiBase {
         */
        public function profileDBOut() {
                if ($this->mTimeIn === 0)
-                       ApiBase :: dieDebug(__METHOD__ . ' must be called while profiling the entire module with profileIn()');
+                       ApiBase :: dieDebug(__METHOD__, 'must be called while profiling the entire module with profileIn()');
                if ($this->mDBTimeIn === 0)
-                       ApiBase :: dieDebug(__METHOD__ . ' called without calling profileDBIn() first');
+                       ApiBase :: dieDebug(__METHOD__, 'called without calling profileDBIn() first');
 
                $time = microtime(true) - $this->mDBTimeIn;
                $this->mDBTimeIn = 0;
@@ -399,7 +387,7 @@ abstract class ApiBase {
         */
        public function getProfileDBTime() {
                if ($this->mDBTimeIn !== 0)
-                       ApiBase :: dieDebug(__METHOD__ . ' called without calling profileDBOut() first');
+                       ApiBase :: dieDebug(__METHOD__, 'called without calling profileDBOut() first');
                return $this->mDBTime;
        }
 }