3 * Copyright © 2014 Wikimedia Foundation and contributors
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 use HtmlFormatter\HtmlFormatter
;
24 use MediaWiki\MediaWikiServices
;
27 * Class to output help for an API module
29 * @since 1.25 completely rewritten
32 class ApiHelp
extends ApiBase
{
33 public function execute() {
34 $params = $this->extractRequestParams();
37 foreach ( $params['modules'] as $path ) {
38 $modules[] = $this->getModuleFromPath( $path );
42 $context = new DerivativeContext( $this->getMain()->getContext() );
43 $context->setSkin( SkinFactory
::getDefaultInstance()->makeSkin( 'apioutput' ) );
44 $context->setLanguage( $this->getMain()->getLanguage() );
45 $context->setTitle( SpecialPage
::getTitleFor( 'ApiHelp' ) );
46 $out = new OutputPage( $context );
47 $out->setCopyrightUrl( 'https://www.mediawiki.org/wiki/Special:MyLanguage/Copyright' );
48 $context->setOutput( $out );
50 self
::getHelp( $context, $modules, $params );
52 // Grab the output from the skin
54 $context->getOutput()->output();
55 $html = ob_get_clean();
57 $result = $this->getResult();
58 if ( $params['wrap'] ) {
60 'mime' => 'text/html',
61 'filename' => 'api-help.html',
64 ApiResult
::setSubelementsList( $data, 'help' );
65 $result->addValue( null, $this->getModuleName(), $data );
68 $result->addValue( null, 'text', $html, ApiResult
::NO_SIZE_CHECK
);
69 $result->addValue( null, 'mime', 'text/html', ApiResult
::NO_SIZE_CHECK
);
70 $result->addValue( null, 'filename', 'api-help.html', ApiResult
::NO_SIZE_CHECK
);
75 * Generate help for the specified modules
77 * Help is placed into the OutputPage object returned by
78 * $context->getOutput().
80 * Recognized options include:
81 * - headerlevel: (int) Header tag level
82 * - nolead: (bool) Skip the inclusion of api-help-lead
83 * - noheader: (bool) Skip the inclusion of the top-level section headers
84 * - submodules: (bool) Include help for submodules of the current module
85 * - recursivesubmodules: (bool) Include help for submodules recursively
86 * - helptitle: (string) Title to link for additional modules' help. Should contain $1.
87 * - toc: (bool) Include a table of contents
89 * @param IContextSource $context
90 * @param ApiBase[]|ApiBase $modules
91 * @param array $options Formatting options (described above)
93 public static function getHelp( IContextSource
$context, $modules, array $options ) {
96 if ( !is_array( $modules ) ) {
97 $modules = [ $modules ];
100 $out = $context->getOutput();
101 $out->addModuleStyles( [
105 if ( !empty( $options['toc'] ) ) {
106 $out->addModules( 'mediawiki.toc' );
108 $out->setPageTitle( $context->msg( 'api-help-title' ) );
110 $cache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
112 if ( count( $modules ) == 1 && $modules[0] instanceof ApiMain
&&
113 $options['recursivesubmodules'] && $context->getLanguage() === $wgContLang
115 $cacheHelpTimeout = $context->getConfig()->get( 'APICacheHelpTimeout' );
116 if ( $cacheHelpTimeout > 0 ) {
117 // Get help text from cache if present
118 $cacheKey = $cache->makeKey( 'apihelp', $modules[0]->getModulePath(),
119 (int)!empty( $options['toc'] ),
120 str_replace( ' ', '_', SpecialVersion
::getVersion( 'nodb' ) ) );
121 $cached = $cache->get( $cacheKey );
123 $out->addHTML( $cached );
128 if ( $out->getHTML() !== '' ) {
129 // Don't save to cache, there's someone else's content in the page
134 $options['recursivesubmodules'] = !empty( $options['recursivesubmodules'] );
135 $options['submodules'] = $options['recursivesubmodules'] ||
!empty( $options['submodules'] );
138 if ( empty( $options['nolead'] ) ) {
139 $msg = $context->msg( 'api-help-lead' );
140 if ( !$msg->isDisabled() ) {
141 $out->addHTML( $msg->parseAsBlock() );
146 $html = self
::getHelpInternal( $context, $modules, $options, $haveModules );
147 if ( !empty( $options['toc'] ) && $haveModules ) {
148 $out->addHTML( Linker
::generateTOC( $haveModules, $context->getLanguage() ) );
150 $out->addHTML( $html );
152 $helptitle = isset( $options['helptitle'] ) ?
$options['helptitle'] : null;
153 $html = self
::fixHelpLinks( $out->getHTML(), $helptitle, $haveModules );
155 $out->addHTML( $html );
157 if ( $cacheKey !== null ) {
158 $cache->set( $cacheKey, $out->getHTML(), $cacheHelpTimeout );
163 * Replace Special:ApiHelp links with links to api.php
165 * @param string $html
166 * @param string|null $helptitle Title to link to rather than api.php, must contain '$1'
167 * @param array $localModules Keys are modules to link within the current page, values are ignored
170 public static function fixHelpLinks( $html, $helptitle = null, $localModules = [] ) {
171 $formatter = new HtmlFormatter( $html );
172 $doc = $formatter->getDoc();
173 $xpath = new DOMXPath( $doc );
174 $nodes = $xpath->query( '//a[@href][not(contains(@class,\'apihelp-linktrail\'))]' );
175 foreach ( $nodes as $node ) {
176 $href = $node->getAttribute( 'href' );
179 $href = rawurldecode( $href );
180 } while ( $old !== $href );
181 if ( preg_match( '!Special:ApiHelp/([^&/|#]+)((?:#.*)?)!', $href, $m ) ) {
182 if ( isset( $localModules[$m[1]] ) ) {
183 $href = $m[2] === '' ?
'#' . $m[1] : $m[2];
184 } elseif ( $helptitle !== null ) {
185 $href = Title
::newFromText( str_replace( '$1', $m[1], $helptitle ) . $m[2] )
188 $href = wfAppendQuery( wfScript( 'api' ), [
193 $node->setAttribute( 'href', $href );
194 $node->removeAttribute( 'title' );
198 return $formatter->getText();
202 * Wrap a message in HTML with a class.
204 * @param Message $msg
205 * @param string $class
209 private static function wrap( Message
$msg, $class, $tag = 'span' ) {
210 return Html
::rawElement( $tag, [ 'class' => $class ],
216 * Recursively-called function to actually construct the help
218 * @param IContextSource $context
219 * @param ApiBase[] $modules
220 * @param array $options
221 * @param array &$haveModules
224 private static function getHelpInternal( IContextSource
$context, array $modules,
225 array $options, &$haveModules
229 $level = empty( $options['headerlevel'] ) ?
2 : $options['headerlevel'];
230 if ( empty( $options['tocnumber'] ) ) {
231 $tocnumber = [ 2 => 0 ];
233 $tocnumber = &$options['tocnumber'];
236 foreach ( $modules as $module ) {
237 $tocnumber[$level]++
;
238 $path = $module->getModulePath();
239 $module->setContext( $context );
250 if ( empty( $options['noheader'] ) ||
!empty( $options['toc'] ) ) {
253 while ( isset( $haveModules[$anchor] ) ) {
254 $anchor = $path . '|' . ++
$i;
257 if ( $module->isMain() ) {
258 $headerContent = $context->msg( 'api-help-main-header' )->parse();
260 'class' => 'apihelp-header',
263 $name = $module->getModuleName();
264 $headerContent = $module->getParent()->getModuleManager()->getModuleGroup( $name ) .
266 if ( $module->getModulePrefix() !== '' ) {
267 $headerContent .= ' ' .
268 $context->msg( 'parentheses', $module->getModulePrefix() )->parse();
270 // Module names are always in English and not localized,
271 // so English language and direction must be set explicitly,
272 // otherwise parentheses will get broken in RTL wikis
274 'class' => 'apihelp-header apihelp-module-name',
280 $headerAttr['id'] = $anchor;
282 $haveModules[$anchor] = [
283 'toclevel' => count( $tocnumber ),
286 'line' => $headerContent,
287 'number' => implode( '.', $tocnumber ),
290 if ( empty( $options['noheader'] ) ) {
291 $help['header'] .= Html
::element(
292 'h' . min( 6, $level ),
298 $haveModules[$path] = true;
303 for ( $m = $module; $m !== null; $m = $m->getParent() ) {
304 $name = $m->getModuleName();
305 if ( $name === 'main_int' ) {
309 if ( count( $modules ) === 1 && $m === $modules[0] &&
310 !( !empty( $options['submodules'] ) && $m->getModuleManager() )
312 $link = Html
::element( 'b', [ 'dir' => 'ltr', 'lang' => 'en' ], $name );
314 $link = SpecialPage
::getTitleFor( 'ApiHelp', $m->getModulePath() )->getLocalURL();
315 $link = Html
::element( 'a',
316 [ 'href' => $link, 'class' => 'apihelp-linktrail', 'dir' => 'ltr', 'lang' => 'en' ],
321 array_unshift( $links, $link );
324 $help['header'] .= self
::wrap(
325 $context->msg( 'parentheses' )
326 ->rawParams( $context->getLanguage()->pipeList( $links ) ),
327 'apihelp-linktrail', 'div'
331 $flags = $module->getHelpFlags();
332 $help['flags'] .= Html
::openElement( 'div',
333 [ 'class' => 'apihelp-block apihelp-flags' ] );
334 $msg = $context->msg( 'api-help-flags' );
335 if ( !$msg->isDisabled() ) {
336 $help['flags'] .= self
::wrap(
337 $msg->numParams( count( $flags ) ), 'apihelp-block-head', 'div'
340 $help['flags'] .= Html
::openElement( 'ul' );
341 foreach ( $flags as $flag ) {
342 $help['flags'] .= Html
::rawElement( 'li', null,
343 self
::wrap( $context->msg( "api-help-flag-$flag" ), "apihelp-flag-$flag" )
346 $sourceInfo = $module->getModuleSourceInfo();
348 if ( isset( $sourceInfo['namemsg'] ) ) {
349 $extname = $context->msg( $sourceInfo['namemsg'] )->text();
351 // Probably English, so wrap it.
352 $extname = Html
::element( 'span', [ 'dir' => 'ltr', 'lang' => 'en' ], $sourceInfo['name'] );
354 $help['flags'] .= Html
::rawElement( 'li', null,
356 $context->msg( 'api-help-source', $extname, $sourceInfo['name'] ),
361 $link = SpecialPage
::getTitleFor( 'Version', 'License/' . $sourceInfo['name'] );
362 if ( isset( $sourceInfo['license-name'] ) ) {
363 $msg = $context->msg( 'api-help-license', $link,
364 Html
::element( 'span', [ 'dir' => 'ltr', 'lang' => 'en' ], $sourceInfo['license-name'] )
366 } elseif ( SpecialVersion
::getExtLicenseFileName( dirname( $sourceInfo['path'] ) ) ) {
367 $msg = $context->msg( 'api-help-license-noname', $link );
369 $msg = $context->msg( 'api-help-license-unknown' );
371 $help['flags'] .= Html
::rawElement( 'li', null,
372 self
::wrap( $msg, 'apihelp-license' )
375 $help['flags'] .= Html
::rawElement( 'li', null,
376 self
::wrap( $context->msg( 'api-help-source-unknown' ), 'apihelp-source' )
378 $help['flags'] .= Html
::rawElement( 'li', null,
379 self
::wrap( $context->msg( 'api-help-license-unknown' ), 'apihelp-license' )
382 $help['flags'] .= Html
::closeElement( 'ul' );
383 $help['flags'] .= Html
::closeElement( 'div' );
385 foreach ( $module->getFinalDescription() as $msg ) {
386 $msg->setContext( $context );
387 $help['description'] .= $msg->parseAsBlock();
390 $urls = $module->getHelpUrls();
392 $help['help-urls'] .= Html
::openElement( 'div',
393 [ 'class' => 'apihelp-block apihelp-help-urls' ]
395 $msg = $context->msg( 'api-help-help-urls' );
396 if ( !$msg->isDisabled() ) {
397 $help['help-urls'] .= self
::wrap(
398 $msg->numParams( count( $urls ) ), 'apihelp-block-head', 'div'
401 if ( !is_array( $urls ) ) {
404 $help['help-urls'] .= Html
::openElement( 'ul' );
405 foreach ( $urls as $url ) {
406 $help['help-urls'] .= Html
::rawElement( 'li', null,
407 Html
::element( 'a', [ 'href' => $url, 'dir' => 'ltr' ], $url )
410 $help['help-urls'] .= Html
::closeElement( 'ul' );
411 $help['help-urls'] .= Html
::closeElement( 'div' );
414 $params = $module->getFinalParams( ApiBase
::GET_VALUES_FOR_HELP
);
415 $dynamicParams = $module->dynamicParameterDocumentation();
417 if ( $params ||
$dynamicParams !== null ) {
418 $help['parameters'] .= Html
::openElement( 'div',
419 [ 'class' => 'apihelp-block apihelp-parameters' ]
421 $msg = $context->msg( 'api-help-parameters' );
422 if ( !$msg->isDisabled() ) {
423 $help['parameters'] .= self
::wrap(
424 $msg->numParams( count( $params ) ), 'apihelp-block-head', 'div'
427 $help['parameters'] .= Html
::openElement( 'dl' );
429 $descriptions = $module->getFinalParamDescription();
431 foreach ( $params as $name => $settings ) {
432 if ( !is_array( $settings ) ) {
433 $settings = [ ApiBase
::PARAM_DFLT
=> $settings ];
436 $help['parameters'] .= Html
::rawElement( 'dt', null,
437 Html
::element( 'span', [ 'dir' => 'ltr', 'lang' => 'en' ], $module->encodeParamName( $name ) )
442 if ( isset( $descriptions[$name] ) ) {
443 foreach ( $descriptions[$name] as $msg ) {
444 $msg->setContext( $context );
445 $description[] = $msg->parseAsBlock();
453 if ( !empty( $settings[ApiBase
::PARAM_REQUIRED
] ) ) {
454 $info[] = $context->msg( 'api-help-param-required' )->parse();
458 if ( !empty( $settings[ApiBase
::PARAM_HELP_MSG_INFO
] ) ) {
459 foreach ( $settings[ApiBase
::PARAM_HELP_MSG_INFO
] as $i ) {
460 $tag = array_shift( $i );
461 $info[] = $context->msg( "apihelp-{$path}-paraminfo-{$tag}" )
462 ->numParams( count( $i ) )
463 ->params( $context->getLanguage()->commaList( $i ) )
464 ->params( $module->getModulePrefix() )
470 if ( !empty( $settings[ApiBase
::PARAM_TEMPLATE_VARS
] ) ) {
472 $msg = 'api-help-param-templated-var-first';
473 foreach ( $settings[ApiBase
::PARAM_TEMPLATE_VARS
] as $k => $v ) {
474 $vars[] = $context->msg( $msg, $k, $module->encodeParamName( $v ) );
475 $msg = 'api-help-param-templated-var';
477 $info[] = $context->msg( 'api-help-param-templated' )
478 ->numParams( count( $vars ) )
479 ->params( Message
::listParam( $vars ) )
483 // Type documentation
484 if ( !isset( $settings[ApiBase
::PARAM_TYPE
] ) ) {
485 $dflt = isset( $settings[ApiBase
::PARAM_DFLT
] )
486 ?
$settings[ApiBase
::PARAM_DFLT
]
488 if ( is_bool( $dflt ) ) {
489 $settings[ApiBase
::PARAM_TYPE
] = 'boolean';
490 } elseif ( is_string( $dflt ) ||
is_null( $dflt ) ) {
491 $settings[ApiBase
::PARAM_TYPE
] = 'string';
492 } elseif ( is_int( $dflt ) ) {
493 $settings[ApiBase
::PARAM_TYPE
] = 'integer';
496 if ( isset( $settings[ApiBase
::PARAM_TYPE
] ) ) {
497 $type = $settings[ApiBase
::PARAM_TYPE
];
498 $multi = !empty( $settings[ApiBase
::PARAM_ISMULTI
] );
499 $hintPipeSeparated = true;
500 $count = !empty( $settings[ApiBase
::PARAM_ISMULTI_LIMIT2
] )
501 ?
$settings[ApiBase
::PARAM_ISMULTI_LIMIT2
] +
1
502 : ApiBase
::LIMIT_SML2 +
1;
504 if ( is_array( $type ) ) {
505 $count = count( $type );
506 $deprecatedValues = isset( $settings[ApiBase
::PARAM_DEPRECATED_VALUES
] )
507 ?
$settings[ApiBase
::PARAM_DEPRECATED_VALUES
]
509 $links = isset( $settings[ApiBase
::PARAM_VALUE_LINKS
] )
510 ?
$settings[ApiBase
::PARAM_VALUE_LINKS
]
512 $values = array_map( function ( $v ) use ( $links, $deprecatedValues ) {
515 // We can't know whether this contains LTR or RTL text.
516 $attr['dir'] = 'auto';
518 if ( isset( $deprecatedValues[$v] ) ) {
519 $attr['class'] = 'apihelp-deprecated-value';
521 $ret = $attr ? Html
::element( 'span', $attr, $v ) : $v;
522 if ( isset( $links[$v] ) ) {
523 $ret = "[[{$links[$v]}|$ret]]";
527 $i = array_search( '', $type, true );
528 if ( $i === false ) {
529 $values = $context->getLanguage()->commaList( $values );
531 unset( $values[$i] );
532 $values = $context->msg( 'api-help-param-list-can-be-empty' )
533 ->numParams( count( $values ) )
534 ->params( $context->getLanguage()->commaList( $values ) )
537 $info[] = $context->msg( 'api-help-param-list' )
538 ->params( $multi ?
2 : 1 )
541 $hintPipeSeparated = false;
547 if ( isset( $settings[ApiBase
::PARAM_SUBMODULE_MAP
] ) ) {
548 $map = $settings[ApiBase
::PARAM_SUBMODULE_MAP
];
551 $prefix = $module->isMain() ?
'' : ( $module->getModulePath() . '+' );
553 foreach ( $module->getModuleManager()->getNames( $name ) as $submoduleName ) {
554 $map[$submoduleName] = $prefix . $submoduleName;
556 $defaultAttrs = [ 'dir' => 'ltr', 'lang' => 'en' ];
561 $deprecatedSubmodules = [];
562 foreach ( $map as $v => $m ) {
563 $attrs = $defaultAttrs;
566 $submod = $module->getModuleFromPath( $m );
568 if ( $submod->isDeprecated() ) {
569 $arr = &$deprecatedSubmodules;
570 $attrs['class'] = 'apihelp-deprecated-value';
573 } catch ( ApiUsageException
$ex ) {
577 $v = Html
::element( 'span', $attrs, $v );
579 $arr[] = "[[Special:ApiHelp/{$m}|{$v}]]";
581 $submodules = array_merge( $submodules, $deprecatedSubmodules );
582 $count = count( $submodules );
583 $info[] = $context->msg( 'api-help-param-list' )
584 ->params( $multi ?
2 : 1 )
585 ->params( $context->getLanguage()->commaList( $submodules ) )
587 $hintPipeSeparated = false;
588 // No type message necessary, we have a list of values.
593 $namespaces = MWNamespace
::getValidNamespaces();
594 if ( isset( $settings[ApiBase
::PARAM_EXTRA_NAMESPACES
] ) &&
595 is_array( $settings[ApiBase
::PARAM_EXTRA_NAMESPACES
] )
597 $namespaces = array_merge( $namespaces, $settings[ApiBase
::PARAM_EXTRA_NAMESPACES
] );
600 $count = count( $namespaces );
601 $info[] = $context->msg( 'api-help-param-list' )
602 ->params( $multi ?
2 : 1 )
603 ->params( $context->getLanguage()->commaList( $namespaces ) )
605 $hintPipeSeparated = false;
606 // No type message necessary, we have a list of values.
611 $tags = ChangeTags
::listExplicitlyDefinedTags();
612 $count = count( $tags );
613 $info[] = $context->msg( 'api-help-param-list' )
614 ->params( $multi ?
2 : 1 )
615 ->params( $context->getLanguage()->commaList( $tags ) )
617 $hintPipeSeparated = false;
622 if ( isset( $settings[ApiBase
::PARAM_MAX2
] ) ) {
623 $info[] = $context->msg( 'api-help-param-limit2' )
624 ->numParams( $settings[ApiBase
::PARAM_MAX
] )
625 ->numParams( $settings[ApiBase
::PARAM_MAX2
] )
628 $info[] = $context->msg( 'api-help-param-limit' )
629 ->numParams( $settings[ApiBase
::PARAM_MAX
] )
635 // Possible messages:
636 // api-help-param-integer-min,
637 // api-help-param-integer-max,
638 // api-help-param-integer-minmax
641 if ( isset( $settings[ApiBase
::PARAM_MIN
] ) ) {
643 $min = $settings[ApiBase
::PARAM_MIN
];
645 if ( isset( $settings[ApiBase
::PARAM_MAX
] ) ) {
647 $max = $settings[ApiBase
::PARAM_MAX
];
649 if ( $suffix !== '' ) {
651 $context->msg( "api-help-param-integer-$suffix" )
652 ->params( $multi ?
2 : 1 )
653 ->numParams( $min, $max )
659 $info[] = $context->msg( 'api-help-param-upload' )
661 // No type message necessary, api-help-param-upload should handle it.
667 // Displaying a type message here would be useless.
673 // Add type. Messages for grep: api-help-param-type-limit
674 // api-help-param-type-integer api-help-param-type-boolean
675 // api-help-param-type-timestamp api-help-param-type-user
676 // api-help-param-type-password
677 if ( is_string( $type ) ) {
678 $msg = $context->msg( "api-help-param-type-$type" );
679 if ( !$msg->isDisabled() ) {
680 $info[] = $msg->params( $multi ?
2 : 1 )->parse();
686 $lowcount = !empty( $settings[ApiBase
::PARAM_ISMULTI_LIMIT1
] )
687 ?
$settings[ApiBase
::PARAM_ISMULTI_LIMIT1
]
688 : ApiBase
::LIMIT_SML1
;
689 $highcount = !empty( $settings[ApiBase
::PARAM_ISMULTI_LIMIT2
] )
690 ?
$settings[ApiBase
::PARAM_ISMULTI_LIMIT2
]
691 : ApiBase
::LIMIT_SML2
;
693 if ( $hintPipeSeparated ) {
694 $extra[] = $context->msg( 'api-help-param-multi-separate' )->parse();
696 if ( $count > $lowcount ) {
697 if ( $lowcount === $highcount ) {
698 $msg = $context->msg( 'api-help-param-multi-max-simple' )
699 ->numParams( $lowcount );
701 $msg = $context->msg( 'api-help-param-multi-max' )
702 ->numParams( $lowcount, $highcount );
704 $extra[] = $msg->parse();
707 $info[] = implode( ' ', $extra );
710 $allowAll = isset( $settings[ApiBase
::PARAM_ALL
] )
711 ?
$settings[ApiBase
::PARAM_ALL
]
713 if ( $allowAll ||
$settings[ApiBase
::PARAM_TYPE
] === 'namespace' ) {
714 if ( $settings[ApiBase
::PARAM_TYPE
] === 'namespace' ) {
715 $allSpecifier = ApiBase
::ALL_DEFAULT_STRING
;
717 $allSpecifier = ( is_string( $allowAll ) ?
$allowAll : ApiBase
::ALL_DEFAULT_STRING
);
719 $info[] = $context->msg( 'api-help-param-multi-all' )
720 ->params( $allSpecifier )
726 if ( isset( $settings[self
::PARAM_MAX_BYTES
] ) ) {
727 $info[] = $context->msg( 'api-help-param-maxbytes' )
728 ->numParams( $settings[self
::PARAM_MAX_BYTES
] );
730 if ( isset( $settings[self
::PARAM_MAX_CHARS
] ) ) {
731 $info[] = $context->msg( 'api-help-param-maxchars' )
732 ->numParams( $settings[self
::PARAM_MAX_CHARS
] );
736 $default = isset( $settings[ApiBase
::PARAM_DFLT
] )
737 ?
$settings[ApiBase
::PARAM_DFLT
]
739 if ( $default === '' ) {
740 $info[] = $context->msg( 'api-help-param-default-empty' )
742 } elseif ( $default !== null && $default !== false ) {
743 // We can't know whether this contains LTR or RTL text.
744 $info[] = $context->msg( 'api-help-param-default' )
745 ->params( Html
::element( 'span', [ 'dir' => 'auto' ], $default ) )
749 if ( !array_filter( $description ) ) {
750 $description = [ self
::wrap(
751 $context->msg( 'api-help-param-no-description' ),
756 // Add "deprecated" flag
757 if ( !empty( $settings[ApiBase
::PARAM_DEPRECATED
] ) ) {
758 $help['parameters'] .= Html
::openElement( 'dd',
759 [ 'class' => 'info' ] );
760 $help['parameters'] .= self
::wrap(
761 $context->msg( 'api-help-param-deprecated' ),
762 'apihelp-deprecated', 'strong'
764 $help['parameters'] .= Html
::closeElement( 'dd' );
767 if ( $description ) {
768 $description = implode( '', $description );
769 $description = preg_replace( '!\s*</([oud]l)>\s*<\1>\s*!', "\n", $description );
770 $help['parameters'] .= Html
::rawElement( 'dd',
771 [ 'class' => 'description' ], $description );
774 foreach ( $info as $i ) {
775 $help['parameters'] .= Html
::rawElement( 'dd', [ 'class' => 'info' ], $i );
779 if ( $dynamicParams !== null ) {
780 $dynamicParams = ApiBase
::makeMessage( $dynamicParams, $context, [
781 $module->getModulePrefix(),
782 $module->getModuleName(),
783 $module->getModulePath()
785 $help['parameters'] .= Html
::element( 'dt', null, '*' );
786 $help['parameters'] .= Html
::rawElement( 'dd',
787 [ 'class' => 'description' ], $dynamicParams->parse() );
790 $help['parameters'] .= Html
::closeElement( 'dl' );
791 $help['parameters'] .= Html
::closeElement( 'div' );
794 $examples = $module->getExamplesMessages();
796 $help['examples'] .= Html
::openElement( 'div',
797 [ 'class' => 'apihelp-block apihelp-examples' ] );
798 $msg = $context->msg( 'api-help-examples' );
799 if ( !$msg->isDisabled() ) {
800 $help['examples'] .= self
::wrap(
801 $msg->numParams( count( $examples ) ), 'apihelp-block-head', 'div'
805 $help['examples'] .= Html
::openElement( 'dl' );
806 foreach ( $examples as $qs => $msg ) {
807 $msg = ApiBase
::makeMessage( $msg, $context, [
808 $module->getModulePrefix(),
809 $module->getModuleName(),
810 $module->getModulePath()
813 $link = wfAppendQuery( wfScript( 'api' ), $qs );
814 $sandbox = SpecialPage
::getTitleFor( 'ApiSandbox' )->getLocalURL() . '#' . $qs;
815 $help['examples'] .= Html
::rawElement( 'dt', null, $msg->parse() );
816 $help['examples'] .= Html
::rawElement( 'dd', null,
817 Html
::element( 'a', [ 'href' => $link, 'dir' => 'ltr' ], "api.php?$qs" ) . ' ' .
818 Html
::rawElement( 'a', [ 'href' => $sandbox ],
819 $context->msg( 'api-help-open-in-apisandbox' )->parse() )
822 $help['examples'] .= Html
::closeElement( 'dl' );
823 $help['examples'] .= Html
::closeElement( 'div' );
826 $subtocnumber = $tocnumber;
827 $subtocnumber[$level +
1] = 0;
829 'submodules' => $options['recursivesubmodules'],
830 'headerlevel' => $level +
1,
831 'tocnumber' => &$subtocnumber,
835 if ( $options['submodules'] && $module->getModuleManager() ) {
836 $manager = $module->getModuleManager();
838 foreach ( $groups as $group ) {
839 $names = $manager->getNames( $group );
841 foreach ( $names as $name ) {
842 $submodules[] = $manager->getModule( $name );
845 $help['submodules'] .= self
::getHelpInternal(
853 $module->modifyHelp( $help, $suboptions, $haveModules );
855 Hooks
::run( 'APIHelpModifyOutput', [ $module, &$help, $suboptions, &$haveModules ] );
857 $out .= implode( "\n", $help );
863 public function shouldCheckMaxlag() {
867 public function isReadMode() {
871 public function getCustomPrinter() {
872 $params = $this->extractRequestParams();
873 if ( $params['wrap'] ) {
877 $main = $this->getMain();
878 $errorPrinter = $main->createPrinterByName( $main->getParameter( 'format' ) );
879 return new ApiFormatRaw( $main, $errorPrinter );
882 public function getAllowedParams() {
885 ApiBase
::PARAM_DFLT
=> 'main',
886 ApiBase
::PARAM_ISMULTI
=> true,
888 'submodules' => false,
889 'recursivesubmodules' => false,
895 protected function getExamplesMessages() {
898 => 'apihelp-help-example-main',
899 'action=help&modules=query&submodules=1'
900 => 'apihelp-help-example-submodules',
901 'action=help&recursivesubmodules=1'
902 => 'apihelp-help-example-recursive',
903 'action=help&modules=help'
904 => 'apihelp-help-example-help',
905 'action=help&modules=query+info|query+categorymembers'
906 => 'apihelp-help-example-query',
910 public function getHelpUrls() {
912 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Main_page',
913 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:FAQ',
914 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Quick_start_guide',