From ff442ae79f1960730b8e5f5761f0d0ca591030ef Mon Sep 17 00:00:00 2001 From: Sam Reed Date: Sat, 8 May 2010 11:45:41 +0000 Subject: [PATCH] * (bug 19721) API action=help should have a way to just list for a specific module --- RELEASE-NOTES | 1 + includes/api/ApiHelp.php | 92 +++++++++++++++++++++++++++++++++++++-- includes/api/ApiMain.php | 11 +++-- includes/api/ApiQuery.php | 21 +++++++++ 4 files changed, 119 insertions(+), 6 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 991df1afa3..b52a74d33f 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -163,6 +163,7 @@ in a negative namespace (which is invalid). * (bug 22868) don't list infinite block expiry date as "now" in API logevents * (bug 22290) prop=revisions now outputs "comment" field even when comment is empty, for consistency with list=recentchanges +* (bug 19721) API action=help should have a way to just list for a specific module === Languages updated in 1.17 === diff --git a/includes/api/ApiHelp.php b/includes/api/ApiHelp.php index 761143fa95..d16fd29a96 100644 --- a/includes/api/ApiHelp.php +++ b/includes/api/ApiHelp.php @@ -40,10 +40,67 @@ class ApiHelp extends ApiBase { } /** - * Stub module for displaying help when no parameters are given + * Module for displaying help */ public function execute() { - $this->dieUsage( '', 'help' ); + // Get parameters + $params = $this->extractRequestParams(); + + if ( !isset( $params['modules'] ) && !isset( $params['querymodules'] ) ) { + $this->dieUsage( '', 'help' ); + } + + $this->getMain()->setHelp(); + + $result = $this->getResult(); + $queryObj = new ApiQuery( $this->getMain(), 'query' ); + $r = array(); + if ( is_array( $params['modules'] ) ) { + $modArr = $this->getMain()->getModules(); + + foreach ( $params['modules'] as $m ) { + if ( !isset( $modArr[$m] ) ) { + $r[] = array( 'name' => $m, 'missing' => '' ); + continue; + } + $module = new $modArr[$m]( $this->getMain(), $m ); + + $r[] = $this->buildModuleHelp( $module, 'action' ); + } + } + + if ( is_array( $params['querymodules'] ) ) { + $qmodArr = $queryObj->getModules(); + + foreach ( $params['querymodules'] as $qm ) { + if ( !isset( $qmodArr[$qm] ) ) { + $r[] = array( 'name' => $qm, 'missing' => '' ); + continue; + } + $module = new $qmodArr[$qm]( $this, $qm ); + $type = $queryObj->getModuleType( $qm ); + + if ( $type === null ) { + $r[] = array( 'name' => $qm, 'missing' => '' ); + continue; + } + + $r[] = $this->buildModuleHelp( $module, $type ); + } + } + $result->setIndexedTagName( $r, 'module' ); + $result->addValue( null, $this->getModuleName(), $r ); + } + + private function buildModuleHelp( $module, $type ) { + $msg = ApiMain::makeHelpMsgHeader( $module, $type ); + + $msg2 = $module->makeHelpMsg(); + if ( $msg2 !== false ) { + $msg .= $msg2; + } + + return $msg; } public function shouldCheckMaxlag() { @@ -53,10 +110,39 @@ class ApiHelp extends ApiBase { public function isReadMode() { return false; } + + public function getAllowedParams() { + return array( + 'modules' => array( + ApiBase::PARAM_ISMULTI => true + ), + 'querymodules' => array( + ApiBase::PARAM_ISMULTI => true + ), + ); + } + + public function getParamDescription() { + return array( + 'modules' => 'List of module names (value of the action= parameter)', + 'querymodules' => 'List of query module names (value of prop=, meta= or list= parameter)', + ); + } public function getDescription() { return array( - 'Display this help screen.' + 'Display this help screen. Or the help screen for the specified module' + ); + } + + protected function getExamples() { + return array( + 'Whole help page:', + ' api.php?action=help', + 'Module help page:', + ' api.php?action=help&modules=protect', + 'Query modules help page:', + ' api.php?action=help&querymodules=categorymembers', ); } diff --git a/includes/api/ApiMain.php b/includes/api/ApiMain.php index edfd2d36b6..fb99be97a3 100644 --- a/includes/api/ApiMain.php +++ b/includes/api/ApiMain.php @@ -674,13 +674,19 @@ class ApiMain extends ApiBase { 'or file a bug report at http://bugzilla.wikimedia.org/' ); } + /** + * Sets whether the pretty-printer should format *bold* and $italics$ + */ + public function setHelp( $help = true ) { + $this->mPrinter->setHelp( $help ); + } /** * Override the parent to generate help messages for all available modules. */ public function makeHelpMsg() { global $wgMemc, $wgAPICacheHelp, $wgAPICacheHelpTimeout; - $this->mPrinter->setHelp(); + $this->setHelp(); // Get help text from cache if present $key = wfMemcKey( 'apihelp', $this->getModuleName(), SpecialVersion::getVersion( 'nodb' ) . @@ -699,7 +705,7 @@ class ApiMain extends ApiBase { } public function reallyMakeHelpMsg() { - $this->mPrinter->setHelp(); + $this->setHelp(); // Use parent to make default message for the main module $msg = parent::makeHelpMsg(); @@ -737,7 +743,6 @@ class ApiMain extends ApiBase { $msg .= "\n*** Credits: ***\n " . implode( "\n ", $this->getCredits() ) . "\n"; - return $msg; } diff --git a/includes/api/ApiQuery.php b/includes/api/ApiQuery.php index 16e0352601..ed84a70647 100644 --- a/includes/api/ApiQuery.php +++ b/includes/api/ApiQuery.php @@ -171,6 +171,27 @@ class ApiQuery extends ApiBase { function getModules() { return array_merge( $this->mQueryPropModules, $this->mQueryListModules, $this->mQueryMetaModules ); } + + /** + * Get whether the specified module is a prop, list or a meta query module + * @param $moduleName string Name of the module to find type for + * @return mixed string or null + */ + function getModuleType( $moduleName ) { + if ( array_key_exists ( $moduleName, $this->mQueryPropModules ) ) { + return 'prop'; + } + + if ( array_key_exists ( $moduleName, $this->mQueryListModules ) ) { + return 'list'; + } + + if ( array_key_exists ( $moduleName, $this->mQueryMetaModules ) ) { + return 'meta'; + } + + return null; + } public function getCustomPrinter() { // If &exportnowrap is set, use the raw formatter -- 2.20.1