Merge "Select column page_lang in prop=info"
[lhc/web/wiklou.git] / includes / api / ApiHelp.php
index bcd6c12..b33b087 100644 (file)
@@ -2,9 +2,9 @@
 /**
  *
  *
- * Created on Sep 6, 2006
+ * Created on Aug 29, 2014
  *
- * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
+ * Copyright © 2014 Brad Jorsch <bjorsch@wikimedia.org>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  */
 
 /**
- * This is a simple class to handle action=help
+ * Class to output help for an API module
  *
+ * @since 1.25 completely rewritten
  * @ingroup API
  */
 class ApiHelp extends ApiBase {
-       /**
-        * Module for displaying help
-        */
        public function execute() {
-               // Get parameters
+               global $wgContLang;
+
                $params = $this->extractRequestParams();
+               $modules = array();
 
-               if ( !isset( $params['modules'] ) && !isset( $params['querymodules'] ) ) {
-                       $this->dieUsage( '', 'help' );
+               foreach ( $params['modules'] as $path ) {
+                       $modules[] = $this->getModuleFromPath( $path );
                }
 
-               $this->getMain()->setHelp();
-               $result = $this->getResult();
+               // Get the help
+               $context = new DerivativeContext( $this->getMain()->getContext() );
+               $context->setSkin( SkinFactory::getDefaultInstance()->makeSkin( 'apioutput' ) );
+               $context->setLanguage( $this->getMain()->getLanguage() );
+               $context->setTitle( SpecialPage::getTitleFor( 'ApiHelp' ) );
+               $out = new OutputPage( $context );
+               $context->setOutput( $out );
 
-               if ( is_array( $params['modules'] ) ) {
-                       $modules = $params['modules'];
+               self::getHelp( $context, $modules, $params );
+
+               // Grab the output from the skin
+               ob_start();
+               $context->getOutput()->output();
+               $html = ob_get_clean();
+
+               $result = $this->getResult();
+               if ( $params['wrap'] ) {
+                       $data = array(
+                               'mime' => 'text/html',
+                               'help' => $html,
+                       );
+                       $result->setSubelements( $data, 'help' );
+                       $result->addValue( null, $this->getModuleName(), $data );
                } else {
-                       $modules = array();
+                       $result->reset();
+                       $result->addValue( null, 'text', $html, ApiResult::NO_SIZE_CHECK );
+                       $result->addValue( null, 'mime', 'text/html', ApiResult::NO_SIZE_CHECK );
+               }
+       }
+
+       /**
+        * Generate help for the specified modules
+        *
+        * Help is placed into the OutputPage object returned by
+        * $context->getOutput().
+        *
+        * Recognized options include:
+        *  - headerlevel: (int) Header tag level
+        *  - nolead: (bool) Skip the inclusion of api-help-lead
+        *  - noheader: (bool) Skip the inclusion of the top-level section headers
+        *  - submodules: (bool) Include help for submodules of the current module
+        *  - recursivesubmodules: (bool) Include help for submodules recursively
+        *  - helptitle: (string) Title to link for additional modules' help. Should contain $1.
+        *
+        * @param IContextSource $context
+        * @param ApiBase[]|ApiBase $modules
+        * @param array $options Formatting options (described above)
+        * @return string
+        */
+       public static function getHelp( IContextSource $context, $modules, array $options ) {
+               global $wgMemc, $wgContLang;
+
+               if ( !is_array( $modules ) ) {
+                       $modules = array( $modules );
                }
 
-               if ( is_array( $params['querymodules'] ) ) {
-                       $this->logFeatureUsage( 'action=help&querymodules' );
-                       $queryModules = $params['querymodules'];
-                       foreach ( $queryModules as $m ) {
-                               $modules[] = 'query+' . $m;
+               $out = $context->getOutput();
+               $out->addModules( 'mediawiki.apihelp' );
+               $out->setPageTitle( $context->msg( 'api-help-title' ) );
+
+               $cacheKey = null;
+               if ( count( $modules ) == 1 && $modules[0] instanceof ApiMain &&
+                       $options['recursivesubmodules'] && $context->getLanguage() === $wgContLang
+               ) {
+                       $cacheHelpTimeout = $context->getConfig()->get( 'APICacheHelpTimeout' );
+                       if ( $cacheHelpTimeout > 0 ) {
+                               // Get help text from cache if present
+                               $cacheKey = wfMemcKey( 'apihelp', $modules[0]->getModulePath(),
+                                       str_replace( ' ', '_', SpecialVersion::getVersion( 'nodb' ) ) );
+                               $cached = $wgMemc->get( $cacheKey );
+                               if ( $cached ) {
+                                       $out->addHTML( $cached );
+                                       return;
+                               }
                        }
-               } else {
-                       $queryModules = array();
+               }
+               if ( $out->getHTML() !== '' ) {
+                       // Don't save to cache, there's someone else's content in the page
+                       // already
+                       $cacheKey = null;
                }
 
-               $r = array();
-               foreach ( $modules as $m ) {
-                       // sub-modules could be given in the form of "name[+name[+name...]]"
-                       $subNames = explode( '+', $m );
-                       if ( count( $subNames ) === 1 ) {
-                               // In case the '+' was typed into URL, it resolves as a space
-                               $subNames = explode( ' ', $m );
+               $options['recursivesubmodules'] = !empty( $options['recursivesubmodules'] );
+               $options['submodules'] = $options['recursivesubmodules'] || !empty( $options['submodules'] );
+
+               // Prepend lead
+               if ( empty( $options['nolead'] ) ) {
+                       $msg = $context->msg( 'api-help-lead' );
+                       if ( !$msg->isDisabled() ) {
+                               $out->addHTML( $msg->parseAsBlock() );
                        }
+               }
 
-                       $module = $this->getMain();
-                       $subNamesCount = count( $subNames );
-                       for ( $i = 0; $i < $subNamesCount; $i++ ) {
-                               $subs = $module->getModuleManager();
-                               if ( $subs === null ) {
-                                       $module = null;
-                               } else {
-                                       $module = $subs->getModule( $subNames[$i] );
-                               }
+               $haveModules = array();
+               $out->addHTML( self::getHelpInternal( $context, $modules, $options, $haveModules ) );
 
-                               if ( $module === null ) {
-                                       if ( count( $subNames ) === 2
-                                               && $i === 1
-                                               && $subNames[0] === 'query'
-                                               && in_array( $subNames[1], $queryModules )
-                                       ) {
-                                               // Legacy: This is one of the renamed 'querymodule=...' parameters,
-                                               // do not use '+' notation in the output, use submodule's name instead.
-                                               $name = $subNames[1];
-                                       } else {
-                                               $name = implode( '+', array_slice( $subNames, 0, $i + 1 ) );
-                                       }
-                                       $r[] = array( 'name' => $name, 'missing' => '' );
-                                       break;
+               $helptitle = isset( $options['helptitle'] ) ? $options['helptitle'] : null;
+               $html = self::fixHelpLinks( $out->getHTML(), $helptitle, $haveModules );
+               $out->clearHTML();
+               $out->addHTML( $html );
+
+               if ( $cacheKey !== null ) {
+                       $wgMemc->set( $cacheKey, $out->getHTML(), $cacheHelpTimeout );
+               }
+       }
+
+       /**
+        * Replace Special:ApiHelp links with links to api.php
+        *
+        * @param string $html
+        * @param string|null $helptitle Title to link to rather than api.php, must contain '$1'
+        * @param array $localModules Modules to link within the current page
+        * @return string
+        */
+       public static function fixHelpLinks( $html, $helptitle = null, $localModules = array() ) {
+               $formatter = new HtmlFormatter( $html );
+               $doc = $formatter->getDoc();
+               $xpath = new DOMXPath( $doc );
+               $nodes = $xpath->query( '//a[@href][not(contains(@class,\'apihelp-linktrail\'))]' );
+               foreach ( $nodes as $node ) {
+                       $href = $node->getAttribute( 'href' );
+                       do {
+                               $old = $href;
+                               $href = rawurldecode( $href );
+                       } while ( $old !== $href );
+                       if ( preg_match( '!Special:ApiHelp/([^&/|]+)!', $href, $m ) ) {
+                               if ( isset( $localModules[$m[1]] ) ) {
+                                       $href = '#' . $m[1];
+                               } elseif ( $helptitle !== null ) {
+                                       $href = Title::newFromText( str_replace( '$1', $m[1], $helptitle ) )
+                                               ->getFullUrl();
                                } else {
-                                       $type = $subs->getModuleGroup( $subNames[$i] );
+                                       $href = wfAppendQuery( wfScript( 'api' ), array(
+                                               'action' => 'help',
+                                               'modules' => $m[1],
+                                       ) );
                                }
-                       }
-
-                       if ( $module !== null ) {
-                               $r[] = $this->buildModuleHelp( $module, $type );
+                               $node->setAttribute( 'href', $href );
+                               $node->removeAttribute( 'title' );
                        }
                }
 
-               $result->setIndexedTagName( $r, 'module' );
-               $result->addValue( null, $this->getModuleName(), $r );
+               return $formatter->getText();
+       }
+
+       /**
+        * Wrap a message in HTML with a class.
+        *
+        * @param Message $msg
+        * @param string $class
+        * @param string $tag
+        * @return string
+        */
+       private static function wrap( Message $msg, $class, $tag = 'span' ) {
+               return Html::rawElement( $tag, array( 'class' => $class ),
+                       $msg->parse()
+               );
        }
 
        /**
-        * @param ApiBase $module
-        * @param string $type What type of request is this? e.g. action, query, list, prop, meta, format
+        * Recursively-called function to actually construct the help
+        *
+        * @param IContextSource $context
+        * @param ApiBase[] $modules
+        * @param array $options
+        * @param array &$haveModules
         * @return string
         */
-       private function buildModuleHelp( $module, $type ) {
-               $msg = ApiMain::makeHelpMsgHeader( $module, $type );
+       private static function getHelpInternal( IContextSource $context, array $modules,
+               array $options, &$haveModules
+       ) {
+               $out = '';
+
+               $level = min( 6, empty( $options['headerlevel'] ) ? 2 : $options['headerlevel'] );
+               $options['headerlevel'] = $level;
+
+               foreach ( $modules as $module ) {
+                       $haveModules[$module->getModulePath()] = true;
+                       $module->setContext( $context );
+                       $help = array(
+                               'header' => '',
+                               'flags' => '',
+                               'description' => '',
+                               'help-urls' => '',
+                               'parameters' => '',
+                               'examples' => '',
+                               'submodules' => '',
+                       );
+
+                       if ( empty( $options['noheader'] ) ) {
+                               $path = $module->getModulePath();
+                               if ( $module->isMain() ) {
+                                       $header = $context->msg( 'api-help-main-header' )->parse();
+                               } else {
+                                       $name = $module->getModuleName();
+                                       $header = $module->getParent()->getModuleManager()->getModuleGroup( $name ) .
+                                               "=$name";
+                                       if ( $module->getModulePrefix() !== '' ) {
+                                               $header .= ' ' .
+                                                       $context->msg( 'parentheses', $module->getModulePrefix() )->parse();
+                                       }
+                               }
+                               $help['header'] .= Html::element( "h$level",
+                                       array( 'id' => $path, 'class' => 'apihelp-header' ),
+                                       $header
+                               );
+                       }
+
+                       $links = array();
+                       $any = false;
+                       for ( $m = $module; $m !== null; $m = $m->getParent() ) {
+                               $name = $m->getModuleName();
+                               if ( $name === 'main_int' ) {
+                                       $name = 'main';
+                               }
+
+                               if ( count( $modules ) === 1 && $m === $modules[0] &&
+                                       !( !empty( $options['submodules'] ) && $m->getModuleManager() )
+                               ) {
+                                       $link = Html::element( 'b', null, $name );
+                               } else {
+                                       $link = SpecialPage::getTitleFor( 'ApiHelp', $m->getModulePath() )->getLocalURL();
+                                       $link = Html::element( 'a',
+                                               array( 'href' => $link, 'class' => 'apihelp-linktrail' ),
+                                               $name
+                                       );
+                                       $any = true;
+                               }
+                               array_unshift( $links, $link );
+                       }
+                       if ( $any ) {
+                               $help['header'] .= self::wrap(
+                                       $context->msg( 'parentheses' )
+                                               ->rawParams( $context->getLanguage()->pipeList( $links ) ),
+                                       'apihelp-linktrail', 'div'
+                               );
+                       }
+
+                       $flags = $module->getHelpFlags();
+                       if ( $flags ) {
+                               $help['flags'] .= Html::openElement( 'div',
+                                       array( 'class' => 'apihelp-block apihelp-flags' ) );
+                               $msg = $context->msg( 'api-help-flags' );
+                               if ( !$msg->isDisabled() ) {
+                                       $help['flags'] .= self::wrap(
+                                               $msg->numParams( count( $flags ) ), 'apihelp-block-head', 'div'
+                                       );
+                               }
+                               $help['flags'] .= Html::openElement( 'ul' );
+                               foreach ( $flags as $flag ) {
+                                       $help['flags'] .= Html::rawElement( 'li', null,
+                                               self::wrap( $context->msg( "api-help-flag-$flag" ), "apihelp-flag-$flag" )
+                                       );
+                               }
+                               $help['flags'] .= Html::closeElement( 'ul' );
+                               $help['flags'] .= Html::closeElement( 'div' );
+                       }
+
+                       foreach ( $module->getFinalDescription() as $msg ) {
+                               $msg->setContext( $context );
+                               $help['description'] .= $msg->parseAsBlock();
+                       }
+
+                       $urls = $module->getHelpUrls();
+                       if ( $urls ) {
+                               $help['help-urls'] .= Html::openElement( 'div',
+                                       array( 'class' => 'apihelp-block apihelp-help-urls' )
+                               );
+                               $msg = $context->msg( 'api-help-help-urls' );
+                               if ( !$msg->isDisabled() ) {
+                                       $help['help-urls'] .= self::wrap(
+                                               $msg->numParams( count( $urls ) ), 'apihelp-block-head', 'div'
+                                       );
+                               }
+                               if ( !is_array( $urls ) ) {
+                                       $urls = array( $urls );
+                               }
+                               $help['help-urls'] .= Html::openElement( 'ul' );
+                               foreach ( $urls as $url ) {
+                                       $help['help-urls'] .= Html::rawElement( 'li', null,
+                                               Html::element( 'a', array( 'href' => $url ), $url )
+                                       );
+                               }
+                               $help['help-urls'] .= Html::closeElement( 'ul' );
+                               $help['help-urls'] .= Html::closeElement( 'div' );
+                       }
+
+                       $params = $module->getFinalParams( ApiBase::GET_VALUES_FOR_HELP );
+                       $groups = array();
+                       if ( $params ) {
+                               $help['parameters'] .= Html::openElement( 'div',
+                                       array( 'class' => 'apihelp-block apihelp-parameters' )
+                               );
+                               $msg = $context->msg( 'api-help-parameters' );
+                               if ( !$msg->isDisabled() ) {
+                                       $help['parameters'] .= self::wrap(
+                                               $msg->numParams( count( $params ) ), 'apihelp-block-head', 'div'
+                                       );
+                               }
+                               $help['parameters'] .= Html::openElement( 'dl' );
+
+                               $descriptions = $module->getFinalParamDescription();
+
+                               foreach ( $params as $name => $settings ) {
+                                       if ( !is_array( $settings ) ) {
+                                               $settings = array( ApiBase::PARAM_DFLT => $settings );
+                                       }
+
+                                       $help['parameters'] .= Html::element( 'dt', null,
+                                               $module->encodeParamName( $name ) );
 
-               $msg2 = $module->makeHelpMsg();
-               if ( $msg2 !== false ) {
-                       $msg .= $msg2;
+                                       // Add description
+                                       $description = array();
+                                       if ( isset( $descriptions[$name] ) ) {
+                                               foreach ( $descriptions[$name] as $msg ) {
+                                                       $msg->setContext( $context );
+                                                       $description[] = $msg->parseAsBlock();
+                                               }
+                                       }
+
+                                       // Add usage info
+                                       $info = array();
+
+                                       // Required?
+                                       if ( !empty( $settings[ApiBase::PARAM_REQUIRED] ) ) {
+                                               $info[] = $context->msg( 'api-help-param-required' )->parse();
+                                       }
+
+                                       // Custom info?
+                                       if ( !empty( $settings[ApiBase::PARAM_HELP_MSG_INFO] ) ) {
+                                               foreach ( $settings[ApiBase::PARAM_HELP_MSG_INFO] as $i ) {
+                                                       $tag = array_shift( $i );
+                                                       $info[] = $context->msg( "apihelp-{$path}-paraminfo-{$tag}" )
+                                                               ->numParams( count( $i ) )
+                                                               ->params( $context->getLanguage()->commaList( $i ) )
+                                                               ->params( $module->getModulePrefix() )
+                                                               ->parse();
+                                               }
+                                       }
+
+                                       // Type documentation
+                                       if ( !isset( $settings[ApiBase::PARAM_TYPE] ) ) {
+                                               $dflt = isset( $settings[ApiBase::PARAM_DFLT] )
+                                                       ? $settings[ApiBase::PARAM_DFLT]
+                                                       : null;
+                                               if ( is_bool( $dflt ) ) {
+                                                       $settings[ApiBase::PARAM_TYPE] = 'boolean';
+                                               } elseif ( is_string( $dflt ) || is_null( $dflt ) ) {
+                                                       $settings[ApiBase::PARAM_TYPE] = 'string';
+                                               } elseif ( is_int( $dflt ) ) {
+                                                       $settings[ApiBase::PARAM_TYPE] = 'integer';
+                                               }
+                                       }
+                                       if ( isset( $settings[ApiBase::PARAM_TYPE] ) ) {
+                                               $type = $settings[ApiBase::PARAM_TYPE];
+                                               $multi = !empty( $settings[ApiBase::PARAM_ISMULTI] );
+                                               $hintPipeSeparated = true;
+                                               $count = ApiBase::LIMIT_SML2 + 1;
+
+                                               if ( is_array( $type ) ) {
+                                                       $count = count( $type );
+                                                       $links = isset( $settings[ApiBase::PARAM_VALUE_LINKS] )
+                                                               ? $settings[ApiBase::PARAM_VALUE_LINKS]
+                                                               : array();
+                                                       $type = array_map( function ( $v ) use ( $links ) {
+                                                               $ret = wfEscapeWikiText( $v );
+                                                               if ( isset( $links[$v] ) ) {
+                                                                       $ret = "[[{$links[$v]}|$ret]]";
+                                                               }
+                                                               return $ret;
+                                                       }, $type );
+                                                       $i = array_search( '', $type, true );
+                                                       if ( $i === false ) {
+                                                               $type = $context->getLanguage()->commaList( $type );
+                                                       } else {
+                                                               unset( $type[$i] );
+                                                               $type = $context->msg( 'api-help-param-list-can-be-empty' )
+                                                                       ->numParams( count( $type ) )
+                                                                       ->params( $context->getLanguage()->commaList( $type ) )
+                                                                       ->parse();
+                                                       }
+                                                       $info[] = $context->msg( 'api-help-param-list' )
+                                                               ->params( $multi ? 2 : 1 )
+                                                               ->params( $type )
+                                                               ->parse();
+                                                       $hintPipeSeparated = false;
+                                               } else {
+                                                       switch ( $type ) {
+                                                               case 'submodule':
+                                                                       $groups[] = $name;
+                                                                       $submodules = $module->getModuleManager()->getNames( $name );
+                                                                       $count = count( $submodules );
+                                                                       sort( $submodules );
+                                                                       $prefix = $module->isMain()
+                                                                               ? '' : ( $module->getModulePath() . '+' );
+                                                                       $submodules = array_map( function ( $name ) use ( $prefix ) {
+                                                                               return "[[Special:ApiHelp/{$prefix}{$name}|{$name}]]";
+                                                                       }, $submodules );
+                                                                       $info[] = $context->msg( 'api-help-param-list' )
+                                                                               ->params( $multi ? 2 : 1 )
+                                                                               ->params( $context->getLanguage()->commaList( $submodules ) )
+                                                                               ->parse();
+                                                                       $hintPipeSeparated = false;
+                                                                       break;
+
+                                                               case 'namespace':
+                                                                       $namespaces = MWNamespace::getValidNamespaces();
+                                                                       $count = count( $namespaces );
+                                                                       $info[] = $context->msg( 'api-help-param-list' )
+                                                                               ->params( $multi ? 2 : 1 )
+                                                                               ->params( $context->getLanguage()->commaList( $namespaces ) )
+                                                                               ->parse();
+                                                                       $hintPipeSeparated = false;
+                                                                       break;
+
+                                                               case 'limit':
+                                                                       if ( isset( $settings[ApiBase::PARAM_MAX2] ) ) {
+                                                                               $info[] = $context->msg( 'api-help-param-limit2' )
+                                                                                       ->numParams( $settings[ApiBase::PARAM_MAX] )
+                                                                                       ->numParams( $settings[ApiBase::PARAM_MAX2] )
+                                                                                       ->parse();
+                                                                       } else {
+                                                                               $info[] = $context->msg( 'api-help-param-limit' )
+                                                                                       ->numParams( $settings[ApiBase::PARAM_MAX] )
+                                                                                       ->parse();
+                                                                       }
+                                                                       break;
+
+                                                               case 'integer':
+                                                                       // Possible messages:
+                                                                       // api-help-param-integer-min,
+                                                                       // api-help-param-integer-max,
+                                                                       // api-help-param-integer-minmax
+                                                                       $suffix = '';
+                                                                       $min = $max = 0;
+                                                                       if ( isset( $settings[ApiBase::PARAM_MIN] ) ) {
+                                                                               $suffix .= 'min';
+                                                                               $min = $settings[ApiBase::PARAM_MIN];
+                                                                       }
+                                                                       if ( isset( $settings[ApiBase::PARAM_MAX] ) ) {
+                                                                               $suffix .= 'max';
+                                                                               $max = $settings[ApiBase::PARAM_MAX];
+                                                                       }
+                                                                       if ( $suffix !== '' ) {
+                                                                               $info[] =
+                                                                                       $context->msg( "api-help-param-integer-$suffix" )
+                                                                                               ->params( $multi ? 2 : 1 )
+                                                                                               ->numParams( $min, $max )
+                                                                                               ->parse();
+                                                                       }
+                                                                       break;
+
+                                                               case 'upload':
+                                                                       $info[] = $context->msg( 'api-help-param-upload' )
+                                                                               ->parse();
+                                                                       break;
+                                                       }
+                                               }
+
+                                               if ( $multi ) {
+                                                       $extra = array();
+                                                       if ( $hintPipeSeparated ) {
+                                                               $extra[] = $context->msg( 'api-help-param-multi-separate' )->parse();
+                                                       }
+                                                       if ( $count > ApiBase::LIMIT_SML1 ) {
+                                                               $extra[] = $context->msg( 'api-help-param-multi-max' )
+                                                                       ->numParams( ApiBase::LIMIT_SML1, ApiBase::LIMIT_SML2 )
+                                                                       ->parse();
+                                                       }
+                                                       if ( $extra ) {
+                                                               $info[] = join( ' ', $extra );
+                                                       }
+                                               }
+                                       }
+
+                                       // Add default
+                                       $default = isset( $settings[ApiBase::PARAM_DFLT] )
+                                               ? $settings[ApiBase::PARAM_DFLT]
+                                               : null;
+                                       if ( $default === '' ) {
+                                               $info[] = $context->msg( 'api-help-param-default-empty' )
+                                                       ->parse();
+                                       } elseif ( $default !== null && $default !== false ) {
+                                               $info[] = $context->msg( 'api-help-param-default' )
+                                                       ->params( wfEscapeWikiText( $default ) )
+                                                       ->parse();
+                                       }
+
+                                       if ( !array_filter( $description ) ) {
+                                               $description = array( self::wrap(
+                                                       $context->msg( 'api-help-param-no-description' ),
+                                                       'apihelp-empty'
+                                               ) );
+                                       }
+
+                                       // Add "deprecated" flag
+                                       if ( !empty( $settings[ApiBase::PARAM_DEPRECATED] ) ) {
+                                               $help['parameters'] .= Html::openElement( 'dd',
+                                                       array( 'class' => 'info' ) );
+                                               $help['parameters'] .= self::wrap(
+                                                       $context->msg( 'api-help-param-deprecated' ),
+                                                       'apihelp-deprecated', 'strong'
+                                               );
+                                               $help['parameters'] .= Html::closeElement( 'dd' );
+                                       }
+
+                                       if ( $description ) {
+                                               $help['parameters'] .= Html::openElement( 'dd',
+                                                       array( 'class' => 'description' ) );
+                                               $help['parameters'] .= join( '', $description );
+                                               $help['parameters'] .= Html::closeElement( 'dd' );
+                                       }
+
+                                       foreach ( $info as $i ) {
+                                               $help['parameters'] .= Html::rawElement( 'dd', array( 'class' => 'info' ), $i );
+                                       }
+                               }
+
+                               $help['parameters'] .= Html::closeElement( 'dl' );
+                               $help['parameters'] .= Html::closeElement( 'div' );
+                       }
+
+                       $examples = $module->getExamplesMessages();
+                       if ( $examples ) {
+                               $help['examples'] .= Html::openElement( 'div',
+                                       array( 'class' => 'apihelp-block apihelp-examples' ) );
+                               $msg = $context->msg( 'api-help-examples' );
+                               if ( !$msg->isDisabled() ) {
+                                       $help['examples'] .= self::wrap(
+                                               $msg->numParams( count( $examples ) ), 'apihelp-block-head', 'div'
+                                       );
+                               }
+
+                               $help['examples'] .= Html::openElement( 'dl' );
+                               foreach ( $examples as $qs => $msg ) {
+                                       $msg = ApiBase::makeMessage( $msg, $context, array(
+                                               $module->getModulePrefix(),
+                                               $module->getModuleName(),
+                                               $module->getModulePath()
+                                       ) );
+
+                                       $link = wfAppendQuery( wfScript( 'api' ), $qs );
+                                       $help['examples'] .= Html::rawElement( 'dt', null, $msg->parse() );
+                                       $help['examples'] .= Html::rawElement( 'dd', null,
+                                               Html::element( 'a', array( 'href' => $link ), "api.php?$qs" )
+                                       );
+                               }
+                               $help['examples'] .= Html::closeElement( 'dl' );
+                               $help['examples'] .= Html::closeElement( 'div' );
+                       }
+
+                       if ( $options['submodules'] && $module->getModuleManager() ) {
+                               $manager = $module->getModuleManager();
+                               $submodules = array();
+                               foreach ( $groups as $group ) {
+                                       $names = $manager->getNames( $group );
+                                       sort( $names );
+                                       foreach ( $names as $name ) {
+                                               $submodules[] = $manager->getModule( $name );
+                                       }
+                               }
+                               $help['submodules'] .= self::getHelpInternal( $context, $submodules, array(
+                                       'submodules' => $options['recursivesubmodules'],
+                                       'headerlevel' => $level + 1,
+                                       'noheader' => false,
+                               ) + $options, $haveModules );
+                       }
+
+                       $module->modifyHelp( $help, $options );
+
+                       wfRunHooks( 'APIHelpModifyOutput', array( $module, &$help, $options ) );
+
+                       $out .= join( "\n", $help );
                }
 
-               return $msg;
+               return $out;
        }
 
        public function shouldCheckMaxlag() {
@@ -131,39 +627,40 @@ class ApiHelp extends ApiBase {
                return false;
        }
 
+       public function getCustomPrinter() {
+               $params = $this->extractRequestParams();
+               if ( $params['wrap'] ) {
+                       return null;
+               }
+
+               $main = $this->getMain();
+               $errorPrinter = $main->createPrinterByName( $main->getParameter( 'format' ) );
+               return new ApiFormatRaw( $main, $errorPrinter );
+       }
+
        public function getAllowedParams() {
                return array(
                        'modules' => array(
-                               ApiBase::PARAM_ISMULTI => true
-                       ),
-                       'querymodules' => array(
+                               ApiBase::PARAM_DFLT => 'main',
                                ApiBase::PARAM_ISMULTI => true,
-                               ApiBase::PARAM_DEPRECATED => true
                        ),
+                       'submodules' => false,
+                       'recursivesubmodules' => false,
+                       'wrap' => false,
+                       'toc' => false,
                );
        }
 
-       public function getParamDescription() {
-               return array(
-                       'modules' => 'List of module names (value of the action= parameter). ' .
-                               'Can specify submodules with a \'+\'',
-                       'querymodules' => 'Use modules=query+value instead. List of query ' .
-                               'module names (value of prop=, meta= or list= parameter)',
-               );
-       }
-
-       public function getDescription() {
-               return 'Display this help screen. Or the help screen for the specified module.';
-       }
-
-       public function getExamples() {
+       protected function getExamplesMessages() {
                return array(
-                       'api.php?action=help' => 'Whole help page',
-                       'api.php?action=help&modules=protect' => 'Module (action) help page',
-                       'api.php?action=help&modules=query+categorymembers'
-                               => 'Help for the query/categorymembers module',
-                       'api.php?action=help&modules=login|query+info'
-                               => 'Help for the login and query/info modules',
+                       'action=help'
+                               => 'apihelp-help-example-main',
+                       'action=help&recursivesubmodules=1'
+                               => 'apihelp-help-example-recursive',
+                       'action=help&modules=help'
+                               => 'apihelp-help-example-help',
+                       'action=help&modules=query+info|query+categorymembers'
+                               => 'apihelp-help-example-query',
                );
        }