(bug 15359) Add APIGetAllowedParams and APIGetParamDescription hooks
authorRoan Kattouw <catrope@users.mediawiki.org>
Sun, 7 Sep 2008 19:04:51 +0000 (19:04 +0000)
committerRoan Kattouw <catrope@users.mediawiki.org>
Sun, 7 Sep 2008 19:04:51 +0000 (19:04 +0000)
docs/hooks.txt
includes/api/ApiBase.php
includes/api/ApiParamInfo.php

index d26d514..9e9dbf9 100644 (file)
@@ -273,6 +273,15 @@ $EditPage : the EditPage object
 $text : the new text of the article (has yet to be saved)
 $resultArr : data in this array will be added to the API result
 
+'APIGetAllowedParams': use this hook to modify a module's parameters.
+&$module: Module object
+&$params: Array of parameters
+
+'APIGetParamDescription': use this hook to modify a module's parameter
+descriptions.
+&$module: Module object
+&$desc: Array of parameter descriptions
+
 'APIQueryInfoTokens': use this hook to add custom tokens to prop=info.
 Every token has an action, which will be used in the intoken parameter
 and in the output (actiontoken="..."), and a callback function which
index 144f61f..7791764 100644 (file)
@@ -242,10 +242,10 @@ abstract class ApiBase {
         * module's help.
         */
        public function makeHelpMsgParameters() {
-               $params = $this->getAllowedParams();
+               $params = $this->getFinalParams();
                if ($params !== false) {
 
-                       $paramsDescription = $this->getParamDescription();
+                       $paramsDescription = $this->getFinalParamDescription();
                        $msg = '';
                        $paramPrefix = "\n" . str_repeat(' ', 19);
                        foreach ($params as $paramName => $paramSettings) {
@@ -323,18 +323,39 @@ abstract class ApiBase {
        }
 
        /**
-        * Returns an array of allowed parameters (keys) => default value for that parameter
+        * Returns an array of allowed parameters (keys) => default value for that parameter.
+        * Don't call this function directly: use getFinalParams() to allow hooks
+        * to modify parameters as needed.
         */
        protected function getAllowedParams() {
                return false;
        }
 
        /**
-        * Returns the description string for the given parameter.
+        * Returns an array of parameter descriptions.
+        * Don't call this functon directly: use getFinalParamDescription() to allow
+        * hooks to modify descriptions as needed.
         */
        protected function getParamDescription() {
                return false;
        }
+       
+       /**
+        * Get final list of parameters, after hooks have had
+        * a chance to tweak it as needed.
+        */
+       public function getFinalParams() {
+               $params = $this->getAllowedParams();
+               wfRunHooks('APIGetAllowedParams', array(&$this, &$params));
+               return $params;
+       }
+       
+       
+       public function getFinalParamDescription() {
+               $desc = $this->getParamDescription();
+               wfRunHooks('APIGetParamDescription', array(&$this, &$desc));
+               return $desc;
+       }
 
        /**
         * This method mangles parameter name based on the prefix supplied to the constructor.
@@ -352,7 +373,7 @@ abstract class ApiBase {
        * when the max limit is not definite, e.g. when getting revisions.
        */
        public function extractRequestParams($parseMaxLimit = true) {
-               $params = $this->getAllowedParams();
+               $params = $this->getFinalParams();
                $results = array ();
 
                foreach ($params as $paramName => $paramSettings)
@@ -365,7 +386,7 @@ abstract class ApiBase {
         * Get a value for the given parameter
         */
        protected function getParameter($paramName, $parseMaxLimit = true) {
-               $params = $this->getAllowedParams();
+               $params = $this->getFinalParams();
                $paramSettings = $params[$paramName];
                return $this->getParameterFromSettings($paramName, $paramSettings, $parseMaxLimit);
        }
index 91e526e..8560753 100644 (file)
@@ -86,12 +86,12 @@ class ApiParamInfo extends ApiBase {
                $retval['classname'] = get_class($obj);
                $retval['description'] = (is_array($obj->getDescription()) ? implode("\n", $obj->getDescription()) : $obj->getDescription());
                $retval['prefix'] = $obj->getModulePrefix();
-               $allowedParams = $obj->getAllowedParams();
+               $allowedParams = $obj->getFinalParams();
                if(!is_array($allowedParams))
                        return $retval;
                $retval['parameters'] = array();
-               $paramDesc = $obj->getParamDescription();
-               foreach($obj->getAllowedParams() as $n => $p)
+               $paramDesc = $obj->getFinalParamDescription();
+               foreach($allowedParams as $n => $p)
                {
                        $a = array('name' => $n);
                        if(!is_array($p))