* API: Refactored per brion's suggestions
[lhc/web/wiklou.git] / includes / api / ApiBase.php
index 649ae51..1f87592 100644 (file)
@@ -46,42 +46,42 @@ abstract class ApiBase {
        /**\r
         * Executes this module\r
         */\r
-       abstract function Execute();\r
+       abstract function execute();\r
 \r
        /**\r
         * Get main module\r
         */\r
-       public function GetMain() {\r
+       public function getMain() {\r
                return $this->mMainModule;\r
        }\r
 \r
        /**\r
         * If this module's $this is the same as $this->mMainModule, its the root, otherwise no\r
         */\r
-       public function IsMain() {\r
+       public function isMain() {\r
                return $this === $this->mMainModule;\r
        }\r
 \r
        /**\r
         * Get result object\r
         */\r
-       public function GetResult() {\r
-               // Main module has GetResult() method overriden\r
+       public function getResult() {\r
+               // Main module has getResult() method overriden\r
                // Safety - avoid infinite loop:\r
-               if ($this->IsMain())\r
-                       $this->DieDebug(__METHOD__ .\r
+               if ($this->isMain())\r
+                       $this->dieDebug(__METHOD__ .\r
                        ' base method was called on main module. ');\r
-               return $this->GetMain()->GetResult();\r
+               return $this->getMain()->getResult();\r
        }\r
 \r
        /**\r
         * Generates help message for this module, or false if there is no description\r
         */\r
-       public function MakeHelpMsg() {\r
+       public function makeHelpMsg() {\r
 \r
                static $lnPrfx = "\n  ";\r
 \r
-               $msg = $this->GetDescription();\r
+               $msg = $this->getDescription();\r
 \r
                if ($msg !== false) {\r
 \r
@@ -92,9 +92,9 @@ abstract class ApiBase {
                        $msg = $lnPrfx . implode($lnPrfx, $msg) . "\n";\r
 \r
                        // Parameters\r
-                       $params = $this->GetAllowedParams();\r
+                       $params = $this->getAllowedParams();\r
                        if ($params !== false) {\r
-                               $paramsDescription = $this->GetParamDescription();\r
+                               $paramsDescription = $this->getParamDescription();\r
                                $msg .= "Parameters:\n";\r
                                foreach (array_keys($params) as $paramName) {\r
                                        $desc = isset ($paramsDescription[$paramName]) ? $paramsDescription[$paramName] : '';\r
@@ -105,7 +105,7 @@ abstract class ApiBase {
                        }\r
 \r
                        // Examples\r
-                       $examples = $this->GetExamples();\r
+                       $examples = $this->getExamples();\r
                        if ($examples !== false) {\r
                                if (!is_array($examples))\r
                                        $examples = array (\r
@@ -122,40 +122,40 @@ abstract class ApiBase {
        /**\r
         * Returns the description string for this module\r
         */\r
-       protected function GetDescription() {\r
+       protected function getDescription() {\r
                return false;\r
        }\r
 \r
        /**\r
         * Returns usage examples for this module. Return null if no examples are available.\r
         */\r
-       protected function GetExamples() {\r
+       protected function getExamples() {\r
                return false;\r
        }\r
 \r
        /**\r
         * Returns an array of allowed parameters (keys) => default value for that parameter\r
         */\r
-       protected function GetAllowedParams() {\r
+       protected function getAllowedParams() {\r
                return false;\r
        }\r
 \r
        /**\r
         * Returns the description string for the given parameter.\r
         */\r
-       protected function GetParamDescription() {\r
+       protected function getParamDescription() {\r
                return false;\r
        }\r
 \r
        /**\r
-       * Using GetAllowedParams(), makes an array of the values provided by the user,\r
+       * Using getAllowedParams(), makes an array of the values provided by the user,\r
        * with key being the name of the variable, and value - validated value from user or default.\r
        * This method can be used to generate local variables using extract().\r
        */\r
-       public function ExtractRequestParams() {\r
+       public function extractRequestParams() {\r
                global $wgRequest;\r
 \r
-               $params = $this->GetAllowedParams();\r
+               $params = $this->getAllowedParams();\r
                $results = array ();\r
 \r
                foreach ($params as $param => $enumParams) {\r
@@ -184,17 +184,17 @@ abstract class ApiBase {
 \r
                                if ($default !== false) {\r
                                        // Having a default value of anything other than 'false' is pointless\r
-                                       $this->DieDebug("Boolean param $param's default is set to '$default'");\r
+                                       $this->dieDebug("Boolean param $param's default is set to '$default'");\r
                                }\r
                        }\r
 \r
                        $value = $wgRequest->getVal($param, $default);\r
 \r
                        if (isset ($value) && ($multi || is_array($type)))\r
-                               $value = $this->ParseMultiValue($param, $value, $multi, is_array($type) ? $type : null);\r
+                               $value = $this->parseMultiValue($param, $value, $multi, is_array($type) ? $type : null);\r
 \r
                        // More validation only when choices were not given\r
-                       // choices were validated in ParseMultiValue()\r
+                       // choices were validated in parseMultiValue()\r
                        if (!is_array($type) && isset ($value)) {\r
 \r
                                switch ($type) {\r
@@ -207,25 +207,25 @@ abstract class ApiBase {
                                                break;\r
                                        case 'limit' :\r
                                                if (!isset ($enumParams[GN_ENUM_MAX1]) || !isset ($enumParams[GN_ENUM_MAX2]))\r
-                                                       $this->DieDebug("MAX1 or MAX2 are not defined for the limit $param");\r
+                                                       $this->dieDebug("MAX1 or MAX2 are not defined for the limit $param");\r
                                                if ($multi)\r
-                                                       $this->DieDebug("Multi-values not supported for $param");\r
+                                                       $this->dieDebug("Multi-values not supported for $param");\r
                                                $min = isset ($enumParams[GN_ENUM_MIN]) ? $enumParams[GN_ENUM_MIN] : 0;\r
                                                $value = intval($value);\r
-                                               $this->ValidateLimit($param, $value, $min, $enumParams[GN_ENUM_MAX1], $enumParams[GN_ENUM_MAX2]);\r
+                                               $this->validateLimit($param, $value, $min, $enumParams[GN_ENUM_MAX1], $enumParams[GN_ENUM_MAX2]);\r
                                                break;\r
                                        case 'boolean' :\r
                                                if ($multi)\r
-                                                       $this->DieDebug("Multi-values not supported for $param");\r
+                                                       $this->dieDebug("Multi-values not supported for $param");\r
                                                $value = isset ($value);\r
                                                break;\r
                                        case 'timestamp' :\r
                                                if ($multi)\r
-                                                       $this->DieDebug("Multi-values not supported for $param");\r
+                                                       $this->dieDebug("Multi-values not supported for $param");\r
                                                $value = $this->prepareTimestamp($value); // Adds quotes around timestamp                                                       \r
                                                break;\r
                                        default :\r
-                                               $this->DieDebug("Param $param's type is unknown - $type");\r
+                                               $this->dieDebug("Param $param's type is unknown - $type");\r
 \r
                                }\r
                        }\r
@@ -246,16 +246,16 @@ abstract class ApiBase {
        * @param allowedValues - An array of values to check against. If null, all values are accepted.\r
        * @return (allowMultiple ? an_array_of_values : a_single_value) \r
        */\r
-       protected function ParseMultiValue($valueName, $value, $allowMultiple, $allowedValues) {\r
+       protected function parseMultiValue($valueName, $value, $allowMultiple, $allowedValues) {\r
                $valuesList = explode('|', $value);\r
                if (!$allowMultiple && count($valuesList) != 1) {\r
                        $possibleValues = is_array($allowedValues) ? "of '" . implode("', '", $allowedValues) . "'" : '';\r
-                       $this->DieUsage("Only one $possibleValues is allowed for parameter '$valueName'", "multival_$valueName");\r
+                       $this->dieUsage("Only one $possibleValues is allowed for parameter '$valueName'", "multival_$valueName");\r
                }\r
                if (is_array($allowedValues)) {\r
                        $unknownValues = array_diff($valuesList, $allowedValues);\r
                        if ($unknownValues) {\r
-                               $this->DieUsage("Unrecognised value" . (count($unknownValues) > 1 ? "s '" : " '") . implode("', '", $unknownValues) . "' for parameter '$valueName'", "unknown_$valueName");\r
+                               $this->dieUsage("Unrecognised value" . (count($unknownValues) > 1 ? "s '" : " '") . implode("', '", $unknownValues) . "' for parameter '$valueName'", "unknown_$valueName");\r
                        }\r
                }\r
 \r
@@ -276,14 +276,14 @@ abstract class ApiBase {
        /**\r
        * Validate the value against the minimum and user/bot maximum limits. Prints usage info on failure.\r
        */\r
-       function ValidateLimit($varname, $value, $min, $max, $botMax) {\r
+       function validateLimit($varname, $value, $min, $max, $botMax) {\r
                global $wgUser;\r
 \r
                if ($value < $min) {\r
                        $this->dieUsage("$varname may not be less than $min (set to $value)", $varname);\r
                }\r
 \r
-               if ($this->GetMain()->IsBot()) {\r
+               if ($this->getMain()->isBot()) {\r
                        if ($value > $botMax) {\r
                                $this->dieUsage("$varname may not be over $botMax (set to $value) for bots", $varname);\r
                        }\r
@@ -297,12 +297,98 @@ abstract class ApiBase {
        /**\r
         * Call main module's error handler \r
         */\r
-       public function DieUsage($description, $errorCode, $httpRespCode = 0) {\r
-               $this->GetMain()->MainDieUsage($description, $errorCode, $httpRespCode);\r
+       public function dieUsage($description, $errorCode, $httpRespCode = 0) {\r
+               $this->getMain()->mainDieUsage($description, $errorCode, $httpRespCode);\r
        }\r
 \r
-       protected function DieDebug($message) {\r
+       /**\r
+        * Internal code errors should be reported with this method\r
+        */\r
+       protected function dieDebug($message) {\r
                wfDebugDieBacktrace("Internal error in '{get_class($this)}': $message");\r
        }\r
+       \r
+       /**\r
+        * Profiling: total module execution time\r
+        */\r
+       private $mTimeIn = 0, $mModuleTime = 0; \r
+       \r
+       /**\r
+        * Start module profiling\r
+        */\r
+       public function profileIn()\r
+       {\r
+               if ($this->mTimeIn !== 0)\r
+                       $this->dieDebug(__FUNCTION__ . ' called twice without calling profileOut()');\r
+               $this->mTimeIn = microtime(true);\r
+       }\r
+       \r
+       /**\r
+        * End module profiling\r
+        */\r
+       public function profileOut()\r
+       {\r
+               if ($this->mTimeIn === 0)\r
+                       $this->dieDebug(__FUNCTION__ . ' called without calling profileIn() first');\r
+               if ($this->mDBTimeIn !== 0)\r
+                       $this->dieDebug(__FUNCTION__ . ' must be called after database profiling is done with profileDBOut()');\r
+\r
+               $this->mModuleTime += microtime(true) - $this->mTimeIn;\r
+               $this->mTimeIn = 0;\r
+       }\r
+       \r
+       /**\r
+        * Total time the module was executed\r
+        */\r
+       public function getProfileTime()\r
+       {\r
+               if ($this->mTimeIn !== 0)\r
+                       $this->dieDebug(__FUNCTION__ . ' called without calling profileOut() first');\r
+               return $this->mModuleTime;\r
+       }\r
+       \r
+       /**\r
+        * Profiling: database execution time\r
+        */\r
+       private $mDBTimeIn = 0, $mDBTime = 0; \r
+       \r
+       /**\r
+        * Start module profiling\r
+        */\r
+       public function profileDBIn()\r
+       {\r
+               if ($this->mTimeIn === 0)\r
+                       $this->dieDebug(__FUNCTION__ . ' must be called while profiling the entire module with profileIn()');\r
+               if ($this->mDBTimeIn !== 0)\r
+                       $this->dieDebug(__FUNCTION__ . ' called twice without calling profileDBOut()');\r
+               $this->mDBTimeIn = microtime(true);\r
+       }\r
+       \r
+       /**\r
+        * End database profiling\r
+        */\r
+       public function profileDBOut()\r
+       {\r
+               if ($this->mTimeIn === 0)\r
+                       $this->dieDebug(__FUNCTION__ . ' must be called while profiling the entire module with profileIn()');\r
+               if ($this->mDBTimeIn === 0)\r
+                       $this->dieDebug(__FUNCTION__ . ' called without calling profileDBIn() first');\r
+\r
+               $time = microtime(true) - $this->mDBTimeIn;\r
+               $this->mDBTimeIn = 0;\r
+\r
+               $this->mDBTime += $time;\r
+               $this->getMain()->mDBTime += $time;\r
+       }\r
+       \r
+       /**\r
+        * Total time the module used the database\r
+        */\r
+       public function getProfileDBTime()\r
+       {\r
+               if ($this->mDBTimeIn !== 0)\r
+                       $this->dieDebug(__FUNCTION__ . ' called without calling profileDBOut() first');\r
+               return $this->mDBTime;\r
+       }\r
 }\r
 ?>
\ No newline at end of file